File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.84.2.7: download - view: text, annotated - select for diffs
Wed Nov 1 03:00:31 2017 UTC (6 years, 8 months ago) by raeburn
Branches: version_2_11_2_msu
Diff to branchpoint 1.160.6.84: preferred, unified
- For 2.11.2 (modified).
  - Include changes in rev. 1.308, 1.312, 1.313.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.160.6.84.2.7 2017/11/01 03:00:31 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, and textbook).  In each case the radio buttons 
  108: 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 File::Copy;
  174: use Locale::Language;
  175: use DateTime::TimeZone;
  176: use DateTime::Locale;
  177: 
  178: my $registered_cleanup;
  179: my $modified_urls;
  180: 
  181: sub handler {
  182:     my $r=shift;
  183:     if ($r->header_only) {
  184:         &Apache::loncommon::content_type($r,'text/html');
  185:         $r->send_http_header;
  186:         return OK;
  187:     }
  188: 
  189:     my $context = 'domain';
  190:     my $dom = $env{'request.role.domain'};
  191:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  192:     if (&Apache::lonnet::allowed('mau',$dom)) {
  193:         &Apache::loncommon::content_type($r,'text/html');
  194:         $r->send_http_header;
  195:     } else {
  196:         $env{'user.error.msg'}=
  197:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  198:         return HTTP_NOT_ACCEPTABLE;
  199:     }
  200: 
  201:     $registered_cleanup=0;
  202:     @{$modified_urls}=();
  203: 
  204:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  205:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  206:                                             ['phase','actions']);
  207:     my $phase = 'pickactions';
  208:     if ( exists($env{'form.phase'}) ) {
  209:         $phase = $env{'form.phase'};
  210:     }
  211:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  212:     my %domconfig =
  213:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  214:                 'quotas','autoenroll','autoupdate','autocreate',
  215:                 'directorysrch','usercreation','usermodification',
  216:                 'contacts','defaults','scantron','coursecategories',
  217:                 'serverstatuses','requestcourses','helpsettings',
  218:                 'coursedefaults','usersessions','loadbalancing',
  219:                 'requestauthor','selfenrollment','inststatus',
  220:                 'ltitools'],$dom);
  221:     if (ref($domconfig{'ltitools'}) eq 'HASH') {
  222:         my %encconfig =
  223:             &Apache::lonnet::get_dom('encconfig',['ltitools'],$dom);
  224:         if (ref($encconfig{'ltitools'}) eq 'HASH') {
  225:             foreach my $id (keys(%{$domconfig{'ltitools'}})) {
  226:                 if (ref($domconfig{'ltitools'}{$id}) eq 'HASH') {
  227:                     foreach my $item ('key','secret') {
  228:                         $domconfig{'ltitools'}{$id}{$item} = $encconfig{'ltitools'}{$id}{$item};
  229:                     }
  230:                 }
  231:             }
  232:         }
  233:     }
  234:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  235:                        'autoupdate','autocreate','directorysrch','contacts',
  236:                        'usercreation','selfcreation','usermodification','scantron',
  237:                        'requestcourses','requestauthor','coursecategories',
  238:                        'serverstatuses','helpsettings','coursedefaults',
  239:                        'ltitools','selfenrollment','usersessions');
  240:     my %existing;
  241:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  242:         %existing = %{$domconfig{'loadbalancing'}};
  243:     }
  244:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  245:         push(@prefs_order,'loadbalancing');
  246:     }
  247:     my %prefs = (
  248:         'rolecolors' =>
  249:                    { text => 'Default color schemes',
  250:                      help => 'Domain_Configuration_Color_Schemes',
  251:                      header => [{col1 => 'Student Settings',
  252:                                  col2 => '',},
  253:                                 {col1 => 'Coordinator Settings',
  254:                                  col2 => '',},
  255:                                 {col1 => 'Author Settings',
  256:                                  col2 => '',},
  257:                                 {col1 => 'Administrator Settings',
  258:                                  col2 => '',}],
  259:                       print => \&print_rolecolors,
  260:                       modify => \&modify_rolecolors,
  261:                     },
  262:         'login' =>
  263:                     { text => 'Log-in page options',
  264:                       help => 'Domain_Configuration_Login_Page',
  265:                       header => [{col1 => 'Log-in Page Items',
  266:                                   col2 => '',},
  267:                                  {col1 => 'Log-in Help',
  268:                                   col2 => 'Value'},
  269:                                  {col1 => 'Custom HTML in document head',
  270:                                   col2 => 'Value'}],
  271:                       print => \&print_login,
  272:                       modify => \&modify_login,
  273:                     },
  274:         'defaults' => 
  275:                     { text => 'Default authentication/language/timezone/portal/types',
  276:                       help => 'Domain_Configuration_LangTZAuth',
  277:                       header => [{col1 => 'Setting',
  278:                                   col2 => 'Value'},
  279:                                  {col1 => 'Internal Authentication',
  280:                                   col2 => 'Value'},
  281:                                  {col1 => 'Institutional user types',
  282:                                   col2 => 'Assignable to e-mail usernames'}],
  283:                       print => \&print_defaults,
  284:                       modify => \&modify_defaults,
  285:                     },
  286:         'quotas' => 
  287:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  288:                       help => 'Domain_Configuration_Quotas',
  289:                       header => [{col1 => 'User affiliation',
  290:                                   col2 => 'Available tools',
  291:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  292:                       print => \&print_quotas,
  293:                       modify => \&modify_quotas,
  294:                     },
  295:         'autoenroll' =>
  296:                    { text => 'Auto-enrollment settings',
  297:                      help => 'Domain_Configuration_Auto_Enrollment',
  298:                      header => [{col1 => 'Configuration setting',
  299:                                  col2 => 'Value(s)'}],
  300:                      print => \&print_autoenroll,
  301:                      modify => \&modify_autoenroll,
  302:                    },
  303:         'autoupdate' => 
  304:                    { text => 'Auto-update settings',
  305:                      help => 'Domain_Configuration_Auto_Updates',
  306:                      header => [{col1 => 'Setting',
  307:                                  col2 => 'Value',},
  308:                                 {col1 => 'Setting',
  309:                                  col2 => 'Affiliation'},
  310:                                 {col1 => 'User population',
  311:                                  col2 => 'Updatable user data'}],
  312:                      print => \&print_autoupdate,
  313:                      modify => \&modify_autoupdate,
  314:                   },
  315:         'autocreate' => 
  316:                   { text => 'Auto-course creation settings',
  317:                      help => 'Domain_Configuration_Auto_Creation',
  318:                      header => [{col1 => 'Configuration Setting',
  319:                                  col2 => 'Value',}],
  320:                      print => \&print_autocreate,
  321:                      modify => \&modify_autocreate,
  322:                   },
  323:         'directorysrch' => 
  324:                   { text => 'Directory searches',
  325:                     help => 'Domain_Configuration_InstDirectory_Search',
  326:                     header => [{col1 => 'Institutional Directory Setting',
  327:                                 col2 => 'Value',},
  328:                                {col1 => 'LON-CAPA Directory Setting',
  329:                                 col2 => 'Value',}],
  330:                     print => \&print_directorysrch,
  331:                     modify => \&modify_directorysrch,
  332:                   },
  333:         'contacts' =>
  334:                   { text => 'E-mail addresses and helpform',
  335:                     help => 'Domain_Configuration_Contact_Info',
  336:                     header => [{col1 => 'Default e-mail addresses',
  337:                                 col2 => 'Value',},
  338:                                {col1 => 'Recipient(s) for notifications',
  339:                                 col2 => 'Value',},
  340:                                {col1 => 'Ask helpdesk form settings',
  341:                                 col2 => 'Value',},],
  342:                     print => \&print_contacts,
  343:                     modify => \&modify_contacts,
  344:                   },
  345:         'usercreation' => 
  346:                   { text => 'User creation',
  347:                     help => 'Domain_Configuration_User_Creation',
  348:                     header => [{col1 => 'Format rule type',
  349:                                 col2 => 'Format rules in force'},
  350:                                {col1 => 'User account creation',
  351:                                 col2 => 'Usernames which may be created',},
  352:                                {col1 => 'Context',
  353:                                 col2 => 'Assignable authentication types'}],
  354:                     print => \&print_usercreation,
  355:                     modify => \&modify_usercreation,
  356:                   },
  357:         'selfcreation' => 
  358:                   { text => 'Users self-creating accounts',
  359:                     help => 'Domain_Configuration_Self_Creation', 
  360:                     header => [{col1 => 'Self-creation with institutional username',
  361:                                 col2 => 'Enabled?'},
  362:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  363:                                 col2 => 'Information user can enter'},
  364:                                {col1 => 'Self-creation with e-mail as username',
  365:                                 col2 => 'Settings'}],
  366:                     print => \&print_selfcreation,
  367:                     modify => \&modify_selfcreation,
  368:                   },
  369:         'usermodification' =>
  370:                   { text => 'User modification',
  371:                     help => 'Domain_Configuration_User_Modification',
  372:                     header => [{col1 => 'Target user has role',
  373:                                 col2 => 'User information updatable in author context'},
  374:                                {col1 => 'Target user has role',
  375:                                 col2 => 'User information updatable in course context'}],
  376:                     print => \&print_usermodification,
  377:                     modify => \&modify_usermodification,
  378:                   },
  379:         'scantron' =>
  380:                   { text => 'Bubblesheet format file',
  381:                     help => 'Domain_Configuration_Scantron_Format',
  382:                     header => [ {col1 => 'Item',
  383:                                  col2 => '',
  384:                               }],
  385:                     print => \&print_scantron,
  386:                     modify => \&modify_scantron,
  387:                   },
  388:         'requestcourses' => 
  389:                  {text => 'Request creation of courses',
  390:                   help => 'Domain_Configuration_Request_Courses',
  391:                   header => [{col1 => 'User affiliation',
  392:                               col2 => 'Availability/Processing of requests',},
  393:                              {col1 => 'Setting',
  394:                               col2 => 'Value'},
  395:                              {col1 => 'Available textbooks',
  396:                               col2 => ''},
  397:                              {col1 => 'Available templates',
  398:                               col2 => ''},
  399:                              {col1 => 'Validation (not official courses)',
  400:                               col2 => 'Value'},],
  401:                   print => \&print_quotas,
  402:                   modify => \&modify_quotas,
  403:                  },
  404:         'requestauthor' =>
  405:                  {text => 'Request Authoring Space',
  406:                   help => 'Domain_Configuration_Request_Author',
  407:                   header => [{col1 => 'User affiliation',
  408:                               col2 => 'Availability/Processing of requests',},
  409:                              {col1 => 'Setting',
  410:                               col2 => 'Value'}],
  411:                   print => \&print_quotas,
  412:                   modify => \&modify_quotas,
  413:                  },
  414:         'coursecategories' =>
  415:                   { text => 'Cataloging of courses/communities',
  416:                     help => 'Domain_Configuration_Cataloging_Courses',
  417:                     header => [{col1 => 'Catalog type/availability',
  418:                                 col2 => '',},
  419:                                {col1 => 'Category settings for standard catalog',
  420:                                 col2 => '',},
  421:                                {col1 => 'Categories',
  422:                                 col2 => '',
  423:                                }],
  424:                     print => \&print_coursecategories,
  425:                     modify => \&modify_coursecategories,
  426:                   },
  427:         'serverstatuses' =>
  428:                  {text   => 'Access to server status pages',
  429:                   help   => 'Domain_Configuration_Server_Status',
  430:                   header => [{col1 => 'Status Page',
  431:                               col2 => 'Other named users',
  432:                               col3 => 'Specific IPs',
  433:                             }],
  434:                   print => \&print_serverstatuses,
  435:                   modify => \&modify_serverstatuses,
  436:                  },
  437:         'helpsettings' =>
  438:                  {text   => 'Support settings',
  439:                   help   => 'Domain_Configuration_Help_Settings',
  440:                   header => [{col1 => 'Help Page Settings (logged-in users)',
  441:                               col2 => 'Value'},
  442:                              {col1 => 'Helpdesk Roles',
  443:                               col2 => 'Settings'},],
  444:                   print  => \&print_helpsettings,
  445:                   modify => \&modify_helpsettings,
  446:                  },
  447:         'coursedefaults' => 
  448:                  {text => 'Course/Community defaults',
  449:                   help => 'Domain_Configuration_Course_Defaults',
  450:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  451:                               col2 => 'Value',},
  452:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  453:                               col2 => 'Value',},],
  454:                   print => \&print_coursedefaults,
  455:                   modify => \&modify_coursedefaults,
  456:                  },
  457:         'selfenrollment' => 
  458:                  {text   => 'Self-enrollment in Course/Community',
  459:                   help   => 'Domain_Configuration_Selfenrollment',
  460:                   header => [{col1 => 'Configuration Rights',
  461:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  462:                              {col1 => 'Defaults',
  463:                               col2 => 'Value'},
  464:                              {col1 => 'Self-enrollment validation (optional)',
  465:                               col2 => 'Value'},],
  466:                   print => \&print_selfenrollment,
  467:                   modify => \&modify_selfenrollment,
  468:                  },
  469:         'usersessions' =>
  470:                  {text  => 'User session hosting/offloading',
  471:                   help  => 'Domain_Configuration_User_Sessions',
  472:                   header => [{col1 => 'Domain server',
  473:                               col2 => 'Servers to offload sessions to when busy'},
  474:                              {col1 => 'Hosting of users from other domains',
  475:                               col2 => 'Rules'},
  476:                              {col1 => "Hosting domain's own users elsewhere",
  477:                               col2 => 'Rules'}],
  478:                   print => \&print_usersessions,
  479:                   modify => \&modify_usersessions,
  480:                  },
  481:         'loadbalancing' =>
  482:                  {text  => 'Dedicated Load Balancer(s)',
  483:                   help  => 'Domain_Configuration_Load_Balancing',
  484:                   header => [{col1 => 'Balancers',
  485:                               col2 => 'Default destinations',
  486:                               col3 => 'User affiliation',
  487:                               col4 => 'Overrides'},
  488:                             ],
  489:                   print => \&print_loadbalancing,
  490:                   modify => \&modify_loadbalancing,
  491:                  },
  492:         'ltitools' =>
  493:                  {text => 'External Tools (LTI)',
  494:                   help => 'Domain_Configuration_LTI_Tools',
  495:                   header => [{col1 => 'Setting',
  496:                               col2 => 'Value',}],
  497:                   print => \&print_ltitools,
  498:                   modify => \&modify_ltitools,
  499:                  },
  500:     );
  501:     if (keys(%servers) > 1) {
  502:         $prefs{'login'}  = { text   => 'Log-in page options',
  503:                              help   => 'Domain_Configuration_Login_Page',
  504:                             header => [{col1 => 'Log-in Service',
  505:                                         col2 => 'Server Setting',},
  506:                                        {col1 => 'Log-in Page Items',
  507:                                         col2 => ''},
  508:                                        {col1 => 'Log-in Help',
  509:                                         col2 => 'Value'},
  510:                                        {col1 => 'Custom HTML in document head',
  511:                                         col2 => 'Value'}],
  512:                             print => \&print_login,
  513:                             modify => \&modify_login,
  514:                            };
  515:     }
  516: 
  517:     my @roles = ('student','coordinator','author','admin');
  518:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  519:     &Apache::lonhtmlcommon::add_breadcrumb
  520:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  521:       text=>"Settings to display/modify"});
  522:     my $confname = $dom.'-domainconfig';
  523: 
  524:     if ($phase eq 'process') {
  525:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  526:                                                               \%prefs,\%domconfig,$confname,\@roles);
  527:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  528:             $r->rflush();
  529:             &devalidate_remote_domconfs($dom,$result);
  530:         }
  531:     } elsif ($phase eq 'display') {
  532:         my $js = &recaptcha_js().
  533:                  &toggle_display_js();
  534:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  535:             my ($othertitle,$usertypes,$types) =
  536:                 &Apache::loncommon::sorted_inst_types($dom);
  537:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  538:                                           $domconfig{'loadbalancing'}).
  539:                    &new_spares_js().
  540:                    &common_domprefs_js().
  541:                    &Apache::loncommon::javascript_array_indexof();
  542:         }
  543:         if (grep(/^requestcourses$/,@actions)) {
  544:             my $javascript_validations;
  545:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  546:             $js .= <<END;
  547: <script type="text/javascript">
  548: $javascript_validations
  549: </script>
  550: $coursebrowserjs
  551: END
  552:         }
  553:         if (grep(/^contacts$/,@actions)) {
  554:             $js .= &contacts_javascript();
  555:         }
  556:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  557:     } else {
  558: # check if domconfig user exists for the domain.
  559:         my $servadm = $r->dir_config('lonAdmEMail');
  560:         my ($configuserok,$author_ok,$switchserver) =
  561:             &config_check($dom,$confname,$servadm);
  562:         unless ($configuserok eq 'ok') {
  563:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  564:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  565:                           $confname).
  566:                       '<br />'
  567:             );
  568:             if ($switchserver) {
  569:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  570:                           '<br />'.
  571:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  572:                           '<br />'.
  573:                           &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).
  574:                           '<br />'.
  575:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  576:                 );
  577:             } else {
  578:                 $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.').
  579:                           '<br />'.
  580:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  581:                 );
  582:             }
  583:             $r->print(&Apache::loncommon::end_page());
  584:             return OK;
  585:         }
  586:         if (keys(%domconfig) == 0) {
  587:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  588:             my @ids=&Apache::lonnet::current_machine_ids();
  589:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  590:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  591:                 my @loginimages = ('img','logo','domlogo','login');
  592:                 my $custom_img_count = 0;
  593:                 foreach my $img (@loginimages) {
  594:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  595:                         $custom_img_count ++;
  596:                     }
  597:                 }
  598:                 foreach my $role (@roles) {
  599:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  600:                         $custom_img_count ++;
  601:                     }
  602:                 }
  603:                 if ($custom_img_count > 0) {
  604:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  605:                     my $switch_server = &check_switchserver($dom,$confname);
  606:                     $r->print(
  607:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  608:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  609:     &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 />'.
  610:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  611:                     if ($switch_server) {
  612:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  613:                     }
  614:                     $r->print(&Apache::loncommon::end_page());
  615:                     return OK;
  616:                 }
  617:             }
  618:         }
  619:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  620:     }
  621:     return OK;
  622: }
  623: 
  624: sub process_changes {
  625:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  626:     my %domconfig;
  627:     if (ref($values) eq 'HASH') {
  628:         %domconfig = %{$values};
  629:     }
  630:     my $output;
  631:     if ($action eq 'login') {
  632:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  633:     } elsif ($action eq 'rolecolors') {
  634:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  635:                                      $lastactref,%domconfig);
  636:     } elsif ($action eq 'quotas') {
  637:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  638:     } elsif ($action eq 'autoenroll') {
  639:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  640:     } elsif ($action eq 'autoupdate') {
  641:         $output = &modify_autoupdate($dom,%domconfig);
  642:     } elsif ($action eq 'autocreate') {
  643:         $output = &modify_autocreate($dom,%domconfig);
  644:     } elsif ($action eq 'directorysrch') {
  645:         $output = &modify_directorysrch($dom,$lastactref,%domconfig);
  646:     } elsif ($action eq 'usercreation') {
  647:         $output = &modify_usercreation($dom,%domconfig);
  648:     } elsif ($action eq 'selfcreation') {
  649:         $output = &modify_selfcreation($dom,%domconfig);
  650:     } elsif ($action eq 'usermodification') {
  651:         $output = &modify_usermodification($dom,%domconfig);
  652:     } elsif ($action eq 'contacts') {
  653:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  654:     } elsif ($action eq 'defaults') {
  655:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  656:     } elsif ($action eq 'scantron') {
  657:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  658:     } elsif ($action eq 'coursecategories') {
  659:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  660:     } elsif ($action eq 'serverstatuses') {
  661:         $output = &modify_serverstatuses($dom,%domconfig);
  662:     } elsif ($action eq 'requestcourses') {
  663:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  664:     } elsif ($action eq 'requestauthor') {
  665:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  666:     } elsif ($action eq 'helpsettings') {
  667:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  668:     } elsif ($action eq 'coursedefaults') {
  669:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  670:     } elsif ($action eq 'selfenrollment') {
  671:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  672:     } elsif ($action eq 'usersessions') {
  673:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  674:     } elsif ($action eq 'loadbalancing') {
  675:         $output = &modify_loadbalancing($dom,%domconfig);
  676:     } elsif ($action eq 'ltitools') {
  677:         $output = &modify_ltitools($r,$dom,$action,$lastactref,%domconfig);
  678:     }
  679:     return $output;
  680: }
  681: 
  682: sub print_config_box {
  683:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  684:     my $rowtotal = 0;
  685:     my $output;
  686:     if ($action eq 'coursecategories') {
  687:         $output = &coursecategories_javascript($settings);
  688:     } elsif ($action eq 'defaults') {
  689:         $output = &defaults_javascript($settings); 
  690:     } elsif ($action eq 'helpsettings') {
  691:         my (%privs,%levelscurrent);
  692:         my %full=();
  693:         my %levels=(
  694:                      course => {},
  695:                      domain => {},
  696:                      system => {},
  697:                    );
  698:         my $context = 'domain';
  699:         my $crstype = 'Course';
  700:         my $formname = 'display';
  701:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  702:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  703:         $output =
  704:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full,
  705:                                                       \@templateroles);
  706:     }
  707:     $output .=
  708:          '<table class="LC_nested_outer">
  709:           <tr>
  710:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  711:            &mt($item->{text}).'&nbsp;'.
  712:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  713:           '</tr>';
  714:     $rowtotal ++;
  715:     my $numheaders = 1;
  716:     if (ref($item->{'header'}) eq 'ARRAY') {
  717:         $numheaders = scalar(@{$item->{'header'}});
  718:     }
  719:     if ($numheaders > 1) {
  720:         my $colspan = '';
  721:         my $rightcolspan = '';
  722:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  723:             ($action eq 'directorysrch') ||
  724:             (($action eq 'login') && ($numheaders < 4))) {
  725:             $colspan = ' colspan="2"';
  726:         }
  727:         if ($action eq 'usersessions') {
  728:             $rightcolspan = ' colspan="3"'; 
  729:         }
  730:         $output .= '
  731:           <tr>
  732:            <td>
  733:             <table class="LC_nested">
  734:              <tr class="LC_info_row">
  735:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  736:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  737:              </tr>';
  738:         $rowtotal ++;
  739:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  740:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  741:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'directorysrch') ||
  742:             ($action eq 'helpsettings') || ($action eq 'contacts')) {
  743:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  744:         } elsif ($action eq 'coursecategories') {
  745:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  746:         } elsif ($action eq 'login') {
  747:             if ($numheaders == 4) {
  748:                 $colspan = ' colspan="2"';
  749:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  750:             } else {
  751:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  752:             }
  753:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  754:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  755:         } elsif ($action eq 'rolecolors') {
  756:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  757:         }
  758:         $output .= '
  759:            </table>
  760:           </td>
  761:          </tr>
  762:          <tr>
  763:            <td>
  764:             <table class="LC_nested">
  765:              <tr class="LC_info_row">
  766:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  767:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  768:              </tr>';
  769:             $rowtotal ++;
  770:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  771:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  772:             ($action eq 'usersessions') || ($action eq 'coursecategories') ||
  773:             ($action eq 'contacts') || ($action eq 'defaults')) {
  774:             if ($action eq 'coursecategories') {
  775:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  776:                 $colspan = ' colspan="2"';
  777:             } else {
  778:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  779:             }
  780:             $output .= '
  781:            </table>
  782:           </td>
  783:          </tr>
  784:          <tr>
  785:            <td>
  786:             <table class="LC_nested">
  787:              <tr class="LC_info_row">
  788:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  789:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  790:              </tr>'."\n";
  791:             if ($action eq 'coursecategories') {
  792:                 $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  793:             } else {
  794:                 $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  795:             }
  796:             $rowtotal ++;
  797:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  798:                  ($action eq 'defaults') || ($action eq 'directorysrch') ||
  799:                  ($action eq 'helpsettings')) {
  800:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  801:         } elsif ($action eq 'login') {
  802:             if ($numheaders == 4) {
  803:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  804:            </table>
  805:           </td>
  806:          </tr>
  807:          <tr>
  808:            <td>
  809:             <table class="LC_nested">
  810:              <tr class="LC_info_row">
  811:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  812:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  813:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  814:                 $rowtotal ++;
  815:             } else {
  816:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  817:             }
  818:             $output .= '
  819:            </table>
  820:           </td>
  821:          </tr>
  822:          <tr>
  823:            <td>
  824:             <table class="LC_nested">
  825:              <tr class="LC_info_row">';
  826:             if ($numheaders == 4) {
  827:                 $output .= '
  828:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  829:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  830:              </tr>';
  831:             } else {
  832:                 $output .= '
  833:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  834:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  835:              </tr>';
  836:             }
  837:             $rowtotal ++;
  838:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal);
  839:         } elsif ($action eq 'requestcourses') {
  840:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  841:             $rowtotal ++;
  842:             $output .= &print_studentcode($settings,\$rowtotal).'
  843:            </table>
  844:           </td>
  845:          </tr>
  846:          <tr>
  847:            <td>
  848:             <table class="LC_nested">
  849:              <tr class="LC_info_row">
  850:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  851:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  852:                        &textbookcourses_javascript($settings).
  853:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
  854:             </table>
  855:            </td>
  856:           </tr>
  857:          <tr>
  858:            <td>
  859:             <table class="LC_nested">
  860:              <tr class="LC_info_row">
  861:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  862:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
  863:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
  864:             </table>
  865:            </td>
  866:           </tr>
  867:           <tr>
  868:            <td>
  869:             <table class="LC_nested">
  870:              <tr class="LC_info_row">
  871:               <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
  872:               <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
  873:              </tr>'.
  874:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
  875:         } elsif ($action eq 'requestauthor') {
  876:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  877:             $rowtotal ++;
  878:         } elsif ($action eq 'rolecolors') {
  879:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  880:            </table>
  881:           </td>
  882:          </tr>
  883:          <tr>
  884:            <td>
  885:             <table class="LC_nested">
  886:              <tr class="LC_info_row">
  887:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  888:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  889:               <td class="LC_right_item" valign="top">'.
  890:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  891:              </tr>'.
  892:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  893:            </table>
  894:           </td>
  895:          </tr>
  896:          <tr>
  897:            <td>
  898:             <table class="LC_nested">
  899:              <tr class="LC_info_row">
  900:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  901:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  902:              </tr>'.
  903:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  904:             $rowtotal += 2;
  905:         }
  906:     } else {
  907:         $output .= '
  908:           <tr>
  909:            <td>
  910:             <table class="LC_nested">
  911:              <tr class="LC_info_row">';
  912:         if ($action eq 'login') {
  913:             $output .= '  
  914:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  915:         } elsif ($action eq 'serverstatuses') {
  916:             $output .= '
  917:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  918:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  919: 
  920:         } else {
  921:             $output .= '
  922:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  923:         }
  924:         if (defined($item->{'header'}->[0]->{'col3'})) {
  925:             $output .= '<td class="LC_left_item" valign="top">'.
  926:                        &mt($item->{'header'}->[0]->{'col2'});
  927:             if ($action eq 'serverstatuses') {
  928:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  929:             } 
  930:         } else {
  931:             $output .= '<td class="LC_right_item" valign="top">'.
  932:                        &mt($item->{'header'}->[0]->{'col2'});
  933:         }
  934:         $output .= '</td>';
  935:         if ($item->{'header'}->[0]->{'col3'}) {
  936:             if (defined($item->{'header'}->[0]->{'col4'})) {
  937:                 $output .= '<td class="LC_left_item" valign="top">'.
  938:                             &mt($item->{'header'}->[0]->{'col3'});
  939:             } else {
  940:                 $output .= '<td class="LC_right_item" valign="top">'.
  941:                            &mt($item->{'header'}->[0]->{'col3'});
  942:             }
  943:             if ($action eq 'serverstatuses') {
  944:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  945:             }
  946:             $output .= '</td>';
  947:         }
  948:         if ($item->{'header'}->[0]->{'col4'}) {
  949:             $output .= '<td class="LC_right_item" valign="top">'.
  950:                        &mt($item->{'header'}->[0]->{'col4'});
  951:         }
  952:         $output .= '</tr>';
  953:         $rowtotal ++;
  954:         if ($action eq 'quotas') {
  955:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  956:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
  957:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing') ||
  958:                  ($action eq 'ltitools')) {
  959:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
  960:         } elsif ($action eq 'scantron') {
  961:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  962:         }
  963:     }
  964:     $output .= '
  965:    </table>
  966:   </td>
  967:  </tr>
  968: </table><br />';
  969:     return ($output,$rowtotal);
  970: }
  971: 
  972: sub print_login {
  973:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  974:     my ($css_class,$datatable);
  975:     my %choices = &login_choices();
  976: 
  977:     if ($caller eq 'service') {
  978:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  979:         my $choice = $choices{'disallowlogin'};
  980:         $css_class = ' class="LC_odd_row"';
  981:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  982:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  983:                       '<th>'.$choices{'server'}.'</th>'.
  984:                       '<th>'.$choices{'serverpath'}.'</th>'.
  985:                       '<th>'.$choices{'custompath'}.'</th>'.
  986:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  987:         my %disallowed;
  988:         if (ref($settings) eq 'HASH') {
  989:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  990:                %disallowed = %{$settings->{'loginvia'}};
  991:             }
  992:         }
  993:         foreach my $lonhost (sort(keys(%servers))) {
  994:             my $direct = 'selected="selected"';
  995:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  996:                 if ($disallowed{$lonhost}{'server'} ne '') {
  997:                     $direct = '';
  998:                 }
  999:             }
 1000:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1001:                           '<td><select name="'.$lonhost.'_server">'.
 1002:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1003:                           '</option>';
 1004:             foreach my $hostid (sort(keys(%servers))) {
 1005:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1006:                 my $selected = '';
 1007:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1008:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1009:                         $selected = 'selected="selected"';
 1010:                     }
 1011:                 }
 1012:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1013:                               $servers{$hostid}.'</option>';
 1014:             }
 1015:             $datatable .= '</select></td>'.
 1016:                           '<td><select name="'.$lonhost.'_serverpath">';
 1017:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1018:                 my $pathname = $path;
 1019:                 if ($path eq 'custom') {
 1020:                     $pathname = &mt('Custom Path').' ->';
 1021:                 }
 1022:                 my $selected = '';
 1023:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1024:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1025:                         $selected = 'selected="selected"';
 1026:                     }
 1027:                 } elsif ($path eq '') {
 1028:                     $selected = 'selected="selected"';
 1029:                 }
 1030:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1031:             }
 1032:             $datatable .= '</select></td>';
 1033:             my ($custom,$exempt);
 1034:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1035:                 $custom = $disallowed{$lonhost}{'custompath'};
 1036:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1037:             }
 1038:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1039:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1040:                           '</tr>';
 1041:         }
 1042:         $datatable .= '</table></td></tr>';
 1043:         return $datatable;
 1044:     } elsif ($caller eq 'page') {
 1045:         my %defaultchecked = ( 
 1046:                                'coursecatalog' => 'on',
 1047:                                'helpdesk'      => 'on',
 1048:                                'adminmail'     => 'off',
 1049:                                'newuser'       => 'off',
 1050:                              );
 1051:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1052:         my (%checkedon,%checkedoff);
 1053:         foreach my $item (@toggles) {
 1054:             if ($defaultchecked{$item} eq 'on') { 
 1055:                 $checkedon{$item} = ' checked="checked" ';
 1056:                 $checkedoff{$item} = ' ';
 1057:             } elsif ($defaultchecked{$item} eq 'off') {
 1058:                 $checkedoff{$item} = ' checked="checked" ';
 1059:                 $checkedon{$item} = ' ';
 1060:             }
 1061:         }
 1062:         my @images = ('img','logo','domlogo','login');
 1063:         my @logintext = ('textcol','bgcol');
 1064:         my @bgs = ('pgbg','mainbg','sidebg');
 1065:         my @links = ('link','alink','vlink');
 1066:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1067:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1068:         my (%is_custom,%designs);
 1069:         my %defaults = (
 1070:                        font => $defaultdesign{'login.font'},
 1071:                        );
 1072:         foreach my $item (@images) {
 1073:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1074:             $defaults{'showlogo'}{$item} = 1;
 1075:         }
 1076:         foreach my $item (@bgs) {
 1077:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1078:         }
 1079:         foreach my $item (@logintext) {
 1080:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1081:         }
 1082:         foreach my $item (@links) {
 1083:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1084:         }
 1085:         if (ref($settings) eq 'HASH') {
 1086:             foreach my $item (@toggles) {
 1087:                 if ($settings->{$item} eq '1') {
 1088:                     $checkedon{$item} =  ' checked="checked" ';
 1089:                     $checkedoff{$item} = ' ';
 1090:                 } elsif ($settings->{$item} eq '0') {
 1091:                     $checkedoff{$item} =  ' checked="checked" ';
 1092:                     $checkedon{$item} = ' ';
 1093:                 }
 1094:             }
 1095:             foreach my $item (@images) {
 1096:                 if (defined($settings->{$item})) {
 1097:                     $designs{$item} = $settings->{$item};
 1098:                     $is_custom{$item} = 1;
 1099:                 }
 1100:                 if (defined($settings->{'showlogo'}{$item})) {
 1101:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1102:                 }
 1103:             }
 1104:             foreach my $item (@logintext) {
 1105:                 if ($settings->{$item} ne '') {
 1106:                     $designs{'logintext'}{$item} = $settings->{$item};
 1107:                     $is_custom{$item} = 1;
 1108:                 }
 1109:             }
 1110:             if ($settings->{'font'} ne '') {
 1111:                 $designs{'font'} = $settings->{'font'};
 1112:                 $is_custom{'font'} = 1;
 1113:             }
 1114:             foreach my $item (@bgs) {
 1115:                 if ($settings->{$item} ne '') {
 1116:                     $designs{'bgs'}{$item} = $settings->{$item};
 1117:                     $is_custom{$item} = 1;
 1118:                 }
 1119:             }
 1120:             foreach my $item (@links) {
 1121:                 if ($settings->{$item} ne '') {
 1122:                     $designs{'links'}{$item} = $settings->{$item};
 1123:                     $is_custom{$item} = 1;
 1124:                 }
 1125:             }
 1126:         } else {
 1127:             if ($designhash{$dom.'.login.font'} ne '') {
 1128:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1129:                 $is_custom{'font'} = 1;
 1130:             }
 1131:             foreach my $item (@images) {
 1132:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1133:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1134:                     $is_custom{$item} = 1;
 1135:                 }
 1136:             }
 1137:             foreach my $item (@bgs) {
 1138:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1139:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1140:                     $is_custom{$item} = 1;
 1141:                 }
 1142:             }
 1143:             foreach my $item (@links) {
 1144:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1145:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1146:                     $is_custom{$item} = 1;
 1147:                 }
 1148:             }
 1149:         }
 1150:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1151:                                                       logo => 'Institution Logo',
 1152:                                                       domlogo => 'Domain Logo',
 1153:                                                       login => 'Login box');
 1154:         my $itemcount = 1;
 1155:         foreach my $item (@toggles) {
 1156:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1157:             $datatable .=  
 1158:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1159:                 '</td><td>'.
 1160:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1161:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1162:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1163:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1164:                 '</tr>';
 1165:             $itemcount ++;
 1166:         }
 1167:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1168:         $datatable .= '</tr></table></td></tr>';
 1169:     } elsif ($caller eq 'help') {
 1170:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1171:         my $switchserver = &check_switchserver($dom,$confname);
 1172:         my $itemcount = 1;
 1173:         $defaulturl = '/adm/loginproblems.html';
 1174:         $defaulttype = 'default';
 1175:         %lt = &Apache::lonlocal::texthash (
 1176:                      del     => 'Delete?',
 1177:                      rep     => 'Replace:',
 1178:                      upl     => 'Upload:',
 1179:                      default => 'Default',
 1180:                      custom  => 'Custom',
 1181:                                              );
 1182:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1183:         my @currlangs;
 1184:         if (ref($settings) eq 'HASH') {
 1185:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1186:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1187:                     next if ($settings->{'helpurl'}{$key} eq '');
 1188:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1189:                     $type{$key} = 'custom';
 1190:                     unless ($key eq 'nolang') {
 1191:                         push(@currlangs,$key);
 1192:                     }
 1193:                 }
 1194:             } elsif ($settings->{'helpurl'} ne '') {
 1195:                 $type{'nolang'} = 'custom';
 1196:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1197:             }
 1198:         }
 1199:         foreach my $lang ('nolang',sort(@currlangs)) {
 1200:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1201:             $datatable .= '<tr'.$css_class.'>';
 1202:             if ($url{$lang} eq '') {
 1203:                 $url{$lang} = $defaulturl;
 1204:             }
 1205:             if ($type{$lang} eq '') {
 1206:                 $type{$lang} = $defaulttype;
 1207:             }
 1208:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1209:             if ($lang eq 'nolang') {
 1210:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1211:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1212:             } else {
 1213:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1214:                                   $langchoices{$lang},
 1215:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1216:             }
 1217:             $datatable .= '</span></td>'."\n".
 1218:                           '<td class="LC_left_item">';
 1219:             if ($type{$lang} eq 'custom') {
 1220:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1221:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1222:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1223:             } else {
 1224:                 $datatable .= $lt{'upl'};
 1225:             }
 1226:             $datatable .='<br />';
 1227:             if ($switchserver) {
 1228:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1229:             } else {
 1230:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1231:             }
 1232:             $datatable .= '</td></tr>';
 1233:             $itemcount ++;
 1234:         }
 1235:         my @addlangs;
 1236:         foreach my $lang (sort(keys(%langchoices))) {
 1237:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1238:             push(@addlangs,$lang);
 1239:         }
 1240:         if (@addlangs > 0) {
 1241:             my %toadd;
 1242:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1243:             $toadd{''} = &mt('Select');
 1244:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1245:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1246:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1247:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1248:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1249:             if ($switchserver) {
 1250:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1251:             } else {
 1252:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1253:             }
 1254:             $datatable .= '</td></tr>';
 1255:             $itemcount ++;
 1256:         }
 1257:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1258:     } elsif ($caller eq 'headtag') {
 1259:         my %domservers = &Apache::lonnet::get_servers($dom);
 1260:         my $choice = $choices{'headtag'};
 1261:         $css_class = ' class="LC_odd_row"';
 1262:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1263:                       '<td align="left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1264:                       '<th>'.$choices{'current'}.'</th>'.
 1265:                       '<th>'.$choices{'action'}.'</th>'.
 1266:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1267:         my (%currurls,%currexempt);
 1268:         if (ref($settings) eq 'HASH') {
 1269:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1270:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1271:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1272:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1273:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1274:                     }
 1275:                 }
 1276:             }
 1277:         }
 1278:         my %lt = &Apache::lonlocal::texthash(
 1279:                                                del  => 'Delete?',
 1280:                                                rep  => 'Replace:',
 1281:                                                upl  => 'Upload:',
 1282:                                                curr => 'View contents',
 1283:                                                none => 'None',
 1284:         );
 1285:         my $switchserver = &check_switchserver($dom,$confname);
 1286:         foreach my $lonhost (sort(keys(%domservers))) {
 1287:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1288:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1289:             if ($currurls{$lonhost}) {
 1290:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1291:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1292:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1293:                               '">'.$lt{'curr'}.'</a></td>'.
 1294:                               '<td><span class="LC_nobreak"><label>'.
 1295:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1296:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1297:             } else {
 1298:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1299:             }
 1300:             $datatable .='<br />';
 1301:             if ($switchserver) {
 1302:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1303:             } else {
 1304:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1305:             }
 1306:             $datatable .= '</td><td><input type="textbox" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1307:         }
 1308:         $datatable .= '</table></td></tr>';
 1309:     }
 1310:     return $datatable;
 1311: }
 1312: 
 1313: sub login_choices {
 1314:     my %choices =
 1315:         &Apache::lonlocal::texthash (
 1316:             coursecatalog => 'Display Course/Community Catalog link?',
 1317:             adminmail     => "Display Administrator's E-mail Address?",
 1318:             helpdesk      => 'Display "Contact Helpdesk" link',
 1319:             disallowlogin => "Login page requests redirected",
 1320:             hostid        => "Server",
 1321:             server        => "Redirect to:",
 1322:             serverpath    => "Path",
 1323:             custompath    => "Custom", 
 1324:             exempt        => "Exempt IP(s)",
 1325:             directlogin   => "No redirect",
 1326:             newuser       => "Link to create a user account",
 1327:             img           => "Header",
 1328:             logo          => "Main Logo",
 1329:             domlogo       => "Domain Logo",
 1330:             login         => "Log-in Header", 
 1331:             textcol       => "Text color",
 1332:             bgcol         => "Box color",
 1333:             bgs           => "Background colors",
 1334:             links         => "Link colors",
 1335:             font          => "Font color",
 1336:             pgbg          => "Header",
 1337:             mainbg        => "Page",
 1338:             sidebg        => "Login box",
 1339:             link          => "Link",
 1340:             alink         => "Active link",
 1341:             vlink         => "Visited link",
 1342:             headtag       => "Custom markup",
 1343:             action        => "Action",
 1344:             current       => "Current",
 1345:         );
 1346:     return %choices;
 1347: }
 1348: 
 1349: sub print_rolecolors {
 1350:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1351:     my %choices = &color_font_choices();
 1352:     my @bgs = ('pgbg','tabbg','sidebg');
 1353:     my @links = ('link','alink','vlink');
 1354:     my @images = ('img');
 1355:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1356:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1357:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1358:     my (%is_custom,%designs);
 1359:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1360:     if (ref($settings) eq 'HASH') {
 1361:         if (ref($settings->{$role}) eq 'HASH') {
 1362:             if ($settings->{$role}->{'img'} ne '') {
 1363:                 $designs{'img'} = $settings->{$role}->{'img'};
 1364:                 $is_custom{'img'} = 1;
 1365:             }
 1366:             if ($settings->{$role}->{'font'} ne '') {
 1367:                 $designs{'font'} = $settings->{$role}->{'font'};
 1368:                 $is_custom{'font'} = 1;
 1369:             }
 1370:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1371:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1372:                 $is_custom{'fontmenu'} = 1;
 1373:             }
 1374:             foreach my $item (@bgs) {
 1375:                 if ($settings->{$role}->{$item} ne '') {
 1376:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1377:                     $is_custom{$item} = 1;
 1378:                 }
 1379:             }
 1380:             foreach my $item (@links) {
 1381:                 if ($settings->{$role}->{$item} ne '') {
 1382:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1383:                     $is_custom{$item} = 1;
 1384:                 }
 1385:             }
 1386:         }
 1387:     } else {
 1388:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1389:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1390:             $is_custom{'img'} = 1;
 1391:         }
 1392:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1393:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1394:             $is_custom{'fontmenu'} = 1; 
 1395:         }
 1396:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1397:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1398:             $is_custom{'font'} = 1;
 1399:         }
 1400:         foreach my $item (@bgs) {
 1401:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1402:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1403:                 $is_custom{$item} = 1;
 1404:             
 1405:             }
 1406:         }
 1407:         foreach my $item (@links) {
 1408:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1409:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1410:                 $is_custom{$item} = 1;
 1411:             }
 1412:         }
 1413:     }
 1414:     my $itemcount = 1;
 1415:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1416:     $datatable .= '</tr></table></td></tr>';
 1417:     return $datatable;
 1418: }
 1419: 
 1420: sub role_defaults {
 1421:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1422:     my %defaults;
 1423:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1424:         return %defaults;
 1425:     }
 1426:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1427:     if ($role eq 'login') {
 1428:         %defaults = (
 1429:                        font => $defaultdesign{$role.'.font'},
 1430:                     );
 1431:         if (ref($logintext) eq 'ARRAY') {
 1432:             foreach my $item (@{$logintext}) {
 1433:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1434:             }
 1435:         }
 1436:         foreach my $item (@{$images}) {
 1437:             $defaults{'showlogo'}{$item} = 1;
 1438:         }
 1439:     } else {
 1440:         %defaults = (
 1441:                        img => $defaultdesign{$role.'.img'},
 1442:                        font => $defaultdesign{$role.'.font'},
 1443:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1444:                     );
 1445:     }
 1446:     foreach my $item (@{$bgs}) {
 1447:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1448:     }
 1449:     foreach my $item (@{$links}) {
 1450:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1451:     }
 1452:     foreach my $item (@{$images}) {
 1453:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1454:     }
 1455:     return %defaults;
 1456: }
 1457: 
 1458: sub display_color_options {
 1459:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1460:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1461:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1462:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1463:     my $datatable = '<tr'.$css_class.'>'.
 1464:         '<td>'.$choices->{'font'}.'</td>';
 1465:     if (!$is_custom->{'font'}) {
 1466:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1467:     } else {
 1468:         $datatable .= '<td>&nbsp;</td>';
 1469:     }
 1470:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1471: 
 1472:     $datatable .= '<td><span class="LC_nobreak">'.
 1473:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1474:                   ' value="'.$current_color.'" />&nbsp;'.
 1475:                   '&nbsp;</td></tr>';
 1476:     unless ($role eq 'login') { 
 1477:         $datatable .= '<tr'.$css_class.'>'.
 1478:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1479:         if (!$is_custom->{'fontmenu'}) {
 1480:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1481:         } else {
 1482:             $datatable .= '<td>&nbsp;</td>';
 1483:         }
 1484: 	$current_color = $designs->{'fontmenu'} ?
 1485: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1486:         $datatable .= '<td><span class="LC_nobreak">'.
 1487:                       '<input class="colorchooser" type="text" size="10" name="'
 1488: 		      .$role.'_fontmenu"'.
 1489:                       ' value="'.$current_color.'" />&nbsp;'.
 1490:                       '&nbsp;</td></tr>';
 1491:     }
 1492:     my $switchserver = &check_switchserver($dom,$confname);
 1493:     foreach my $img (@{$images}) {
 1494: 	$itemcount ++;
 1495:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1496:         $datatable .= '<tr'.$css_class.'>'.
 1497:                       '<td>'.$choices->{$img};
 1498:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1499:         if ($role eq 'login') {
 1500:             if ($img eq 'login') {
 1501:                 $login_hdr_pick =
 1502:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1503:                 $logincolors =
 1504:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1505:                                        $designs,$defaults);
 1506:             } elsif ($img ne 'domlogo') {
 1507:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1508:             }
 1509:         }
 1510:         $datatable .= '</td>';
 1511:         if ($designs->{$img} ne '') {
 1512:             $imgfile = $designs->{$img};
 1513: 	    $img_import = ($imgfile =~ m{^/adm/});
 1514:         } else {
 1515:             $imgfile = $defaults->{$img};
 1516:         }
 1517:         if ($imgfile) {
 1518:             my ($showfile,$fullsize);
 1519:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1520:                 my $urldir = $1;
 1521:                 my $filename = $2;
 1522:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1523:                 if (@info) {
 1524:                     my $thumbfile = 'tn-'.$filename;
 1525:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1526:                     if (@thumb) {
 1527:                         $showfile = $urldir.'/'.$thumbfile;
 1528:                     } else {
 1529:                         $showfile = $imgfile;
 1530:                     }
 1531:                 } else {
 1532:                     $showfile = '';
 1533:                 }
 1534:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1535:                 $showfile = $imgfile;
 1536:                 my $imgdir = $1;
 1537:                 my $filename = $2;
 1538:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1539:                     $showfile = "/$imgdir/tn-".$filename;
 1540:                 } else {
 1541:                     my $input = $londocroot.$imgfile;
 1542:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1543:                     if (!-e $output) {
 1544:                         my ($width,$height) = &thumb_dimensions();
 1545:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1546:                         if ($fullwidth ne '' && $fullheight ne '') {
 1547:                             if ($fullwidth > $width && $fullheight > $height) { 
 1548:                                 my $size = $width.'x'.$height;
 1549:                                 system("convert -sample $size $input $output");
 1550:                                 $showfile = "/$imgdir/tn-".$filename;
 1551:                             }
 1552:                         }
 1553:                     }
 1554:                 }
 1555:             }
 1556:             if ($showfile) {
 1557:                 if ($showfile =~ m{^/(adm|res)/}) {
 1558:                     if ($showfile =~ m{^/res/}) {
 1559:                         my $local_showfile =
 1560:                             &Apache::lonnet::filelocation('',$showfile);
 1561:                         &Apache::lonnet::repcopy($local_showfile);
 1562:                     }
 1563:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1564:                 }
 1565:                 if ($imgfile) {
 1566:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1567:                         if ($imgfile =~ m{^/res/}) {
 1568:                             my $local_imgfile =
 1569:                                 &Apache::lonnet::filelocation('',$imgfile);
 1570:                             &Apache::lonnet::repcopy($local_imgfile);
 1571:                         }
 1572:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1573:                     } else {
 1574:                         $fullsize = $imgfile;
 1575:                     }
 1576:                 }
 1577:                 $datatable .= '<td>';
 1578:                 if ($img eq 'login') {
 1579:                     $datatable .= $login_hdr_pick;
 1580:                 } 
 1581:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1582:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1583:             } else {
 1584:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1585:                               &mt('Upload:').'<br />';
 1586:             }
 1587:         } else {
 1588:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1589:                           &mt('Upload:').'<br />';
 1590:         }
 1591:         if ($switchserver) {
 1592:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1593:         } else {
 1594:             if ($img ne 'login') { # suppress file selection for Log-in header
 1595:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1596:             }
 1597:         }
 1598:         $datatable .= '</td></tr>';
 1599:     }
 1600:     $itemcount ++;
 1601:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1602:     $datatable .= '<tr'.$css_class.'>'.
 1603:                   '<td>'.$choices->{'bgs'}.'</td>';
 1604:     my $bgs_def;
 1605:     foreach my $item (@{$bgs}) {
 1606:         if (!$is_custom->{$item}) {
 1607:             $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>';
 1608:         }
 1609:     }
 1610:     if ($bgs_def) {
 1611:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1612:     } else {
 1613:         $datatable .= '<td>&nbsp;</td>';
 1614:     }
 1615:     $datatable .= '<td class="LC_right_item">'.
 1616:                   '<table border="0"><tr>';
 1617: 
 1618:     foreach my $item (@{$bgs}) {
 1619:         $datatable .= '<td align="center">'.$choices->{$item};
 1620: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1621:         if ($designs->{'bgs'}{$item}) {
 1622:             $datatable .= '&nbsp;';
 1623:         }
 1624:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1625:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1626:     }
 1627:     $datatable .= '</tr></table></td></tr>';
 1628:     $itemcount ++;
 1629:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1630:     $datatable .= '<tr'.$css_class.'>'.
 1631:                   '<td>'.$choices->{'links'}.'</td>';
 1632:     my $links_def;
 1633:     foreach my $item (@{$links}) {
 1634:         if (!$is_custom->{$item}) {
 1635:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1636:         }
 1637:     }
 1638:     if ($links_def) {
 1639:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1640:     } else {
 1641:         $datatable .= '<td>&nbsp;</td>';
 1642:     }
 1643:     $datatable .= '<td class="LC_right_item">'.
 1644:                   '<table border="0"><tr>';
 1645:     foreach my $item (@{$links}) {
 1646: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1647:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1648:         if ($designs->{'links'}{$item}) {
 1649:             $datatable.='&nbsp;';
 1650:         }
 1651:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1652:                       '" /></td>';
 1653:     }
 1654:     $$rowtotal += $itemcount;
 1655:     return $datatable;
 1656: }
 1657: 
 1658: sub logo_display_options {
 1659:     my ($img,$defaults,$designs) = @_;
 1660:     my $checkedon;
 1661:     if (ref($defaults) eq 'HASH') {
 1662:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1663:             if ($defaults->{'showlogo'}{$img}) {
 1664:                 $checkedon = 'checked="checked" ';     
 1665:             }
 1666:         } 
 1667:     }
 1668:     if (ref($designs) eq 'HASH') {
 1669:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1670:             if (defined($designs->{'showlogo'}{$img})) {
 1671:                 if ($designs->{'showlogo'}{$img} == 0) {
 1672:                     $checkedon = '';
 1673:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1674:                     $checkedon = 'checked="checked" ';
 1675:                 }
 1676:             }
 1677:         }
 1678:     }
 1679:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1680:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1681:            &mt('show').'</label>'."\n";
 1682: }
 1683: 
 1684: sub login_header_options  {
 1685:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1686:     my $output = '';
 1687:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1688:         $output .= &mt('Text default(s):').'<br />';
 1689:         if (!$is_custom->{'textcol'}) {
 1690:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1691:                        '&nbsp;&nbsp;&nbsp;';
 1692:         }
 1693:         if (!$is_custom->{'bgcol'}) {
 1694:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1695:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1696:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1697:         }
 1698:         $output .= '<br />';
 1699:     }
 1700:     $output .='<br />';
 1701:     return $output;
 1702: }
 1703: 
 1704: sub login_text_colors {
 1705:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1706:     my $color_menu = '<table border="0"><tr>';
 1707:     foreach my $item (@{$logintext}) {
 1708:         $color_menu .= '<td align="center">'.$choices->{$item};
 1709:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1710:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1711:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1712:     }
 1713:     $color_menu .= '</tr></table><br />';
 1714:     return $color_menu;
 1715: }
 1716: 
 1717: sub image_changes {
 1718:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1719:     my $output;
 1720:     if ($img eq 'login') {
 1721:             # suppress image for Log-in header
 1722:     } elsif (!$is_custom) {
 1723:         if ($img ne 'domlogo') {
 1724:             $output .= &mt('Default image:').'<br />';
 1725:         } else {
 1726:             $output .= &mt('Default in use:').'<br />';
 1727:         }
 1728:     }
 1729:     if ($img eq 'login') { # suppress image for Log-in header
 1730:         $output .= '<td>'.$logincolors;
 1731:     } else {
 1732:         if ($img_import) {
 1733:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1734:         }
 1735:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1736:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1737:         if ($is_custom) {
 1738:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1739:                        '<input type="checkbox" name="'.
 1740:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1741:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1742:         } else {
 1743:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1744:         }
 1745:     }
 1746:     return $output;
 1747: }
 1748: 
 1749: sub print_quotas {
 1750:     my ($dom,$settings,$rowtotal,$action) = @_;
 1751:     my $context;
 1752:     if ($action eq 'quotas') {
 1753:         $context = 'tools';
 1754:     } else {
 1755:         $context = $action;
 1756:     }
 1757:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1758:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1759:     my $typecount = 0;
 1760:     my ($css_class,%titles);
 1761:     if ($context eq 'requestcourses') {
 1762:         @usertools = ('official','unofficial','community','textbook');
 1763:         @options =('norequest','approval','validate','autolimit');
 1764:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1765:         %titles = &courserequest_titles();
 1766:     } elsif ($context eq 'requestauthor') {
 1767:         @usertools = ('author');
 1768:         @options = ('norequest','approval','automatic');
 1769:         %titles = &authorrequest_titles();
 1770:     } else {
 1771:         @usertools = ('aboutme','blog','webdav','portfolio');
 1772:         %titles = &tool_titles();
 1773:     }
 1774:     if (ref($types) eq 'ARRAY') {
 1775:         foreach my $type (@{$types}) {
 1776:             my ($currdefquota,$currauthorquota);
 1777:             unless (($context eq 'requestcourses') ||
 1778:                     ($context eq 'requestauthor')) {
 1779:                 if (ref($settings) eq 'HASH') {
 1780:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1781:                         $currdefquota = $settings->{defaultquota}->{$type};
 1782:                     } else {
 1783:                         $currdefquota = $settings->{$type};
 1784:                     }
 1785:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1786:                         $currauthorquota = $settings->{authorquota}->{$type};
 1787:                     }
 1788:                 }
 1789:             }
 1790:             if (defined($usertypes->{$type})) {
 1791:                 $typecount ++;
 1792:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1793:                 $datatable .= '<tr'.$css_class.'>'.
 1794:                               '<td>'.$usertypes->{$type}.'</td>'.
 1795:                               '<td class="LC_left_item">';
 1796:                 if ($context eq 'requestcourses') {
 1797:                     $datatable .= '<table><tr>';
 1798:                 }
 1799:                 my %cell;  
 1800:                 foreach my $item (@usertools) {
 1801:                     if ($context eq 'requestcourses') {
 1802:                         my ($curroption,$currlimit);
 1803:                         if (ref($settings) eq 'HASH') {
 1804:                             if (ref($settings->{$item}) eq 'HASH') {
 1805:                                 $curroption = $settings->{$item}->{$type};
 1806:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1807:                                     $currlimit = $1; 
 1808:                                 }
 1809:                             }
 1810:                         }
 1811:                         if (!$curroption) {
 1812:                             $curroption = 'norequest';
 1813:                         }
 1814:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1815:                         foreach my $option (@options) {
 1816:                             my $val = $option;
 1817:                             if ($option eq 'norequest') {
 1818:                                 $val = 0;  
 1819:                             }
 1820:                             if ($option eq 'validate') {
 1821:                                 my $canvalidate = 0;
 1822:                                 if (ref($validations{$item}) eq 'HASH') { 
 1823:                                     if ($validations{$item}{$type}) {
 1824:                                         $canvalidate = 1;
 1825:                                     }
 1826:                                 }
 1827:                                 next if (!$canvalidate);
 1828:                             }
 1829:                             my $checked = '';
 1830:                             if ($option eq $curroption) {
 1831:                                 $checked = ' checked="checked"';
 1832:                             } elsif ($option eq 'autolimit') {
 1833:                                 if ($curroption =~ /^autolimit/) {
 1834:                                     $checked = ' checked="checked"';
 1835:                                 }                       
 1836:                             } 
 1837:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1838:                                   '<input type="radio" name="crsreq_'.$item.
 1839:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1840:                                   $titles{$option}.'</label>';
 1841:                             if ($option eq 'autolimit') {
 1842:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1843:                                                 $item.'_limit_'.$type.'" size="1" '.
 1844:                                                 'value="'.$currlimit.'" />';
 1845:                             }
 1846:                             $cell{$item} .= '</span> ';
 1847:                             if ($option eq 'autolimit') {
 1848:                                 $cell{$item} .= $titles{'unlimited'};
 1849:                             }
 1850:                         }
 1851:                     } elsif ($context eq 'requestauthor') {
 1852:                         my $curroption;
 1853:                         if (ref($settings) eq 'HASH') {
 1854:                             $curroption = $settings->{$type};
 1855:                         }
 1856:                         if (!$curroption) {
 1857:                             $curroption = 'norequest';
 1858:                         }
 1859:                         foreach my $option (@options) {
 1860:                             my $val = $option;
 1861:                             if ($option eq 'norequest') {
 1862:                                 $val = 0;
 1863:                             }
 1864:                             my $checked = '';
 1865:                             if ($option eq $curroption) {
 1866:                                 $checked = ' checked="checked"';
 1867:                             }
 1868:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1869:                                   '<input type="radio" name="authorreq_'.$type.
 1870:                                   '" value="'.$val.'"'.$checked.' />'.
 1871:                                   $titles{$option}.'</label></span>&nbsp; ';
 1872:                         }
 1873:                     } else {
 1874:                         my $checked = 'checked="checked" ';
 1875:                         if (ref($settings) eq 'HASH') {
 1876:                             if (ref($settings->{$item}) eq 'HASH') {
 1877:                                 if ($settings->{$item}->{$type} == 0) {
 1878:                                     $checked = '';
 1879:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1880:                                     $checked =  'checked="checked" ';
 1881:                                 }
 1882:                             }
 1883:                         }
 1884:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1885:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1886:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1887:                                       '</label></span>&nbsp; ';
 1888:                     }
 1889:                 }
 1890:                 if ($context eq 'requestcourses') {
 1891:                     $datatable .= '</tr><tr>';
 1892:                     foreach my $item (@usertools) {
 1893:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1894:                     }
 1895:                     $datatable .= '</tr></table>';
 1896:                 }
 1897:                 $datatable .= '</td>';
 1898:                 unless (($context eq 'requestcourses') ||
 1899:                         ($context eq 'requestauthor')) {
 1900:                     $datatable .= 
 1901:                               '<td class="LC_right_item">'.
 1902:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1903:                               '<input type="text" name="quota_'.$type.
 1904:                               '" value="'.$currdefquota.
 1905:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1906:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1907:                               '<input type="text" name="authorquota_'.$type.
 1908:                               '" value="'.$currauthorquota.
 1909:                               '" size="5" /></span></td>';
 1910:                 }
 1911:                 $datatable .= '</tr>';
 1912:             }
 1913:         }
 1914:     }
 1915:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1916:         $defaultquota = '20';
 1917:         $authorquota = '500';
 1918:         if (ref($settings) eq 'HASH') {
 1919:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1920:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1921:             } elsif (defined($settings->{'default'})) {
 1922:                 $defaultquota = $settings->{'default'};
 1923:             }
 1924:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1925:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1926:             }
 1927:         }
 1928:     }
 1929:     $typecount ++;
 1930:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1931:     $datatable .= '<tr'.$css_class.'>'.
 1932:                   '<td>'.$othertitle.'</td>'.
 1933:                   '<td class="LC_left_item">';
 1934:     if ($context eq 'requestcourses') {
 1935:         $datatable .= '<table><tr>';
 1936:     }
 1937:     my %defcell;
 1938:     foreach my $item (@usertools) {
 1939:         if ($context eq 'requestcourses') {
 1940:             my ($curroption,$currlimit);
 1941:             if (ref($settings) eq 'HASH') {
 1942:                 if (ref($settings->{$item}) eq 'HASH') {
 1943:                     $curroption = $settings->{$item}->{'default'};
 1944:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1945:                         $currlimit = $1;
 1946:                     }
 1947:                 }
 1948:             }
 1949:             if (!$curroption) {
 1950:                 $curroption = 'norequest';
 1951:             }
 1952:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1953:             foreach my $option (@options) {
 1954:                 my $val = $option;
 1955:                 if ($option eq 'norequest') {
 1956:                     $val = 0;
 1957:                 }
 1958:                 if ($option eq 'validate') {
 1959:                     my $canvalidate = 0;
 1960:                     if (ref($validations{$item}) eq 'HASH') {
 1961:                         if ($validations{$item}{'default'}) {
 1962:                             $canvalidate = 1;
 1963:                         }
 1964:                     }
 1965:                     next if (!$canvalidate);
 1966:                 }
 1967:                 my $checked = '';
 1968:                 if ($option eq $curroption) {
 1969:                     $checked = ' checked="checked"';
 1970:                 } elsif ($option eq 'autolimit') {
 1971:                     if ($curroption =~ /^autolimit/) {
 1972:                         $checked = ' checked="checked"';
 1973:                     }
 1974:                 }
 1975:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1976:                                   '<input type="radio" name="crsreq_'.$item.
 1977:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1978:                                   $titles{$option}.'</label>';
 1979:                 if ($option eq 'autolimit') {
 1980:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1981:                                        $item.'_limit_default" size="1" '.
 1982:                                        'value="'.$currlimit.'" />';
 1983:                 }
 1984:                 $defcell{$item} .= '</span> ';
 1985:                 if ($option eq 'autolimit') {
 1986:                     $defcell{$item} .= $titles{'unlimited'};
 1987:                 }
 1988:             }
 1989:         } elsif ($context eq 'requestauthor') {
 1990:             my $curroption;
 1991:             if (ref($settings) eq 'HASH') {
 1992:                 $curroption = $settings->{'default'};
 1993:             }
 1994:             if (!$curroption) {
 1995:                 $curroption = 'norequest';
 1996:             }
 1997:             foreach my $option (@options) {
 1998:                 my $val = $option;
 1999:                 if ($option eq 'norequest') {
 2000:                     $val = 0;
 2001:                 }
 2002:                 my $checked = '';
 2003:                 if ($option eq $curroption) {
 2004:                     $checked = ' checked="checked"';
 2005:                 }
 2006:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2007:                               '<input type="radio" name="authorreq_default"'.
 2008:                               ' value="'.$val.'"'.$checked.' />'.
 2009:                               $titles{$option}.'</label></span>&nbsp; ';
 2010:             }
 2011:         } else {
 2012:             my $checked = 'checked="checked" ';
 2013:             if (ref($settings) eq 'HASH') {
 2014:                 if (ref($settings->{$item}) eq 'HASH') {
 2015:                     if ($settings->{$item}->{'default'} == 0) {
 2016:                         $checked = '';
 2017:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2018:                         $checked = 'checked="checked" ';
 2019:                     }
 2020:                 }
 2021:             }
 2022:             $datatable .= '<span class="LC_nobreak"><label>'.
 2023:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2024:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2025:                           '</label></span>&nbsp; ';
 2026:         }
 2027:     }
 2028:     if ($context eq 'requestcourses') {
 2029:         $datatable .= '</tr><tr>';
 2030:         foreach my $item (@usertools) {
 2031:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2032:         }
 2033:         $datatable .= '</tr></table>';
 2034:     }
 2035:     $datatable .= '</td>';
 2036:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2037:         $datatable .= '<td class="LC_right_item">'.
 2038:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2039:                       '<input type="text" name="defaultquota" value="'.
 2040:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2041:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2042:                       '<input type="text" name="authorquota" value="'.
 2043:                       $authorquota.'" size="5" /></span></td>';
 2044:     }
 2045:     $datatable .= '</tr>';
 2046:     $typecount ++;
 2047:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2048:     $datatable .= '<tr'.$css_class.'>'.
 2049:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2050:     if ($context eq 'requestcourses') {
 2051:         $datatable .= &mt('(overrides affiliation, if set)').
 2052:                       '</td>'.
 2053:                       '<td class="LC_left_item">'.
 2054:                       '<table><tr>';
 2055:     } else {
 2056:         $datatable .= &mt('(overrides affiliation, if checked)').
 2057:                       '</td>'.
 2058:                       '<td class="LC_left_item" colspan="2">'.
 2059:                       '<br />';
 2060:     }
 2061:     my %advcell;
 2062:     foreach my $item (@usertools) {
 2063:         if ($context eq 'requestcourses') {
 2064:             my ($curroption,$currlimit);
 2065:             if (ref($settings) eq 'HASH') {
 2066:                 if (ref($settings->{$item}) eq 'HASH') {
 2067:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2068:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2069:                         $currlimit = $1;
 2070:                     }
 2071:                 }
 2072:             }
 2073:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2074:             my $checked = '';
 2075:             if ($curroption eq '') {
 2076:                 $checked = ' checked="checked"';
 2077:             }
 2078:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2079:                                '<input type="radio" name="crsreq_'.$item.
 2080:                                '__LC_adv" value=""'.$checked.' />'.
 2081:                                &mt('No override set').'</label></span>&nbsp; ';
 2082:             foreach my $option (@options) {
 2083:                 my $val = $option;
 2084:                 if ($option eq 'norequest') {
 2085:                     $val = 0;
 2086:                 }
 2087:                 if ($option eq 'validate') {
 2088:                     my $canvalidate = 0;
 2089:                     if (ref($validations{$item}) eq 'HASH') {
 2090:                         if ($validations{$item}{'_LC_adv'}) {
 2091:                             $canvalidate = 1;
 2092:                         }
 2093:                     }
 2094:                     next if (!$canvalidate);
 2095:                 }
 2096:                 my $checked = '';
 2097:                 if ($val eq $curroption) {
 2098:                     $checked = ' checked="checked"';
 2099:                 } elsif ($option eq 'autolimit') {
 2100:                     if ($curroption =~ /^autolimit/) {
 2101:                         $checked = ' checked="checked"';
 2102:                     }
 2103:                 }
 2104:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2105:                                   '<input type="radio" name="crsreq_'.$item.
 2106:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2107:                                   $titles{$option}.'</label>';
 2108:                 if ($option eq 'autolimit') {
 2109:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2110:                                        $item.'_limit__LC_adv" size="1" '.
 2111:                                        'value="'.$currlimit.'" />';
 2112:                 }
 2113:                 $advcell{$item} .= '</span> ';
 2114:                 if ($option eq 'autolimit') {
 2115:                     $advcell{$item} .= $titles{'unlimited'};
 2116:                 }
 2117:             }
 2118:         } elsif ($context eq 'requestauthor') {
 2119:             my $curroption;
 2120:             if (ref($settings) eq 'HASH') {
 2121:                 $curroption = $settings->{'_LC_adv'};
 2122:             }
 2123:             my $checked = '';
 2124:             if ($curroption eq '') {
 2125:                 $checked = ' checked="checked"';
 2126:             }
 2127:             $datatable .= '<span class="LC_nobreak"><label>'.
 2128:                           '<input type="radio" name="authorreq__LC_adv"'.
 2129:                           ' value=""'.$checked.' />'.
 2130:                           &mt('No override set').'</label></span>&nbsp; ';
 2131:             foreach my $option (@options) {
 2132:                 my $val = $option;
 2133:                 if ($option eq 'norequest') {
 2134:                     $val = 0;
 2135:                 }
 2136:                 my $checked = '';
 2137:                 if ($val eq $curroption) {
 2138:                     $checked = ' checked="checked"';
 2139:                 }
 2140:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2141:                               '<input type="radio" name="authorreq__LC_adv"'.
 2142:                               ' value="'.$val.'"'.$checked.' />'.
 2143:                               $titles{$option}.'</label></span>&nbsp; ';
 2144:             }
 2145:         } else {
 2146:             my $checked = 'checked="checked" ';
 2147:             if (ref($settings) eq 'HASH') {
 2148:                 if (ref($settings->{$item}) eq 'HASH') {
 2149:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2150:                         $checked = '';
 2151:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2152:                         $checked = 'checked="checked" ';
 2153:                     }
 2154:                 }
 2155:             }
 2156:             $datatable .= '<span class="LC_nobreak"><label>'.
 2157:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2158:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2159:                           '</label></span>&nbsp; ';
 2160:         }
 2161:     }
 2162:     if ($context eq 'requestcourses') {
 2163:         $datatable .= '</tr><tr>';
 2164:         foreach my $item (@usertools) {
 2165:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2166:         }
 2167:         $datatable .= '</tr></table>';
 2168:     }
 2169:     $datatable .= '</td></tr>';
 2170:     $$rowtotal += $typecount;
 2171:     return $datatable;
 2172: }
 2173: 
 2174: sub print_requestmail {
 2175:     my ($dom,$action,$settings,$rowtotal) = @_;
 2176:     my ($now,$datatable,%currapp);
 2177:     $now = time;
 2178:     if (ref($settings) eq 'HASH') {
 2179:         if (ref($settings->{'notify'}) eq 'HASH') {
 2180:             if ($settings->{'notify'}{'approval'} ne '') {
 2181:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2182:             }
 2183:         }
 2184:     }
 2185:     my $numinrow = 2;
 2186:     my $css_class;
 2187:     $css_class = ($$rowtotal%2? ' class="LC_odd_row"':'');
 2188:     my $text;
 2189:     if ($action eq 'requestcourses') {
 2190:         $text = &mt('Receive notification of course requests requiring approval');
 2191:     } elsif ($action eq 'requestauthor') {
 2192:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2193:     } else {
 2194:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2195:     }
 2196:     $datatable = '<tr'.$css_class.'>'.
 2197:                  ' <td>'.$text.'</td>'.
 2198:                  ' <td class="LC_left_item">';
 2199:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2200:                                                  $action.'notifyapproval',%currapp);
 2201:     if ($numdc > 0) {
 2202:         $datatable .= $table;
 2203:     } else {
 2204:         $datatable .= &mt('There are no active Domain Coordinators');
 2205:     }
 2206:     $datatable .='</td></tr>';
 2207:     return $datatable;
 2208: }
 2209: 
 2210: sub print_studentcode {
 2211:     my ($settings,$rowtotal) = @_;
 2212:     my $rownum = 0; 
 2213:     my ($output,%current);
 2214:     my @crstypes = ('official','unofficial','community','textbook');
 2215:     if (ref($settings) eq 'HASH') {
 2216:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2217:             foreach my $type (@crstypes) {
 2218:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2219:             }
 2220:         }
 2221:     }
 2222:     $output .= '<tr>'.
 2223:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2224:                '<td class="LC_left_item">';
 2225:     foreach my $type (@crstypes) {
 2226:         my $check = ' ';
 2227:         if ($current{$type}) {
 2228:             $check = ' checked="checked" ';
 2229:         }
 2230:         $output .= '<span class="LC_nobreak"><label>'.
 2231:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2232:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2233:     }
 2234:     $output .= '</td></tr>';
 2235:     $$rowtotal ++;
 2236:     return $output;
 2237: }
 2238: 
 2239: sub print_textbookcourses {
 2240:     my ($dom,$type,$settings,$rowtotal) = @_;
 2241:     my $rownum = 0;
 2242:     my $css_class;
 2243:     my $itemcount = 1;
 2244:     my $maxnum = 0;
 2245:     my $bookshash;
 2246:     if (ref($settings) eq 'HASH') {
 2247:         $bookshash = $settings->{$type};
 2248:     }
 2249:     my %ordered;
 2250:     if (ref($bookshash) eq 'HASH') {
 2251:         foreach my $item (keys(%{$bookshash})) {
 2252:             if (ref($bookshash->{$item}) eq 'HASH') {
 2253:                 my $num = $bookshash->{$item}{'order'};
 2254:                 $ordered{$num} = $item;
 2255:             }
 2256:         }
 2257:     }
 2258:     my $confname = $dom.'-domainconfig';
 2259:     my $switchserver = &check_switchserver($dom,$confname);
 2260:     my $maxnum = scalar(keys(%ordered));
 2261:     my $datatable;
 2262:     if (keys(%ordered)) {
 2263:         my @items = sort { $a <=> $b } keys(%ordered);
 2264:         for (my $i=0; $i<@items; $i++) {
 2265:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2266:             my $key = $ordered{$items[$i]};
 2267:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2268:             my $coursetitle = $coursehash{'description'};
 2269:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2270:             if (ref($bookshash->{$key}) eq 'HASH') {
 2271:                 $subject = $bookshash->{$key}->{'subject'};
 2272:                 $title = $bookshash->{$key}->{'title'};
 2273:                 if ($type eq 'textbooks') {
 2274:                     $publisher = $bookshash->{$key}->{'publisher'};
 2275:                     $author = $bookshash->{$key}->{'author'};
 2276:                     $image = $bookshash->{$key}->{'image'};
 2277:                     if ($image ne '') {
 2278:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2279:                         my $imagethumb = "$path/tn-".$imagefile;
 2280:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2281:                     }
 2282:                 }
 2283:             }
 2284:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2285:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2286:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2287:             for (my $k=0; $k<=$maxnum; $k++) {
 2288:                 my $vpos = $k+1;
 2289:                 my $selstr;
 2290:                 if ($k == $i) {
 2291:                     $selstr = ' selected="selected" ';
 2292:                 }
 2293:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2294:             }
 2295:             $datatable .= '</select>'.('&nbsp;'x2).
 2296:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2297:                 &mt('Delete?').'</label></span></td>'.
 2298:                 '<td colspan="2">'.
 2299:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2300:                 ('&nbsp;'x2).
 2301:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2302:             if ($type eq 'textbooks') {
 2303:                 $datatable .= ('&nbsp;'x2).
 2304:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2305:                               ('&nbsp;'x2).
 2306:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2307:                               ('&nbsp;'x2).
 2308:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2309:                 if ($image) {
 2310:                     $datatable .= '<span class="LC_nobreak">'.
 2311:                                   $imgsrc.
 2312:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2313:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2314:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2315:                 }
 2316:                 if ($switchserver) {
 2317:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2318:                 } else {
 2319:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2320:                 }
 2321:             }
 2322:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2323:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2324:                           $coursetitle.'</span></td></tr>'."\n";
 2325:             $itemcount ++;
 2326:         }
 2327:     }
 2328:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2329:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2330:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2331:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2332:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2333:     for (my $k=0; $k<$maxnum+1; $k++) {
 2334:         my $vpos = $k+1;
 2335:         my $selstr;
 2336:         if ($k == $maxnum) {
 2337:             $selstr = ' selected="selected" ';
 2338:         }
 2339:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2340:     }
 2341:     $datatable .= '</select>&nbsp;'."\n".
 2342:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2343:                   '<td colspan="2">'.
 2344:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2345:                   ('&nbsp;'x2).
 2346:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2347:                   ('&nbsp;'x2);
 2348:     if ($type eq 'textbooks') {
 2349:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2350:                       ('&nbsp;'x2).
 2351:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2352:                       ('&nbsp;'x2).
 2353:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2354:         if ($switchserver) {
 2355:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2356:         } else {
 2357:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2358:         }
 2359:     }
 2360:     $datatable .= '</span>'."\n".
 2361:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2362:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2363:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2364:                   &Apache::loncommon::selectcourse_link
 2365:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course');
 2366:                   '</span></td>'."\n".
 2367:                   '</tr>'."\n";
 2368:     $itemcount ++;
 2369:     return $datatable;
 2370: }
 2371: 
 2372: sub textbookcourses_javascript {
 2373:     my ($settings) = @_;
 2374:     return unless(ref($settings) eq 'HASH');
 2375:     my (%ordered,%total,%jstext);
 2376:     foreach my $type ('textbooks','templates') {
 2377:         $total{$type} = 0;
 2378:         if (ref($settings->{$type}) eq 'HASH') {
 2379:             foreach my $item (keys(%{$settings->{$type}})) {
 2380:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2381:                     my $num = $settings->{$type}->{$item}{'order'};
 2382:                     $ordered{$type}{$num} = $item;
 2383:                 }
 2384:             }
 2385:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2386:         }
 2387:         my @jsarray = ();
 2388:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2389:             push(@jsarray,$ordered{$type}{$item});
 2390:         }
 2391:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2392:     }
 2393:     return <<"ENDSCRIPT";
 2394: <script type="text/javascript">
 2395: // <![CDATA[
 2396: function reorderBooks(form,item,caller) {
 2397:     var changedVal;
 2398: $jstext{'textbooks'};
 2399: $jstext{'templates'};
 2400:     var newpos;
 2401:     var maxh;
 2402:     if (caller == 'textbooks') {  
 2403:         newpos = 'textbooks_addbook_pos';
 2404:         maxh = 1 + $total{'textbooks'};
 2405:     } else {
 2406:         newpos = 'templates_addbook_pos';
 2407:         maxh = 1 + $total{'templates'};
 2408:     }
 2409:     var current = new Array;
 2410:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2411:     if (item == newpos) {
 2412:         changedVal = newitemVal;
 2413:     } else {
 2414:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2415:         current[newitemVal] = newpos;
 2416:     }
 2417:     if (caller == 'textbooks') {
 2418:         for (var i=0; i<textbooks.length; i++) {
 2419:             var elementName = 'textbooks_'+textbooks[i];
 2420:             if (elementName != item) {
 2421:                 if (form.elements[elementName]) {
 2422:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2423:                     current[currVal] = elementName;
 2424:                 }
 2425:             }
 2426:         }
 2427:     }
 2428:     if (caller == 'templates') {
 2429:         for (var i=0; i<templates.length; i++) {
 2430:             var elementName = 'templates_'+templates[i];
 2431:             if (elementName != item) {
 2432:                 if (form.elements[elementName]) {
 2433:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2434:                     current[currVal] = elementName;
 2435:                 }
 2436:             }
 2437:         }
 2438:     }
 2439:     var oldVal;
 2440:     for (var j=0; j<maxh; j++) {
 2441:         if (current[j] == undefined) {
 2442:             oldVal = j;
 2443:         }
 2444:     }
 2445:     if (oldVal < changedVal) {
 2446:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2447:            var elementName = current[k];
 2448:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2449:         }
 2450:     } else {
 2451:         for (var k=changedVal; k<oldVal; k++) {
 2452:             var elementName = current[k];
 2453:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2454:         }
 2455:     }
 2456:     return;
 2457: }
 2458: 
 2459: // ]]>
 2460: </script>
 2461: 
 2462: ENDSCRIPT
 2463: }
 2464: 
 2465: sub ltitools_javascript {
 2466:     my ($settings) = @_;
 2467:     return unless(ref($settings) eq 'HASH');
 2468:     my (%ordered,$total,%jstext);
 2469:     $total = 0;
 2470:     foreach my $item (keys(%{$settings})) {
 2471:         if (ref($settings->{$item}) eq 'HASH') {
 2472:             my $num = $settings->{$item}{'order'};
 2473:             $ordered{$num} = $item;
 2474:         }
 2475:     }
 2476:     $total = scalar(keys(%{$settings}));
 2477:     my @jsarray = ();
 2478:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2479:         push(@jsarray,$ordered{$item});
 2480:     }
 2481:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 2482:     return <<"ENDSCRIPT";
 2483: <script type="text/javascript">
 2484: // <![CDATA[
 2485: function reorderLTI(form,item) {
 2486:     var changedVal;
 2487: $jstext
 2488:     var newpos = 'ltitools_add_pos';
 2489:     var maxh = 1 + $total;
 2490:     var current = new Array;
 2491:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2492:     if (item == newpos) {
 2493:         changedVal = newitemVal;
 2494:     } else {
 2495:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2496:         current[newitemVal] = newpos;
 2497:     }
 2498:     for (var i=0; i<ltitools.length; i++) {
 2499:         var elementName = 'ltitools_'+ltitools[i];
 2500:         if (elementName != item) {
 2501:             if (form.elements[elementName]) {
 2502:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2503:                 current[currVal] = elementName;
 2504:             }
 2505:         }
 2506:     }
 2507:     var oldVal;
 2508:     for (var j=0; j<maxh; j++) {
 2509:         if (current[j] == undefined) {
 2510:             oldVal = j;
 2511:         }
 2512:     }
 2513:     if (oldVal < changedVal) {
 2514:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2515:            var elementName = current[k];
 2516:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2517:         }
 2518:     } else {
 2519:         for (var k=changedVal; k<oldVal; k++) {
 2520:             var elementName = current[k];
 2521:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2522:         }
 2523:     }
 2524:     return;
 2525: }
 2526: 
 2527: // ]]>
 2528: </script>
 2529: 
 2530: ENDSCRIPT
 2531: }
 2532: 
 2533: sub print_autoenroll {
 2534:     my ($dom,$settings,$rowtotal) = @_;
 2535:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2536:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,$failsafe);
 2537:     if (ref($settings) eq 'HASH') {
 2538:         if (exists($settings->{'run'})) {
 2539:             if ($settings->{'run'} eq '0') {
 2540:                 $runoff = ' checked="checked" ';
 2541:                 $runon = ' ';
 2542:             } else {
 2543:                 $runon = ' checked="checked" ';
 2544:                 $runoff = ' ';
 2545:             }
 2546:         } else {
 2547:             if ($autorun) {
 2548:                 $runon = ' checked="checked" ';
 2549:                 $runoff = ' ';
 2550:             } else {
 2551:                 $runoff = ' checked="checked" ';
 2552:                 $runon = ' ';
 2553:             }
 2554:         }
 2555:         if (exists($settings->{'co-owners'})) {
 2556:             if ($settings->{'co-owners'} eq '0') {
 2557:                 $coownersoff = ' checked="checked" ';
 2558:                 $coownerson = ' ';
 2559:             } else {
 2560:                 $coownerson = ' checked="checked" ';
 2561:                 $coownersoff = ' ';
 2562:             }
 2563:         } else {
 2564:             $coownersoff = ' checked="checked" ';
 2565:             $coownerson = ' ';
 2566:         }
 2567:         if (exists($settings->{'sender_domain'})) {
 2568:             $defdom = $settings->{'sender_domain'};
 2569:         }
 2570:         if (exists($settings->{'autofailsafe'})) {
 2571:             $failsafe = $settings->{'autofailsafe'};
 2572:         }
 2573:     } else {
 2574:         if ($autorun) {
 2575:             $runon = ' checked="checked" ';
 2576:             $runoff = ' ';
 2577:         } else {
 2578:             $runoff = ' checked="checked" ';
 2579:             $runon = ' ';
 2580:         }
 2581:     }
 2582:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2583:     my $notif_sender;
 2584:     if (ref($settings) eq 'HASH') {
 2585:         $notif_sender = $settings->{'sender_uname'};
 2586:     }
 2587:     my $datatable='<tr class="LC_odd_row">'.
 2588:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2589:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2590:                   '<input type="radio" name="autoenroll_run"'.
 2591:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2592:                   '<label><input type="radio" name="autoenroll_run"'.
 2593:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2594:                   '</tr><tr>'.
 2595:                   '<td>'.&mt('Notification messages - sender').
 2596:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2597:                   &mt('username').':&nbsp;'.
 2598:                   '<input type="text" name="sender_uname" value="'.
 2599:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2600:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2601:                   '<tr class="LC_odd_row">'.
 2602:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2603:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2604:                   '<input type="radio" name="autoassign_coowners"'.
 2605:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2606:                   '<label><input type="radio" name="autoassign_coowners"'.
 2607:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2608:                   '</tr><tr>'.
 2609:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 2610:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2611:                   '<input type="text" name="autoenroll_failsafe"'.
 2612:                   ' value="'.$failsafe.'" size="4" /></td></tr>';
 2613:     $$rowtotal += 4;
 2614:     return $datatable;
 2615: }
 2616: 
 2617: sub print_autoupdate {
 2618:     my ($position,$dom,$settings,$rowtotal) = @_;
 2619:     my $datatable;
 2620:     if ($position eq 'top') {
 2621:         my $updateon = ' ';
 2622:         my $updateoff = ' checked="checked" ';
 2623:         my $classlistson = ' ';
 2624:         my $classlistsoff = ' checked="checked" ';
 2625:         if (ref($settings) eq 'HASH') {
 2626:             if ($settings->{'run'} eq '1') {
 2627:                 $updateon = $updateoff;
 2628:                 $updateoff = ' ';
 2629:             }
 2630:             if ($settings->{'classlists'} eq '1') {
 2631:                 $classlistson = $classlistsoff;
 2632:                 $classlistsoff = ' ';
 2633:             }
 2634:         }
 2635:         my %title = (
 2636:                    run => 'Auto-update active?',
 2637:                    classlists => 'Update information in classlists?',
 2638:                     );
 2639:         $datatable = '<tr class="LC_odd_row">'. 
 2640:                   '<td>'.&mt($title{'run'}).'</td>'.
 2641:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2642:                   '<input type="radio" name="autoupdate_run"'.
 2643:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2644:                   '<label><input type="radio" name="autoupdate_run"'.
 2645:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2646:                   '</tr><tr>'.
 2647:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2648:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2649:                   '<label><input type="radio" name="classlists"'.
 2650:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2651:                   '<label><input type="radio" name="classlists"'.
 2652:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2653:                   '</tr>';
 2654:         $$rowtotal += 2;
 2655:     } elsif ($position eq 'middle') {
 2656:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2657:         my $numinrow = 3;
 2658:         my $locknamesettings;
 2659:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2660:                                      $dom,$numinrow,$othertitle,
 2661:                                     'lockablenames',$rowtotal);
 2662:         $$rowtotal ++;
 2663:     } else {
 2664:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2665:         my @fields = ('lastname','firstname','middlename','generation',
 2666:                       'permanentemail','id');
 2667:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2668:         my $numrows = 0;
 2669:         if (ref($types) eq 'ARRAY') {
 2670:             if (@{$types} > 0) {
 2671:                 $datatable = 
 2672:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2673:                                          \@fields,$types,\$numrows);
 2674:                     $$rowtotal += @{$types}; 
 2675:             }
 2676:         }
 2677:         $datatable .= 
 2678:             &usertype_update_row($settings,{'default' => $othertitle},
 2679:                                  \%fieldtitles,\@fields,['default'],
 2680:                                  \$numrows);
 2681:         $$rowtotal ++;     
 2682:     }
 2683:     return $datatable;
 2684: }
 2685: 
 2686: sub print_autocreate {
 2687:     my ($dom,$settings,$rowtotal) = @_;
 2688:     my (%createon,%createoff,%currhash);
 2689:     my @types = ('xml','req');
 2690:     if (ref($settings) eq 'HASH') {
 2691:         foreach my $item (@types) {
 2692:             $createoff{$item} = ' checked="checked" ';
 2693:             $createon{$item} = ' ';
 2694:             if (exists($settings->{$item})) {
 2695:                 if ($settings->{$item}) {
 2696:                     $createon{$item} = ' checked="checked" ';
 2697:                     $createoff{$item} = ' ';
 2698:                 }
 2699:             }
 2700:         }
 2701:         if ($settings->{'xmldc'} ne '') {
 2702:             $currhash{$settings->{'xmldc'}} = 1;
 2703:         }
 2704:     } else {
 2705:         foreach my $item (@types) {
 2706:             $createoff{$item} = ' checked="checked" ';
 2707:             $createon{$item} = ' ';
 2708:         }
 2709:     }
 2710:     $$rowtotal += 2;
 2711:     my $numinrow = 2;
 2712:     my $datatable='<tr class="LC_odd_row">'.
 2713:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2714:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2715:                   '<input type="radio" name="autocreate_xml"'.
 2716:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2717:                   '<label><input type="radio" name="autocreate_xml"'.
 2718:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2719:                   '</td></tr><tr>'.
 2720:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2721:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2722:                   '<input type="radio" name="autocreate_req"'.
 2723:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2724:                   '<label><input type="radio" name="autocreate_req"'.
 2725:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2726:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2727:                                                    'autocreate_xmldc',%currhash);
 2728:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 2729:     if ($numdc > 1) {
 2730:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 2731:                       '</td><td class="LC_left_item">';
 2732:     } else {
 2733:         $datatable .= &mt('Course creation processed as:').
 2734:                       '</td><td class="LC_right_item">';
 2735:     }
 2736:     $datatable .= $dctable.'</td></tr>';
 2737:     $$rowtotal += $rows;
 2738:     return $datatable;
 2739: }
 2740: 
 2741: sub print_directorysrch {
 2742:     my ($position,$dom,$settings,$rowtotal) = @_;
 2743:     my $datatable;
 2744:     if ($position eq 'top') {
 2745:         my $instsrchon = ' ';
 2746:         my $instsrchoff = ' checked="checked" ';
 2747:         my ($exacton,$containson,$beginson);
 2748:         my $instlocalon = ' ';
 2749:         my $instlocaloff = ' checked="checked" ';
 2750:         if (ref($settings) eq 'HASH') {
 2751:             if ($settings->{'available'} eq '1') {
 2752:                 $instsrchon = $instsrchoff;
 2753:                 $instsrchoff = ' ';
 2754:             }
 2755:             if ($settings->{'localonly'} eq '1') {
 2756:                 $instlocalon = $instlocaloff;
 2757:                 $instlocaloff = ' ';
 2758:             }
 2759:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2760:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 2761:                     if ($type eq 'exact') {
 2762:                         $exacton = ' checked="checked" ';
 2763:                     } elsif ($type eq 'contains') {
 2764:                         $containson = ' checked="checked" ';
 2765:                     } elsif ($type eq 'begins') {
 2766:                         $beginson = ' checked="checked" ';
 2767:                     }
 2768:                 }
 2769:             } else {
 2770:                 if ($settings->{'searchtypes'} eq 'exact') {
 2771:                     $exacton = ' checked="checked" ';
 2772:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 2773:                     $containson = ' checked="checked" ';
 2774:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 2775:                     $exacton = ' checked="checked" ';
 2776:                     $containson = ' checked="checked" ';
 2777:                 }
 2778:             }
 2779:         }
 2780:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2781:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2782: 
 2783:         my $numinrow = 4;
 2784:         my $cansrchrow = 0;
 2785:         $datatable='<tr class="LC_odd_row">'.
 2786:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 2787:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2788:                    '<input type="radio" name="dirsrch_available"'.
 2789:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2790:                    '<label><input type="radio" name="dirsrch_available"'.
 2791:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2792:                    '</tr><tr>'.
 2793:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 2794:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2795:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 2796:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2797:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 2798:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2799:                    '</tr>';
 2800:         $$rowtotal += 2;
 2801:         if (ref($usertypes) eq 'HASH') {
 2802:             if (keys(%{$usertypes}) > 0) {
 2803:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2804:                                              $numinrow,$othertitle,'cansearch',
 2805:                                              $rowtotal);
 2806:                 $cansrchrow = 1;
 2807:             }
 2808:         }
 2809:         if ($cansrchrow) {
 2810:             $$rowtotal ++;
 2811:             $datatable .= '<tr>';
 2812:         } else {
 2813:             $datatable .= '<tr class="LC_odd_row">';
 2814:         }
 2815:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2816:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2817:         foreach my $title (@{$titleorder}) {
 2818:             if (defined($searchtitles->{$title})) {
 2819:                 my $check = ' ';
 2820:                 if (ref($settings) eq 'HASH') {
 2821:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2822:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2823:                             $check = ' checked="checked" ';
 2824:                         }
 2825:                     }
 2826:                 }
 2827:                 $datatable .= '<td class="LC_left_item">'.
 2828:                               '<span class="LC_nobreak"><label>'.
 2829:                               '<input type="checkbox" name="searchby" '.
 2830:                               'value="'.$title.'"'.$check.'/>'.
 2831:                               $searchtitles->{$title}.'</label></span></td>';
 2832:             }
 2833:         }
 2834:         $datatable .= '</tr></table></td></tr>';
 2835:         $$rowtotal ++;
 2836:         if ($cansrchrow) {
 2837:             $datatable .= '<tr class="LC_odd_row">';
 2838:         } else {
 2839:             $datatable .= '<tr>';
 2840:         }
 2841:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2842:                       '<td class="LC_left_item" colspan="2">'.
 2843:                       '<span class="LC_nobreak"><label>'.
 2844:                       '<input type="checkbox" name="searchtypes" '.
 2845:                       $exacton.' value="exact" />'.&mt('Exact match').
 2846:                       '</label>&nbsp;'.
 2847:                       '<label><input type="checkbox" name="searchtypes" '.
 2848:                       $beginson.' value="begins" />'.&mt('Begins with').
 2849:                       '</label>&nbsp;'.
 2850:                       '<label><input type="checkbox" name="searchtypes" '.
 2851:                       $containson.' value="contains" />'.&mt('Contains').
 2852:                       '</label></span></td></tr>';
 2853:         $$rowtotal ++;
 2854:     } else {
 2855:         my $domsrchon = ' checked="checked" ';
 2856:         my $domsrchoff = ' ';
 2857:         my $domlocalon = ' ';
 2858:         my $domlocaloff = ' checked="checked" ';
 2859:         if (ref($settings) eq 'HASH') {
 2860:             if ($settings->{'lclocalonly'} eq '1') {
 2861:                 $domlocalon = $domlocaloff;
 2862:                 $domlocaloff = ' ';
 2863:             }
 2864:             if ($settings->{'lcavailable'} eq '0') {
 2865:                 $domsrchoff = $domsrchon;
 2866:                 $domsrchon = ' ';
 2867:             }
 2868:         }
 2869:         $datatable='<tr class="LC_odd_row">'.
 2870:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 2871:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2872:                       '<input type="radio" name="dirsrch_domavailable"'.
 2873:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2874:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 2875:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2876:                       '</tr><tr>'.
 2877:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 2878:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2879:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 2880:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2881:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 2882:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2883:                       '</tr>';
 2884:         $$rowtotal += 2;
 2885:     }
 2886:     return $datatable;
 2887: }
 2888: 
 2889: sub print_contacts {
 2890:     my ($position,$dom,$settings,$rowtotal) = @_;
 2891:     my $datatable;
 2892:     my @contacts = ('adminemail','supportemail');
 2893:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 2894:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings);
 2895:     if ($position eq 'top') {
 2896:         if (ref($settings) eq 'HASH') {
 2897:             foreach my $item (@contacts) {
 2898:                 if (exists($settings->{$item})) {
 2899:                     $to{$item} = $settings->{$item};
 2900:                 }
 2901:             }
 2902:         }
 2903:     } elsif ($position eq 'middle') {
 2904:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 2905:                      'updatesmail','idconflictsmail');
 2906:         foreach my $type (@mailings) {
 2907:             $otheremails{$type} = '';
 2908:         }
 2909:     } else {
 2910:         @mailings = ('helpdeskmail','otherdomsmail');
 2911:         foreach my $type (@mailings) {
 2912:             $otheremails{$type} = '';
 2913:         }
 2914:         $bccemails{'helpdeskmail'} = '';
 2915:         $bccemails{'otherdomsmail'} = '';
 2916:         $includestr{'helpdeskmail'} = '';
 2917:         $includestr{'otherdomsmail'} = '';
 2918:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 2919:     }
 2920:     if (ref($settings) eq 'HASH') {
 2921:         unless ($position eq 'top') {
 2922:             foreach my $type (@mailings) {
 2923:                 if (exists($settings->{$type})) {
 2924:                     if (ref($settings->{$type}) eq 'HASH') {
 2925:                         foreach my $item (@contacts) {
 2926:                             if ($settings->{$type}{$item}) {
 2927:                                 $checked{$type}{$item} = ' checked="checked" ';
 2928:                             }
 2929:                         }
 2930:                         $otheremails{$type} = $settings->{$type}{'others'};
 2931:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 2932:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 2933:                             if ($settings->{$type}{'include'} ne '') {
 2934:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 2935:                                 $includestr{$type} = &unescape($includestr{$type});
 2936:                             }
 2937:                         }
 2938:                     }
 2939:                 } elsif ($type eq 'lonstatusmail') {
 2940:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2941:                 }
 2942:             }
 2943:         }
 2944:         if ($position eq 'bottom') {
 2945:             foreach my $type (@mailings) {
 2946:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 2947:                 if ($settings->{$type}{'include'} ne '') {
 2948:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 2949:                     $includestr{$type} = &unescape($includestr{$type});
 2950:                 }
 2951:             }
 2952:             if (ref($settings->{'helpform'}) eq 'HASH') {
 2953:                 if (ref($fields) eq 'ARRAY') {
 2954:                     foreach my $field (@{$fields}) {
 2955:                         $currfield{$field} = $settings->{'helpform'}{$field};
 2956:                     }
 2957:                 }
 2958:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 2959:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 2960:                 } else {
 2961:                     $maxsize = '1.0';
 2962:                 }
 2963:             } else {
 2964:                 if (ref($fields) eq 'ARRAY') {
 2965:                     foreach my $field (@{$fields}) {
 2966:                         $currfield{$field} = 'yes';
 2967:                     }
 2968:                 }
 2969:                 $maxsize = '1.0';
 2970:             }
 2971:         }
 2972:     } else {
 2973:         if ($position eq 'top') {
 2974:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2975:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2976:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2977:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2978:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2979:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2980:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2981:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 2982:         } elsif ($position eq 'bottom') {
 2983:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2984:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 2985:             if (ref($fields) eq 'ARRAY') {
 2986:                 foreach my $field (@{$fields}) {
 2987:                     $currfield{$field} = 'yes';
 2988:                 }
 2989:             }
 2990:             $maxsize = '1.0';
 2991:         }
 2992:     }
 2993:     my ($titles,$short_titles) = &contact_titles();
 2994:     my $rownum = 0;
 2995:     my $css_class;
 2996:     if ($position eq 'top') {
 2997:         foreach my $item (@contacts) {
 2998:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 2999:             $datatable .= '<tr'.$css_class.'>'. 
 3000:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 3001:                           '</span></td><td class="LC_right_item">'.
 3002:                           '<input type="text" name="'.$item.'" value="'.
 3003:                           $to{$item}.'" /></td></tr>';
 3004:             $rownum ++;
 3005:         }
 3006:     } elsif ($position eq 'bottom') {
 3007:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3008:         $datatable .= '<tr'.$css_class.'>'.
 3009:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 3010:                       &mt('(e-mail, subject, and description always shown)').
 3011:                       '</td><td class="LC_left_item">';
 3012:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 3013:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 3014:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 3015:             foreach my $field (@{$fields}) {
 3016:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 3017:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 3018:                     $datatable .= ' '.&mt('(logged-in users)');
 3019:                 }
 3020:                 $datatable .='</td><td>';
 3021:                 my $clickaction;
 3022:                 if ($field eq 'screenshot') {
 3023:                     $clickaction = ' onclick="screenshotSize(this);"';
 3024:                 }
 3025:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 3026:                     foreach my $option (@{$possoptions->{$field}}) {
 3027:                         my $checked;
 3028:                         if ($currfield{$field} eq $option) {
 3029:                             $checked = ' checked="checked"';
 3030:                         }
 3031:                         $datatable .= '<span class="LC_nobreak"><label>'.
 3032:                                       '<input type="radio" name="helpform_'.$field.'" '.
 3033:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 3034:                                       '</label></span>'.('&nbsp;'x2);
 3035:                     }
 3036:                 }
 3037:                 if ($field eq 'screenshot') {
 3038:                     my $display;
 3039:                     if ($currfield{$field} eq 'no') {
 3040:                         $display = ' style="display:none"';
 3041:                     }
 3042:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.' />'.
 3043:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 3044:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 3045:                 }
 3046:                 $datatable .= '</td></tr>';
 3047:             }
 3048:             $datatable .= '</table>';
 3049:         }
 3050:         $datatable .= '</td></tr>'."\n";
 3051:         $rownum ++;
 3052:     }
 3053:     unless ($position eq 'top') {
 3054:         foreach my $type (@mailings) {
 3055:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3056:             $datatable .= '<tr'.$css_class.'>'.
 3057:                           '<td><span class="LC_nobreak">'.
 3058:                           $titles->{$type}.': </span></td>'.
 3059:                           '<td class="LC_left_item">';
 3060:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3061:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 3062:             }
 3063:             $datatable .= '<span class="LC_nobreak">';
 3064:             foreach my $item (@contacts) {
 3065:                 $datatable .= '<label>'.
 3066:                               '<input type="checkbox" name="'.$type.'"'.
 3067:                               $checked{$type}{$item}.
 3068:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 3069:                               '</label>&nbsp;';
 3070:             }
 3071:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3072:                           '<input type="text" name="'.$type.'_others" '.
 3073:                           'value="'.$otheremails{$type}.'"  />';
 3074:             my %locchecked;
 3075:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3076:                 foreach my $loc ('s','b') {
 3077:                     if ($includeloc{$type} eq $loc) {
 3078:                         $locchecked{$loc} = ' checked="checked"';
 3079:                         last;
 3080:                     }
 3081:                 }
 3082:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3083:                               '<input type="text" name="'.$type.'_bcc" '.
 3084:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 3085:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3086:                               &mt('Text automatically added to e-mail:').' '.
 3087:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br >'.
 3088:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3089:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3090:                               ('&nbsp;'x2).
 3091:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3092:                               '</span></fieldset>';
 3093:             }
 3094:             $datatable .= '</td></tr>'."\n";
 3095:             $rownum ++;
 3096:         }
 3097:     }
 3098:     if ($position eq 'middle') {
 3099:         my %choices;
 3100:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 3101:                                        &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3102:                                        &mt('LON-CAPA core group - MSU'),600,500));
 3103:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 3104:                                         &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3105:                                         &mt('LON-CAPA core group - MSU'),600,500));
 3106:         my @toggles = ('reporterrors','reportupdates');
 3107:         my %defaultchecked = ('reporterrors'  => 'on',
 3108:                               'reportupdates' => 'on');
 3109:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3110:                                                    \%choices,$rownum);
 3111:         $datatable .= $reports;
 3112:     } elsif ($position eq 'bottom') {
 3113:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3114:         my (@posstypes,%usertypeshash);
 3115:         if (ref($types) eq 'ARRAY') {
 3116:             @posstypes = @{$types};
 3117:         }
 3118:         if (@posstypes) {
 3119:             if (ref($usertypes) eq 'HASH') {
 3120:                 %usertypeshash = %{$usertypes};
 3121:             }
 3122:             my @overridden;
 3123:             my $numinrow = 4;
 3124:             if (ref($settings) eq 'HASH') {
 3125:                 if (ref($settings->{'overrides'}) eq 'HASH') {
 3126:                     foreach my $key (sort(keys(%{$settings->{'overrides'}}))) {
 3127:                         if (ref($settings->{'overrides'}{$key}) eq 'HASH') {
 3128:                             push(@overridden,$key);
 3129:                             foreach my $item (@contacts) {
 3130:                                 if ($settings->{'overrides'}{$key}{$item}) {
 3131:                                     $checked{'override_'.$key}{$item} = ' checked="checked" ';
 3132:                                 }
 3133:                             }
 3134:                             $otheremails{'override_'.$key} = $settings->{'overrides'}{$key}{'others'};
 3135:                             $bccemails{'override_'.$key} = $settings->{'overrides'}{$key}{'bcc'};
 3136:                             $includeloc{'override_'.$key} = '';
 3137:                             $includestr{'override_'.$key} = '';
 3138:                             if ($settings->{'overrides'}{$key}{'include'} ne '') {
 3139:                                 ($includeloc{'override_'.$key},$includestr{'override_'.$key}) =
 3140:                                     split(/:/,$settings->{'overrides'}{$key}{'include'},2);
 3141:                                 $includestr{'override_'.$key} = &unescape($includestr{'override_'.$key});
 3142:                             }
 3143:                         }
 3144:                     }
 3145:                 }
 3146:             }
 3147:             my $customclass = 'LC_helpdesk_override';
 3148:             my $optionsprefix = 'LC_options_helpdesk_';
 3149: 
 3150:             my $onclicktypes = "toggleHelpdeskRow(this.form,'overrides','$customclass','$optionsprefix');";
 3151: 
 3152:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 3153:                                          $numinrow,$othertitle,'overrides',
 3154:                                          \$rownum,$onclicktypes,$customclass);
 3155:             $rownum ++;
 3156:             $usertypeshash{'default'} = $othertitle;
 3157:             foreach my $status (@posstypes) {
 3158:                 my $css_class;
 3159:                 if ($rownum%2) {
 3160:                     $css_class = 'LC_odd_row ';
 3161:                 }
 3162:                 $css_class .= $customclass;
 3163:                 my $rowid = $optionsprefix.$status;
 3164:                 my $hidden = 1;
 3165:                 my $currstyle = 'display:none';
 3166:                 if (grep(/^\Q$status\E$/,@overridden)) {
 3167:                     $currstyle = 'display:table-row';
 3168:                     $hidden = 0;
 3169:                 }
 3170:                 my $key = 'override_'.$status;
 3171:                 $datatable .= &overridden_helpdesk($checked{$key},$otheremails{$key},$bccemails{$key},
 3172:                                                   $includeloc{$key},$includestr{$key},$status,$rowid,
 3173:                                                   $usertypeshash{$status},$css_class,$currstyle,
 3174:                                                   \@contacts,$short_titles);
 3175:                 unless ($hidden) {
 3176:                     $rownum ++;
 3177:                 }
 3178:             }
 3179:         }
 3180:     }
 3181:     $$rowtotal += $rownum;
 3182:     return $datatable;
 3183: }
 3184: 
 3185: sub overridden_helpdesk {
 3186:     my ($checked,$otheremails,$bccemails,$includeloc,$includestr,$type,$rowid,
 3187:         $typetitle,$css_class,$rowstyle,$contacts,$short_titles) = @_;
 3188:     my $class = 'LC_left_item';
 3189:     if ($css_class) {
 3190:         $css_class = ' class="'.$css_class.'"';
 3191:     }
 3192:     if ($rowid) {
 3193:         $rowid = ' id="'.$rowid.'"';
 3194:     }
 3195:     if ($rowstyle) {
 3196:         $rowstyle = ' style="'.$rowstyle.'"';
 3197:     }
 3198:     my ($output,$description);
 3199:     $description = &mt('Helpdesk requests from: [_1] in this domain (overrides default)',"<b>$typetitle</b>");
 3200:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 3201:               "<td>$description</td>\n".
 3202:               '<td class="'.$class.'" colspan="2">'.
 3203:               '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>'.
 3204:               '<span class="LC_nobreak">';
 3205:     if (ref($contacts) eq 'ARRAY') {
 3206:         foreach my $item (@{$contacts}) {
 3207:             my $check;
 3208:             if (ref($checked) eq 'HASH') {
 3209:                $check = $checked->{$item};
 3210:             }
 3211:             my $title;
 3212:             if (ref($short_titles) eq 'HASH') {
 3213:                 $title = $short_titles->{$item};
 3214:             }
 3215:             $output .= '<label>'.
 3216:                        '<input type="checkbox" name="override_'.$type.'"'.$check.
 3217:                        ' value="'.$item.'" />'.$title.'</label>&nbsp;';
 3218:         }
 3219:     }
 3220:     $output .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3221:                '<input type="text" name="override_'.$type.'_others" '.
 3222:                'value="'.$otheremails.'"  />';
 3223:     my %locchecked;
 3224:     foreach my $loc ('s','b') {
 3225:         if ($includeloc eq $loc) {
 3226:             $locchecked{$loc} = ' checked="checked"';
 3227:             last;
 3228:         }
 3229:     }
 3230:     $output .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3231:                '<input type="text" name="override_'.$type.'_bcc" '.
 3232:                'value="'.$bccemails.'"  /></fieldset>'.
 3233:                '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3234:                &mt('Text automatically added to e-mail:').' '.
 3235:                '<input type="text" name="override_'.$type.'_includestr" value="'.$includestr.'" /><br >'.
 3236:                '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3237:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3238:                ('&nbsp;'x2).
 3239:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3240:                '</span></fieldset>'.
 3241:                '</td></tr>'."\n";
 3242:     return $output;
 3243: }
 3244: 
 3245: sub contacts_javascript {
 3246:     return <<"ENDSCRIPT";
 3247: 
 3248: <script type="text/javascript">
 3249: // <![CDATA[
 3250: 
 3251: function screenshotSize(field) {
 3252:     if (document.getElementById('help_screenshotsize')) {
 3253:         if (field.value == 'no') {
 3254:             document.getElementById('help_screenshotsize').style.display="none";
 3255:         } else {
 3256:             document.getElementById('help_screenshotsize').style.display="";
 3257:         }
 3258:     }
 3259:     return;
 3260: }
 3261: 
 3262: function toggleHelpdeskRow(form,checkbox,target,prefix,docount) {
 3263:     if (form.elements[checkbox].length != undefined) {
 3264:         var count = 0;
 3265:         if (docount) {
 3266:             for (var i=0; i<form.elements[checkbox].length; i++) {
 3267:                 if (form.elements[checkbox][i].checked) {
 3268:                     count ++;
 3269:                 }
 3270:             }
 3271:         }
 3272:         for (var i=0; i<form.elements[checkbox].length; i++) {
 3273:             var type = form.elements[checkbox][i].value;
 3274:             if (document.getElementById(prefix+type)) {
 3275:                 if (form.elements[checkbox][i].checked) {
 3276:                     document.getElementById(prefix+type).style.display = 'table-row';
 3277:                     if (count % 2 == 1) {
 3278:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 3279:                     } else {
 3280:                         document.getElementById(prefix+type).className = target;
 3281:                     }
 3282:                     count ++;
 3283:                 } else {
 3284:                     document.getElementById(prefix+type).style.display = 'none';
 3285:                 }
 3286:             }
 3287:         }
 3288:     }
 3289:     return;
 3290: }
 3291: 
 3292: 
 3293: // ]]>
 3294: </script>
 3295: 
 3296: ENDSCRIPT
 3297: }
 3298: 
 3299: sub print_helpsettings {
 3300:     my ($position,$dom,$settings,$rowtotal) = @_;
 3301:     my $confname = $dom.'-domainconfig';
 3302:     my $formname = 'display';
 3303:     my ($datatable,$itemcount);
 3304:     if ($position eq 'top') {
 3305:         $itemcount = 1;
 3306:         my (%choices,%defaultchecked,@toggles);
 3307:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 3308:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 3309:                                      &mt('LON-CAPA bug tracker'),600,500));
 3310:         %defaultchecked = ('submitbugs' => 'on');
 3311:         @toggles = ('submitbugs');
 3312:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3313:                                                      \%choices,$itemcount);
 3314:         $$rowtotal ++;
 3315:     } else {
 3316:         my $css_class;
 3317:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 3318:         my (%customroles,%ordered,%current);
 3319:         if (ref($settings) eq 'HASH') {
 3320:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 3321:                 %current = %{$settings->{'adhoc'}};
 3322:             }
 3323:         }
 3324:         my $count = 0;
 3325:         foreach my $key (sort(keys(%existing))) {
 3326:             if ($key=~/^rolesdef\_(\w+)$/) {
 3327:                 my $rolename = $1;
 3328:                 my (%privs,$order);
 3329:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 3330:                 $customroles{$rolename} = \%privs;
 3331:                 if (ref($current{$rolename}) eq 'HASH') {
 3332:                     $order = $current{$rolename}{'order'};
 3333:                 }
 3334:                 if ($order eq '') {
 3335:                     $order = $count;
 3336:                 }
 3337:                 $ordered{$order} = $rolename;
 3338:                 $count++;
 3339:             }
 3340:         }
 3341:         my $maxnum = scalar(keys(%ordered));
 3342:         my @roles_by_num = ();
 3343:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3344:             push(@roles_by_num,$item);
 3345:         }
 3346:         my $context = 'domprefs';
 3347:         my $crstype = 'Course';
 3348:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3349:         my @accesstypes = ('all','dh','da','none');
 3350:         my ($numstatustypes,@jsarray);
 3351:         if (ref($types) eq 'ARRAY') {
 3352:             if (@{$types} > 0) {
 3353:                 $numstatustypes = scalar(@{$types});
 3354:                 push(@accesstypes,'status');
 3355:                 @jsarray = ('bystatus');
 3356:             }
 3357:         }
 3358:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 3359:         if (keys(%domhelpdesk)) {
 3360:             push(@accesstypes,('inc','exc'));
 3361:             push(@jsarray,('notinc','notexc'));
 3362:         }
 3363:         my $hiddenstr = join("','",@jsarray);
 3364:         $datatable .= &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname);
 3365:         my $context = 'domprefs';
 3366:         my $crstype = 'Course';
 3367:         my $prefix = 'helproles_';
 3368:         my $add_class = 'LC_hidden';
 3369:         foreach my $num (@roles_by_num) {
 3370:             my $role = $ordered{$num};
 3371:             my ($desc,$access,@statuses);
 3372:             if (ref($current{$role}) eq 'HASH') {
 3373:                 $desc = $current{$role}{'desc'};
 3374:                 $access = $current{$role}{'access'};
 3375:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 3376:                     @statuses = @{$current{$role}{'insttypes'}};
 3377:                 }
 3378:             }
 3379:             if ($desc eq '') {
 3380:                 $desc = $role;
 3381:             }
 3382:             my $identifier = 'custhelp'.$num;
 3383:             my %full=();
 3384:             my %levels= (
 3385:                          course => {},
 3386:                          domain => {},
 3387:                          system => {},
 3388:                         );
 3389:             my %levelscurrent=(
 3390:                                course => {},
 3391:                                domain => {},
 3392:                                system => {},
 3393:                               );
 3394:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 3395:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3396:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3397:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 3398:             $datatable .= '<tr '.$css_class.'><td valign="top"><b>'.$role.'</b><br />'.
 3399:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 3400:             for (my $k=0; $k<=$maxnum; $k++) {
 3401:                 my $vpos = $k+1;
 3402:                 my $selstr;
 3403:                 if ($k == $num) {
 3404:                     $selstr = ' selected="selected" ';
 3405:                 }
 3406:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3407:             }
 3408:             $datatable .= '</select>'.('&nbsp;'x2).
 3409:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 3410:                           '</td>'.
 3411:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3412:                           &mt('Name shown to users:').
 3413:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 3414:                           '</fieldset>'.
 3415:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 3416:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 3417:                           '<fieldset>'.
 3418:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 3419:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 3420:                                                                    \%levelscurrent,$identifier,
 3421:                                                                    'LC_hidden',$prefix.$num.'_privs').
 3422:                           '</fieldset></td>';
 3423:             $itemcount ++;
 3424:         }
 3425:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3426:         my $newcust = 'custhelp'.$count;
 3427:         my (%privs,%levelscurrent);
 3428:         my %full=();
 3429:         my %levels= (
 3430:                      course => {},
 3431:                      domain => {},
 3432:                      system => {},
 3433:                     );
 3434:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 3435:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3436:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 3437:         $datatable .= '<tr '.$css_class.'><td valign="top"><span class="LC_nobreak"><label>'.
 3438:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 3439:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 3440:         for (my $k=0; $k<$maxnum+1; $k++) {
 3441:             my $vpos = $k+1;
 3442:             my $selstr;
 3443:             if ($k == $maxnum) {
 3444:                 $selstr = ' selected="selected" ';
 3445:             }
 3446:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3447:         }
 3448:         $datatable .= '</select>&nbsp;'."\n".
 3449:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 3450:                       '</label></span></td>'.
 3451:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3452:                       '<span class="LC_nobreak">'.
 3453:                       &mt('Internal name:').
 3454:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 3455:                       '</span>'.('&nbsp;'x4).
 3456:                       '<span class="LC_nobreak">'.
 3457:                       &mt('Name shown to users:').
 3458:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 3459:                       '</span></fieldset>'.
 3460:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 3461:                                              $usertypes,$types,\%domhelpdesk).
 3462:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 3463:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 3464:                                                                 \@templateroles,$newcust).
 3465:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 3466:                                                                \%levelscurrent,$newcust).
 3467:                       '</fieldset></td></tr>';
 3468:         $count ++;
 3469:         $$rowtotal += $count;
 3470:     }
 3471:     return $datatable;
 3472: }
 3473: 
 3474: sub adhocbutton {
 3475:     my ($prefix,$num,$field,$visibility) = @_;
 3476:     my %lt = &Apache::lonlocal::texthash(
 3477:                                           show => 'Show details',
 3478:                                           hide => 'Hide details',
 3479:                                         );
 3480:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 3481:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 3482:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 3483:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 3484: }
 3485: 
 3486: sub helpsettings_javascript {
 3487:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 3488:     return unless(ref($roles_by_num) eq 'ARRAY');
 3489:     my %html_js_lt = &Apache::lonlocal::texthash(
 3490:                                           show => 'Show details',
 3491:                                           hide => 'Hide details',
 3492:                                         );
 3493:     &html_escape(\%html_js_lt);
 3494:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 3495:     return <<"ENDSCRIPT";
 3496: <script type="text/javascript">
 3497: // <![CDATA[
 3498: 
 3499: function reorderHelpRoles(form,item) {
 3500:     var changedVal;
 3501: $jstext
 3502:     var newpos = 'helproles_${total}_pos';
 3503:     var maxh = 1 + $total;
 3504:     var current = new Array();
 3505:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3506:     if (item == newpos) {
 3507:         changedVal = newitemVal;
 3508:     } else {
 3509:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3510:         current[newitemVal] = newpos;
 3511:     }
 3512:     for (var i=0; i<helproles.length; i++) {
 3513:         var elementName = 'helproles_'+helproles[i]+'_pos';
 3514:         if (elementName != item) {
 3515:             if (form.elements[elementName]) {
 3516:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3517:                 current[currVal] = elementName;
 3518:             }
 3519:         }
 3520:     }
 3521:     var oldVal;
 3522:     for (var j=0; j<maxh; j++) {
 3523:         if (current[j] == undefined) {
 3524:             oldVal = j;
 3525:         }
 3526:     }
 3527:     if (oldVal < changedVal) {
 3528:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3529:            var elementName = current[k];
 3530:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3531:         }
 3532:     } else {
 3533:         for (var k=changedVal; k<oldVal; k++) {
 3534:             var elementName = current[k];
 3535:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3536:         }
 3537:     }
 3538:     return;
 3539: }
 3540: 
 3541: function helpdeskAccess(num) {
 3542:     var curraccess = null;
 3543:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 3544:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 3545:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 3546:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 3547:             }
 3548:         }
 3549:     }
 3550:     var shown = Array();
 3551:     var hidden = Array();
 3552:     if (curraccess == 'none') {
 3553:         hidden = Array('$hiddenstr');
 3554:     } else {
 3555:         if (curraccess == 'status') {
 3556:             shown = Array('bystatus');
 3557:             hidden = Array('notinc','notexc');
 3558:         } else {
 3559:             if (curraccess == 'exc') {
 3560:                 shown = Array('notexc');
 3561:                 hidden = Array('notinc','bystatus');
 3562:             }
 3563:             if (curraccess == 'inc') {
 3564:                 shown = Array('notinc');
 3565:                 hidden = Array('notexc','bystatus');
 3566:             }
 3567:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 3568:                 hidden = Array('notinc','notexc','bystatus');
 3569:             }
 3570:         }
 3571:     }
 3572:     if (hidden.length > 0) {
 3573:         for (var i=0; i<hidden.length; i++) {
 3574:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 3575:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 3576:             }
 3577:         }
 3578:     }
 3579:     if (shown.length > 0) {
 3580:         for (var i=0; i<shown.length; i++) {
 3581:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 3582:                 if (shown[i] == 'privs') {
 3583:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 3584:                 } else {
 3585:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 3586:                 }
 3587:             }
 3588:         }
 3589:     }
 3590:     return;
 3591: }
 3592: 
 3593: function toggleHelpdeskItem(num,field) {
 3594:     if (document.getElementById('helproles_'+num+'_'+field)) {
 3595:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 3596:             document.getElementById('helproles_'+num+'_'+field).className =
 3597:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 3598:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3599:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 3600:             }
 3601:         } else {
 3602:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 3603:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3604:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 3605:             }
 3606:         }
 3607:     }
 3608:     return;
 3609: }
 3610: 
 3611: // ]]>
 3612: </script>
 3613: 
 3614: ENDSCRIPT
 3615: }
 3616: 
 3617: sub helpdeskroles_access {
 3618:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 3619:         $usertypes,$types,$domhelpdesk) = @_;
 3620:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 3621:     my %lt = &Apache::lonlocal::texthash(
 3622:                     'rou'    => 'Role usage',
 3623:                     'whi'    => 'Which helpdesk personnel may use this role?',
 3624:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 3625:                     'dh'     => 'All with domain helpdesk role',
 3626:                     'da'     => 'All with domain helpdesk assistant role',
 3627:                     'none'   => 'None',
 3628:                     'status' => 'Determined based on institutional status',
 3629:                     'inc'    => 'Include all, but exclude specific personnel',
 3630:                     'exc'    => 'Exclude all, but include specific personnel',
 3631:                   );
 3632:     my %usecheck = (
 3633:                      all => ' checked="checked"',
 3634:                    );
 3635:     my %displaydiv = (
 3636:                       status => 'none',
 3637:                       inc    => 'none',
 3638:                       exc    => 'none',
 3639:                       priv   => 'block',
 3640:                      );
 3641:     my $output;
 3642:     if (ref($current) eq 'HASH') {
 3643:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 3644:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 3645:                 $usecheck{$current->{access}} = $usecheck{'all'};
 3646:                 delete($usecheck{'all'});
 3647:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 3648:                     my $access = $1;
 3649:                     $displaydiv{$access} = 'inline';
 3650:                 } elsif ($current->{access} eq 'none') {
 3651:                     $displaydiv{'priv'} = 'none';
 3652:                 }
 3653:             }
 3654:         }
 3655:     }
 3656:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 3657:               '<p>'.$lt{'whi'}.'</p>';
 3658:     foreach my $access (@{$accesstypes}) {
 3659:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 3660:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 3661:                    $lt{$access}.'</label>';
 3662:         if ($access eq 'status') {
 3663:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 3664:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 3665:                                                                  $othertitle,$usertypes,$types).
 3666:                        '</div>';
 3667:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 3668:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 3669:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3670:                        '</div>';
 3671:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 3672:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 3673:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3674:                        '</div>';
 3675:         }
 3676:         $output .= '</p>';
 3677:     }
 3678:     $output .= '</fieldset>';
 3679:     return $output;
 3680: }
 3681: 
 3682: sub radiobutton_prefs {
 3683:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 3684:         $additional,$align) = @_;
 3685:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 3686:                    (ref($choices) eq 'HASH'));
 3687: 
 3688:     my (%checkedon,%checkedoff,$datatable,$css_class);
 3689: 
 3690:     foreach my $item (@{$toggles}) {
 3691:         if ($defaultchecked->{$item} eq 'on') {
 3692:             $checkedon{$item} = ' checked="checked" ';
 3693:             $checkedoff{$item} = ' ';
 3694:         } elsif ($defaultchecked->{$item} eq 'off') {
 3695:             $checkedoff{$item} = ' checked="checked" ';
 3696:             $checkedon{$item} = ' ';
 3697:         }
 3698:     }
 3699:     if (ref($settings) eq 'HASH') {
 3700:         foreach my $item (@{$toggles}) {
 3701:             if ($settings->{$item} eq '1') {
 3702:                 $checkedon{$item} =  ' checked="checked" ';
 3703:                 $checkedoff{$item} = ' ';
 3704:             } elsif ($settings->{$item} eq '0') {
 3705:                 $checkedoff{$item} =  ' checked="checked" ';
 3706:                 $checkedon{$item} = ' ';
 3707:             }
 3708:         }
 3709:     }
 3710:     if ($onclick) {
 3711:         $onclick = ' onclick="'.$onclick.'"';
 3712:     }
 3713:     foreach my $item (@{$toggles}) {
 3714:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3715:         $datatable .=
 3716:             '<tr'.$css_class.'><td valign="top">'.
 3717:             '<span class="LC_nobreak">'.$choices->{$item}.
 3718:             '</span></td>';
 3719:         if ($align eq 'left') {
 3720:             $datatable .= '<td class="LC_left_item">';
 3721:         } else {
 3722:             $datatable .= '<td class="LC_right_item">';
 3723:         }
 3724:         $datatable .=
 3725:             '<span class="LC_nobreak">'.
 3726:             '<label><input type="radio" name="'.
 3727:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 3728:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 3729:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 3730:             '</span>'.$additional.
 3731:             '</td>'.
 3732:             '</tr>';
 3733:         $itemcount ++;
 3734:     }
 3735:     return ($datatable,$itemcount);
 3736: }
 3737: 
 3738: sub print_ltitools {
 3739:     my ($dom,$settings,$rowtotal) = @_;
 3740:     my $rownum = 0;
 3741:     my $css_class;
 3742:     my $itemcount = 1;
 3743:     my $maxnum = 0;
 3744:     my %ordered;
 3745:     if (ref($settings) eq 'HASH') {
 3746:         foreach my $item (keys(%{$settings})) {
 3747:             if (ref($settings->{$item}) eq 'HASH') {
 3748:                 my $num = $settings->{$item}{'order'};
 3749:                 $ordered{$num} = $item;
 3750:             }
 3751:         }
 3752:     }
 3753:     my $confname = $dom.'-domainconfig';
 3754:     my $switchserver = &check_switchserver($dom,$confname);
 3755:     my $maxnum = scalar(keys(%ordered));
 3756:     my $datatable = &ltitools_javascript($settings);
 3757:     my %lt = &ltitools_names();
 3758:     my @courseroles = ('cc','in','ta','ep','st');
 3759:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 3760:     my @fields = ('fullname','firstname','lastname','email','user','roles');
 3761:     if (keys(%ordered)) {
 3762:         my @items = sort { $a <=> $b } keys(%ordered);
 3763:         for (my $i=0; $i<@items; $i++) {
 3764:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3765:             my $item = $ordered{$items[$i]};
 3766:             my ($title,$key,$secret,$url,$imgsrc,$version);
 3767:             if (ref($settings->{$item}) eq 'HASH') {
 3768:                 $title = $settings->{$item}->{'title'};
 3769:                 $url = $settings->{$item}->{'url'};
 3770:                 $key = $settings->{$item}->{'key'};
 3771:                 $secret = $settings->{$item}->{'secret'};
 3772:                 my $image = $settings->{$item}->{'image'};
 3773:                 if ($image ne '') {
 3774:                     $imgsrc = '<img src="'.$image.'" alt="'.&mt('Tool Provider icon').'" />';
 3775:                 }
 3776:             }
 3777:             my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'ltitools_".$item."'".');"';
 3778:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 3779:                          .'<select name="ltitools_'.$item.'"'.$chgstr.'>';
 3780:             for (my $k=0; $k<=$maxnum; $k++) {
 3781:                 my $vpos = $k+1;
 3782:                 my $selstr;
 3783:                 if ($k == $i) {
 3784:                     $selstr = ' selected="selected" ';
 3785:                 }
 3786:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3787:             }
 3788:             $datatable .= '</select>'.('&nbsp;'x2).
 3789:                 '<label><input type="checkbox" name="ltitools_del" value="'.$item.'" />'.
 3790:                 &mt('Delete?').'</label></span></td>'.
 3791:                 '<td colspan="2">'.
 3792:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 3793:                 '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="30" name="ltitools_title_'.$i.'" value="'.$title.'" /></span> '.
 3794:                 ('&nbsp;'x2).
 3795:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_version_'.$i.'">'.
 3796:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 3797:                 ('&nbsp;'x2).
 3798:                 '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_msgtype_'.$i.'">'.
 3799:                 '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 3800:                 '<br /><br />'.
 3801:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="30" name="ltitools_url_'.$i.'"'.
 3802:                 ' value="'.$url.'" /></span>'.
 3803:                 ('&nbsp;'x2).
 3804:                 '<span class="LC_nobreak">'.$lt{'key'}.
 3805:                 '<input type="text" size="25" name="ltitools_key_'.$i.'" value="'.$key.'" /></span> '.
 3806:                 ('&nbsp;'x2).
 3807:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 3808:                 '<input type="password" size="20" name="ltitools_secret_'.$i.'" value="'.$secret.'" />'.
 3809:                 '<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>'.
 3810:                 '<input type="hidden" name="ltitools_id_'.$i.'" value="'.$item.'" /></span>'.
 3811:                 '</fieldset>'.
 3812:                 '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 3813:                 '<span class="LC_nobreak">'.&mt('Display target:');
 3814:             my %currdisp;
 3815:             if (ref($settings->{$item}->{'display'}) eq 'HASH') {
 3816:                 if ($settings->{$item}->{'display'}->{'target'} eq 'window') {
 3817:                     $currdisp{'window'} = ' checked="checked"';
 3818:                 } elsif ($settings->{$item}->{'display'}->{'target'} eq 'tab') {
 3819:                     $currdisp{'tab'} = ' checked="checked"';
 3820:                 } else {
 3821:                     $currdisp{'iframe'} = ' checked="checked"';
 3822:                 }
 3823:                 if ($settings->{$item}->{'display'}->{'width'} =~ /^(\d+)$/) {
 3824:                     $currdisp{'width'} = $1;
 3825:                 }
 3826:                 if ($settings->{$item}->{'display'}->{'height'} =~ /^(\d+)$/) {
 3827:                      $currdisp{'height'} = $1;
 3828:                 }
 3829:                 $currdisp{'linktext'} = $settings->{$item}->{'display'}->{'linktext'};
 3830:                 $currdisp{'explanation'} = $settings->{$item}->{'display'}->{'explanation'};
 3831:             } else {
 3832:                 $currdisp{'iframe'} = ' checked="checked"';
 3833:             }
 3834:             foreach my $disp ('iframe','tab','window') {
 3835:                 $datatable .= '<label><input type="radio" name="ltitools_target_'.$i.'" value="'.$disp.'"'.$currdisp{$disp}.' />'.
 3836:                               $lt{$disp}.'</label>'.('&nbsp;'x2);
 3837:             }
 3838:             $datatable .= ('&nbsp;'x4);
 3839:             foreach my $dimen ('width','height') {
 3840:                 $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 3841:                               '<input type="text" name="ltitools_'.$dimen.'_'.$i.'" size="5" value="'.$currdisp{$dimen}.'" /></label>'.
 3842:                               ('&nbsp;'x2);
 3843:             }
 3844:             $datatable .= '<br />'.
 3845:                           '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 3846:                           '<input type="text" name="ltitools_linktext_'.$i.'" size="25" value="'.$currdisp{'linktext'}.'" /></label></div>'.
 3847:                           '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 3848:                           '<textarea name="ltitools_explanation_'.$i.'" rows="5" cols="40">'.$currdisp{'explanation'}.
 3849:                           '</textarea></div><div style=""></div><br />';
 3850:             $datatable .= '<br />';
 3851:             foreach my $extra ('passback','roster') {
 3852:                 my $checkedon = '';
 3853:                 my $checkedoff = ' checked="checked"';
 3854:                 if ($settings->{$item}->{$extra}) {
 3855:                     $checkedon = $checkedoff;
 3856:                     $checkedoff = '';
 3857:                 }
 3858:                 $datatable .= $lt{$extra}.'&nbsp;'.
 3859:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="1"'.$checkedon.' />'.
 3860:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 3861:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="0"'.$checkedoff.' />'.
 3862:                               &mt('No').'</label>'.('&nbsp;'x4);
 3863:             }
 3864:             $datatable .= '<br /><br /><span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 3865:             if ($imgsrc) {
 3866:                 $datatable .= $imgsrc.
 3867:                               '<label><input type="checkbox" name="ltitools_image_del"'.
 3868:                               ' value="'.$item.'" />'.&mt('Delete?').'</label></span> '.
 3869:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 3870:             } else {
 3871:                 $datatable .= '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 3872:             }
 3873:             if ($switchserver) {
 3874:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3875:             } else {
 3876:                 $datatable .= '<input type="file" name="ltitools_image_'.$i.'" value="" />';
 3877:             }
 3878:             $datatable .= '</span></fieldset>';
 3879:             my (%checkedfields,%rolemaps);
 3880:             if (ref($settings->{$item}) eq 'HASH') {
 3881:                 if (ref($settings->{$item}->{'fields'}) eq 'HASH') {
 3882:                     %checkedfields = %{$settings->{$item}->{'fields'}};
 3883:                 }
 3884:                 if (ref($settings->{$item}->{'roles'}) eq 'HASH') {
 3885:                     %rolemaps = %{$settings->{$item}->{'roles'}};
 3886:                     $checkedfields{'roles'} = 1;
 3887:                 }
 3888:             }
 3889:             $datatable .= '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 3890:                           '<span class="LC_nobreak">';
 3891:             foreach my $field (@fields) {
 3892:                 my $checked;
 3893:                 if ($checkedfields{$field}) {
 3894:                     $checked = ' checked="checked"';
 3895:                 }
 3896:                 $datatable .= '<label>'.
 3897:                               '<input type="checkbox" name="ltitools_fields_'.$i.'" value="'.$field.'"'.$checked.' />'.
 3898:                               $lt{$field}.'</label>'.('&nbsp;' x2);
 3899:             }
 3900:             $datatable .= '</span></fieldset>'.
 3901:                           '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 3902:             foreach my $role (@courseroles) {
 3903:                 my ($selected,$selectnone);
 3904:                 if (!$rolemaps{$role}) {
 3905:                     $selectnone = ' selected="selected"';
 3906:                 }
 3907:                 $datatable .= '<td align="center">'.
 3908:                               &Apache::lonnet::plaintext($role,'Course').'<br />'.
 3909:                               '<select name="ltitools_roles_'.$role.'_'.$i.'">'.
 3910:                               '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 3911:                 foreach my $ltirole (@ltiroles) {
 3912:                     unless ($selectnone) {
 3913:                         if ($rolemaps{$role} eq $ltirole) {
 3914:                             $selected = ' selected="selected"';
 3915:                         } else {
 3916:                             $selected = '';
 3917:                         }
 3918:                     }
 3919:                     $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 3920:                 }
 3921:                 $datatable .= '</select></td>';
 3922:             }
 3923:             $datatable .= '</tr></table></fieldset>';
 3924:             my %courseconfig;
 3925:             if (ref($settings->{$item}) eq 'HASH') {
 3926:                 if (ref($settings->{$item}->{'crsconf'}) eq 'HASH') {
 3927:                     %courseconfig = %{$settings->{$item}->{'crsconf'}};
 3928:                 }
 3929:             }
 3930:             $datatable .= '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 3931:             foreach my $item ('label','title','target','linktext','explanation') {
 3932:                 my $checked;
 3933:                 if ($courseconfig{$item}) {
 3934:                     $checked = ' checked="checked"';
 3935:                 }
 3936:                 $datatable .= '<label>'.
 3937:                        '<input type="checkbox" name="ltitools_courseconfig_'.$i.'" value="'.$item.'"'.$checked.' />'.
 3938:                        $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 3939:             }
 3940:             $datatable .= '</span></fieldset>'.
 3941:                           '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 3942:                           '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>';
 3943:             if (ref($settings->{$item}->{'custom'}) eq 'HASH') {
 3944:                 my %custom = %{$settings->{$item}->{'custom'}};
 3945:                 if (keys(%custom) > 0) {
 3946:                     foreach my $key (sort(keys(%custom))) {
 3947:                         $datatable .= '<tr><td><span class="LC_nobreak">'.
 3948:                                       '<label><input type="checkbox" name="ltitools_customdel_'.$i.'" value="'.
 3949:                                       $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 3950:                                       '<td><input type="text" name="ltitools_customval_'.$key.'_'.$i.'"'.
 3951:                                       ' value="'.$custom{$key}.'" /></td></tr>';
 3952:                     }
 3953:                 }
 3954:             }
 3955:             $datatable .= '<tr><td><span class="LC_nobreak">'.
 3956:                           '<label><input type="checkbox" name="ltitools_customadd" value="'.$i.'" />'.
 3957:                           &mt('Add').'</label></span></td><td><input type="text" name="ltitools_custom_name_'.$i.'" />'.
 3958:                           '</td><td><input type="text" name="ltitools_custom_value_'.$i.'" /></td></tr>';
 3959:             $datatable .= '</table></fieldset></td></tr>'."\n";
 3960:             $itemcount ++;
 3961:         }
 3962:     }
 3963:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3964:     my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'ltitools_add_pos'".');"';
 3965:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 3966:                   '<input type="hidden" name="ltitools_maxnum" value="'.$maxnum.'" />'."\n".
 3967:                   '<select name="ltitools_add_pos"'.$chgstr.'>';
 3968:     for (my $k=0; $k<$maxnum+1; $k++) {
 3969:         my $vpos = $k+1;
 3970:         my $selstr;
 3971:         if ($k == $maxnum) {
 3972:             $selstr = ' selected="selected" ';
 3973:         }
 3974:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3975:     }
 3976:     $datatable .= '</select>&nbsp;'."\n".
 3977:                   '<input type="checkbox" name="ltitools_add" value="1" />'.&mt('Add').'</td>'."\n".
 3978:                   '<td colspan="2">'.
 3979:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 3980:                   '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="30" name="ltitools_add_title" value="" /></span> '."\n".
 3981:                   ('&nbsp;'x2).
 3982:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_add_version">'.
 3983:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 3984:                   ('&nbsp;'x2).
 3985:                   '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_add_msgtype">'.
 3986:                   '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 3987:                   '<br />'.
 3988:                   '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="30" name="ltitools_add_url" value="" /></span> '."\n".
 3989:                   ('&nbsp;'x2).
 3990:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="ltitools_add_key" value="" /></span> '."\n".
 3991:                   ('&nbsp;'x2).
 3992:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="ltitools_add_secret" value="" />'.
 3993:                   '<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".
 3994:                   '</fieldset>'.
 3995:                   '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 3996:                   '<span class="LC_nobreak">'.&mt('Display target:');
 3997:     my %defaultdisp;
 3998:     $defaultdisp{'iframe'} = ' checked="checked"';
 3999:     foreach my $disp ('iframe','tab','window') {
 4000:         $datatable .= '<label><input type="radio" name="ltitools_add_target" value="'.$disp.'"'.$defaultdisp{$disp}.' />'.
 4001:                       $lt{$disp}.'</label>'.('&nbsp;'x2);
 4002:     }
 4003:     $datatable .= ('&nbsp;'x4);
 4004:     foreach my $dimen ('width','height') {
 4005:         $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 4006:                       '<input type="text" name="ltitools_add_'.$dimen.'" size="5" /></label>'.
 4007:                       ('&nbsp;'x2);
 4008:     }
 4009:     $datatable .= '<br />'.
 4010:                   '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 4011:                   '<input type="text" name="ltitools_add_linktext" size="5" /></label></div>'.
 4012:                   '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 4013:                   '<textarea name=ltitools_add_explanation" rows="5" cols="40"></textarea>'.
 4014:                   '</div><div style=""></div><br />';
 4015:     foreach my $extra ('passback','roster') {
 4016:         $datatable .= $lt{$extra}.'&nbsp;'.
 4017:                       '<label><input type="radio" name="ltitools_add_'.$extra.'" value="1" />'.
 4018:                       &mt('Yes').'</label>'.('&nbsp;'x2).
 4019:                       '<label><input type="radio" name="ltitools_add_'.$extra.'" value="0" checked="checked" />'.
 4020:                       &mt('No').'</label>'.('&nbsp;'x4);
 4021:     }
 4022:     $datatable .= '<br /><br /><span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;'.
 4023:                   '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 4024:     if ($switchserver) {
 4025:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4026:     } else {
 4027:         $datatable .= '<input type="file" name="ltitools_add_image" value="" />';
 4028:     }
 4029:     $datatable .= '</span></fieldset>'.
 4030:                   '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 4031:                   '<span class="LC_nobreak">';
 4032:     foreach my $field (@fields) {
 4033:         $datatable .= '<label>'.
 4034:                       '<input type="checkbox" name="ltitools_add_fields" value="'.$field.'" />'.
 4035:                       $lt{$field}.'</label>'.('&nbsp;' x2);
 4036:     }
 4037:     $datatable .= '</span></fieldset>'.
 4038:                   '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 4039:     foreach my $role (@courseroles) {
 4040:         my ($checked,$checkednone);
 4041:         $datatable .= '<td align="center">'.
 4042:                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 4043:                       '<select name="ltitools_add_roles_'.$role.'">'.
 4044:                       '<option value="" selected="selected">'.&mt('Select').'</option>';
 4045:         foreach my $ltirole (@ltiroles) {
 4046:             $datatable .= '<option value="'.$ltirole.'">'.$ltirole.'</option>';
 4047:         }
 4048:         $datatable .= '</select></td>';
 4049:     }
 4050:     $datatable .= '</tr></table></fieldset>'.
 4051:                   '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 4052:     foreach my $item ('label','title','target','linktext','explanation') {
 4053:         $datatable .= '<label>'.
 4054:                       '<input type="checkbox" name="ltitools_courseconfig" value="'.$item.'" checked="checked" />'.
 4055:                       $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 4056:     }
 4057:     $datatable .= '</span></fieldset>'.
 4058:                   '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 4059:                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 4060:                   '<tr><td><span class="LC_nobreak">'.
 4061:                   '<label><input type="checkbox" name="ltitools_add_custom" value="1" />'.
 4062:                   &mt('Add').'</label></span></td><td><input type="text" name="ltitools_add_custom_name" />'.
 4063:                   '</td><td><input type="text" name="ltitools_add_custom_value" /></td></tr>'.
 4064:                   '</table></fieldset></td></tr>'."\n".
 4065:                   '</td>'."\n".
 4066:                   '</tr>'."\n";
 4067:     $itemcount ++;
 4068:     return $datatable;
 4069: }
 4070: 
 4071: sub ltitools_names {
 4072:     my %lt = &Apache::lonlocal::texthash(
 4073:                                           'title'          => 'Title',
 4074:                                           'version'        => 'Version',
 4075:                                           'msgtype'        => 'Message Type',
 4076:                                           'url'            => 'URL',
 4077:                                           'key'            => 'Key',
 4078:                                           'secret'         => 'Secret',
 4079:                                           'icon'           => 'Icon',
 4080:                                           'user'           => 'Username:domain',
 4081:                                           'fullname'       => 'Full Name',
 4082:                                           'firstname'      => 'First Name',
 4083:                                           'lastname'       => 'Last Name',
 4084:                                           'email'          => 'E-mail',
 4085:                                           'roles'          => 'Role',
 4086:                                           'window'         => 'Window',
 4087:                                           'tab'            => 'Tab',
 4088:                                           'iframe'         => 'iFrame',
 4089:                                           'height'         => 'Height',
 4090:                                           'width'          => 'Width',
 4091:                                           'linktext'       => 'Default Link Text',
 4092:                                           'explanation'    => 'Default Explanation',
 4093:                                           'passback'       => 'Tool can return grades:',
 4094:                                           'roster'         => 'Tool can retrieve roster:',
 4095:                                           'crstarget'      => 'Display target',
 4096:                                           'crslabel'       => 'Course label',
 4097:                                           'crstitle'       => 'Course title',
 4098:                                           'crslinktext'    => 'Link Text',
 4099:                                           'crsexplanation' => 'Explanation',
 4100:                                         );
 4101: 
 4102:     return %lt;
 4103: }
 4104: 
 4105: sub print_coursedefaults {
 4106:     my ($position,$dom,$settings,$rowtotal) = @_;
 4107:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 4108:     my $itemcount = 1;
 4109:     my %choices =  &Apache::lonlocal::texthash (
 4110:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 4111:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 4112:         coursecredits        => 'Credits can be specified for courses',
 4113:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 4114:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 4115:         postsubmit           => 'Disable submit button/keypress following student submission',
 4116:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 4117:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 4118:     );
 4119:     my %staticdefaults = (
 4120:                            anonsurvey_threshold => 10,
 4121:                            uploadquota          => 500,
 4122:                            postsubmit           => 60,
 4123:                            mysqltables          => 172800,
 4124:                          );
 4125:     if ($position eq 'top') {
 4126:         %defaultchecked = (
 4127:                             'uselcmath'       => 'on',
 4128:                             'usejsme'         => 'on',
 4129:                             'canclone'        => 'none',
 4130:                           );
 4131:         @toggles = ('uselcmath','usejsme');
 4132:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4133:                                                      \%choices,$itemcount);
 4134:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4135:         $datatable .=
 4136:             '<tr'.$css_class.'><td valign="top">'.
 4137:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 4138:             '</span></td><td class="LC_left_item">';
 4139:         my $currcanclone = 'none';
 4140:         my $onclick;
 4141:         my @cloneoptions = ('none','domain');
 4142:         my %clonetitles = (
 4143:                              none     => 'No additional course requesters',
 4144:                              domain   => "Any course requester in course's domain",
 4145:                              instcode => 'Course requests for official courses ...',
 4146:                           );
 4147:         my (%codedefaults,@code_order,@posscodes);
 4148:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 4149:                                                     \@code_order) eq 'ok') {
 4150:             if (@code_order > 0) {
 4151:                 push(@cloneoptions,'instcode');
 4152:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 4153:             }
 4154:         }
 4155:         if (ref($settings) eq 'HASH') {
 4156:             if ($settings->{'canclone'}) {
 4157:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 4158:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 4159:                         if (@code_order > 0) {
 4160:                             $currcanclone = 'instcode';
 4161:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 4162:                         }
 4163:                     }
 4164:                 } elsif ($settings->{'canclone'} eq 'domain') {
 4165:                     $currcanclone = $settings->{'canclone'};
 4166:                 }
 4167:             }
 4168:         }
 4169:         foreach my $option (@cloneoptions) {
 4170:             my ($checked,$additional);
 4171:             if ($currcanclone eq $option) {
 4172:                 $checked = ' checked="checked"';
 4173:             }
 4174:             if ($option eq 'instcode') {
 4175:                 if (@code_order) {
 4176:                     my $show = 'none';
 4177:                     if ($checked) {
 4178:                         $show = 'block';
 4179:                     }
 4180:                     $additional = '<div id="cloneinstcode" style="display:'.$show.'" />'.
 4181:                                   &mt('Institutional codes for new and cloned course have identical:').
 4182:                                   '<br />';
 4183:                     foreach my $item (@code_order) {
 4184:                         my $codechk;
 4185:                         if ($checked) {
 4186:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 4187:                                 $codechk = ' checked="checked"';
 4188:                             }
 4189:                         }
 4190:                         $additional .= '<label>'.
 4191:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 4192:                                        $item.'</label>';
 4193:                     }
 4194:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 4195:                 }
 4196:             }
 4197:             $datatable .=
 4198:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 4199:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 4200:                 '</label>&nbsp;'.$additional.'</span><br />';
 4201:         }
 4202:         $datatable .= '</td>'.
 4203:                       '</tr>';
 4204:         $itemcount ++;
 4205:     } else {
 4206:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4207:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 4208:         my $currusecredits = 0;
 4209:         my $postsubmitclient = 1;
 4210:         my @types = ('official','unofficial','community','textbook');
 4211:         if (ref($settings) eq 'HASH') {
 4212:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 4213:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 4214:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 4215:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 4216:                 }
 4217:             }
 4218:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 4219:                 foreach my $type (@types) {
 4220:                     next if ($type eq 'community');
 4221:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 4222:                     if ($defcredits{$type} ne '') {
 4223:                         $currusecredits = 1;
 4224:                     }
 4225:                 }
 4226:             }
 4227:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 4228:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 4229:                     $postsubmitclient = 0;
 4230:                     foreach my $type (@types) {
 4231:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4232:                     }
 4233:                 } else {
 4234:                     foreach my $type (@types) {
 4235:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 4236:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 4237:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 4238:                             } else {
 4239:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4240:                             }
 4241:                         } else {
 4242:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4243:                         }
 4244:                     }
 4245:                 }
 4246:             } else {
 4247:                 foreach my $type (@types) {
 4248:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4249:                 }
 4250:             }
 4251:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 4252:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 4253:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 4254:                 }
 4255:             } else {
 4256:                 foreach my $type (@types) {
 4257:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 4258:                 }
 4259:             }
 4260:         } else {
 4261:             foreach my $type (@types) {
 4262:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4263:             }
 4264:         }
 4265:         if (!$currdefresponder) {
 4266:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 4267:         } elsif ($currdefresponder < 1) {
 4268:             $currdefresponder = 1;
 4269:         }
 4270:         foreach my $type (@types) {
 4271:             if ($curruploadquota{$type} eq '') {
 4272:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 4273:             }
 4274:         }
 4275:         $datatable .=
 4276:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4277:                 $choices{'anonsurvey_threshold'}.
 4278:                 '</span></td>'.
 4279:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 4280:                 '<input type="text" name="anonsurvey_threshold"'.
 4281:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 4282:                 '</td></tr>'."\n";
 4283:         $itemcount ++;
 4284:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4285:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4286:                       $choices{'uploadquota'}.
 4287:                       '</span></td>'.
 4288:                       '<td align="right" class="LC_right_item">'.
 4289:                       '<table><tr>';
 4290:         foreach my $type (@types) {
 4291:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 4292:                            '<input type="text" name="uploadquota_'.$type.'"'.
 4293:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 4294:         }
 4295:         $datatable .= '</tr></table></td></tr>'."\n";
 4296:         $itemcount ++;
 4297:         my $onclick = "toggleDisplay(this.form,'credits');";
 4298:         my $display = 'none';
 4299:         if ($currusecredits) {
 4300:             $display = 'block';
 4301:         }
 4302:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 4303:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 4304:         foreach my $type (@types) {
 4305:             next if ($type eq 'community');
 4306:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 4307:                            '<input type="text" name="'.$type.'_credits"'.
 4308:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 4309:         }
 4310:         $additional .= '</tr></table></div>'."\n";
 4311:         %defaultchecked = ('coursecredits' => 'off');
 4312:         @toggles = ('coursecredits');
 4313:         my $current = {
 4314:                         'coursecredits' => $currusecredits,
 4315:                       };
 4316:         (my $table,$itemcount) =
 4317:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 4318:                                \%choices,$itemcount,$onclick,$additional,'left');
 4319:         $datatable .= $table;
 4320:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 4321:         my $display = 'none';
 4322:         if ($postsubmitclient) {
 4323:             $display = 'block';
 4324:         }
 4325:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 4326:                       &mt('Number of seconds submit is disabled').'<br />'.
 4327:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 4328:                       '<table><tr>';
 4329:         foreach my $type (@types) {
 4330:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 4331:                            '<input type="text" name="'.$type.'_timeout" value="'.
 4332:                            $deftimeout{$type}.'" size="5" /></td>';
 4333:         }
 4334:         $additional .= '</tr></table></div>'."\n";
 4335:         %defaultchecked = ('postsubmit' => 'on');
 4336:         @toggles = ('postsubmit');
 4337:         $current = {
 4338:                        'postsubmit' => $postsubmitclient,
 4339:                    };
 4340:         ($table,$itemcount) =
 4341:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 4342:                                \%choices,$itemcount,$onclick,$additional,'left');
 4343:         $datatable .= $table;
 4344:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4345:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4346:                       $choices{'mysqltables'}.
 4347:                       '</span></td>'.
 4348:                       '<td align="right" class="LC_right_item">'.
 4349:                       '<table><tr>';
 4350:         foreach my $type (@types) {
 4351:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 4352:                            '<input type="text" name="mysqltables_'.$type.'"'.
 4353:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 4354:         }
 4355:         $datatable .= '</tr></table></td></tr>'."\n";
 4356:         $itemcount ++;
 4357: 
 4358:     }
 4359:     $$rowtotal += $itemcount;
 4360:     return $datatable;
 4361: }
 4362: 
 4363: sub print_selfenrollment {
 4364:     my ($position,$dom,$settings,$rowtotal) = @_;
 4365:     my ($css_class,$datatable);
 4366:     my $itemcount = 1;
 4367:     my @types = ('official','unofficial','community','textbook');
 4368:     if (($position eq 'top') || ($position eq 'middle')) {
 4369:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 4370:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 4371:         my @rows;
 4372:         my $key;
 4373:         if ($position eq 'top') {
 4374:             $key = 'admin'; 
 4375:             if (ref($rowsref) eq 'ARRAY') {
 4376:                 @rows = @{$rowsref};
 4377:             }
 4378:         } elsif ($position eq 'middle') {
 4379:             $key = 'default';
 4380:             @rows = ('types','registered','approval','limit');
 4381:         }
 4382:         foreach my $row (@rows) {
 4383:             if (defined($titlesref->{$row})) {
 4384:                 $itemcount ++;
 4385:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4386:                 $datatable .= '<tr'.$css_class.'>'.
 4387:                               '<td>'.$titlesref->{$row}.'</td>'.
 4388:                               '<td class="LC_left_item">'.
 4389:                               '<table><tr>';
 4390:                 my (%current,%currentcap);
 4391:                 if (ref($settings) eq 'HASH') {
 4392:                     if (ref($settings->{$key}) eq 'HASH') {
 4393:                         foreach my $type (@types) {
 4394:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4395:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 4396:                             }
 4397:                             if (($row eq 'limit') && ($key eq 'default')) {
 4398:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4399:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 4400:                                 }
 4401:                             }
 4402:                         }
 4403:                     }
 4404:                 }
 4405:                 my %roles = (
 4406:                              '0' => &Apache::lonnet::plaintext('dc'),
 4407:                             ); 
 4408:             
 4409:                 foreach my $type (@types) {
 4410:                     unless (($row eq 'registered') && ($key eq 'default')) {
 4411:                         $datatable .= '<th>'.&mt($type).'</th>';
 4412:                     }
 4413:                 }
 4414:                 unless (($row eq 'registered') && ($key eq 'default')) {
 4415:                     $datatable .= '</tr><tr>';
 4416:                 }
 4417:                 foreach my $type (@types) {
 4418:                     if ($type eq 'community') {
 4419:                         $roles{'1'} = &mt('Community personnel');
 4420:                     } else {
 4421:                         $roles{'1'} = &mt('Course personnel');
 4422:                     }
 4423:                     $datatable .= '<td style="vertical-align: top">';
 4424:                     if ($position eq 'top') {
 4425:                         my %checked;
 4426:                         if ($current{$type} eq '0') {
 4427:                             $checked{'0'} = ' checked="checked"';
 4428:                         } else {
 4429:                             $checked{'1'} = ' checked="checked"';
 4430:                         }
 4431:                         foreach my $role ('1','0') {
 4432:                             $datatable .= '<span class="LC_nobreak"><label>'.
 4433:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 4434:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 4435:                                           $roles{$role}.'</label></span> ';
 4436:                         }
 4437:                     } else {
 4438:                         if ($row eq 'types') {
 4439:                             my %checked;
 4440:                             if ($current{$type} =~ /^(all|dom)$/) {
 4441:                                 $checked{$1} = ' checked="checked"';
 4442:                             } else {
 4443:                                 $checked{''} = ' checked="checked"';
 4444:                             }
 4445:                             foreach my $val ('','dom','all') {
 4446:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4447:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4448:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4449:                             }
 4450:                         } elsif ($row eq 'registered') {
 4451:                             my %checked;
 4452:                             if ($current{$type} eq '1') {
 4453:                                 $checked{'1'} = ' checked="checked"';
 4454:                             } else {
 4455:                                 $checked{'0'} = ' checked="checked"';
 4456:                             }
 4457:                             foreach my $val ('0','1') {
 4458:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4459:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4460:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4461:                             }
 4462:                         } elsif ($row eq 'approval') {
 4463:                             my %checked;
 4464:                             if ($current{$type} =~ /^([12])$/) {
 4465:                                 $checked{$1} = ' checked="checked"';
 4466:                             } else {
 4467:                                 $checked{'0'} = ' checked="checked"';
 4468:                             }
 4469:                             for my $val (0..2) {
 4470:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4471:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4472:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4473:                             }
 4474:                         } elsif ($row eq 'limit') {
 4475:                             my %checked;
 4476:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 4477:                                 $checked{$1} = ' checked="checked"';
 4478:                             } else {
 4479:                                 $checked{'none'} = ' checked="checked"';
 4480:                             }
 4481:                             my $cap;
 4482:                             if ($currentcap{$type} =~ /^\d+$/) {
 4483:                                 $cap = $currentcap{$type};
 4484:                             }
 4485:                             foreach my $val ('none','allstudents','selfenrolled') {
 4486:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4487:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4488:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4489:                             }
 4490:                             $datatable .= '<br />'.
 4491:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 4492:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 4493:                                           '</span>'; 
 4494:                         }
 4495:                     }
 4496:                     $datatable .= '</td>';
 4497:                 }
 4498:                 $datatable .= '</tr>';
 4499:             }
 4500:             $datatable .= '</table></td></tr>';
 4501:         }
 4502:     } elsif ($position eq 'bottom') {
 4503:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 4504:     }
 4505:     $$rowtotal += $itemcount;
 4506:     return $datatable;
 4507: }
 4508: 
 4509: sub print_validation_rows {
 4510:     my ($caller,$dom,$settings,$rowtotal) = @_;
 4511:     my ($itemsref,$namesref,$fieldsref);
 4512:     if ($caller eq 'selfenroll') { 
 4513:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 4514:     } elsif ($caller eq 'requestcourses') {
 4515:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 4516:     }
 4517:     my %currvalidation;
 4518:     if (ref($settings) eq 'HASH') {
 4519:         if (ref($settings->{'validation'}) eq 'HASH') {
 4520:             %currvalidation = %{$settings->{'validation'}};
 4521:         }
 4522:     }
 4523:     my $datatable;
 4524:     my $itemcount = 0;
 4525:     foreach my $item (@{$itemsref}) {
 4526:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4527:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4528:                       $namesref->{$item}.
 4529:                       '</span></td>'.
 4530:                       '<td class="LC_left_item">';
 4531:         if (($item eq 'url') || ($item eq 'button')) {
 4532:             $datatable .= '<span class="LC_nobreak">'.
 4533:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 4534:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 4535:         } elsif ($item eq 'fields') {
 4536:             my @currfields;
 4537:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 4538:                 @currfields = @{$currvalidation{$item}};
 4539:             }
 4540:             foreach my $field (@{$fieldsref}) {
 4541:                 my $check = '';
 4542:                 if (grep(/^\Q$field\E$/,@currfields)) {
 4543:                     $check = ' checked="checked"';
 4544:                 }
 4545:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4546:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 4547:                               ' value="'.$field.'"'.$check.' />'.$field.
 4548:                               '</label></span> ';
 4549:             }
 4550:         } elsif ($item eq 'markup') {
 4551:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5" wrap="soft">'.
 4552:                            $currvalidation{$item}.
 4553:                               '</textarea>';
 4554:         }
 4555:         $datatable .= '</td></tr>'."\n";
 4556:         if (ref($rowtotal)) {
 4557:             $itemcount ++;
 4558:         }
 4559:     }
 4560:     if ($caller eq 'requestcourses') {
 4561:         my %currhash;
 4562:         if (ref($settings) eq 'HASH') {
 4563:             if (ref($settings->{'validation'}) eq 'HASH') {
 4564:                 if ($settings->{'validation'}{'dc'} ne '') {
 4565:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 4566:                 }
 4567:             }
 4568:         }
 4569:         my $numinrow = 2;
 4570:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 4571:                                                        'validationdc',%currhash);
 4572:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4573:         $datatable .= '</td></tr><tr'.$css_class.'><td>';
 4574:         if ($numdc > 1) {
 4575:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 4576:         } else {
 4577:             $datatable .=  &mt('Course creation processed as: ');
 4578:         }
 4579:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 4580:         $itemcount ++;
 4581:     }
 4582:     if (ref($rowtotal)) {
 4583:         $$rowtotal += $itemcount;
 4584:     }
 4585:     return $datatable;
 4586: }
 4587: 
 4588: sub print_usersessions {
 4589:     my ($position,$dom,$settings,$rowtotal) = @_;
 4590:     my ($css_class,$datatable,%checked,%choices);
 4591:     my (%by_ip,%by_location,@intdoms);
 4592:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 4593: 
 4594:     my @alldoms = &Apache::lonnet::all_domains();
 4595:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 4596:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4597:     my %altids = &id_for_thisdom(%servers);
 4598:     my $itemcount = 1;
 4599:     if ($position eq 'top') {
 4600:         if (keys(%serverhomes) > 1) {
 4601:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 4602:             my $curroffloadnow;
 4603:             if (ref($settings) eq 'HASH') {
 4604:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 4605:                     $curroffloadnow = $settings->{'offloadnow'};
 4606:                 }
 4607:             }
 4608:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$curroffloadnow,$rowtotal);
 4609:         } else {
 4610:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 4611:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 4612:         }
 4613:     } else {
 4614:         if (keys(%by_location) == 0) {
 4615:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 4616:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 4617:         } else {
 4618:             my %lt = &usersession_titles();
 4619:             my $numinrow = 5;
 4620:             my $prefix;
 4621:             my @types;
 4622:             if ($position eq 'bottom') {
 4623:                 $prefix = 'remote';
 4624:                 @types = ('version','excludedomain','includedomain');
 4625:             } else {
 4626:                 $prefix = 'hosted';
 4627:                 @types = ('excludedomain','includedomain');
 4628:             }
 4629:             my (%current,%checkedon,%checkedoff);
 4630:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 4631:             my @locations = sort(keys(%by_location));
 4632:             foreach my $type (@types) {
 4633:                 $checkedon{$type} = '';
 4634:                 $checkedoff{$type} = ' checked="checked"';
 4635:             }
 4636:             if (ref($settings) eq 'HASH') {
 4637:                 if (ref($settings->{$prefix}) eq 'HASH') {
 4638:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 4639:                         $current{$key} = $settings->{$prefix}{$key};
 4640:                         if ($key eq 'version') {
 4641:                             if ($current{$key} ne '') {
 4642:                                 $checkedon{$key} = ' checked="checked"';
 4643:                                 $checkedoff{$key} = '';
 4644:                             }
 4645:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 4646:                             $checkedon{$key} = ' checked="checked"';
 4647:                             $checkedoff{$key} = '';
 4648:                         }
 4649:                     }
 4650:                 }
 4651:             }
 4652:             foreach my $type (@types) {
 4653:                 next if ($type ne 'version' && !@locations);
 4654:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4655:                 $datatable .= '<tr'.$css_class.'>
 4656:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 4657:                                <span class="LC_nobreak">&nbsp;
 4658:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 4659:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 4660:                 if ($type eq 'version') {
 4661:                     my $selector = '<select name="'.$prefix.'_version">';
 4662:                     foreach my $version (@lcversions) {
 4663:                         my $selected = '';
 4664:                         if ($current{'version'} eq $version) {
 4665:                             $selected = ' selected="selected"';
 4666:                         }
 4667:                         $selector .= ' <option value="'.$version.'"'.
 4668:                                      $selected.'>'.$version.'</option>';
 4669:                     }
 4670:                     $selector .= '</select> ';
 4671:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 4672:                 } else {
 4673:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 4674:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 4675:                                  ' />'.('&nbsp;'x2).
 4676:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 4677:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 4678:                                  "\n".
 4679:                                  '</div><div><table>';
 4680:                     my $rem;
 4681:                     for (my $i=0; $i<@locations; $i++) {
 4682:                         my ($showloc,$value,$checkedtype);
 4683:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 4684:                             my $ip = $by_location{$locations[$i]}->[0];
 4685:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 4686:                                  $value = join(':',@{$by_ip{$ip}});
 4687:                                 $showloc = join(', ',@{$by_ip{$ip}});
 4688:                                 if (ref($current{$type}) eq 'ARRAY') {
 4689:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 4690:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 4691:                                             $checkedtype = ' checked="checked"';
 4692:                                             last;
 4693:                                         }
 4694:                                     }
 4695:                                 }
 4696:                             }
 4697:                         }
 4698:                         $rem = $i%($numinrow);
 4699:                         if ($rem == 0) {
 4700:                             if ($i > 0) {
 4701:                                 $datatable .= '</tr>';
 4702:                             }
 4703:                             $datatable .= '<tr>';
 4704:                         }
 4705:                         $datatable .= '<td class="LC_left_item">'.
 4706:                                       '<span class="LC_nobreak"><label>'.
 4707:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 4708:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 4709:                                       '</label></span></td>';
 4710:                     }
 4711:                     $rem = @locations%($numinrow);
 4712:                     my $colsleft = $numinrow - $rem;
 4713:                     if ($colsleft > 1 ) {
 4714:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4715:                                       '&nbsp;</td>';
 4716:                     } elsif ($colsleft == 1) {
 4717:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4718:                     }
 4719:                     $datatable .= '</tr></table>';
 4720:                 }
 4721:                 $datatable .= '</td></tr>';
 4722:                 $itemcount ++;
 4723:             }
 4724:         }
 4725:     }
 4726:     $$rowtotal += $itemcount;
 4727:     return $datatable;
 4728: }
 4729: 
 4730: sub build_location_hashes {
 4731:     my ($intdoms,$by_ip,$by_location) = @_;
 4732:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 4733:                   (ref($by_location) eq 'HASH')); 
 4734:     my %iphost = &Apache::lonnet::get_iphost();
 4735:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 4736:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 4737:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 4738:         foreach my $id (@{$iphost{$primary_ip}}) {
 4739:             my $intdom = &Apache::lonnet::internet_dom($id);
 4740:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 4741:                 push(@{$intdoms},$intdom);
 4742:             }
 4743:         }
 4744:     }
 4745:     foreach my $ip (keys(%iphost)) {
 4746:         if (ref($iphost{$ip}) eq 'ARRAY') {
 4747:             foreach my $id (@{$iphost{$ip}}) {
 4748:                 my $location = &Apache::lonnet::internet_dom($id);
 4749:                 if ($location) {
 4750:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 4751:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4752:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 4753:                             push(@{$by_ip->{$ip}},$location);
 4754:                         }
 4755:                     } else {
 4756:                         $by_ip->{$ip} = [$location];
 4757:                     }
 4758:                 }
 4759:             }
 4760:         }
 4761:     }
 4762:     foreach my $ip (sort(keys(%{$by_ip}))) {
 4763:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4764:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 4765:             my $first = $by_ip->{$ip}->[0];
 4766:             if (ref($by_location->{$first}) eq 'ARRAY') {
 4767:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 4768:                     push(@{$by_location->{$first}},$ip);
 4769:                 }
 4770:             } else {
 4771:                 $by_location->{$first} = [$ip];
 4772:             }
 4773:         }
 4774:     }
 4775:     return;
 4776: }
 4777: 
 4778: sub current_offloads_to {
 4779:     my ($dom,$settings,$servers) = @_;
 4780:     my (%spareid,%otherdomconfigs);
 4781:     if (ref($servers) eq 'HASH') {
 4782:         foreach my $lonhost (sort(keys(%{$servers}))) {
 4783:             my $gotspares;
 4784:             if (ref($settings) eq 'HASH') {
 4785:                 if (ref($settings->{'spares'}) eq 'HASH') {
 4786:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 4787:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 4788:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 4789:                         $gotspares = 1;
 4790:                     }
 4791:                 }
 4792:             }
 4793:             unless ($gotspares) {
 4794:                 my $gotspares;
 4795:                 my $serverhomeID =
 4796:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 4797:                 my $serverhomedom =
 4798:                     &Apache::lonnet::host_domain($serverhomeID);
 4799:                 if ($serverhomedom ne $dom) {
 4800:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 4801:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4802:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4803:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4804:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4805:                                 $gotspares = 1;
 4806:                             }
 4807:                         }
 4808:                     } else {
 4809:                         $otherdomconfigs{$serverhomedom} =
 4810:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 4811:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 4812:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4813:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4814:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 4815:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4816:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4817:                                         $gotspares = 1;
 4818:                                     }
 4819:                                 }
 4820:                             }
 4821:                         }
 4822:                     }
 4823:                 }
 4824:             }
 4825:             unless ($gotspares) {
 4826:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4827:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4828:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4829:                } else {
 4830:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 4831:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 4832:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4833:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4834:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4835:                     } else {
 4836:                         my %what = (
 4837:                              spareid => 1,
 4838:                         );
 4839:                         my ($result,$returnhash) = 
 4840:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 4841:                         if ($result eq 'ok') { 
 4842:                             if (ref($returnhash) eq 'HASH') {
 4843:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 4844:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 4845:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 4846:                                 }
 4847:                             }
 4848:                         }
 4849:                     }
 4850:                 }
 4851:             }
 4852:         }
 4853:     }
 4854:     return %spareid;
 4855: }
 4856: 
 4857: sub spares_row {
 4858:     my ($dom,$servers,$spareid,$serverhomes,$altids,$curroffloadnow,$rowtotal) = @_;
 4859:     my $css_class;
 4860:     my $numinrow = 4;
 4861:     my $itemcount = 1;
 4862:     my $datatable;
 4863:     my %typetitles = &sparestype_titles();
 4864:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 4865:         foreach my $server (sort(keys(%{$servers}))) {
 4866:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 4867:             my ($othercontrol,$serverdom);
 4868:             if ($serverhome ne $server) {
 4869:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 4870:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 4871:             } else {
 4872:                 $serverdom = &Apache::lonnet::host_domain($server);
 4873:                 if ($serverdom ne $dom) {
 4874:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 4875:                 }
 4876:             }
 4877:             next unless (ref($spareid->{$server}) eq 'HASH');
 4878:             my $checkednow;
 4879:             if (ref($curroffloadnow) eq 'HASH') {
 4880:                 if ($curroffloadnow->{$server}) {
 4881:                     $checkednow = ' checked="checked"';
 4882:                 }
 4883:             }
 4884:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4885:             $datatable .= '<tr'.$css_class.'>
 4886:                            <td rowspan="2">
 4887:                             <span class="LC_nobreak">'.
 4888:                           &mt('[_1] when busy, offloads to:'
 4889:                               ,'<b>'.$server.'</b>').'</span><br />'.
 4890:                           '<span class="LC_nobreak">'."\n".
 4891:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 4892:                           '&nbsp;'.&mt('Switch active users on next access').'</label></span>'.
 4893:                           "\n";
 4894:             my (%current,%canselect);
 4895:             my @choices = 
 4896:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 4897:             foreach my $type ('primary','default') {
 4898:                 if (ref($spareid->{$server}) eq 'HASH') {
 4899:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 4900:                         my @spares = @{$spareid->{$server}{$type}};
 4901:                         if (@spares > 0) {
 4902:                             if ($othercontrol) {
 4903:                                 $current{$type} = join(', ',@spares);
 4904:                             } else {
 4905:                                 $current{$type} .= '<table>';
 4906:                                 my $numspares = scalar(@spares);
 4907:                                 for (my $i=0;  $i<@spares; $i++) {
 4908:                                     my $rem = $i%($numinrow);
 4909:                                     if ($rem == 0) {
 4910:                                         if ($i > 0) {
 4911:                                             $current{$type} .= '</tr>';
 4912:                                         }
 4913:                                         $current{$type} .= '<tr>';
 4914:                                     }
 4915:                                     $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;'.
 4916:                                                        $spareid->{$server}{$type}[$i].
 4917:                                                        '</label></td>'."\n";
 4918:                                 }
 4919:                                 my $rem = @spares%($numinrow);
 4920:                                 my $colsleft = $numinrow - $rem;
 4921:                                 if ($colsleft > 1 ) {
 4922:                                     $current{$type} .= '<td colspan="'.$colsleft.
 4923:                                                        '" class="LC_left_item">'.
 4924:                                                        '&nbsp;</td>';
 4925:                                 } elsif ($colsleft == 1) {
 4926:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 4927:                                 }
 4928:                                 $current{$type} .= '</tr></table>';
 4929:                             }
 4930:                         }
 4931:                     }
 4932:                     if ($current{$type} eq '') {
 4933:                         $current{$type} = &mt('None specified');
 4934:                     }
 4935:                     if ($othercontrol) {
 4936:                         if ($type eq 'primary') {
 4937:                             $canselect{$type} = $othercontrol;
 4938:                         }
 4939:                     } else {
 4940:                         $canselect{$type} = 
 4941:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 4942:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 4943:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 4944:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 4945:                         if (@choices > 0) {
 4946:                             foreach my $lonhost (@choices) {
 4947:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 4948:                             }
 4949:                         }
 4950:                         $canselect{$type} .= '</select>'."\n";
 4951:                     }
 4952:                 } else {
 4953:                     $current{$type} = &mt('Could not be determined');
 4954:                     if ($type eq 'primary') {
 4955:                         $canselect{$type} =  $othercontrol;
 4956:                     }
 4957:                 }
 4958:                 if ($type eq 'default') {
 4959:                     $datatable .= '<tr'.$css_class.'>';
 4960:                 }
 4961:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 4962:                               '<td>'.$current{$type}.'</td>'."\n".
 4963:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 4964:             }
 4965:             $itemcount ++;
 4966:         }
 4967:     }
 4968:     $$rowtotal += $itemcount;
 4969:     return $datatable;
 4970: }
 4971: 
 4972: sub possible_newspares {
 4973:     my ($server,$currspares,$serverhomes,$altids) = @_;
 4974:     my $serverhostname = &Apache::lonnet::hostname($server);
 4975:     my %excluded;
 4976:     if ($serverhostname ne '') {
 4977:         %excluded = (
 4978:                        $serverhostname => 1,
 4979:                     );
 4980:     }
 4981:     if (ref($currspares) eq 'HASH') {
 4982:         foreach my $type (keys(%{$currspares})) {
 4983:             if (ref($currspares->{$type}) eq 'ARRAY') {
 4984:                 if (@{$currspares->{$type}} > 0) {
 4985:                     foreach my $curr (@{$currspares->{$type}}) {
 4986:                         my $hostname = &Apache::lonnet::hostname($curr);
 4987:                         $excluded{$hostname} = 1;
 4988:                     }
 4989:                 }
 4990:             }
 4991:         }
 4992:     }
 4993:     my @choices;
 4994:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 4995:         if (keys(%{$serverhomes}) > 1) {
 4996:             foreach my $name (sort(keys(%{$serverhomes}))) {
 4997:                 unless ($excluded{$name}) {
 4998:                     if (exists($altids->{$serverhomes->{$name}})) {
 4999:                         push(@choices,$altids->{$serverhomes->{$name}});
 5000:                     } else {
 5001:                         push(@choices,$serverhomes->{$name});
 5002:                     }
 5003:                 }
 5004:             }
 5005:         }
 5006:     }
 5007:     return sort(@choices);
 5008: }
 5009: 
 5010: sub print_loadbalancing {
 5011:     my ($dom,$settings,$rowtotal) = @_;
 5012:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 5013:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 5014:     my $numinrow = 1;
 5015:     my $datatable;
 5016:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5017:     my (%currbalancer,%currtargets,%currrules,%existing);
 5018:     if (ref($settings) eq 'HASH') {
 5019:         %existing = %{$settings};
 5020:     }
 5021:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 5022:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 5023:                                   \%currtargets,\%currrules);
 5024:     } else {
 5025:         return;
 5026:     }
 5027:     my ($othertitle,$usertypes,$types) =
 5028:         &Apache::loncommon::sorted_inst_types($dom);
 5029:     my $rownum = 8;
 5030:     if (ref($types) eq 'ARRAY') {
 5031:         $rownum += scalar(@{$types});
 5032:     }
 5033:     my @css_class = ('LC_odd_row','LC_even_row');
 5034:     my $balnum = 0;
 5035:     my $islast;
 5036:     my (@toshow,$disabledtext);
 5037:     if (keys(%currbalancer) > 0) {
 5038:         @toshow = sort(keys(%currbalancer));
 5039:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 5040:             push(@toshow,'');
 5041:         }
 5042:     } else {
 5043:         @toshow = ('');
 5044:         $disabledtext = &mt('No existing load balancer');
 5045:     }
 5046:     foreach my $lonhost (@toshow) {
 5047:         if ($balnum == scalar(@toshow)-1) {
 5048:             $islast = 1;
 5049:         } else {
 5050:             $islast = 0;
 5051:         }
 5052:         my $cssidx = $balnum%2;
 5053:         my $targets_div_style = 'display: none';
 5054:         my $disabled_div_style = 'display: block';
 5055:         my $homedom_div_style = 'display: none';
 5056:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 5057:                       '<td rowspan="'.$rownum.'" valign="top">'.
 5058:                       '<p>';
 5059:         if ($lonhost eq '') {
 5060:             $datatable .= '<span class="LC_nobreak">';
 5061:             if (keys(%currbalancer) > 0) {
 5062:                 $datatable .= &mt('Add balancer:');
 5063:             } else {
 5064:                 $datatable .= &mt('Enable balancer:');
 5065:             }
 5066:             $datatable .= '&nbsp;'.
 5067:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 5068:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 5069:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 5070:                           '<option value="" selected="selected">'.&mt('None').
 5071:                           '</option>'."\n";
 5072:             foreach my $server (sort(keys(%servers))) {
 5073:                 next if ($currbalancer{$server});
 5074:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 5075:             }
 5076:             $datatable .=
 5077:                 '</select>'."\n".
 5078:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 5079:         } else {
 5080:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 5081:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 5082:                            &mt('Stop balancing').'</label>'.
 5083:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 5084:             $targets_div_style = 'display: block';
 5085:             $disabled_div_style = 'display: none';
 5086:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 5087:                 $homedom_div_style = 'display: block';
 5088:             }
 5089:         }
 5090:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 5091:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 5092:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 5093:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 5094:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 5095:         my @sparestypes = ('primary','default');
 5096:         my %typetitles = &sparestype_titles();
 5097:         my %hostherechecked = (
 5098:                                   no => ' checked="checked"',
 5099:                               );
 5100:         foreach my $sparetype (@sparestypes) {
 5101:             my $targettable;
 5102:             for (my $i=0; $i<$numspares; $i++) {
 5103:                 my $checked;
 5104:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 5105:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5106:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5107:                             $checked = ' checked="checked"';
 5108:                         }
 5109:                     }
 5110:                 }
 5111:                 my ($chkboxval,$disabled);
 5112:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 5113:                     $chkboxval = $spares[$i];
 5114:                 }
 5115:                 if (exists($currbalancer{$spares[$i]})) {
 5116:                     $disabled = ' disabled="disabled"';
 5117:                 }
 5118:                 $targettable .=
 5119:                     '<td><span class="LC_nobreak"><label>'.
 5120:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 5121:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 5122:                     '</span></label></span></td>';
 5123:                 my $rem = $i%($numinrow);
 5124:                 if ($rem == 0) {
 5125:                     if (($i > 0) && ($i < $numspares-1)) {
 5126:                         $targettable .= '</tr>';
 5127:                     }
 5128:                     if ($i < $numspares-1) {
 5129:                         $targettable .= '<tr>';
 5130:                     }
 5131:                 }
 5132:             }
 5133:             if ($targettable ne '') {
 5134:                 my $rem = $numspares%($numinrow);
 5135:                 my $colsleft = $numinrow - $rem;
 5136:                 if ($colsleft > 1 ) {
 5137:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5138:                                     '&nbsp;</td>';
 5139:                 } elsif ($colsleft == 1) {
 5140:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 5141:                 }
 5142:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 5143:                                '<table><tr>'.$targettable.'</tr></table><br />';
 5144:             }
 5145:             $hostherechecked{$sparetype} = '';
 5146:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 5147:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5148:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5149:                         $hostherechecked{$sparetype} = ' checked="checked"';
 5150:                         $hostherechecked{'no'} = '';
 5151:                     }
 5152:                 }
 5153:             }
 5154:         }
 5155:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 5156:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 5157:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 5158:         foreach my $sparetype (@sparestypes) {
 5159:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 5160:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 5161:                           '</i></label><br />';
 5162:         }
 5163:         $datatable .= '</div></td></tr>'.
 5164:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 5165:                                            $othertitle,$usertypes,$types,\%servers,
 5166:                                            \%currbalancer,$lonhost,
 5167:                                            $targets_div_style,$homedom_div_style,
 5168:                                            $css_class[$cssidx],$balnum,$islast);
 5169:         $$rowtotal += $rownum;
 5170:         $balnum ++;
 5171:     }
 5172:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 5173:     return $datatable;
 5174: }
 5175: 
 5176: sub get_loadbalancers_config {
 5177:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 5178:     return unless ((ref($servers) eq 'HASH') &&
 5179:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 5180:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 5181:     if (keys(%{$existing}) > 0) {
 5182:         my $oldlonhost;
 5183:         foreach my $key (sort(keys(%{$existing}))) {
 5184:             if ($key eq 'lonhost') {
 5185:                 $oldlonhost = $existing->{'lonhost'};
 5186:                 $currbalancer->{$oldlonhost} = 1;
 5187:             } elsif ($key eq 'targets') {
 5188:                 if ($oldlonhost) {
 5189:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 5190:                 }
 5191:             } elsif ($key eq 'rules') {
 5192:                 if ($oldlonhost) {
 5193:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 5194:                 }
 5195:             } elsif (ref($existing->{$key}) eq 'HASH') {
 5196:                 $currbalancer->{$key} = 1;
 5197:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 5198:                 $currrules->{$key} = $existing->{$key}{'rules'};
 5199:             }
 5200:         }
 5201:     } else {
 5202:         my ($balancerref,$targetsref) =
 5203:                 &Apache::lonnet::get_lonbalancer_config($servers);
 5204:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 5205:             foreach my $server (sort(keys(%{$balancerref}))) {
 5206:                 $currbalancer->{$server} = 1;
 5207:                 $currtargets->{$server} = $targetsref->{$server};
 5208:             }
 5209:         }
 5210:     }
 5211:     return;
 5212: }
 5213: 
 5214: sub loadbalancing_rules {
 5215:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 5216:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 5217:         $css_class,$balnum,$islast) = @_;
 5218:     my $output;
 5219:     my $num = 0;
 5220:     my ($alltypes,$othertypes,$titles) =
 5221:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 5222:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 5223:         foreach my $type (@{$alltypes}) {
 5224:             $num ++;
 5225:             my $current;
 5226:             if (ref($currrules) eq 'HASH') {
 5227:                 $current = $currrules->{$type};
 5228:             }
 5229:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5230:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 5231:                     $current = '';
 5232:                 }
 5233:             }
 5234:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 5235:                                              $servers,$currbalancer,$lonhost,$dom,
 5236:                                              $targets_div_style,$homedom_div_style,
 5237:                                              $css_class,$balnum,$num,$islast);
 5238:         }
 5239:     }
 5240:     return $output;
 5241: }
 5242: 
 5243: sub loadbalancing_titles {
 5244:     my ($dom,$intdom,$usertypes,$types) = @_;
 5245:     my %othertypes = (
 5246:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 5247:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 5248:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 5249:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 5250:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 5251:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 5252:                      );
 5253:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 5254:     my @available;
 5255:     if (ref($types) eq 'ARRAY') {
 5256:         @available = @{$types};
 5257:     }
 5258:     unless (grep(/^default$/,@available)) {
 5259:         push(@available,'default');
 5260:     }
 5261:     unshift(@alltypes,@available);
 5262:     my %titles;
 5263:     foreach my $type (@alltypes) {
 5264:         if ($type =~ /^_LC_/) {
 5265:             $titles{$type} = $othertypes{$type};
 5266:         } elsif ($type eq 'default') {
 5267:             $titles{$type} = &mt('All users from [_1]',$dom);
 5268:             if (ref($types) eq 'ARRAY') {
 5269:                 if (@{$types} > 0) {
 5270:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 5271:                 }
 5272:             }
 5273:         } elsif (ref($usertypes) eq 'HASH') {
 5274:             $titles{$type} = $usertypes->{$type};
 5275:         }
 5276:     }
 5277:     return (\@alltypes,\%othertypes,\%titles);
 5278: }
 5279: 
 5280: sub loadbalance_rule_row {
 5281:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 5282:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 5283:     my @rulenames;
 5284:     my %ruletitles = &offloadtype_text();
 5285:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 5286:         @rulenames = ('balancer','offloadedto','specific');
 5287:     } else {
 5288:         @rulenames = ('default','homeserver');
 5289:         if ($type eq '_LC_external') {
 5290:             push(@rulenames,'externalbalancer');
 5291:         } else {
 5292:             push(@rulenames,'specific');
 5293:         }
 5294:         push(@rulenames,'none');
 5295:     }
 5296:     my $style = $targets_div_style;
 5297:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5298:         $style = $homedom_div_style;
 5299:     }
 5300:     my $space;
 5301:     if ($islast && $num == 1) {
 5302:         $space = '<div display="inline-block">&nbsp;</div>';
 5303:     }
 5304:     my $output =
 5305:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 5306:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 5307:         '<td valaign="top">'.$space.
 5308:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 5309:     for (my $i=0; $i<@rulenames; $i++) {
 5310:         my $rule = $rulenames[$i];
 5311:         my ($checked,$extra);
 5312:         if ($rulenames[$i] eq 'default') {
 5313:             $rule = '';
 5314:         }
 5315:         if ($rulenames[$i] eq 'specific') {
 5316:             if (ref($servers) eq 'HASH') {
 5317:                 my $default;
 5318:                 if (($current ne '') && (exists($servers->{$current}))) {
 5319:                     $checked = ' checked="checked"';
 5320:                 }
 5321:                 unless ($checked) {
 5322:                     $default = ' selected="selected"';
 5323:                 }
 5324:                 $extra =
 5325:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5326:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5327:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 5328:                     '<option value=""'.$default.'></option>'."\n";
 5329:                 foreach my $server (sort(keys(%{$servers}))) {
 5330:                     if (ref($currbalancer) eq 'HASH') {
 5331:                         next if (exists($currbalancer->{$server}));
 5332:                     }
 5333:                     my $selected;
 5334:                     if ($server eq $current) {
 5335:                         $selected = ' selected="selected"';
 5336:                     }
 5337:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 5338:                 }
 5339:                 $extra .= '</select>';
 5340:             }
 5341:         } elsif ($rule eq $current) {
 5342:             $checked = ' checked="checked"';
 5343:         }
 5344:         $output .= '<span class="LC_nobreak"><label>'.
 5345:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 5346:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 5347:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 5348:                    ')"'.$checked.' />&nbsp;';
 5349:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 5350:             $output .= $ruletitles{'particular'};
 5351:         } else {
 5352:             $output .= $ruletitles{$rulenames[$i]};
 5353:         }
 5354:         $output .= '</label>'.$extra.'</span><br />'."\n";
 5355:     }
 5356:     $output .= '</div></td></tr>'."\n";
 5357:     return $output;
 5358: }
 5359: 
 5360: sub offloadtype_text {
 5361:     my %ruletitles = &Apache::lonlocal::texthash (
 5362:            'default'          => 'Offloads to default destinations',
 5363:            'homeserver'       => "Offloads to user's home server",
 5364:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 5365:            'specific'         => 'Offloads to specific server',
 5366:            'none'             => 'No offload',
 5367:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 5368:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 5369:            'particular'       => 'Session hosted (after re-auth) on server:',
 5370:     );
 5371:     return %ruletitles;
 5372: }
 5373: 
 5374: sub sparestype_titles {
 5375:     my %typestitles = &Apache::lonlocal::texthash (
 5376:                           'primary' => 'primary',
 5377:                           'default' => 'default',
 5378:                       );
 5379:     return %typestitles;
 5380: }
 5381: 
 5382: sub contact_titles {
 5383:     my %titles = &Apache::lonlocal::texthash (
 5384:                    'supportemail'    => 'Support E-mail address',
 5385:                    'adminemail'      => 'Default Server Admin E-mail address',
 5386:                    'errormail'       => 'Error reports to be e-mailed to',
 5387:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 5388:                    'helpdeskmail'    => "Helpdesk requests from all users in this domain",
 5389:                    'otherdomsmail'   => 'Helpdesk requests from users in other (unconfigured) domains',
 5390:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 5391:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 5392:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 5393:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 5394:                  );
 5395:     my %short_titles = &Apache::lonlocal::texthash (
 5396:                            adminemail   => 'Admin E-mail address',
 5397:                            supportemail => 'Support E-mail',
 5398:                        );   
 5399:     return (\%titles,\%short_titles);
 5400: }
 5401: 
 5402: sub helpform_fields {
 5403:     my %titles =  &Apache::lonlocal::texthash (
 5404:                        'username'   => 'Name',
 5405:                        'user'       => 'Username/domain',
 5406:                        'phone'      => 'Phone',
 5407:                        'cc'         => 'Cc e-mail',
 5408:                        'course'     => 'Course Details',
 5409:                        'section'    => 'Sections',
 5410:                        'screenshot' => 'File upload',
 5411:     );
 5412:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 5413:     my %possoptions = (
 5414:                         username     => ['yes','no','req'],
 5415:                         phone        => ['yes','no','req'],
 5416:                         user         => ['yes','no'],
 5417:                         cc           => ['yes','no'],
 5418:                         course       => ['yes','no'],
 5419:                         section      => ['yes','no'],
 5420:                         screenshot   => ['yes','no'],
 5421:                       );
 5422:     my %fieldoptions = &Apache::lonlocal::texthash (
 5423:                          'yes'  => 'Optional',
 5424:                          'req'  => 'Required',
 5425:                          'no'   => "Not shown",
 5426:     );
 5427:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 5428: }
 5429: 
 5430: sub tool_titles {
 5431:     my %titles = &Apache::lonlocal::texthash (
 5432:                      aboutme    => 'Personal web page',
 5433:                      blog       => 'Blog',
 5434:                      webdav     => 'WebDAV',
 5435:                      portfolio  => 'Portfolio',
 5436:                      official   => 'Official courses (with institutional codes)',
 5437:                      unofficial => 'Unofficial courses',
 5438:                      community  => 'Communities',
 5439:                      textbook   => 'Textbook courses',
 5440:                  );
 5441:     return %titles;
 5442: }
 5443: 
 5444: sub courserequest_titles {
 5445:     my %titles = &Apache::lonlocal::texthash (
 5446:                                    official   => 'Official',
 5447:                                    unofficial => 'Unofficial',
 5448:                                    community  => 'Communities',
 5449:                                    textbook   => 'Textbook',
 5450:                                    norequest  => 'Not allowed',
 5451:                                    approval   => 'Approval by Dom. Coord.',
 5452:                                    validate   => 'With validation',
 5453:                                    autolimit  => 'Numerical limit',
 5454:                                    unlimited  => '(blank for unlimited)',
 5455:                  );
 5456:     return %titles;
 5457: }
 5458: 
 5459: sub authorrequest_titles {
 5460:     my %titles = &Apache::lonlocal::texthash (
 5461:                                    norequest  => 'Not allowed',
 5462:                                    approval   => 'Approval by Dom. Coord.',
 5463:                                    automatic  => 'Automatic approval',
 5464:                  );
 5465:     return %titles;
 5466: }
 5467: 
 5468: sub courserequest_conditions {
 5469:     my %conditions = &Apache::lonlocal::texthash (
 5470:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 5471:        validate   => '(Processing of request subject to institutional validation).',
 5472:                  );
 5473:     return %conditions;
 5474: }
 5475: 
 5476: 
 5477: sub print_usercreation {
 5478:     my ($position,$dom,$settings,$rowtotal) = @_;
 5479:     my $numinrow = 4;
 5480:     my $datatable;
 5481:     if ($position eq 'top') {
 5482:         $$rowtotal ++;
 5483:         my $rowcount = 0;
 5484:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 5485:         if (ref($rules) eq 'HASH') {
 5486:             if (keys(%{$rules}) > 0) {
 5487:                 $datatable .= &user_formats_row('username',$settings,$rules,
 5488:                                                 $ruleorder,$numinrow,$rowcount);
 5489:                 $$rowtotal ++;
 5490:                 $rowcount ++;
 5491:             }
 5492:         }
 5493:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 5494:         if (ref($idrules) eq 'HASH') {
 5495:             if (keys(%{$idrules}) > 0) {
 5496:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 5497:                                                 $idruleorder,$numinrow,$rowcount);
 5498:                 $$rowtotal ++;
 5499:                 $rowcount ++;
 5500:             }
 5501:         }
 5502:         if ($rowcount == 0) {
 5503:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 5504:             $$rowtotal ++;
 5505:             $rowcount ++;
 5506:         }
 5507:     } elsif ($position eq 'middle') {
 5508:         my @creators = ('author','course','requestcrs');
 5509:         my ($rules,$ruleorder) =
 5510:             &Apache::lonnet::inst_userrules($dom,'username');
 5511:         my %lt = &usercreation_types();
 5512:         my %checked;
 5513:         if (ref($settings) eq 'HASH') {
 5514:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 5515:                 foreach my $item (@creators) {
 5516:                     $checked{$item} = $settings->{'cancreate'}{$item};
 5517:                 }
 5518:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 5519:                 foreach my $item (@creators) {
 5520:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 5521:                         $checked{$item} = 'none';
 5522:                     }
 5523:                 }
 5524:             }
 5525:         }
 5526:         my $rownum = 0;
 5527:         foreach my $item (@creators) {
 5528:             $rownum ++;
 5529:             if ($checked{$item} eq '') {
 5530:                 $checked{$item} = 'any';
 5531:             }
 5532:             my $css_class;
 5533:             if ($rownum%2) {
 5534:                 $css_class = '';
 5535:             } else {
 5536:                 $css_class = ' class="LC_odd_row" ';
 5537:             }
 5538:             $datatable .= '<tr'.$css_class.'>'.
 5539:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 5540:                          '</span></td><td align="right">';
 5541:             my @options = ('any');
 5542:             if (ref($rules) eq 'HASH') {
 5543:                 if (keys(%{$rules}) > 0) {
 5544:                     push(@options,('official','unofficial'));
 5545:                 }
 5546:             }
 5547:             push(@options,'none');
 5548:             foreach my $option (@options) {
 5549:                 my $type = 'radio';
 5550:                 my $check = ' ';
 5551:                 if ($checked{$item} eq $option) {
 5552:                     $check = ' checked="checked" ';
 5553:                 } 
 5554:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5555:                               '<input type="'.$type.'" name="can_createuser_'.
 5556:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 5557:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 5558:             }
 5559:             $datatable .= '</td></tr>';
 5560:         }
 5561:     } else {
 5562:         my @contexts = ('author','course','domain');
 5563:         my @authtypes = ('int','krb4','krb5','loc');
 5564:         my %checked;
 5565:         if (ref($settings) eq 'HASH') {
 5566:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 5567:                 foreach my $item (@contexts) {
 5568:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 5569:                         foreach my $auth (@authtypes) {
 5570:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 5571:                                 $checked{$item}{$auth} = ' checked="checked" ';
 5572:                             }
 5573:                         }
 5574:                     }
 5575:                 }
 5576:             }
 5577:         } else {
 5578:             foreach my $item (@contexts) {
 5579:                 foreach my $auth (@authtypes) {
 5580:                     $checked{$item}{$auth} = ' checked="checked" ';
 5581:                 }
 5582:             }
 5583:         }
 5584:         my %title = &context_names();
 5585:         my %authname = &authtype_names();
 5586:         my $rownum = 0;
 5587:         my $css_class; 
 5588:         foreach my $item (@contexts) {
 5589:             if ($rownum%2) {
 5590:                 $css_class = '';
 5591:             } else {
 5592:                 $css_class = ' class="LC_odd_row" ';
 5593:             }
 5594:             $datatable .=   '<tr'.$css_class.'>'.
 5595:                             '<td>'.$title{$item}.
 5596:                             '</td><td class="LC_left_item">'.
 5597:                             '<span class="LC_nobreak">';
 5598:             foreach my $auth (@authtypes) {
 5599:                 $datatable .= '<label>'. 
 5600:                               '<input type="checkbox" name="'.$item.'_auth" '.
 5601:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 5602:                               $authname{$auth}.'</label>&nbsp;';
 5603:             }
 5604:             $datatable .= '</span></td></tr>';
 5605:             $rownum ++;
 5606:         }
 5607:         $$rowtotal += $rownum;
 5608:     }
 5609:     return $datatable;
 5610: }
 5611: 
 5612: sub print_selfcreation {
 5613:     my ($position,$dom,$settings,$rowtotal) = @_;
 5614:     my (@selfcreate,$createsettings,$processing,$datatable);
 5615:     if (ref($settings) eq 'HASH') {
 5616:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 5617:             $createsettings = $settings->{'cancreate'};
 5618:             if (ref($createsettings) eq 'HASH') {
 5619:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 5620:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 5621:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 5622:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 5623:                         @selfcreate = ('email','login','sso');
 5624:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 5625:                         @selfcreate = ($createsettings->{'selfcreate'});
 5626:                     }
 5627:                 }
 5628:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 5629:                     $processing = $createsettings->{'selfcreateprocessing'};
 5630:                 }
 5631:             }
 5632:         }
 5633:     }
 5634:     my %radiohash;
 5635:     my $numinrow = 4;
 5636:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 5637:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5638:     if ($position eq 'top') {
 5639:         my %choices = &Apache::lonlocal::texthash (
 5640:                                                       cancreate_login      => 'Institutional Login',
 5641:                                                       cancreate_sso        => 'Institutional Single Sign On',
 5642:                                                   );
 5643:         my @toggles = sort(keys(%choices));
 5644:         my %defaultchecked = (
 5645:                                'cancreate_login' => 'off',
 5646:                                'cancreate_sso'   => 'off',
 5647:                              );
 5648:         my ($onclick,$itemcount);
 5649:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5650:                                                      \%choices,$itemcount,$onclick);
 5651:         $$rowtotal += $itemcount;
 5652:         
 5653:         if (ref($usertypes) eq 'HASH') {
 5654:             if (keys(%{$usertypes}) > 0) {
 5655:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 5656:                                              $dom,$numinrow,$othertitle,
 5657:                                              'statustocreate',$rowtotal);
 5658:                 $$rowtotal ++;
 5659:             }
 5660:         }
 5661:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 5662:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5663:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 5664:         my $rem;
 5665:         my $numperrow = 2;
 5666:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 5667:         $datatable .= '<tr'.$css_class.'>'.
 5668:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 5669:                      '<td class="LC_left_item">'."\n".
 5670:                      '<table><tr><td>'."\n";
 5671:         for (my $i=0; $i<@fields; $i++) {
 5672:             $rem = $i%($numperrow);
 5673:             if ($rem == 0) {
 5674:                 if ($i > 0) {
 5675:                     $datatable .= '</tr>';
 5676:                 }
 5677:                 $datatable .= '<tr>';
 5678:             }
 5679:             my $currval;
 5680:             if (ref($createsettings) eq 'HASH') {
 5681:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 5682:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 5683:                 }
 5684:             }
 5685:             $datatable .= '<td class="LC_left_item">'.
 5686:                           '<span class="LC_nobreak">'.
 5687:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 5688:                           'value="'.$currval.'" size="10" />&nbsp;'.
 5689:                           $fieldtitles{$fields[$i]}.'</span></td>';
 5690:         }
 5691:         my $colsleft = $numperrow - $rem;
 5692:         if ($colsleft > 1 ) {
 5693:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5694:                          '&nbsp;</td>';
 5695:         } elsif ($colsleft == 1) {
 5696:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 5697:         }
 5698:         $datatable .= '</tr></table></td></tr>';
 5699:         $$rowtotal ++;
 5700:     } elsif ($position eq 'middle') {
 5701:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 5702:         my @posstypes;
 5703:         if (ref($types) eq 'ARRAY') {
 5704:             @posstypes = @{$types};
 5705:         }
 5706:         unless (grep(/^default$/,@posstypes)) {
 5707:             push(@posstypes,'default');
 5708:         }
 5709:         my %usertypeshash;
 5710:         if (ref($usertypes) eq 'HASH') {
 5711:             %usertypeshash = %{$usertypes};
 5712:         }
 5713:         $usertypeshash{'default'} = $othertitle;
 5714:         foreach my $status (@posstypes) {
 5715:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 5716:                                                    $numinrow,$$rowtotal,\%usertypeshash);
 5717:             $$rowtotal ++;
 5718:         }
 5719:     } else {
 5720:         my %choices = &Apache::lonlocal::texthash (
 5721:                                                       cancreate_email => 'E-mail address as username',
 5722:                                                   );
 5723:         my @toggles = sort(keys(%choices));
 5724:         my %defaultchecked = (
 5725:                                'cancreate_email' => 'off',
 5726:                              );
 5727:         my $itemcount = 0;
 5728:         my $display = 'none';
 5729:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 5730:             $display = 'block';
 5731:         }
 5732:         my $onclick = "toggleDisplay(this.form,'emailoptions');";
 5733:         my $additional = '<div id="emailoptions" style="display: '.$display.'">';
 5734:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5735:         if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
 5736:             $order = $domdefaults{'inststatusguest'};
 5737:         }
 5738:         my (@ordered,%usertypeshash);
 5739:         if (ref($order) eq 'ARRAY') {
 5740:             @ordered = @{$order};
 5741:         }
 5742:         if (@ordered) {
 5743:             unless (grep(/^default$/,@ordered)) {
 5744:                 push(@ordered,'default');
 5745:             }
 5746:             if (ref($usertypes) eq 'HASH') {
 5747:                 %usertypeshash = %{$usertypes};
 5748:             }
 5749:             $usertypeshash{'default'} = $othertitle;
 5750:             $additional .= '<table><tr>';
 5751:             foreach my $status (@ordered) {
 5752:                 $additional .= '<th>'.$usertypeshash{$status}.'</th>';
 5753:             }
 5754:             $additional .= '</tr><tr>';
 5755:             foreach my $status (@ordered) {
 5756:                 $additional .= '<td>'.&email_as_username($rowtotal,$processing,$status).'</td>';
 5757:             }
 5758:             $additional .= '</tr></table>';
 5759:         } else {
 5760:             $usertypeshash{'default'} = $othertitle;
 5761:             $additional .= &email_as_username($rowtotal,$processing);
 5762:         }
 5763:         $additional .= '</div>'."\n";
 5764: 
 5765:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5766:                                                      \%choices,$$rowtotal,$onclick,$additional);
 5767:         $$rowtotal ++;
 5768:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal);
 5769:         $$rowtotal ++;
 5770:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 5771:         $numinrow = 1;
 5772:         foreach my $status (@ordered) {
 5773:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 5774:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,$infotitles);
 5775:             $$rowtotal ++;
 5776:         }
 5777:         my ($emailrules,$emailruleorder) =
 5778:             &Apache::lonnet::inst_userrules($dom,'email');
 5779:         if (ref($emailrules) eq 'HASH') {
 5780:             if (keys(%{$emailrules}) > 0) {
 5781:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 5782:                                                 $emailruleorder,$numinrow,$$rowtotal);
 5783:                 $$rowtotal ++;
 5784:             }
 5785:         }
 5786:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal);
 5787:     }
 5788:     return $datatable;
 5789: }
 5790: 
 5791: sub email_as_username {
 5792:     my ($rowtotal,$processing,$type) = @_;
 5793:     my %choices =
 5794:         &Apache::lonlocal::texthash (
 5795:                                       automatic => 'Automatic approval',
 5796:                                       approval  => 'Queued for approval',
 5797:                                     );
 5798:     my $output;
 5799:     foreach my $option ('automatic','approval') {
 5800:         my $checked;
 5801:         if (ref($processing) eq 'HASH') {
 5802:             if ($type eq '') {   
 5803:                 if (!exists($processing->{'default'})) {
 5804:                     if ($option eq 'automatic') {
 5805:                         $checked = ' checked="checked"';
 5806:                     }
 5807:                 } else {
 5808:                     if ($processing->{'default'} eq $option) {
 5809:                         $checked = ' checked="checked"';
 5810:                     }
 5811:                 }
 5812:             } else {
 5813:                 if (!exists($processing->{$type})) {
 5814:                     if ($option eq 'automatic') {
 5815:                         $checked = ' checked="checked"';
 5816:                     }
 5817:                 } else {
 5818:                     if ($processing->{$type} eq $option) {
 5819:                         $checked = ' checked="checked"';
 5820:                     }
 5821:                 }
 5822:             }
 5823:         } elsif ($option eq 'automatic') {
 5824:             $checked = ' checked="checked"'; 
 5825:         }
 5826:         my $name = 'cancreate_emailprocess';
 5827:         if (($type ne '') && ($type ne 'default')) {
 5828:             $name .= '_'.$type;
 5829:         }
 5830:         $output .= '<span class="LC_nobreak"><label>'.
 5831:                    '<input type="radio" name="'.$name.'"'.
 5832:                    $checked.' value="'.$option.'" />'.
 5833:                    $choices{$option}.'</label></span>';
 5834:         if ($type eq '') {
 5835:             $output .= '&nbsp;';
 5836:         } else {
 5837:             $output .= '<br />';
 5838:         }
 5839:     }
 5840:     $$rowtotal ++;
 5841:     return $output;
 5842: }
 5843: 
 5844: sub captcha_choice {
 5845:     my ($context,$settings,$itemcount) = @_;
 5846:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 5847:         $vertext,$currver); 
 5848:     my %lt = &captcha_phrases();
 5849:     $keyentry = 'hidden';
 5850:     if ($context eq 'cancreate') {
 5851:         $rowname = &mt('CAPTCHA validation');
 5852:     } elsif ($context eq 'login') {
 5853:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 5854:     }
 5855:     if (ref($settings) eq 'HASH') {
 5856:         if ($settings->{'captcha'}) {
 5857:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 5858:         } else {
 5859:             $checked{'original'} = ' checked="checked"';
 5860:         }
 5861:         if ($settings->{'captcha'} eq 'recaptcha') {
 5862:             $pubtext = $lt{'pub'};
 5863:             $privtext = $lt{'priv'};
 5864:             $keyentry = 'text';
 5865:             $vertext = $lt{'ver'};
 5866:             $currver = $settings->{'recaptchaversion'};
 5867:             if ($currver ne '2') {
 5868:                 $currver = 1;
 5869:             }
 5870:         }
 5871:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 5872:             $currpub = $settings->{'recaptchakeys'}{'public'};
 5873:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 5874:         }
 5875:     } else {
 5876:         $checked{'original'} = ' checked="checked"';
 5877:     }
 5878:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5879:     my $output = '<tr'.$css_class.'>'.
 5880:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 5881:                  '<table><tr><td>'."\n";
 5882:     foreach my $option ('original','recaptcha','notused') {
 5883:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 5884:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 5885:                    $lt{$option}.'</label></span>';
 5886:         unless ($option eq 'notused') {
 5887:             $output .= ('&nbsp;'x2)."\n";
 5888:         }
 5889:     }
 5890: #
 5891: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 5892: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 5893: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 5894: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 5895: #
 5896:     $output .= '</td></tr>'."\n".
 5897:                '<tr><td>'."\n".
 5898:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 5899:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 5900:                $currpub.'" size="40" /></span><br />'."\n".
 5901:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 5902:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 5903:                $currpriv.'" size="40" /></span><br />'.
 5904:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 5905:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 5906:                $currver.'" size="3" /></span><br />'.
 5907:                '</td></tr></table>'."\n".
 5908:                '</td></tr>';
 5909:     return $output;
 5910: }
 5911: 
 5912: sub user_formats_row {
 5913:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 5914:     my $output;
 5915:     my %text = (
 5916:                    'username' => 'new usernames',
 5917:                    'id'       => 'IDs',
 5918:                    'email'    => 'self-created accounts (e-mail)',
 5919:                );
 5920:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 5921:     $output = '<tr '.$css_class.'>'.
 5922:               '<td><span class="LC_nobreak">';
 5923:     if ($type eq 'email') {
 5924:         $output .= &mt("Formats disallowed for $text{$type}: ");
 5925:     } else {
 5926:         $output .= &mt("Format rules to check for $text{$type}: ");
 5927:     }
 5928:     $output .= '</span></td>'.
 5929:                '<td class="LC_left_item" colspan="2"><table>';
 5930:     my $rem;
 5931:     if (ref($ruleorder) eq 'ARRAY') {
 5932:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 5933:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 5934:                 my $rem = $i%($numinrow);
 5935:                 if ($rem == 0) {
 5936:                     if ($i > 0) {
 5937:                         $output .= '</tr>';
 5938:                     }
 5939:                     $output .= '<tr>';
 5940:                 }
 5941:                 my $check = ' ';
 5942:                 if (ref($settings) eq 'HASH') {
 5943:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 5944:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 5945:                             $check = ' checked="checked" ';
 5946:                         }
 5947:                     }
 5948:                 }
 5949:                 $output .= '<td class="LC_left_item">'.
 5950:                            '<span class="LC_nobreak"><label>'.
 5951:                            '<input type="checkbox" name="'.$type.'_rule" '.
 5952:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 5953:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 5954:             }
 5955:         }
 5956:         $rem = @{$ruleorder}%($numinrow);
 5957:     }
 5958:     my $colsleft = $numinrow - $rem;
 5959:     if ($colsleft > 1 ) {
 5960:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5961:                    '&nbsp;</td>';
 5962:     } elsif ($colsleft == 1) {
 5963:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 5964:     }
 5965:     $output .= '</tr></table></td></tr>';
 5966:     return $output;
 5967: }
 5968: 
 5969: sub usercreation_types {
 5970:     my %lt = &Apache::lonlocal::texthash (
 5971:                     author     => 'When adding a co-author',
 5972:                     course     => 'When adding a user to a course',
 5973:                     requestcrs => 'When requesting a course',
 5974:                     any        => 'Any',
 5975:                     official   => 'Institutional only ',
 5976:                     unofficial => 'Non-institutional only',
 5977:                     none       => 'None',
 5978:     );
 5979:     return %lt;
 5980: }
 5981: 
 5982: sub selfcreation_types {
 5983:     my %lt = &Apache::lonlocal::texthash (
 5984:                     selfcreate => 'User creates own account',
 5985:                     any        => 'Any',
 5986:                     official   => 'Institutional only ',
 5987:                     unofficial => 'Non-institutional only',
 5988:                     email      => 'E-mail address',
 5989:                     login      => 'Institutional Login',
 5990:                     sso        => 'SSO',
 5991:              );
 5992: }
 5993: 
 5994: sub authtype_names {
 5995:     my %lt = &Apache::lonlocal::texthash(
 5996:                       int    => 'Internal',
 5997:                       krb4   => 'Kerberos 4',
 5998:                       krb5   => 'Kerberos 5',
 5999:                       loc    => 'Local',
 6000:                   );
 6001:     return %lt;
 6002: }
 6003: 
 6004: sub context_names {
 6005:     my %context_title = &Apache::lonlocal::texthash(
 6006:        author => 'Creating users when an Author',
 6007:        course => 'Creating users when in a course',
 6008:        domain => 'Creating users when a Domain Coordinator',
 6009:     );
 6010:     return %context_title;
 6011: }
 6012: 
 6013: sub print_usermodification {
 6014:     my ($position,$dom,$settings,$rowtotal) = @_;
 6015:     my $numinrow = 4;
 6016:     my ($context,$datatable,$rowcount);
 6017:     if ($position eq 'top') {
 6018:         $rowcount = 0;
 6019:         $context = 'author'; 
 6020:         foreach my $role ('ca','aa') {
 6021:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 6022:                                                    $numinrow,$rowcount);
 6023:             $$rowtotal ++;
 6024:             $rowcount ++;
 6025:         }
 6026:     } elsif ($position eq 'bottom') {
 6027:         $context = 'course';
 6028:         $rowcount = 0;
 6029:         foreach my $role ('st','ep','ta','in','cr') {
 6030:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 6031:                                                    $numinrow,$rowcount);
 6032:             $$rowtotal ++;
 6033:             $rowcount ++;
 6034:         }
 6035:     }
 6036:     return $datatable;
 6037: }
 6038: 
 6039: sub print_defaults {
 6040:     my ($position,$dom,$settings,$rowtotal) = @_;
 6041:     my $rownum = 0;
 6042:     my ($datatable,$css_class,$titles);
 6043:     unless ($position eq 'bottom') {
 6044:         $titles = &defaults_titles($dom);
 6045:     }
 6046:     if ($position eq 'top') {
 6047:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 6048:                      'datelocale_def','portal_def');
 6049:         my %defaults;
 6050:         if (ref($settings) eq 'HASH') {
 6051:             %defaults = %{$settings};
 6052:         } else {
 6053:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6054:             foreach my $item (@items) {
 6055:                 $defaults{$item} = $domdefaults{$item};
 6056:             }
 6057:         }
 6058:         foreach my $item (@items) {
 6059:             if ($rownum%2) {
 6060:                 $css_class = '';
 6061:             } else {
 6062:                 $css_class = ' class="LC_odd_row" ';
 6063:             }
 6064:             $datatable .= '<tr'.$css_class.'>'.
 6065:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 6066:                           '</span></td><td class="LC_right_item" colspan="3">';
 6067:             if ($item eq 'auth_def') {
 6068:                 my @authtypes = ('internal','krb4','krb5','localauth');
 6069:                 my %shortauth = (
 6070:                                  internal => 'int',
 6071:                                  krb4 => 'krb4',
 6072:                                  krb5 => 'krb5',
 6073:                                  localauth  => 'loc'
 6074:                                 );
 6075:                 my %authnames = &authtype_names();
 6076:                 foreach my $auth (@authtypes) {
 6077:                     my $checked = ' ';
 6078:                     if ($defaults{$item} eq $auth) {
 6079:                         $checked = ' checked="checked" ';
 6080:                     }
 6081:                     $datatable .= '<label><input type="radio" name="'.$item.
 6082:                                   '" value="'.$auth.'"'.$checked.'/>'.
 6083:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 6084:                 }
 6085:             } elsif ($item eq 'timezone_def') {
 6086:                 my $includeempty = 1;
 6087:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 6088:             } elsif ($item eq 'datelocale_def') {
 6089:                 my $includeempty = 1;
 6090:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 6091:             } elsif ($item eq 'lang_def') {
 6092:                 my $includeempty = 1;
 6093:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 6094:             } else {
 6095:                 my $size;
 6096:                 if ($item eq 'portal_def') {
 6097:                     $size = ' size="25"';
 6098:                 }
 6099:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 6100:                               $defaults{$item}.'"'.$size.' />';
 6101:             }
 6102:             $datatable .= '</td></tr>';
 6103:             $rownum ++;
 6104:         }
 6105:     } elsif ($position eq 'middle') {
 6106:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 6107:         my %defaults;
 6108:         if (ref($settings) eq 'HASH') {
 6109:             %defaults = %{$settings};
 6110:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 6111:                 $defaults{'intauth_cost'} = 10;
 6112:             }
 6113:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 6114:                 $defaults{'intauth_check'} = 0;
 6115:             }
 6116:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 6117:                 $defaults{'intauth_switch'} = 0;
 6118:             }
 6119:         } else {
 6120:             %defaults = (
 6121:                           'intauth_cost'   => 10,
 6122:                           'intauth_check'  => 0,
 6123:                           'intauth_switch' => 0,
 6124:                         );
 6125:         }
 6126:         foreach my $item (@items) {
 6127:             if ($rownum%2) {
 6128:                 $css_class = '';
 6129:             } else {
 6130:                 $css_class = ' class="LC_odd_row" ';
 6131:             }
 6132:             $datatable .= '<tr'.$css_class.'>'.
 6133:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 6134:                           '</span></td><td class="LC_left_item" colspan="3">';
 6135:             if ($item eq 'intauth_switch') {
 6136:                 my @options = (0,1,2);
 6137:                 my %optiondesc = &Apache::lonlocal::texthash (
 6138:                                    0 => 'No',
 6139:                                    1 => 'Yes',
 6140:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 6141:                                  );
 6142:                 $datatable .= '<table width="100%">';
 6143:                 foreach my $option (@options) {
 6144:                     my $checked = ' ';
 6145:                     if ($defaults{$item} eq $option) {
 6146:                         $checked = ' checked="checked"';
 6147:                     }
 6148:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 6149:                                   '<label><input type="radio" name="'.$item.
 6150:                                   '" value="'.$option.'"'.$checked.' />'.
 6151:                                   $optiondesc{$option}.'</label></span></td></tr>';
 6152:                 }
 6153:                 $datatable .= '</table>';
 6154:             } elsif ($item eq 'intauth_check') {
 6155:                 my @options = (0,1,2);
 6156:                 my %optiondesc = &Apache::lonlocal::texthash (
 6157:                                    0 => 'No',
 6158:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 6159:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 6160:                                  );
 6161:                 $datatable .= '<table wisth="100%">';
 6162:                 foreach my $option (@options) {
 6163:                     my $checked = ' ';
 6164:                     my $onclick;
 6165:                     if ($defaults{$item} eq $option) {
 6166:                         $checked = ' checked="checked"';
 6167:                     }
 6168:                     if ($option == 2) {
 6169:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 6170:                     }
 6171:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 6172:                                   '<label><input type="radio" name="'.$item.
 6173:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 6174:                                   $optiondesc{$option}.'</label></span></td></tr>';
 6175:                 }
 6176:                 $datatable .= '</table>';
 6177:             } else {
 6178:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 6179:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />';
 6180:             }
 6181:             $datatable .= '</td></tr>';
 6182:             $rownum ++;
 6183:         }
 6184:     } else {
 6185:         my %defaults;
 6186:         if (ref($settings) eq 'HASH') {
 6187:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH') &&
 6188:                 (ref($settings->{'inststatusguest'}) eq 'ARRAY')) {
 6189:                 my $maxnum = @{$settings->{'inststatusorder'}};
 6190:                 for (my $i=0; $i<$maxnum; $i++) {
 6191:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 6192:                     my $item = $settings->{'inststatusorder'}->[$i];
 6193:                     my $title = $settings->{'inststatustypes'}->{$item};
 6194:                     my $guestok;
 6195:                     if (grep(/^\Q$item\E$/,@{$settings->{'inststatusguest'}})) {
 6196:                         $guestok = 1;
 6197:                     }
 6198:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 6199:                     $datatable .= '<tr'.$css_class.'>'.
 6200:                                   '<td><span class="LC_nobreak">'.
 6201:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 6202:                     for (my $k=0; $k<=$maxnum; $k++) {
 6203:                         my $vpos = $k+1;
 6204:                         my $selstr;
 6205:                         if ($k == $i) {
 6206:                             $selstr = ' selected="selected" ';
 6207:                         }
 6208:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6209:                     }
 6210:                     my ($checkedon,$checkedoff);
 6211:                     $checkedoff = ' checked="checked"';
 6212:                     if ($guestok) {
 6213:                         $checkedon = $checkedoff;
 6214:                         $checkedoff = ''; 
 6215:                     }
 6216:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 6217:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 6218:                                   &mt('delete').'</span></td>'.
 6219:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed:').
 6220:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 6221:                                   '</span></td>'.
 6222:                                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6223:                                   '<label><input type="radio" value="1" name="inststatus_guest_'.$item.'"'.$checkedon.' />'.
 6224:                                   &mt('Yes').'</label>'.('&nbsp;'x2).
 6225:                                   '<label><input type="radio" value="0" name="inststatus_guest_'.$item.'"'.$checkedoff.' />'.
 6226:                                   &mt('No').'</label></span></td></tr>';
 6227:                 }
 6228:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 6229:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 6230:                 $datatable .= '<tr '.$css_class.'>'.
 6231:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 6232:                 for (my $k=0; $k<=$maxnum; $k++) {
 6233:                     my $vpos = $k+1;
 6234:                     my $selstr;
 6235:                     if ($k == $maxnum) {
 6236:                         $selstr = ' selected="selected" ';
 6237:                     }
 6238:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6239:                 }
 6240:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 6241:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 6242:                               '&nbsp;'.&mt('(new)').
 6243:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 6244:                               &mt('Name displayed:').
 6245:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 6246:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 6247:                               '<label><input type="radio" value="1" name="addinststatus_guest" />'.
 6248:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 6249:                               '<label><input type="radio" value="0" name="addinststatus_guest" />'.
 6250:                               &mt('No').'</label></span></td></tr>';
 6251:                               '</tr>'."\n";
 6252:                 $rownum ++;
 6253:             }
 6254:         }
 6255:     }
 6256:     $$rowtotal += $rownum;
 6257:     return $datatable;
 6258: }
 6259: 
 6260: sub get_languages_hash {
 6261:     my %langchoices;
 6262:     foreach my $id (&Apache::loncommon::languageids()) {
 6263:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 6264:         if ($code ne '') {
 6265:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 6266:         }
 6267:     }
 6268:     return %langchoices;
 6269: }
 6270: 
 6271: sub defaults_titles {
 6272:     my ($dom) = @_;
 6273:     my %titles = &Apache::lonlocal::texthash (
 6274:                    'auth_def'      => 'Default authentication type',
 6275:                    'auth_arg_def'  => 'Default authentication argument',
 6276:                    'lang_def'      => 'Default language',
 6277:                    'timezone_def'  => 'Default timezone',
 6278:                    'datelocale_def' => 'Default locale for dates',
 6279:                    'portal_def'     => 'Portal/Default URL',
 6280:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
 6281:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
 6282:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
 6283:                  );
 6284:     if ($dom) {
 6285:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 6286:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 6287:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 6288:         $protocol = 'http' if ($protocol ne 'https');
 6289:         if ($uint_dom) {
 6290:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 6291:                                          $uint_dom);
 6292:         }
 6293:     }
 6294:     return (\%titles);
 6295: }
 6296: 
 6297: sub print_scantronformat {
 6298:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 6299:     my $itemcount = 1;
 6300:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 6301:         %confhash);
 6302:     my $switchserver = &check_switchserver($dom,$confname);
 6303:     my %lt = &Apache::lonlocal::texthash (
 6304:                 default => 'Default bubblesheet format file error',
 6305:                 custom  => 'Custom bubblesheet format file error',
 6306:              );
 6307:     my %scantronfiles = (
 6308:         default => 'default.tab',
 6309:         custom => 'custom.tab',
 6310:     );
 6311:     foreach my $key (keys(%scantronfiles)) {
 6312:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 6313:                               .$scantronfiles{$key};
 6314:     }
 6315:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 6316:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 6317:         if (!$switchserver) {
 6318:             my $servadm = $r->dir_config('lonAdmEMail');
 6319:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 6320:             if ($configuserok eq 'ok') {
 6321:                 if ($author_ok eq 'ok') {
 6322:                     my %legacyfile = (
 6323:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 6324:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 6325:                     );
 6326:                     my %md5chk;
 6327:                     foreach my $type (keys(%legacyfile)) {
 6328:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 6329:                         chomp($md5chk{$type});
 6330:                     }
 6331:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 6332:                         foreach my $type (keys(%legacyfile)) {
 6333:                             ($scantronurls{$type},my $error) = 
 6334:                                 &legacy_scantronformat($r,$dom,$confname,
 6335:                                                  $type,$legacyfile{$type},
 6336:                                                  $scantronurls{$type},
 6337:                                                  $scantronfiles{$type});
 6338:                             if ($error ne '') {
 6339:                                 $error{$type} = $error;
 6340:                             }
 6341:                         }
 6342:                         if (keys(%error) == 0) {
 6343:                             $is_custom = 1;
 6344:                             $confhash{'scantron'}{'scantronformat'} = 
 6345:                                 $scantronurls{'custom'};
 6346:                             my $putresult = 
 6347:                                 &Apache::lonnet::put_dom('configuration',
 6348:                                                          \%confhash,$dom);
 6349:                             if ($putresult ne 'ok') {
 6350:                                 $error{'custom'} = 
 6351:                                     '<span class="LC_error">'.
 6352:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 6353:                             }
 6354:                         }
 6355:                     } else {
 6356:                         ($scantronurls{'default'},my $error) =
 6357:                             &legacy_scantronformat($r,$dom,$confname,
 6358:                                           'default',$legacyfile{'default'},
 6359:                                           $scantronurls{'default'},
 6360:                                           $scantronfiles{'default'});
 6361:                         if ($error eq '') {
 6362:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 6363:                             my $putresult =
 6364:                                 &Apache::lonnet::put_dom('configuration',
 6365:                                                          \%confhash,$dom);
 6366:                             if ($putresult ne 'ok') {
 6367:                                 $error{'default'} =
 6368:                                     '<span class="LC_error">'.
 6369:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 6370:                             }
 6371:                         } else {
 6372:                             $error{'default'} = $error;
 6373:                         }
 6374:                     }
 6375:                 }
 6376:             }
 6377:         } else {
 6378:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 6379:         }
 6380:     }
 6381:     if (ref($settings) eq 'HASH') {
 6382:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 6383:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 6384:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 6385:                 $scantronurl = '';
 6386:             } else {
 6387:                 $scantronurl = $settings->{'scantronformat'};
 6388:             }
 6389:             $is_custom = 1;
 6390:         } else {
 6391:             $scantronurl = $scantronurls{'default'};
 6392:         }
 6393:     } else {
 6394:         if ($is_custom) {
 6395:             $scantronurl = $scantronurls{'custom'};
 6396:         } else {
 6397:             $scantronurl = $scantronurls{'default'};
 6398:         }
 6399:     }
 6400:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6401:     $datatable .= '<tr'.$css_class.'>';
 6402:     if (!$is_custom) {
 6403:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 6404:                       '<span class="LC_nobreak">';
 6405:         if ($scantronurl) {
 6406:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 6407:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 6408:         } else {
 6409:             $datatable = &mt('File unavailable for display');
 6410:         }
 6411:         $datatable .= '</span></td>';
 6412:         if (keys(%error) == 0) { 
 6413:             $datatable .= '<td valign="bottom">';
 6414:             if (!$switchserver) {
 6415:                 $datatable .= &mt('Upload:').'<br />';
 6416:             }
 6417:         } else {
 6418:             my $errorstr;
 6419:             foreach my $key (sort(keys(%error))) {
 6420:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 6421:             }
 6422:             $datatable .= '<td>'.$errorstr;
 6423:         }
 6424:     } else {
 6425:         if (keys(%error) > 0) {
 6426:             my $errorstr;
 6427:             foreach my $key (sort(keys(%error))) {
 6428:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 6429:             } 
 6430:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 6431:         } elsif ($scantronurl) {
 6432:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 6433:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 6434:             $datatable .= '<td><span class="LC_nobreak">'.
 6435:                           $link.
 6436:                           '<label><input type="checkbox" name="scantronformat_del"'.
 6437:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 6438:                           '<td><span class="LC_nobreak">&nbsp;'.
 6439:                           &mt('Replace:').'</span><br />';
 6440:         }
 6441:     }
 6442:     if (keys(%error) == 0) {
 6443:         if ($switchserver) {
 6444:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 6445:         } else {
 6446:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 6447:                          '<input type="file" name="scantronformat" /></span>';
 6448:         }
 6449:     }
 6450:     $datatable .= '</td></tr>';
 6451:     $$rowtotal ++;
 6452:     return $datatable;
 6453: }
 6454: 
 6455: sub legacy_scantronformat {
 6456:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 6457:     my ($url,$error);
 6458:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 6459:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 6460:         (my $result,$url) =
 6461:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 6462:                          '','',$newfile);
 6463:         if ($result ne 'ok') {
 6464:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 6465:         }
 6466:     }
 6467:     return ($url,$error);
 6468: }
 6469: 
 6470: sub print_coursecategories {
 6471:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 6472:     my $datatable;
 6473:     if ($position eq 'top') {
 6474:         my (%checked);
 6475:         my @catitems = ('unauth','auth');
 6476:         my @cattypes = ('std','domonly','codesrch','none');
 6477:         $checked{'unauth'} = 'std';
 6478:         $checked{'auth'} = 'std';
 6479:         if (ref($settings) eq 'HASH') {
 6480:             foreach my $type (@cattypes) {
 6481:                 if ($type eq $settings->{'unauth'}) {
 6482:                     $checked{'unauth'} = $type;
 6483:                 }
 6484:                 if ($type eq $settings->{'auth'}) {
 6485:                     $checked{'auth'} = $type;
 6486:                 }
 6487:             }
 6488:         }
 6489:         my %lt = &Apache::lonlocal::texthash (
 6490:                                                unauth   => 'Catalog type for unauthenticated users',
 6491:                                                auth     => 'Catalog type for authenticated users',
 6492:                                                none     => 'No catalog',
 6493:                                                std      => 'Standard catalog',
 6494:                                                domonly  => 'Domain-only catalog',
 6495:                                                codesrch => "Code search form",
 6496:                                              );
 6497:        my $itemcount = 0;
 6498:        foreach my $item (@catitems) {
 6499:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 6500:            $datatable .= '<tr '.$css_class.'>'.
 6501:                          '<td>'.$lt{$item}.'</td>'.
 6502:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 6503:            foreach my $type (@cattypes) {
 6504:                my $ischecked;
 6505:                if ($checked{$item} eq $type) {
 6506:                    $ischecked=' checked="checked"';
 6507:                }
 6508:                $datatable .= '<label>'.
 6509:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 6510:                              ' />'.$lt{$type}.'</label>&nbsp;';
 6511:            }
 6512:            $datatable .= '</td></tr>';
 6513:            $itemcount ++;
 6514:         }
 6515:         $$rowtotal += $itemcount;
 6516:     } elsif ($position eq 'middle') {
 6517:         my $toggle_cats_crs = ' ';
 6518:         my $toggle_cats_dom = ' checked="checked" ';
 6519:         my $can_cat_crs = ' ';
 6520:         my $can_cat_dom = ' checked="checked" ';
 6521:         my $toggle_catscomm_comm = ' ';
 6522:         my $toggle_catscomm_dom = ' checked="checked" ';
 6523:         my $can_catcomm_comm = ' ';
 6524:         my $can_catcomm_dom = ' checked="checked" ';
 6525: 
 6526:         if (ref($settings) eq 'HASH') {
 6527:             if ($settings->{'togglecats'} eq 'crs') {
 6528:                 $toggle_cats_crs = $toggle_cats_dom;
 6529:                 $toggle_cats_dom = ' ';
 6530:             }
 6531:             if ($settings->{'categorize'} eq 'crs') {
 6532:                 $can_cat_crs = $can_cat_dom;
 6533:                 $can_cat_dom = ' ';
 6534:             }
 6535:             if ($settings->{'togglecatscomm'} eq 'comm') {
 6536:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 6537:                 $toggle_catscomm_dom = ' ';
 6538:             }
 6539:             if ($settings->{'categorizecomm'} eq 'comm') {
 6540:                 $can_catcomm_comm = $can_catcomm_dom;
 6541:                 $can_catcomm_dom = ' ';
 6542:             }
 6543:         }
 6544:         my %title = &Apache::lonlocal::texthash (
 6545:                      togglecats     => 'Show/Hide a course in catalog',
 6546:                      togglecatscomm => 'Show/Hide a community in catalog',
 6547:                      categorize     => 'Assign a category to a course',
 6548:                      categorizecomm => 'Assign a category to a community',
 6549:                     );
 6550:         my %level = &Apache::lonlocal::texthash (
 6551:                      dom  => 'Set in Domain',
 6552:                      crs  => 'Set in Course',
 6553:                      comm => 'Set in Community',
 6554:                     );
 6555:         $datatable = '<tr class="LC_odd_row">'.
 6556:                   '<td>'.$title{'togglecats'}.'</td>'.
 6557:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 6558:                   '<input type="radio" name="togglecats"'.
 6559:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6560:                   '<label><input type="radio" name="togglecats"'.
 6561:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 6562:                   '</tr><tr>'.
 6563:                   '<td>'.$title{'categorize'}.'</td>'.
 6564:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6565:                   '<label><input type="radio" name="categorize"'.
 6566:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6567:                   '<label><input type="radio" name="categorize"'.
 6568:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 6569:                   '</tr><tr class="LC_odd_row">'.
 6570:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 6571:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 6572:                   '<input type="radio" name="togglecatscomm"'.
 6573:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6574:                   '<label><input type="radio" name="togglecatscomm"'.
 6575:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 6576:                   '</tr><tr>'.
 6577:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 6578:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6579:                   '<label><input type="radio" name="categorizecomm"'.
 6580:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6581:                   '<label><input type="radio" name="categorizecomm"'.
 6582:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 6583:                   '</tr>';
 6584:         $$rowtotal += 4;
 6585:     } else {
 6586:         my $css_class;
 6587:         my $itemcount = 1;
 6588:         my $cathash; 
 6589:         if (ref($settings) eq 'HASH') {
 6590:             $cathash = $settings->{'cats'};
 6591:         }
 6592:         if (ref($cathash) eq 'HASH') {
 6593:             my (@cats,@trails,%allitems,%idx,@jsarray);
 6594:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 6595:                                                    \%allitems,\%idx,\@jsarray);
 6596:             my $maxdepth = scalar(@cats);
 6597:             my $colattrib = '';
 6598:             if ($maxdepth > 2) {
 6599:                 $colattrib = ' colspan="2" ';
 6600:             }
 6601:             my @path;
 6602:             if (@cats > 0) {
 6603:                 if (ref($cats[0]) eq 'ARRAY') {
 6604:                     my $numtop = @{$cats[0]};
 6605:                     my $maxnum = $numtop;
 6606:                     my %default_names = (
 6607:                           instcode    => &mt('Official courses'),
 6608:                           communities => &mt('Communities'),
 6609:                     );
 6610: 
 6611:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 6612:                         ($cathash->{'instcode::0'} eq '') ||
 6613:                         (!grep(/^communities$/,@{$cats[0]})) || 
 6614:                         ($cathash->{'communities::0'} eq '')) {
 6615:                         $maxnum ++;
 6616:                     }
 6617:                     my $lastidx;
 6618:                     for (my $i=0; $i<$numtop; $i++) {
 6619:                         my $parent = $cats[0][$i];
 6620:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6621:                         my $item = &escape($parent).'::0';
 6622:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 6623:                         $lastidx = $idx{$item};
 6624:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6625:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 6626:                         for (my $k=0; $k<=$maxnum; $k++) {
 6627:                             my $vpos = $k+1;
 6628:                             my $selstr;
 6629:                             if ($k == $i) {
 6630:                                 $selstr = ' selected="selected" ';
 6631:                             }
 6632:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6633:                         }
 6634:                         $datatable .= '</select></span></td><td>';
 6635:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 6636:                             $datatable .=  '<span class="LC_nobreak">'
 6637:                                            .$default_names{$parent}.'</span>';
 6638:                             if ($parent eq 'instcode') {
 6639:                                 $datatable .= '<br /><span class="LC_nobreak">('
 6640:                                               .&mt('with institutional codes')
 6641:                                               .')</span></td><td'.$colattrib.'>';
 6642:                             } else {
 6643:                                 $datatable .= '<table><tr><td>';
 6644:                             }
 6645:                             $datatable .= '<span class="LC_nobreak">'
 6646:                                           .'<label><input type="radio" name="'
 6647:                                           .$parent.'" value="1" checked="checked" />'
 6648:                                           .&mt('Display').'</label>';
 6649:                             if ($parent eq 'instcode') {
 6650:                                 $datatable .= '&nbsp;';
 6651:                             } else {
 6652:                                 $datatable .= '</span></td></tr><tr><td>'
 6653:                                               .'<span class="LC_nobreak">';
 6654:                             }
 6655:                             $datatable .= '<label><input type="radio" name="'
 6656:                                           .$parent.'" value="0" />'
 6657:                                           .&mt('Do not display').'</label></span>';
 6658:                             if ($parent eq 'communities') {
 6659:                                 $datatable .= '</td></tr></table>';
 6660:                             }
 6661:                             $datatable .= '</td>';
 6662:                         } else {
 6663:                             $datatable .= $parent
 6664:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 6665:                                           .'<input type="checkbox" name="deletecategory" '
 6666:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 6667:                         }
 6668:                         my $depth = 1;
 6669:                         push(@path,$parent);
 6670:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 6671:                         pop(@path);
 6672:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 6673:                         $itemcount ++;
 6674:                     }
 6675:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6676:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 6677:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 6678:                     for (my $k=0; $k<=$maxnum; $k++) {
 6679:                         my $vpos = $k+1;
 6680:                         my $selstr;
 6681:                         if ($k == $numtop) {
 6682:                             $selstr = ' selected="selected" ';
 6683:                         }
 6684:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6685:                     }
 6686:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 6687:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 6688:                                   .'</tr>'."\n";
 6689:                     $itemcount ++;
 6690:                     foreach my $default ('instcode','communities') {
 6691:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 6692:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6693:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 6694:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 6695:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 6696:                             for (my $k=0; $k<=$maxnum; $k++) {
 6697:                                 my $vpos = $k+1;
 6698:                                 my $selstr;
 6699:                                 if ($k == $maxnum) {
 6700:                                     $selstr = ' selected="selected" ';
 6701:                                 }
 6702:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6703:                             }
 6704:                             $datatable .= '</select></span></td>'.
 6705:                                           '<td><span class="LC_nobreak">'.
 6706:                                           $default_names{$default}.'</span>';
 6707:                             if ($default eq 'instcode') {
 6708:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 6709:                                               .&mt('with institutional codes').')</span>';
 6710:                             }
 6711:                             $datatable .= '</td>'
 6712:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 6713:                                           .&mt('Display').'</label>&nbsp;'
 6714:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 6715:                                           .&mt('Do not display').'</label></span></td></tr>';
 6716:                         }
 6717:                     }
 6718:                 }
 6719:             } else {
 6720:                 $datatable .= &initialize_categories($itemcount);
 6721:             }
 6722:         } else {
 6723:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td>'
 6724:                           .&initialize_categories($itemcount);
 6725:         }
 6726:         $$rowtotal += $itemcount;
 6727:     }
 6728:     return $datatable;
 6729: }
 6730: 
 6731: sub print_serverstatuses {
 6732:     my ($dom,$settings,$rowtotal) = @_;
 6733:     my $datatable;
 6734:     my @pages = &serverstatus_pages();
 6735:     my (%namedaccess,%machineaccess);
 6736:     foreach my $type (@pages) {
 6737:         $namedaccess{$type} = '';
 6738:         $machineaccess{$type}= '';
 6739:     }
 6740:     if (ref($settings) eq 'HASH') {
 6741:         foreach my $type (@pages) {
 6742:             if (exists($settings->{$type})) {
 6743:                 if (ref($settings->{$type}) eq 'HASH') {
 6744:                     foreach my $key (keys(%{$settings->{$type}})) {
 6745:                         if ($key eq 'namedusers') {
 6746:                             $namedaccess{$type} = $settings->{$type}->{$key};
 6747:                         } elsif ($key eq 'machines') {
 6748:                             $machineaccess{$type} = $settings->{$type}->{$key};
 6749:                         }
 6750:                     }
 6751:                 }
 6752:             }
 6753:         }
 6754:     }
 6755:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 6756:     my $rownum = 0;
 6757:     my $css_class;
 6758:     foreach my $type (@pages) {
 6759:         $rownum ++;
 6760:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 6761:         $datatable .= '<tr'.$css_class.'>'.
 6762:                       '<td><span class="LC_nobreak">'.
 6763:                       $titles->{$type}.'</span></td>'.
 6764:                       '<td class="LC_left_item">'.
 6765:                       '<input type="text" name="'.$type.'_namedusers" '.
 6766:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 6767:                       '<td class="LC_right_item">'.
 6768:                       '<span class="LC_nobreak">'.
 6769:                       '<input type="text" name="'.$type.'_machines" '.
 6770:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 6771:                       '</td></tr>'."\n";
 6772:     }
 6773:     $$rowtotal += $rownum;
 6774:     return $datatable;
 6775: }
 6776: 
 6777: sub serverstatus_pages {
 6778:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 6779:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 6780:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 6781:             'uniquecodes','diskusage','coursecatalog');
 6782: }
 6783: 
 6784: sub defaults_javascript {
 6785:     my ($settings) = @_;
 6786:     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.');
 6787:     my $intauthcost = &mt('Warning: bcrypt encryption cost for internal authentication must be an integer.');
 6788:     &js_escape(\$intauthcheck);
 6789:     &js_escape(\$intauthcost);
 6790:     my $intauthjs = <<"ENDSCRIPT";
 6791: 
 6792: function warnIntAuth(field) {
 6793:     if (field.name == 'intauth_check') {
 6794:         if (field.value == '2') {
 6795:             alert('$intauthcheck');
 6796:         }
 6797:     }
 6798:     if (field.name == 'intauth_cost') {
 6799:         field.value.replace(/\s/g,'');
 6800:         if (field.value != '') {
 6801:             var regexdigit=/^\\d+\$/;
 6802:             if (!regexdigit.test(field.value)) {
 6803:                 alert('$intauthcost');
 6804:             }
 6805:         }
 6806:     }
 6807:     return;
 6808: }
 6809: 
 6810: ENDSCRIPT
 6811: 
 6812:     if (ref($settings) ne 'HASH') {
 6813:         return &Apache::lonhtmlcommon::scripttag($intauthjs);
 6814:     }
 6815:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 6816:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 6817:         if ($maxnum eq '') {
 6818:             $maxnum = 0;
 6819:         }
 6820:         $maxnum ++;
 6821:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 6822:         return <<"ENDSCRIPT";
 6823: <script type="text/javascript">
 6824: // <![CDATA[
 6825: function reorderTypes(form,caller) {
 6826:     var changedVal;
 6827: $jstext 
 6828:     var newpos = 'addinststatus_pos';
 6829:     var current = new Array;
 6830:     var maxh = $maxnum;
 6831:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 6832:     var oldVal;
 6833:     if (caller == newpos) {
 6834:         changedVal = newitemVal;
 6835:     } else {
 6836:         var curritem = 'inststatus_pos_'+caller;
 6837:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 6838:         current[newitemVal] = newpos;
 6839:     }
 6840:     for (var i=0; i<inststatuses.length; i++) {
 6841:         if (inststatuses[i] != caller) {
 6842:             var elementName = 'inststatus_pos_'+inststatuses[i];
 6843:             if (form.elements[elementName]) {
 6844:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 6845:                 current[currVal] = elementName;
 6846:             }
 6847:         }
 6848:     }
 6849:     for (var j=0; j<maxh; j++) {
 6850:         if (current[j] == undefined) {
 6851:             oldVal = j;
 6852:         }
 6853:     }
 6854:     if (oldVal < changedVal) {
 6855:         for (var k=oldVal+1; k<=changedVal ; k++) {
 6856:            var elementName = current[k];
 6857:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 6858:         }
 6859:     } else {
 6860:         for (var k=changedVal; k<oldVal; k++) {
 6861:             var elementName = current[k];
 6862:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 6863:         }
 6864:     }
 6865:     return;
 6866: }
 6867: 
 6868: $intauthjs
 6869: 
 6870: // ]]>
 6871: </script>
 6872: 
 6873: ENDSCRIPT
 6874:     } else {
 6875:         return &Apache::lonhtmlcommon::scripttag($intauthjs);
 6876:     }
 6877: }
 6878: 
 6879: sub coursecategories_javascript {
 6880:     my ($settings) = @_;
 6881:     my ($output,$jstext,$cathash);
 6882:     if (ref($settings) eq 'HASH') {
 6883:         $cathash = $settings->{'cats'};
 6884:     }
 6885:     if (ref($cathash) eq 'HASH') {
 6886:         my (@cats,@jsarray,%idx);
 6887:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 6888:         if (@jsarray > 0) {
 6889:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 6890:             for (my $i=0; $i<@jsarray; $i++) {
 6891:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 6892:                     my $catstr = join('","',@{$jsarray[$i]});
 6893:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 6894:                 }
 6895:             }
 6896:         }
 6897:     } else {
 6898:         $jstext  = '    var categories = Array(1);'."\n".
 6899:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 6900:     }
 6901:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 6902:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 6903:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
 6904:     &js_escape(\$instcode_reserved);
 6905:     &js_escape(\$communities_reserved);
 6906:     &js_escape(\$choose_again);
 6907:     $output = <<"ENDSCRIPT";
 6908: <script type="text/javascript">
 6909: // <![CDATA[
 6910: function reorderCats(form,parent,item,idx) {
 6911:     var changedVal;
 6912: $jstext
 6913:     var newpos = 'addcategory_pos';
 6914:     if (parent == '') {
 6915:         var has_instcode = 0;
 6916:         var maxtop = categories[idx].length;
 6917:         for (var j=0; j<maxtop; j++) {
 6918:             if (categories[idx][j] == 'instcode::0') {
 6919:                 has_instcode == 1;
 6920:             }
 6921:         }
 6922:         if (has_instcode == 0) {
 6923:             categories[idx][maxtop] = 'instcode_pos';
 6924:         }
 6925:     } else {
 6926:         newpos += '_'+parent;
 6927:     }
 6928:     var maxh = 1 + categories[idx].length;
 6929:     var current = new Array;
 6930:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 6931:     if (item == newpos) {
 6932:         changedVal = newitemVal;
 6933:     } else {
 6934:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 6935:         current[newitemVal] = newpos;
 6936:     }
 6937:     for (var i=0; i<categories[idx].length; i++) {
 6938:         var elementName = categories[idx][i];
 6939:         if (elementName != item) {
 6940:             if (form.elements[elementName]) {
 6941:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 6942:                 current[currVal] = elementName;
 6943:             }
 6944:         }
 6945:     }
 6946:     var oldVal;
 6947:     for (var j=0; j<maxh; j++) {
 6948:         if (current[j] == undefined) {
 6949:             oldVal = j;
 6950:         }
 6951:     }
 6952:     if (oldVal < changedVal) {
 6953:         for (var k=oldVal+1; k<=changedVal ; k++) {
 6954:            var elementName = current[k];
 6955:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 6956:         }
 6957:     } else {
 6958:         for (var k=changedVal; k<oldVal; k++) {
 6959:             var elementName = current[k];
 6960:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 6961:         }
 6962:     }
 6963:     return;
 6964: }
 6965: 
 6966: function categoryCheck(form) {
 6967:     if (form.elements['addcategory_name'].value == 'instcode') {
 6968:         alert('$instcode_reserved\\n$choose_again');
 6969:         return false;
 6970:     }
 6971:     if (form.elements['addcategory_name'].value == 'communities') {
 6972:         alert('$communities_reserved\\n$choose_again');
 6973:         return false;
 6974:     }
 6975:     return true;
 6976: }
 6977: 
 6978: // ]]>
 6979: </script>
 6980: 
 6981: ENDSCRIPT
 6982:     return $output;
 6983: }
 6984: 
 6985: sub initialize_categories {
 6986:     my ($itemcount) = @_;
 6987:     my ($datatable,$css_class,$chgstr);
 6988:     my %default_names = (
 6989:                       instcode    => 'Official courses (with institutional codes)',
 6990:                       communities => 'Communities',
 6991:                         );
 6992:     my $select0 = ' selected="selected"';
 6993:     my $select1 = '';
 6994:     foreach my $default ('instcode','communities') {
 6995:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6996:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 6997:         if ($default eq 'communities') {
 6998:             $select1 = $select0;
 6999:             $select0 = '';
 7000:         }
 7001:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7002:                      .'<select name="'.$default.'_pos">'
 7003:                      .'<option value="0"'.$select0.'>1</option>'
 7004:                      .'<option value="1"'.$select1.'>2</option>'
 7005:                      .'<option value="2">3</option></select>&nbsp;'
 7006:                      .$default_names{$default}
 7007:                      .'</span></td><td><span class="LC_nobreak">'
 7008:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 7009:                      .&mt('Display').'</label>&nbsp;<label>'
 7010:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 7011:                  .'</label></span></td></tr>';
 7012:         $itemcount ++;
 7013:     }
 7014:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7015:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 7016:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7017:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 7018:                   .'<option value="0">1</option>'
 7019:                   .'<option value="1">2</option>'
 7020:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 7021:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 7022:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 7023:     return $datatable;
 7024: }
 7025: 
 7026: sub build_category_rows {
 7027:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 7028:     my ($text,$name,$item,$chgstr);
 7029:     if (ref($cats) eq 'ARRAY') {
 7030:         my $maxdepth = scalar(@{$cats});
 7031:         if (ref($cats->[$depth]) eq 'HASH') {
 7032:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 7033:                 my $numchildren = @{$cats->[$depth]{$parent}};
 7034:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7035:                 $text .= '<td><table class="LC_data_table">';
 7036:                 my ($idxnum,$parent_name,$parent_item);
 7037:                 my $higher = $depth - 1;
 7038:                 if ($higher == 0) {
 7039:                     $parent_name = &escape($parent).'::'.$higher;
 7040:                 } else {
 7041:                     if (ref($path) eq 'ARRAY') {
 7042:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 7043:                     }
 7044:                 }
 7045:                 $parent_item = 'addcategory_pos_'.$parent_name;
 7046:                 for (my $j=0; $j<=$numchildren; $j++) {
 7047:                     if ($j < $numchildren) {
 7048:                         $name = $cats->[$depth]{$parent}[$j];
 7049:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 7050:                         $idxnum = $idx->{$item};
 7051:                     } else {
 7052:                         $name = $parent_name;
 7053:                         $item = $parent_item;
 7054:                     }
 7055:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 7056:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 7057:                     for (my $i=0; $i<=$numchildren; $i++) {
 7058:                         my $vpos = $i+1;
 7059:                         my $selstr;
 7060:                         if ($j == $i) {
 7061:                             $selstr = ' selected="selected" ';
 7062:                         }
 7063:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 7064:                     }
 7065:                     $text .= '</select>&nbsp;';
 7066:                     if ($j < $numchildren) {
 7067:                         my $deeper = $depth+1;
 7068:                         $text .= $name.'&nbsp;'
 7069:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 7070:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 7071:                         if(ref($path) eq 'ARRAY') {
 7072:                             push(@{$path},$name);
 7073:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 7074:                             pop(@{$path});
 7075:                         }
 7076:                     } else {
 7077:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 7078:                         if ($j == $numchildren) {
 7079:                             $text .= $name;
 7080:                         } else {
 7081:                             $text .= $item;
 7082:                         }
 7083:                         $text .= '" value="" />';
 7084:                     }
 7085:                     $text .= '</td></tr>';
 7086:                 }
 7087:                 $text .= '</table></td>';
 7088:             } else {
 7089:                 my $higher = $depth-1;
 7090:                 if ($higher == 0) {
 7091:                     $name = &escape($parent).'::'.$higher;
 7092:                 } else {
 7093:                     if (ref($path) eq 'ARRAY') {
 7094:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 7095:                     }
 7096:                 }
 7097:                 my $colspan;
 7098:                 if ($parent ne 'instcode') {
 7099:                     $colspan = $maxdepth - $depth - 1;
 7100:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 7101:                 }
 7102:             }
 7103:         }
 7104:     }
 7105:     return $text;
 7106: }
 7107: 
 7108: sub modifiable_userdata_row {
 7109:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref) = @_;
 7110:     my ($role,$rolename,$statustype);
 7111:     $role = $item;
 7112:     if ($context eq 'cancreate') {
 7113:         if ($item =~ /^emailusername_(.+)$/) {
 7114:             $statustype = $1;
 7115:             $role = 'emailusername';
 7116:             if (ref($usertypes) eq 'HASH') {
 7117:                 if ($usertypes->{$statustype}) {
 7118:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 7119:                 } else {
 7120:                     $rolename = &mt('Data provided by user');
 7121:                 }
 7122:             }
 7123:         }
 7124:     } elsif ($context eq 'selfcreate') {
 7125:         if (ref($usertypes) eq 'HASH') {
 7126:             $rolename = $usertypes->{$role};
 7127:         } else {
 7128:             $rolename = $role;
 7129:         }
 7130:     } else {
 7131:         if ($role eq 'cr') {
 7132:             $rolename = &mt('Custom role');
 7133:         } else {
 7134:             $rolename = &Apache::lonnet::plaintext($role);
 7135:         }
 7136:     }
 7137:     my (@fields,%fieldtitles);
 7138:     if (ref($fieldsref) eq 'ARRAY') {
 7139:         @fields = @{$fieldsref};
 7140:     } else {
 7141:         @fields = ('lastname','firstname','middlename','generation',
 7142:                    'permanentemail','id');
 7143:     }
 7144:     if ((ref($titlesref) eq 'HASH')) {
 7145:         %fieldtitles = %{$titlesref};
 7146:     } else {
 7147:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7148:     }
 7149:     my $output;
 7150:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 7151:     $output = '<tr '.$css_class.'>'.
 7152:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 7153:               '<td class="LC_left_item" colspan="2"><table>';
 7154:     my $rem;
 7155:     my %checks;
 7156:     if (ref($settings) eq 'HASH') {
 7157:         if (ref($settings->{$context}) eq 'HASH') {
 7158:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 7159:                 my $hashref = $settings->{$context}->{$role};
 7160:                 if ($role eq 'emailusername') {
 7161:                     if ($statustype) {
 7162:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 7163:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 7164:                             if (ref($hashref) eq 'HASH') { 
 7165:                                 foreach my $field (@fields) {
 7166:                                     if ($hashref->{$field}) {
 7167:                                         $checks{$field} = $hashref->{$field};
 7168:                                     }
 7169:                                 }
 7170:                             }
 7171:                         }
 7172:                     }
 7173:                 } else {
 7174:                     if (ref($hashref) eq 'HASH') {
 7175:                         foreach my $field (@fields) {
 7176:                             if ($hashref->{$field}) {
 7177:                                 $checks{$field} = ' checked="checked" ';
 7178:                             }
 7179:                         }
 7180:                     }
 7181:                 }
 7182:             }
 7183:         }
 7184:     }
 7185:      
 7186:     for (my $i=0; $i<@fields; $i++) {
 7187:         my $rem = $i%($numinrow);
 7188:         if ($rem == 0) {
 7189:             if ($i > 0) {
 7190:                 $output .= '</tr>';
 7191:             }
 7192:             $output .= '<tr>';
 7193:         }
 7194:         my $check = ' ';
 7195:         unless ($role eq 'emailusername') {
 7196:             if (exists($checks{$fields[$i]})) {
 7197:                 $check = $checks{$fields[$i]}
 7198:             } else {
 7199:                 if ($role eq 'st') {
 7200:                     if (ref($settings) ne 'HASH') {
 7201:                         $check = ' checked="checked" '; 
 7202:                     }
 7203:                 }
 7204:             }
 7205:         }
 7206:         $output .= '<td class="LC_left_item">'.
 7207:                    '<span class="LC_nobreak">';
 7208:         if ($role eq 'emailusername') {
 7209:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 7210:                 $checks{$fields[$i]} = 'omit';
 7211:             }
 7212:             foreach my $option ('required','optional','omit') {
 7213:                 my $checked='';
 7214:                 if ($checks{$fields[$i]} eq $option) {
 7215:                     $checked='checked="checked" ';
 7216:                 }
 7217:                 $output .= '<label>'.
 7218:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 7219:                            &mt($option).'</label>'.('&nbsp;' x2);
 7220:             }
 7221:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 7222:         } else {
 7223:             $output .= '<label>'.
 7224:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 7225:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 7226:                        '</label>';
 7227:         }
 7228:         $output .= '</span></td>';
 7229:         $rem = @fields%($numinrow);
 7230:     }
 7231:     my $colsleft = $numinrow - $rem;
 7232:     if ($colsleft > 1 ) {
 7233:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7234:                    '&nbsp;</td>';
 7235:     } elsif ($colsleft == 1) {
 7236:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 7237:     }
 7238:     $output .= '</tr></table></td></tr>';
 7239:     return $output;
 7240: }
 7241: 
 7242: sub insttypes_row {
 7243:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
 7244:         $customcss,$rowstyle) = @_;
 7245:     my %lt = &Apache::lonlocal::texthash (
 7246:                       cansearch => 'Users allowed to search',
 7247:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 7248:                       lockablenames => 'User preference to lock name',
 7249:                       overrides     => "Override domain's helpdesk settings based on requester's affiliation",
 7250:              );
 7251:     my $showdom;
 7252:     if ($context eq 'cansearch') {
 7253:         $showdom = ' ('.$dom.')';
 7254:     }
 7255:     my $class = 'LC_left_item';
 7256:     if ($context eq 'statustocreate') {
 7257:         $class = 'LC_right_item';
 7258:     }
 7259:     my $css_class;
 7260:     if ($$rowtotal%2) {
 7261:         $css_class = 'LC_odd_row';
 7262:     }
 7263:     if ($customcss) {
 7264:         $css_class .= ' '.$customcss;
 7265:     }
 7266:     $css_class =~ s/^\s+//;
 7267:     if ($css_class) {
 7268:         $css_class = ' class="'.$css_class.'"';
 7269:     }
 7270:     if ($rowstyle) {
 7271:         $css_class .= ' style="'.$rowstyle.'"';
 7272:     }
 7273:     if ($onclick) {
 7274:         $onclick = 'onclick="'.$onclick.'" ';
 7275:     }
 7276:     my $output = '<tr'.$css_class.'>'.
 7277:                  '<td>'.$lt{$context}.$showdom.
 7278:                  '</td><td class="'.$class.'" colspan="2"><table>';
 7279:     my $rem;
 7280:     if (ref($types) eq 'ARRAY') {
 7281:         for (my $i=0; $i<@{$types}; $i++) {
 7282:             if (defined($usertypes->{$types->[$i]})) {
 7283:                 my $rem = $i%($numinrow);
 7284:                 if ($rem == 0) {
 7285:                     if ($i > 0) {
 7286:                         $output .= '</tr>';
 7287:                     }
 7288:                     $output .= '<tr>';
 7289:                 }
 7290:                 my $check = ' ';
 7291:                 if (ref($settings) eq 'HASH') {
 7292:                     if (ref($settings->{$context}) eq 'ARRAY') {
 7293:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 7294:                             $check = ' checked="checked" ';
 7295:                         }
 7296:                     } elsif (ref($settings->{$context}) eq 'HASH') {
 7297:                         if (ref($settings->{$context}->{$types->[$i]}) eq 'HASH') {
 7298:                             $check = ' checked="checked" ';
 7299:                         }
 7300:                     } elsif ($context eq 'statustocreate') {
 7301:                         $check = ' checked="checked" ';
 7302:                     }
 7303:                 }
 7304:                 $output .= '<td class="LC_left_item">'.
 7305:                            '<span class="LC_nobreak"><label>'.
 7306:                            '<input type="checkbox" name="'.$context.'" '.
 7307:                            'value="'.$types->[$i].'"'.$check.'/>'.
 7308:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 7309:             }
 7310:         }
 7311:         $rem = @{$types}%($numinrow);
 7312:     }
 7313:     my $colsleft = $numinrow - $rem;
 7314:     if ($context eq 'overrides') {
 7315:         if ($colsleft > 1) {
 7316:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 7317:         } else {
 7318:             $output .= '<td class="LC_left_item">';
 7319:         }
 7320:         $output .= '&nbsp;';
 7321:     } else {
 7322:         if (($rem == 0) && (@{$types} > 0)) {
 7323:             $output .= '<tr>';
 7324:         }
 7325:         if ($colsleft > 1) {
 7326:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 7327:         } else {
 7328:             $output .= '<td class="LC_left_item">';
 7329:         }
 7330:         my $defcheck = ' ';
 7331:         if (ref($settings) eq 'HASH') {  
 7332:             if (ref($settings->{$context}) eq 'ARRAY') {
 7333:                 if (grep(/^default$/,@{$settings->{$context}})) {
 7334:                     $defcheck = ' checked="checked" ';
 7335:                 }
 7336:             } elsif ($context eq 'statustocreate') {
 7337:                 $defcheck = ' checked="checked" ';
 7338:             }
 7339:         }
 7340:         $output .= '<span class="LC_nobreak"><label>'.
 7341:                    '<input type="checkbox" name="'.$context.'" '.
 7342:                    'value="default"'.$defcheck.'/>'.
 7343:                    $othertitle.'</label></span>';
 7344:     }
 7345:     $output .= '</td></tr></table></td></tr>';
 7346:     return $output;
 7347: }
 7348: 
 7349: sub sorted_searchtitles {
 7350:     my %searchtitles = &Apache::lonlocal::texthash(
 7351:                          'uname' => 'username',
 7352:                          'lastname' => 'last name',
 7353:                          'lastfirst' => 'last name, first name',
 7354:                      );
 7355:     my @titleorder = ('uname','lastname','lastfirst');
 7356:     return (\%searchtitles,\@titleorder);
 7357: }
 7358: 
 7359: sub sorted_searchtypes {
 7360:     my %srchtypes_desc = (
 7361:                            exact    => 'is exact match',
 7362:                            contains => 'contains ..',
 7363:                            begins   => 'begins with ..',
 7364:                          );
 7365:     my @srchtypeorder = ('exact','begins','contains');
 7366:     return (\%srchtypes_desc,\@srchtypeorder);
 7367: }
 7368: 
 7369: sub usertype_update_row {
 7370:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 7371:     my $datatable;
 7372:     my $numinrow = 4;
 7373:     foreach my $type (@{$types}) {
 7374:         if (defined($usertypes->{$type})) {
 7375:             $$rownums ++;
 7376:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 7377:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 7378:                           '</td><td class="LC_left_item"><table>';
 7379:             for (my $i=0; $i<@{$fields}; $i++) {
 7380:                 my $rem = $i%($numinrow);
 7381:                 if ($rem == 0) {
 7382:                     if ($i > 0) {
 7383:                         $datatable .= '</tr>';
 7384:                     }
 7385:                     $datatable .= '<tr>';
 7386:                 }
 7387:                 my $check = ' ';
 7388:                 if (ref($settings) eq 'HASH') {
 7389:                     if (ref($settings->{'fields'}) eq 'HASH') {
 7390:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 7391:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 7392:                                 $check = ' checked="checked" ';
 7393:                             }
 7394:                         }
 7395:                     }
 7396:                 }
 7397: 
 7398:                 if ($i == @{$fields}-1) {
 7399:                     my $colsleft = $numinrow - $rem;
 7400:                     if ($colsleft > 1) {
 7401:                         $datatable .= '<td colspan="'.$colsleft.'">';
 7402:                     } else {
 7403:                         $datatable .= '<td>';
 7404:                     }
 7405:                 } else {
 7406:                     $datatable .= '<td>';
 7407:                 }
 7408:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7409:                               '<input type="checkbox" name="updateable_'.$type.
 7410:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 7411:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 7412:             }
 7413:             $datatable .= '</tr></table></td></tr>';
 7414:         }
 7415:     }
 7416:     return $datatable;
 7417: }
 7418: 
 7419: sub modify_login {
 7420:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 7421:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 7422:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 7423:     %title = ( coursecatalog => 'Display course catalog',
 7424:                adminmail => 'Display administrator E-mail address',
 7425:                helpdesk  => 'Display "Contact Helpdesk" link',
 7426:                newuser => 'Link for visitors to create a user account',
 7427:                loginheader => 'Log-in box header');
 7428:     @offon = ('off','on');
 7429:     if (ref($domconfig{login}) eq 'HASH') {
 7430:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 7431:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 7432:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 7433:             }
 7434:         }
 7435:     }
 7436:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 7437:                                            \%domconfig,\%loginhash);
 7438:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 7439:     foreach my $item (@toggles) {
 7440:         $loginhash{login}{$item} = $env{'form.'.$item};
 7441:     }
 7442:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 7443:     if (ref($colchanges{'login'}) eq 'HASH') {  
 7444:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 7445:                                          \%loginhash);
 7446:     }
 7447: 
 7448:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7449:     my %domservers = &Apache::lonnet::get_servers($dom);
 7450:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 7451:     if (keys(%servers) > 1) {
 7452:         foreach my $lonhost (keys(%servers)) {
 7453:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 7454:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 7455:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 7456:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 7457:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 7458:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7459:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7460:                         $changes{'loginvia'}{$lonhost} = 1;
 7461:                     } else {
 7462:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 7463:                         $changes{'loginvia'}{$lonhost} = 1;
 7464:                     }
 7465:                 } else {
 7466:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7467:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7468:                         $changes{'loginvia'}{$lonhost} = 1;
 7469:                     }
 7470:                 }
 7471:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 7472:                     foreach my $item (@loginvia_attribs) {
 7473:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 7474:                     }
 7475:                 } else {
 7476:                     foreach my $item (@loginvia_attribs) {
 7477:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 7478:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 7479:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 7480:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 7481:                                 $new = '/';
 7482:                             }
 7483:                         }
 7484:                         if (($item eq 'custompath') && 
 7485:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 7486:                             $new = '';
 7487:                         }
 7488:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 7489:                             $changes{'loginvia'}{$lonhost} = 1;
 7490:                         }
 7491:                         if ($item eq 'exempt') {
 7492:                             $new = &check_exempt_addresses($new);
 7493:                         }
 7494:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 7495:                     }
 7496:                 }
 7497:             } else {
 7498:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7499:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7500:                     $changes{'loginvia'}{$lonhost} = 1;
 7501:                     foreach my $item (@loginvia_attribs) {
 7502:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 7503:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 7504:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 7505:                                 $new = '/';
 7506:                             }
 7507:                         }
 7508:                         if (($item eq 'custompath') && 
 7509:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 7510:                             $new = '';
 7511:                         }
 7512:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 7513:                     }
 7514:                 }
 7515:             }
 7516:         }
 7517:     }
 7518: 
 7519:     my $servadm = $r->dir_config('lonAdmEMail');
 7520:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 7521:     if (ref($domconfig{'login'}) eq 'HASH') {
 7522:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 7523:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 7524:                 if ($lang eq 'nolang') {
 7525:                     push(@currlangs,$lang);
 7526:                 } elsif (defined($langchoices{$lang})) {
 7527:                     push(@currlangs,$lang);
 7528:                 } else {
 7529:                     next;
 7530:                 }
 7531:             }
 7532:         }
 7533:     }
 7534:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 7535:     if (@currlangs > 0) {
 7536:         foreach my $lang (@currlangs) {
 7537:             if (grep(/^\Q$lang\E$/,@delurls)) {
 7538:                 $changes{'helpurl'}{$lang} = 1;
 7539:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 7540:                 $changes{'helpurl'}{$lang} = 1;
 7541:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 7542:                 push(@newlangs,$lang);
 7543:             } else {
 7544:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 7545:             }
 7546:         }
 7547:     }
 7548:     unless (grep(/^nolang$/,@currlangs)) {
 7549:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 7550:             $changes{'helpurl'}{'nolang'} = 1;
 7551:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 7552:             push(@newlangs,'nolang');
 7553:         }
 7554:     }
 7555:     if ($env{'form.loginhelpurl_add_lang'}) {
 7556:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 7557:             ($env{'form.loginhelpurl_add_file.filename'})) {
 7558:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 7559:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 7560:         }
 7561:     }
 7562:     if ((@newlangs > 0) || ($addedfile)) {
 7563:         my $error;
 7564:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 7565:         if ($configuserok eq 'ok') {
 7566:             if ($switchserver) {
 7567:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 7568:             } elsif ($author_ok eq 'ok') {
 7569:                 my @allnew = @newlangs;
 7570:                 if ($addedfile ne '') {
 7571:                     push(@allnew,$addedfile);
 7572:                 }
 7573:                 foreach my $lang (@allnew) {
 7574:                     my $formelem = 'loginhelpurl_'.$lang;
 7575:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 7576:                         $formelem = 'loginhelpurl_add_file';
 7577:                     }
 7578:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 7579:                                                                "help/$lang",'','',$newfile{$lang});
 7580:                     if ($result eq 'ok') {
 7581:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 7582:                         $changes{'helpurl'}{$lang} = 1;
 7583:                     } else {
 7584:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 7585:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 7586:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 7587:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 7588:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 7589:                         }
 7590:                     }
 7591:                 }
 7592:             } else {
 7593:                 $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);
 7594:             }
 7595:         } else {
 7596:             $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);
 7597:         }
 7598:         if ($error) {
 7599:             &Apache::lonnet::logthis($error);
 7600:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7601:         }
 7602:     }
 7603: 
 7604:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
 7605:     if (ref($domconfig{'login'}) eq 'HASH') {
 7606:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
 7607:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
 7608:                 if ($domservers{$lonhost}) {
 7609:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 7610:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
 7611:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
 7612:                     }
 7613:                 }
 7614:             }
 7615:         }
 7616:     }
 7617:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
 7618:     foreach my $lonhost (sort(keys(%domservers))) {
 7619:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 7620:             $changes{'headtag'}{$lonhost} = 1;
 7621:         } else {
 7622:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
 7623:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
 7624:             }
 7625:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
 7626:                 push(@newhosts,$lonhost);
 7627:             } elsif ($currheadtagurls{$lonhost}) {
 7628:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
 7629:                 if ($currexempt{$lonhost}) {
 7630:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
 7631:                         $changes{'headtag'}{$lonhost} = 1;
 7632:                     }
 7633:                 } elsif ($possexempt{$lonhost}) {
 7634:                     $changes{'headtag'}{$lonhost} = 1;
 7635:                 }
 7636:                 if ($possexempt{$lonhost}) {
 7637:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 7638:                 }
 7639:             }
 7640:         }
 7641:     }
 7642:     if (@newhosts) {
 7643:         my $error;
 7644:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 7645:         if ($configuserok eq 'ok') {
 7646:             if ($switchserver) {
 7647:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
 7648:             } elsif ($author_ok eq 'ok') {
 7649:                 foreach my $lonhost (@newhosts) {
 7650:                     my $formelem = 'loginheadtag_'.$lonhost;
 7651:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 7652:                                                                           "login/headtag/$lonhost",'','',
 7653:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
 7654:                     if ($result eq 'ok') {
 7655:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
 7656:                         $changes{'headtag'}{$lonhost} = 1;
 7657:                         if ($possexempt{$lonhost}) {
 7658:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 7659:                         }
 7660:                     } else {
 7661:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
 7662:                                            $newheadtagurls{$lonhost},$result);
 7663:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 7664:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
 7665:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
 7666:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
 7667:                         }
 7668:                     }
 7669:                 }
 7670:             } else {
 7671:                 $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);
 7672:             }
 7673:         } else {
 7674:             $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);
 7675:         }
 7676:         if ($error) {
 7677:             &Apache::lonnet::logthis($error);
 7678:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7679:         }
 7680:     }
 7681:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 7682: 
 7683:     my $defaulthelpfile = '/adm/loginproblems.html';
 7684:     my $defaulttext = &mt('Default in use');
 7685: 
 7686:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 7687:                                              $dom);
 7688:     if ($putresult eq 'ok') {
 7689:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 7690:         my %defaultchecked = (
 7691:                     'coursecatalog' => 'on',
 7692:                     'helpdesk'      => 'on',
 7693:                     'adminmail'     => 'off',
 7694:                     'newuser'       => 'off',
 7695:         );
 7696:         if (ref($domconfig{'login'}) eq 'HASH') {
 7697:             foreach my $item (@toggles) {
 7698:                 if ($defaultchecked{$item} eq 'on') { 
 7699:                     if (($domconfig{'login'}{$item} eq '0') &&
 7700:                         ($env{'form.'.$item} eq '1')) {
 7701:                         $changes{$item} = 1;
 7702:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 7703:                               $domconfig{'login'}{$item} eq '1') &&
 7704:                              ($env{'form.'.$item} eq '0')) {
 7705:                         $changes{$item} = 1;
 7706:                     }
 7707:                 } elsif ($defaultchecked{$item} eq 'off') {
 7708:                     if (($domconfig{'login'}{$item} eq '1') &&
 7709:                         ($env{'form.'.$item} eq '0')) {
 7710:                         $changes{$item} = 1;
 7711:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 7712:                               $domconfig{'login'}{$item} eq '0') &&
 7713:                              ($env{'form.'.$item} eq '1')) {
 7714:                         $changes{$item} = 1;
 7715:                     }
 7716:                 }
 7717:             }
 7718:         }
 7719:         if (keys(%changes) > 0 || $colchgtext) {
 7720:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 7721:             if (ref($lastactref) eq 'HASH') {
 7722:                 $lastactref->{'domainconfig'} = 1;
 7723:             }
 7724:             $resulttext = &mt('Changes made:').'<ul>';
 7725:             foreach my $item (sort(keys(%changes))) {
 7726:                 if ($item eq 'loginvia') {
 7727:                     if (ref($changes{$item}) eq 'HASH') {
 7728:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 7729:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 7730:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 7731:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 7732:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 7733:                                     $protocol = 'http' if ($protocol ne 'https');
 7734:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 7735: 
 7736:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 7737:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 7738:                                     } else {
 7739:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 7740:                                     }
 7741:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 7742:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 7743:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 7744:                                     }
 7745:                                     $resulttext .= '</li>';
 7746:                                 } else {
 7747:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 7748:                                 }
 7749:                             } else {
 7750:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 7751:                             }
 7752:                         }
 7753:                         $resulttext .= '</ul></li>';
 7754:                     }
 7755:                 } elsif ($item eq 'helpurl') {
 7756:                     if (ref($changes{$item}) eq 'HASH') {
 7757:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 7758:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 7759:                                 my ($chg,$link);
 7760:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 7761:                                 if ($lang eq 'nolang') {
 7762:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 7763:                                 } else {
 7764:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 7765:                                 }
 7766:                                 $resulttext .= '<li>'.$chg.'</li>';
 7767:                             } else {
 7768:                                 my $chg;
 7769:                                 if ($lang eq 'nolang') {
 7770:                                     $chg = &mt('custom log-in help file for no preferred language');
 7771:                                 } else {
 7772:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 7773:                                 }
 7774:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 7775:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 7776:                                                       '?inhibitmenu=yes',$chg,600,500).
 7777:                                                '</li>';
 7778:                             }
 7779:                         }
 7780:                     }
 7781:                 } elsif ($item eq 'headtag') {
 7782:                     if (ref($changes{$item}) eq 'HASH') {
 7783:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 7784:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 7785:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
 7786:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 7787:                                 $resulttext .= '<li><a href="'.
 7788:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
 7789:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 7790:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
 7791:                                 if ($possexempt{$lonhost}) {
 7792:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
 7793:                                 } else {
 7794:                                     $resulttext .= &mt('included for any client IP');
 7795:                                 }
 7796:                                 $resulttext .= '</li>';
 7797:                             }
 7798:                         }
 7799:                     }
 7800:                 } elsif ($item eq 'captcha') {
 7801:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7802:                         my $chgtxt;
 7803:                         if ($loginhash{'login'}{$item} eq 'notused') {
 7804:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 7805:                         } else {
 7806:                             my %captchas = &captcha_phrases();
 7807:                             if ($captchas{$loginhash{'login'}{$item}}) {
 7808:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 7809:                             } else {
 7810:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 7811:                             }
 7812:                         }
 7813:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 7814:                     }
 7815:                 } elsif ($item eq 'recaptchakeys') {
 7816:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7817:                         my ($privkey,$pubkey);
 7818:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 7819:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 7820:                             $privkey = $loginhash{'login'}{$item}{'private'};
 7821:                         }
 7822:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 7823:                         if (!$pubkey) {
 7824:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 7825:                         } else {
 7826:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 7827:                         }
 7828:                         if (!$privkey) {
 7829:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 7830:                         } else {
 7831:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 7832:                         }
 7833:                         $chgtxt .= '</ul>';
 7834:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 7835:                     }
 7836:                 } elsif ($item eq 'recaptchaversion') {
 7837:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7838:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
 7839:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
 7840:                                            '</li>';
 7841:                         }
 7842:                     }
 7843:                 } else {
 7844:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 7845:                 }
 7846:             }
 7847:             $resulttext .= $colchgtext.'</ul>';
 7848:         } else {
 7849:             $resulttext = &mt('No changes made to log-in page settings');
 7850:         }
 7851:     } else {
 7852:         $resulttext = '<span class="LC_error">'.
 7853: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7854:     }
 7855:     if ($errors) {
 7856:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 7857:                        $errors.'</ul>';
 7858:     }
 7859:     return $resulttext;
 7860: }
 7861: 
 7862: sub check_exempt_addresses {
 7863:     my ($iplist) = @_;
 7864:     $iplist =~ s/^\s+//;
 7865:     $iplist =~ s/\s+$//;
 7866:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
 7867:     my (@okips,$new);
 7868:     foreach my $ip (@poss_ips) {
 7869:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 7870:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 7871:                 push(@okips,$ip);
 7872:             }
 7873:         }
 7874:     }
 7875:     if (@okips > 0) {
 7876:         $new = join(',',@okips);
 7877:     } else {
 7878:         $new = '';
 7879:     }
 7880:     return $new;
 7881: }
 7882: 
 7883: sub color_font_choices {
 7884:     my %choices =
 7885:         &Apache::lonlocal::texthash (
 7886:             img => "Header",
 7887:             bgs => "Background colors",
 7888:             links => "Link colors",
 7889:             images => "Images",
 7890:             font => "Font color",
 7891:             fontmenu => "Font menu",
 7892:             pgbg => "Page",
 7893:             tabbg => "Header",
 7894:             sidebg => "Border",
 7895:             link => "Link",
 7896:             alink => "Active link",
 7897:             vlink => "Visited link",
 7898:         );
 7899:     return %choices;
 7900: }
 7901: 
 7902: sub modify_rolecolors {
 7903:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 7904:     my ($resulttext,%rolehash);
 7905:     $rolehash{'rolecolors'} = {};
 7906:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 7907:         if ($domconfig{'rolecolors'} eq '') {
 7908:             $domconfig{'rolecolors'} = {};
 7909:         }
 7910:     }
 7911:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 7912:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 7913:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 7914:                                              $dom);
 7915:     if ($putresult eq 'ok') {
 7916:         if (keys(%changes) > 0) {
 7917:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 7918:             if (ref($lastactref) eq 'HASH') {
 7919:                 $lastactref->{'domainconfig'} = 1;
 7920:             }
 7921:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 7922:                                              $rolehash{'rolecolors'});
 7923:         } else {
 7924:             $resulttext = &mt('No changes made to default color schemes');
 7925:         }
 7926:     } else {
 7927:         $resulttext = '<span class="LC_error">'.
 7928: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7929:     }
 7930:     if ($errors) {
 7931:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 7932:                        $errors.'</ul>';
 7933:     }
 7934:     return $resulttext;
 7935: }
 7936: 
 7937: sub modify_colors {
 7938:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 7939:     my (%changes,%choices);
 7940:     my @bgs;
 7941:     my @links = ('link','alink','vlink');
 7942:     my @logintext;
 7943:     my @images;
 7944:     my $servadm = $r->dir_config('lonAdmEMail');
 7945:     my $errors;
 7946:     my %defaults;
 7947:     foreach my $role (@{$roles}) {
 7948:         if ($role eq 'login') {
 7949:             %choices = &login_choices();
 7950:             @logintext = ('textcol','bgcol');
 7951:         } else {
 7952:             %choices = &color_font_choices();
 7953:         }
 7954:         if ($role eq 'login') {
 7955:             @images = ('img','logo','domlogo','login');
 7956:             @bgs = ('pgbg','mainbg','sidebg');
 7957:         } else {
 7958:             @images = ('img');
 7959:             @bgs = ('pgbg','tabbg','sidebg');
 7960:         }
 7961:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 7962:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 7963:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 7964:         }
 7965:         if ($role eq 'login') {
 7966:             foreach my $item (@logintext) {
 7967:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 7968:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 7969:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 7970:                 }
 7971:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 7972:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 7973:                 }
 7974:             }
 7975:         } else {
 7976:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 7977:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 7978:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 7979:             }
 7980:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 7981:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 7982:             }
 7983:         }
 7984:         foreach my $item (@bgs) {
 7985:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 7986:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 7987:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 7988:             }
 7989:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 7990:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 7991:             }
 7992:         }
 7993:         foreach my $item (@links) {
 7994:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 7995:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 7996:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 7997:             }
 7998:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 7999:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 8000:             }
 8001:         }
 8002:         my ($configuserok,$author_ok,$switchserver) = 
 8003:             &config_check($dom,$confname,$servadm);
 8004:         my ($width,$height) = &thumb_dimensions();
 8005:         if (ref($domconfig->{$role}) ne 'HASH') {
 8006:             $domconfig->{$role} = {};
 8007:         }
 8008:         foreach my $img (@images) {
 8009:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 8010:                 if (defined($env{'form.login_showlogo_'.$img})) {
 8011:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 8012:                 } else { 
 8013:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 8014:                 }
 8015:             } 
 8016: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 8017: 		 && !defined($domconfig->{$role}{$img})
 8018: 		 && !$env{'form.'.$role.'_del_'.$img}
 8019: 		 && $env{'form.'.$role.'_import_'.$img}) {
 8020: 		# import the old configured image from the .tab setting
 8021: 		# if they haven't provided a new one 
 8022: 		$domconfig->{$role}{$img} = 
 8023: 		    $env{'form.'.$role.'_import_'.$img};
 8024: 	    }
 8025:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 8026:                 my $error;
 8027:                 if ($configuserok eq 'ok') {
 8028:                     if ($switchserver) {
 8029:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 8030:                     } else {
 8031:                         if ($author_ok eq 'ok') {
 8032:                             my ($result,$logourl) = 
 8033:                                 &publishlogo($r,'upload',$role.'_'.$img,
 8034:                                            $dom,$confname,$img,$width,$height);
 8035:                             if ($result eq 'ok') {
 8036:                                 $confhash->{$role}{$img} = $logourl;
 8037:                                 $changes{$role}{'images'}{$img} = 1;
 8038:                             } else {
 8039:                                 $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);
 8040:                             }
 8041:                         } else {
 8042:                             $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);
 8043:                         }
 8044:                     }
 8045:                 } else {
 8046:                     $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);
 8047:                 }
 8048:                 if ($error) {
 8049:                     &Apache::lonnet::logthis($error);
 8050:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8051:                 }
 8052:             } elsif ($domconfig->{$role}{$img} ne '') {
 8053:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 8054:                     my $error;
 8055:                     if ($configuserok eq 'ok') {
 8056: # is confname an author?
 8057:                         if ($switchserver eq '') {
 8058:                             if ($author_ok eq 'ok') {
 8059:                                 my ($result,$logourl) = 
 8060:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 8061:                                             $dom,$confname,$img,$width,$height);
 8062:                                 if ($result eq 'ok') {
 8063:                                     $confhash->{$role}{$img} = $logourl;
 8064: 				    $changes{$role}{'images'}{$img} = 1;
 8065:                                 }
 8066:                             }
 8067:                         }
 8068:                     }
 8069:                 }
 8070:             }
 8071:         }
 8072:         if (ref($domconfig) eq 'HASH') {
 8073:             if (ref($domconfig->{$role}) eq 'HASH') {
 8074:                 foreach my $img (@images) {
 8075:                     if ($domconfig->{$role}{$img} ne '') {
 8076:                         if ($env{'form.'.$role.'_del_'.$img}) {
 8077:                             $confhash->{$role}{$img} = '';
 8078:                             $changes{$role}{'images'}{$img} = 1;
 8079:                         } else {
 8080:                             if ($confhash->{$role}{$img} eq '') {
 8081:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 8082:                             }
 8083:                         }
 8084:                     } else {
 8085:                         if ($env{'form.'.$role.'_del_'.$img}) {
 8086:                             $confhash->{$role}{$img} = '';
 8087:                             $changes{$role}{'images'}{$img} = 1;
 8088:                         } 
 8089:                     }
 8090:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 8091:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 8092:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 8093:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 8094:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 8095:                             }
 8096:                         } else {
 8097:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 8098:                                 $changes{$role}{'showlogo'}{$img} = 1;
 8099:                             }
 8100:                         }
 8101:                     }
 8102:                 }
 8103:                 if ($domconfig->{$role}{'font'} ne '') {
 8104:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 8105:                         $changes{$role}{'font'} = 1;
 8106:                     }
 8107:                 } else {
 8108:                     if ($confhash->{$role}{'font'}) {
 8109:                         $changes{$role}{'font'} = 1;
 8110:                     }
 8111:                 }
 8112:                 if ($role ne 'login') {
 8113:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 8114:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 8115:                             $changes{$role}{'fontmenu'} = 1;
 8116:                         }
 8117:                     } else {
 8118:                         if ($confhash->{$role}{'fontmenu'}) {
 8119:                             $changes{$role}{'fontmenu'} = 1;
 8120:                         }
 8121:                     }
 8122:                 }
 8123:                 foreach my $item (@bgs) {
 8124:                     if ($domconfig->{$role}{$item} ne '') {
 8125:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8126:                             $changes{$role}{'bgs'}{$item} = 1;
 8127:                         } 
 8128:                     } else {
 8129:                         if ($confhash->{$role}{$item}) {
 8130:                             $changes{$role}{'bgs'}{$item} = 1;
 8131:                         }
 8132:                     }
 8133:                 }
 8134:                 foreach my $item (@links) {
 8135:                     if ($domconfig->{$role}{$item} ne '') {
 8136:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8137:                             $changes{$role}{'links'}{$item} = 1;
 8138:                         }
 8139:                     } else {
 8140:                         if ($confhash->{$role}{$item}) {
 8141:                             $changes{$role}{'links'}{$item} = 1;
 8142:                         }
 8143:                     }
 8144:                 }
 8145:                 foreach my $item (@logintext) {
 8146:                     if ($domconfig->{$role}{$item} ne '') {
 8147:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8148:                             $changes{$role}{'logintext'}{$item} = 1;
 8149:                         }
 8150:                     } else {
 8151:                         if ($confhash->{$role}{$item}) {
 8152:                             $changes{$role}{'logintext'}{$item} = 1;
 8153:                         }
 8154:                     }
 8155:                 }
 8156:             } else {
 8157:                 &default_change_checker($role,\@images,\@links,\@bgs,
 8158:                                         \@logintext,$confhash,\%changes); 
 8159:             }
 8160:         } else {
 8161:             &default_change_checker($role,\@images,\@links,\@bgs,
 8162:                                     \@logintext,$confhash,\%changes); 
 8163:         }
 8164:     }
 8165:     return ($errors,%changes);
 8166: }
 8167: 
 8168: sub config_check {
 8169:     my ($dom,$confname,$servadm) = @_;
 8170:     my ($configuserok,$author_ok,$switchserver,%currroles);
 8171:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 8172:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 8173:                                                    $confname,$servadm);
 8174:     if ($configuserok eq 'ok') {
 8175:         $switchserver = &check_switchserver($dom,$confname);
 8176:         if ($switchserver eq '') {
 8177:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 8178:         }
 8179:     }
 8180:     return ($configuserok,$author_ok,$switchserver);
 8181: }
 8182: 
 8183: sub default_change_checker {
 8184:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 8185:     foreach my $item (@{$links}) {
 8186:         if ($confhash->{$role}{$item}) {
 8187:             $changes->{$role}{'links'}{$item} = 1;
 8188:         }
 8189:     }
 8190:     foreach my $item (@{$bgs}) {
 8191:         if ($confhash->{$role}{$item}) {
 8192:             $changes->{$role}{'bgs'}{$item} = 1;
 8193:         }
 8194:     }
 8195:     foreach my $item (@{$logintext}) {
 8196:         if ($confhash->{$role}{$item}) {
 8197:             $changes->{$role}{'logintext'}{$item} = 1;
 8198:         }
 8199:     }
 8200:     foreach my $img (@{$images}) {
 8201:         if ($env{'form.'.$role.'_del_'.$img}) {
 8202:             $confhash->{$role}{$img} = '';
 8203:             $changes->{$role}{'images'}{$img} = 1;
 8204:         }
 8205:         if ($role eq 'login') {
 8206:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 8207:                 $changes->{$role}{'showlogo'}{$img} = 1;
 8208:             }
 8209:         }
 8210:     }
 8211:     if ($confhash->{$role}{'font'}) {
 8212:         $changes->{$role}{'font'} = 1;
 8213:     }
 8214: }
 8215: 
 8216: sub display_colorchgs {
 8217:     my ($dom,$changes,$roles,$confhash) = @_;
 8218:     my (%choices,$resulttext);
 8219:     if (!grep(/^login$/,@{$roles})) {
 8220:         $resulttext = &mt('Changes made:').'<br />';
 8221:     }
 8222:     foreach my $role (@{$roles}) {
 8223:         if ($role eq 'login') {
 8224:             %choices = &login_choices();
 8225:         } else {
 8226:             %choices = &color_font_choices();
 8227:         }
 8228:         if (ref($changes->{$role}) eq 'HASH') {
 8229:             if ($role ne 'login') {
 8230:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 8231:             }
 8232:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 8233:                 if ($role ne 'login') {
 8234:                     $resulttext .= '<ul>';
 8235:                 }
 8236:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 8237:                     if ($role ne 'login') {
 8238:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 8239:                     }
 8240:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 8241:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 8242:                             if ($confhash->{$role}{$key}{$item}) {
 8243:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 8244:                             } else {
 8245:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 8246:                             }
 8247:                         } elsif ($confhash->{$role}{$item} eq '') {
 8248:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 8249:                         } else {
 8250:                             my $newitem = $confhash->{$role}{$item};
 8251:                             if ($key eq 'images') {
 8252:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 8253:                             }
 8254:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 8255:                         }
 8256:                     }
 8257:                     if ($role ne 'login') {
 8258:                         $resulttext .= '</ul></li>';
 8259:                     }
 8260:                 } else {
 8261:                     if ($confhash->{$role}{$key} eq '') {
 8262:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 8263:                     } else {
 8264:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 8265:                     }
 8266:                 }
 8267:                 if ($role ne 'login') {
 8268:                     $resulttext .= '</ul>';
 8269:                 }
 8270:             }
 8271:         }
 8272:     }
 8273:     return $resulttext;
 8274: }
 8275: 
 8276: sub thumb_dimensions {
 8277:     return ('200','50');
 8278: }
 8279: 
 8280: sub check_dimensions {
 8281:     my ($inputfile) = @_;
 8282:     my ($fullwidth,$fullheight);
 8283:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 8284:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 8285:             my $imageinfo = <PIPE>;
 8286:             if (!close(PIPE)) {
 8287:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 8288:             }
 8289:             chomp($imageinfo);
 8290:             my ($fullsize) = 
 8291:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 8292:             if ($fullsize) {
 8293:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 8294:             }
 8295:         }
 8296:     }
 8297:     return ($fullwidth,$fullheight);
 8298: }
 8299: 
 8300: sub check_configuser {
 8301:     my ($uhome,$dom,$confname,$servadm) = @_;
 8302:     my ($configuserok,%currroles);
 8303:     if ($uhome eq 'no_host') {
 8304:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 8305:         my $configpass = &LONCAPA::Enrollment::create_password();
 8306:         $configuserok = 
 8307:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 8308:                              $configpass,'','','','','',undef,$servadm);
 8309:     } else {
 8310:         $configuserok = 'ok';
 8311:         %currroles = 
 8312:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 8313:     }
 8314:     return ($configuserok,%currroles);
 8315: }
 8316: 
 8317: sub check_authorstatus {
 8318:     my ($dom,$confname,%currroles) = @_;
 8319:     my $author_ok;
 8320:     if (!$currroles{':'.$dom.':au'}) {
 8321:         my $start = time;
 8322:         my $end = 0;
 8323:         $author_ok = 
 8324:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 8325:                                         'au',$end,$start,'','','domconfig');
 8326:     } else {
 8327:         $author_ok = 'ok';
 8328:     }
 8329:     return $author_ok;
 8330: }
 8331: 
 8332: sub publishlogo {
 8333:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 8334:     my ($output,$fname,$logourl);
 8335:     if ($action eq 'upload') {
 8336:         $fname=$env{'form.'.$formname.'.filename'};
 8337:         chop($env{'form.'.$formname});
 8338:     } else {
 8339:         ($fname) = ($formname =~ /([^\/]+)$/);
 8340:     }
 8341:     if ($savefileas ne '') {
 8342:         $fname = $savefileas;
 8343:     }
 8344:     $fname=&Apache::lonnet::clean_filename($fname);
 8345: # See if there is anything left
 8346:     unless ($fname) { return ('error: no uploaded file'); }
 8347:     $fname="$subdir/$fname";
 8348:     my $docroot=$r->dir_config('lonDocRoot');
 8349:     my $filepath="$docroot/priv";
 8350:     my $relpath = "$dom/$confname";
 8351:     my ($fnamepath,$file,$fetchthumb);
 8352:     $file=$fname;
 8353:     if ($fname=~m|/|) {
 8354:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 8355:     }
 8356:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 8357:     my $count;
 8358:     for ($count=5;$count<=$#parts;$count++) {
 8359:         $filepath.="/$parts[$count]";
 8360:         if ((-e $filepath)!=1) {
 8361:             mkdir($filepath,02770);
 8362:         }
 8363:     }
 8364:     # Check for bad extension and disallow upload
 8365:     if ($file=~/\.(\w+)$/ &&
 8366:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 8367:         $output = 
 8368:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 8369:     } elsif ($file=~/\.(\w+)$/ &&
 8370:         !defined(&Apache::loncommon::fileembstyle($1))) {
 8371:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 8372:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 8373:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 8374:     } elsif (-d "$filepath/$file") {
 8375:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 8376:     } else {
 8377:         my $source = $filepath.'/'.$file;
 8378:         my $logfile;
 8379:         if (!open($logfile,">>$source".'.log')) {
 8380:             return (&mt('No write permission to Authoring Space'));
 8381:         }
 8382:         print $logfile
 8383: "\n================= Publish ".localtime()." ================\n".
 8384: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 8385: # Save the file
 8386:         if (!open(FH,'>'.$source)) {
 8387:             &Apache::lonnet::logthis('Failed to create '.$source);
 8388:             return (&mt('Failed to create file'));
 8389:         }
 8390:         if ($action eq 'upload') {
 8391:             if (!print FH ($env{'form.'.$formname})) {
 8392:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 8393:                 return (&mt('Failed to write file'));
 8394:             }
 8395:         } else {
 8396:             my $original = &Apache::lonnet::filelocation('',$formname);
 8397:             if(!copy($original,$source)) {
 8398:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 8399:                 return (&mt('Failed to write file'));
 8400:             }
 8401:         }
 8402:         close(FH);
 8403:         chmod(0660, $source); # Permissions to rw-rw---.
 8404: 
 8405:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 8406:         my $copyfile=$targetdir.'/'.$file;
 8407: 
 8408:         my @parts=split(/\//,$targetdir);
 8409:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 8410:         for (my $count=5;$count<=$#parts;$count++) {
 8411:             $path.="/$parts[$count]";
 8412:             if (!-e $path) {
 8413:                 print $logfile "\nCreating directory ".$path;
 8414:                 mkdir($path,02770);
 8415:             }
 8416:         }
 8417:         my $versionresult;
 8418:         if (-e $copyfile) {
 8419:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 8420:         } else {
 8421:             $versionresult = 'ok';
 8422:         }
 8423:         if ($versionresult eq 'ok') {
 8424:             if (copy($source,$copyfile)) {
 8425:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 8426:                 $output = 'ok';
 8427:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 8428:                 push(@{$modified_urls},[$copyfile,$source]);
 8429:                 my $metaoutput = 
 8430:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 8431:                 unless ($registered_cleanup) {
 8432:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 8433:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 8434:                     $registered_cleanup=1;
 8435:                 }
 8436:             } else {
 8437:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 8438:                 $output = &mt('Failed to copy file to RES space').", $!";
 8439:             }
 8440:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 8441:                 my $inputfile = $filepath.'/'.$file;
 8442:                 my $outfile = $filepath.'/'.'tn-'.$file;
 8443:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 8444:                 if ($fullwidth ne '' && $fullheight ne '') { 
 8445:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 8446:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 8447:                         system("convert -sample $thumbsize $inputfile $outfile");
 8448:                         chmod(0660, $filepath.'/tn-'.$file);
 8449:                         if (-e $outfile) {
 8450:                             my $copyfile=$targetdir.'/tn-'.$file;
 8451:                             if (copy($outfile,$copyfile)) {
 8452:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 8453:                                 my $thumb_metaoutput = 
 8454:                                     &write_metadata($dom,$confname,$formname,
 8455:                                                     $targetdir,'tn-'.$file,$logfile);
 8456:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 8457:                                 unless ($registered_cleanup) {
 8458:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 8459:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 8460:                                     $registered_cleanup=1;
 8461:                                 }
 8462:                             } else {
 8463:                                 print $logfile "\nUnable to write ".$copyfile.
 8464:                                                ':'.$!."\n";
 8465:                             }
 8466:                         }
 8467:                     }
 8468:                 }
 8469:             }
 8470:         } else {
 8471:             $output = $versionresult;
 8472:         }
 8473:     }
 8474:     return ($output,$logourl);
 8475: }
 8476: 
 8477: sub logo_versioning {
 8478:     my ($targetdir,$file,$logfile) = @_;
 8479:     my $target = $targetdir.'/'.$file;
 8480:     my ($maxversion,$fn,$extn,$output);
 8481:     $maxversion = 0;
 8482:     if ($file =~ /^(.+)\.(\w+)$/) {
 8483:         $fn=$1;
 8484:         $extn=$2;
 8485:     }
 8486:     opendir(DIR,$targetdir);
 8487:     while (my $filename=readdir(DIR)) {
 8488:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 8489:             $maxversion=($1>$maxversion)?$1:$maxversion;
 8490:         }
 8491:     }
 8492:     $maxversion++;
 8493:     print $logfile "\nCreating old version ".$maxversion."\n";
 8494:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 8495:     if (copy($target,$copyfile)) {
 8496:         print $logfile "Copied old target to ".$copyfile."\n";
 8497:         $copyfile=$copyfile.'.meta';
 8498:         if (copy($target.'.meta',$copyfile)) {
 8499:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 8500:             $output = 'ok';
 8501:         } else {
 8502:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 8503:             $output = &mt('Failed to copy old meta').", $!, ";
 8504:         }
 8505:     } else {
 8506:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 8507:         $output = &mt('Failed to copy old target').", $!, ";
 8508:     }
 8509:     return $output;
 8510: }
 8511: 
 8512: sub write_metadata {
 8513:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 8514:     my (%metadatafields,%metadatakeys,$output);
 8515:     $metadatafields{'title'}=$formname;
 8516:     $metadatafields{'creationdate'}=time;
 8517:     $metadatafields{'lastrevisiondate'}=time;
 8518:     $metadatafields{'copyright'}='public';
 8519:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 8520:                                          $env{'user.domain'};
 8521:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 8522:     $metadatafields{'domain'}=$dom;
 8523:     {
 8524:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 8525:         my $mfh;
 8526:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 8527:             foreach (sort(keys(%metadatafields))) {
 8528:                 unless ($_=~/\./) {
 8529:                     my $unikey=$_;
 8530:                     $unikey=~/^([A-Za-z]+)/;
 8531:                     my $tag=$1;
 8532:                     $tag=~tr/A-Z/a-z/;
 8533:                     print $mfh "\n\<$tag";
 8534:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 8535:                         my $value=$metadatafields{$unikey.'.'.$_};
 8536:                         $value=~s/\"/\'\'/g;
 8537:                         print $mfh ' '.$_.'="'.$value.'"';
 8538:                     }
 8539:                     print $mfh '>'.
 8540:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 8541:                             .'</'.$tag.'>';
 8542:                 }
 8543:             }
 8544:             $output = 'ok';
 8545:             print $logfile "\nWrote metadata";
 8546:             close($mfh);
 8547:         } else {
 8548:             print $logfile "\nFailed to open metadata file";
 8549:             $output = &mt('Could not write metadata');
 8550:         }
 8551:     }
 8552:     return $output;
 8553: }
 8554: 
 8555: sub notifysubscribed {
 8556:     foreach my $targetsource (@{$modified_urls}){
 8557:         next unless (ref($targetsource) eq 'ARRAY');
 8558:         my ($target,$source)=@{$targetsource};
 8559:         if ($source ne '') {
 8560:             if (open(my $logfh,'>>'.$source.'.log')) {
 8561:                 print $logfh "\nCleanup phase: Notifications\n";
 8562:                 my @subscribed=&subscribed_hosts($target);
 8563:                 foreach my $subhost (@subscribed) {
 8564:                     print $logfh "\nNotifying host ".$subhost.':';
 8565:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 8566:                     print $logfh $reply;
 8567:                 }
 8568:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 8569:                 foreach my $subhost (@subscribedmeta) {
 8570:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 8571:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 8572:                                                         $subhost);
 8573:                     print $logfh $reply;
 8574:                 }
 8575:                 print $logfh "\n============ Done ============\n";
 8576:                 close($logfh);
 8577:             }
 8578:         }
 8579:     }
 8580:     return OK;
 8581: }
 8582: 
 8583: sub subscribed_hosts {
 8584:     my ($target) = @_;
 8585:     my @subscribed;
 8586:     if (open(my $fh,"<$target.subscription")) {
 8587:         while (my $subline=<$fh>) {
 8588:             if ($subline =~ /^($match_lonid):/) {
 8589:                 my $host = $1;
 8590:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 8591:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 8592:                         push(@subscribed,$host);
 8593:                     }
 8594:                 }
 8595:             }
 8596:         }
 8597:     }
 8598:     return @subscribed;
 8599: }
 8600: 
 8601: sub check_switchserver {
 8602:     my ($dom,$confname) = @_;
 8603:     my ($allowed,$switchserver);
 8604:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 8605:     if ($home eq 'no_host') {
 8606:         $home = &Apache::lonnet::domain($dom,'primary');
 8607:     }
 8608:     my @ids=&Apache::lonnet::current_machine_ids();
 8609:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 8610:     if (!$allowed) {
 8611: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 8612:     }
 8613:     return $switchserver;
 8614: }
 8615: 
 8616: sub modify_quotas {
 8617:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 8618:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 8619:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 8620:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 8621:         $validationfieldsref);
 8622:     if ($action eq 'quotas') {
 8623:         $context = 'tools'; 
 8624:     } else {
 8625:         $context = $action;
 8626:     }
 8627:     if ($context eq 'requestcourses') {
 8628:         @usertools = ('official','unofficial','community','textbook');
 8629:         @options =('norequest','approval','validate','autolimit');
 8630:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 8631:         %titles = &courserequest_titles();
 8632:         $toolregexp = join('|',@usertools);
 8633:         %conditions = &courserequest_conditions();
 8634:         $confname = $dom.'-domainconfig';
 8635:         my $servadm = $r->dir_config('lonAdmEMail');
 8636:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 8637:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 8638:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 8639:     } elsif ($context eq 'requestauthor') {
 8640:         @usertools = ('author');
 8641:         %titles = &authorrequest_titles();
 8642:     } else {
 8643:         @usertools = ('aboutme','blog','webdav','portfolio');
 8644:         %titles = &tool_titles();
 8645:     }
 8646:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8647:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8648:     foreach my $key (keys(%env)) {
 8649:         if ($context eq 'requestcourses') {
 8650:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 8651:                 my $item = $1;
 8652:                 my $type = $2;
 8653:                 if ($type =~ /^limit_(.+)/) {
 8654:                     $limithash{$item}{$1} = $env{$key};
 8655:                 } else {
 8656:                     $confhash{$item}{$type} = $env{$key};
 8657:                 }
 8658:             }
 8659:         } elsif ($context eq 'requestauthor') {
 8660:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 8661:                 $confhash{$1} = $env{$key};
 8662:             }
 8663:         } else {
 8664:             if ($key =~ /^form\.quota_(.+)$/) {
 8665:                 $confhash{'defaultquota'}{$1} = $env{$key};
 8666:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 8667:                 $confhash{'authorquota'}{$1} = $env{$key};
 8668:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 8669:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 8670:             }
 8671:         }
 8672:     }
 8673:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 8674:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 8675:         @approvalnotify = sort(@approvalnotify);
 8676:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 8677:         my @crstypes = ('official','unofficial','community','textbook');
 8678:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 8679:         foreach my $type (@hasuniquecode) {
 8680:             if (grep(/^\Q$type\E$/,@crstypes)) {
 8681:                 $confhash{'uniquecode'}{$type} = 1;
 8682:             }
 8683:         }
 8684:         my (%newbook,%allpos);
 8685:         if ($context eq 'requestcourses') {
 8686:             foreach my $type ('textbooks','templates') {
 8687:                 @{$allpos{$type}} = (); 
 8688:                 my $invalid;
 8689:                 if ($type eq 'textbooks') {
 8690:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
 8691:                 } else {
 8692:                     $invalid = &mt('Invalid LON-CAPA course for template');
 8693:                 }
 8694:                 if ($env{'form.'.$type.'_addbook'}) {
 8695:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
 8696:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
 8697:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
 8698:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
 8699:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 8700:                         } else {
 8701:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
 8702:                             my $position = $env{'form.'.$type.'_addbook_pos'};
 8703:                             $position =~ s/\D+//g;
 8704:                             if ($position ne '') {
 8705:                                 $allpos{$type}[$position] = $newbook{$type};
 8706:                             }
 8707:                         }
 8708:                     } else {
 8709:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 8710:                     }
 8711:                 }
 8712:             } 
 8713:         }
 8714:         if (ref($domconfig{$action}) eq 'HASH') {
 8715:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 8716:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 8717:                     $changes{'notify'}{'approval'} = 1;
 8718:                 }
 8719:             } else {
 8720:                 if ($confhash{'notify'}{'approval'}) {
 8721:                     $changes{'notify'}{'approval'} = 1;
 8722:                 }
 8723:             }
 8724:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 8725:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 8726:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 8727:                         unless ($confhash{'uniquecode'}{$crstype}) {
 8728:                             $changes{'uniquecode'} = 1;
 8729:                         }
 8730:                     }
 8731:                     unless ($changes{'uniquecode'}) {
 8732:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 8733:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 8734:                                 $changes{'uniquecode'} = 1;
 8735:                             }
 8736:                         }
 8737:                     }
 8738:                } else {
 8739:                    $changes{'uniquecode'} = 1;
 8740:                }
 8741:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 8742:                 $changes{'uniquecode'} = 1;
 8743:             }
 8744:             if ($context eq 'requestcourses') {
 8745:                 foreach my $type ('textbooks','templates') {
 8746:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
 8747:                         my %deletions;
 8748:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
 8749:                         if (@todelete) {
 8750:                             map { $deletions{$_} = 1; } @todelete;
 8751:                         }
 8752:                         my %imgdeletions;
 8753:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
 8754:                         if (@todeleteimages) {
 8755:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
 8756:                         }
 8757:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
 8758:                         for (my $i=0; $i<=$maxnum; $i++) {
 8759:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
 8760:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
 8761:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
 8762:                                 if ($deletions{$key}) {
 8763:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
 8764:                                         #FIXME need to obsolete item in RES space
 8765:                                     }
 8766:                                     next;
 8767:                                 } else {
 8768:                                     my $newpos = $env{'form.'.$itemid};
 8769:                                     $newpos =~ s/\D+//g;
 8770:                                     foreach my $item ('subject','title','publisher','author') {
 8771:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 8772:                                                  ($type eq 'templates'));
 8773:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
 8774:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
 8775:                                             $changes{$type}{$key} = 1;
 8776:                                         }
 8777:                                     }
 8778:                                     $allpos{$type}[$newpos] = $key;
 8779:                                 }
 8780:                                 if ($imgdeletions{$key}) {
 8781:                                     $changes{$type}{$key} = 1;
 8782:                                     #FIXME need to obsolete item in RES space
 8783:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
 8784:                                     my ($cdom,$cnum) = split(/_/,$key);
 8785:                                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
 8786:                                         $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
 8787:                                     } else { 
 8788:                                         my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
 8789:                                                                                       $cdom,$cnum,$type,$configuserok,
 8790:                                                                                       $switchserver,$author_ok);
 8791:                                         if ($imgurl) {
 8792:                                             $confhash{$type}{$key}{'image'} = $imgurl;
 8793:                                             $changes{$type}{$key} = 1; 
 8794:                                         }
 8795:                                         if ($error) {
 8796:                                             &Apache::lonnet::logthis($error);
 8797:                                             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8798:                                         }
 8799:                                     } 
 8800:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
 8801:                                     $confhash{$type}{$key}{'image'} = 
 8802:                                         $domconfig{$action}{$type}{$key}{'image'};
 8803:                                 }
 8804:                             }
 8805:                         }
 8806:                     }
 8807:                 }
 8808:             }
 8809:         } else {
 8810:             if ($confhash{'notify'}{'approval'}) {
 8811:                 $changes{'notify'}{'approval'} = 1;
 8812:             }
 8813:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 8814:                 $changes{'uniquecode'} = 1;
 8815:             }
 8816:         }
 8817:         if ($context eq 'requestcourses') {
 8818:             foreach my $type ('textbooks','templates') {
 8819:                 if ($newbook{$type}) {
 8820:                     $changes{$type}{$newbook{$type}} = 1;
 8821:                     foreach my $item ('subject','title','publisher','author') {
 8822:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 8823:                                  ($type eq 'template'));
 8824:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
 8825:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
 8826:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
 8827:                         }
 8828:                     }
 8829:                     if ($type eq 'textbooks') {
 8830:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
 8831:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
 8832:                             my ($imageurl,$error) =
 8833:                                 &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
 8834:                                                         $configuserok,$switchserver,$author_ok);
 8835:                             if ($imageurl) {
 8836:                                 $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
 8837:                             }
 8838:                             if ($error) {
 8839:                                 &Apache::lonnet::logthis($error);
 8840:                                 $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8841:                             }
 8842:                         }
 8843:                     }
 8844:                 }
 8845:                 if (@{$allpos{$type}} > 0) {
 8846:                     my $idx = 0;
 8847:                     foreach my $item (@{$allpos{$type}}) {
 8848:                         if ($item ne '') {
 8849:                             $confhash{$type}{$item}{'order'} = $idx;
 8850:                             if (ref($domconfig{$action}) eq 'HASH') {
 8851:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
 8852:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
 8853:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
 8854:                                             $changes{$type}{$item} = 1;
 8855:                                         }
 8856:                                     }
 8857:                                 }
 8858:                             }
 8859:                             $idx ++;
 8860:                         }
 8861:                     }
 8862:                 }
 8863:             }
 8864:             if (ref($validationitemsref) eq 'ARRAY') {
 8865:                 foreach my $item (@{$validationitemsref}) {
 8866:                     if ($item eq 'fields') {
 8867:                         my @changed;
 8868:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 8869:                         if (@{$confhash{'validation'}{$item}} > 0) {
 8870:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 8871:                         }
 8872:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8873:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8874:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 8875:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 8876:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
 8877:                                 } else {
 8878:                                     @changed = @{$confhash{'validation'}{$item}};
 8879:                                 }
 8880:                             } else {
 8881:                                 @changed = @{$confhash{'validation'}{$item}};
 8882:                             }
 8883:                         } else {
 8884:                             @changed = @{$confhash{'validation'}{$item}};
 8885:                         }
 8886:                         if (@changed) {
 8887:                             if ($confhash{'validation'}{$item}) {
 8888:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 8889:                             } else {
 8890:                                 $changes{'validation'}{$item} = &mt('None');
 8891:                             }
 8892:                         }
 8893:                     } else {
 8894:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 8895:                         if ($item eq 'markup') {
 8896:                             if ($env{'form.requestcourses_validation_'.$item}) {
 8897:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 8898:                             }
 8899:                         }
 8900:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8901:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8902:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 8903:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 8904:                                 }
 8905:                             } else {
 8906:                                 if ($confhash{'validation'}{$item} ne '') {
 8907:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 8908:                                 }
 8909:                             }
 8910:                         } else {
 8911:                             if ($confhash{'validation'}{$item} ne '') {
 8912:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 8913:                             }
 8914:                         }
 8915:                     }
 8916:                 }
 8917:             }
 8918:             if ($env{'form.validationdc'}) {
 8919:                 my $newval = $env{'form.validationdc'};
 8920:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
 8921:                 if (exists($domcoords{$newval})) {
 8922:                     $confhash{'validation'}{'dc'} = $newval;
 8923:                 }
 8924:             }
 8925:             if (ref($confhash{'validation'}) eq 'HASH') {
 8926:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8927:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8928:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 8929:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 8930:                                 if ($confhash{'validation'}{'dc'} eq '') {
 8931:                                     $changes{'validation'}{'dc'} = &mt('None');
 8932:                                 } else {
 8933:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8934:                                 }
 8935:                             }
 8936:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
 8937:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8938:                         }
 8939:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 8940:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8941:                     }
 8942:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 8943:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8944:                 }
 8945:             } else {
 8946:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8947:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8948:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 8949:                             $changes{'validation'}{'dc'} = &mt('None');
 8950:                         }
 8951:                     }
 8952:                 }
 8953:             }
 8954:         }
 8955:     } else {
 8956:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 8957:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 8958:     }
 8959:     foreach my $item (@usertools) {
 8960:         foreach my $type (@{$types},'default','_LC_adv') {
 8961:             my $unset; 
 8962:             if ($context eq 'requestcourses') {
 8963:                 $unset = '0';
 8964:                 if ($type eq '_LC_adv') {
 8965:                     $unset = '';
 8966:                 }
 8967:                 if ($confhash{$item}{$type} eq 'autolimit') {
 8968:                     $confhash{$item}{$type} .= '=';
 8969:                     unless ($limithash{$item}{$type} =~ /\D/) {
 8970:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 8971:                     }
 8972:                 }
 8973:             } elsif ($context eq 'requestauthor') {
 8974:                 $unset = '0';
 8975:                 if ($type eq '_LC_adv') {
 8976:                     $unset = '';
 8977:                 }
 8978:             } else {
 8979:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 8980:                     $confhash{$item}{$type} = 1;
 8981:                 } else {
 8982:                     $confhash{$item}{$type} = 0;
 8983:                 }
 8984:             }
 8985:             if (ref($domconfig{$action}) eq 'HASH') {
 8986:                 if ($action eq 'requestauthor') {
 8987:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 8988:                         $changes{$type} = 1;
 8989:                     }
 8990:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 8991:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 8992:                         $changes{$item}{$type} = 1;
 8993:                     }
 8994:                 } else {
 8995:                     if ($context eq 'requestcourses') {
 8996:                         if ($confhash{$item}{$type} ne $unset) {
 8997:                             $changes{$item}{$type} = 1;
 8998:                         }
 8999:                     } else {
 9000:                         if (!$confhash{$item}{$type}) {
 9001:                             $changes{$item}{$type} = 1;
 9002:                         }
 9003:                     }
 9004:                 }
 9005:             } else {
 9006:                 if ($context eq 'requestcourses') {
 9007:                     if ($confhash{$item}{$type} ne $unset) {
 9008:                         $changes{$item}{$type} = 1;
 9009:                     }
 9010:                 } elsif ($context eq 'requestauthor') {
 9011:                     if ($confhash{$type} ne $unset) {
 9012:                         $changes{$type} = 1;
 9013:                     }
 9014:                 } else {
 9015:                     if (!$confhash{$item}{$type}) {
 9016:                         $changes{$item}{$type} = 1;
 9017:                     }
 9018:                 }
 9019:             }
 9020:         }
 9021:     }
 9022:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 9023:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 9024:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 9025:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 9026:                     if (exists($confhash{'defaultquota'}{$key})) {
 9027:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 9028:                             $changes{'defaultquota'}{$key} = 1;
 9029:                         }
 9030:                     } else {
 9031:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 9032:                     }
 9033:                 }
 9034:             } else {
 9035:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 9036:                     if (exists($confhash{'defaultquota'}{$key})) {
 9037:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 9038:                             $changes{'defaultquota'}{$key} = 1;
 9039:                         }
 9040:                     } else {
 9041:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 9042:                     }
 9043:                 }
 9044:             }
 9045:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 9046:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 9047:                     if (exists($confhash{'authorquota'}{$key})) {
 9048:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 9049:                             $changes{'authorquota'}{$key} = 1;
 9050:                         }
 9051:                     } else {
 9052:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 9053:                     }
 9054:                 }
 9055:             }
 9056:         }
 9057:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 9058:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 9059:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 9060:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 9061:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 9062:                             $changes{'defaultquota'}{$key} = 1;
 9063:                         }
 9064:                     } else {
 9065:                         if (!exists($domconfig{'quotas'}{$key})) {
 9066:                             $changes{'defaultquota'}{$key} = 1;
 9067:                         }
 9068:                     }
 9069:                 } else {
 9070:                     $changes{'defaultquota'}{$key} = 1;
 9071:                 }
 9072:             }
 9073:         }
 9074:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 9075:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 9076:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 9077:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 9078:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 9079:                             $changes{'authorquota'}{$key} = 1;
 9080:                         }
 9081:                     } else {
 9082:                         $changes{'authorquota'}{$key} = 1;
 9083:                     }
 9084:                 } else {
 9085:                     $changes{'authorquota'}{$key} = 1;
 9086:                 }
 9087:             }
 9088:         }
 9089:     }
 9090: 
 9091:     if ($context eq 'requestauthor') {
 9092:         $domdefaults{'requestauthor'} = \%confhash;
 9093:     } else {
 9094:         foreach my $key (keys(%confhash)) {
 9095:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
 9096:                 $domdefaults{$key} = $confhash{$key};
 9097:             }
 9098:         }
 9099:     }
 9100: 
 9101:     my %quotahash = (
 9102:                       $action => { %confhash }
 9103:                     );
 9104:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 9105:                                              $dom);
 9106:     if ($putresult eq 'ok') {
 9107:         if (keys(%changes) > 0) {
 9108:             my $cachetime = 24*60*60;
 9109:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9110:             if (ref($lastactref) eq 'HASH') {
 9111:                 $lastactref->{'domdefaults'} = 1;
 9112:             }
 9113:             $resulttext = &mt('Changes made:').'<ul>';
 9114:             unless (($context eq 'requestcourses') ||
 9115:                     ($context eq 'requestauthor')) {
 9116:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 9117:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 9118:                     foreach my $type (@{$types},'default') {
 9119:                         if (defined($changes{'defaultquota'}{$type})) {
 9120:                             my $typetitle = $usertypes->{$type};
 9121:                             if ($type eq 'default') {
 9122:                                 $typetitle = $othertitle;
 9123:                             }
 9124:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 9125:                         }
 9126:                     }
 9127:                     $resulttext .= '</ul></li>';
 9128:                 }
 9129:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 9130:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 9131:                     foreach my $type (@{$types},'default') {
 9132:                         if (defined($changes{'authorquota'}{$type})) {
 9133:                             my $typetitle = $usertypes->{$type};
 9134:                             if ($type eq 'default') {
 9135:                                 $typetitle = $othertitle;
 9136:                             }
 9137:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 9138:                         }
 9139:                     }
 9140:                     $resulttext .= '</ul></li>';
 9141:                 }
 9142:             }
 9143:             my %newenv;
 9144:             foreach my $item (@usertools) {
 9145:                 my (%haschgs,%inconf);
 9146:                 if ($context eq 'requestauthor') {
 9147:                     %haschgs = %changes;
 9148:                     %inconf = %confhash;
 9149:                 } else {
 9150:                     if (ref($changes{$item}) eq 'HASH') {
 9151:                         %haschgs = %{$changes{$item}};
 9152:                     }
 9153:                     if (ref($confhash{$item}) eq 'HASH') {
 9154:                         %inconf = %{$confhash{$item}};
 9155:                     }
 9156:                 }
 9157:                 if (keys(%haschgs) > 0) {
 9158:                     my $newacc = 
 9159:                         &Apache::lonnet::usertools_access($env{'user.name'},
 9160:                                                           $env{'user.domain'},
 9161:                                                           $item,'reload',$context);
 9162:                     if (($context eq 'requestcourses') ||
 9163:                         ($context eq 'requestauthor')) {
 9164:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 9165:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 9166:                         }
 9167:                     } else {
 9168:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 9169:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 9170:                         }
 9171:                     }
 9172:                     unless ($context eq 'requestauthor') {
 9173:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 9174:                     }
 9175:                     foreach my $type (@{$types},'default','_LC_adv') {
 9176:                         if ($haschgs{$type}) {
 9177:                             my $typetitle = $usertypes->{$type};
 9178:                             if ($type eq 'default') {
 9179:                                 $typetitle = $othertitle;
 9180:                             } elsif ($type eq '_LC_adv') {
 9181:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 9182:                             }
 9183:                             if ($inconf{$type}) {
 9184:                                 if ($context eq 'requestcourses') {
 9185:                                     my $cond;
 9186:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 9187:                                         if ($1 eq '') {
 9188:                                             $cond = &mt('(Automatic processing of any request).');
 9189:                                         } else {
 9190:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 9191:                                         }
 9192:                                     } else { 
 9193:                                         $cond = $conditions{$inconf{$type}};
 9194:                                     }
 9195:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 9196:                                 } elsif ($context eq 'requestauthor') {
 9197:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 9198:                                                              $titles{$inconf{$type}},$typetitle);
 9199: 
 9200:                                 } else {
 9201:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 9202:                                 }
 9203:                             } else {
 9204:                                 if ($type eq '_LC_adv') {
 9205:                                     if ($inconf{$type} eq '0') {
 9206:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 9207:                                     } else { 
 9208:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 9209:                                     }
 9210:                                 } else {
 9211:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 9212:                                 }
 9213:                             }
 9214:                         }
 9215:                     }
 9216:                     unless ($context eq 'requestauthor') {
 9217:                         $resulttext .= '</ul></li>';
 9218:                     }
 9219:                 }
 9220:             }
 9221:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 9222:                 if (ref($changes{'notify'}) eq 'HASH') {
 9223:                     if ($changes{'notify'}{'approval'}) {
 9224:                         if (ref($confhash{'notify'}) eq 'HASH') {
 9225:                             if ($confhash{'notify'}{'approval'}) {
 9226:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 9227:                             } else {
 9228:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 9229:                             }
 9230:                         }
 9231:                     }
 9232:                 }
 9233:             }
 9234:             if ($action eq 'requestcourses') {
 9235:                 my @offon = ('off','on');
 9236:                 if ($changes{'uniquecode'}) {
 9237:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 9238:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 9239:                         $resulttext .= '<li>'.
 9240:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 9241:                                        '</li>';
 9242:                     } else {
 9243:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 9244:                                        '</li>';
 9245:                     }
 9246:                 }
 9247:                 foreach my $type ('textbooks','templates') {
 9248:                     if (ref($changes{$type}) eq 'HASH') {
 9249:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
 9250:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
 9251:                             my %coursehash = &Apache::lonnet::coursedescription($key);
 9252:                             my $coursetitle = $coursehash{'description'};
 9253:                             my $position = $confhash{$type}{$key}{'order'} + 1;
 9254:                             $resulttext .= '<li>';
 9255:                             foreach my $item ('subject','title','publisher','author') {
 9256:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
 9257:                                          ($type eq 'templates'));
 9258:                                 my $name = $item.':';
 9259:                                 $name =~ s/^(\w)/\U$1/;
 9260:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
 9261:                             }
 9262:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 9263:                             if ($type eq 'textbooks') {
 9264:                                 if ($confhash{$type}{$key}{'image'}) {
 9265:                                     $resulttext .= ' '.&mt('Image: [_1]',
 9266:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
 9267:                                                    ' alt="Textbook cover" />').'<br />';
 9268:                                 }
 9269:                             }
 9270:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 9271:                         }
 9272:                         $resulttext .= '</ul></li>';
 9273:                     }
 9274:                 }
 9275:                 if (ref($changes{'validation'}) eq 'HASH') {
 9276:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 9277:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 9278:                         foreach my $item (@{$validationitemsref}) {
 9279:                             if (exists($changes{'validation'}{$item})) {
 9280:                                 if ($item eq 'markup') {
 9281:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9282:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 9283:                                 } else {
 9284:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9285:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 9286:                                 }
 9287:                             }
 9288:                         }
 9289:                         if (exists($changes{'validation'}{'dc'})) {
 9290:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 9291:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 9292:                         }
 9293:                     }
 9294:                 }
 9295:             }
 9296:             $resulttext .= '</ul>';
 9297:             if (keys(%newenv)) {
 9298:                 &Apache::lonnet::appenv(\%newenv);
 9299:             }
 9300:         } else {
 9301:             if ($context eq 'requestcourses') {
 9302:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 9303:             } elsif ($context eq 'requestauthor') {
 9304:                 $resulttext = &mt('No changes made to rights to request author space.');
 9305:             } else {
 9306:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 9307:             }
 9308:         }
 9309:     } else {
 9310:         $resulttext = '<span class="LC_error">'.
 9311: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9312:     }
 9313:     if ($errors) {
 9314:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 9315:                        '<ul>'.$errors.'</ul></p>';
 9316:     }
 9317:     return $resulttext;
 9318: }
 9319: 
 9320: sub process_textbook_image {
 9321:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
 9322:     my $filename = $env{'form.'.$caller.'.filename'};
 9323:     my ($error,$url);
 9324:     my ($width,$height) = (50,50);
 9325:     if ($configuserok eq 'ok') {
 9326:         if ($switchserver) {
 9327:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 9328:                          $switchserver);
 9329:         } elsif ($author_ok eq 'ok') {
 9330:             my ($result,$imageurl) =
 9331:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 9332:                              "$type/$cdom/$cnum/cover",$width,$height);
 9333:             if ($result eq 'ok') {
 9334:                 $url = $imageurl;
 9335:             } else {
 9336:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 9337:             }
 9338:         } else {
 9339:             $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);
 9340:         }
 9341:     } else {
 9342:         $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);
 9343:     }
 9344:     return ($url,$error);
 9345: }
 9346: 
 9347: sub modify_ltitools {
 9348:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 9349:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9350:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
 9351:     my $confname = $dom.'-domainconfig';
 9352:     my $servadm = $r->dir_config('lonAdmEMail');
 9353:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 9354:     my (%posslti,%possfield);
 9355:     my @courseroles = ('cc','in','ta','ep','st');
 9356:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 9357:     map { $posslti{$_} = 1; } @ltiroles;
 9358:     my @allfields = ('fullname','firstname','lastname','email','user','roles');
 9359:     map { $possfield{$_} = 1; } @allfields;
 9360:     my %lt = &ltitools_names();
 9361:     if ($env{'form.ltitools_add'}) {
 9362:         my $title = $env{'form.ltitools_add_title'};
 9363:         $title =~ s/(`)/'/g;
 9364:         ($newid,my $error) = &get_ltitools_id($dom,$title);
 9365:         if ($newid) {
 9366:             my $position = $env{'form.ltitools_add_pos'};
 9367:             $position =~ s/\D+//g;
 9368:             if ($position ne '') {
 9369:                 $allpos[$position] = $newid;
 9370:             }
 9371:             $changes{$newid} = 1;
 9372:             foreach my $item ('title','url','key','secret') {
 9373:                 $env{'form.ltitools_add_'.$item} =~ s/(`)/'/g;
 9374:                 if ($env{'form.ltitools_add_'.$item}) {
 9375:                     if (($item eq 'key') || ($item eq 'secret')) {
 9376:                         $encconfig{$newid}{$item} = $env{'form.ltitools_add_'.$item};
 9377:                     } else {
 9378:                         $confhash{$newid}{$item} = $env{'form.ltitools_add_'.$item};
 9379:                     }
 9380:                 }
 9381:             }
 9382:             if ($env{'form.ltitools_add_version'} eq 'LTI-1p0') {
 9383:                 $confhash{$newid}{'version'} = $env{'form.ltitools_add_version'};
 9384:             }
 9385:             if ($env{'form.ltitools_add_msgtype'} eq 'basic-lti-launch-request') {
 9386:                 $confhash{$newid}{'msgtype'} = $env{'form.ltitools_add_msgtype'};
 9387:             }
 9388:             foreach my $item ('width','height','linktext','explanation') {
 9389:                 $env{'form.ltitools_add_'.$item} =~ s/^\s+//;
 9390:                 $env{'form.ltitools_add_'.$item} =~ s/\s+$//;
 9391:                 if (($item eq 'width') || ($item eq 'height')) {
 9392:                     if ($env{'form.ltitools_add_'.$item} =~ /^\d+$/) {
 9393:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
 9394:                     }
 9395:                 } else {
 9396:                     if ($env{'form.ltitools_add_'.$item} ne '') {
 9397:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
 9398:                     }
 9399:                 }
 9400:             }
 9401:             if ($env{'form.ltitools_add_target'} eq 'window') {
 9402:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
 9403:             } elsif ($env{'form.ltitools_add_target'} eq 'tab') {
 9404:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
 9405:             } else {
 9406:                 $confhash{$newid}{'display'}{'target'} = 'iframe';
 9407:             }
 9408:             foreach my $item ('passback','roster') {
 9409:                 if ($env{'form.ltitools_add_'.$item}) {
 9410:                     $confhash{$newid}{$item} = 1;
 9411:                 }
 9412:             }
 9413:             if ($env{'form.ltitools_add_image.filename'} ne '') {
 9414:                 my ($imageurl,$error) =
 9415:                     &process_ltitools_image($r,$dom,$confname,'ltitools_add_image',$newid,
 9416:                                             $configuserok,$switchserver,$author_ok);
 9417:                 if ($imageurl) {
 9418:                     $confhash{$newid}{'image'} = $imageurl;
 9419:                 }
 9420:                 if ($error) {
 9421:                     &Apache::lonnet::logthis($error);
 9422:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9423:                 }
 9424:             }
 9425:             my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_add_fields');
 9426:             foreach my $field (@fields) {
 9427:                 if ($possfield{$field}) {
 9428:                     if ($field eq 'roles') {
 9429:                         foreach my $role (@courseroles) {
 9430:                             my $choice = $env{'form.ltitools_add_roles_'.$role};
 9431:                             if (($choice ne '') && ($posslti{$choice})) {
 9432:                                 $confhash{$newid}{'roles'}{$role} = $choice;
 9433:                                 if ($role eq 'cc') {
 9434:                                     $confhash{$newid}{'roles'}{'co'} = $choice;
 9435:                                 }
 9436:                             }
 9437:                         }
 9438:                     } else {
 9439:                         $confhash{$newid}{'fields'}{$field} = 1;
 9440:                     }
 9441:                 }
 9442:             }
 9443:             my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig');
 9444:             foreach my $item (@courseconfig) {
 9445:                 $confhash{$newid}{'crsconf'}{$item} = 1;
 9446:             }
 9447:             if ($env{'form.ltitools_add_custom'}) {
 9448:                 my $name = $env{'form.ltitools_add_custom_name'};
 9449:                 my $value = $env{'form.ltitools_add_custom_value'};
 9450:                 $value =~ s/(`)/'/g;
 9451:                 $name =~ s/(`)/'/g;
 9452:                 $confhash{$newid}{'custom'}{$name} = $value;
 9453:             }
 9454:         } else {
 9455:             my $error = &mt('Failed to acquire unique ID for new external tool');
 9456:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9457:         }
 9458:     }
 9459:     if (ref($domconfig{$action}) eq 'HASH') {
 9460:         my %deletions;
 9461:         my @todelete = &Apache::loncommon::get_env_multiple('form.ltitools_del');
 9462:         if (@todelete) {
 9463:             map { $deletions{$_} = 1; } @todelete;
 9464:         }
 9465:         my %customadds;
 9466:         my @newcustom = &Apache::loncommon::get_env_multiple('form.ltitools_customadd');
 9467:         if (@newcustom) {
 9468:             map { $customadds{$_} = 1; } @newcustom;
 9469:         }
 9470:         my %imgdeletions;
 9471:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.ltitools_image_del');
 9472:         if (@todeleteimages) {
 9473:             map { $imgdeletions{$_} = 1; } @todeleteimages;
 9474:         }
 9475:         my $maxnum = $env{'form.ltitools_maxnum'};
 9476:         for (my $i=0; $i<=$maxnum; $i++) {
 9477:             my $itemid = $env{'form.ltitools_id_'.$i};
 9478:             $itemid =~ s/\D+//g;
 9479:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
 9480:                 if ($deletions{$itemid}) {
 9481:                     if ($domconfig{$action}{$itemid}{'image'}) {
 9482:                         #FIXME need to obsolete item in RES space
 9483:                     }
 9484:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'title'};
 9485:                     next;
 9486:                 } else {
 9487:                     my $newpos = $env{'form.ltitools_'.$itemid};
 9488:                     $newpos =~ s/\D+//g;
 9489:                     foreach my $item ('title','url') {
 9490:                         $confhash{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
 9491:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
 9492:                             $changes{$itemid} = 1;
 9493:                         }
 9494:                     }
 9495:                     foreach my $item ('key','secret') {
 9496:                         $encconfig{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
 9497:                         if ($domconfig{$action}{$itemid}{$item} ne $encconfig{$itemid}{$item}) {
 9498:                             $changes{$itemid} = 1;
 9499:                         }
 9500:                     }
 9501:                     if ($env{'form.ltitools_version_'.$i} eq 'LTI-1p0') {
 9502:                         $confhash{$itemid}{'version'} = $env{'form.ltitools_version_'.$i};
 9503:                     }
 9504:                     if ($env{'form.ltitools_msgtype_'.$i} eq 'basic-lti-launch-request') {
 9505:                         $confhash{$itemid}{'msgtype'} = $env{'form.ltitools_msgtype_'.$i};
 9506:                     }
 9507:                     foreach my $size ('width','height') {
 9508:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/^\s+//;
 9509:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/\s+$//;
 9510:                         if ($env{'form.ltitools_'.$size.'_'.$i} =~ /^\d+$/) {
 9511:                             $confhash{$itemid}{'display'}{$size} = $env{'form.ltitools_'.$size.'_'.$i};
 9512:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9513:                                 if ($domconfig{$action}{$itemid}{'display'}{$size} ne $confhash{$itemid}{'display'}{$size}) {
 9514:                                     $changes{$itemid} = 1;
 9515:                                 }
 9516:                             } else {
 9517:                                 $changes{$itemid} = 1;
 9518:                             }
 9519:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9520:                             if ($domconfig{$action}{$itemid}{'display'}{$size} ne '') {
 9521:                                 $changes{$itemid} = 1;
 9522:                             }
 9523:                         }
 9524:                     }
 9525:                     foreach my $item ('linktext','explanation') {
 9526:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/^\s+//;
 9527:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/\s+$//;
 9528:                         if ($env{'form.ltitools_'.$item.'_'.$i} ne '') {
 9529:                             $confhash{$itemid}{'display'}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
 9530:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9531:                                 if ($domconfig{$action}{$itemid}{'display'}{$item} ne $confhash{$itemid}{'display'}{$item}) {
 9532:                                     $changes{$itemid} = 1;
 9533:                                 }
 9534:                             } else {
 9535:                                 $changes{$itemid} = 1;
 9536:                             }
 9537:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9538:                             if ($domconfig{$action}{$itemid}{'display'}{$item} ne '') {
 9539:                                 $changes{$itemid} = 1;
 9540:                             }
 9541:                         }
 9542:                     }
 9543:                     if ($env{'form.ltitools_target_'.$i} eq 'window') {
 9544:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
 9545:                     } elsif ($env{'form.ltitools_target_'.$i} eq 'tab') {
 9546:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
 9547:                     } else {
 9548:                         $confhash{$itemid}{'display'}{'target'} = 'iframe';
 9549:                     }
 9550:                     if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9551:                         if ($domconfig{$action}{$itemid}{'display'}{'target'} ne $confhash{$itemid}{'display'}{'target'}) {
 9552:                             $changes{$itemid} = 1;
 9553:                         }
 9554:                     } else {
 9555:                         $changes{$itemid} = 1;
 9556:                     }
 9557:                     foreach my $extra ('passback','roster') {
 9558:                         if ($env{'form.ltitools_'.$extra.'_'.$i}) {
 9559:                             $confhash{$itemid}{$extra} = 1;
 9560:                         }
 9561:                         if ($domconfig{$action}{$itemid}{$extra} ne $confhash{$itemid}{$extra}) {
 9562:                             $changes{$itemid} = 1;
 9563:                         }
 9564:                     }
 9565:                     my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig_'.$i);
 9566:                     foreach my $item ('label','title','target','linktext','explanation') {
 9567:                         if (grep(/^\Q$item\E$/,@courseconfig)) {
 9568:                             $confhash{$itemid}{'crsconf'}{$item} = 1;
 9569:                             if (ref($domconfig{$action}{$itemid}{'crsconf'}) eq 'HASH') {
 9570:                                 if ($domconfig{$action}{$itemid}{'crsconf'}{$item} ne $confhash{$itemid}{'crsconf'}{$item}) {
 9571:                                     $changes{$itemid} = 1;
 9572:                                 }
 9573:                             } else {
 9574:                                 $changes{$itemid} = 1;
 9575:                             }
 9576:                         }
 9577:                     }
 9578:                     my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_fields_'.$i);
 9579:                     foreach my $field (@fields) {
 9580:                         if ($possfield{$field}) {
 9581:                             if ($field eq 'roles') {
 9582:                                 foreach my $role (@courseroles) {
 9583:                                     my $choice = $env{'form.ltitools_roles_'.$role.'_'.$i};
 9584:                                     if (($choice ne '') && ($posslti{$choice})) {
 9585:                                         $confhash{$itemid}{'roles'}{$role} = $choice;
 9586:                                         if ($role eq 'cc') {
 9587:                                             $confhash{$itemid}{'roles'}{'co'} = $choice;
 9588:                                         }
 9589:                                     }
 9590:                                     if (ref($domconfig{$action}{$itemid}{'roles'}) eq 'HASH') {
 9591:                                         if ($domconfig{$action}{$itemid}{'roles'}{$role} ne $confhash{$itemid}{'roles'}{$role}) {
 9592:                                             $changes{$itemid} = 1;
 9593:                                         }
 9594:                                     } elsif ($confhash{$itemid}{'roles'}{$role}) {
 9595:                                         $changes{$itemid} = 1;
 9596:                                     }
 9597:                                 }
 9598:                             } else {
 9599:                                 $confhash{$itemid}{'fields'}{$field} = 1;
 9600:                                 if (ref($domconfig{$action}{$itemid}{'fields'}) eq 'HASH') {
 9601:                                     if ($domconfig{$action}{$itemid}{'fields'}{$field} ne $confhash{$itemid}{'fields'}{$field}) {
 9602:                                         $changes{$itemid} = 1;
 9603:                                     }
 9604:                                 } else {
 9605:                                     $changes{$itemid} = 1;
 9606:                                 }
 9607:                             }
 9608:                         }
 9609:                     }
 9610:                     $allpos[$newpos] = $itemid;
 9611:                 }
 9612:                 if ($imgdeletions{$itemid}) {
 9613:                     $changes{$itemid} = 1;
 9614:                     #FIXME need to obsolete item in RES space
 9615:                 } elsif ($env{'form.ltitools_image_'.$i.'.filename'}) {
 9616:                     my ($imgurl,$error) = &process_ltitools_image($r,$dom,$confname,'ltitools_image_'.$i,
 9617:                                                                  $itemid,$configuserok,$switchserver,
 9618:                                                                  $author_ok);
 9619:                     if ($imgurl) {
 9620:                         $confhash{$itemid}{'image'} = $imgurl;
 9621:                         $changes{$itemid} = 1;
 9622:                     }
 9623:                     if ($error) {
 9624:                         &Apache::lonnet::logthis($error);
 9625:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9626:                     }
 9627:                 } elsif ($domconfig{$action}{$itemid}{'image'}) {
 9628:                     $confhash{$itemid}{'image'} =
 9629:                        $domconfig{$action}{$itemid}{'image'};
 9630:                 }
 9631:                 if ($customadds{$i}) {
 9632:                     my $name = $env{'form.ltitools_custom_name_'.$i};
 9633:                     $name =~ s/(`)/'/g;
 9634:                     $name =~ s/^\s+//;
 9635:                     $name =~ s/\s+$//;
 9636:                     my $value = $env{'form.ltitools_custom_value_'.$i};
 9637:                     $value =~ s/(`)/'/g;
 9638:                     $value =~ s/^\s+//;
 9639:                     $value =~ s/\s+$//;
 9640:                     if ($name ne '') {
 9641:                         $confhash{$itemid}{'custom'}{$name} = $value;
 9642:                         $changes{$itemid} = 1;
 9643:                     }
 9644:                 }
 9645:                 my %customdels;
 9646:                 my @customdeletions = &Apache::loncommon::get_env_multiple('form.ltitools_customdel_'.$i);
 9647:                 if (@customdeletions) {
 9648:                     $changes{$itemid} = 1;
 9649:                 }
 9650:                 map { $customdels{$_} = 1; } @customdeletions;
 9651:                 if (ref($domconfig{$action}{$itemid}{'custom'}) eq 'HASH') {
 9652:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}{'custom'}})) {
 9653:                         unless ($customdels{$key}) {
 9654:                             if ($env{'form.ltitools_customval_'.$key.'_'.$i} ne '') {
 9655:                                 $confhash{$itemid}{'custom'}{$key} = $env{'form.ltitools_customval_'.$key.'_'.$i};
 9656:                             }
 9657:                             if ($domconfig{$action}{$itemid}{'custom'}{$key} ne $env{'form.ltitools_customval_'.$key.'_'.$i}) {
 9658:                                 $changes{$itemid} = 1;
 9659:                             }
 9660:                         }
 9661:                     }
 9662:                 }
 9663:                 unless ($changes{$itemid}) {
 9664:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}})) {
 9665:                         if (ref($domconfig{$action}{$itemid}{$key}) eq 'HASH') {
 9666:                             if (ref($confhash{$itemid}{$key}) eq 'HASH') {
 9667:                                 foreach my $innerkey (keys(%{$domconfig{$action}{$itemid}{$key}})) {
 9668:                                     unless (exists($confhash{$itemid}{$key}{$innerkey})) {
 9669:                                         $changes{$itemid} = 1;
 9670:                                         last;
 9671:                                     }
 9672:                                 }
 9673:                             } elsif (keys(%{$domconfig{$action}{$itemid}{$key}}) > 0) {
 9674:                                 $changes{$itemid} = 1;
 9675:                             }
 9676:                         }
 9677:                         last if ($changes{$itemid});
 9678:                     }
 9679:                 }
 9680:             }
 9681:         }
 9682:     }
 9683:     if (@allpos > 0) {
 9684:         my $idx = 0;
 9685:         foreach my $itemid (@allpos) {
 9686:             if ($itemid ne '') {
 9687:                 $confhash{$itemid}{'order'} = $idx;
 9688:                 if (ref($domconfig{$action}) eq 'HASH') {
 9689:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
 9690:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
 9691:                             $changes{$itemid} = 1;
 9692:                         }
 9693:                     }
 9694:                 }
 9695:                 $idx ++;
 9696:             }
 9697:         }
 9698:     }
 9699:     my %ltitoolshash = (
 9700:                           $action => { %confhash }
 9701:                        );
 9702:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,
 9703:                                              $dom);
 9704:     if ($putresult eq 'ok') {
 9705:         my %ltienchash = (
 9706:                              $action => { %encconfig }
 9707:                          );
 9708:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom);
 9709:         if (keys(%changes) > 0) {
 9710:             my $cachetime = 24*60*60;
 9711:             my %ltiall = %confhash;
 9712:             foreach my $id (keys(%ltiall)) {
 9713:                 if (ref($encconfig{$id}) eq 'HASH') {
 9714:                     foreach my $item ('key','secret') {
 9715:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
 9716:                     }
 9717:                 }
 9718:             }
 9719:             &Apache::lonnet::do_cache_new('ltitools',$dom,\%ltiall,$cachetime);
 9720:             if (ref($lastactref) eq 'HASH') {
 9721:                 $lastactref->{'ltitools'} = 1;
 9722:             }
 9723:             $resulttext = &mt('Changes made:').'<ul>';
 9724:             my %bynum;
 9725:             foreach my $itemid (sort(keys(%changes))) {
 9726:                 my $position = $confhash{$itemid}{'order'};
 9727:                 $bynum{$position} = $itemid;
 9728:             }
 9729:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
 9730:                 my $itemid = $bynum{$pos};
 9731:                 if (ref($confhash{$itemid}) ne 'HASH') {
 9732:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
 9733:                 } else {
 9734:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'title'}.'</b>';
 9735:                     if ($confhash{$itemid}{'image'}) {
 9736:                         $resulttext .= '&nbsp;'.
 9737:                                        '<img src="'.$confhash{$itemid}{'image'}.'"'.
 9738:                                        ' alt="'.&mt('Tool Provider icon').'" />';
 9739:                     }
 9740:                     $resulttext .= '</li><ul>';
 9741:                     my $position = $pos + 1;
 9742:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
 9743:                     foreach my $item ('version','msgtype','url') {
 9744:                         if ($confhash{$itemid}{$item} ne '') {
 9745:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
 9746:                         }
 9747:                     }
 9748:                     if ($encconfig{$itemid}{'key'} ne '') {
 9749:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
 9750:                     }
 9751:                     if ($encconfig{$itemid}{'secret'} ne '') {
 9752:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
 9753:                         my $num = length($encconfig{$itemid}{'secret'});
 9754:                         $resulttext .= ('*'x$num).'</li>';
 9755:                     }
 9756:                     $resulttext .= '<li>'.&mt('Configurable in course:');
 9757:                     my @possconfig = ('label','title','target','linktext','explanation');
 9758:                     my $numconfig = 0;
 9759:                     if (ref($confhash{$itemid}{'crsconf'}) eq 'HASH') {
 9760:                         foreach my $item (@possconfig) {
 9761:                             if ($confhash{$itemid}{'crsconf'}{$item}) {
 9762:                                 $numconfig ++;
 9763:                                 $resulttext .= ' "'.$lt{'crs'.$item}.'"';
 9764:                             }
 9765:                         }
 9766:                     }
 9767:                     if (!$numconfig) {
 9768:                         $resulttext .= &mt('None');
 9769:                     }
 9770:                     $resulttext .= '</li>';
 9771:                     foreach my $item ('passback','roster') {
 9772:                         $resulttext .= '<li>'.$lt{$item}.'&nbsp;';
 9773:                         if ($confhash{$itemid}{$item}) {
 9774:                             $resulttext .= &mt('Yes');
 9775:                         } else {
 9776:                             $resulttext .= &mt('No');
 9777:                         }
 9778:                         $resulttext .= '</li>';
 9779:                     }
 9780:                     if (ref($confhash{$itemid}{'display'}) eq 'HASH') {
 9781:                         my $displaylist;
 9782:                         if ($confhash{$itemid}{'display'}{'target'}) {
 9783:                             $displaylist = &mt('Display target').':&nbsp;'.
 9784:                                            $confhash{$itemid}{'display'}{'target'}.',';
 9785:                         }
 9786:                         foreach my $size ('width','height') {
 9787:                             if ($confhash{$itemid}{'display'}{$size}) {
 9788:                                 $displaylist .= ('&nbsp;'x2).$lt{$size}.':&nbsp;'.
 9789:                                                 $confhash{$itemid}{'display'}{$size}.',';
 9790:                             }
 9791:                         }
 9792:                         if ($displaylist) {
 9793:                             $displaylist =~ s/,$//;
 9794:                             $resulttext .= '<li>'.$displaylist.'</li>';
 9795:                         }
 9796:                         foreach my $item ('linktext','explanation') {
 9797:                             if ($confhash{$itemid}{'display'}{$item}) {
 9798:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{'display'}{$item}.'</li>';
 9799:                             }
 9800:                         }
 9801:                     }
 9802:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
 9803:                         my $fieldlist;
 9804:                         foreach my $field (@allfields) {
 9805:                             if ($confhash{$itemid}{'fields'}{$field}) {
 9806:                                 $fieldlist .= ('&nbsp;'x2).$lt{$field}.',';
 9807:                             }
 9808:                         }
 9809:                         if ($fieldlist) {
 9810:                             $fieldlist =~ s/,$//;
 9811:                             $resulttext .= '<li>'.&mt('Data sent').':'.$fieldlist.'</li>';
 9812:                         }
 9813:                     }
 9814:                     if (ref($confhash{$itemid}{'roles'}) eq 'HASH') {
 9815:                         my $rolemaps;
 9816:                         foreach my $role (@courseroles) {
 9817:                             if ($confhash{$itemid}{'roles'}{$role}) {
 9818:                                 $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
 9819:                                              $confhash{$itemid}{'roles'}{$role}.',';
 9820:                             }
 9821:                         }
 9822:                         if ($rolemaps) {
 9823:                             $rolemaps =~ s/,$//;
 9824:                             $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
 9825:                         }
 9826:                     }
 9827:                     if (ref($confhash{$itemid}{'custom'}) eq 'HASH') {
 9828:                         my $customlist;
 9829:                         if (keys(%{$confhash{$itemid}{'custom'}})) {
 9830:                             foreach my $key (sort(keys(%{$confhash{$itemid}{'custom'}}))) {
 9831:                                 $customlist .= $key.':'.$confhash{$itemid}{'custom'}{$key}.('&nbsp;'x2);
 9832:                             }
 9833:                         }
 9834:                         if ($customlist) {
 9835:                             $resulttext .= '<li>'.&mt('Custom items').':'.$customlist.'</li>';
 9836:                         }
 9837:                     }
 9838:                     $resulttext .= '</ul></li>';
 9839:                 }
 9840:             }
 9841:             $resulttext .= '</ul>';
 9842:         } else {
 9843:             $resulttext = &mt('No changes made.');
 9844:         }
 9845:     } else {
 9846:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
 9847:     }
 9848:     if ($errors) {
 9849:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 9850:                        $errors.'</ul>';
 9851:     }
 9852:     return $resulttext;
 9853: }
 9854: 
 9855: sub process_ltitools_image {
 9856:     my ($r,$dom,$confname,$caller,$itemid,$configuserok,$switchserver,$author_ok) = @_;
 9857:     my $filename = $env{'form.'.$caller.'.filename'};
 9858:     my ($error,$url);
 9859:     my ($width,$height) = (21,21);
 9860:     if ($configuserok eq 'ok') {
 9861:         if ($switchserver) {
 9862:             $error = &mt('Upload of Tool Provider (LTI) icon is not permitted to this server: [_1]',
 9863:                          $switchserver);
 9864:         } elsif ($author_ok eq 'ok') {
 9865:             my ($result,$imageurl,$madethumb) =
 9866:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 9867:                              "ltitools/$itemid/icon",$width,$height);
 9868:             if ($result eq 'ok') {
 9869:                 if ($madethumb) {
 9870:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
 9871:                     my $imagethumb = "$path/tn-".$imagefile;
 9872:                     $url = $imagethumb;
 9873:                 } else {
 9874:                     $url = $imageurl;
 9875:                 }
 9876:             } else {
 9877:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 9878:             }
 9879:         } else {
 9880:             $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);
 9881:         }
 9882:     } else {
 9883:         $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);
 9884:     }
 9885:     return ($url,$error);
 9886: }
 9887: 
 9888: sub get_ltitools_id {
 9889:     my ($cdom,$title) = @_;
 9890:     # get lock on ltitools db
 9891:     my $lockhash = {
 9892:                       lock => $env{'user.name'}.
 9893:                               ':'.$env{'user.domain'},
 9894:                    };
 9895:     my $tries = 0;
 9896:     my $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
 9897:     my ($id,$error);
 9898: 
 9899:     while (($gotlock ne 'ok') && ($tries<10)) {
 9900:         $tries ++;
 9901:         sleep (0.1);
 9902:         $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
 9903:     }
 9904:     if ($gotlock eq 'ok') {
 9905:         my %currids = &Apache::lonnet::dump_dom('ltitools',$cdom);
 9906:         if ($currids{'lock'}) {
 9907:             delete($currids{'lock'});
 9908:             if (keys(%currids)) {
 9909:                 my @curr = sort { $a <=> $b } keys(%currids);
 9910:                 if ($curr[-1] =~ /^\d+$/) {
 9911:                     $id = 1 + $curr[-1];
 9912:                 }
 9913:             } else {
 9914:                 $id = 1;
 9915:             }
 9916:             if ($id) {
 9917:                 unless (&Apache::lonnet::newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
 9918:                     $error = 'nostore';
 9919:                 }
 9920:             } else {
 9921:                 $error = 'nonumber';
 9922:             }
 9923:         }
 9924:         my $dellockoutcome = &Apache::lonnet::del_dom('ltitools',['lock'],$cdom);
 9925:     } else {
 9926:         $error = 'nolock';
 9927:     }
 9928:     return ($id,$error);
 9929: }
 9930: 
 9931: sub modify_autoenroll {
 9932:     my ($dom,$lastactref,%domconfig) = @_;
 9933:     my ($resulttext,%changes);
 9934:     my %currautoenroll;
 9935:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 9936:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 9937:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 9938:         }
 9939:     }
 9940:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 9941:     my %title = ( run => 'Auto-enrollment active',
 9942:                   sender => 'Sender for notification messages',
 9943:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
 9944:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
 9945:     my @offon = ('off','on');
 9946:     my $sender_uname = $env{'form.sender_uname'};
 9947:     my $sender_domain = $env{'form.sender_domain'};
 9948:     if ($sender_domain eq '') {
 9949:         $sender_uname = '';
 9950:     } elsif ($sender_uname eq '') {
 9951:         $sender_domain = '';
 9952:     }
 9953:     my $coowners = $env{'form.autoassign_coowners'};
 9954:     my $failsafe = $env{'form.autoenroll_failsafe'};
 9955:     $failsafe =~ s{^\s+|\s+$}{}g;
 9956:     if ($failsafe =~ /\D/) {
 9957:         undef($failsafe);
 9958:     }
 9959:     my %autoenrollhash =  (
 9960:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 9961:                                        'sender_uname' => $sender_uname,
 9962:                                        'sender_domain' => $sender_domain,
 9963:                                        'co-owners' => $coowners,
 9964:                                        'autofailsafe' => $failsafe,
 9965:                                 }
 9966:                      );
 9967:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 9968:                                              $dom);
 9969:     if ($putresult eq 'ok') {
 9970:         if (exists($currautoenroll{'run'})) {
 9971:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 9972:                  $changes{'run'} = 1;
 9973:              }
 9974:         } elsif ($autorun) {
 9975:             if ($env{'form.autoenroll_run'} ne '1') {
 9976:                  $changes{'run'} = 1;
 9977:             }
 9978:         }
 9979:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 9980:             $changes{'sender'} = 1;
 9981:         }
 9982:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 9983:             $changes{'sender'} = 1;
 9984:         }
 9985:         if ($currautoenroll{'co-owners'} ne '') {
 9986:             if ($currautoenroll{'co-owners'} ne $coowners) {
 9987:                 $changes{'coowners'} = 1;
 9988:             }
 9989:         } elsif ($coowners) {
 9990:             $changes{'coowners'} = 1;
 9991:         }
 9992:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
 9993:             $changes{'autofailsafe'} = 1;
 9994:         }
 9995:         if (keys(%changes) > 0) {
 9996:             $resulttext = &mt('Changes made:').'<ul>';
 9997:             if ($changes{'run'}) {
 9998:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 9999:             }
10000:             if ($changes{'sender'}) {
10001:                 if ($sender_uname eq '' || $sender_domain eq '') {
10002:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
10003:                 } else {
10004:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
10005:                 }
10006:             }
10007:             if ($changes{'coowners'}) {
10008:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
10009:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
10010:                 if (ref($lastactref) eq 'HASH') {
10011:                     $lastactref->{'domainconfig'} = 1;
10012:                 }
10013:             }
10014:             if ($changes{'autofailsafe'}) {
10015:                 if ($failsafe ne '') {
10016:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$failsafe).'</li>';
10017:                 } else {
10018:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section: deleted');
10019:                 }
10020:                 &Apache::lonnet::get_domain_defaults($dom,1);
10021:                 if (ref($lastactref) eq 'HASH') {
10022:                     $lastactref->{'domdefaults'} = 1;
10023:                 }
10024:             }
10025:             $resulttext .= '</ul>';
10026:         } else {
10027:             $resulttext = &mt('No changes made to auto-enrollment settings');
10028:         }
10029:     } else {
10030:         $resulttext = '<span class="LC_error">'.
10031: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
10032:     }
10033:     return $resulttext;
10034: }
10035: 
10036: sub modify_autoupdate {
10037:     my ($dom,%domconfig) = @_;
10038:     my ($resulttext,%currautoupdate,%fields,%changes);
10039:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
10040:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
10041:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
10042:         }
10043:     }
10044:     my @offon = ('off','on');
10045:     my %title = &Apache::lonlocal::texthash (
10046:                    run => 'Auto-update:',
10047:                    classlists => 'Updates to user information in classlists?'
10048:                 );
10049:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10050:     my %fieldtitles = &Apache::lonlocal::texthash (
10051:                         id => 'Student/Employee ID',
10052:                         permanentemail => 'E-mail address',
10053:                         lastname => 'Last Name',
10054:                         firstname => 'First Name',
10055:                         middlename => 'Middle Name',
10056:                         generation => 'Generation',
10057:                       );
10058:     $othertitle = &mt('All users');
10059:     if (keys(%{$usertypes}) >  0) {
10060:         $othertitle = &mt('Other users');
10061:     }
10062:     foreach my $key (keys(%env)) {
10063:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
10064:             my ($usertype,$item) = ($1,$2);
10065:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
10066:                 if ($usertype eq 'default') {   
10067:                     push(@{$fields{$1}},$2);
10068:                 } elsif (ref($types) eq 'ARRAY') {
10069:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
10070:                         push(@{$fields{$1}},$2);
10071:                     }
10072:                 }
10073:             }
10074:         }
10075:     }
10076:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
10077:     @lockablenames = sort(@lockablenames);
10078:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
10079:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
10080:         if (@changed) {
10081:             $changes{'lockablenames'} = 1;
10082:         }
10083:     } else {
10084:         if (@lockablenames) {
10085:             $changes{'lockablenames'} = 1;
10086:         }
10087:     }
10088:     my %updatehash = (
10089:                       autoupdate => { run => $env{'form.autoupdate_run'},
10090:                                       classlists => $env{'form.classlists'},
10091:                                       fields => {%fields},
10092:                                       lockablenames => \@lockablenames,
10093:                                     }
10094:                      );
10095:     foreach my $key (keys(%currautoupdate)) {
10096:         if (($key eq 'run') || ($key eq 'classlists')) {
10097:             if (exists($updatehash{autoupdate}{$key})) {
10098:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
10099:                     $changes{$key} = 1;
10100:                 }
10101:             }
10102:         } elsif ($key eq 'fields') {
10103:             if (ref($currautoupdate{$key}) eq 'HASH') {
10104:                 foreach my $item (@{$types},'default') {
10105:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
10106:                         my $change = 0;
10107:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
10108:                             if (!exists($fields{$item})) {
10109:                                 $change = 1;
10110:                                 last;
10111:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
10112:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
10113:                                     $change = 1;
10114:                                     last;
10115:                                 }
10116:                             }
10117:                         }
10118:                         if ($change) {
10119:                             push(@{$changes{$key}},$item);
10120:                         }
10121:                     } 
10122:                 }
10123:             }
10124:         } elsif ($key eq 'lockablenames') {
10125:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
10126:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
10127:                 if (@changed) {
10128:                     $changes{'lockablenames'} = 1;
10129:                 }
10130:             } else {
10131:                 if (@lockablenames) {
10132:                     $changes{'lockablenames'} = 1;
10133:                 }
10134:             }
10135:         }
10136:     }
10137:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
10138:         if (@lockablenames) {
10139:             $changes{'lockablenames'} = 1;
10140:         }
10141:     }
10142:     foreach my $item (@{$types},'default') {
10143:         if (defined($fields{$item})) {
10144:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
10145:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
10146:                     my $change = 0;
10147:                     if (ref($fields{$item}) eq 'ARRAY') {
10148:                         foreach my $type (@{$fields{$item}}) {
10149:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
10150:                                 $change = 1;
10151:                                 last;
10152:                             }
10153:                         }
10154:                     }
10155:                     if ($change) {
10156:                         push(@{$changes{'fields'}},$item);
10157:                     }
10158:                 } else {
10159:                     push(@{$changes{'fields'}},$item);
10160:                 }
10161:             } else {
10162:                 push(@{$changes{'fields'}},$item);
10163:             }
10164:         }
10165:     }
10166:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
10167:                                              $dom);
10168:     if ($putresult eq 'ok') {
10169:         if (keys(%changes) > 0) {
10170:             $resulttext = &mt('Changes made:').'<ul>';
10171:             foreach my $key (sort(keys(%changes))) {
10172:                 if ($key eq 'lockablenames') {
10173:                     $resulttext .= '<li>';
10174:                     if (@lockablenames) {
10175:                         $usertypes->{'default'} = $othertitle;
10176:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
10177:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
10178:                     } else {
10179:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
10180:                     }
10181:                     $resulttext .= '</li>';
10182:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
10183:                     foreach my $item (@{$changes{$key}}) {
10184:                         my @newvalues;
10185:                         foreach my $type (@{$fields{$item}}) {
10186:                             push(@newvalues,$fieldtitles{$type});
10187:                         }
10188:                         my $newvaluestr;
10189:                         if (@newvalues > 0) {
10190:                             $newvaluestr = join(', ',@newvalues);
10191:                         } else {
10192:                             $newvaluestr = &mt('none');
10193:                         }
10194:                         if ($item eq 'default') {
10195:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
10196:                         } else {
10197:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
10198:                         }
10199:                     }
10200:                 } else {
10201:                     my $newvalue;
10202:                     if ($key eq 'run') {
10203:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
10204:                     } else {
10205:                         $newvalue = $offon[$env{'form.'.$key}];
10206:                     }
10207:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
10208:                 }
10209:             }
10210:             $resulttext .= '</ul>';
10211:         } else {
10212:             $resulttext = &mt('No changes made to autoupdates');
10213:         }
10214:     } else {
10215:         $resulttext = '<span class="LC_error">'.
10216: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
10217:     }
10218:     return $resulttext;
10219: }
10220: 
10221: sub modify_autocreate {
10222:     my ($dom,%domconfig) = @_;
10223:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
10224:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
10225:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
10226:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
10227:         }
10228:     }
10229:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
10230:                  req => 'Auto-creation of validated requests for official courses',
10231:                  xmldc => 'Identity of course creator of courses from XML files',
10232:                );
10233:     my @types = ('xml','req');
10234:     foreach my $item (@types) {
10235:         $newvals{$item} = $env{'form.autocreate_'.$item};
10236:         $newvals{$item} =~ s/\D//g;
10237:         $newvals{$item} = 0 if ($newvals{$item} eq '');
10238:     }
10239:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
10240:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
10241:     unless (exists($domcoords{$newvals{'xmldc'}})) {
10242:         $newvals{'xmldc'} = '';
10243:     } 
10244:     %autocreatehash =  (
10245:                         autocreate => { xml => $newvals{'xml'},
10246:                                         req => $newvals{'req'},
10247:                                       }
10248:                        );
10249:     if ($newvals{'xmldc'} ne '') {
10250:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
10251:     }
10252:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
10253:                                              $dom);
10254:     if ($putresult eq 'ok') {
10255:         my @items = @types;
10256:         if ($newvals{'xml'}) {
10257:             push(@items,'xmldc');
10258:         }
10259:         foreach my $item (@items) {
10260:             if (exists($currautocreate{$item})) {
10261:                 if ($currautocreate{$item} ne $newvals{$item}) {
10262:                     $changes{$item} = 1;
10263:                 }
10264:             } elsif ($newvals{$item}) {
10265:                 $changes{$item} = 1;
10266:             }
10267:         }
10268:         if (keys(%changes) > 0) {
10269:             my @offon = ('off','on'); 
10270:             $resulttext = &mt('Changes made:').'<ul>';
10271:             foreach my $item (@types) {
10272:                 if ($changes{$item}) {
10273:                     my $newtxt = $offon[$newvals{$item}];
10274:                     $resulttext .= '<li>'.
10275:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
10276:                                        '<b>','</b>').
10277:                                    '</li>';
10278:                 }
10279:             }
10280:             if ($changes{'xmldc'}) {
10281:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
10282:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
10283:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
10284:             }
10285:             $resulttext .= '</ul>';
10286:         } else {
10287:             $resulttext = &mt('No changes made to auto-creation settings');
10288:         }
10289:     } else {
10290:         $resulttext = '<span class="LC_error">'.
10291:             &mt('An error occurred: [_1]',$putresult).'</span>';
10292:     }
10293:     return $resulttext;
10294: }
10295: 
10296: sub modify_directorysrch {
10297:     my ($dom,$lastactref,%domconfig) = @_;
10298:     my ($resulttext,%changes);
10299:     my %currdirsrch;
10300:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
10301:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
10302:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
10303:         }
10304:     }
10305:     my %title = ( available => 'Institutional directory search available',
10306:                   localonly => 'Other domains can search institution',
10307:                   lcavailable => 'LON-CAPA directory search available',
10308:                   lclocalonly => 'Other domains can search LON-CAPA domain',
10309:                   searchby => 'Search types',
10310:                   searchtypes => 'Search latitude');
10311:     my @offon = ('off','on');
10312:     my @otherdoms = ('Yes','No');
10313: 
10314:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
10315:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
10316:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
10317: 
10318:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10319:     if (keys(%{$usertypes}) == 0) {
10320:         @cansearch = ('default');
10321:     } else {
10322:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
10323:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
10324:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
10325:                     push(@{$changes{'cansearch'}},$type);
10326:                 }
10327:             }
10328:             foreach my $type (@cansearch) {
10329:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
10330:                     push(@{$changes{'cansearch'}},$type);
10331:                 }
10332:             }
10333:         } else {
10334:             push(@{$changes{'cansearch'}},@cansearch);
10335:         }
10336:     }
10337: 
10338:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
10339:         foreach my $by (@{$currdirsrch{'searchby'}}) {
10340:             if (!grep(/^\Q$by\E$/,@searchby)) {
10341:                 push(@{$changes{'searchby'}},$by);
10342:             }
10343:         }
10344:         foreach my $by (@searchby) {
10345:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
10346:                 push(@{$changes{'searchby'}},$by);
10347:             }
10348:         }
10349:     } else {
10350:         push(@{$changes{'searchby'}},@searchby);
10351:     }
10352: 
10353:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
10354:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
10355:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
10356:                 push(@{$changes{'searchtypes'}},$type);
10357:             }
10358:         }
10359:         foreach my $type (@searchtypes) {
10360:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
10361:                 push(@{$changes{'searchtypes'}},$type);
10362:             }
10363:         }
10364:     } else {
10365:         if (exists($currdirsrch{'searchtypes'})) {
10366:             foreach my $type (@searchtypes) {  
10367:                 if ($type ne $currdirsrch{'searchtypes'}) { 
10368:                     push(@{$changes{'searchtypes'}},$type);
10369:                 }
10370:             }
10371:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
10372:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
10373:             }   
10374:         } else {
10375:             push(@{$changes{'searchtypes'}},@searchtypes); 
10376:         }
10377:     }
10378: 
10379:     my %dirsrch_hash =  (
10380:             directorysrch => { available => $env{'form.dirsrch_available'},
10381:                                cansearch => \@cansearch,
10382:                                localonly => $env{'form.dirsrch_instlocalonly'},
10383:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
10384:                                lcavailable => $env{'form.dirsrch_domavailable'},
10385:                                searchby => \@searchby,
10386:                                searchtypes => \@searchtypes,
10387:                              }
10388:             );
10389:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
10390:                                              $dom);
10391:     if ($putresult eq 'ok') {
10392:         if (exists($currdirsrch{'available'})) {
10393:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
10394:                  $changes{'available'} = 1;
10395:              }
10396:         } else {
10397:             if ($env{'form.dirsrch_available'} eq '1') {
10398:                 $changes{'available'} = 1;
10399:             }
10400:         }
10401:         if (exists($currdirsrch{'lcavailable'})) {
10402:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
10403:                 $changes{'lcavailable'} = 1;
10404:             }
10405:         } else {
10406:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
10407:                 $changes{'lcavailable'} = 1;
10408:             }
10409:         }
10410:         if (exists($currdirsrch{'localonly'})) {
10411:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
10412:                 $changes{'localonly'} = 1;
10413:             }
10414:         } else {
10415:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
10416:                 $changes{'localonly'} = 1;
10417:             }
10418:         }
10419:         if (exists($currdirsrch{'lclocalonly'})) {
10420:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
10421:                 $changes{'lclocalonly'} = 1;
10422:             }
10423:         } else {
10424:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
10425:                 $changes{'lclocalonly'} = 1;
10426:             }
10427:         }
10428:         if (keys(%changes) > 0) {
10429:             $resulttext = &mt('Changes made:').'<ul>';
10430:             if ($changes{'available'}) {
10431:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
10432:             }
10433:             if ($changes{'lcavailable'}) {
10434:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
10435:             }
10436:             if ($changes{'localonly'}) {
10437:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
10438:             }
10439:             if ($changes{'lclocalonly'}) {
10440:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
10441:             }
10442:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
10443:                 my $chgtext;
10444:                 if (ref($usertypes) eq 'HASH') {
10445:                     if (keys(%{$usertypes}) > 0) {
10446:                         foreach my $type (@{$types}) {
10447:                             if (grep(/^\Q$type\E$/,@cansearch)) {
10448:                                 $chgtext .= $usertypes->{$type}.'; ';
10449:                             }
10450:                         }
10451:                         if (grep(/^default$/,@cansearch)) {
10452:                             $chgtext .= $othertitle;
10453:                         } else {
10454:                             $chgtext =~ s/\; $//;
10455:                         }
10456:                         $resulttext .=
10457:                             '<li>'.
10458:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
10459:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
10460:                             '</li>';
10461:                     }
10462:                 }
10463:             }
10464:             if (ref($changes{'searchby'}) eq 'ARRAY') {
10465:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
10466:                 my $chgtext;
10467:                 foreach my $type (@{$titleorder}) {
10468:                     if (grep(/^\Q$type\E$/,@searchby)) {
10469:                         if (defined($searchtitles->{$type})) {
10470:                             $chgtext .= $searchtitles->{$type}.'; ';
10471:                         }
10472:                     }
10473:                 }
10474:                 $chgtext =~ s/\; $//;
10475:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
10476:             }
10477:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
10478:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
10479:                 my $chgtext;
10480:                 foreach my $type (@{$srchtypeorder}) {
10481:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
10482:                         if (defined($srchtypes_desc->{$type})) {
10483:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
10484:                         }
10485:                     }
10486:                 }
10487:                 $chgtext =~ s/\; $//;
10488:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
10489:             }
10490:             $resulttext .= '</ul>';
10491:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
10492:             if (ref($lastactref) eq 'HASH') {
10493:                 $lastactref->{'directorysrch'} = 1;
10494:             }
10495:         } else {
10496:             $resulttext = &mt('No changes made to directory search settings');
10497:         }
10498:     } else {
10499:         $resulttext = '<span class="LC_error">'.
10500:                       &mt('An error occurred: [_1]',$putresult).'</span>';
10501:     }
10502:     return $resulttext;
10503: }
10504: 
10505: sub modify_contacts {
10506:     my ($dom,$lastactref,%domconfig) = @_;
10507:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
10508:     if (ref($domconfig{'contacts'}) eq 'HASH') {
10509:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
10510:             $currsetting{$key} = $domconfig{'contacts'}{$key};
10511:         }
10512:     }
10513:     my (%others,%to,%bcc,%includestr,%includeloc);
10514:     my @contacts = ('supportemail','adminemail');
10515:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
10516:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail');
10517:     my @toggles = ('reporterrors','reportupdates');
10518:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
10519:     foreach my $type (@mailings) {
10520:         @{$newsetting{$type}} = 
10521:             &Apache::loncommon::get_env_multiple('form.'.$type);
10522:         foreach my $item (@contacts) {
10523:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
10524:                 $contacts_hash{contacts}{$type}{$item} = 1;
10525:             } else {
10526:                 $contacts_hash{contacts}{$type}{$item} = 0;
10527:             }
10528:         }
10529:         $others{$type} = $env{'form.'.$type.'_others'};
10530:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
10531:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10532:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
10533:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
10534:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
10535:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
10536:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
10537:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
10538:             }
10539:         }
10540:     }
10541:     foreach my $item (@contacts) {
10542:         $to{$item} = $env{'form.'.$item};
10543:         $contacts_hash{'contacts'}{$item} = $to{$item};
10544:     }
10545:     foreach my $item (@toggles) {
10546:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
10547:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
10548:         }
10549:     }
10550:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
10551:         foreach my $field (@{$fields}) {
10552:             if (ref($possoptions->{$field}) eq 'ARRAY') {
10553:                 my $value = $env{'form.helpform_'.$field};
10554:                 $value =~ s/^\s+|\s+$//g;
10555:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
10556:                     $contacts_hash{'contacts'}{'helpform'}{$field} = $value;
10557:                     if ($field eq 'screenshot') {
10558:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
10559:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
10560:                             $contacts_hash{'contacts'}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
10561:                         }
10562:                     }
10563:                 }
10564:             }
10565:         }
10566:     }
10567:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10568:     my (@statuses,%usertypeshash,@overrides);
10569:     if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
10570:         @statuses = @{$types};
10571:         if (ref($usertypes) eq 'HASH') {
10572:             %usertypeshash = %{$usertypes};
10573:         }
10574:     }
10575:     if (@statuses) {
10576:         my @possoverrides = &Apache::loncommon::get_env_multiple('form.overrides');
10577:         foreach my $type (@possoverrides) {
10578:             if (($type ne '') && (grep(/^\Q$type\E$/,@statuses))) {
10579:                 push(@overrides,$type);
10580:             }
10581:         }
10582:         if (@overrides) {
10583:             foreach my $type (@overrides) {
10584:                 my @standard = &Apache::loncommon::get_env_multiple('form.override_'.$type);
10585:                 foreach my $item (@contacts) {
10586:                     if (grep(/^\Q$item\E$/,@standard)) {
10587:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 1;
10588:                         $newsetting{'override_'.$type}{$item} = 1;
10589:                     } else {
10590:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 0;
10591:                         $newsetting{'override_'.$type}{$item} = 0;
10592:                     }
10593:                 }
10594:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'others'} = $env{'form.override_'.$type.'_others'};
10595:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
10596:                 $newsetting{'override_'.$type}{'others'} = $env{'form.override_'.$type.'_others'};
10597:                 $newsetting{'override_'.$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
10598:                 if (($env{'form.override_'.$type.'_includestr'} ne '') && ($env{'form.override_'.$type.'_includeloc'} =~ /^s|b$/)) {
10599:                     $includestr{$type} = $env{'form.override_'.$type.'_includestr'};
10600:                     $includeloc{$type} = $env{'form.override_'.$type.'_includeloc'};
10601:                     $contacts_hash{'contacts'}{'overrides'}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
10602:                     $newsetting{'override_'.$type}{'include'} = $contacts_hash{'contacts'}{'overrides'}{$type}{'include'};
10603:                 }
10604:             }
10605:         }
10606:     }
10607:     if (keys(%currsetting) > 0) {
10608:         foreach my $item (@contacts) {
10609:             if ($to{$item} ne $currsetting{$item}) {
10610:                 $changes{$item} = 1;
10611:             }
10612:         }
10613:         foreach my $type (@mailings) {
10614:             foreach my $item (@contacts) {
10615:                 if (ref($currsetting{$type}) eq 'HASH') {
10616:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
10617:                         push(@{$changes{$type}},$item);
10618:                     }
10619:                 } else {
10620:                     push(@{$changes{$type}},@{$newsetting{$type}});
10621:                 }
10622:             }
10623:             if ($others{$type} ne $currsetting{$type}{'others'}) {
10624:                 push(@{$changes{$type}},'others');
10625:             }
10626:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10627:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
10628:                     push(@{$changes{$type}},'bcc'); 
10629:                 }
10630:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
10631:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
10632:                     push(@{$changes{$type}},'include');
10633:                 }
10634:             }
10635:         }
10636:         if (ref($fields) eq 'ARRAY') {
10637:             if (ref($currsetting{'helpform'}) eq 'HASH') {
10638:                 foreach my $field (@{$fields}) {
10639:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
10640:                         push(@{$changes{'helpform'}},$field);
10641:                     }
10642:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10643:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
10644:                             push(@{$changes{'helpform'}},'maxsize');
10645:                         }
10646:                     }
10647:                 }
10648:             } else {
10649:                 foreach my $field (@{$fields}) {
10650:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
10651:                         push(@{$changes{'helpform'}},$field);
10652:                     }
10653:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10654:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
10655:                             push(@{$changes{'helpform'}},'maxsize');
10656:                         }
10657:                     }
10658:                 }
10659:             }
10660:         }
10661:         if (@statuses) {
10662:             if (ref($currsetting{'overrides'}) eq 'HASH') {
10663:                 foreach my $key (keys(%{$currsetting{'overrides'}})) {
10664:                     if (ref($currsetting{'overrides'}{$key}) eq 'HASH') {
10665:                         if (ref($newsetting{'override_'.$key}) eq 'HASH') {
10666:                             foreach my $item (@contacts,'bcc','others','include') {
10667:                                 if ($currsetting{'overrides'}{$key}{$item} ne $newsetting{'override_'.$key}{$item}) {
10668:                                     push(@{$changes{'overrides'}},$key);
10669:                                     last;
10670:                                 }
10671:                             }
10672:                         } else {
10673:                             push(@{$changes{'overrides'}},$key);
10674:                         }
10675:                     }
10676:                 }
10677:                 foreach my $key (@overrides) {
10678:                     unless (exists($currsetting{'overrides'}{$key})) {
10679:                         push(@{$changes{'overrides'}},$key);
10680:                     }
10681:                 }
10682:             } else {
10683:                 foreach my $key (@overrides) {
10684:                     push(@{$changes{'overrides'}},$key);
10685:                 }
10686:             }
10687:         }
10688:     } else {
10689:         my %default;
10690:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
10691:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
10692:         $default{'errormail'} = 'adminemail';
10693:         $default{'packagesmail'} = 'adminemail';
10694:         $default{'helpdeskmail'} = 'supportemail';
10695:         $default{'otherdomsmail'} = 'supportemail';
10696:         $default{'lonstatusmail'} = 'adminemail';
10697:         $default{'requestsmail'} = 'adminemail';
10698:         $default{'updatesmail'} = 'adminemail';
10699:         foreach my $item (@contacts) {
10700:            if ($to{$item} ne $default{$item}) {
10701:                $changes{$item} = 1;
10702:            }
10703:         }
10704:         foreach my $type (@mailings) {
10705:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
10706:                 push(@{$changes{$type}},@{$newsetting{$type}});
10707:             }
10708:             if ($others{$type} ne '') {
10709:                 push(@{$changes{$type}},'others');
10710:             }
10711:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10712:                 if ($bcc{$type} ne '') {
10713:                     push(@{$changes{$type}},'bcc');
10714:                 }
10715:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
10716:                     push(@{$changes{$type}},'include');
10717:                 }
10718:             }
10719:         }
10720:         if (ref($fields) eq 'ARRAY') {
10721:             foreach my $field (@{$fields}) {
10722:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
10723:                     push(@{$changes{'helpform'}},$field);
10724:                 }
10725:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10726:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
10727:                         push(@{$changes{'helpform'}},'maxsize');
10728:                     }
10729:                 }
10730:             }
10731:         }
10732:     }
10733:     foreach my $item (@toggles) {
10734:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
10735:             $changes{$item} = 1;
10736:         } elsif ((!$env{'form.'.$item}) &&
10737:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
10738:             $changes{$item} = 1;
10739:         }
10740:     }
10741:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
10742:                                              $dom);
10743:     if ($putresult eq 'ok') {
10744:         if (keys(%changes) > 0) {
10745:             &Apache::loncommon::devalidate_domconfig_cache($dom);
10746:             if (ref($lastactref) eq 'HASH') {
10747:                 $lastactref->{'domainconfig'} = 1;
10748:             }
10749:             my ($titles,$short_titles)  = &contact_titles();
10750:             $resulttext = &mt('Changes made:').'<ul>';
10751:             foreach my $item (@contacts) {
10752:                 if ($changes{$item}) {
10753:                     $resulttext .= '<li>'.$titles->{$item}.
10754:                                     &mt(' set to: ').
10755:                                     '<span class="LC_cusr_emph">'.
10756:                                     $to{$item}.'</span></li>';
10757:                 }
10758:             }
10759:             foreach my $type (@mailings) {
10760:                 if (ref($changes{$type}) eq 'ARRAY') {
10761:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10762:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
10763:                     } else {
10764:                         $resulttext .= '<li>'.$titles->{$type}.': ';
10765:                     }
10766:                     my @text;
10767:                     foreach my $item (@{$newsetting{$type}}) {
10768:                         push(@text,$short_titles->{$item});
10769:                     }
10770:                     if ($others{$type} ne '') {
10771:                         push(@text,$others{$type});
10772:                     }
10773:                     if (@text) {
10774:                         $resulttext .= '<span class="LC_cusr_emph">'.
10775:                                        join(', ',@text).'</span>';
10776:                     }
10777:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10778:                         if ($bcc{$type} ne '') {
10779:                             my $bcctext;
10780:                             if (@text) {
10781:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
10782:                             } else {
10783:                                 $bcctext = '(Bcc)';
10784:                             }
10785:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
10786:                         } elsif (!@text) {
10787:                             $resulttext .= &mt('No one');
10788:                         }
10789:                         if ($includestr{$type} ne '') {
10790:                             if ($includeloc{$type} eq 'b') {
10791:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
10792:                             } elsif ($includeloc{$type} eq 's') {
10793:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
10794:                             }
10795:                         }
10796:                     } elsif (!@text) {
10797:                         $resulttext .= &mt('No recipients');
10798:                     }
10799:                     $resulttext .= '</li>';
10800:                 }
10801:             }
10802:             if (ref($changes{'overrides'}) eq 'ARRAY') {
10803:                 my @deletions;
10804:                 foreach my $type (@{$changes{'overrides'}}) {
10805:                     if ($usertypeshash{$type}) {
10806:                         if (grep(/^\Q$type\E/,@overrides)) {
10807:                             $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation set for [_1]",
10808:                                                       $usertypeshash{$type}).'<ul><li>';
10809:                             if (ref($newsetting{'override_'.$type}) eq 'HASH') {
10810:                                 my @text;
10811:                                 foreach my $item (@contacts) {
10812:                                     if ($newsetting{'override_'.$type}{$item}) {
10813:                                         push(@text,$short_titles->{$item});
10814:                                     }
10815:                                 }
10816:                                 if ($newsetting{'override_'.$type}{'others'} ne '') {
10817:                                     push(@text,$newsetting{'override_'.$type}{'others'});
10818:                                 }
10819: 
10820:                                 if (@text) {
10821:                                     $resulttext .= &mt('Helpdesk e-mail sent to: [_1]',
10822:                                                        '<span class="LC_cusr_emph">'.join(', ',@text).'</span>');
10823:                                 }
10824:                                 if ($newsetting{'override_'.$type}{'bcc'} ne '') {
10825:                                     my $bcctext;
10826:                                     if (@text) {
10827:                                         $bcctext = '&nbsp;'.&mt('with Bcc to');
10828:                                     } else {
10829:                                         $bcctext = '(Bcc)';
10830:                                     }
10831:                                     $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$newsetting{'override_'.$type}{'bcc'}.'</span>';
10832:                                 } elsif (!@text) {
10833:                                      $resulttext .= &mt('Helpdesk e-mail sent to no one');
10834:                                 }
10835:                                 $resulttext .= '</li>';
10836:                                 if ($newsetting{'override_'.$type}{'include'} ne '') {
10837:                                     my ($loc,$str) = split(/:/,$newsetting{'override_'.$type}{'include'});
10838:                                     if ($loc eq 'b') {
10839:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail body:').' '.&unescape($str).'</li>';
10840:                                     } elsif ($loc eq 's') {
10841:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail subject:').' '.&unescape($str).'</li>';
10842:                                     }
10843:                                 }
10844:                             }
10845:                             $resulttext .= '</li></ul></li>';
10846:                         } else {
10847:                             push(@deletions,$usertypeshash{$type});
10848:                         }
10849:                     }
10850:                 }
10851:                 if (@deletions) {
10852:                     $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation discontinued for: [_1]",
10853:                                               join(', ',@deletions)).'</li>';
10854:                 }
10855:             }
10856:             my @offon = ('off','on');
10857:             if ($changes{'reporterrors'}) {
10858:                 $resulttext .= '<li>'.
10859:                                &mt('E-mail error reports to [_1] set to "'.
10860:                                    $offon[$env{'form.reporterrors'}].'".',
10861:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
10862:                                        &mt('LON-CAPA core group - MSU'),600,500)).
10863:                                '</li>';
10864:             }
10865:             if ($changes{'reportupdates'}) {
10866:                 $resulttext .= '<li>'.
10867:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
10868:                                     $offon[$env{'form.reportupdates'}].'".',
10869:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
10870:                                         &mt('LON-CAPA core group - MSU'),600,500)).
10871:                                 '</li>';
10872:             }
10873:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
10874:                 my (@optional,@required,@unused,$maxsizechg);
10875:                 foreach my $field (@{$changes{'helpform'}}) {
10876:                     if ($field eq 'maxsize') {
10877:                         $maxsizechg = 1;
10878:                         next;
10879:                     }
10880:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
10881:                         push(@optional,$field);
10882:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
10883:                         push(@unused,$field);
10884:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
10885:                         push(@required,$field);
10886:                     }
10887:                 }
10888:                 if (@optional) {
10889:                     $resulttext .= '<li>'.
10890:                                    &mt('Help form fields changed to "Optional": [_1].',
10891:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
10892:                                    '</li>';
10893:                 }
10894:                 if (@required) {
10895:                     $resulttext .= '<li>'.
10896:                                    &mt('Help form fields changed to "Required": [_1].',
10897:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
10898:                                    '</li>';
10899:                 }
10900:                 if (@unused) {
10901:                     $resulttext .= '<li>'.
10902:                                    &mt('Help form fields changed to "Not shown": [_1].',
10903:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
10904:                                    '</li>';
10905:                 }
10906:                 if ($maxsizechg) {
10907:                     $resulttext .= '<li>'.
10908:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
10909:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
10910:                                    '</li>';
10911:                 }
10912:             }
10913:             $resulttext .= '</ul>';
10914:         } else {
10915:             $resulttext = &mt('No changes made to contacts and form settings');
10916:         }
10917:     } else {
10918:         $resulttext = '<span class="LC_error">'.
10919:             &mt('An error occurred: [_1].',$putresult).'</span>';
10920:     }
10921:     return $resulttext;
10922: }
10923: 
10924: sub modify_usercreation {
10925:     my ($dom,%domconfig) = @_;
10926:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
10927:     my $warningmsg;
10928:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
10929:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
10930:             if ($key eq 'cancreate') {
10931:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
10932:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
10933:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
10934:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
10935:                             ($item eq 'recaptchaversion')) {
10936:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
10937:                         } else {
10938:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
10939:                         }
10940:                     }
10941:                 }
10942:             } elsif ($key eq 'email_rule') {
10943:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
10944:             } else {
10945:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
10946:             }
10947:         }
10948:     }
10949:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
10950:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
10951:     my @contexts = ('author','course','requestcrs');
10952:     foreach my $item(@contexts) {
10953:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
10954:     }
10955:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
10956:         foreach my $item (@contexts) {
10957:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
10958:                 push(@{$changes{'cancreate'}},$item);
10959:             }
10960:         }
10961:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
10962:         foreach my $item (@contexts) {
10963:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
10964:                 if ($cancreate{$item} ne 'any') {
10965:                     push(@{$changes{'cancreate'}},$item);
10966:                 }
10967:             } else {
10968:                 if ($cancreate{$item} ne 'none') {
10969:                     push(@{$changes{'cancreate'}},$item);
10970:                 }
10971:             }
10972:         }
10973:     } else {
10974:         foreach my $item (@contexts)  {
10975:             push(@{$changes{'cancreate'}},$item);
10976:         }
10977:     }
10978: 
10979:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
10980:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
10981:             if (!grep(/^\Q$type\E$/,@username_rule)) {
10982:                 push(@{$changes{'username_rule'}},$type);
10983:             }
10984:         }
10985:         foreach my $type (@username_rule) {
10986:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
10987:                 push(@{$changes{'username_rule'}},$type);
10988:             }
10989:         }
10990:     } else {
10991:         push(@{$changes{'username_rule'}},@username_rule);
10992:     }
10993: 
10994:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
10995:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
10996:             if (!grep(/^\Q$type\E$/,@id_rule)) {
10997:                 push(@{$changes{'id_rule'}},$type);
10998:             }
10999:         }
11000:         foreach my $type (@id_rule) {
11001:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
11002:                 push(@{$changes{'id_rule'}},$type);
11003:             }
11004:         }
11005:     } else {
11006:         push(@{$changes{'id_rule'}},@id_rule);
11007:     }
11008: 
11009:     my @authen_contexts = ('author','course','domain');
11010:     my @authtypes = ('int','krb4','krb5','loc');
11011:     my %authhash;
11012:     foreach my $item (@authen_contexts) {
11013:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
11014:         foreach my $auth (@authtypes) {
11015:             if (grep(/^\Q$auth\E$/,@authallowed)) {
11016:                 $authhash{$item}{$auth} = 1;
11017:             } else {
11018:                 $authhash{$item}{$auth} = 0;
11019:             }
11020:         }
11021:     }
11022:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
11023:         foreach my $item (@authen_contexts) {
11024:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
11025:                 foreach my $auth (@authtypes) {
11026:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
11027:                         push(@{$changes{'authtypes'}},$item);
11028:                         last;
11029:                     }
11030:                 }
11031:             }
11032:         }
11033:     } else {
11034:         foreach my $item (@authen_contexts) {
11035:             push(@{$changes{'authtypes'}},$item);
11036:         }
11037:     }
11038: 
11039:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
11040:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
11041:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
11042:     $save_usercreate{'id_rule'} = \@id_rule;
11043:     $save_usercreate{'username_rule'} = \@username_rule,
11044:     $save_usercreate{'authtypes'} = \%authhash;
11045: 
11046:     my %usercreation_hash =  (
11047:         usercreation     => \%save_usercreate,
11048:     );
11049: 
11050:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
11051:                                              $dom);
11052: 
11053:     if ($putresult eq 'ok') {
11054:         if (keys(%changes) > 0) {
11055:             $resulttext = &mt('Changes made:').'<ul>';
11056:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
11057:                 my %lt = &usercreation_types();
11058:                 foreach my $type (@{$changes{'cancreate'}}) {
11059:                     my $chgtext = $lt{$type}.', ';
11060:                     if ($cancreate{$type} eq 'none') {
11061:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
11062:                     } elsif ($cancreate{$type} eq 'any') {
11063:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
11064:                     } elsif ($cancreate{$type} eq 'official') {
11065:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
11066:                     } elsif ($cancreate{$type} eq 'unofficial') {
11067:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
11068:                     }
11069:                     $resulttext .= '<li>'.$chgtext.'</li>';
11070:                 }
11071:             }
11072:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
11073:                 my ($rules,$ruleorder) = 
11074:                     &Apache::lonnet::inst_userrules($dom,'username');
11075:                 my $chgtext = '<ul>';
11076:                 foreach my $type (@username_rule) {
11077:                     if (ref($rules->{$type}) eq 'HASH') {
11078:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
11079:                     }
11080:                 }
11081:                 $chgtext .= '</ul>';
11082:                 if (@username_rule > 0) {
11083:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
11084:                 } else {
11085:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
11086:                 }
11087:             }
11088:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
11089:                 my ($idrules,$idruleorder) = 
11090:                     &Apache::lonnet::inst_userrules($dom,'id');
11091:                 my $chgtext = '<ul>';
11092:                 foreach my $type (@id_rule) {
11093:                     if (ref($idrules->{$type}) eq 'HASH') {
11094:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
11095:                     }
11096:                 }
11097:                 $chgtext .= '</ul>';
11098:                 if (@id_rule > 0) {
11099:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
11100:                 } else {
11101:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
11102:                 }
11103:             }
11104:             my %authname = &authtype_names();
11105:             my %context_title = &context_names();
11106:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
11107:                 my $chgtext = '<ul>';
11108:                 foreach my $type (@{$changes{'authtypes'}}) {
11109:                     my @allowed;
11110:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
11111:                     foreach my $auth (@authtypes) {
11112:                         if ($authhash{$type}{$auth}) {
11113:                             push(@allowed,$authname{$auth});
11114:                         }
11115:                     }
11116:                     if (@allowed > 0) {
11117:                         $chgtext .= join(', ',@allowed).'</li>';
11118:                     } else {
11119:                         $chgtext .= &mt('none').'</li>';
11120:                     }
11121:                 }
11122:                 $chgtext .= '</ul>';
11123:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
11124:                 $resulttext .= '</li>';
11125:             }
11126:             $resulttext .= '</ul>';
11127:         } else {
11128:             $resulttext = &mt('No changes made to user creation settings');
11129:         }
11130:     } else {
11131:         $resulttext = '<span class="LC_error">'.
11132:             &mt('An error occurred: [_1]',$putresult).'</span>';
11133:     }
11134:     if ($warningmsg ne '') {
11135:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
11136:     }
11137:     return $resulttext;
11138: }
11139: 
11140: sub modify_selfcreation {
11141:     my ($dom,%domconfig) = @_;
11142:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%changes,%cancreate);
11143:     my (%save_usercreate,%save_usermodify);
11144:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
11145:     if (ref($types) eq 'ARRAY') {
11146:         $usertypes->{'default'} = $othertitle;
11147:         push(@{$types},'default');
11148:     }
11149: #
11150: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
11151: #
11152:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
11153:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
11154:             if ($key eq 'cancreate') {
11155:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
11156:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
11157:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
11158:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') || 
11159:                             ($item eq 'recaptchaversion') ||
11160:                             ($item eq 'emailusername') || ($item eq 'notify') ||
11161:                             ($item eq 'selfcreateprocessing') || ($item eq 'shibenv')) {
11162:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11163:                         } else {
11164:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11165:                         }
11166:                     }
11167:                 }
11168:             } elsif ($key eq 'email_rule') {
11169:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
11170:             } else {
11171:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
11172:             }
11173:         }
11174:     }
11175: #
11176: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
11177: #
11178:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
11179:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
11180:             if ($key eq 'selfcreate') {
11181:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
11182:             } else {
11183:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
11184:             }
11185:         }
11186:     }
11187: 
11188:     my @contexts = ('selfcreate');
11189:     @{$cancreate{'selfcreate'}} = ();
11190:     %{$cancreate{'emailusername'}} = ();
11191:     @{$cancreate{'statustocreate'}} = ();
11192:     %{$cancreate{'selfcreateprocessing'}} = ();
11193:     %{$cancreate{'shibenv'}} = ();
11194:     my %selfcreatetypes = (
11195:                              sso   => 'users authenticated by institutional single sign on',
11196:                              login => 'users authenticated by institutional log-in',
11197:                              email => 'users who provide a valid e-mail address for use as username',
11198:                           );
11199: #
11200: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
11201: # is permitted.
11202: #
11203: 
11204:     my @statuses;
11205:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
11206:         if (ref($domconfig{'inststatus'}{'inststatusguest'}) eq 'ARRAY') {
11207:             @statuses = @{$domconfig{'inststatus'}{'inststatusguest'}};
11208:         }
11209:     }
11210:     push(@statuses,'default');
11211: 
11212:     foreach my $item ('login','sso','email') {
11213:         if ($item eq 'email') {
11214:             if ($env{'form.cancreate_email'}) {
11215:                 push(@{$cancreate{'selfcreate'}},'email');
11216:                 push(@contexts,'selfcreateprocessing');
11217:                 foreach my $type (@statuses) {
11218:                     if ($type eq 'default') {
11219:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess'};
11220:                     } else { 
11221:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
11222:                     }
11223:                 }
11224:             }
11225:         } else {
11226:             if ($env{'form.cancreate_'.$item}) {
11227:                 push(@{$cancreate{'selfcreate'}},$item);
11228:             }
11229:         }
11230:     }
11231:     my (@email_rule,%userinfo,%savecaptcha);
11232:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
11233: #
11234: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
11235: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
11236: #
11237: 
11238:     if ($env{'form.cancreate_email'}) {
11239:         push(@contexts,'emailusername');
11240:         if (ref($types) eq 'ARRAY') {
11241:             foreach my $type (@{$types}) {
11242:                 if (ref($infofields) eq 'ARRAY') {
11243:                     foreach my $field (@{$infofields}) {
11244:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
11245:                             $cancreate{'emailusername'}{$type}{$field} = $1;
11246:                         }
11247:                     }
11248:                 }
11249:             }
11250:         }
11251: #
11252: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
11253: # queued requests for self-creation of account using e-mail address as username
11254: #
11255: 
11256:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
11257:         @approvalnotify = sort(@approvalnotify);
11258:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
11259:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
11260:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
11261:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
11262:                     push(@{$changes{'cancreate'}},'notify');
11263:                 }
11264:             } else {
11265:                 if ($cancreate{'notify'}{'approval'}) {
11266:                     push(@{$changes{'cancreate'}},'notify');
11267:                 }
11268:             }
11269:         } elsif ($cancreate{'notify'}{'approval'}) {
11270:             push(@{$changes{'cancreate'}},'notify');
11271:         }
11272: 
11273: #
11274: # Retrieve rules (if any) governing types of e-mail address which may be used as a username
11275: #
11276:         @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
11277:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
11278:         if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
11279:             if (@{$curr_usercreation{'email_rule'}} > 0) {
11280:                 foreach my $type (@{$curr_usercreation{'email_rule'}}) {
11281:                     if (!grep(/^\Q$type\E$/,@email_rule)) {
11282:                         push(@{$changes{'email_rule'}},$type);
11283:                     }
11284:                 }
11285:             }
11286:             if (@email_rule > 0) {
11287:                 foreach my $type (@email_rule) {
11288:                     if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
11289:                         push(@{$changes{'email_rule'}},$type);
11290:                     }
11291:                 }
11292:             }
11293:         } elsif (@email_rule > 0) {
11294:             push(@{$changes{'email_rule'}},@email_rule);
11295:         }
11296:     }
11297: #  
11298: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
11299: # institutional log-in.
11300: #
11301:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
11302:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11303:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
11304:                ($domdefaults{'auth_def'} eq 'localauth'))) {
11305:             $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.').' '.
11306:                           &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.');
11307:         }
11308:     }
11309:     my @fields = ('lastname','firstname','middlename','generation',
11310:                   'permanentemail','id');
11311:     my @shibfields = (@fields,'inststatus');
11312:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11313: #
11314: # Where usernames may created for institutional log-in and/or institutional single sign on:
11315: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
11316: # may self-create accounts 
11317: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
11318: # which the user may supply, if institutional data is unavailable.
11319: #
11320:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
11321:         if (ref($types) eq 'ARRAY') {
11322:             if (@{$types} > 1) {
11323:                 @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
11324:                 push(@contexts,'statustocreate');
11325:             } else {
11326:                 undef($cancreate{'statustocreate'});
11327:             } 
11328:             foreach my $type (@{$types}) {
11329:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
11330:                 foreach my $field (@fields) {
11331:                     if (grep(/^\Q$field\E$/,@modifiable)) {
11332:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
11333:                     } else {
11334:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
11335:                     }
11336:                 }
11337:             }
11338:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
11339:                 foreach my $type (@{$types}) {
11340:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
11341:                         foreach my $field (@fields) {
11342:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
11343:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
11344:                                 push(@{$changes{'selfcreate'}},$type);
11345:                                 last;
11346:                             }
11347:                         }
11348:                     }
11349:                 }
11350:             } else {
11351:                 foreach my $type (@{$types}) {
11352:                     push(@{$changes{'selfcreate'}},$type);
11353:                 }
11354:             }
11355:         }
11356:         foreach my $field (@shibfields) {
11357:             if ($env{'form.shibenv_'.$field} ne '') {
11358:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
11359:             }
11360:         }
11361:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
11362:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
11363:                 foreach my $field (@shibfields) {
11364:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
11365:                         push(@{$changes{'cancreate'}},'shibenv');
11366:                     }
11367:                 }
11368:             } else {
11369:                 foreach my $field (@shibfields) {
11370:                     if ($env{'form.shibenv_'.$field}) {
11371:                         push(@{$changes{'cancreate'}},'shibenv');
11372:                         last;
11373:                     }
11374:                 }
11375:             }
11376:         }
11377:     }
11378:     foreach my $item (@contexts) {
11379:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
11380:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
11381:                 if (ref($cancreate{$item}) eq 'ARRAY') {
11382:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
11383:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11384:                             push(@{$changes{'cancreate'}},$item);
11385:                         }
11386:                     }
11387:                 }
11388:             }
11389:             if (ref($cancreate{$item}) eq 'ARRAY') {
11390:                 foreach my $type (@{$cancreate{$item}}) {
11391:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
11392:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11393:                             push(@{$changes{'cancreate'}},$item);
11394:                         }
11395:                     }
11396:                 }
11397:             }
11398:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
11399:             if (ref($cancreate{$item}) eq 'HASH') {
11400:                 foreach my $curr (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
11401:                     if (ref($curr_usercreation{'cancreate'}{$item}{$curr}) eq 'HASH') {
11402:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$curr}})) {
11403:                             unless ($curr_usercreation{'cancreate'}{$item}{$curr}{$field} eq $cancreate{$item}{$curr}{$field}) {
11404:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11405:                                     push(@{$changes{'cancreate'}},$item);
11406:                                 }
11407:                             }
11408:                         }
11409:                     } elsif ($item eq 'selfcreateprocessing') {
11410:                         if ($cancreate{$item}{$curr} ne $curr_usercreation{'cancreate'}{$item}{$curr}) {
11411:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11412:                                 push(@{$changes{'cancreate'}},$item);
11413:                             }
11414:                         }
11415:                     } else {
11416:                         if (!$cancreate{$item}{$curr}) {
11417:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11418:                                 push(@{$changes{'cancreate'}},$item);
11419:                             }
11420:                         }
11421:                     }
11422:                 }
11423:                 foreach my $field (keys(%{$cancreate{$item}})) {
11424:                     if (ref($cancreate{$item}{$field}) eq 'HASH') {
11425:                         foreach my $inner (keys(%{$cancreate{$item}{$field}})) {
11426:                             if (ref($curr_usercreation{'cancreate'}{$item}{$field}) eq 'HASH') {
11427:                                 unless ($curr_usercreation{'cancreate'}{$item}{$field}{$inner} eq $cancreate{$item}{$field}{$inner}) {
11428:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11429:                                         push(@{$changes{'cancreate'}},$item);
11430:                                     }
11431:                                 }
11432:                             } else {
11433:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11434:                                     push(@{$changes{'cancreate'}},$item);
11435:                                 }
11436:                             }
11437:                         }
11438:                     } elsif ($item eq 'selfcreateprocessing') {
11439:                         if ($cancreate{$item}{$field} ne $curr_usercreation{'cancreate'}{$item}{$field}) {
11440:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11441:                                 push(@{$changes{'cancreate'}},$item);
11442:                             }
11443:                         }
11444:                     } else {
11445:                         if (!$curr_usercreation{'cancreate'}{$item}{$field}) {
11446:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11447:                                 push(@{$changes{'cancreate'}},$item);
11448:                             }
11449:                         }
11450:                     }
11451:                 }
11452:             }
11453:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
11454:             if (ref($cancreate{$item}) eq 'ARRAY') {
11455:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
11456:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11457:                         push(@{$changes{'cancreate'}},$item);
11458:                     }
11459:                 }
11460:             } elsif (ref($cancreate{$item}) eq 'HASH') {
11461:                 if (!$cancreate{$item}{$curr_usercreation{'cancreate'}{$item}}) {
11462:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11463:                         push(@{$changes{'cancreate'}},$item);
11464:                     }
11465:                 }
11466:             }
11467:         } elsif ($item eq 'emailusername') {
11468:             if (ref($cancreate{$item}) eq 'HASH') {
11469:                 foreach my $type (keys(%{$cancreate{$item}})) {
11470:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
11471:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
11472:                             if ($cancreate{$item}{$type}{$field}) {
11473:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11474:                                     push(@{$changes{'cancreate'}},$item);
11475:                                 }
11476:                                 last;
11477:                             }
11478:                         }
11479:                     }
11480:                 }
11481:             }
11482:         }
11483:     }
11484: #
11485: # Populate %save_usercreate hash with updates to self-creation configuration.
11486: #
11487:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
11488:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
11489:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
11490:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
11491:     if (ref($cancreate{'notify'}) eq 'HASH') {
11492:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
11493:     }
11494:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
11495:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
11496:     }
11497:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
11498:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
11499:     }
11500:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
11501:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
11502:     }
11503:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
11504:     $save_usercreate{'email_rule'} = \@email_rule;
11505: 
11506:     my %userconfig_hash = (
11507:             usercreation     => \%save_usercreate,
11508:             usermodification => \%save_usermodify,
11509:     );
11510:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
11511:                                              $dom);
11512: #
11513: # Accumulate details of changes to domain cofiguration for self-creation of usernames in $resulttext
11514: #
11515:     if ($putresult eq 'ok') {
11516:         if (keys(%changes) > 0) {
11517:             $resulttext = &mt('Changes made:').'<ul>';
11518:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
11519:                 my %lt = &selfcreation_types();
11520:                 foreach my $type (@{$changes{'cancreate'}}) {
11521:                     my $chgtext;
11522:                     if ($type eq 'selfcreate') {
11523:                         if (@{$cancreate{$type}} == 0) {
11524:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
11525:                         } else {
11526:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
11527:                                         '<ul>';
11528:                             foreach my $case (@{$cancreate{$type}}) {
11529:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
11530:                             }
11531:                             $chgtext .= '</ul>';
11532:                             if (ref($cancreate{$type}) eq 'ARRAY') {
11533:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
11534:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
11535:                                         if (@{$cancreate{'statustocreate'}} == 0) {
11536:                                             $chgtext .= '<br />'.
11537:                                                         '<span class="LC_warning">'.
11538:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
11539:                                                         '</span>';
11540:                                         }
11541:                                     }
11542:                                 }
11543:                             }
11544:                         }
11545:                     } elsif ($type eq 'shibenv') {
11546:                         if (keys(%{$cancreate{$type}}) == 0) {
11547:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information'); 
11548:                         } else {
11549:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
11550:                                         '<ul>';
11551:                             foreach my $field (@shibfields) {
11552:                                 next if ($cancreate{$type}{$field} eq '');
11553:                                 if ($field eq 'inststatus') {
11554:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
11555:                                 } else {
11556:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
11557:                                 }
11558:                             }
11559:                             $chgtext .= '</ul>';
11560:                         }  
11561:                     } elsif ($type eq 'statustocreate') {
11562:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
11563:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
11564:                             if (@{$cancreate{'selfcreate'}} > 0) {
11565:                                 if (@{$cancreate{'statustocreate'}} == 0) {
11566:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
11567:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
11568:                                         $chgtext .= '<br />'.
11569:                                                     '<span class="LC_warning">'.
11570:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
11571:                                                     '</span>';
11572:                                     }
11573:                                 } elsif (ref($usertypes) eq 'HASH') {
11574:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
11575:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
11576:                                     } else {
11577:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
11578:                                     }
11579:                                     $chgtext .= '<ul>';
11580:                                     foreach my $case (@{$cancreate{$type}}) {
11581:                                         if ($case eq 'default') {
11582:                                             $chgtext .= '<li>'.$othertitle.'</li>';
11583:                                         } else {
11584:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
11585:                                         }
11586:                                     }
11587:                                     $chgtext .= '</ul>';
11588:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
11589:                                         $chgtext .= '<br /><span class="LC_warning">'.
11590:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
11591:                                                     '</span>';
11592:                                     }
11593:                                 }
11594:                             } else {
11595:                                 if (@{$cancreate{$type}} == 0) {
11596:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
11597:                                 } else {
11598:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
11599:                                 }
11600:                             }
11601:                         }
11602:                     } elsif ($type eq 'selfcreateprocessing') {
11603:                         my %choices = &Apache::lonlocal::texthash (
11604:                                                                     automatic => 'Automatic approval',
11605:                                                                     approval  => 'Queued for approval',
11606:                                                                   );
11607:                         if (@statuses > 1) {
11608:                             $chgtext .= &mt('Processing of requests to create account with e-mail address as username set as follows:'). 
11609:                                         '<ul>';
11610:                            foreach my $type (@statuses) {
11611:                                if ($type eq 'default') {
11612:                                    $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
11613:                                } else {
11614:                                    $chgtext .= '<li>'.$usertypes->{$type}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
11615:                                }
11616:                            }
11617:                            $chgtext .= '</ul>';
11618:                         } else {
11619:                            $chgtext .= &mt('Processing of requests to create account with e-mail address as username set to: "[_1]"',
11620:                                          $choices{$cancreate{'selfcreateprocessing'}{'default'}});
11621:                         }
11622:                     } elsif ($type eq 'captcha') {
11623:                         if ($savecaptcha{$type} eq 'notused') {
11624:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
11625:                         } else {
11626:                             my %captchas = &captcha_phrases();
11627:                             if ($captchas{$savecaptcha{$type}}) {
11628:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
11629:                             } else {
11630:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
11631:                             }
11632:                         }
11633:                     } elsif ($type eq 'recaptchakeys') {
11634:                         my ($privkey,$pubkey);
11635:                         if (ref($savecaptcha{$type}) eq 'HASH') {
11636:                             $pubkey = $savecaptcha{$type}{'public'};
11637:                             $privkey = $savecaptcha{$type}{'private'};
11638:                         }
11639:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
11640:                         if (!$pubkey) {
11641:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
11642:                         } else {
11643:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
11644:                         }
11645:                         if (!$privkey) {
11646:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
11647:                         } else {
11648:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
11649:                         }
11650:                         $chgtext .= '</ul>';
11651:                     } elsif ($type eq 'recaptchaversion') {
11652:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
11653:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
11654:                         }
11655:                     } elsif ($type eq 'emailusername') {
11656:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
11657:                             if (ref($types) eq 'ARRAY') {
11658:                                 foreach my $type (@{$types}) {
11659:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
11660:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
11661:                                             $chgtext .= &mt('When self-creating account with e-mail as username, the following information will be provided by [_1]:',"'$usertypes->{$type}'").
11662:                                                     '<ul>';
11663:                                             foreach my $field (@{$infofields}) {
11664:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
11665:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
11666:                                                 }
11667:                                             }
11668:                                             $chgtext .= '</ul>';
11669:                                         } else {
11670:                                             $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 />';
11671:                                         }
11672:                                     } else {
11673:                                         $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 />';
11674:                                     }
11675:                                 }
11676:                             }
11677:                         }
11678:                     } elsif ($type eq 'notify') {
11679:                         $chgtext = &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
11680:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
11681:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
11682:                                 if ($cancreate{'notify'}{'approval'}) {
11683:                                     $chgtext = &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
11684:                                 }
11685:                             }
11686:                         }
11687:                     }
11688:                     if ($chgtext) {
11689:                         $resulttext .= '<li>'.$chgtext.'</li>';
11690:                     }
11691:                 }
11692:             }
11693:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
11694:                 my ($emailrules,$emailruleorder) =
11695:                     &Apache::lonnet::inst_userrules($dom,'email');
11696:                 my $chgtext = '<ul>';
11697:                 foreach my $type (@email_rule) {
11698:                     if (ref($emailrules->{$type}) eq 'HASH') {
11699:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
11700:                     }
11701:                 }
11702:                 $chgtext .= '</ul>';
11703:                 if (@email_rule > 0) {
11704:                     $resulttext .= '<li>'.
11705:                                    &mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').
11706:                                        $chgtext.
11707:                                    '</li>';
11708:                 } else {
11709:                     $resulttext .= '<li>'.
11710:                                    &mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').
11711:                                    '</li>';
11712:                 }
11713:             }
11714:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
11715:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
11716:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11717:                 foreach my $type (@{$changes{'selfcreate'}}) {
11718:                     my $typename = $type;
11719:                     if (ref($usertypes) eq 'HASH') {
11720:                         if ($usertypes->{$type} ne '') {
11721:                             $typename = $usertypes->{$type};
11722:                         }
11723:                     }
11724:                     my @modifiable;
11725:                     $resulttext .= '<li>'.
11726:                                     &mt('Self-creation of account by users with status: [_1]',
11727:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
11728:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
11729:                     foreach my $field (@fields) {
11730:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
11731:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
11732:                         }
11733:                     }
11734:                     if (@modifiable > 0) {
11735:                         $resulttext .= join(', ',@modifiable);
11736:                     } else {
11737:                         $resulttext .= &mt('none');
11738:                     }
11739:                     $resulttext .= '</li>';
11740:                 }
11741:                 $resulttext .= '</ul></li>';
11742:             }
11743:             $resulttext .= '</ul>';
11744:         } else {
11745:             $resulttext = &mt('No changes made to self-creation settings');
11746:         }
11747:     } else {
11748:         $resulttext = '<span class="LC_error">'.
11749:             &mt('An error occurred: [_1]',$putresult).'</span>';
11750:     }
11751:     if ($warningmsg ne '') {
11752:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
11753:     }
11754:     return $resulttext;
11755: }
11756: 
11757: sub process_captcha {
11758:     my ($container,$changes,$newsettings,$current) = @_;
11759:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
11760:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
11761:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
11762:         $newsettings->{'captcha'} = 'original';
11763:     }
11764:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
11765:         if ($container eq 'cancreate') {
11766:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
11767:                 push(@{$changes->{'cancreate'}},'captcha');
11768:             } elsif (!defined($changes->{'cancreate'})) {
11769:                 $changes->{'cancreate'} = ['captcha'];
11770:             }
11771:         } else {
11772:             $changes->{'captcha'} = 1;
11773:         }
11774:     }
11775:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
11776:     if ($newsettings->{'captcha'} eq 'recaptcha') {
11777:         $newpub = $env{'form.'.$container.'_recaptchapub'};
11778:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
11779:         $newpub =~ s/[^\w\-]//g;
11780:         $newpriv =~ s/[^\w\-]//g;
11781:         $newsettings->{'recaptchakeys'} = {
11782:                                              public  => $newpub,
11783:                                              private => $newpriv,
11784:                                           };
11785:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
11786:         $newversion =~ s/\D//g;
11787:         if ($newversion ne '2') {
11788:             $newversion = 1;
11789:         }
11790:         $newsettings->{'recaptchaversion'} = $newversion;
11791:     }
11792:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
11793:         $currpub = $current->{'recaptchakeys'}{'public'};
11794:         $currpriv = $current->{'recaptchakeys'}{'private'};
11795:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
11796:             $newsettings->{'recaptchakeys'} = {
11797:                                                  public  => '',
11798:                                                  private => '',
11799:                                               }
11800:         }
11801:     }
11802:     if ($current->{'captcha'} eq 'recaptcha') {
11803:         $currversion = $current->{'recaptchaversion'};
11804:         if ($currversion ne '2') {
11805:             $currversion = 1;
11806:         }
11807:     }
11808:     if ($currversion ne $newversion) {
11809:         if ($container eq 'cancreate') {
11810:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
11811:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
11812:             } elsif (!defined($changes->{'cancreate'})) {
11813:                 $changes->{'cancreate'} = ['recaptchaversion'];
11814:             }
11815:         } else {
11816:             $changes->{'recaptchaversion'} = 1;
11817:         }
11818:     }
11819:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
11820:         if ($container eq 'cancreate') {
11821:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
11822:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
11823:             } elsif (!defined($changes->{'cancreate'})) {
11824:                 $changes->{'cancreate'} = ['recaptchakeys'];
11825:             }
11826:         } else {
11827:             $changes->{'recaptchakeys'} = 1;
11828:         }
11829:     }
11830:     return;
11831: }
11832: 
11833: sub modify_usermodification {
11834:     my ($dom,%domconfig) = @_;
11835:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
11836:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
11837:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
11838:             if ($key eq 'selfcreate') {
11839:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
11840:             } else {  
11841:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
11842:             }
11843:         }
11844:     }
11845:     my @contexts = ('author','course');
11846:     my %context_title = (
11847:                            author => 'In author context',
11848:                            course => 'In course context',
11849:                         );
11850:     my @fields = ('lastname','firstname','middlename','generation',
11851:                   'permanentemail','id');
11852:     my %roles = (
11853:                   author => ['ca','aa'],
11854:                   course => ['st','ep','ta','in','cr'],
11855:                 );
11856:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11857:     foreach my $context (@contexts) {
11858:         foreach my $role (@{$roles{$context}}) {
11859:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
11860:             foreach my $item (@fields) {
11861:                 if (grep(/^\Q$item\E$/,@modifiable)) {
11862:                     $modifyhash{$context}{$role}{$item} = 1;
11863:                 } else {
11864:                     $modifyhash{$context}{$role}{$item} = 0;
11865:                 }
11866:             }
11867:         }
11868:         if (ref($curr_usermodification{$context}) eq 'HASH') {
11869:             foreach my $role (@{$roles{$context}}) {
11870:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
11871:                     foreach my $field (@fields) {
11872:                         if ($modifyhash{$context}{$role}{$field} ne 
11873:                                 $curr_usermodification{$context}{$role}{$field}) {
11874:                             push(@{$changes{$context}},$role);
11875:                             last;
11876:                         }
11877:                     }
11878:                 }
11879:             }
11880:         } else {
11881:             foreach my $context (@contexts) {
11882:                 foreach my $role (@{$roles{$context}}) {
11883:                     push(@{$changes{$context}},$role);
11884:                 }
11885:             }
11886:         }
11887:     }
11888:     my %usermodification_hash =  (
11889:                                    usermodification => \%modifyhash,
11890:                                  );
11891:     my $putresult = &Apache::lonnet::put_dom('configuration',
11892:                                              \%usermodification_hash,$dom);
11893:     if ($putresult eq 'ok') {
11894:         if (keys(%changes) > 0) {
11895:             $resulttext = &mt('Changes made: ').'<ul>';
11896:             foreach my $context (@contexts) {
11897:                 if (ref($changes{$context}) eq 'ARRAY') {
11898:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
11899:                     if (ref($changes{$context}) eq 'ARRAY') {
11900:                         foreach my $role (@{$changes{$context}}) {
11901:                             my $rolename;
11902:                             if ($role eq 'cr') {
11903:                                 $rolename = &mt('Custom');
11904:                             } else {
11905:                                 $rolename = &Apache::lonnet::plaintext($role);
11906:                             }
11907:                             my @modifiable;
11908:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
11909:                             foreach my $field (@fields) {
11910:                                 if ($modifyhash{$context}{$role}{$field}) {
11911:                                     push(@modifiable,$fieldtitles{$field});
11912:                                 }
11913:                             }
11914:                             if (@modifiable > 0) {
11915:                                 $resulttext .= join(', ',@modifiable);
11916:                             } else {
11917:                                 $resulttext .= &mt('none'); 
11918:                             }
11919:                             $resulttext .= '</li>';
11920:                         }
11921:                         $resulttext .= '</ul></li>';
11922:                     }
11923:                 }
11924:             }
11925:             $resulttext .= '</ul>';
11926:         } else {
11927:             $resulttext = &mt('No changes made to user modification settings');
11928:         }
11929:     } else {
11930:         $resulttext = '<span class="LC_error">'.
11931:             &mt('An error occurred: [_1]',$putresult).'</span>';
11932:     }
11933:     return $resulttext;
11934: }
11935: 
11936: sub modify_defaults {
11937:     my ($dom,$lastactref,%domconfig) = @_;
11938:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
11939:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11940:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
11941:                  'portal_def','intauth_cost','intauth_check','intauth_switch');
11942:     my @authtypes = ('internal','krb4','krb5','localauth');
11943:     foreach my $item (@items) {
11944:         $newvalues{$item} = $env{'form.'.$item};
11945:         if ($item eq 'auth_def') {
11946:             if ($newvalues{$item} ne '') {
11947:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
11948:                     push(@errors,$item);
11949:                 }
11950:             }
11951:         } elsif ($item eq 'lang_def') {
11952:             if ($newvalues{$item} ne '') {
11953:                 if ($newvalues{$item} =~ /^(\w+)/) {
11954:                     my $langcode = $1;
11955:                     if ($langcode ne 'x_chef') {
11956:                         if (code2language($langcode) eq '') {
11957:                             push(@errors,$item);
11958:                         }
11959:                     }
11960:                 } else {
11961:                     push(@errors,$item);
11962:                 }
11963:             }
11964:         } elsif ($item eq 'timezone_def') {
11965:             if ($newvalues{$item} ne '') {
11966:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
11967:                     push(@errors,$item);   
11968:                 }
11969:             }
11970:         } elsif ($item eq 'datelocale_def') {
11971:             if ($newvalues{$item} ne '') {
11972:                 my @datelocale_ids = DateTime::Locale->ids();
11973:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
11974:                     push(@errors,$item);
11975:                 }
11976:             }
11977:         } elsif ($item eq 'portal_def') {
11978:             if ($newvalues{$item} ne '') {
11979:                 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])\/?$/) {
11980:                     push(@errors,$item);
11981:                 }
11982:             }
11983:         } elsif ($item eq 'intauth_cost') {
11984:             if ($newvalues{$item} ne '') {
11985:                 if ($newvalues{$item} =~ /\D/) {
11986:                     push(@errors,$item);
11987:                 }
11988:             }
11989:         } elsif ($item eq 'intauth_check') {
11990:             if ($newvalues{$item} ne '') {
11991:                 unless ($newvalues{$item} =~ /^(0|1|2)$/) {
11992:                     push(@errors,$item);
11993:                 }
11994:             }
11995:         } elsif ($item eq 'intauth_switch') {
11996:             if ($newvalues{$item} ne '') {
11997:                 unless ($newvalues{$item} =~ /^(0|1|2)$/) {
11998:                     push(@errors,$item);
11999:                 }
12000:             }
12001:         }
12002:         if (grep(/^\Q$item\E$/,@errors)) {
12003:             $newvalues{$item} = $domdefaults{$item};
12004:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
12005:             $changes{$item} = 1;
12006:         }
12007:         $domdefaults{$item} = $newvalues{$item};
12008:     }
12009:     my %defaults_hash = (
12010:                          defaults => \%newvalues,
12011:                         );
12012:     my $title = &defaults_titles();
12013: 
12014:     my $currinststatus;
12015:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
12016:         $currinststatus = $domconfig{'inststatus'};
12017:     } else {
12018:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12019:         $currinststatus = {
12020:                              inststatustypes => $usertypes,
12021:                              inststatusorder => $types,
12022:                              inststatusguest => [],
12023:                           };
12024:     }
12025:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
12026:     my @allpos;
12027:     my %guests;
12028:     my %alltypes;
12029:     my ($currtitles,$currguests,$currorder);
12030:     if (ref($currinststatus) eq 'HASH') {
12031:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
12032:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
12033:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
12034:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
12035:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
12036:                     }
12037:                 }
12038:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
12039:                     my $position = $env{'form.inststatus_pos_'.$type};
12040:                     $position =~ s/\D+//g;
12041:                     $allpos[$position] = $type;
12042:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
12043:                     $alltypes{$type} =~ s/`//g;
12044:                     if ($env{'form.inststatus_guest_'.$type}) {
12045:                         $guests{$type} = 1;
12046:                     }
12047:                 }
12048:             }
12049:             if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
12050:                 $currguests = join(',',@{$currinststatus->{'inststatusguest'}});
12051:             }
12052:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
12053:             $currtitles =~ s/,$//;
12054:         }
12055:     }
12056:     if ($env{'form.addinststatus'}) {
12057:         my $newtype = $env{'form.addinststatus'};
12058:         $newtype =~ s/\W//g;
12059:         unless (exists($alltypes{$newtype})) {
12060:             if ($env{'form.addinststatus_guest'}) {
12061:                 $guests{$newtype} = 1;
12062:             }
12063:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
12064:             $alltypes{$newtype} =~ s/`//g; 
12065:             my $position = $env{'form.addinststatus_pos'};
12066:             $position =~ s/\D+//g;
12067:             if ($position ne '') {
12068:                 $allpos[$position] = $newtype;
12069:             }
12070:         }
12071:     }
12072:     my (@orderedstatus,@orderedguests);
12073:     foreach my $type (@allpos) {
12074:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
12075:             push(@orderedstatus,$type);
12076:             if ($guests{$type}) {
12077:                 push(@orderedguests,$type);
12078:             }
12079:         }
12080:     }
12081:     foreach my $type (keys(%alltypes)) {
12082:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
12083:             delete($alltypes{$type});
12084:         }
12085:     }
12086:     $defaults_hash{'inststatus'} = {
12087:                                      inststatustypes => \%alltypes,
12088:                                      inststatusorder => \@orderedstatus,
12089:                                      inststatusguest => \@orderedguests,
12090:                                    };
12091:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
12092:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
12093:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
12094:         }
12095:     }
12096:     if ($currorder ne join(',',@orderedstatus)) {
12097:         $changes{'inststatus'}{'inststatusorder'} = 1;
12098:     }
12099:     if ($currguests ne join(',',@orderedguests)) {
12100:         $changes{'inststatus'}{'inststatusguest'} = 1;
12101:     }
12102:     my $newtitles;
12103:     foreach my $item (@orderedstatus) {
12104:         $newtitles .= $alltypes{$item}.',';
12105:     }
12106:     $newtitles =~ s/,$//;
12107:     if ($currtitles ne $newtitles) {
12108:         $changes{'inststatus'}{'inststatustypes'} = 1;
12109:     }
12110:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
12111:                                              $dom);
12112:     if ($putresult eq 'ok') {
12113:         if (keys(%changes) > 0) {
12114:             $resulttext = &mt('Changes made:').'<ul>';
12115:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
12116:             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";
12117:             foreach my $item (sort(keys(%changes))) {
12118:                 if ($item eq 'inststatus') {
12119:                     if (ref($changes{'inststatus'}) eq 'HASH') {
12120:                         if (($changes{'inststatus'}{'inststatustypes'}) || $changes{'inststatus'}{'inststatusorder'}) {
12121:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
12122:                             foreach my $type (@orderedstatus) { 
12123:                                 $resulttext .= $alltypes{$type}.', ';
12124:                             }
12125:                             $resulttext =~ s/, $//;
12126:                             $resulttext .= '</li>';
12127:                         }
12128:                         if ($changes{'inststatus'}{'inststatusguest'}) {
12129:                             $resulttext .= '<li>'; 
12130:                             if (@orderedguests) {
12131:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to:').' ';
12132:                                 foreach my $type (@orderedguests) {
12133:                                     $resulttext .= $alltypes{$type}.', ';
12134:                                 }
12135:                                 $resulttext =~ s/, $//;
12136:                             } else {
12137:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to none.');
12138:                             }
12139:                             $resulttext .= '</li>';
12140:                         }
12141:                     }
12142:                 } else {
12143:                     my $value = $env{'form.'.$item};
12144:                     if ($value eq '') {
12145:                         $value = &mt('none');
12146:                     } elsif ($item eq 'auth_def') {
12147:                         my %authnames = &authtype_names();
12148:                         my %shortauth = (
12149:                                           internal   => 'int',
12150:                                           krb4       => 'krb4',
12151:                                           krb5       => 'krb5',
12152:                                           localauth  => 'loc',
12153:                         );
12154:                         $value = $authnames{$shortauth{$value}};
12155:                     } elsif ($item eq 'intauth_switch') {
12156:                         my %optiondesc = &Apache::lonlocal::texthash (
12157:                                             0 => 'No',
12158:                                             1 => 'Yes',
12159:                                             2 => 'Yes, and copy existing passwd file to passwd.bak file',
12160:                                          );
12161:                         if ($value =~ /^(0|1|2)$/) {
12162:                             $value = $optiondesc{$value};
12163:                         } else {
12164:                             $value = &mt('none -- defaults to No');
12165:                         }
12166:                     } elsif ($item eq 'intauth_check') {
12167:                         my %optiondesc = &Apache::lonlocal::texthash (
12168:                                              0 => 'No',
12169:                                              1 => 'Yes, allow login then update passwd file using default cost (if higher)',
12170:                                              2 => 'Yes, disallow login if stored cost is less than domain default',
12171:                                          );
12172:                         if ($value =~ /^(0|1|2)$/) {
12173:                             $value = $optiondesc{$value};
12174:                         } else {
12175:                             $value = &mt('none -- defaults to No');
12176:                         }
12177:                     }
12178:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
12179:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
12180:                 }
12181:             }
12182:             $resulttext .= '</ul>';
12183:             $mailmsgtext .= "\n";
12184:             my $cachetime = 24*60*60;
12185:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12186:             if (ref($lastactref) eq 'HASH') {
12187:                 $lastactref->{'domdefaults'} = 1;
12188:             }
12189:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
12190:                 my $notify = 1;
12191:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
12192:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
12193:                         $notify = 0;
12194:                     }
12195:                 }
12196:                 if ($notify) {
12197:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
12198:                                                "LON-CAPA Domain Settings Change - $dom",
12199:                                                $mailmsgtext);
12200:                 }
12201:             }
12202:         } else {
12203:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
12204:         }
12205:     } else {
12206:         $resulttext = '<span class="LC_error">'.
12207:             &mt('An error occurred: [_1]',$putresult).'</span>';
12208:     }
12209:     if (@errors > 0) {
12210:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
12211:         foreach my $item (@errors) {
12212:             $resulttext .= ' "'.$title->{$item}.'",';
12213:         }
12214:         $resulttext =~ s/,$//;
12215:     }
12216:     return $resulttext;
12217: }
12218: 
12219: sub modify_scantron {
12220:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
12221:     my ($resulttext,%confhash,%changes,$errors);
12222:     my $custom = 'custom.tab';
12223:     my $default = 'default.tab';
12224:     my $servadm = $r->dir_config('lonAdmEMail');
12225:     my ($configuserok,$author_ok,$switchserver) = 
12226:         &config_check($dom,$confname,$servadm);
12227:     if ($env{'form.scantronformat.filename'} ne '') {
12228:         my $error;
12229:         if ($configuserok eq 'ok') {
12230:             if ($switchserver) {
12231:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
12232:             } else {
12233:                 if ($author_ok eq 'ok') {
12234:                     my ($result,$scantronurl) =
12235:                         &publishlogo($r,'upload','scantronformat',$dom,
12236:                                      $confname,'scantron','','',$custom);
12237:                     if ($result eq 'ok') {
12238:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
12239:                         $changes{'scantronformat'} = 1;
12240:                     } else {
12241:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
12242:                     }
12243:                 } else {
12244:                     $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);
12245:                 }
12246:             }
12247:         } else {
12248:             $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);
12249:         }
12250:         if ($error) {
12251:             &Apache::lonnet::logthis($error);
12252:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12253:         }
12254:     }
12255:     if (ref($domconfig{'scantron'}) eq 'HASH') {
12256:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
12257:             if ($env{'form.scantronformat_del'}) {
12258:                 $confhash{'scantron'}{'scantronformat'} = '';
12259:                 $changes{'scantronformat'} = 1;
12260:             }
12261:         }
12262:     }
12263:     if (keys(%confhash) > 0) {
12264:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
12265:                                                  $dom);
12266:         if ($putresult eq 'ok') {
12267:             if (keys(%changes) > 0) {
12268:                 if (ref($confhash{'scantron'}) eq 'HASH') {
12269:                     $resulttext = &mt('Changes made:').'<ul>';
12270:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
12271:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
12272:                     } else {
12273:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
12274:                     }
12275:                     $resulttext .= '</ul>';
12276:                 } else {
12277:                     $resulttext = &mt('Changes made to bubblesheet format file.');
12278:                 }
12279:                 $resulttext .= '</ul>';
12280:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
12281:                 if (ref($lastactref) eq 'HASH') {
12282:                     $lastactref->{'domainconfig'} = 1;
12283:                 }
12284:             } else {
12285:                 $resulttext = &mt('No changes made to bubblesheet format file');
12286:             }
12287:         } else {
12288:             $resulttext = '<span class="LC_error">'.
12289:                 &mt('An error occurred: [_1]',$putresult).'</span>';
12290:         }
12291:     } else {
12292:         $resulttext = &mt('No changes made to bubblesheet format file'); 
12293:     }
12294:     if ($errors) {
12295:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
12296:                        $errors.'</ul>';
12297:     }
12298:     return $resulttext;
12299: }
12300: 
12301: sub modify_coursecategories {
12302:     my ($dom,$lastactref,%domconfig) = @_;
12303:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
12304:         $cathash);
12305:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
12306:     my @catitems = ('unauth','auth');
12307:     my @cattypes = ('std','domonly','codesrch','none');
12308:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
12309:         $cathash = $domconfig{'coursecategories'}{'cats'};
12310:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
12311:             $changes{'togglecats'} = 1;
12312:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
12313:         }
12314:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
12315:             $changes{'categorize'} = 1;
12316:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
12317:         }
12318:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
12319:             $changes{'togglecatscomm'} = 1;
12320:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
12321:         }
12322:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
12323:             $changes{'categorizecomm'} = 1;
12324:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
12325:         }
12326:         foreach my $item (@catitems) {
12327:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
12328:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
12329:                     $changes{$item} = 1;
12330:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
12331:                 }
12332:             }
12333:         }
12334:     } else {
12335:         $changes{'togglecats'} = 1;
12336:         $changes{'categorize'} = 1;
12337:         $changes{'togglecatscomm'} = 1;
12338:         $changes{'categorizecomm'} = 1;
12339:         $domconfig{'coursecategories'} = {
12340:                                              togglecats => $env{'form.togglecats'},
12341:                                              categorize => $env{'form.categorize'},
12342:                                              togglecatscomm => $env{'form.togglecatscomm'},
12343:                                              categorizecomm => $env{'form.categorizecomm'},
12344:                                          };
12345:         foreach my $item (@catitems) {
12346:             if ($env{'form.coursecat_'.$item} ne 'std') {
12347:                 $changes{$item} = 1;
12348:             }
12349:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
12350:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
12351:             }
12352:         }
12353:     }
12354:     if (ref($cathash) eq 'HASH') {
12355:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
12356:             push (@deletecategory,'instcode::0');
12357:         }
12358:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
12359:             push(@deletecategory,'communities::0');
12360:         }
12361:     }
12362:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
12363:     if (ref($cathash) eq 'HASH') {
12364:         if (@deletecategory > 0) {
12365:             #FIXME Need to remove category from all courses using a deleted category 
12366:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
12367:             foreach my $item (@deletecategory) {
12368:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
12369:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
12370:                     $deletions{$item} = 1;
12371:                     &recurse_cat_deletes($item,$cathash,\%deletions);
12372:                 }
12373:             }
12374:         }
12375:         foreach my $item (keys(%{$cathash})) {
12376:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
12377:             if ($cathash->{$item} ne $env{'form.'.$item}) {
12378:                 $reorderings{$item} = 1;
12379:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
12380:             }
12381:             if ($env{'form.addcategory_name_'.$item} ne '') {
12382:                 my $newcat = $env{'form.addcategory_name_'.$item};
12383:                 my $newdepth = $depth+1;
12384:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
12385:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
12386:                 $adds{$newitem} = 1; 
12387:             }
12388:             if ($env{'form.subcat_'.$item} ne '') {
12389:                 my $newcat = $env{'form.subcat_'.$item};
12390:                 my $newdepth = $depth+1;
12391:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
12392:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
12393:                 $adds{$newitem} = 1;
12394:             }
12395:         }
12396:     }
12397:     if ($env{'form.instcode'} eq '1') {
12398:         if (ref($cathash) eq 'HASH') {
12399:             my $newitem = 'instcode::0';
12400:             if ($cathash->{$newitem} eq '') {  
12401:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
12402:                 $adds{$newitem} = 1;
12403:             }
12404:         } else {
12405:             my $newitem = 'instcode::0';
12406:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
12407:             $adds{$newitem} = 1;
12408:         }
12409:     }
12410:     if ($env{'form.communities'} eq '1') {
12411:         if (ref($cathash) eq 'HASH') {
12412:             my $newitem = 'communities::0';
12413:             if ($cathash->{$newitem} eq '') {
12414:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
12415:                 $adds{$newitem} = 1;
12416:             }
12417:         } else {
12418:             my $newitem = 'communities::0';
12419:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
12420:             $adds{$newitem} = 1;
12421:         }
12422:     }
12423:     if ($env{'form.addcategory_name'} ne '') {
12424:         if (($env{'form.addcategory_name'} ne 'instcode') &&
12425:             ($env{'form.addcategory_name'} ne 'communities')) {
12426:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
12427:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
12428:             $adds{$newitem} = 1;
12429:         }
12430:     }
12431:     my $putresult;
12432:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
12433:         if (keys(%deletions) > 0) {
12434:             foreach my $key (keys(%deletions)) {
12435:                 if ($predelallitems{$key} ne '') {
12436:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
12437:                 }
12438:             }
12439:         }
12440:         my (@chkcats,@chktrails,%chkallitems);
12441:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
12442:         if (ref($chkcats[0]) eq 'ARRAY') {
12443:             my $depth = 0;
12444:             my $chg = 0;
12445:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
12446:                 my $name = $chkcats[0][$i];
12447:                 my $item;
12448:                 if ($name eq '') {
12449:                     $chg ++;
12450:                 } else {
12451:                     $item = &escape($name).'::0';
12452:                     if ($chg) {
12453:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
12454:                     }
12455:                     $depth ++; 
12456:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
12457:                     $depth --;
12458:                 }
12459:             }
12460:         }
12461:     }
12462:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
12463:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
12464:         if ($putresult eq 'ok') {
12465:             my %title = (
12466:                          togglecats     => 'Show/Hide a course in catalog',
12467:                          categorize     => 'Assign a category to a course',
12468:                          togglecatscomm => 'Show/Hide a community in catalog',
12469:                          categorizecomm => 'Assign a category to a community',
12470:                         );
12471:             my %level = (
12472:                          dom  => 'set in Domain ("Modify Course/Community")',
12473:                          crs  => 'set in Course ("Course Configuration")',
12474:                          comm => 'set in Community ("Community Configuration")',
12475:                          none     => 'No catalog',
12476:                          std      => 'Standard catalog',
12477:                          domonly  => 'Domain-only catalog',
12478:                          codesrch => 'Code search form',
12479:                         );
12480:             $resulttext = &mt('Changes made:').'<ul>';
12481:             if ($changes{'togglecats'}) {
12482:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
12483:             }
12484:             if ($changes{'categorize'}) {
12485:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
12486:             }
12487:             if ($changes{'togglecatscomm'}) {
12488:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
12489:             }
12490:             if ($changes{'categorizecomm'}) {
12491:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
12492:             }
12493:             if ($changes{'unauth'}) {
12494:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
12495:             }
12496:             if ($changes{'auth'}) {
12497:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
12498:             }
12499:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
12500:                 my $cathash;
12501:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
12502:                     $cathash = $domconfig{'coursecategories'}{'cats'};
12503:                 } else {
12504:                     $cathash = {};
12505:                 } 
12506:                 my (@cats,@trails,%allitems);
12507:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
12508:                 if (keys(%deletions) > 0) {
12509:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
12510:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
12511:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
12512:                     }
12513:                     $resulttext .= '</ul></li>';
12514:                 }
12515:                 if (keys(%reorderings) > 0) {
12516:                     my %sort_by_trail;
12517:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
12518:                     foreach my $key (keys(%reorderings)) {
12519:                         if ($allitems{$key} ne '') {
12520:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
12521:                         }
12522:                     }
12523:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
12524:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
12525:                     }
12526:                     $resulttext .= '</ul></li>';
12527:                 }
12528:                 if (keys(%adds) > 0) {
12529:                     my %sort_by_trail;
12530:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
12531:                     foreach my $key (keys(%adds)) {
12532:                         if ($allitems{$key} ne '') {
12533:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
12534:                         }
12535:                     }
12536:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
12537:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
12538:                     }
12539:                     $resulttext .= '</ul></li>';
12540:                 }
12541:             }
12542:             $resulttext .= '</ul>';
12543:             if ($changes{'unauth'} || $changes{'auth'}) {
12544:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
12545:                 if ($changes{'auth'}) {
12546:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
12547:                 }
12548:                 if ($changes{'unauth'}) {
12549:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
12550:                 }
12551:                 my $cachetime = 24*60*60;
12552:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12553:                 if (ref($lastactref) eq 'HASH') {
12554:                     $lastactref->{'domdefaults'} = 1;
12555:                 }
12556:             }
12557:         } else {
12558:             $resulttext = '<span class="LC_error">'.
12559:                           &mt('An error occurred: [_1]',$putresult).'</span>';
12560:         }
12561:     } else {
12562:         $resulttext = &mt('No changes made to course and community categories');
12563:     }
12564:     return $resulttext;
12565: }
12566: 
12567: sub modify_serverstatuses {
12568:     my ($dom,%domconfig) = @_;
12569:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
12570:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
12571:         %currserverstatus = %{$domconfig{'serverstatuses'}};
12572:     }
12573:     my @pages = &serverstatus_pages();
12574:     foreach my $type (@pages) {
12575:         $newserverstatus{$type}{'namedusers'} = '';
12576:         $newserverstatus{$type}{'machines'} = '';
12577:         if (defined($env{'form.'.$type.'_namedusers'})) {
12578:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
12579:             my @okusers;
12580:             foreach my $user (@users) {
12581:                 my ($uname,$udom) = split(/:/,$user);
12582:                 if (($udom =~ /^$match_domain$/) &&   
12583:                     (&Apache::lonnet::domain($udom)) &&
12584:                     ($uname =~ /^$match_username$/)) {
12585:                     if (!grep(/^\Q$user\E/,@okusers)) {
12586:                         push(@okusers,$user);
12587:                     }
12588:                 }
12589:             }
12590:             if (@okusers > 0) {
12591:                  @okusers = sort(@okusers);
12592:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
12593:             }
12594:         }
12595:         if (defined($env{'form.'.$type.'_machines'})) {
12596:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
12597:             my @okmachines;
12598:             foreach my $ip (@machines) {
12599:                 my @parts = split(/\./,$ip);
12600:                 next if (@parts < 4);
12601:                 my $badip = 0;
12602:                 for (my $i=0; $i<4; $i++) {
12603:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
12604:                         $badip = 1;
12605:                         last;
12606:                     }
12607:                 }
12608:                 if (!$badip) {
12609:                     push(@okmachines,$ip);     
12610:                 }
12611:             }
12612:             @okmachines = sort(@okmachines);
12613:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
12614:         }
12615:     }
12616:     my %serverstatushash =  (
12617:                                 serverstatuses => \%newserverstatus,
12618:                             );
12619:     foreach my $type (@pages) {
12620:         foreach my $setting ('namedusers','machines') {
12621:             my (@current,@new);
12622:             if (ref($currserverstatus{$type}) eq 'HASH') {
12623:                 if ($currserverstatus{$type}{$setting} ne '') { 
12624:                     @current = split(/,/,$currserverstatus{$type}{$setting});
12625:                 }
12626:             }
12627:             if ($newserverstatus{$type}{$setting} ne '') {
12628:                 @new = split(/,/,$newserverstatus{$type}{$setting});
12629:             }
12630:             if (@current > 0) {
12631:                 if (@new > 0) {
12632:                     foreach my $item (@current) {
12633:                         if (!grep(/^\Q$item\E$/,@new)) {
12634:                             $changes{$type}{$setting} = 1;
12635:                             last;
12636:                         }
12637:                     }
12638:                     foreach my $item (@new) {
12639:                         if (!grep(/^\Q$item\E$/,@current)) {
12640:                             $changes{$type}{$setting} = 1;
12641:                             last;
12642:                         }
12643:                     }
12644:                 } else {
12645:                     $changes{$type}{$setting} = 1;
12646:                 }
12647:             } elsif (@new > 0) {
12648:                 $changes{$type}{$setting} = 1;
12649:             }
12650:         }
12651:     }
12652:     if (keys(%changes) > 0) {
12653:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
12654:         my $putresult = &Apache::lonnet::put_dom('configuration',
12655:                                                  \%serverstatushash,$dom);
12656:         if ($putresult eq 'ok') {
12657:             $resulttext .= &mt('Changes made:').'<ul>';
12658:             foreach my $type (@pages) {
12659:                 if (ref($changes{$type}) eq 'HASH') {
12660:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
12661:                     if ($changes{$type}{'namedusers'}) {
12662:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
12663:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
12664:                         } else {
12665:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
12666:                         }
12667:                     }
12668:                     if ($changes{$type}{'machines'}) {
12669:                         if ($newserverstatus{$type}{'machines'} eq '') {
12670:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
12671:                         } else {
12672:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
12673:                         }
12674: 
12675:                     }
12676:                     $resulttext .= '</ul></li>';
12677:                 }
12678:             }
12679:             $resulttext .= '</ul>';
12680:         } else {
12681:             $resulttext = '<span class="LC_error">'.
12682:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
12683: 
12684:         }
12685:     } else {
12686:         $resulttext = &mt('No changes made to access to server status pages');
12687:     }
12688:     return $resulttext;
12689: }
12690: 
12691: sub modify_helpsettings {
12692:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
12693:     my ($resulttext,$errors,%changes,%helphash);
12694:     my %defaultchecked = ('submitbugs' => 'on');
12695:     my @offon = ('off','on');
12696:     my @toggles = ('submitbugs');
12697:     my %current = ('submitbugs' => '',
12698:                    'adhoc'      => {},
12699:                   );
12700:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
12701:         %current = %{$domconfig{'helpsettings'}};
12702:     }
12703:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12704:     foreach my $item (@toggles) {
12705:         if ($defaultchecked{$item} eq 'on') { 
12706:             if ($current{$item} eq '') {
12707:                 if ($env{'form.'.$item} eq '0') {
12708:                     $changes{$item} = 1;
12709:                 }
12710:             } elsif ($current{$item} ne $env{'form.'.$item}) {
12711:                 $changes{$item} = 1;
12712:             }
12713:         } elsif ($defaultchecked{$item} eq 'off') {
12714:             if ($current{$item} eq '') {
12715:                 if ($env{'form.'.$item} eq '1') {
12716:                     $changes{$item} = 1;
12717:                 }
12718:             } elsif ($current{$item} ne $env{'form.'.$item}) {
12719:                 $changes{$item} = 1;
12720:             }
12721:         }
12722:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
12723:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
12724:         }
12725:     }
12726:     my $maxnum = $env{'form.helproles_maxnum'};
12727:     my $confname = $dom.'-domainconfig';
12728:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
12729:     my (@allpos,%newsettings,%changedprivs,$newrole);
12730:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12731:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
12732:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
12733:     my %lt = &Apache::lonlocal::texthash(
12734:                     s      => 'system',
12735:                     d      => 'domain',
12736:                     order  => 'Display order',
12737:                     access => 'Role usage',
12738:                     all    => 'All with domain helpdesk or helpdesk assistant role',
12739:                     dh     => 'All with domain helpdesk role',
12740:                     da     => 'All with domain helpdesk assistant role',
12741:                     none   => 'None',
12742:                     status => 'Determined based on institutional status',
12743:                     inc    => 'Include all, but exclude specific personnel',
12744:                     exc    => 'Exclude all, but include specific personnel',
12745:     );
12746:     for (my $num=0; $num<=$maxnum; $num++) {
12747:         my ($prefix,$identifier,$rolename,%curr);
12748:         if ($num == $maxnum) {
12749:             next unless ($env{'form.newcusthelp'} == $maxnum);
12750:             $identifier = 'custhelp'.$num;
12751:             $prefix = 'helproles_'.$num;
12752:             $rolename = $env{'form.custhelpname'.$num};
12753:             $rolename=~s/[^A-Za-z0-9]//gs;
12754:             next if ($rolename eq '');
12755:             next if (exists($existing{'rolesdef_'.$rolename}));
12756:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
12757:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
12758:                                                      $newprivs{'c'},$confname,$dom);
12759:             if ($result ne 'ok') {
12760:                 $errors .= '<li><span class="LC_error">'.
12761:                            &mt('An error occurred storing the new custom role: [_1]',
12762:                            $result).'</span></li>';
12763:                 next;
12764:             } else {
12765:                 $changedprivs{$rolename} = \%newprivs;
12766:                 $newrole = $rolename;
12767:             }
12768:         } else {
12769:             $prefix = 'helproles_'.$num;
12770:             $rolename = $env{'form.'.$prefix};
12771:             next if ($rolename eq '');
12772:             next unless (exists($existing{'rolesdef_'.$rolename}));
12773:             $identifier = 'custhelp'.$num;
12774:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
12775:             my %currprivs;
12776:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
12777:                 split(/\_/,$existing{'rolesdef_'.$rolename});
12778:             foreach my $level ('c','d','s') {
12779:                 if ($newprivs{$level} ne $currprivs{$level}) {
12780:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
12781:                                                              $newprivs{'c'},$confname,$dom);
12782:                     if ($result ne 'ok') {
12783:                         $errors .= '<li><span class="LC_error">'.
12784:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
12785:                                        $rolename,$result).'</span></li>';
12786:                     } else {
12787:                         $changedprivs{$rolename} = \%newprivs;
12788:                     }
12789:                     last;
12790:                 }
12791:             }
12792:             if (ref($current{'adhoc'}) eq 'HASH') {
12793:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
12794:                     %curr = %{$current{'adhoc'}{$rolename}};
12795:                 }
12796:             }
12797:         }
12798:         my $newpos = $env{'form.'.$prefix.'_pos'};
12799:         $newpos =~ s/\D+//g;
12800:         $allpos[$newpos] = $rolename;
12801:         my $newdesc = $env{'form.'.$prefix.'_desc'};
12802:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
12803:         if ($curr{'desc'}) {
12804:             if ($curr{'desc'} ne $newdesc) {
12805:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
12806:                 $newsettings{$rolename}{'desc'} = $newdesc;
12807:             }
12808:         } elsif ($newdesc ne '') {
12809:             $changes{'customrole'}{$rolename}{'desc'} = 1;
12810:             $newsettings{$rolename}{'desc'} = $newdesc;
12811:         }
12812:         my $access = $env{'form.'.$prefix.'_access'};
12813:         if (grep(/^\Q$access\E$/,@accesstypes)) {
12814:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
12815:             if ($access eq 'status') {
12816:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
12817:                 if (scalar(@statuses) == 0) {
12818:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
12819:                 } else {
12820:                     my (@shownstatus,$numtypes);
12821:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
12822:                     if (ref($types) eq 'ARRAY') {
12823:                         $numtypes = scalar(@{$types});
12824:                         foreach my $type (sort(@statuses)) {
12825:                             if ($type eq 'default') {
12826:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
12827:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
12828:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
12829:                                 push(@shownstatus,$usertypes->{$type});
12830:                             }
12831:                         }
12832:                     }
12833:                     if (grep(/^default$/,@statuses)) {
12834:                         push(@shownstatus,$othertitle);
12835:                     }
12836:                     if (scalar(@shownstatus) == 1+$numtypes) {
12837:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
12838:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
12839:                     } else {
12840:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
12841:                         if (ref($curr{'status'}) eq 'ARRAY') {
12842:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
12843:                             if (@diffs) {
12844:                                 $changes{'customrole'}{$rolename}{$access} = 1;
12845:                             }
12846:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
12847:                             $changes{'customrole'}{$rolename}{$access} = 1;
12848:                         }
12849:                     }
12850:                 }
12851:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
12852:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
12853:                 my @newspecstaff;
12854:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
12855:                 foreach my $person (sort(@personnel)) {
12856:                     if ($domhelpdesk{$person}) {
12857:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
12858:                     }
12859:                 }
12860:                 if (ref($curr{$access}) eq 'ARRAY') {
12861:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
12862:                     if (@diffs) {
12863:                         $changes{'customrole'}{$rolename}{$access} = 1;
12864:                     }
12865:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
12866:                     $changes{'customrole'}{$rolename}{$access} = 1;
12867:                 }
12868:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
12869:                     my ($uname,$udom) = split(/:/,$person);
12870:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
12871:                 }
12872:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
12873:             }
12874:         } else {
12875:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
12876:         }
12877:         unless ($curr{'access'} eq $access) {
12878:             $changes{'customrole'}{$rolename}{'access'} = 1;
12879:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
12880:         }
12881:     }
12882:     if (@allpos > 0) {
12883:         my $idx = 0;
12884:         foreach my $rolename (@allpos) {
12885:             if ($rolename ne '') {
12886:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
12887:                 if (ref($current{'adhoc'}) eq 'HASH') {
12888:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
12889:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
12890:                             $changes{'customrole'}{$rolename}{'order'} = 1;
12891:                             $newsettings{$rolename}{'order'} = $idx+1;
12892:                         }
12893:                     }
12894:                 }
12895:                 $idx ++;
12896:             }
12897:         }
12898:     }
12899:     my $putresult;
12900:     if (keys(%changes) > 0) {
12901:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
12902:         if ($putresult eq 'ok') {
12903:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
12904:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
12905:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
12906:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
12907:                 }
12908:             }
12909:             my $cachetime = 24*60*60;
12910:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12911:             if (ref($lastactref) eq 'HASH') {
12912:                 $lastactref->{'domdefaults'} = 1;
12913:             }
12914:         } else {
12915:             $errors .= '<li><span class="LC_error">'.
12916:                        &mt('An error occurred storing the settings: [_1]',
12917:                            $putresult).'</span></li>';
12918:         }
12919:     }
12920:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
12921:         $resulttext = &mt('Changes made:').'<ul>';
12922:         my (%shownprivs,@levelorder);
12923:         @levelorder = ('c','d','s');
12924:         if ((keys(%changes)) && ($putresult eq 'ok')) {
12925:             foreach my $item (sort(keys(%changes))) {
12926:                 if ($item eq 'submitbugs') {
12927:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
12928:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
12929:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
12930:                 } elsif ($item eq 'customrole') {
12931:                     if (ref($changes{'customrole'}) eq 'HASH') {
12932:                         my @keyorder = ('order','desc','access','status','exc','inc');
12933:                         my %keytext = &Apache::lonlocal::texthash(
12934:                                                                    order  => 'Order',
12935:                                                                    desc   => 'Role description',
12936:                                                                    access => 'Role usage',
12937:                                                                    status => 'Allowed institutional types',
12938:                                                                    exc    => 'Allowed personnel',
12939:                                                                    inc    => 'Disallowed personnel',
12940:                         );
12941:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
12942:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
12943:                                 if ($role eq $newrole) {
12944:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
12945:                                                               $role).'<ul>';
12946:                                 } else {
12947:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
12948:                                                               $role).'<ul>';
12949:                                 }
12950:                                 foreach my $key (@keyorder) {
12951:                                     if ($changes{'customrole'}{$role}{$key}) {
12952:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
12953:                                                                   $keytext{$key},$newsettings{$role}{$key}).
12954:                                                        '</li>';
12955:                                     }
12956:                                 }
12957:                                 if (ref($changedprivs{$role}) eq 'HASH') {
12958:                                     $shownprivs{$role} = 1;
12959:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
12960:                                     foreach my $level (@levelorder) {
12961:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
12962:                                             next if ($item eq '');
12963:                                             my ($priv) = split(/\&/,$item,2);
12964:                                             if (&Apache::lonnet::plaintext($priv)) {
12965:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
12966:                                                 unless ($level eq 'c') {
12967:                                                     $resulttext .= ' ('.$lt{$level}.')';
12968:                                                 }
12969:                                                 $resulttext .= '</li>';
12970:                                             }
12971:                                         }
12972:                                     }
12973:                                     $resulttext .= '</ul>';
12974:                                 }
12975:                                 $resulttext .= '</ul></li>';
12976:                             }
12977:                         }
12978:                     }
12979:                 }
12980:             }
12981:         }
12982:         if (keys(%changedprivs)) {
12983:             foreach my $role (sort(keys(%changedprivs))) {
12984:                 unless ($shownprivs{$role}) {
12985:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
12986:                                               $role).'<ul>'.
12987:                                    '<li>'.&mt('Privileges set to :').'<ul>';
12988:                     foreach my $level (@levelorder) {
12989:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
12990:                             next if ($item eq '');
12991:                             my ($priv) = split(/\&/,$item,2);
12992:                             if (&Apache::lonnet::plaintext($priv)) {
12993:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
12994:                                 unless ($level eq 'c') {
12995:                                     $resulttext .= ' ('.$lt{$level}.')';
12996:                                 }
12997:                                 $resulttext .= '</li>';
12998:                             }
12999:                         }
13000:                     }
13001:                     $resulttext .= '</ul></li></ul></li>';
13002:                 }
13003:             }
13004:         }
13005:         $resulttext .= '</ul>';
13006:     } else {
13007:         $resulttext = &mt('No changes made to help settings');
13008:     }
13009:     if ($errors) {
13010:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
13011:                                     $errors.'</ul>';
13012:     }
13013:     return $resulttext;
13014: }
13015: 
13016: sub modify_coursedefaults {
13017:     my ($dom,$lastactref,%domconfig) = @_;
13018:     my ($resulttext,$errors,%changes,%defaultshash);
13019:     my %defaultchecked = (
13020:                            'uselcmath'       => 'on',
13021:                            'usejsme'         => 'on'
13022:                          );
13023:     my @toggles = ('uselcmath','usejsme');
13024:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
13025:                    'uploadquota_community','uploadquota_textbook','mysqltables_official',
13026:                    'mysqltables_unofficial','mysqltables_community','mysqltables_textbook');
13027:     my @types = ('official','unofficial','community','textbook');
13028:     my %staticdefaults = (
13029:                            anonsurvey_threshold => 10,
13030:                            uploadquota          => 500,
13031:                            postsubmit           => 60,
13032:                            mysqltables          => 172800,
13033:                          );
13034: 
13035:     $defaultshash{'coursedefaults'} = {};
13036: 
13037:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
13038:         if ($domconfig{'coursedefaults'} eq '') {
13039:             $domconfig{'coursedefaults'} = {};
13040:         }
13041:     }
13042: 
13043:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
13044:         foreach my $item (@toggles) {
13045:             if ($defaultchecked{$item} eq 'on') {
13046:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
13047:                     ($env{'form.'.$item} eq '0')) {
13048:                     $changes{$item} = 1;
13049:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
13050:                     $changes{$item} = 1;
13051:                 }
13052:             } elsif ($defaultchecked{$item} eq 'off') {
13053:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
13054:                     ($env{'form.'.$item} eq '1')) {
13055:                     $changes{$item} = 1;
13056:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
13057:                     $changes{$item} = 1;
13058:                 }
13059:             }
13060:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
13061:         }
13062:         foreach my $item (@numbers) {
13063:             my ($currdef,$newdef);
13064:             $newdef = $env{'form.'.$item};
13065:             if ($item eq 'anonsurvey_threshold') {
13066:                 $currdef = $domconfig{'coursedefaults'}{$item};
13067:                 $newdef =~ s/\D//g;
13068:                 if ($newdef eq '' || $newdef < 1) {
13069:                     $newdef = 1;
13070:                 }
13071:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
13072:             } else {
13073:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
13074:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
13075:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
13076:                 }
13077:                 $newdef =~ s/[^\w.\-]//g;
13078:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
13079:             }
13080:             if ($currdef ne $newdef) {
13081:                 my $staticdef;
13082:                 if ($item eq 'anonsurvey_threshold') {
13083:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
13084:                         $changes{$item} = 1;
13085:                     }
13086:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
13087:                     my $setting = $1;
13088:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
13089:                         $changes{$setting} = 1;
13090:                     }
13091:                 }
13092:             }
13093:         }
13094:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
13095:         my @currclonecode;
13096:         if (ref($currclone) eq 'HASH') {
13097:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
13098:                 @currclonecode = @{$currclone->{'instcode'}};
13099:             }
13100:         }
13101:         my $newclone;
13102:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
13103:             $newclone = $env{'form.canclone'};
13104:         }
13105:         if ($newclone eq 'instcode') {
13106:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
13107:             my (%codedefaults,@code_order,@clonecode);
13108:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
13109:                                                     \@code_order);
13110:             foreach my $item (@code_order) {
13111:                 if (grep(/^\Q$item\E$/,@newcodes)) {
13112:                     push(@clonecode,$item);
13113:                 }
13114:             }
13115:             if (@clonecode) {
13116:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
13117:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
13118:                 if (@diffs) {
13119:                     $changes{'canclone'} = 1;
13120:                 }
13121:             } else {
13122:                 $newclone eq '';
13123:             }
13124:         } elsif ($newclone ne '') {
13125:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
13126:         }
13127:         if ($newclone ne $currclone) {
13128:             $changes{'canclone'} = 1;
13129:         }
13130:         my %credits;
13131:         foreach my $type (@types) {
13132:             unless ($type eq 'community') {
13133:                 $credits{$type} = $env{'form.'.$type.'_credits'};
13134:                 $credits{$type} =~ s/[^\d.]+//g;
13135:             }
13136:         }
13137:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
13138:             ($env{'form.coursecredits'} eq '1')) {
13139:             $changes{'coursecredits'} = 1;
13140:             foreach my $type (keys(%credits)) {
13141:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
13142:             }
13143:         } else {
13144:             if ($env{'form.coursecredits'} eq '1') {
13145:                 foreach my $type (@types) {
13146:                     unless ($type eq 'community') {
13147:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
13148:                             $changes{'coursecredits'} = 1;
13149:                         }
13150:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
13151:                     }
13152:                 }
13153:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
13154:                 foreach my $type (@types) {
13155:                     unless ($type eq 'community') {
13156:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
13157:                             $changes{'coursecredits'} = 1;
13158:                             last;
13159:                         }
13160:                     }
13161:                 }
13162:             }
13163:         }
13164:         if ($env{'form.postsubmit'} eq '1') {
13165:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
13166:             my %currtimeout;
13167:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13168:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
13169:                     $changes{'postsubmit'} = 1;
13170:                 }
13171:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
13172:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
13173:                 }
13174:             } else {
13175:                 $changes{'postsubmit'} = 1;
13176:             }
13177:             foreach my $type (@types) {
13178:                 my $timeout = $env{'form.'.$type.'_timeout'};
13179:                 $timeout =~ s/\D//g;
13180:                 if ($timeout == $staticdefaults{'postsubmit'}) {
13181:                     $timeout = '';
13182:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
13183:                     $timeout = '0';
13184:                 }
13185:                 unless ($timeout eq '') {
13186:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
13187:                 }
13188:                 if (exists($currtimeout{$type})) {
13189:                     if ($timeout ne $currtimeout{$type}) {
13190:                         $changes{'postsubmit'} = 1;
13191:                     }
13192:                 } elsif ($timeout ne '') {
13193:                     $changes{'postsubmit'} = 1;
13194:                 }
13195:             }
13196:         } else {
13197:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
13198:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13199:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
13200:                     $changes{'postsubmit'} = 1;
13201:                 }
13202:             } else {
13203:                 $changes{'postsubmit'} = 1;
13204:             }
13205:         }
13206:     }
13207:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
13208:                                              $dom);
13209:     if ($putresult eq 'ok') {
13210:         if (keys(%changes) > 0) {
13211:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13212:             if (($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
13213:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
13214:                 ($changes{'canclone'}) || ($changes{'mysqltables'})) {
13215:                 foreach my $item ('uselcmath','usejsme') {
13216:                     if ($changes{$item}) {
13217:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
13218:                     }
13219:                 }
13220:                 if ($changes{'coursecredits'}) {
13221:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
13222:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
13223:                             $domdefaults{$type.'credits'} =
13224:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
13225:                         }
13226:                     }
13227:                 }
13228:                 if ($changes{'postsubmit'}) {
13229:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13230:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
13231:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
13232:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
13233:                                 $domdefaults{$type.'postsubtimeout'} =
13234:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
13235:                             }
13236:                         }
13237:                     }
13238:                 }
13239:                 if ($changes{'uploadquota'}) {
13240:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
13241:                         foreach my $type (@types) {
13242:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
13243:                         }
13244:                     }
13245:                 }
13246:                 if ($changes{'canclone'}) {
13247:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
13248:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
13249:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
13250:                             if (@clonecodes) {
13251:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
13252:                             }
13253:                         }
13254:                     } else {
13255:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
13256:                     }
13257:                 }
13258:                 my $cachetime = 24*60*60;
13259:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13260:                 if (ref($lastactref) eq 'HASH') {
13261:                     $lastactref->{'domdefaults'} = 1;
13262:                 }
13263:             }
13264:             $resulttext = &mt('Changes made:').'<ul>';
13265:             foreach my $item (sort(keys(%changes))) {
13266:                 if ($item eq 'uselcmath') {
13267:                     if ($env{'form.'.$item} eq '1') {
13268:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
13269:                     } else {
13270:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
13271:                     }
13272:                 } elsif ($item eq 'usejsme') {
13273:                     if ($env{'form.'.$item} eq '1') {
13274:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
13275:                     } else {
13276:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
13277:                     }
13278:                 } elsif ($item eq 'anonsurvey_threshold') {
13279:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
13280:                 } elsif ($item eq 'uploadquota') {
13281:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
13282:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
13283:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
13284:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
13285:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
13286: 
13287:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
13288:                                        '</ul>'.
13289:                                        '</li>';
13290:                     } else {
13291:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
13292:                     }
13293:                 } elsif ($item eq 'mysqltables') {
13294:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
13295:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
13296:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
13297:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
13298:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
13299:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
13300:                                        '</ul>'.
13301:                                        '</li>';
13302:                     } else {
13303:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
13304:                     }
13305:                 } elsif ($item eq 'postsubmit') {
13306:                     if ($domdefaults{'postsubmit'} eq 'off') {
13307:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
13308:                     } else {
13309:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
13310:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13311:                             $resulttext .= &mt('durations:').'<ul>';
13312:                             foreach my $type (@types) {
13313:                                 $resulttext .= '<li>';
13314:                                 my $timeout;
13315:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
13316:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
13317:                                 }
13318:                                 my $display;
13319:                                 if ($timeout eq '0') {
13320:                                     $display = &mt('unlimited');
13321:                                 } elsif ($timeout eq '') {
13322:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
13323:                                 } else {
13324:                                     $display = &mt('[quant,_1,second]',$timeout);
13325:                                 }
13326:                                 if ($type eq 'community') {
13327:                                     $resulttext .= &mt('Communities');
13328:                                 } elsif ($type eq 'official') {
13329:                                     $resulttext .= &mt('Official courses');
13330:                                 } elsif ($type eq 'unofficial') {
13331:                                     $resulttext .= &mt('Unofficial courses');
13332:                                 } elsif ($type eq 'textbook') {
13333:                                     $resulttext .= &mt('Textbook courses');
13334:                                 }
13335:                                 $resulttext .= ' -- '.$display.'</li>';
13336:                             }
13337:                             $resulttext .= '</ul>';
13338:                         }
13339:                         $resulttext .= '</li>';
13340:                     }
13341:                 } elsif ($item eq 'coursecredits') {
13342:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
13343:                         if (($domdefaults{'officialcredits'} eq '') &&
13344:                             ($domdefaults{'unofficialcredits'} eq '') &&
13345:                             ($domdefaults{'textbookcredits'} eq '')) {
13346:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
13347:                         } else {
13348:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
13349:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
13350:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
13351:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
13352:                                            '</ul>'.
13353:                                            '</li>';
13354:                         }
13355:                     } else {
13356:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
13357:                     }
13358:                 } elsif ($item eq 'canclone') {
13359:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
13360:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
13361:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
13362:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
13363:                         }
13364:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
13365:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
13366:                     } else {
13367:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
13368:                     }
13369:                 }
13370:             }
13371:             $resulttext .= '</ul>';
13372:         } else {
13373:             $resulttext = &mt('No changes made to course defaults');
13374:         }
13375:     } else {
13376:         $resulttext = '<span class="LC_error">'.
13377:             &mt('An error occurred: [_1]',$putresult).'</span>';
13378:     }
13379:     return $resulttext;
13380: }
13381: 
13382: sub modify_selfenrollment {
13383:     my ($dom,$lastactref,%domconfig) = @_;
13384:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
13385:     my @types = ('official','unofficial','community','textbook');
13386:     my %titles = &tool_titles();
13387:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
13388:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
13389:     $ordered{'default'} = ['types','registered','approval','limit'];
13390: 
13391:     my (%roles,%shown,%toplevel);
13392:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
13393: 
13394:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
13395:         if ($domconfig{'selfenrollment'} eq '') {
13396:             $domconfig{'selfenrollment'} = {};
13397:         }
13398:     }
13399:     %toplevel = (
13400:                   admin      => 'Configuration Rights',
13401:                   default    => 'Default settings',
13402:                   validation => 'Validation of self-enrollment requests',
13403:                 );
13404:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
13405: 
13406:     if (ref($ordered{'admin'}) eq 'ARRAY') {
13407:         foreach my $item (@{$ordered{'admin'}}) {
13408:             foreach my $type (@types) {
13409:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
13410:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
13411:                 } else {
13412:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
13413:                 }
13414:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
13415:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
13416:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
13417:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
13418:                             push(@{$changes{'admin'}{$type}},$item);
13419:                         }
13420:                     } else {
13421:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
13422:                             push(@{$changes{'admin'}{$type}},$item);
13423:                         }
13424:                     }
13425:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
13426:                     push(@{$changes{'admin'}{$type}},$item);
13427:                 }
13428:             }
13429:         }
13430:     }
13431: 
13432:     foreach my $item (@{$ordered{'default'}}) {
13433:         foreach my $type (@types) {
13434:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
13435:             if ($item eq 'types') {
13436:                 unless (($value eq 'all') || ($value eq 'dom')) {
13437:                     $value = '';
13438:                 }
13439:             } elsif ($item eq 'registered') {
13440:                 unless ($value eq '1') {
13441:                     $value = 0;
13442:                 }
13443:             } elsif ($item eq 'approval') {
13444:                 unless ($value =~ /^[012]$/) {
13445:                     $value = 0;
13446:                 }
13447:             } else {
13448:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
13449:                     $value = 'none';
13450:                 }
13451:             }
13452:             $selfenrollhash{'default'}{$type}{$item} = $value;
13453:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
13454:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
13455:                     if ($selfenrollhash{'default'}{$type}{$item} ne
13456:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
13457:                          push(@{$changes{'default'}{$type}},$item);
13458:                     }
13459:                 } else {
13460:                     push(@{$changes{'default'}{$type}},$item);
13461:                 }
13462:             } else {
13463:                 push(@{$changes{'default'}{$type}},$item);
13464:             }
13465:             if ($item eq 'limit') {
13466:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
13467:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
13468:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
13469:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
13470:                     }
13471:                 } else {
13472:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
13473:                 }
13474:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
13475:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
13476:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
13477:                          push(@{$changes{'default'}{$type}},'cap');
13478:                     }
13479:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
13480:                     push(@{$changes{'default'}{$type}},'cap');
13481:                 }
13482:             }
13483:         }
13484:     }
13485: 
13486:     foreach my $item (@{$itemsref}) {
13487:         if ($item eq 'fields') {
13488:             my @changed;
13489:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
13490:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
13491:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
13492:             }
13493:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
13494:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
13495:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
13496:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
13497:                 } else {
13498:                     @changed = @{$selfenrollhash{'validation'}{$item}};
13499:                 }
13500:             } else {
13501:                 @changed = @{$selfenrollhash{'validation'}{$item}};
13502:             }
13503:             if (@changed) {
13504:                 if ($selfenrollhash{'validation'}{$item}) { 
13505:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
13506:                 } else {
13507:                     $changes{'validation'}{$item} = &mt('None');
13508:                 }
13509:             }
13510:         } else {
13511:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
13512:             if ($item eq 'markup') {
13513:                if ($env{'form.selfenroll_validation_'.$item}) {
13514:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
13515:                }
13516:             }
13517:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
13518:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
13519:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
13520:                 }
13521:             }
13522:         }
13523:     }
13524: 
13525:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
13526:                                              $dom);
13527:     if ($putresult eq 'ok') {
13528:         if (keys(%changes) > 0) {
13529:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13530:             $resulttext = &mt('Changes made:').'<ul>';
13531:             foreach my $key ('admin','default','validation') {
13532:                 if (ref($changes{$key}) eq 'HASH') {
13533:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
13534:                     if ($key eq 'validation') {
13535:                         foreach my $item (@{$itemsref}) {
13536:                             if (exists($changes{$key}{$item})) {
13537:                                 if ($item eq 'markup') {
13538:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
13539:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
13540:                                 } else {  
13541:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
13542:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
13543:                                 }
13544:                             }
13545:                         }
13546:                     } else {
13547:                         foreach my $type (@types) {
13548:                             if ($type eq 'community') {
13549:                                 $roles{'1'} = &mt('Community personnel');
13550:                             } else {
13551:                                 $roles{'1'} = &mt('Course personnel');
13552:                             }
13553:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
13554:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
13555:                                     if ($key eq 'admin') {
13556:                                         my @mgrdc = ();
13557:                                         if (ref($ordered{$key}) eq 'ARRAY') {
13558:                                             foreach my $item (@{$ordered{'admin'}}) {
13559:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
13560:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
13561:                                                         push(@mgrdc,$item);
13562:                                                     }
13563:                                                 }
13564:                                             }
13565:                                             if (@mgrdc) {
13566:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
13567:                                             } else {
13568:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
13569:                                             }
13570:                                         }
13571:                                     } else {
13572:                                         if (ref($ordered{$key}) eq 'ARRAY') {
13573:                                             foreach my $item (@{$ordered{$key}}) {
13574:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
13575:                                                     $domdefaults{$type.'selfenroll'.$item} =
13576:                                                         $selfenrollhash{$key}{$type}{$item};
13577:                                                 }
13578:                                             }
13579:                                         }
13580:                                     }
13581:                                 }
13582:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
13583:                                 foreach my $item (@{$ordered{$key}}) {
13584:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
13585:                                         $resulttext .= '<li>';
13586:                                         if ($key eq 'admin') {
13587:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
13588:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
13589:                                         } else {
13590:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
13591:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
13592:                                         }
13593:                                         $resulttext .= '</li>';
13594:                                     }
13595:                                 }
13596:                                 $resulttext .= '</ul></li>';
13597:                             }
13598:                         }
13599:                         $resulttext .= '</ul></li>'; 
13600:                     }
13601:                 }
13602:                 if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
13603:                     my $cachetime = 24*60*60;
13604:                     &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13605:                     if (ref($lastactref) eq 'HASH') {
13606:                         $lastactref->{'domdefaults'} = 1;
13607:                     }
13608:                 }
13609:             }
13610:             $resulttext .= '</ul>';
13611:         } else {
13612:             $resulttext = &mt('No changes made to self-enrollment settings');
13613:         }
13614:     } else {
13615:         $resulttext = '<span class="LC_error">'.
13616:             &mt('An error occurred: [_1]',$putresult).'</span>';
13617:     }
13618:     return $resulttext;
13619: }
13620: 
13621: sub modify_usersessions {
13622:     my ($dom,$lastactref,%domconfig) = @_;
13623:     my @hostingtypes = ('version','excludedomain','includedomain');
13624:     my @offloadtypes = ('primary','default');
13625:     my %types = (
13626:                   remote => \@hostingtypes,
13627:                   hosted => \@hostingtypes,
13628:                   spares => \@offloadtypes,
13629:                 );
13630:     my @prefixes = ('remote','hosted','spares');
13631:     my @lcversions = &Apache::lonnet::all_loncaparevs();
13632:     my (%by_ip,%by_location,@intdoms);
13633:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
13634:     my @locations = sort(keys(%by_location));
13635:     my (%defaultshash,%changes);
13636:     foreach my $prefix (@prefixes) {
13637:         $defaultshash{'usersessions'}{$prefix} = {};
13638:     }
13639:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13640:     my $resulttext;
13641:     my %iphost = &Apache::lonnet::get_iphost();
13642:     foreach my $prefix (@prefixes) {
13643:         next if ($prefix eq 'spares');
13644:         foreach my $type (@{$types{$prefix}}) {
13645:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
13646:             if ($type eq 'version') {
13647:                 my $value = $env{'form.'.$prefix.'_'.$type};
13648:                 my $okvalue;
13649:                 if ($value ne '') {
13650:                     if (grep(/^\Q$value\E$/,@lcversions)) {
13651:                         $okvalue = $value;
13652:                     }
13653:                 }
13654:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
13655:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
13656:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
13657:                             if ($inuse == 0) {
13658:                                 $changes{$prefix}{$type} = 1;
13659:                             } else {
13660:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
13661:                                     $changes{$prefix}{$type} = 1;
13662:                                 }
13663:                                 if ($okvalue ne '') {
13664:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13665:                                 } 
13666:                             }
13667:                         } else {
13668:                             if (($inuse == 1) && ($okvalue ne '')) {
13669:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13670:                                 $changes{$prefix}{$type} = 1;
13671:                             }
13672:                         }
13673:                     } else {
13674:                         if (($inuse == 1) && ($okvalue ne '')) {
13675:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13676:                             $changes{$prefix}{$type} = 1;
13677:                         }
13678:                     }
13679:                 } else {
13680:                     if (($inuse == 1) && ($okvalue ne '')) {
13681:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13682:                         $changes{$prefix}{$type} = 1;
13683:                     }
13684:                 }
13685:             } else {
13686:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
13687:                 my @okvals;
13688:                 foreach my $val (@vals) {
13689:                     if ($val =~ /:/) {
13690:                         my @items = split(/:/,$val);
13691:                         foreach my $item (@items) {
13692:                             if (ref($by_location{$item}) eq 'ARRAY') {
13693:                                 push(@okvals,$item);
13694:                             }
13695:                         }
13696:                     } else {
13697:                         if (ref($by_location{$val}) eq 'ARRAY') {
13698:                             push(@okvals,$val);
13699:                         }
13700:                     }
13701:                 }
13702:                 @okvals = sort(@okvals);
13703:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
13704:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
13705:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
13706:                             if ($inuse == 0) {
13707:                                 $changes{$prefix}{$type} = 1; 
13708:                             } else {
13709:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13710:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
13711:                                 if (@changed > 0) {
13712:                                     $changes{$prefix}{$type} = 1;
13713:                                 }
13714:                             }
13715:                         } else {
13716:                             if ($inuse == 1) {
13717:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13718:                                 $changes{$prefix}{$type} = 1;
13719:                             }
13720:                         } 
13721:                     } else {
13722:                         if ($inuse == 1) {
13723:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13724:                             $changes{$prefix}{$type} = 1;
13725:                         }
13726:                     }
13727:                 } else {
13728:                     if ($inuse == 1) {
13729:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13730:                         $changes{$prefix}{$type} = 1;
13731:                     }
13732:                 }
13733:             }
13734:         }
13735:     }
13736: 
13737:     my @alldoms = &Apache::lonnet::all_domains();
13738:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
13739:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
13740:     my $savespares;
13741: 
13742:     foreach my $lonhost (sort(keys(%servers))) {
13743:         my $serverhomeID =
13744:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
13745:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
13746:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
13747:         my %spareschg;
13748:         foreach my $type (@{$types{'spares'}}) {
13749:             my @okspares;
13750:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
13751:             foreach my $server (@checked) {
13752:                 if (&Apache::lonnet::hostname($server) ne '') {
13753:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
13754:                         unless (grep(/^\Q$server\E$/,@okspares)) {
13755:                             push(@okspares,$server);
13756:                         }
13757:                     }
13758:                 }
13759:             }
13760:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
13761:             my $newspare;
13762:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
13763:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
13764:                     $newspare = $new;
13765:                 }
13766:             }
13767:             my @spares;
13768:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
13769:                 @spares = sort(@okspares,$newspare);
13770:             } else {
13771:                 @spares = sort(@okspares);
13772:             }
13773:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
13774:             if (ref($spareid{$lonhost}) eq 'HASH') {
13775:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
13776:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
13777:                     if (@diffs > 0) {
13778:                         $spareschg{$type} = 1;
13779:                     }
13780:                 }
13781:             }
13782:         }
13783:         if (keys(%spareschg) > 0) {
13784:             $changes{'spares'}{$lonhost} = \%spareschg;
13785:         }
13786:     }
13787:     $defaultshash{'usersessions'}{'offloadnow'} = {};
13788:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
13789:     my @okoffload;
13790:     if (@offloadnow) {
13791:         foreach my $server (@offloadnow) {
13792:             if (&Apache::lonnet::hostname($server) ne '') {
13793:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
13794:                     push(@okoffload,$server);
13795:                 }
13796:             }
13797:         }
13798:         if (@okoffload) {
13799:             foreach my $lonhost (@okoffload) {
13800:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
13801:             }
13802:         }
13803:     }
13804:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
13805:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
13806:             if (ref($changes{'spares'}) eq 'HASH') {
13807:                 if (keys(%{$changes{'spares'}}) > 0) {
13808:                     $savespares = 1;
13809:                 }
13810:             }
13811:         } else {
13812:             $savespares = 1;
13813:         }
13814:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
13815:             foreach my $lonhost (keys(%{$domconfig{'usersessions'}{'offloadnow'}})) {
13816:                 unless ($defaultshash{'usersessions'}{'offloadnow'}{$lonhost}) {
13817:                     $changes{'offloadnow'} = 1;
13818:                     last;
13819:                 }
13820:             }
13821:             unless ($changes{'offloadnow'}) {
13822:                 foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{'offloadnow'}})) {
13823:                     unless ($domconfig{'usersessions'}{'offloadnow'}{$lonhost}) {
13824:                         $changes{'offloadnow'} = 1;
13825:                         last;
13826:                     }
13827:                 }
13828:             }
13829:         } elsif (@okoffload) {
13830:             $changes{'offloadnow'} = 1;
13831:         }
13832:     } elsif (@okoffload) {
13833:         $changes{'offloadnow'} = 1;
13834:     }
13835:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
13836:     if ((keys(%changes) > 0) || ($savespares)) {
13837:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
13838:                                                  $dom);
13839:         if ($putresult eq 'ok') {
13840:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
13841:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
13842:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
13843:                 }
13844:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
13845:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
13846:                 }
13847:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
13848:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
13849:                 }
13850:             }
13851:             my $cachetime = 24*60*60;
13852:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13853:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
13854:             if (ref($lastactref) eq 'HASH') {
13855:                 $lastactref->{'domdefaults'} = 1;
13856:                 $lastactref->{'usersessions'} = 1;
13857:             }
13858:             if (keys(%changes) > 0) {
13859:                 my %lt = &usersession_titles();
13860:                 $resulttext = &mt('Changes made:').'<ul>';
13861:                 foreach my $prefix (@prefixes) {
13862:                     if (ref($changes{$prefix}) eq 'HASH') {
13863:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
13864:                         if ($prefix eq 'spares') {
13865:                             if (ref($changes{$prefix}) eq 'HASH') {
13866:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
13867:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
13868:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
13869:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
13870:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
13871:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
13872:                                         foreach my $type (@{$types{$prefix}}) {
13873:                                             if ($changes{$prefix}{$lonhost}{$type}) {
13874:                                                 my $offloadto = &mt('None');
13875:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
13876:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
13877:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
13878:                                                     }
13879:                                                 }
13880:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
13881:                                             }
13882:                                         }
13883:                                     }
13884:                                     $resulttext .= '</li>';
13885:                                 }
13886:                             }
13887:                         } else {
13888:                             foreach my $type (@{$types{$prefix}}) {
13889:                                 if (defined($changes{$prefix}{$type})) {
13890:                                     my $newvalue;
13891:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
13892:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
13893:                                             if ($type eq 'version') {
13894:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
13895:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
13896:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
13897:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
13898:                                                 }
13899:                                             }
13900:                                         }
13901:                                     }
13902:                                     if ($newvalue eq '') {
13903:                                         if ($type eq 'version') {
13904:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
13905:                                         } else {
13906:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
13907:                                         }
13908:                                     } else {
13909:                                         if ($type eq 'version') {
13910:                                             $newvalue .= ' '.&mt('(or later)'); 
13911:                                         }
13912:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
13913:                                     }
13914:                                 }
13915:                             }
13916:                         }
13917:                         $resulttext .= '</ul>';
13918:                     }
13919:                 }
13920:                 if ($changes{'offloadnow'}) {
13921:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
13922:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
13923:                             $resulttext .= '<li>'.&mt('Switch active users on next access, for server(s):').'<ul>';
13924:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
13925:                                 $resulttext .= '<li>'.$lonhost.'</li>';
13926:                             }
13927:                             $resulttext .= '</ul>';
13928:                         } else {
13929:                             $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.');
13930:                         }
13931:                     } else {
13932:                         $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.').'</li>';
13933:                     }
13934:                 }
13935:                 $resulttext .= '</ul>';
13936:             } else {
13937:                 $resulttext = $nochgmsg;
13938:             }
13939:         } else {
13940:             $resulttext = '<span class="LC_error">'.
13941:                           &mt('An error occurred: [_1]',$putresult).'</span>';
13942:         }
13943:     } else {
13944:         $resulttext = $nochgmsg;
13945:     }
13946:     return $resulttext;
13947: }
13948: 
13949: sub modify_loadbalancing {
13950:     my ($dom,%domconfig) = @_;
13951:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
13952:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
13953:     my ($othertitle,$usertypes,$types) =
13954:         &Apache::loncommon::sorted_inst_types($dom);
13955:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
13956:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
13957:     my @sparestypes = ('primary','default');
13958:     my %typetitles = &sparestype_titles();
13959:     my $resulttext;
13960:     my (%currbalancer,%currtargets,%currrules,%existing);
13961:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
13962:         %existing = %{$domconfig{'loadbalancing'}};
13963:     }
13964:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
13965:                               \%currtargets,\%currrules);
13966:     my ($saveloadbalancing,%defaultshash,%changes);
13967:     my ($alltypes,$othertypes,$titles) =
13968:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
13969:     my %ruletitles = &offloadtype_text();
13970:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
13971:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
13972:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
13973:         if ($balancer eq '') {
13974:             next;
13975:         }
13976:         if (!exists($servers{$balancer})) {
13977:             if (exists($currbalancer{$balancer})) {
13978:                 push(@{$changes{'delete'}},$balancer);
13979:             }
13980:             next;
13981:         }
13982:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
13983:             push(@{$changes{'delete'}},$balancer);
13984:             next;
13985:         }
13986:         if (!exists($currbalancer{$balancer})) {
13987:             push(@{$changes{'add'}},$balancer);
13988:         }
13989:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
13990:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
13991:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
13992:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
13993:             $saveloadbalancing = 1;
13994:         }
13995:         foreach my $sparetype (@sparestypes) {
13996:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
13997:             my @offloadto;
13998:             foreach my $target (@targets) {
13999:                 if (($servers{$target}) && ($target ne $balancer)) {
14000:                     if ($sparetype eq 'default') {
14001:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
14002:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
14003:                         }
14004:                     }
14005:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
14006:                         push(@offloadto,$target);
14007:                     }
14008:                 }
14009:             }
14010:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
14011:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
14012:                     push(@offloadto,$balancer);
14013:                 }
14014:             }
14015:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
14016:         }
14017:         if (ref($currtargets{$balancer}) eq 'HASH') {
14018:             foreach my $sparetype (@sparestypes) {
14019:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
14020:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
14021:                     if (@targetdiffs > 0) {
14022:                         $changes{'curr'}{$balancer}{'targets'} = 1;
14023:                     }
14024:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14025:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14026:                         $changes{'curr'}{$balancer}{'targets'} = 1;
14027:                     }
14028:                 }
14029:             }
14030:         } else {
14031:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
14032:                 foreach my $sparetype (@sparestypes) {
14033:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14034:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14035:                             $changes{'curr'}{$balancer}{'targets'} = 1;
14036:                         }
14037:                     }
14038:                 }
14039:             }
14040:         }
14041:         my $ishomedom;
14042:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
14043:             $ishomedom = 1;
14044:         }
14045:         if (ref($alltypes) eq 'ARRAY') {
14046:             foreach my $type (@{$alltypes}) {
14047:                 my $rule;
14048:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
14049:                          (!$ishomedom)) {
14050:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
14051:                 }
14052:                 if ($rule eq 'specific') {
14053:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
14054:                     if (exists($servers{$specifiedhost})) {
14055:                         $rule = $specifiedhost;
14056:                     }
14057:                 }
14058:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
14059:                 if (ref($currrules{$balancer}) eq 'HASH') {
14060:                     if ($rule ne $currrules{$balancer}{$type}) {
14061:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
14062:                     }
14063:                 } elsif ($rule ne '') {
14064:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
14065:                 }
14066:             }
14067:         }
14068:     }
14069:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
14070:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
14071:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
14072:             $defaultshash{'loadbalancing'} = {};
14073:         }
14074:         my $putresult = &Apache::lonnet::put_dom('configuration',
14075:                                                  \%defaultshash,$dom);
14076:         if ($putresult eq 'ok') {
14077:             if (keys(%changes) > 0) {
14078:                 my %toupdate;
14079:                 if (ref($changes{'delete'}) eq 'ARRAY') {
14080:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
14081:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
14082:                         $toupdate{$balancer} = 1;
14083:                     }
14084:                 }
14085:                 if (ref($changes{'add'}) eq 'ARRAY') {
14086:                     foreach my $balancer (sort(@{$changes{'add'}})) {
14087:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
14088:                         $toupdate{$balancer} = 1;
14089:                     }
14090:                 }
14091:                 if (ref($changes{'curr'}) eq 'HASH') {
14092:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
14093:                         $toupdate{$balancer} = 1;
14094:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
14095:                             if ($changes{'curr'}{$balancer}{'targets'}) {
14096:                                 my %offloadstr;
14097:                                 foreach my $sparetype (@sparestypes) {
14098:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14099:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14100:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
14101:                                         }
14102:                                     }
14103:                                 }
14104:                                 if (keys(%offloadstr) == 0) {
14105:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
14106:                                 } else {
14107:                                     my $showoffload;
14108:                                     foreach my $sparetype (@sparestypes) {
14109:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
14110:                                         if (defined($offloadstr{$sparetype})) {
14111:                                             $showoffload .= $offloadstr{$sparetype};
14112:                                         } else {
14113:                                             $showoffload .= &mt('None');
14114:                                         }
14115:                                         $showoffload .= ('&nbsp;'x3);
14116:                                     }
14117:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
14118:                                 }
14119:                             }
14120:                         }
14121:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
14122:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
14123:                                 foreach my $type (@{$alltypes}) {
14124:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
14125:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
14126:                                         my $balancetext;
14127:                                         if ($rule eq '') {
14128:                                             $balancetext =  $ruletitles{'default'};
14129:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
14130:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
14131:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
14132:                                                 foreach my $sparetype (@sparestypes) {
14133:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14134:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
14135:                                                     }
14136:                                                 }
14137:                                                 foreach my $item (@{$alltypes}) {
14138:                                                     next if ($item =~  /^_LC_ipchange/);
14139:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
14140:                                                     if ($hasrule eq 'homeserver') {
14141:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
14142:                                                     } else {
14143:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
14144:                                                             if ($servers{$hasrule}) {
14145:                                                                 $toupdate{$hasrule} = 1;
14146:                                                             }
14147:                                                         }
14148:                                                     }
14149:                                                 }
14150:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
14151:                                                     $balancetext =  $ruletitles{$rule};
14152:                                                 } else {
14153:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
14154:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
14155:                                                     if ($receiver) {
14156:                                                         $toupdate{$receiver};
14157:                                                     }
14158:                                                 }
14159:                                             } else {
14160:                                                 $balancetext =  $ruletitles{$rule};
14161:                                             }
14162:                                         } else {
14163:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
14164:                                         }
14165:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
14166:                                     }
14167:                                 }
14168:                             }
14169:                         }
14170:                         if (keys(%toupdate)) {
14171:                             my %thismachine;
14172:                             my $updatedhere;
14173:                             my $cachetime = 60*60*24;
14174:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
14175:                             foreach my $lonhost (keys(%toupdate)) {
14176:                                 if ($thismachine{$lonhost}) {
14177:                                     unless ($updatedhere) {
14178:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
14179:                                                                       $defaultshash{'loadbalancing'},
14180:                                                                       $cachetime);
14181:                                         $updatedhere = 1;
14182:                                     }
14183:                                 } else {
14184:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
14185:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
14186:                                 }
14187:                             }
14188:                         }
14189:                     }
14190:                 }
14191:                 if ($resulttext ne '') {
14192:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
14193:                 } else {
14194:                     $resulttext = $nochgmsg;
14195:                 }
14196:             } else {
14197:                 $resulttext = $nochgmsg;
14198:             }
14199:         } else {
14200:             $resulttext = '<span class="LC_error">'.
14201:                           &mt('An error occurred: [_1]',$putresult).'</span>';
14202:         }
14203:     } else {
14204:         $resulttext = $nochgmsg;
14205:     }
14206:     return $resulttext;
14207: }
14208: 
14209: sub recurse_check {
14210:     my ($chkcats,$categories,$depth,$name) = @_;
14211:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
14212:         my $chg = 0;
14213:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
14214:             my $category = $chkcats->[$depth]{$name}[$j];
14215:             my $item;
14216:             if ($category eq '') {
14217:                 $chg ++;
14218:             } else {
14219:                 my $deeper = $depth + 1;
14220:                 $item = &escape($category).':'.&escape($name).':'.$depth;
14221:                 if ($chg) {
14222:                     $categories->{$item} -= $chg;
14223:                 }
14224:                 &recurse_check($chkcats,$categories,$deeper,$category);
14225:                 $deeper --;
14226:             }
14227:         }
14228:     }
14229:     return;
14230: }
14231: 
14232: sub recurse_cat_deletes {
14233:     my ($item,$coursecategories,$deletions) = @_;
14234:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
14235:     my $subdepth = $depth + 1;
14236:     if (ref($coursecategories) eq 'HASH') {
14237:         foreach my $subitem (keys(%{$coursecategories})) {
14238:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
14239:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
14240:                 delete($coursecategories->{$subitem});
14241:                 $deletions->{$subitem} = 1;
14242:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
14243:             }
14244:         }
14245:     }
14246:     return;
14247: }
14248: 
14249: sub active_dc_picker {
14250:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
14251:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
14252:     my @domcoord = keys(%domcoords);
14253:     if (keys(%currhash)) {
14254:         foreach my $dc (keys(%currhash)) {
14255:             unless (exists($domcoords{$dc})) {
14256:                 push(@domcoord,$dc);
14257:             }
14258:         }
14259:     }
14260:     @domcoord = sort(@domcoord);
14261:     my $numdcs = scalar(@domcoord);
14262:     my $rows = 0;
14263:     my $table;
14264:     if ($numdcs > 1) {
14265:         $table = '<table>';
14266:         for (my $i=0; $i<@domcoord; $i++) {
14267:             my $rem = $i%($numinrow);
14268:             if ($rem == 0) {
14269:                 if ($i > 0) {
14270:                     $table .= '</tr>';
14271:                 }
14272:                 $table .= '<tr>';
14273:                 $rows ++;
14274:             }
14275:             my $check = '';
14276:             if ($inputtype eq 'radio') {
14277:                 if (keys(%currhash) == 0) {
14278:                     if (!$i) {
14279:                         $check = ' checked="checked"';
14280:                     }
14281:                 } elsif (exists($currhash{$domcoord[$i]})) {
14282:                     $check = ' checked="checked"';
14283:                 }
14284:             } else {
14285:                 if (exists($currhash{$domcoord[$i]})) {
14286:                     $check = ' checked="checked"';
14287:                 }
14288:             }
14289:             if ($i == @domcoord - 1) {
14290:                 my $colsleft = $numinrow - $rem;
14291:                 if ($colsleft > 1) {
14292:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
14293:                 } else {
14294:                     $table .= '<td class="LC_left_item">';
14295:                 }
14296:             } else {
14297:                 $table .= '<td class="LC_left_item">';
14298:             }
14299:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
14300:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
14301:             $table .= '<span class="LC_nobreak"><label>'.
14302:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
14303:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
14304:             if ($user ne $dcname.':'.$dcdom) {
14305:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
14306:             }
14307:             $table .= '</label></span></td>';
14308:         }
14309:         $table .= '</tr></table>';
14310:     } elsif ($numdcs == 1) {
14311:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
14312:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
14313:         if ($inputtype eq 'radio') {
14314:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
14315:             if ($user ne $dcname.':'.$dcdom) {
14316:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
14317:             }
14318:         } else {
14319:             my $check;
14320:             if (exists($currhash{$domcoord[0]})) {
14321:                 $check = ' checked="checked"';
14322:             }
14323:             $table = '<span class="LC_nobreak"><label>'.
14324:                      '<input type="checkbox" name="'.$name.'" '.
14325:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
14326:             if ($user ne $dcname.':'.$dcdom) {
14327:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
14328:             }
14329:             $table .= '</label></span>';
14330:             $rows ++;
14331:         }
14332:     }
14333:     return ($numdcs,$table,$rows);
14334: }
14335: 
14336: sub usersession_titles {
14337:     return &Apache::lonlocal::texthash(
14338:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
14339:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
14340:                spares => 'Servers offloaded to, when busy',
14341:                version => 'LON-CAPA version requirement',
14342:                excludedomain => 'Allow all, but exclude specific domains',
14343:                includedomain => 'Deny all, but include specific domains',
14344:                primary => 'Primary (checked first)',
14345:                default => 'Default',
14346:            );
14347: }
14348: 
14349: sub id_for_thisdom {
14350:     my (%servers) = @_;
14351:     my %altids;
14352:     foreach my $server (keys(%servers)) {
14353:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
14354:         if ($serverhome ne $server) {
14355:             $altids{$serverhome} = $server;
14356:         }
14357:     }
14358:     return %altids;
14359: }
14360: 
14361: sub count_servers {
14362:     my ($currbalancer,%servers) = @_;
14363:     my (@spares,$numspares);
14364:     foreach my $lonhost (sort(keys(%servers))) {
14365:         next if ($currbalancer eq $lonhost);
14366:         push(@spares,$lonhost);
14367:     }
14368:     if ($currbalancer) {
14369:         $numspares = scalar(@spares);
14370:     } else {
14371:         $numspares = scalar(@spares) - 1;
14372:     }
14373:     return ($numspares,@spares);
14374: }
14375: 
14376: sub lonbalance_targets_js {
14377:     my ($dom,$types,$servers,$settings) = @_;
14378:     my $select = &mt('Select');
14379:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
14380:     if (ref($servers) eq 'HASH') {
14381:         $alltargets = join("','",sort(keys(%{$servers})));
14382:         my @homedoms;
14383:         foreach my $server (sort(keys(%{$servers}))) {
14384:             if (&Apache::lonnet::host_domain($server) eq $dom) {
14385:                 push(@homedoms,'1');
14386:             } else {
14387:                 push(@homedoms,'0');
14388:             }
14389:         }
14390:         $allishome = join("','",@homedoms);
14391:     }
14392:     if (ref($types) eq 'ARRAY') {
14393:         if (@{$types} > 0) {
14394:             @alltypes = @{$types};
14395:         }
14396:     }
14397:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
14398:     $allinsttypes = join("','",@alltypes);
14399:     my (%currbalancer,%currtargets,%currrules,%existing);
14400:     if (ref($settings) eq 'HASH') {
14401:         %existing = %{$settings};
14402:     }
14403:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
14404:                               \%currtargets,\%currrules);
14405:     my $balancers = join("','",sort(keys(%currbalancer)));
14406:     return <<"END";
14407: 
14408: <script type="text/javascript">
14409: // <![CDATA[
14410: 
14411: currBalancers = new Array('$balancers');
14412: 
14413: function toggleTargets(balnum) {
14414:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
14415:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
14416:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
14417:     var prevbalancer = prevhostitem.value;
14418:     var baltotal = document.getElementById('loadbalancing_total').value;
14419:     prevhostitem.value = balancer;
14420:     if (prevbalancer != '') {
14421:         var prevIdx = currBalancers.indexOf(prevbalancer);
14422:         if (prevIdx != -1) {
14423:             currBalancers.splice(prevIdx,1);
14424:         }
14425:     }
14426:     if (balancer == '') {
14427:         hideSpares(balnum);
14428:     } else {
14429:         var currIdx = currBalancers.indexOf(balancer);
14430:         if (currIdx == -1) {
14431:             currBalancers.push(balancer);
14432:         }
14433:         var homedoms = new Array('$allishome');
14434:         var ishomedom = homedoms[lonhostitem.selectedIndex];
14435:         showSpares(balancer,ishomedom,balnum);
14436:     }
14437:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
14438:     return;
14439: }
14440: 
14441: function showSpares(balancer,ishomedom,balnum) {
14442:     var alltargets = new Array('$alltargets');
14443:     var insttypes = new Array('$allinsttypes');
14444:     var offloadtypes = new Array('primary','default');
14445: 
14446:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
14447:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
14448:  
14449:     for (var i=0; i<offloadtypes.length; i++) {
14450:         var count = 0;
14451:         for (var j=0; j<alltargets.length; j++) {
14452:             if (alltargets[j] != balancer) {
14453:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
14454:                 item.value = alltargets[j];
14455:                 item.style.textAlign='left';
14456:                 item.style.textFace='normal';
14457:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
14458:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
14459:                     item.disabled = '';
14460:                 } else {
14461:                     item.disabled = 'disabled';
14462:                     item.checked = false;
14463:                 }
14464:                 count ++;
14465:             }
14466:         }
14467:     }
14468:     for (var k=0; k<insttypes.length; k++) {
14469:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
14470:             if (ishomedom == 1) {
14471:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
14472:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
14473:             } else {
14474:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
14475:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
14476:             }
14477:         } else {
14478:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
14479:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
14480:         }
14481:         if ((insttypes[k] != '_LC_external') && 
14482:             ((insttypes[k] != '_LC_internetdom') ||
14483:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
14484:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
14485:             item.options.length = 0;
14486:             item.options[0] = new Option("","",true,true);
14487:             var idx = 0;
14488:             for (var m=0; m<alltargets.length; m++) {
14489:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
14490:                     idx ++;
14491:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
14492:                 }
14493:             }
14494:         }
14495:     }
14496:     return;
14497: }
14498: 
14499: function hideSpares(balnum) {
14500:     var alltargets = new Array('$alltargets');
14501:     var insttypes = new Array('$allinsttypes');
14502:     var offloadtypes = new Array('primary','default');
14503: 
14504:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
14505:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
14506: 
14507:     var total = alltargets.length - 1;
14508:     for (var i=0; i<offloadtypes; i++) {
14509:         for (var j=0; j<total; j++) {
14510:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
14511:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
14512:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
14513:         }
14514:     }
14515:     for (var k=0; k<insttypes.length; k++) {
14516:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
14517:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
14518:         if (insttypes[k] != '_LC_external') {
14519:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
14520:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
14521:         }
14522:     }
14523:     return;
14524: }
14525: 
14526: function checkOffloads(item,balnum,type) {
14527:     var alltargets = new Array('$alltargets');
14528:     var offloadtypes = new Array('primary','default');
14529:     if (item.checked) {
14530:         var total = alltargets.length - 1;
14531:         var other;
14532:         if (type == offloadtypes[0]) {
14533:             other = offloadtypes[1];
14534:         } else {
14535:             other = offloadtypes[0];
14536:         }
14537:         for (var i=0; i<total; i++) {
14538:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
14539:             if (server == item.value) {
14540:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
14541:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
14542:                 }
14543:             }
14544:         }
14545:     }
14546:     return;
14547: }
14548: 
14549: function singleServerToggle(balnum,type) {
14550:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
14551:     if (offloadtoSelIdx == 0) {
14552:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
14553:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
14554: 
14555:     } else {
14556:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
14557:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
14558:     }
14559:     return;
14560: }
14561: 
14562: function balanceruleChange(formname,balnum,type) {
14563:     if (type == '_LC_external') {
14564:         return;
14565:     }
14566:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
14567:     for (var i=0; i<typesRules.length; i++) {
14568:         if (formname.elements[typesRules[i]].checked) {
14569:             if (formname.elements[typesRules[i]].value != 'specific') {
14570:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
14571:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
14572:             } else {
14573:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
14574:             }
14575:         }
14576:     }
14577:     return;
14578: }
14579: 
14580: function balancerDeleteChange(balnum) {
14581:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
14582:     var baltotal = document.getElementById('loadbalancing_total').value;
14583:     var addtarget;
14584:     var removetarget;
14585:     var action = 'delete';
14586:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
14587:         var lonhost = hostitem.value;
14588:         var currIdx = currBalancers.indexOf(lonhost);
14589:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
14590:             if (currIdx != -1) {
14591:                 currBalancers.splice(currIdx,1);
14592:             }
14593:             addtarget = lonhost;
14594:         } else {
14595:             if (currIdx == -1) {
14596:                 currBalancers.push(lonhost);
14597:             }
14598:             removetarget = lonhost;
14599:             action = 'undelete';
14600:         }
14601:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
14602:     }
14603:     return;
14604: }
14605: 
14606: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
14607:     if (baltotal > 1) {
14608:         var offloadtypes = new Array('primary','default');
14609:         var alltargets = new Array('$alltargets');
14610:         var insttypes = new Array('$allinsttypes');
14611:         for (var i=0; i<baltotal; i++) {
14612:             if (i != balnum) {
14613:                 for (var j=0; j<offloadtypes.length; j++) {
14614:                     var total = alltargets.length - 1;
14615:                     for (var k=0; k<total; k++) {
14616:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
14617:                         var server = serveritem.value;
14618:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
14619:                             if (server == addtarget) {
14620:                                 serveritem.disabled = '';
14621:                             }
14622:                         }
14623:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
14624:                             if (server == removetarget) {
14625:                                 serveritem.disabled = 'disabled';
14626:                                 serveritem.checked = false;
14627:                             }
14628:                         }
14629:                     }
14630:                 }
14631:                 for (var j=0; j<insttypes.length; j++) {
14632:                     if (insttypes[j] != '_LC_external') {
14633:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
14634:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
14635:                             var currSel = singleserver.selectedIndex;
14636:                             var currVal = singleserver.options[currSel].value;
14637:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
14638:                                 var numoptions = singleserver.options.length;
14639:                                 var needsnew = 1;
14640:                                 for (var k=0; k<numoptions; k++) {
14641:                                     if (singleserver.options[k] == addtarget) {
14642:                                         needsnew = 0;
14643:                                         break;
14644:                                     }
14645:                                 }
14646:                                 if (needsnew == 1) {
14647:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
14648:                                 }
14649:                             }
14650:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
14651:                                 singleserver.options.length = 0;
14652:                                 if ((currVal) && (currVal != removetarget)) {
14653:                                     singleserver.options[0] = new Option("","",false,false);
14654:                                 } else {
14655:                                     singleserver.options[0] = new Option("","",true,true);
14656:                                 }
14657:                                 var idx = 0;
14658:                                 for (var m=0; m<alltargets.length; m++) {
14659:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
14660:                                         idx ++;
14661:                                         if (currVal == alltargets[m]) {
14662:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
14663:                                         } else {
14664:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
14665:                                         }
14666:                                     }
14667:                                 }
14668:                             }
14669:                         }
14670:                     }
14671:                 }
14672:             }
14673:         }
14674:     }
14675:     return;
14676: }
14677: 
14678: // ]]>
14679: </script>
14680: 
14681: END
14682: }
14683: 
14684: sub new_spares_js {
14685:     my @sparestypes = ('primary','default');
14686:     my $types = join("','",@sparestypes);
14687:     my $select = &mt('Select');
14688:     return <<"END";
14689: 
14690: <script type="text/javascript">
14691: // <![CDATA[
14692: 
14693: function updateNewSpares(formname,lonhost) {
14694:     var types = new Array('$types');
14695:     var include = new Array();
14696:     var exclude = new Array();
14697:     for (var i=0; i<types.length; i++) {
14698:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
14699:         for (var j=0; j<spareboxes.length; j++) {
14700:             if (formname.elements[spareboxes[j]].checked) {
14701:                 exclude.push(formname.elements[spareboxes[j]].value);
14702:             } else {
14703:                 include.push(formname.elements[spareboxes[j]].value);
14704:             }
14705:         }
14706:     }
14707:     for (var i=0; i<types.length; i++) {
14708:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
14709:         var selIdx = newSpare.selectedIndex;
14710:         var currnew = newSpare.options[selIdx].value;
14711:         var okSpares = new Array();
14712:         for (var j=0; j<newSpare.options.length; j++) {
14713:             var possible = newSpare.options[j].value;
14714:             if (possible != '') {
14715:                 if (exclude.indexOf(possible) == -1) {
14716:                     okSpares.push(possible);
14717:                 } else {
14718:                     if (currnew == possible) {
14719:                         selIdx = 0;
14720:                     }
14721:                 }
14722:             }
14723:         }
14724:         for (var k=0; k<include.length; k++) {
14725:             if (okSpares.indexOf(include[k]) == -1) {
14726:                 okSpares.push(include[k]);
14727:             }
14728:         }
14729:         okSpares.sort();
14730:         newSpare.options.length = 0;
14731:         if (selIdx == 0) {
14732:             newSpare.options[0] = new Option("$select","",true,true);
14733:         } else {
14734:             newSpare.options[0] = new Option("$select","",false,false);
14735:         }
14736:         for (var m=0; m<okSpares.length; m++) {
14737:             var idx = m+1;
14738:             var selThis = 0;
14739:             if (selIdx != 0) {
14740:                 if (okSpares[m] == currnew) {
14741:                     selThis = 1;
14742:                 }
14743:             }
14744:             if (selThis == 1) {
14745:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
14746:             } else {
14747:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
14748:             }
14749:         }
14750:     }
14751:     return;
14752: }
14753: 
14754: function checkNewSpares(lonhost,type) {
14755:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
14756:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
14757:     if (chosen != '') { 
14758:         var othertype;
14759:         var othernewSpare;
14760:         if (type == 'primary') {
14761:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
14762:         }
14763:         if (type == 'default') {
14764:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
14765:         }
14766:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
14767:             othernewSpare.selectedIndex = 0;
14768:         }
14769:     }
14770:     return;
14771: }
14772: 
14773: // ]]>
14774: </script>
14775: 
14776: END
14777: 
14778: }
14779: 
14780: sub common_domprefs_js {
14781:     return <<"END";
14782: 
14783: <script type="text/javascript">
14784: // <![CDATA[
14785: 
14786: function getIndicesByName(formname,item) {
14787:     var group = new Array();
14788:     for (var i=0;i<formname.elements.length;i++) {
14789:         if (formname.elements[i].name == item) {
14790:             group.push(formname.elements[i].id);
14791:         }
14792:     }
14793:     return group;
14794: }
14795: 
14796: // ]]>
14797: </script>
14798: 
14799: END
14800: 
14801: }
14802: 
14803: sub recaptcha_js {
14804:     my %lt = &captcha_phrases();
14805:     return <<"END";
14806: 
14807: <script type="text/javascript">
14808: // <![CDATA[
14809: 
14810: function updateCaptcha(caller,context) {
14811:     var privitem;
14812:     var pubitem;
14813:     var privtext;
14814:     var pubtext;
14815:     var versionitem;
14816:     var versiontext;
14817:     if (document.getElementById(context+'_recaptchapub')) {
14818:         pubitem = document.getElementById(context+'_recaptchapub');
14819:     } else {
14820:         return;
14821:     }
14822:     if (document.getElementById(context+'_recaptchapriv')) {
14823:         privitem = document.getElementById(context+'_recaptchapriv');
14824:     } else {
14825:         return;
14826:     }
14827:     if (document.getElementById(context+'_recaptchapubtxt')) {
14828:         pubtext = document.getElementById(context+'_recaptchapubtxt');
14829:     } else {
14830:         return;
14831:     }
14832:     if (document.getElementById(context+'_recaptchaprivtxt')) {
14833:         privtext = document.getElementById(context+'_recaptchaprivtxt');
14834:     } else {
14835:         return;
14836:     }
14837:     if (document.getElementById(context+'_recaptchaversion')) {
14838:         versionitem = document.getElementById(context+'_recaptchaversion');
14839:     } else {
14840:         return;
14841:     }
14842:     if (document.getElementById(context+'_recaptchavertxt')) {
14843:         versiontext = document.getElementById(context+'_recaptchavertxt');
14844:     } else {
14845:         return;
14846:     }
14847:     if (caller.checked) {
14848:         if (caller.value == 'recaptcha') {
14849:             pubitem.type = 'text';
14850:             privitem.type = 'text';
14851:             pubitem.size = '40';
14852:             privitem.size = '40';
14853:             pubtext.innerHTML = "$lt{'pub'}";
14854:             privtext.innerHTML = "$lt{'priv'}";
14855:             versionitem.type = 'text';
14856:             versionitem.size = '3';
14857:             versiontext.innerHTML = "$lt{'ver'}";
14858:         } else {
14859:             pubitem.type = 'hidden';
14860:             privitem.type = 'hidden';
14861:             versionitem.type = 'hidden';
14862:             pubtext.innerHTML = '';
14863:             privtext.innerHTML = '';
14864:             versiontext.innerHTML = '';
14865:         }
14866:     }
14867:     return;
14868: }
14869: 
14870: // ]]>
14871: </script>
14872: 
14873: END
14874: 
14875: }
14876: 
14877: sub toggle_display_js {
14878:     return <<"END";
14879: 
14880: <script type="text/javascript">
14881: // <![CDATA[
14882: 
14883: function toggleDisplay(domForm,caller) {
14884:     if (document.getElementById(caller)) {
14885:         var divitem = document.getElementById(caller);
14886:         var optionsElement = domForm.coursecredits;
14887:         var checkval = 1;
14888:         var dispval = 'block';
14889:         if (caller == 'emailoptions') {
14890:             optionsElement = domForm.cancreate_email; 
14891:         }
14892:         if (caller == 'studentsubmission') {
14893:             optionsElement = domForm.postsubmit;
14894:         }
14895:         if (caller == 'cloneinstcode') {
14896:             optionsElement = domForm.canclone;
14897:             checkval = 'instcode';
14898:         }
14899:         if (optionsElement.length) {
14900:             var currval;
14901:             for (var i=0; i<optionsElement.length; i++) {
14902:                 if (optionsElement[i].checked) {
14903:                    currval = optionsElement[i].value;
14904:                 }
14905:             }
14906:             if (currval == checkval) {
14907:                 divitem.style.display = dispval;
14908:             } else {
14909:                 divitem.style.display = 'none';
14910:             }
14911:         }
14912:     }
14913:     return;
14914: }
14915: 
14916: // ]]>
14917: </script>
14918: 
14919: END
14920: 
14921: }
14922: 
14923: sub captcha_phrases {
14924:     return &Apache::lonlocal::texthash (
14925:                  priv => 'Private key',
14926:                  pub  => 'Public key',
14927:                  original  => 'original (CAPTCHA)',
14928:                  recaptcha => 'successor (ReCAPTCHA)',
14929:                  notused   => 'unused',
14930:                  ver => 'ReCAPTCHA version (1 or 2)',
14931:     );
14932: }
14933: 
14934: sub devalidate_remote_domconfs {
14935:     my ($dom,$cachekeys) = @_;
14936:     return unless (ref($cachekeys) eq 'HASH');
14937:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
14938:     my %thismachine;
14939:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
14940:     my @posscached = ('domainconfig','domdefaults','ltitools','usersessions','directorysrch');
14941:     if (keys(%servers)) {
14942:         foreach my $server (keys(%servers)) {
14943:             next if ($thismachine{$server});
14944:             my @cached;
14945:             foreach my $name (@posscached) {
14946:                 if ($cachekeys->{$name}) {
14947:                     push(@cached,&escape($name).':'.&escape($dom));
14948:                 }
14949:             }
14950:             if (@cached) {
14951:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
14952:             }
14953:         }
14954:     }
14955:     return;
14956: }
14957: 
14958: 1;

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