File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.84.2.4: download - view: text, annotated - select for diffs
Sun Oct 15 18:51:15 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 1.315, and 1.160.6.85.

    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.4 2017/10/15 18:51:15 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:     if (ref($types) eq 'ARRAY') {
 5255:         unshift(@alltypes,@{$types},'default');
 5256:     }
 5257:     my %titles;
 5258:     foreach my $type (@alltypes) {
 5259:         if ($type =~ /^_LC_/) {
 5260:             $titles{$type} = $othertypes{$type};
 5261:         } elsif ($type eq 'default') {
 5262:             $titles{$type} = &mt('All users from [_1]',$dom);
 5263:             if (ref($types) eq 'ARRAY') {
 5264:                 if (@{$types} > 0) {
 5265:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 5266:                 }
 5267:             }
 5268:         } elsif (ref($usertypes) eq 'HASH') {
 5269:             $titles{$type} = $usertypes->{$type};
 5270:         }
 5271:     }
 5272:     return (\@alltypes,\%othertypes,\%titles);
 5273: }
 5274: 
 5275: sub loadbalance_rule_row {
 5276:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 5277:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 5278:     my @rulenames;
 5279:     my %ruletitles = &offloadtype_text();
 5280:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 5281:         @rulenames = ('balancer','offloadedto','specific');
 5282:     } else {
 5283:         @rulenames = ('default','homeserver');
 5284:         if ($type eq '_LC_external') {
 5285:             push(@rulenames,'externalbalancer');
 5286:         } else {
 5287:             push(@rulenames,'specific');
 5288:         }
 5289:         push(@rulenames,'none');
 5290:     }
 5291:     my $style = $targets_div_style;
 5292:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5293:         $style = $homedom_div_style;
 5294:     }
 5295:     my $space;
 5296:     if ($islast && $num == 1) {
 5297:         $space = '<div display="inline-block">&nbsp;</div>';
 5298:     }
 5299:     my $output =
 5300:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 5301:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 5302:         '<td valaign="top">'.$space.
 5303:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 5304:     for (my $i=0; $i<@rulenames; $i++) {
 5305:         my $rule = $rulenames[$i];
 5306:         my ($checked,$extra);
 5307:         if ($rulenames[$i] eq 'default') {
 5308:             $rule = '';
 5309:         }
 5310:         if ($rulenames[$i] eq 'specific') {
 5311:             if (ref($servers) eq 'HASH') {
 5312:                 my $default;
 5313:                 if (($current ne '') && (exists($servers->{$current}))) {
 5314:                     $checked = ' checked="checked"';
 5315:                 }
 5316:                 unless ($checked) {
 5317:                     $default = ' selected="selected"';
 5318:                 }
 5319:                 $extra =
 5320:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5321:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5322:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 5323:                     '<option value=""'.$default.'></option>'."\n";
 5324:                 foreach my $server (sort(keys(%{$servers}))) {
 5325:                     if (ref($currbalancer) eq 'HASH') {
 5326:                         next if (exists($currbalancer->{$server}));
 5327:                     }
 5328:                     my $selected;
 5329:                     if ($server eq $current) {
 5330:                         $selected = ' selected="selected"';
 5331:                     }
 5332:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 5333:                 }
 5334:                 $extra .= '</select>';
 5335:             }
 5336:         } elsif ($rule eq $current) {
 5337:             $checked = ' checked="checked"';
 5338:         }
 5339:         $output .= '<span class="LC_nobreak"><label>'.
 5340:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 5341:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 5342:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 5343:                    ')"'.$checked.' />&nbsp;';
 5344:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 5345:             $output .= $ruletitles{'particular'};
 5346:         } else {
 5347:             $output .= $ruletitles{$rulenames[$i]};
 5348:         }
 5349:         $output .= '</label>'.$extra.'</span><br />'."\n";
 5350:     }
 5351:     $output .= '</div></td></tr>'."\n";
 5352:     return $output;
 5353: }
 5354: 
 5355: sub offloadtype_text {
 5356:     my %ruletitles = &Apache::lonlocal::texthash (
 5357:            'default'          => 'Offloads to default destinations',
 5358:            'homeserver'       => "Offloads to user's home server",
 5359:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 5360:            'specific'         => 'Offloads to specific server',
 5361:            'none'             => 'No offload',
 5362:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 5363:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 5364:            'particular'       => 'Session hosted (after re-auth) on server:',
 5365:     );
 5366:     return %ruletitles;
 5367: }
 5368: 
 5369: sub sparestype_titles {
 5370:     my %typestitles = &Apache::lonlocal::texthash (
 5371:                           'primary' => 'primary',
 5372:                           'default' => 'default',
 5373:                       );
 5374:     return %typestitles;
 5375: }
 5376: 
 5377: sub contact_titles {
 5378:     my %titles = &Apache::lonlocal::texthash (
 5379:                    'supportemail'    => 'Support E-mail address',
 5380:                    'adminemail'      => 'Default Server Admin E-mail address',
 5381:                    'errormail'       => 'Error reports to be e-mailed to',
 5382:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 5383:                    'helpdeskmail'    => "Helpdesk requests from all users in this domain",
 5384:                    'otherdomsmail'   => 'Helpdesk requests from users in other (unconfigured) domains',
 5385:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 5386:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 5387:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 5388:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 5389:                  );
 5390:     my %short_titles = &Apache::lonlocal::texthash (
 5391:                            adminemail   => 'Admin E-mail address',
 5392:                            supportemail => 'Support E-mail',
 5393:                        );   
 5394:     return (\%titles,\%short_titles);
 5395: }
 5396: 
 5397: sub helpform_fields {
 5398:     my %titles =  &Apache::lonlocal::texthash (
 5399:                        'username'   => 'Name',
 5400:                        'user'       => 'Username/domain',
 5401:                        'phone'      => 'Phone',
 5402:                        'cc'         => 'Cc e-mail',
 5403:                        'course'     => 'Course Details',
 5404:                        'section'    => 'Sections',
 5405:                        'screenshot' => 'File upload',
 5406:     );
 5407:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 5408:     my %possoptions = (
 5409:                         username     => ['yes','no','req'],
 5410:                         phone        => ['yes','no','req'],
 5411:                         user         => ['yes','no'],
 5412:                         cc           => ['yes','no'],
 5413:                         course       => ['yes','no'],
 5414:                         section      => ['yes','no'],
 5415:                         screenshot   => ['yes','no'],
 5416:                       );
 5417:     my %fieldoptions = &Apache::lonlocal::texthash (
 5418:                          'yes'  => 'Optional',
 5419:                          'req'  => 'Required',
 5420:                          'no'   => "Not shown",
 5421:     );
 5422:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 5423: }
 5424: 
 5425: sub tool_titles {
 5426:     my %titles = &Apache::lonlocal::texthash (
 5427:                      aboutme    => 'Personal web page',
 5428:                      blog       => 'Blog',
 5429:                      webdav     => 'WebDAV',
 5430:                      portfolio  => 'Portfolio',
 5431:                      official   => 'Official courses (with institutional codes)',
 5432:                      unofficial => 'Unofficial courses',
 5433:                      community  => 'Communities',
 5434:                      textbook   => 'Textbook courses',
 5435:                  );
 5436:     return %titles;
 5437: }
 5438: 
 5439: sub courserequest_titles {
 5440:     my %titles = &Apache::lonlocal::texthash (
 5441:                                    official   => 'Official',
 5442:                                    unofficial => 'Unofficial',
 5443:                                    community  => 'Communities',
 5444:                                    textbook   => 'Textbook',
 5445:                                    norequest  => 'Not allowed',
 5446:                                    approval   => 'Approval by Dom. Coord.',
 5447:                                    validate   => 'With validation',
 5448:                                    autolimit  => 'Numerical limit',
 5449:                                    unlimited  => '(blank for unlimited)',
 5450:                  );
 5451:     return %titles;
 5452: }
 5453: 
 5454: sub authorrequest_titles {
 5455:     my %titles = &Apache::lonlocal::texthash (
 5456:                                    norequest  => 'Not allowed',
 5457:                                    approval   => 'Approval by Dom. Coord.',
 5458:                                    automatic  => 'Automatic approval',
 5459:                  );
 5460:     return %titles;
 5461: }
 5462: 
 5463: sub courserequest_conditions {
 5464:     my %conditions = &Apache::lonlocal::texthash (
 5465:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 5466:        validate   => '(Processing of request subject to institutional validation).',
 5467:                  );
 5468:     return %conditions;
 5469: }
 5470: 
 5471: 
 5472: sub print_usercreation {
 5473:     my ($position,$dom,$settings,$rowtotal) = @_;
 5474:     my $numinrow = 4;
 5475:     my $datatable;
 5476:     if ($position eq 'top') {
 5477:         $$rowtotal ++;
 5478:         my $rowcount = 0;
 5479:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 5480:         if (ref($rules) eq 'HASH') {
 5481:             if (keys(%{$rules}) > 0) {
 5482:                 $datatable .= &user_formats_row('username',$settings,$rules,
 5483:                                                 $ruleorder,$numinrow,$rowcount);
 5484:                 $$rowtotal ++;
 5485:                 $rowcount ++;
 5486:             }
 5487:         }
 5488:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 5489:         if (ref($idrules) eq 'HASH') {
 5490:             if (keys(%{$idrules}) > 0) {
 5491:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 5492:                                                 $idruleorder,$numinrow,$rowcount);
 5493:                 $$rowtotal ++;
 5494:                 $rowcount ++;
 5495:             }
 5496:         }
 5497:         if ($rowcount == 0) {
 5498:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 5499:             $$rowtotal ++;
 5500:             $rowcount ++;
 5501:         }
 5502:     } elsif ($position eq 'middle') {
 5503:         my @creators = ('author','course','requestcrs');
 5504:         my ($rules,$ruleorder) =
 5505:             &Apache::lonnet::inst_userrules($dom,'username');
 5506:         my %lt = &usercreation_types();
 5507:         my %checked;
 5508:         if (ref($settings) eq 'HASH') {
 5509:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 5510:                 foreach my $item (@creators) {
 5511:                     $checked{$item} = $settings->{'cancreate'}{$item};
 5512:                 }
 5513:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 5514:                 foreach my $item (@creators) {
 5515:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 5516:                         $checked{$item} = 'none';
 5517:                     }
 5518:                 }
 5519:             }
 5520:         }
 5521:         my $rownum = 0;
 5522:         foreach my $item (@creators) {
 5523:             $rownum ++;
 5524:             if ($checked{$item} eq '') {
 5525:                 $checked{$item} = 'any';
 5526:             }
 5527:             my $css_class;
 5528:             if ($rownum%2) {
 5529:                 $css_class = '';
 5530:             } else {
 5531:                 $css_class = ' class="LC_odd_row" ';
 5532:             }
 5533:             $datatable .= '<tr'.$css_class.'>'.
 5534:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 5535:                          '</span></td><td align="right">';
 5536:             my @options = ('any');
 5537:             if (ref($rules) eq 'HASH') {
 5538:                 if (keys(%{$rules}) > 0) {
 5539:                     push(@options,('official','unofficial'));
 5540:                 }
 5541:             }
 5542:             push(@options,'none');
 5543:             foreach my $option (@options) {
 5544:                 my $type = 'radio';
 5545:                 my $check = ' ';
 5546:                 if ($checked{$item} eq $option) {
 5547:                     $check = ' checked="checked" ';
 5548:                 } 
 5549:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5550:                               '<input type="'.$type.'" name="can_createuser_'.
 5551:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 5552:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 5553:             }
 5554:             $datatable .= '</td></tr>';
 5555:         }
 5556:     } else {
 5557:         my @contexts = ('author','course','domain');
 5558:         my @authtypes = ('int','krb4','krb5','loc');
 5559:         my %checked;
 5560:         if (ref($settings) eq 'HASH') {
 5561:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 5562:                 foreach my $item (@contexts) {
 5563:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 5564:                         foreach my $auth (@authtypes) {
 5565:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 5566:                                 $checked{$item}{$auth} = ' checked="checked" ';
 5567:                             }
 5568:                         }
 5569:                     }
 5570:                 }
 5571:             }
 5572:         } else {
 5573:             foreach my $item (@contexts) {
 5574:                 foreach my $auth (@authtypes) {
 5575:                     $checked{$item}{$auth} = ' checked="checked" ';
 5576:                 }
 5577:             }
 5578:         }
 5579:         my %title = &context_names();
 5580:         my %authname = &authtype_names();
 5581:         my $rownum = 0;
 5582:         my $css_class; 
 5583:         foreach my $item (@contexts) {
 5584:             if ($rownum%2) {
 5585:                 $css_class = '';
 5586:             } else {
 5587:                 $css_class = ' class="LC_odd_row" ';
 5588:             }
 5589:             $datatable .=   '<tr'.$css_class.'>'.
 5590:                             '<td>'.$title{$item}.
 5591:                             '</td><td class="LC_left_item">'.
 5592:                             '<span class="LC_nobreak">';
 5593:             foreach my $auth (@authtypes) {
 5594:                 $datatable .= '<label>'. 
 5595:                               '<input type="checkbox" name="'.$item.'_auth" '.
 5596:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 5597:                               $authname{$auth}.'</label>&nbsp;';
 5598:             }
 5599:             $datatable .= '</span></td></tr>';
 5600:             $rownum ++;
 5601:         }
 5602:         $$rowtotal += $rownum;
 5603:     }
 5604:     return $datatable;
 5605: }
 5606: 
 5607: sub print_selfcreation {
 5608:     my ($position,$dom,$settings,$rowtotal) = @_;
 5609:     my (@selfcreate,$createsettings,$processing,$datatable);
 5610:     if (ref($settings) eq 'HASH') {
 5611:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 5612:             $createsettings = $settings->{'cancreate'};
 5613:             if (ref($createsettings) eq 'HASH') {
 5614:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 5615:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 5616:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 5617:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 5618:                         @selfcreate = ('email','login','sso');
 5619:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 5620:                         @selfcreate = ($createsettings->{'selfcreate'});
 5621:                     }
 5622:                 }
 5623:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 5624:                     $processing = $createsettings->{'selfcreateprocessing'};
 5625:                 }
 5626:             }
 5627:         }
 5628:     }
 5629:     my %radiohash;
 5630:     my $numinrow = 4;
 5631:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 5632:     if ($position eq 'top') {
 5633:         my %choices = &Apache::lonlocal::texthash (
 5634:                                                       cancreate_login      => 'Institutional Login',
 5635:                                                       cancreate_sso        => 'Institutional Single Sign On',
 5636:                                                   );
 5637:         my @toggles = sort(keys(%choices));
 5638:         my %defaultchecked = (
 5639:                                'cancreate_login' => 'off',
 5640:                                'cancreate_sso'   => 'off',
 5641:                              );
 5642:         my ($onclick,$itemcount);
 5643:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5644:                                                      \%choices,$itemcount,$onclick);
 5645:         $$rowtotal += $itemcount;
 5646:         
 5647:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5648: 
 5649:         if (ref($usertypes) eq 'HASH') {
 5650:             if (keys(%{$usertypes}) > 0) {
 5651:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 5652:                                              $dom,$numinrow,$othertitle,
 5653:                                              'statustocreate',$rowtotal);
 5654:                 $$rowtotal ++;
 5655:             }
 5656:         }
 5657:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 5658:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5659:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 5660:         my $rem;
 5661:         my $numperrow = 2;
 5662:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 5663:         $datatable .= '<tr'.$css_class.'>'.
 5664:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 5665:                      '<td class="LC_left_item">'."\n".
 5666:                      '<table><tr><td>'."\n";
 5667:         for (my $i=0; $i<@fields; $i++) {
 5668:             $rem = $i%($numperrow);
 5669:             if ($rem == 0) {
 5670:                 if ($i > 0) {
 5671:                     $datatable .= '</tr>';
 5672:                 }
 5673:                 $datatable .= '<tr>';
 5674:             }
 5675:             my $currval;
 5676:             if (ref($createsettings) eq 'HASH') {
 5677:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 5678:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 5679:                 }
 5680:             }
 5681:             $datatable .= '<td class="LC_left_item">'.
 5682:                           '<span class="LC_nobreak">'.
 5683:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 5684:                           'value="'.$currval.'" size="10" />&nbsp;'.
 5685:                           $fieldtitles{$fields[$i]}.'</span></td>';
 5686:         }
 5687:         my $colsleft = $numperrow - $rem;
 5688:         if ($colsleft > 1 ) {
 5689:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5690:                          '&nbsp;</td>';
 5691:         } elsif ($colsleft == 1) {
 5692:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 5693:         }
 5694:         $datatable .= '</tr></table></td></tr>';
 5695:         $$rowtotal ++;
 5696:     } elsif ($position eq 'middle') {
 5697:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 5698:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5699:         $usertypes->{'default'} = $othertitle;
 5700:         if (ref($types) eq 'ARRAY') {
 5701:             push(@{$types},'default');
 5702:             $usertypes->{'default'} = $othertitle;
 5703:             foreach my $status (@{$types}) {
 5704:                 $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 5705:                                                        $numinrow,$$rowtotal,$usertypes);
 5706:                 $$rowtotal ++;
 5707:             }
 5708:         }
 5709:     } else {
 5710:         my %choices = &Apache::lonlocal::texthash (
 5711:                                                       cancreate_email => 'E-mail address as username',
 5712:                                                   );
 5713:         my @toggles = sort(keys(%choices));
 5714:         my %defaultchecked = (
 5715:                                'cancreate_email' => 'off',
 5716:                              );
 5717:         my $itemcount = 0;
 5718:         my $display = 'none';
 5719:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 5720:             $display = 'block';
 5721:         }
 5722:         my $onclick = "toggleDisplay(this.form,'emailoptions');";
 5723:         my $additional = '<div id="emailoptions" style="display: '.$display.'">';
 5724:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5725:         my $usertypes = {};
 5726:         my $order = [];
 5727:         if ((ref($domdefaults{'inststatustypes'}) eq 'HASH') && (ref($domdefaults{'inststatusguest'}) eq 'ARRAY')) {
 5728:             $usertypes = $domdefaults{'inststatustypes'};
 5729:             $order = $domdefaults{'inststatusguest'};
 5730:         }
 5731:         if (ref($order) eq 'ARRAY') {
 5732:             push(@{$order},'default');
 5733:             if (@{$order} > 1) {
 5734:                 $usertypes->{'default'} = &mt('Other users');
 5735:                 $additional .= '<table><tr>';
 5736:                 foreach my $status (@{$order}) {
 5737:                     $additional .= '<th>'.$usertypes->{$status}.'</th>';
 5738:                 }
 5739:                 $additional .= '</tr><tr>';
 5740:                 foreach my $status (@{$order}) {
 5741:                     $additional .= '<td>'.&email_as_username($rowtotal,$processing,$status).'</td>';
 5742:                 }
 5743:                 $additional .= '</tr></table>';
 5744:             } else {
 5745:                 $usertypes->{'default'} = &mt('All users');
 5746:                 $additional .= &email_as_username($rowtotal,$processing);
 5747:             }
 5748:         }
 5749:         $additional .= '</div>'."\n";
 5750: 
 5751:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5752:                                                      \%choices,$$rowtotal,$onclick,$additional);
 5753:         $$rowtotal ++;
 5754:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal);
 5755:         $$rowtotal ++;
 5756:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 5757:         $numinrow = 1;
 5758:         if (ref($order) eq 'ARRAY') {
 5759:             foreach my $status (@{$order}) {
 5760:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 5761:                                                        $numinrow,$$rowtotal,$usertypes,$infofields,$infotitles);
 5762:                 $$rowtotal ++;
 5763:             }
 5764:         }
 5765:         my ($emailrules,$emailruleorder) =
 5766:             &Apache::lonnet::inst_userrules($dom,'email');
 5767:         if (ref($emailrules) eq 'HASH') {
 5768:             if (keys(%{$emailrules}) > 0) {
 5769:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 5770:                                                 $emailruleorder,$numinrow,$$rowtotal);
 5771:                 $$rowtotal ++;
 5772:             }
 5773:         }
 5774:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal);
 5775:     }
 5776:     return $datatable;
 5777: }
 5778: 
 5779: sub email_as_username {
 5780:     my ($rowtotal,$processing,$type) = @_;
 5781:     my %choices =
 5782:         &Apache::lonlocal::texthash (
 5783:                                       automatic => 'Automatic approval',
 5784:                                       approval  => 'Queued for approval',
 5785:                                     );
 5786:     my $output;
 5787:     foreach my $option ('automatic','approval') {
 5788:         my $checked;
 5789:         if (ref($processing) eq 'HASH') {
 5790:             if ($type eq '') {   
 5791:                 if (!exists($processing->{'default'})) {
 5792:                     if ($option eq 'automatic') {
 5793:                         $checked = ' checked="checked"';
 5794:                     }
 5795:                 } else {
 5796:                     if ($processing->{'default'} eq $option) {
 5797:                         $checked = ' checked="checked"';
 5798:                     }
 5799:                 }
 5800:             } else {
 5801:                 if (!exists($processing->{$type})) {
 5802:                     if ($option eq 'automatic') {
 5803:                         $checked = ' checked="checked"';
 5804:                     }
 5805:                 } else {
 5806:                     if ($processing->{$type} eq $option) {
 5807:                         $checked = ' checked="checked"';
 5808:                     }
 5809:                 }
 5810:             }
 5811:         } elsif ($option eq 'automatic') {
 5812:             $checked = ' checked="checked"'; 
 5813:         }
 5814:         my $name = 'cancreate_emailprocess';
 5815:         if (($type ne '') && ($type ne 'default')) {
 5816:             $name .= '_'.$type;
 5817:         }
 5818:         $output .= '<span class="LC_nobreak"><label>'.
 5819:                    '<input type="radio" name="'.$name.'"'.
 5820:                    $checked.' value="'.$option.'" />'.
 5821:                    $choices{$option}.'</label></span>';
 5822:         if ($type eq '') {
 5823:             $output .= '&nbsp;';
 5824:         } else {
 5825:             $output .= '<br />';
 5826:         }
 5827:     }
 5828:     $$rowtotal ++;
 5829:     return $output;
 5830: }
 5831: 
 5832: sub captcha_choice {
 5833:     my ($context,$settings,$itemcount) = @_;
 5834:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 5835:         $vertext,$currver); 
 5836:     my %lt = &captcha_phrases();
 5837:     $keyentry = 'hidden';
 5838:     if ($context eq 'cancreate') {
 5839:         $rowname = &mt('CAPTCHA validation');
 5840:     } elsif ($context eq 'login') {
 5841:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 5842:     }
 5843:     if (ref($settings) eq 'HASH') {
 5844:         if ($settings->{'captcha'}) {
 5845:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 5846:         } else {
 5847:             $checked{'original'} = ' checked="checked"';
 5848:         }
 5849:         if ($settings->{'captcha'} eq 'recaptcha') {
 5850:             $pubtext = $lt{'pub'};
 5851:             $privtext = $lt{'priv'};
 5852:             $keyentry = 'text';
 5853:             $vertext = $lt{'ver'};
 5854:             $currver = $settings->{'recaptchaversion'};
 5855:             if ($currver ne '2') {
 5856:                 $currver = 1;
 5857:             }
 5858:         }
 5859:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 5860:             $currpub = $settings->{'recaptchakeys'}{'public'};
 5861:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 5862:         }
 5863:     } else {
 5864:         $checked{'original'} = ' checked="checked"';
 5865:     }
 5866:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5867:     my $output = '<tr'.$css_class.'>'.
 5868:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 5869:                  '<table><tr><td>'."\n";
 5870:     foreach my $option ('original','recaptcha','notused') {
 5871:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 5872:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 5873:                    $lt{$option}.'</label></span>';
 5874:         unless ($option eq 'notused') {
 5875:             $output .= ('&nbsp;'x2)."\n";
 5876:         }
 5877:     }
 5878: #
 5879: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 5880: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 5881: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 5882: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 5883: #
 5884:     $output .= '</td></tr>'."\n".
 5885:                '<tr><td>'."\n".
 5886:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 5887:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 5888:                $currpub.'" size="40" /></span><br />'."\n".
 5889:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 5890:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 5891:                $currpriv.'" size="40" /></span><br />'.
 5892:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 5893:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 5894:                $currver.'" size="3" /></span><br />'.
 5895:                '</td></tr></table>'."\n".
 5896:                '</td></tr>';
 5897:     return $output;
 5898: }
 5899: 
 5900: sub user_formats_row {
 5901:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 5902:     my $output;
 5903:     my %text = (
 5904:                    'username' => 'new usernames',
 5905:                    'id'       => 'IDs',
 5906:                    'email'    => 'self-created accounts (e-mail)',
 5907:                );
 5908:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 5909:     $output = '<tr '.$css_class.'>'.
 5910:               '<td><span class="LC_nobreak">';
 5911:     if ($type eq 'email') {
 5912:         $output .= &mt("Formats disallowed for $text{$type}: ");
 5913:     } else {
 5914:         $output .= &mt("Format rules to check for $text{$type}: ");
 5915:     }
 5916:     $output .= '</span></td>'.
 5917:                '<td class="LC_left_item" colspan="2"><table>';
 5918:     my $rem;
 5919:     if (ref($ruleorder) eq 'ARRAY') {
 5920:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 5921:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 5922:                 my $rem = $i%($numinrow);
 5923:                 if ($rem == 0) {
 5924:                     if ($i > 0) {
 5925:                         $output .= '</tr>';
 5926:                     }
 5927:                     $output .= '<tr>';
 5928:                 }
 5929:                 my $check = ' ';
 5930:                 if (ref($settings) eq 'HASH') {
 5931:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 5932:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 5933:                             $check = ' checked="checked" ';
 5934:                         }
 5935:                     }
 5936:                 }
 5937:                 $output .= '<td class="LC_left_item">'.
 5938:                            '<span class="LC_nobreak"><label>'.
 5939:                            '<input type="checkbox" name="'.$type.'_rule" '.
 5940:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 5941:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 5942:             }
 5943:         }
 5944:         $rem = @{$ruleorder}%($numinrow);
 5945:     }
 5946:     my $colsleft = $numinrow - $rem;
 5947:     if ($colsleft > 1 ) {
 5948:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5949:                    '&nbsp;</td>';
 5950:     } elsif ($colsleft == 1) {
 5951:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 5952:     }
 5953:     $output .= '</tr></table></td></tr>';
 5954:     return $output;
 5955: }
 5956: 
 5957: sub usercreation_types {
 5958:     my %lt = &Apache::lonlocal::texthash (
 5959:                     author     => 'When adding a co-author',
 5960:                     course     => 'When adding a user to a course',
 5961:                     requestcrs => 'When requesting a course',
 5962:                     any        => 'Any',
 5963:                     official   => 'Institutional only ',
 5964:                     unofficial => 'Non-institutional only',
 5965:                     none       => 'None',
 5966:     );
 5967:     return %lt;
 5968: }
 5969: 
 5970: sub selfcreation_types {
 5971:     my %lt = &Apache::lonlocal::texthash (
 5972:                     selfcreate => 'User creates own account',
 5973:                     any        => 'Any',
 5974:                     official   => 'Institutional only ',
 5975:                     unofficial => 'Non-institutional only',
 5976:                     email      => 'E-mail address',
 5977:                     login      => 'Institutional Login',
 5978:                     sso        => 'SSO',
 5979:              );
 5980: }
 5981: 
 5982: sub authtype_names {
 5983:     my %lt = &Apache::lonlocal::texthash(
 5984:                       int    => 'Internal',
 5985:                       krb4   => 'Kerberos 4',
 5986:                       krb5   => 'Kerberos 5',
 5987:                       loc    => 'Local',
 5988:                   );
 5989:     return %lt;
 5990: }
 5991: 
 5992: sub context_names {
 5993:     my %context_title = &Apache::lonlocal::texthash(
 5994:        author => 'Creating users when an Author',
 5995:        course => 'Creating users when in a course',
 5996:        domain => 'Creating users when a Domain Coordinator',
 5997:     );
 5998:     return %context_title;
 5999: }
 6000: 
 6001: sub print_usermodification {
 6002:     my ($position,$dom,$settings,$rowtotal) = @_;
 6003:     my $numinrow = 4;
 6004:     my ($context,$datatable,$rowcount);
 6005:     if ($position eq 'top') {
 6006:         $rowcount = 0;
 6007:         $context = 'author'; 
 6008:         foreach my $role ('ca','aa') {
 6009:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 6010:                                                    $numinrow,$rowcount);
 6011:             $$rowtotal ++;
 6012:             $rowcount ++;
 6013:         }
 6014:     } elsif ($position eq 'bottom') {
 6015:         $context = 'course';
 6016:         $rowcount = 0;
 6017:         foreach my $role ('st','ep','ta','in','cr') {
 6018:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 6019:                                                    $numinrow,$rowcount);
 6020:             $$rowtotal ++;
 6021:             $rowcount ++;
 6022:         }
 6023:     }
 6024:     return $datatable;
 6025: }
 6026: 
 6027: sub print_defaults {
 6028:     my ($position,$dom,$settings,$rowtotal) = @_;
 6029:     my $rownum = 0;
 6030:     my ($datatable,$css_class,$titles);
 6031:     unless ($position eq 'bottom') {
 6032:         $titles = &defaults_titles($dom);
 6033:     }
 6034:     if ($position eq 'top') {
 6035:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 6036:                      'datelocale_def','portal_def');
 6037:         my %defaults;
 6038:         if (ref($settings) eq 'HASH') {
 6039:             %defaults = %{$settings};
 6040:         } else {
 6041:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6042:             foreach my $item (@items) {
 6043:                 $defaults{$item} = $domdefaults{$item};
 6044:             }
 6045:         }
 6046:         foreach my $item (@items) {
 6047:             if ($rownum%2) {
 6048:                 $css_class = '';
 6049:             } else {
 6050:                 $css_class = ' class="LC_odd_row" ';
 6051:             }
 6052:             $datatable .= '<tr'.$css_class.'>'.
 6053:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 6054:                           '</span></td><td class="LC_right_item" colspan="3">';
 6055:             if ($item eq 'auth_def') {
 6056:                 my @authtypes = ('internal','krb4','krb5','localauth');
 6057:                 my %shortauth = (
 6058:                                  internal => 'int',
 6059:                                  krb4 => 'krb4',
 6060:                                  krb5 => 'krb5',
 6061:                                  localauth  => 'loc'
 6062:                                 );
 6063:                 my %authnames = &authtype_names();
 6064:                 foreach my $auth (@authtypes) {
 6065:                     my $checked = ' ';
 6066:                     if ($defaults{$item} eq $auth) {
 6067:                         $checked = ' checked="checked" ';
 6068:                     }
 6069:                     $datatable .= '<label><input type="radio" name="'.$item.
 6070:                                   '" value="'.$auth.'"'.$checked.'/>'.
 6071:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 6072:                 }
 6073:             } elsif ($item eq 'timezone_def') {
 6074:                 my $includeempty = 1;
 6075:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 6076:             } elsif ($item eq 'datelocale_def') {
 6077:                 my $includeempty = 1;
 6078:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 6079:             } elsif ($item eq 'lang_def') {
 6080:                 my $includeempty = 1;
 6081:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 6082:             } else {
 6083:                 my $size;
 6084:                 if ($item eq 'portal_def') {
 6085:                     $size = ' size="25"';
 6086:                 }
 6087:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 6088:                               $defaults{$item}.'"'.$size.' />';
 6089:             }
 6090:             $datatable .= '</td></tr>';
 6091:             $rownum ++;
 6092:         }
 6093:     } elsif ($position eq 'middle') {
 6094:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 6095:         my %defaults;
 6096:         if (ref($settings) eq 'HASH') {
 6097:             %defaults = %{$settings};
 6098:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 6099:                 $defaults{'intauth_cost'} = 10;
 6100:             }
 6101:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 6102:                 $defaults{'intauth_check'} = 0;
 6103:             }
 6104:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 6105:                 $defaults{'intauth_switch'} = 0;
 6106:             }
 6107:         } else {
 6108:             %defaults = (
 6109:                           'intauth_cost'   => 10,
 6110:                           'intauth_check'  => 0,
 6111:                           'intauth_switch' => 0,
 6112:                         );
 6113:         }
 6114:         foreach my $item (@items) {
 6115:             if ($rownum%2) {
 6116:                 $css_class = '';
 6117:             } else {
 6118:                 $css_class = ' class="LC_odd_row" ';
 6119:             }
 6120:             $datatable .= '<tr'.$css_class.'>'.
 6121:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 6122:                           '</span></td><td class="LC_left_item" colspan="3">';
 6123:             if ($item eq 'intauth_switch') {
 6124:                 my @options = (0,1,2);
 6125:                 my %optiondesc = &Apache::lonlocal::texthash (
 6126:                                    0 => 'No',
 6127:                                    1 => 'Yes',
 6128:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 6129:                                  );
 6130:                 $datatable .= '<table width="100%">';
 6131:                 foreach my $option (@options) {
 6132:                     my $checked = ' ';
 6133:                     if ($defaults{$item} eq $option) {
 6134:                         $checked = ' checked="checked"';
 6135:                     }
 6136:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 6137:                                   '<label><input type="radio" name="'.$item.
 6138:                                   '" value="'.$option.'"'.$checked.' />'.
 6139:                                   $optiondesc{$option}.'</label></span></td></tr>';
 6140:                 }
 6141:                 $datatable .= '</table>';
 6142:             } elsif ($item eq 'intauth_check') {
 6143:                 my @options = (0,1,2);
 6144:                 my %optiondesc = &Apache::lonlocal::texthash (
 6145:                                    0 => 'No',
 6146:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 6147:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 6148:                                  );
 6149:                 $datatable .= '<table wisth="100%">';
 6150:                 foreach my $option (@options) {
 6151:                     my $checked = ' ';
 6152:                     my $onclick;
 6153:                     if ($defaults{$item} eq $option) {
 6154:                         $checked = ' checked="checked"';
 6155:                     }
 6156:                     if ($option == 2) {
 6157:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 6158:                     }
 6159:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 6160:                                   '<label><input type="radio" name="'.$item.
 6161:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 6162:                                   $optiondesc{$option}.'</label></span></td></tr>';
 6163:                 }
 6164:                 $datatable .= '</table>';
 6165:             } else {
 6166:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 6167:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />';
 6168:             }
 6169:             $datatable .= '</td></tr>';
 6170:             $rownum ++;
 6171:         }
 6172:     } else {
 6173:         my %defaults;
 6174:         if (ref($settings) eq 'HASH') {
 6175:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH') &&
 6176:                 (ref($settings->{'inststatusguest'}) eq 'ARRAY')) {
 6177:                 my $maxnum = @{$settings->{'inststatusorder'}};
 6178:                 for (my $i=0; $i<$maxnum; $i++) {
 6179:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 6180:                     my $item = $settings->{'inststatusorder'}->[$i];
 6181:                     my $title = $settings->{'inststatustypes'}->{$item};
 6182:                     my $guestok;
 6183:                     if (grep(/^\Q$item\E$/,@{$settings->{'inststatusguest'}})) {
 6184:                         $guestok = 1;
 6185:                     }
 6186:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 6187:                     $datatable .= '<tr'.$css_class.'>'.
 6188:                                   '<td><span class="LC_nobreak">'.
 6189:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 6190:                     for (my $k=0; $k<=$maxnum; $k++) {
 6191:                         my $vpos = $k+1;
 6192:                         my $selstr;
 6193:                         if ($k == $i) {
 6194:                             $selstr = ' selected="selected" ';
 6195:                         }
 6196:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6197:                     }
 6198:                     my ($checkedon,$checkedoff);
 6199:                     $checkedoff = ' checked="checked"';
 6200:                     if ($guestok) {
 6201:                         $checkedon = $checkedoff;
 6202:                         $checkedoff = ''; 
 6203:                     }
 6204:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 6205:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 6206:                                   &mt('delete').'</span></td>'.
 6207:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed:').
 6208:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 6209:                                   '</span></td>'.
 6210:                                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6211:                                   '<label><input type="radio" value="1" name="inststatus_guest_'.$item.'"'.$checkedon.' />'.
 6212:                                   &mt('Yes').'</label>'.('&nbsp;'x2).
 6213:                                   '<label><input type="radio" value="0" name="inststatus_guest_'.$item.'"'.$checkedoff.' />'.
 6214:                                   &mt('No').'</label></span></td></tr>';
 6215:                 }
 6216:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 6217:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 6218:                 $datatable .= '<tr '.$css_class.'>'.
 6219:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 6220:                 for (my $k=0; $k<=$maxnum; $k++) {
 6221:                     my $vpos = $k+1;
 6222:                     my $selstr;
 6223:                     if ($k == $maxnum) {
 6224:                         $selstr = ' selected="selected" ';
 6225:                     }
 6226:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6227:                 }
 6228:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 6229:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 6230:                               '&nbsp;'.&mt('(new)').
 6231:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 6232:                               &mt('Name displayed:').
 6233:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 6234:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 6235:                               '<label><input type="radio" value="1" name="addinststatus_guest" />'.
 6236:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 6237:                               '<label><input type="radio" value="0" name="addinststatus_guest" />'.
 6238:                               &mt('No').'</label></span></td></tr>';
 6239:                               '</tr>'."\n";
 6240:                 $rownum ++;
 6241:             }
 6242:         }
 6243:     }
 6244:     $$rowtotal += $rownum;
 6245:     return $datatable;
 6246: }
 6247: 
 6248: sub get_languages_hash {
 6249:     my %langchoices;
 6250:     foreach my $id (&Apache::loncommon::languageids()) {
 6251:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 6252:         if ($code ne '') {
 6253:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 6254:         }
 6255:     }
 6256:     return %langchoices;
 6257: }
 6258: 
 6259: sub defaults_titles {
 6260:     my ($dom) = @_;
 6261:     my %titles = &Apache::lonlocal::texthash (
 6262:                    'auth_def'      => 'Default authentication type',
 6263:                    'auth_arg_def'  => 'Default authentication argument',
 6264:                    'lang_def'      => 'Default language',
 6265:                    'timezone_def'  => 'Default timezone',
 6266:                    'datelocale_def' => 'Default locale for dates',
 6267:                    'portal_def'     => 'Portal/Default URL',
 6268:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
 6269:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
 6270:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
 6271:                  );
 6272:     if ($dom) {
 6273:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 6274:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 6275:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 6276:         $protocol = 'http' if ($protocol ne 'https');
 6277:         if ($uint_dom) {
 6278:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 6279:                                          $uint_dom);
 6280:         }
 6281:     }
 6282:     return (\%titles);
 6283: }
 6284: 
 6285: sub print_scantronformat {
 6286:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 6287:     my $itemcount = 1;
 6288:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 6289:         %confhash);
 6290:     my $switchserver = &check_switchserver($dom,$confname);
 6291:     my %lt = &Apache::lonlocal::texthash (
 6292:                 default => 'Default bubblesheet format file error',
 6293:                 custom  => 'Custom bubblesheet format file error',
 6294:              );
 6295:     my %scantronfiles = (
 6296:         default => 'default.tab',
 6297:         custom => 'custom.tab',
 6298:     );
 6299:     foreach my $key (keys(%scantronfiles)) {
 6300:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 6301:                               .$scantronfiles{$key};
 6302:     }
 6303:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 6304:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 6305:         if (!$switchserver) {
 6306:             my $servadm = $r->dir_config('lonAdmEMail');
 6307:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 6308:             if ($configuserok eq 'ok') {
 6309:                 if ($author_ok eq 'ok') {
 6310:                     my %legacyfile = (
 6311:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 6312:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 6313:                     );
 6314:                     my %md5chk;
 6315:                     foreach my $type (keys(%legacyfile)) {
 6316:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 6317:                         chomp($md5chk{$type});
 6318:                     }
 6319:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 6320:                         foreach my $type (keys(%legacyfile)) {
 6321:                             ($scantronurls{$type},my $error) = 
 6322:                                 &legacy_scantronformat($r,$dom,$confname,
 6323:                                                  $type,$legacyfile{$type},
 6324:                                                  $scantronurls{$type},
 6325:                                                  $scantronfiles{$type});
 6326:                             if ($error ne '') {
 6327:                                 $error{$type} = $error;
 6328:                             }
 6329:                         }
 6330:                         if (keys(%error) == 0) {
 6331:                             $is_custom = 1;
 6332:                             $confhash{'scantron'}{'scantronformat'} = 
 6333:                                 $scantronurls{'custom'};
 6334:                             my $putresult = 
 6335:                                 &Apache::lonnet::put_dom('configuration',
 6336:                                                          \%confhash,$dom);
 6337:                             if ($putresult ne 'ok') {
 6338:                                 $error{'custom'} = 
 6339:                                     '<span class="LC_error">'.
 6340:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 6341:                             }
 6342:                         }
 6343:                     } else {
 6344:                         ($scantronurls{'default'},my $error) =
 6345:                             &legacy_scantronformat($r,$dom,$confname,
 6346:                                           'default',$legacyfile{'default'},
 6347:                                           $scantronurls{'default'},
 6348:                                           $scantronfiles{'default'});
 6349:                         if ($error eq '') {
 6350:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 6351:                             my $putresult =
 6352:                                 &Apache::lonnet::put_dom('configuration',
 6353:                                                          \%confhash,$dom);
 6354:                             if ($putresult ne 'ok') {
 6355:                                 $error{'default'} =
 6356:                                     '<span class="LC_error">'.
 6357:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 6358:                             }
 6359:                         } else {
 6360:                             $error{'default'} = $error;
 6361:                         }
 6362:                     }
 6363:                 }
 6364:             }
 6365:         } else {
 6366:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 6367:         }
 6368:     }
 6369:     if (ref($settings) eq 'HASH') {
 6370:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 6371:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 6372:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 6373:                 $scantronurl = '';
 6374:             } else {
 6375:                 $scantronurl = $settings->{'scantronformat'};
 6376:             }
 6377:             $is_custom = 1;
 6378:         } else {
 6379:             $scantronurl = $scantronurls{'default'};
 6380:         }
 6381:     } else {
 6382:         if ($is_custom) {
 6383:             $scantronurl = $scantronurls{'custom'};
 6384:         } else {
 6385:             $scantronurl = $scantronurls{'default'};
 6386:         }
 6387:     }
 6388:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6389:     $datatable .= '<tr'.$css_class.'>';
 6390:     if (!$is_custom) {
 6391:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 6392:                       '<span class="LC_nobreak">';
 6393:         if ($scantronurl) {
 6394:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 6395:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 6396:         } else {
 6397:             $datatable = &mt('File unavailable for display');
 6398:         }
 6399:         $datatable .= '</span></td>';
 6400:         if (keys(%error) == 0) { 
 6401:             $datatable .= '<td valign="bottom">';
 6402:             if (!$switchserver) {
 6403:                 $datatable .= &mt('Upload:').'<br />';
 6404:             }
 6405:         } else {
 6406:             my $errorstr;
 6407:             foreach my $key (sort(keys(%error))) {
 6408:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 6409:             }
 6410:             $datatable .= '<td>'.$errorstr;
 6411:         }
 6412:     } else {
 6413:         if (keys(%error) > 0) {
 6414:             my $errorstr;
 6415:             foreach my $key (sort(keys(%error))) {
 6416:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 6417:             } 
 6418:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 6419:         } elsif ($scantronurl) {
 6420:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 6421:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 6422:             $datatable .= '<td><span class="LC_nobreak">'.
 6423:                           $link.
 6424:                           '<label><input type="checkbox" name="scantronformat_del"'.
 6425:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 6426:                           '<td><span class="LC_nobreak">&nbsp;'.
 6427:                           &mt('Replace:').'</span><br />';
 6428:         }
 6429:     }
 6430:     if (keys(%error) == 0) {
 6431:         if ($switchserver) {
 6432:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 6433:         } else {
 6434:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 6435:                          '<input type="file" name="scantronformat" /></span>';
 6436:         }
 6437:     }
 6438:     $datatable .= '</td></tr>';
 6439:     $$rowtotal ++;
 6440:     return $datatable;
 6441: }
 6442: 
 6443: sub legacy_scantronformat {
 6444:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 6445:     my ($url,$error);
 6446:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 6447:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 6448:         (my $result,$url) =
 6449:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 6450:                          '','',$newfile);
 6451:         if ($result ne 'ok') {
 6452:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 6453:         }
 6454:     }
 6455:     return ($url,$error);
 6456: }
 6457: 
 6458: sub print_coursecategories {
 6459:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 6460:     my $datatable;
 6461:     if ($position eq 'top') {
 6462:         my (%checked);
 6463:         my @catitems = ('unauth','auth');
 6464:         my @cattypes = ('std','domonly','codesrch','none');
 6465:         $checked{'unauth'} = 'std';
 6466:         $checked{'auth'} = 'std';
 6467:         if (ref($settings) eq 'HASH') {
 6468:             foreach my $type (@cattypes) {
 6469:                 if ($type eq $settings->{'unauth'}) {
 6470:                     $checked{'unauth'} = $type;
 6471:                 }
 6472:                 if ($type eq $settings->{'auth'}) {
 6473:                     $checked{'auth'} = $type;
 6474:                 }
 6475:             }
 6476:         }
 6477:         my %lt = &Apache::lonlocal::texthash (
 6478:                                                unauth   => 'Catalog type for unauthenticated users',
 6479:                                                auth     => 'Catalog type for authenticated users',
 6480:                                                none     => 'No catalog',
 6481:                                                std      => 'Standard catalog',
 6482:                                                domonly  => 'Domain-only catalog',
 6483:                                                codesrch => "Code search form",
 6484:                                              );
 6485:        my $itemcount = 0;
 6486:        foreach my $item (@catitems) {
 6487:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 6488:            $datatable .= '<tr '.$css_class.'>'.
 6489:                          '<td>'.$lt{$item}.'</td>'.
 6490:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 6491:            foreach my $type (@cattypes) {
 6492:                my $ischecked;
 6493:                if ($checked{$item} eq $type) {
 6494:                    $ischecked=' checked="checked"';
 6495:                }
 6496:                $datatable .= '<label>'.
 6497:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 6498:                              ' />'.$lt{$type}.'</label>&nbsp;';
 6499:            }
 6500:            $datatable .= '</td></tr>';
 6501:            $itemcount ++;
 6502:         }
 6503:         $$rowtotal += $itemcount;
 6504:     } elsif ($position eq 'middle') {
 6505:         my $toggle_cats_crs = ' ';
 6506:         my $toggle_cats_dom = ' checked="checked" ';
 6507:         my $can_cat_crs = ' ';
 6508:         my $can_cat_dom = ' checked="checked" ';
 6509:         my $toggle_catscomm_comm = ' ';
 6510:         my $toggle_catscomm_dom = ' checked="checked" ';
 6511:         my $can_catcomm_comm = ' ';
 6512:         my $can_catcomm_dom = ' checked="checked" ';
 6513: 
 6514:         if (ref($settings) eq 'HASH') {
 6515:             if ($settings->{'togglecats'} eq 'crs') {
 6516:                 $toggle_cats_crs = $toggle_cats_dom;
 6517:                 $toggle_cats_dom = ' ';
 6518:             }
 6519:             if ($settings->{'categorize'} eq 'crs') {
 6520:                 $can_cat_crs = $can_cat_dom;
 6521:                 $can_cat_dom = ' ';
 6522:             }
 6523:             if ($settings->{'togglecatscomm'} eq 'comm') {
 6524:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 6525:                 $toggle_catscomm_dom = ' ';
 6526:             }
 6527:             if ($settings->{'categorizecomm'} eq 'comm') {
 6528:                 $can_catcomm_comm = $can_catcomm_dom;
 6529:                 $can_catcomm_dom = ' ';
 6530:             }
 6531:         }
 6532:         my %title = &Apache::lonlocal::texthash (
 6533:                      togglecats     => 'Show/Hide a course in catalog',
 6534:                      togglecatscomm => 'Show/Hide a community in catalog',
 6535:                      categorize     => 'Assign a category to a course',
 6536:                      categorizecomm => 'Assign a category to a community',
 6537:                     );
 6538:         my %level = &Apache::lonlocal::texthash (
 6539:                      dom  => 'Set in Domain',
 6540:                      crs  => 'Set in Course',
 6541:                      comm => 'Set in Community',
 6542:                     );
 6543:         $datatable = '<tr class="LC_odd_row">'.
 6544:                   '<td>'.$title{'togglecats'}.'</td>'.
 6545:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 6546:                   '<input type="radio" name="togglecats"'.
 6547:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6548:                   '<label><input type="radio" name="togglecats"'.
 6549:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 6550:                   '</tr><tr>'.
 6551:                   '<td>'.$title{'categorize'}.'</td>'.
 6552:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6553:                   '<label><input type="radio" name="categorize"'.
 6554:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6555:                   '<label><input type="radio" name="categorize"'.
 6556:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 6557:                   '</tr><tr class="LC_odd_row">'.
 6558:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 6559:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 6560:                   '<input type="radio" name="togglecatscomm"'.
 6561:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6562:                   '<label><input type="radio" name="togglecatscomm"'.
 6563:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 6564:                   '</tr><tr>'.
 6565:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 6566:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6567:                   '<label><input type="radio" name="categorizecomm"'.
 6568:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6569:                   '<label><input type="radio" name="categorizecomm"'.
 6570:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 6571:                   '</tr>';
 6572:         $$rowtotal += 4;
 6573:     } else {
 6574:         my $css_class;
 6575:         my $itemcount = 1;
 6576:         my $cathash; 
 6577:         if (ref($settings) eq 'HASH') {
 6578:             $cathash = $settings->{'cats'};
 6579:         }
 6580:         if (ref($cathash) eq 'HASH') {
 6581:             my (@cats,@trails,%allitems,%idx,@jsarray);
 6582:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 6583:                                                    \%allitems,\%idx,\@jsarray);
 6584:             my $maxdepth = scalar(@cats);
 6585:             my $colattrib = '';
 6586:             if ($maxdepth > 2) {
 6587:                 $colattrib = ' colspan="2" ';
 6588:             }
 6589:             my @path;
 6590:             if (@cats > 0) {
 6591:                 if (ref($cats[0]) eq 'ARRAY') {
 6592:                     my $numtop = @{$cats[0]};
 6593:                     my $maxnum = $numtop;
 6594:                     my %default_names = (
 6595:                           instcode    => &mt('Official courses'),
 6596:                           communities => &mt('Communities'),
 6597:                     );
 6598: 
 6599:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 6600:                         ($cathash->{'instcode::0'} eq '') ||
 6601:                         (!grep(/^communities$/,@{$cats[0]})) || 
 6602:                         ($cathash->{'communities::0'} eq '')) {
 6603:                         $maxnum ++;
 6604:                     }
 6605:                     my $lastidx;
 6606:                     for (my $i=0; $i<$numtop; $i++) {
 6607:                         my $parent = $cats[0][$i];
 6608:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6609:                         my $item = &escape($parent).'::0';
 6610:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 6611:                         $lastidx = $idx{$item};
 6612:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6613:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 6614:                         for (my $k=0; $k<=$maxnum; $k++) {
 6615:                             my $vpos = $k+1;
 6616:                             my $selstr;
 6617:                             if ($k == $i) {
 6618:                                 $selstr = ' selected="selected" ';
 6619:                             }
 6620:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6621:                         }
 6622:                         $datatable .= '</select></span></td><td>';
 6623:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 6624:                             $datatable .=  '<span class="LC_nobreak">'
 6625:                                            .$default_names{$parent}.'</span>';
 6626:                             if ($parent eq 'instcode') {
 6627:                                 $datatable .= '<br /><span class="LC_nobreak">('
 6628:                                               .&mt('with institutional codes')
 6629:                                               .')</span></td><td'.$colattrib.'>';
 6630:                             } else {
 6631:                                 $datatable .= '<table><tr><td>';
 6632:                             }
 6633:                             $datatable .= '<span class="LC_nobreak">'
 6634:                                           .'<label><input type="radio" name="'
 6635:                                           .$parent.'" value="1" checked="checked" />'
 6636:                                           .&mt('Display').'</label>';
 6637:                             if ($parent eq 'instcode') {
 6638:                                 $datatable .= '&nbsp;';
 6639:                             } else {
 6640:                                 $datatable .= '</span></td></tr><tr><td>'
 6641:                                               .'<span class="LC_nobreak">';
 6642:                             }
 6643:                             $datatable .= '<label><input type="radio" name="'
 6644:                                           .$parent.'" value="0" />'
 6645:                                           .&mt('Do not display').'</label></span>';
 6646:                             if ($parent eq 'communities') {
 6647:                                 $datatable .= '</td></tr></table>';
 6648:                             }
 6649:                             $datatable .= '</td>';
 6650:                         } else {
 6651:                             $datatable .= $parent
 6652:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 6653:                                           .'<input type="checkbox" name="deletecategory" '
 6654:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 6655:                         }
 6656:                         my $depth = 1;
 6657:                         push(@path,$parent);
 6658:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 6659:                         pop(@path);
 6660:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 6661:                         $itemcount ++;
 6662:                     }
 6663:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6664:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 6665:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 6666:                     for (my $k=0; $k<=$maxnum; $k++) {
 6667:                         my $vpos = $k+1;
 6668:                         my $selstr;
 6669:                         if ($k == $numtop) {
 6670:                             $selstr = ' selected="selected" ';
 6671:                         }
 6672:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6673:                     }
 6674:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 6675:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 6676:                                   .'</tr>'."\n";
 6677:                     $itemcount ++;
 6678:                     foreach my $default ('instcode','communities') {
 6679:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 6680:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6681:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 6682:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 6683:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 6684:                             for (my $k=0; $k<=$maxnum; $k++) {
 6685:                                 my $vpos = $k+1;
 6686:                                 my $selstr;
 6687:                                 if ($k == $maxnum) {
 6688:                                     $selstr = ' selected="selected" ';
 6689:                                 }
 6690:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6691:                             }
 6692:                             $datatable .= '</select></span></td>'.
 6693:                                           '<td><span class="LC_nobreak">'.
 6694:                                           $default_names{$default}.'</span>';
 6695:                             if ($default eq 'instcode') {
 6696:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 6697:                                               .&mt('with institutional codes').')</span>';
 6698:                             }
 6699:                             $datatable .= '</td>'
 6700:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 6701:                                           .&mt('Display').'</label>&nbsp;'
 6702:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 6703:                                           .&mt('Do not display').'</label></span></td></tr>';
 6704:                         }
 6705:                     }
 6706:                 }
 6707:             } else {
 6708:                 $datatable .= &initialize_categories($itemcount);
 6709:             }
 6710:         } else {
 6711:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td>'
 6712:                           .&initialize_categories($itemcount);
 6713:         }
 6714:         $$rowtotal += $itemcount;
 6715:     }
 6716:     return $datatable;
 6717: }
 6718: 
 6719: sub print_serverstatuses {
 6720:     my ($dom,$settings,$rowtotal) = @_;
 6721:     my $datatable;
 6722:     my @pages = &serverstatus_pages();
 6723:     my (%namedaccess,%machineaccess);
 6724:     foreach my $type (@pages) {
 6725:         $namedaccess{$type} = '';
 6726:         $machineaccess{$type}= '';
 6727:     }
 6728:     if (ref($settings) eq 'HASH') {
 6729:         foreach my $type (@pages) {
 6730:             if (exists($settings->{$type})) {
 6731:                 if (ref($settings->{$type}) eq 'HASH') {
 6732:                     foreach my $key (keys(%{$settings->{$type}})) {
 6733:                         if ($key eq 'namedusers') {
 6734:                             $namedaccess{$type} = $settings->{$type}->{$key};
 6735:                         } elsif ($key eq 'machines') {
 6736:                             $machineaccess{$type} = $settings->{$type}->{$key};
 6737:                         }
 6738:                     }
 6739:                 }
 6740:             }
 6741:         }
 6742:     }
 6743:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 6744:     my $rownum = 0;
 6745:     my $css_class;
 6746:     foreach my $type (@pages) {
 6747:         $rownum ++;
 6748:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 6749:         $datatable .= '<tr'.$css_class.'>'.
 6750:                       '<td><span class="LC_nobreak">'.
 6751:                       $titles->{$type}.'</span></td>'.
 6752:                       '<td class="LC_left_item">'.
 6753:                       '<input type="text" name="'.$type.'_namedusers" '.
 6754:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 6755:                       '<td class="LC_right_item">'.
 6756:                       '<span class="LC_nobreak">'.
 6757:                       '<input type="text" name="'.$type.'_machines" '.
 6758:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 6759:                       '</td></tr>'."\n";
 6760:     }
 6761:     $$rowtotal += $rownum;
 6762:     return $datatable;
 6763: }
 6764: 
 6765: sub serverstatus_pages {
 6766:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 6767:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 6768:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 6769:             'uniquecodes','diskusage','coursecatalog');
 6770: }
 6771: 
 6772: sub defaults_javascript {
 6773:     my ($settings) = @_;
 6774:     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.');
 6775:     my $intauthcost = &mt('Warning: bcrypt encryption cost for internal authentication must be an integer.');
 6776:     &js_escape(\$intauthcheck);
 6777:     &js_escape(\$intauthcost);
 6778:     my $intauthjs = <<"ENDSCRIPT";
 6779: 
 6780: function warnIntAuth(field) {
 6781:     if (field.name == 'intauth_check') {
 6782:         if (field.value == '2') {
 6783:             alert('$intauthcheck');
 6784:         }
 6785:     }
 6786:     if (field.name == 'intauth_cost') {
 6787:         field.value.replace(/\s/g,'');
 6788:         if (field.value != '') {
 6789:             var regexdigit=/^\\d+\$/;
 6790:             if (!regexdigit.test(field.value)) {
 6791:                 alert('$intauthcost');
 6792:             }
 6793:         }
 6794:     }
 6795:     return;
 6796: }
 6797: 
 6798: ENDSCRIPT
 6799: 
 6800:     if (ref($settings) ne 'HASH') {
 6801:         return &Apache::lonhtmlcommon::scripttag($intauthjs);
 6802:     }
 6803:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 6804:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 6805:         if ($maxnum eq '') {
 6806:             $maxnum = 0;
 6807:         }
 6808:         $maxnum ++;
 6809:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 6810:         return <<"ENDSCRIPT";
 6811: <script type="text/javascript">
 6812: // <![CDATA[
 6813: function reorderTypes(form,caller) {
 6814:     var changedVal;
 6815: $jstext 
 6816:     var newpos = 'addinststatus_pos';
 6817:     var current = new Array;
 6818:     var maxh = $maxnum;
 6819:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 6820:     var oldVal;
 6821:     if (caller == newpos) {
 6822:         changedVal = newitemVal;
 6823:     } else {
 6824:         var curritem = 'inststatus_pos_'+caller;
 6825:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 6826:         current[newitemVal] = newpos;
 6827:     }
 6828:     for (var i=0; i<inststatuses.length; i++) {
 6829:         if (inststatuses[i] != caller) {
 6830:             var elementName = 'inststatus_pos_'+inststatuses[i];
 6831:             if (form.elements[elementName]) {
 6832:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 6833:                 current[currVal] = elementName;
 6834:             }
 6835:         }
 6836:     }
 6837:     for (var j=0; j<maxh; j++) {
 6838:         if (current[j] == undefined) {
 6839:             oldVal = j;
 6840:         }
 6841:     }
 6842:     if (oldVal < changedVal) {
 6843:         for (var k=oldVal+1; k<=changedVal ; k++) {
 6844:            var elementName = current[k];
 6845:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 6846:         }
 6847:     } else {
 6848:         for (var k=changedVal; k<oldVal; k++) {
 6849:             var elementName = current[k];
 6850:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 6851:         }
 6852:     }
 6853:     return;
 6854: }
 6855: 
 6856: $intauthjs
 6857: 
 6858: // ]]>
 6859: </script>
 6860: 
 6861: ENDSCRIPT
 6862:     } else {
 6863:         return &Apache::lonhtmlcommon::scripttag($intauthjs);
 6864:     }
 6865: }
 6866: 
 6867: sub coursecategories_javascript {
 6868:     my ($settings) = @_;
 6869:     my ($output,$jstext,$cathash);
 6870:     if (ref($settings) eq 'HASH') {
 6871:         $cathash = $settings->{'cats'};
 6872:     }
 6873:     if (ref($cathash) eq 'HASH') {
 6874:         my (@cats,@jsarray,%idx);
 6875:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 6876:         if (@jsarray > 0) {
 6877:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 6878:             for (my $i=0; $i<@jsarray; $i++) {
 6879:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 6880:                     my $catstr = join('","',@{$jsarray[$i]});
 6881:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 6882:                 }
 6883:             }
 6884:         }
 6885:     } else {
 6886:         $jstext  = '    var categories = Array(1);'."\n".
 6887:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 6888:     }
 6889:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 6890:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 6891:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
 6892:     &js_escape(\$instcode_reserved);
 6893:     &js_escape(\$communities_reserved);
 6894:     &js_escape(\$choose_again);
 6895:     $output = <<"ENDSCRIPT";
 6896: <script type="text/javascript">
 6897: // <![CDATA[
 6898: function reorderCats(form,parent,item,idx) {
 6899:     var changedVal;
 6900: $jstext
 6901:     var newpos = 'addcategory_pos';
 6902:     if (parent == '') {
 6903:         var has_instcode = 0;
 6904:         var maxtop = categories[idx].length;
 6905:         for (var j=0; j<maxtop; j++) {
 6906:             if (categories[idx][j] == 'instcode::0') {
 6907:                 has_instcode == 1;
 6908:             }
 6909:         }
 6910:         if (has_instcode == 0) {
 6911:             categories[idx][maxtop] = 'instcode_pos';
 6912:         }
 6913:     } else {
 6914:         newpos += '_'+parent;
 6915:     }
 6916:     var maxh = 1 + categories[idx].length;
 6917:     var current = new Array;
 6918:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 6919:     if (item == newpos) {
 6920:         changedVal = newitemVal;
 6921:     } else {
 6922:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 6923:         current[newitemVal] = newpos;
 6924:     }
 6925:     for (var i=0; i<categories[idx].length; i++) {
 6926:         var elementName = categories[idx][i];
 6927:         if (elementName != item) {
 6928:             if (form.elements[elementName]) {
 6929:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 6930:                 current[currVal] = elementName;
 6931:             }
 6932:         }
 6933:     }
 6934:     var oldVal;
 6935:     for (var j=0; j<maxh; j++) {
 6936:         if (current[j] == undefined) {
 6937:             oldVal = j;
 6938:         }
 6939:     }
 6940:     if (oldVal < changedVal) {
 6941:         for (var k=oldVal+1; k<=changedVal ; k++) {
 6942:            var elementName = current[k];
 6943:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 6944:         }
 6945:     } else {
 6946:         for (var k=changedVal; k<oldVal; k++) {
 6947:             var elementName = current[k];
 6948:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 6949:         }
 6950:     }
 6951:     return;
 6952: }
 6953: 
 6954: function categoryCheck(form) {
 6955:     if (form.elements['addcategory_name'].value == 'instcode') {
 6956:         alert('$instcode_reserved\\n$choose_again');
 6957:         return false;
 6958:     }
 6959:     if (form.elements['addcategory_name'].value == 'communities') {
 6960:         alert('$communities_reserved\\n$choose_again');
 6961:         return false;
 6962:     }
 6963:     return true;
 6964: }
 6965: 
 6966: // ]]>
 6967: </script>
 6968: 
 6969: ENDSCRIPT
 6970:     return $output;
 6971: }
 6972: 
 6973: sub initialize_categories {
 6974:     my ($itemcount) = @_;
 6975:     my ($datatable,$css_class,$chgstr);
 6976:     my %default_names = (
 6977:                       instcode    => 'Official courses (with institutional codes)',
 6978:                       communities => 'Communities',
 6979:                         );
 6980:     my $select0 = ' selected="selected"';
 6981:     my $select1 = '';
 6982:     foreach my $default ('instcode','communities') {
 6983:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6984:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 6985:         if ($default eq 'communities') {
 6986:             $select1 = $select0;
 6987:             $select0 = '';
 6988:         }
 6989:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6990:                      .'<select name="'.$default.'_pos">'
 6991:                      .'<option value="0"'.$select0.'>1</option>'
 6992:                      .'<option value="1"'.$select1.'>2</option>'
 6993:                      .'<option value="2">3</option></select>&nbsp;'
 6994:                      .$default_names{$default}
 6995:                      .'</span></td><td><span class="LC_nobreak">'
 6996:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 6997:                      .&mt('Display').'</label>&nbsp;<label>'
 6998:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 6999:                  .'</label></span></td></tr>';
 7000:         $itemcount ++;
 7001:     }
 7002:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7003:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 7004:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7005:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 7006:                   .'<option value="0">1</option>'
 7007:                   .'<option value="1">2</option>'
 7008:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 7009:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 7010:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 7011:     return $datatable;
 7012: }
 7013: 
 7014: sub build_category_rows {
 7015:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 7016:     my ($text,$name,$item,$chgstr);
 7017:     if (ref($cats) eq 'ARRAY') {
 7018:         my $maxdepth = scalar(@{$cats});
 7019:         if (ref($cats->[$depth]) eq 'HASH') {
 7020:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 7021:                 my $numchildren = @{$cats->[$depth]{$parent}};
 7022:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7023:                 $text .= '<td><table class="LC_data_table">';
 7024:                 my ($idxnum,$parent_name,$parent_item);
 7025:                 my $higher = $depth - 1;
 7026:                 if ($higher == 0) {
 7027:                     $parent_name = &escape($parent).'::'.$higher;
 7028:                 } else {
 7029:                     if (ref($path) eq 'ARRAY') {
 7030:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 7031:                     }
 7032:                 }
 7033:                 $parent_item = 'addcategory_pos_'.$parent_name;
 7034:                 for (my $j=0; $j<=$numchildren; $j++) {
 7035:                     if ($j < $numchildren) {
 7036:                         $name = $cats->[$depth]{$parent}[$j];
 7037:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 7038:                         $idxnum = $idx->{$item};
 7039:                     } else {
 7040:                         $name = $parent_name;
 7041:                         $item = $parent_item;
 7042:                     }
 7043:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 7044:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 7045:                     for (my $i=0; $i<=$numchildren; $i++) {
 7046:                         my $vpos = $i+1;
 7047:                         my $selstr;
 7048:                         if ($j == $i) {
 7049:                             $selstr = ' selected="selected" ';
 7050:                         }
 7051:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 7052:                     }
 7053:                     $text .= '</select>&nbsp;';
 7054:                     if ($j < $numchildren) {
 7055:                         my $deeper = $depth+1;
 7056:                         $text .= $name.'&nbsp;'
 7057:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 7058:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 7059:                         if(ref($path) eq 'ARRAY') {
 7060:                             push(@{$path},$name);
 7061:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 7062:                             pop(@{$path});
 7063:                         }
 7064:                     } else {
 7065:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 7066:                         if ($j == $numchildren) {
 7067:                             $text .= $name;
 7068:                         } else {
 7069:                             $text .= $item;
 7070:                         }
 7071:                         $text .= '" value="" />';
 7072:                     }
 7073:                     $text .= '</td></tr>';
 7074:                 }
 7075:                 $text .= '</table></td>';
 7076:             } else {
 7077:                 my $higher = $depth-1;
 7078:                 if ($higher == 0) {
 7079:                     $name = &escape($parent).'::'.$higher;
 7080:                 } else {
 7081:                     if (ref($path) eq 'ARRAY') {
 7082:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 7083:                     }
 7084:                 }
 7085:                 my $colspan;
 7086:                 if ($parent ne 'instcode') {
 7087:                     $colspan = $maxdepth - $depth - 1;
 7088:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 7089:                 }
 7090:             }
 7091:         }
 7092:     }
 7093:     return $text;
 7094: }
 7095: 
 7096: sub modifiable_userdata_row {
 7097:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref) = @_;
 7098:     my ($role,$rolename,$statustype);
 7099:     $role = $item;
 7100:     if ($context eq 'cancreate') {
 7101:         if ($item =~ /^emailusername_(.+)$/) {
 7102:             $statustype = $1;
 7103:             $role = 'emailusername';
 7104:             if (ref($usertypes) eq 'HASH') {
 7105:                 if ($usertypes->{$statustype}) {
 7106:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 7107:                 } else {
 7108:                     $rolename = &mt('Data provided by user');
 7109:                 }
 7110:             }
 7111:         }
 7112:     } elsif ($context eq 'selfcreate') {
 7113:         if (ref($usertypes) eq 'HASH') {
 7114:             $rolename = $usertypes->{$role};
 7115:         } else {
 7116:             $rolename = $role;
 7117:         }
 7118:     } else {
 7119:         if ($role eq 'cr') {
 7120:             $rolename = &mt('Custom role');
 7121:         } else {
 7122:             $rolename = &Apache::lonnet::plaintext($role);
 7123:         }
 7124:     }
 7125:     my (@fields,%fieldtitles);
 7126:     if (ref($fieldsref) eq 'ARRAY') {
 7127:         @fields = @{$fieldsref};
 7128:     } else {
 7129:         @fields = ('lastname','firstname','middlename','generation',
 7130:                    'permanentemail','id');
 7131:     }
 7132:     if ((ref($titlesref) eq 'HASH')) {
 7133:         %fieldtitles = %{$titlesref};
 7134:     } else {
 7135:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7136:     }
 7137:     my $output;
 7138:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 7139:     $output = '<tr '.$css_class.'>'.
 7140:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 7141:               '<td class="LC_left_item" colspan="2"><table>';
 7142:     my $rem;
 7143:     my %checks;
 7144:     if (ref($settings) eq 'HASH') {
 7145:         if (ref($settings->{$context}) eq 'HASH') {
 7146:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 7147:                 my $hashref = $settings->{$context}->{$role};
 7148:                 if ($role eq 'emailusername') {
 7149:                     if ($statustype) {
 7150:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 7151:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 7152:                             if (ref($hashref) eq 'HASH') { 
 7153:                                 foreach my $field (@fields) {
 7154:                                     if ($hashref->{$field}) {
 7155:                                         $checks{$field} = $hashref->{$field};
 7156:                                     }
 7157:                                 }
 7158:                             }
 7159:                         }
 7160:                     }
 7161:                 } else {
 7162:                     if (ref($hashref) eq 'HASH') {
 7163:                         foreach my $field (@fields) {
 7164:                             if ($hashref->{$field}) {
 7165:                                 $checks{$field} = ' checked="checked" ';
 7166:                             }
 7167:                         }
 7168:                     }
 7169:                 }
 7170:             }
 7171:         }
 7172:     }
 7173:      
 7174:     for (my $i=0; $i<@fields; $i++) {
 7175:         my $rem = $i%($numinrow);
 7176:         if ($rem == 0) {
 7177:             if ($i > 0) {
 7178:                 $output .= '</tr>';
 7179:             }
 7180:             $output .= '<tr>';
 7181:         }
 7182:         my $check = ' ';
 7183:         unless ($role eq 'emailusername') {
 7184:             if (exists($checks{$fields[$i]})) {
 7185:                 $check = $checks{$fields[$i]}
 7186:             } else {
 7187:                 if ($role eq 'st') {
 7188:                     if (ref($settings) ne 'HASH') {
 7189:                         $check = ' checked="checked" '; 
 7190:                     }
 7191:                 }
 7192:             }
 7193:         }
 7194:         $output .= '<td class="LC_left_item">'.
 7195:                    '<span class="LC_nobreak">';
 7196:         if ($role eq 'emailusername') {
 7197:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 7198:                 $checks{$fields[$i]} = 'omit';
 7199:             }
 7200:             foreach my $option ('required','optional','omit') {
 7201:                 my $checked='';
 7202:                 if ($checks{$fields[$i]} eq $option) {
 7203:                     $checked='checked="checked" ';
 7204:                 }
 7205:                 $output .= '<label>'.
 7206:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 7207:                            &mt($option).'</label>'.('&nbsp;' x2);
 7208:             }
 7209:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 7210:         } else {
 7211:             $output .= '<label>'.
 7212:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 7213:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 7214:                        '</label>';
 7215:         }
 7216:         $output .= '</span></td>';
 7217:         $rem = @fields%($numinrow);
 7218:     }
 7219:     my $colsleft = $numinrow - $rem;
 7220:     if ($colsleft > 1 ) {
 7221:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7222:                    '&nbsp;</td>';
 7223:     } elsif ($colsleft == 1) {
 7224:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 7225:     }
 7226:     $output .= '</tr></table></td></tr>';
 7227:     return $output;
 7228: }
 7229: 
 7230: sub insttypes_row {
 7231:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
 7232:         $customcss,$rowstyle) = @_;
 7233:     my %lt = &Apache::lonlocal::texthash (
 7234:                       cansearch => 'Users allowed to search',
 7235:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 7236:                       lockablenames => 'User preference to lock name',
 7237:                       overrides     => "Override domain's helpdesk settings based on requester's affiliation",
 7238:              );
 7239:     my $showdom;
 7240:     if ($context eq 'cansearch') {
 7241:         $showdom = ' ('.$dom.')';
 7242:     }
 7243:     my $class = 'LC_left_item';
 7244:     if ($context eq 'statustocreate') {
 7245:         $class = 'LC_right_item';
 7246:     }
 7247:     my $css_class;
 7248:     if ($$rowtotal%2) {
 7249:         $css_class = 'LC_odd_row';
 7250:     }
 7251:     if ($customcss) {
 7252:         $css_class .= ' '.$customcss;
 7253:     }
 7254:     $css_class =~ s/^\s+//;
 7255:     if ($css_class) {
 7256:         $css_class = ' class="'.$css_class.'"';
 7257:     }
 7258:     if ($rowstyle) {
 7259:         $css_class .= ' style="'.$rowstyle.'"';
 7260:     }
 7261:     if ($onclick) {
 7262:         $onclick = 'onclick="'.$onclick.'" ';
 7263:     }
 7264:     my $output = '<tr'.$css_class.'>'.
 7265:                  '<td>'.$lt{$context}.$showdom.
 7266:                  '</td><td class="'.$class.'" colspan="2"><table>';
 7267:     my $rem;
 7268:     if (ref($types) eq 'ARRAY') {
 7269:         for (my $i=0; $i<@{$types}; $i++) {
 7270:             if (defined($usertypes->{$types->[$i]})) {
 7271:                 my $rem = $i%($numinrow);
 7272:                 if ($rem == 0) {
 7273:                     if ($i > 0) {
 7274:                         $output .= '</tr>';
 7275:                     }
 7276:                     $output .= '<tr>';
 7277:                 }
 7278:                 my $check = ' ';
 7279:                 if (ref($settings) eq 'HASH') {
 7280:                     if (ref($settings->{$context}) eq 'ARRAY') {
 7281:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 7282:                             $check = ' checked="checked" ';
 7283:                         }
 7284:                     } elsif (ref($settings->{$context}) eq 'HASH') {
 7285:                         if (ref($settings->{$context}->{$types->[$i]}) eq 'HASH') {
 7286:                             $check = ' checked="checked" ';
 7287:                         }
 7288:                     } elsif ($context eq 'statustocreate') {
 7289:                         $check = ' checked="checked" ';
 7290:                     }
 7291:                 }
 7292:                 $output .= '<td class="LC_left_item">'.
 7293:                            '<span class="LC_nobreak"><label>'.
 7294:                            '<input type="checkbox" name="'.$context.'" '.
 7295:                            'value="'.$types->[$i].'"'.$check.'/>'.
 7296:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 7297:             }
 7298:         }
 7299:         $rem = @{$types}%($numinrow);
 7300:     }
 7301:     my $colsleft = $numinrow - $rem;
 7302:     if ($context eq 'overrides') {
 7303:         if ($colsleft > 1) {
 7304:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 7305:         } else {
 7306:             $output .= '<td class="LC_left_item">';
 7307:         }
 7308:         $output .= '&nbsp;';
 7309:     } else {
 7310:         if (($rem == 0) && (@{$types} > 0)) {
 7311:             $output .= '<tr>';
 7312:         }
 7313:         if ($colsleft > 1) {
 7314:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 7315:         } else {
 7316:             $output .= '<td class="LC_left_item">';
 7317:         }
 7318:         my $defcheck = ' ';
 7319:         if (ref($settings) eq 'HASH') {  
 7320:             if (ref($settings->{$context}) eq 'ARRAY') {
 7321:                 if (grep(/^default$/,@{$settings->{$context}})) {
 7322:                     $defcheck = ' checked="checked" ';
 7323:                 }
 7324:             } elsif ($context eq 'statustocreate') {
 7325:                 $defcheck = ' checked="checked" ';
 7326:             }
 7327:         }
 7328:         $output .= '<span class="LC_nobreak"><label>'.
 7329:                    '<input type="checkbox" name="'.$context.'" '.
 7330:                    'value="default"'.$defcheck.'/>'.
 7331:                    $othertitle.'</label></span>';
 7332:     }
 7333:     $output .= '</td></tr></table></td></tr>';
 7334:     return $output;
 7335: }
 7336: 
 7337: sub sorted_searchtitles {
 7338:     my %searchtitles = &Apache::lonlocal::texthash(
 7339:                          'uname' => 'username',
 7340:                          'lastname' => 'last name',
 7341:                          'lastfirst' => 'last name, first name',
 7342:                      );
 7343:     my @titleorder = ('uname','lastname','lastfirst');
 7344:     return (\%searchtitles,\@titleorder);
 7345: }
 7346: 
 7347: sub sorted_searchtypes {
 7348:     my %srchtypes_desc = (
 7349:                            exact    => 'is exact match',
 7350:                            contains => 'contains ..',
 7351:                            begins   => 'begins with ..',
 7352:                          );
 7353:     my @srchtypeorder = ('exact','begins','contains');
 7354:     return (\%srchtypes_desc,\@srchtypeorder);
 7355: }
 7356: 
 7357: sub usertype_update_row {
 7358:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 7359:     my $datatable;
 7360:     my $numinrow = 4;
 7361:     foreach my $type (@{$types}) {
 7362:         if (defined($usertypes->{$type})) {
 7363:             $$rownums ++;
 7364:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 7365:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 7366:                           '</td><td class="LC_left_item"><table>';
 7367:             for (my $i=0; $i<@{$fields}; $i++) {
 7368:                 my $rem = $i%($numinrow);
 7369:                 if ($rem == 0) {
 7370:                     if ($i > 0) {
 7371:                         $datatable .= '</tr>';
 7372:                     }
 7373:                     $datatable .= '<tr>';
 7374:                 }
 7375:                 my $check = ' ';
 7376:                 if (ref($settings) eq 'HASH') {
 7377:                     if (ref($settings->{'fields'}) eq 'HASH') {
 7378:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 7379:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 7380:                                 $check = ' checked="checked" ';
 7381:                             }
 7382:                         }
 7383:                     }
 7384:                 }
 7385: 
 7386:                 if ($i == @{$fields}-1) {
 7387:                     my $colsleft = $numinrow - $rem;
 7388:                     if ($colsleft > 1) {
 7389:                         $datatable .= '<td colspan="'.$colsleft.'">';
 7390:                     } else {
 7391:                         $datatable .= '<td>';
 7392:                     }
 7393:                 } else {
 7394:                     $datatable .= '<td>';
 7395:                 }
 7396:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7397:                               '<input type="checkbox" name="updateable_'.$type.
 7398:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 7399:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 7400:             }
 7401:             $datatable .= '</tr></table></td></tr>';
 7402:         }
 7403:     }
 7404:     return $datatable;
 7405: }
 7406: 
 7407: sub modify_login {
 7408:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 7409:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 7410:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 7411:     %title = ( coursecatalog => 'Display course catalog',
 7412:                adminmail => 'Display administrator E-mail address',
 7413:                helpdesk  => 'Display "Contact Helpdesk" link',
 7414:                newuser => 'Link for visitors to create a user account',
 7415:                loginheader => 'Log-in box header');
 7416:     @offon = ('off','on');
 7417:     if (ref($domconfig{login}) eq 'HASH') {
 7418:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 7419:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 7420:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 7421:             }
 7422:         }
 7423:     }
 7424:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 7425:                                            \%domconfig,\%loginhash);
 7426:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 7427:     foreach my $item (@toggles) {
 7428:         $loginhash{login}{$item} = $env{'form.'.$item};
 7429:     }
 7430:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 7431:     if (ref($colchanges{'login'}) eq 'HASH') {  
 7432:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 7433:                                          \%loginhash);
 7434:     }
 7435: 
 7436:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7437:     my %domservers = &Apache::lonnet::get_servers($dom);
 7438:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 7439:     if (keys(%servers) > 1) {
 7440:         foreach my $lonhost (keys(%servers)) {
 7441:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 7442:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 7443:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 7444:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 7445:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 7446:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7447:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7448:                         $changes{'loginvia'}{$lonhost} = 1;
 7449:                     } else {
 7450:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 7451:                         $changes{'loginvia'}{$lonhost} = 1;
 7452:                     }
 7453:                 } else {
 7454:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7455:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7456:                         $changes{'loginvia'}{$lonhost} = 1;
 7457:                     }
 7458:                 }
 7459:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 7460:                     foreach my $item (@loginvia_attribs) {
 7461:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 7462:                     }
 7463:                 } else {
 7464:                     foreach my $item (@loginvia_attribs) {
 7465:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 7466:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 7467:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 7468:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 7469:                                 $new = '/';
 7470:                             }
 7471:                         }
 7472:                         if (($item eq 'custompath') && 
 7473:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 7474:                             $new = '';
 7475:                         }
 7476:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 7477:                             $changes{'loginvia'}{$lonhost} = 1;
 7478:                         }
 7479:                         if ($item eq 'exempt') {
 7480:                             $new = &check_exempt_addresses($new);
 7481:                         }
 7482:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 7483:                     }
 7484:                 }
 7485:             } else {
 7486:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7487:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7488:                     $changes{'loginvia'}{$lonhost} = 1;
 7489:                     foreach my $item (@loginvia_attribs) {
 7490:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 7491:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 7492:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 7493:                                 $new = '/';
 7494:                             }
 7495:                         }
 7496:                         if (($item eq 'custompath') && 
 7497:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 7498:                             $new = '';
 7499:                         }
 7500:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 7501:                     }
 7502:                 }
 7503:             }
 7504:         }
 7505:     }
 7506: 
 7507:     my $servadm = $r->dir_config('lonAdmEMail');
 7508:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 7509:     if (ref($domconfig{'login'}) eq 'HASH') {
 7510:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 7511:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 7512:                 if ($lang eq 'nolang') {
 7513:                     push(@currlangs,$lang);
 7514:                 } elsif (defined($langchoices{$lang})) {
 7515:                     push(@currlangs,$lang);
 7516:                 } else {
 7517:                     next;
 7518:                 }
 7519:             }
 7520:         }
 7521:     }
 7522:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 7523:     if (@currlangs > 0) {
 7524:         foreach my $lang (@currlangs) {
 7525:             if (grep(/^\Q$lang\E$/,@delurls)) {
 7526:                 $changes{'helpurl'}{$lang} = 1;
 7527:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 7528:                 $changes{'helpurl'}{$lang} = 1;
 7529:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 7530:                 push(@newlangs,$lang);
 7531:             } else {
 7532:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 7533:             }
 7534:         }
 7535:     }
 7536:     unless (grep(/^nolang$/,@currlangs)) {
 7537:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 7538:             $changes{'helpurl'}{'nolang'} = 1;
 7539:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 7540:             push(@newlangs,'nolang');
 7541:         }
 7542:     }
 7543:     if ($env{'form.loginhelpurl_add_lang'}) {
 7544:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 7545:             ($env{'form.loginhelpurl_add_file.filename'})) {
 7546:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 7547:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 7548:         }
 7549:     }
 7550:     if ((@newlangs > 0) || ($addedfile)) {
 7551:         my $error;
 7552:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 7553:         if ($configuserok eq 'ok') {
 7554:             if ($switchserver) {
 7555:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 7556:             } elsif ($author_ok eq 'ok') {
 7557:                 my @allnew = @newlangs;
 7558:                 if ($addedfile ne '') {
 7559:                     push(@allnew,$addedfile);
 7560:                 }
 7561:                 foreach my $lang (@allnew) {
 7562:                     my $formelem = 'loginhelpurl_'.$lang;
 7563:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 7564:                         $formelem = 'loginhelpurl_add_file';
 7565:                     }
 7566:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 7567:                                                                "help/$lang",'','',$newfile{$lang});
 7568:                     if ($result eq 'ok') {
 7569:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 7570:                         $changes{'helpurl'}{$lang} = 1;
 7571:                     } else {
 7572:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 7573:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 7574:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 7575:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 7576:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 7577:                         }
 7578:                     }
 7579:                 }
 7580:             } else {
 7581:                 $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);
 7582:             }
 7583:         } else {
 7584:             $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);
 7585:         }
 7586:         if ($error) {
 7587:             &Apache::lonnet::logthis($error);
 7588:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7589:         }
 7590:     }
 7591: 
 7592:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
 7593:     if (ref($domconfig{'login'}) eq 'HASH') {
 7594:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
 7595:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
 7596:                 if ($domservers{$lonhost}) {
 7597:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 7598:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
 7599:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
 7600:                     }
 7601:                 }
 7602:             }
 7603:         }
 7604:     }
 7605:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
 7606:     foreach my $lonhost (sort(keys(%domservers))) {
 7607:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 7608:             $changes{'headtag'}{$lonhost} = 1;
 7609:         } else {
 7610:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
 7611:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
 7612:             }
 7613:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
 7614:                 push(@newhosts,$lonhost);
 7615:             } elsif ($currheadtagurls{$lonhost}) {
 7616:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
 7617:                 if ($currexempt{$lonhost}) {
 7618:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
 7619:                         $changes{'headtag'}{$lonhost} = 1;
 7620:                     }
 7621:                 } elsif ($possexempt{$lonhost}) {
 7622:                     $changes{'headtag'}{$lonhost} = 1;
 7623:                 }
 7624:                 if ($possexempt{$lonhost}) {
 7625:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 7626:                 }
 7627:             }
 7628:         }
 7629:     }
 7630:     if (@newhosts) {
 7631:         my $error;
 7632:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 7633:         if ($configuserok eq 'ok') {
 7634:             if ($switchserver) {
 7635:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
 7636:             } elsif ($author_ok eq 'ok') {
 7637:                 foreach my $lonhost (@newhosts) {
 7638:                     my $formelem = 'loginheadtag_'.$lonhost;
 7639:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 7640:                                                                           "login/headtag/$lonhost",'','',
 7641:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
 7642:                     if ($result eq 'ok') {
 7643:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
 7644:                         $changes{'headtag'}{$lonhost} = 1;
 7645:                         if ($possexempt{$lonhost}) {
 7646:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 7647:                         }
 7648:                     } else {
 7649:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
 7650:                                            $newheadtagurls{$lonhost},$result);
 7651:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 7652:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
 7653:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
 7654:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
 7655:                         }
 7656:                     }
 7657:                 }
 7658:             } else {
 7659:                 $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);
 7660:             }
 7661:         } else {
 7662:             $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);
 7663:         }
 7664:         if ($error) {
 7665:             &Apache::lonnet::logthis($error);
 7666:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7667:         }
 7668:     }
 7669:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 7670: 
 7671:     my $defaulthelpfile = '/adm/loginproblems.html';
 7672:     my $defaulttext = &mt('Default in use');
 7673: 
 7674:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 7675:                                              $dom);
 7676:     if ($putresult eq 'ok') {
 7677:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 7678:         my %defaultchecked = (
 7679:                     'coursecatalog' => 'on',
 7680:                     'helpdesk'      => 'on',
 7681:                     'adminmail'     => 'off',
 7682:                     'newuser'       => 'off',
 7683:         );
 7684:         if (ref($domconfig{'login'}) eq 'HASH') {
 7685:             foreach my $item (@toggles) {
 7686:                 if ($defaultchecked{$item} eq 'on') { 
 7687:                     if (($domconfig{'login'}{$item} eq '0') &&
 7688:                         ($env{'form.'.$item} eq '1')) {
 7689:                         $changes{$item} = 1;
 7690:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 7691:                               $domconfig{'login'}{$item} eq '1') &&
 7692:                              ($env{'form.'.$item} eq '0')) {
 7693:                         $changes{$item} = 1;
 7694:                     }
 7695:                 } elsif ($defaultchecked{$item} eq 'off') {
 7696:                     if (($domconfig{'login'}{$item} eq '1') &&
 7697:                         ($env{'form.'.$item} eq '0')) {
 7698:                         $changes{$item} = 1;
 7699:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 7700:                               $domconfig{'login'}{$item} eq '0') &&
 7701:                              ($env{'form.'.$item} eq '1')) {
 7702:                         $changes{$item} = 1;
 7703:                     }
 7704:                 }
 7705:             }
 7706:         }
 7707:         if (keys(%changes) > 0 || $colchgtext) {
 7708:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 7709:             if (ref($lastactref) eq 'HASH') {
 7710:                 $lastactref->{'domainconfig'} = 1;
 7711:             }
 7712:             $resulttext = &mt('Changes made:').'<ul>';
 7713:             foreach my $item (sort(keys(%changes))) {
 7714:                 if ($item eq 'loginvia') {
 7715:                     if (ref($changes{$item}) eq 'HASH') {
 7716:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 7717:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 7718:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 7719:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 7720:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 7721:                                     $protocol = 'http' if ($protocol ne 'https');
 7722:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 7723: 
 7724:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 7725:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 7726:                                     } else {
 7727:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 7728:                                     }
 7729:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 7730:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 7731:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 7732:                                     }
 7733:                                     $resulttext .= '</li>';
 7734:                                 } else {
 7735:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 7736:                                 }
 7737:                             } else {
 7738:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 7739:                             }
 7740:                         }
 7741:                         $resulttext .= '</ul></li>';
 7742:                     }
 7743:                 } elsif ($item eq 'helpurl') {
 7744:                     if (ref($changes{$item}) eq 'HASH') {
 7745:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 7746:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 7747:                                 my ($chg,$link);
 7748:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 7749:                                 if ($lang eq 'nolang') {
 7750:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 7751:                                 } else {
 7752:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 7753:                                 }
 7754:                                 $resulttext .= '<li>'.$chg.'</li>';
 7755:                             } else {
 7756:                                 my $chg;
 7757:                                 if ($lang eq 'nolang') {
 7758:                                     $chg = &mt('custom log-in help file for no preferred language');
 7759:                                 } else {
 7760:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 7761:                                 }
 7762:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 7763:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 7764:                                                       '?inhibitmenu=yes',$chg,600,500).
 7765:                                                '</li>';
 7766:                             }
 7767:                         }
 7768:                     }
 7769:                 } elsif ($item eq 'headtag') {
 7770:                     if (ref($changes{$item}) eq 'HASH') {
 7771:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 7772:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 7773:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
 7774:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 7775:                                 $resulttext .= '<li><a href="'.
 7776:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
 7777:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 7778:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
 7779:                                 if ($possexempt{$lonhost}) {
 7780:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
 7781:                                 } else {
 7782:                                     $resulttext .= &mt('included for any client IP');
 7783:                                 }
 7784:                                 $resulttext .= '</li>';
 7785:                             }
 7786:                         }
 7787:                     }
 7788:                 } elsif ($item eq 'captcha') {
 7789:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7790:                         my $chgtxt;
 7791:                         if ($loginhash{'login'}{$item} eq 'notused') {
 7792:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 7793:                         } else {
 7794:                             my %captchas = &captcha_phrases();
 7795:                             if ($captchas{$loginhash{'login'}{$item}}) {
 7796:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 7797:                             } else {
 7798:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 7799:                             }
 7800:                         }
 7801:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 7802:                     }
 7803:                 } elsif ($item eq 'recaptchakeys') {
 7804:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7805:                         my ($privkey,$pubkey);
 7806:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 7807:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 7808:                             $privkey = $loginhash{'login'}{$item}{'private'};
 7809:                         }
 7810:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 7811:                         if (!$pubkey) {
 7812:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 7813:                         } else {
 7814:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 7815:                         }
 7816:                         if (!$privkey) {
 7817:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 7818:                         } else {
 7819:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 7820:                         }
 7821:                         $chgtxt .= '</ul>';
 7822:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 7823:                     }
 7824:                 } elsif ($item eq 'recaptchaversion') {
 7825:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7826:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
 7827:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
 7828:                                            '</li>';
 7829:                         }
 7830:                     }
 7831:                 } else {
 7832:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 7833:                 }
 7834:             }
 7835:             $resulttext .= $colchgtext.'</ul>';
 7836:         } else {
 7837:             $resulttext = &mt('No changes made to log-in page settings');
 7838:         }
 7839:     } else {
 7840:         $resulttext = '<span class="LC_error">'.
 7841: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7842:     }
 7843:     if ($errors) {
 7844:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 7845:                        $errors.'</ul>';
 7846:     }
 7847:     return $resulttext;
 7848: }
 7849: 
 7850: sub check_exempt_addresses {
 7851:     my ($iplist) = @_;
 7852:     $iplist =~ s/^\s+//;
 7853:     $iplist =~ s/\s+$//;
 7854:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
 7855:     my (@okips,$new);
 7856:     foreach my $ip (@poss_ips) {
 7857:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 7858:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 7859:                 push(@okips,$ip);
 7860:             }
 7861:         }
 7862:     }
 7863:     if (@okips > 0) {
 7864:         $new = join(',',@okips);
 7865:     } else {
 7866:         $new = '';
 7867:     }
 7868:     return $new;
 7869: }
 7870: 
 7871: sub color_font_choices {
 7872:     my %choices =
 7873:         &Apache::lonlocal::texthash (
 7874:             img => "Header",
 7875:             bgs => "Background colors",
 7876:             links => "Link colors",
 7877:             images => "Images",
 7878:             font => "Font color",
 7879:             fontmenu => "Font menu",
 7880:             pgbg => "Page",
 7881:             tabbg => "Header",
 7882:             sidebg => "Border",
 7883:             link => "Link",
 7884:             alink => "Active link",
 7885:             vlink => "Visited link",
 7886:         );
 7887:     return %choices;
 7888: }
 7889: 
 7890: sub modify_rolecolors {
 7891:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 7892:     my ($resulttext,%rolehash);
 7893:     $rolehash{'rolecolors'} = {};
 7894:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 7895:         if ($domconfig{'rolecolors'} eq '') {
 7896:             $domconfig{'rolecolors'} = {};
 7897:         }
 7898:     }
 7899:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 7900:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 7901:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 7902:                                              $dom);
 7903:     if ($putresult eq 'ok') {
 7904:         if (keys(%changes) > 0) {
 7905:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 7906:             if (ref($lastactref) eq 'HASH') {
 7907:                 $lastactref->{'domainconfig'} = 1;
 7908:             }
 7909:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 7910:                                              $rolehash{'rolecolors'});
 7911:         } else {
 7912:             $resulttext = &mt('No changes made to default color schemes');
 7913:         }
 7914:     } else {
 7915:         $resulttext = '<span class="LC_error">'.
 7916: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7917:     }
 7918:     if ($errors) {
 7919:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 7920:                        $errors.'</ul>';
 7921:     }
 7922:     return $resulttext;
 7923: }
 7924: 
 7925: sub modify_colors {
 7926:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 7927:     my (%changes,%choices);
 7928:     my @bgs;
 7929:     my @links = ('link','alink','vlink');
 7930:     my @logintext;
 7931:     my @images;
 7932:     my $servadm = $r->dir_config('lonAdmEMail');
 7933:     my $errors;
 7934:     my %defaults;
 7935:     foreach my $role (@{$roles}) {
 7936:         if ($role eq 'login') {
 7937:             %choices = &login_choices();
 7938:             @logintext = ('textcol','bgcol');
 7939:         } else {
 7940:             %choices = &color_font_choices();
 7941:         }
 7942:         if ($role eq 'login') {
 7943:             @images = ('img','logo','domlogo','login');
 7944:             @bgs = ('pgbg','mainbg','sidebg');
 7945:         } else {
 7946:             @images = ('img');
 7947:             @bgs = ('pgbg','tabbg','sidebg');
 7948:         }
 7949:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 7950:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 7951:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 7952:         }
 7953:         if ($role eq 'login') {
 7954:             foreach my $item (@logintext) {
 7955:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 7956:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 7957:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 7958:                 }
 7959:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 7960:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 7961:                 }
 7962:             }
 7963:         } else {
 7964:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 7965:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 7966:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 7967:             }
 7968:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 7969:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 7970:             }
 7971:         }
 7972:         foreach my $item (@bgs) {
 7973:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 7974:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 7975:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 7976:             }
 7977:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 7978:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 7979:             }
 7980:         }
 7981:         foreach my $item (@links) {
 7982:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 7983:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 7984:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 7985:             }
 7986:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 7987:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 7988:             }
 7989:         }
 7990:         my ($configuserok,$author_ok,$switchserver) = 
 7991:             &config_check($dom,$confname,$servadm);
 7992:         my ($width,$height) = &thumb_dimensions();
 7993:         if (ref($domconfig->{$role}) ne 'HASH') {
 7994:             $domconfig->{$role} = {};
 7995:         }
 7996:         foreach my $img (@images) {
 7997:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 7998:                 if (defined($env{'form.login_showlogo_'.$img})) {
 7999:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 8000:                 } else { 
 8001:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 8002:                 }
 8003:             } 
 8004: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 8005: 		 && !defined($domconfig->{$role}{$img})
 8006: 		 && !$env{'form.'.$role.'_del_'.$img}
 8007: 		 && $env{'form.'.$role.'_import_'.$img}) {
 8008: 		# import the old configured image from the .tab setting
 8009: 		# if they haven't provided a new one 
 8010: 		$domconfig->{$role}{$img} = 
 8011: 		    $env{'form.'.$role.'_import_'.$img};
 8012: 	    }
 8013:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 8014:                 my $error;
 8015:                 if ($configuserok eq 'ok') {
 8016:                     if ($switchserver) {
 8017:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 8018:                     } else {
 8019:                         if ($author_ok eq 'ok') {
 8020:                             my ($result,$logourl) = 
 8021:                                 &publishlogo($r,'upload',$role.'_'.$img,
 8022:                                            $dom,$confname,$img,$width,$height);
 8023:                             if ($result eq 'ok') {
 8024:                                 $confhash->{$role}{$img} = $logourl;
 8025:                                 $changes{$role}{'images'}{$img} = 1;
 8026:                             } else {
 8027:                                 $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);
 8028:                             }
 8029:                         } else {
 8030:                             $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);
 8031:                         }
 8032:                     }
 8033:                 } else {
 8034:                     $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);
 8035:                 }
 8036:                 if ($error) {
 8037:                     &Apache::lonnet::logthis($error);
 8038:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8039:                 }
 8040:             } elsif ($domconfig->{$role}{$img} ne '') {
 8041:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 8042:                     my $error;
 8043:                     if ($configuserok eq 'ok') {
 8044: # is confname an author?
 8045:                         if ($switchserver eq '') {
 8046:                             if ($author_ok eq 'ok') {
 8047:                                 my ($result,$logourl) = 
 8048:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 8049:                                             $dom,$confname,$img,$width,$height);
 8050:                                 if ($result eq 'ok') {
 8051:                                     $confhash->{$role}{$img} = $logourl;
 8052: 				    $changes{$role}{'images'}{$img} = 1;
 8053:                                 }
 8054:                             }
 8055:                         }
 8056:                     }
 8057:                 }
 8058:             }
 8059:         }
 8060:         if (ref($domconfig) eq 'HASH') {
 8061:             if (ref($domconfig->{$role}) eq 'HASH') {
 8062:                 foreach my $img (@images) {
 8063:                     if ($domconfig->{$role}{$img} ne '') {
 8064:                         if ($env{'form.'.$role.'_del_'.$img}) {
 8065:                             $confhash->{$role}{$img} = '';
 8066:                             $changes{$role}{'images'}{$img} = 1;
 8067:                         } else {
 8068:                             if ($confhash->{$role}{$img} eq '') {
 8069:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 8070:                             }
 8071:                         }
 8072:                     } else {
 8073:                         if ($env{'form.'.$role.'_del_'.$img}) {
 8074:                             $confhash->{$role}{$img} = '';
 8075:                             $changes{$role}{'images'}{$img} = 1;
 8076:                         } 
 8077:                     }
 8078:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 8079:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 8080:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 8081:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 8082:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 8083:                             }
 8084:                         } else {
 8085:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 8086:                                 $changes{$role}{'showlogo'}{$img} = 1;
 8087:                             }
 8088:                         }
 8089:                     }
 8090:                 }
 8091:                 if ($domconfig->{$role}{'font'} ne '') {
 8092:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 8093:                         $changes{$role}{'font'} = 1;
 8094:                     }
 8095:                 } else {
 8096:                     if ($confhash->{$role}{'font'}) {
 8097:                         $changes{$role}{'font'} = 1;
 8098:                     }
 8099:                 }
 8100:                 if ($role ne 'login') {
 8101:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 8102:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 8103:                             $changes{$role}{'fontmenu'} = 1;
 8104:                         }
 8105:                     } else {
 8106:                         if ($confhash->{$role}{'fontmenu'}) {
 8107:                             $changes{$role}{'fontmenu'} = 1;
 8108:                         }
 8109:                     }
 8110:                 }
 8111:                 foreach my $item (@bgs) {
 8112:                     if ($domconfig->{$role}{$item} ne '') {
 8113:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8114:                             $changes{$role}{'bgs'}{$item} = 1;
 8115:                         } 
 8116:                     } else {
 8117:                         if ($confhash->{$role}{$item}) {
 8118:                             $changes{$role}{'bgs'}{$item} = 1;
 8119:                         }
 8120:                     }
 8121:                 }
 8122:                 foreach my $item (@links) {
 8123:                     if ($domconfig->{$role}{$item} ne '') {
 8124:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8125:                             $changes{$role}{'links'}{$item} = 1;
 8126:                         }
 8127:                     } else {
 8128:                         if ($confhash->{$role}{$item}) {
 8129:                             $changes{$role}{'links'}{$item} = 1;
 8130:                         }
 8131:                     }
 8132:                 }
 8133:                 foreach my $item (@logintext) {
 8134:                     if ($domconfig->{$role}{$item} ne '') {
 8135:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8136:                             $changes{$role}{'logintext'}{$item} = 1;
 8137:                         }
 8138:                     } else {
 8139:                         if ($confhash->{$role}{$item}) {
 8140:                             $changes{$role}{'logintext'}{$item} = 1;
 8141:                         }
 8142:                     }
 8143:                 }
 8144:             } else {
 8145:                 &default_change_checker($role,\@images,\@links,\@bgs,
 8146:                                         \@logintext,$confhash,\%changes); 
 8147:             }
 8148:         } else {
 8149:             &default_change_checker($role,\@images,\@links,\@bgs,
 8150:                                     \@logintext,$confhash,\%changes); 
 8151:         }
 8152:     }
 8153:     return ($errors,%changes);
 8154: }
 8155: 
 8156: sub config_check {
 8157:     my ($dom,$confname,$servadm) = @_;
 8158:     my ($configuserok,$author_ok,$switchserver,%currroles);
 8159:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 8160:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 8161:                                                    $confname,$servadm);
 8162:     if ($configuserok eq 'ok') {
 8163:         $switchserver = &check_switchserver($dom,$confname);
 8164:         if ($switchserver eq '') {
 8165:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 8166:         }
 8167:     }
 8168:     return ($configuserok,$author_ok,$switchserver);
 8169: }
 8170: 
 8171: sub default_change_checker {
 8172:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 8173:     foreach my $item (@{$links}) {
 8174:         if ($confhash->{$role}{$item}) {
 8175:             $changes->{$role}{'links'}{$item} = 1;
 8176:         }
 8177:     }
 8178:     foreach my $item (@{$bgs}) {
 8179:         if ($confhash->{$role}{$item}) {
 8180:             $changes->{$role}{'bgs'}{$item} = 1;
 8181:         }
 8182:     }
 8183:     foreach my $item (@{$logintext}) {
 8184:         if ($confhash->{$role}{$item}) {
 8185:             $changes->{$role}{'logintext'}{$item} = 1;
 8186:         }
 8187:     }
 8188:     foreach my $img (@{$images}) {
 8189:         if ($env{'form.'.$role.'_del_'.$img}) {
 8190:             $confhash->{$role}{$img} = '';
 8191:             $changes->{$role}{'images'}{$img} = 1;
 8192:         }
 8193:         if ($role eq 'login') {
 8194:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 8195:                 $changes->{$role}{'showlogo'}{$img} = 1;
 8196:             }
 8197:         }
 8198:     }
 8199:     if ($confhash->{$role}{'font'}) {
 8200:         $changes->{$role}{'font'} = 1;
 8201:     }
 8202: }
 8203: 
 8204: sub display_colorchgs {
 8205:     my ($dom,$changes,$roles,$confhash) = @_;
 8206:     my (%choices,$resulttext);
 8207:     if (!grep(/^login$/,@{$roles})) {
 8208:         $resulttext = &mt('Changes made:').'<br />';
 8209:     }
 8210:     foreach my $role (@{$roles}) {
 8211:         if ($role eq 'login') {
 8212:             %choices = &login_choices();
 8213:         } else {
 8214:             %choices = &color_font_choices();
 8215:         }
 8216:         if (ref($changes->{$role}) eq 'HASH') {
 8217:             if ($role ne 'login') {
 8218:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 8219:             }
 8220:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 8221:                 if ($role ne 'login') {
 8222:                     $resulttext .= '<ul>';
 8223:                 }
 8224:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 8225:                     if ($role ne 'login') {
 8226:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 8227:                     }
 8228:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 8229:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 8230:                             if ($confhash->{$role}{$key}{$item}) {
 8231:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 8232:                             } else {
 8233:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 8234:                             }
 8235:                         } elsif ($confhash->{$role}{$item} eq '') {
 8236:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 8237:                         } else {
 8238:                             my $newitem = $confhash->{$role}{$item};
 8239:                             if ($key eq 'images') {
 8240:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 8241:                             }
 8242:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 8243:                         }
 8244:                     }
 8245:                     if ($role ne 'login') {
 8246:                         $resulttext .= '</ul></li>';
 8247:                     }
 8248:                 } else {
 8249:                     if ($confhash->{$role}{$key} eq '') {
 8250:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 8251:                     } else {
 8252:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 8253:                     }
 8254:                 }
 8255:                 if ($role ne 'login') {
 8256:                     $resulttext .= '</ul>';
 8257:                 }
 8258:             }
 8259:         }
 8260:     }
 8261:     return $resulttext;
 8262: }
 8263: 
 8264: sub thumb_dimensions {
 8265:     return ('200','50');
 8266: }
 8267: 
 8268: sub check_dimensions {
 8269:     my ($inputfile) = @_;
 8270:     my ($fullwidth,$fullheight);
 8271:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 8272:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 8273:             my $imageinfo = <PIPE>;
 8274:             if (!close(PIPE)) {
 8275:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 8276:             }
 8277:             chomp($imageinfo);
 8278:             my ($fullsize) = 
 8279:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 8280:             if ($fullsize) {
 8281:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 8282:             }
 8283:         }
 8284:     }
 8285:     return ($fullwidth,$fullheight);
 8286: }
 8287: 
 8288: sub check_configuser {
 8289:     my ($uhome,$dom,$confname,$servadm) = @_;
 8290:     my ($configuserok,%currroles);
 8291:     if ($uhome eq 'no_host') {
 8292:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 8293:         my $configpass = &LONCAPA::Enrollment::create_password();
 8294:         $configuserok = 
 8295:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 8296:                              $configpass,'','','','','',undef,$servadm);
 8297:     } else {
 8298:         $configuserok = 'ok';
 8299:         %currroles = 
 8300:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 8301:     }
 8302:     return ($configuserok,%currroles);
 8303: }
 8304: 
 8305: sub check_authorstatus {
 8306:     my ($dom,$confname,%currroles) = @_;
 8307:     my $author_ok;
 8308:     if (!$currroles{':'.$dom.':au'}) {
 8309:         my $start = time;
 8310:         my $end = 0;
 8311:         $author_ok = 
 8312:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 8313:                                         'au',$end,$start,'','','domconfig');
 8314:     } else {
 8315:         $author_ok = 'ok';
 8316:     }
 8317:     return $author_ok;
 8318: }
 8319: 
 8320: sub publishlogo {
 8321:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 8322:     my ($output,$fname,$logourl);
 8323:     if ($action eq 'upload') {
 8324:         $fname=$env{'form.'.$formname.'.filename'};
 8325:         chop($env{'form.'.$formname});
 8326:     } else {
 8327:         ($fname) = ($formname =~ /([^\/]+)$/);
 8328:     }
 8329:     if ($savefileas ne '') {
 8330:         $fname = $savefileas;
 8331:     }
 8332:     $fname=&Apache::lonnet::clean_filename($fname);
 8333: # See if there is anything left
 8334:     unless ($fname) { return ('error: no uploaded file'); }
 8335:     $fname="$subdir/$fname";
 8336:     my $docroot=$r->dir_config('lonDocRoot');
 8337:     my $filepath="$docroot/priv";
 8338:     my $relpath = "$dom/$confname";
 8339:     my ($fnamepath,$file,$fetchthumb);
 8340:     $file=$fname;
 8341:     if ($fname=~m|/|) {
 8342:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 8343:     }
 8344:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 8345:     my $count;
 8346:     for ($count=5;$count<=$#parts;$count++) {
 8347:         $filepath.="/$parts[$count]";
 8348:         if ((-e $filepath)!=1) {
 8349:             mkdir($filepath,02770);
 8350:         }
 8351:     }
 8352:     # Check for bad extension and disallow upload
 8353:     if ($file=~/\.(\w+)$/ &&
 8354:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 8355:         $output = 
 8356:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 8357:     } elsif ($file=~/\.(\w+)$/ &&
 8358:         !defined(&Apache::loncommon::fileembstyle($1))) {
 8359:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 8360:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 8361:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 8362:     } elsif (-d "$filepath/$file") {
 8363:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 8364:     } else {
 8365:         my $source = $filepath.'/'.$file;
 8366:         my $logfile;
 8367:         if (!open($logfile,">>$source".'.log')) {
 8368:             return (&mt('No write permission to Authoring Space'));
 8369:         }
 8370:         print $logfile
 8371: "\n================= Publish ".localtime()." ================\n".
 8372: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 8373: # Save the file
 8374:         if (!open(FH,'>'.$source)) {
 8375:             &Apache::lonnet::logthis('Failed to create '.$source);
 8376:             return (&mt('Failed to create file'));
 8377:         }
 8378:         if ($action eq 'upload') {
 8379:             if (!print FH ($env{'form.'.$formname})) {
 8380:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 8381:                 return (&mt('Failed to write file'));
 8382:             }
 8383:         } else {
 8384:             my $original = &Apache::lonnet::filelocation('',$formname);
 8385:             if(!copy($original,$source)) {
 8386:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 8387:                 return (&mt('Failed to write file'));
 8388:             }
 8389:         }
 8390:         close(FH);
 8391:         chmod(0660, $source); # Permissions to rw-rw---.
 8392: 
 8393:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 8394:         my $copyfile=$targetdir.'/'.$file;
 8395: 
 8396:         my @parts=split(/\//,$targetdir);
 8397:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 8398:         for (my $count=5;$count<=$#parts;$count++) {
 8399:             $path.="/$parts[$count]";
 8400:             if (!-e $path) {
 8401:                 print $logfile "\nCreating directory ".$path;
 8402:                 mkdir($path,02770);
 8403:             }
 8404:         }
 8405:         my $versionresult;
 8406:         if (-e $copyfile) {
 8407:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 8408:         } else {
 8409:             $versionresult = 'ok';
 8410:         }
 8411:         if ($versionresult eq 'ok') {
 8412:             if (copy($source,$copyfile)) {
 8413:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 8414:                 $output = 'ok';
 8415:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 8416:                 push(@{$modified_urls},[$copyfile,$source]);
 8417:                 my $metaoutput = 
 8418:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 8419:                 unless ($registered_cleanup) {
 8420:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 8421:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 8422:                     $registered_cleanup=1;
 8423:                 }
 8424:             } else {
 8425:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 8426:                 $output = &mt('Failed to copy file to RES space').", $!";
 8427:             }
 8428:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 8429:                 my $inputfile = $filepath.'/'.$file;
 8430:                 my $outfile = $filepath.'/'.'tn-'.$file;
 8431:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 8432:                 if ($fullwidth ne '' && $fullheight ne '') { 
 8433:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 8434:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 8435:                         system("convert -sample $thumbsize $inputfile $outfile");
 8436:                         chmod(0660, $filepath.'/tn-'.$file);
 8437:                         if (-e $outfile) {
 8438:                             my $copyfile=$targetdir.'/tn-'.$file;
 8439:                             if (copy($outfile,$copyfile)) {
 8440:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 8441:                                 my $thumb_metaoutput = 
 8442:                                     &write_metadata($dom,$confname,$formname,
 8443:                                                     $targetdir,'tn-'.$file,$logfile);
 8444:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 8445:                                 unless ($registered_cleanup) {
 8446:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 8447:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 8448:                                     $registered_cleanup=1;
 8449:                                 }
 8450:                             } else {
 8451:                                 print $logfile "\nUnable to write ".$copyfile.
 8452:                                                ':'.$!."\n";
 8453:                             }
 8454:                         }
 8455:                     }
 8456:                 }
 8457:             }
 8458:         } else {
 8459:             $output = $versionresult;
 8460:         }
 8461:     }
 8462:     return ($output,$logourl);
 8463: }
 8464: 
 8465: sub logo_versioning {
 8466:     my ($targetdir,$file,$logfile) = @_;
 8467:     my $target = $targetdir.'/'.$file;
 8468:     my ($maxversion,$fn,$extn,$output);
 8469:     $maxversion = 0;
 8470:     if ($file =~ /^(.+)\.(\w+)$/) {
 8471:         $fn=$1;
 8472:         $extn=$2;
 8473:     }
 8474:     opendir(DIR,$targetdir);
 8475:     while (my $filename=readdir(DIR)) {
 8476:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 8477:             $maxversion=($1>$maxversion)?$1:$maxversion;
 8478:         }
 8479:     }
 8480:     $maxversion++;
 8481:     print $logfile "\nCreating old version ".$maxversion."\n";
 8482:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 8483:     if (copy($target,$copyfile)) {
 8484:         print $logfile "Copied old target to ".$copyfile."\n";
 8485:         $copyfile=$copyfile.'.meta';
 8486:         if (copy($target.'.meta',$copyfile)) {
 8487:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 8488:             $output = 'ok';
 8489:         } else {
 8490:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 8491:             $output = &mt('Failed to copy old meta').", $!, ";
 8492:         }
 8493:     } else {
 8494:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 8495:         $output = &mt('Failed to copy old target').", $!, ";
 8496:     }
 8497:     return $output;
 8498: }
 8499: 
 8500: sub write_metadata {
 8501:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 8502:     my (%metadatafields,%metadatakeys,$output);
 8503:     $metadatafields{'title'}=$formname;
 8504:     $metadatafields{'creationdate'}=time;
 8505:     $metadatafields{'lastrevisiondate'}=time;
 8506:     $metadatafields{'copyright'}='public';
 8507:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 8508:                                          $env{'user.domain'};
 8509:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 8510:     $metadatafields{'domain'}=$dom;
 8511:     {
 8512:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 8513:         my $mfh;
 8514:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 8515:             foreach (sort(keys(%metadatafields))) {
 8516:                 unless ($_=~/\./) {
 8517:                     my $unikey=$_;
 8518:                     $unikey=~/^([A-Za-z]+)/;
 8519:                     my $tag=$1;
 8520:                     $tag=~tr/A-Z/a-z/;
 8521:                     print $mfh "\n\<$tag";
 8522:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 8523:                         my $value=$metadatafields{$unikey.'.'.$_};
 8524:                         $value=~s/\"/\'\'/g;
 8525:                         print $mfh ' '.$_.'="'.$value.'"';
 8526:                     }
 8527:                     print $mfh '>'.
 8528:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 8529:                             .'</'.$tag.'>';
 8530:                 }
 8531:             }
 8532:             $output = 'ok';
 8533:             print $logfile "\nWrote metadata";
 8534:             close($mfh);
 8535:         } else {
 8536:             print $logfile "\nFailed to open metadata file";
 8537:             $output = &mt('Could not write metadata');
 8538:         }
 8539:     }
 8540:     return $output;
 8541: }
 8542: 
 8543: sub notifysubscribed {
 8544:     foreach my $targetsource (@{$modified_urls}){
 8545:         next unless (ref($targetsource) eq 'ARRAY');
 8546:         my ($target,$source)=@{$targetsource};
 8547:         if ($source ne '') {
 8548:             if (open(my $logfh,'>>'.$source.'.log')) {
 8549:                 print $logfh "\nCleanup phase: Notifications\n";
 8550:                 my @subscribed=&subscribed_hosts($target);
 8551:                 foreach my $subhost (@subscribed) {
 8552:                     print $logfh "\nNotifying host ".$subhost.':';
 8553:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 8554:                     print $logfh $reply;
 8555:                 }
 8556:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 8557:                 foreach my $subhost (@subscribedmeta) {
 8558:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 8559:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 8560:                                                         $subhost);
 8561:                     print $logfh $reply;
 8562:                 }
 8563:                 print $logfh "\n============ Done ============\n";
 8564:                 close($logfh);
 8565:             }
 8566:         }
 8567:     }
 8568:     return OK;
 8569: }
 8570: 
 8571: sub subscribed_hosts {
 8572:     my ($target) = @_;
 8573:     my @subscribed;
 8574:     if (open(my $fh,"<$target.subscription")) {
 8575:         while (my $subline=<$fh>) {
 8576:             if ($subline =~ /^($match_lonid):/) {
 8577:                 my $host = $1;
 8578:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 8579:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 8580:                         push(@subscribed,$host);
 8581:                     }
 8582:                 }
 8583:             }
 8584:         }
 8585:     }
 8586:     return @subscribed;
 8587: }
 8588: 
 8589: sub check_switchserver {
 8590:     my ($dom,$confname) = @_;
 8591:     my ($allowed,$switchserver);
 8592:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 8593:     if ($home eq 'no_host') {
 8594:         $home = &Apache::lonnet::domain($dom,'primary');
 8595:     }
 8596:     my @ids=&Apache::lonnet::current_machine_ids();
 8597:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 8598:     if (!$allowed) {
 8599: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 8600:     }
 8601:     return $switchserver;
 8602: }
 8603: 
 8604: sub modify_quotas {
 8605:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 8606:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 8607:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 8608:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 8609:         $validationfieldsref);
 8610:     if ($action eq 'quotas') {
 8611:         $context = 'tools'; 
 8612:     } else {
 8613:         $context = $action;
 8614:     }
 8615:     if ($context eq 'requestcourses') {
 8616:         @usertools = ('official','unofficial','community','textbook');
 8617:         @options =('norequest','approval','validate','autolimit');
 8618:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 8619:         %titles = &courserequest_titles();
 8620:         $toolregexp = join('|',@usertools);
 8621:         %conditions = &courserequest_conditions();
 8622:         $confname = $dom.'-domainconfig';
 8623:         my $servadm = $r->dir_config('lonAdmEMail');
 8624:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 8625:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 8626:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 8627:     } elsif ($context eq 'requestauthor') {
 8628:         @usertools = ('author');
 8629:         %titles = &authorrequest_titles();
 8630:     } else {
 8631:         @usertools = ('aboutme','blog','webdav','portfolio');
 8632:         %titles = &tool_titles();
 8633:     }
 8634:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8635:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8636:     foreach my $key (keys(%env)) {
 8637:         if ($context eq 'requestcourses') {
 8638:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 8639:                 my $item = $1;
 8640:                 my $type = $2;
 8641:                 if ($type =~ /^limit_(.+)/) {
 8642:                     $limithash{$item}{$1} = $env{$key};
 8643:                 } else {
 8644:                     $confhash{$item}{$type} = $env{$key};
 8645:                 }
 8646:             }
 8647:         } elsif ($context eq 'requestauthor') {
 8648:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 8649:                 $confhash{$1} = $env{$key};
 8650:             }
 8651:         } else {
 8652:             if ($key =~ /^form\.quota_(.+)$/) {
 8653:                 $confhash{'defaultquota'}{$1} = $env{$key};
 8654:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 8655:                 $confhash{'authorquota'}{$1} = $env{$key};
 8656:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 8657:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 8658:             }
 8659:         }
 8660:     }
 8661:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 8662:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 8663:         @approvalnotify = sort(@approvalnotify);
 8664:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 8665:         my @crstypes = ('official','unofficial','community','textbook');
 8666:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 8667:         foreach my $type (@hasuniquecode) {
 8668:             if (grep(/^\Q$type\E$/,@crstypes)) {
 8669:                 $confhash{'uniquecode'}{$type} = 1;
 8670:             }
 8671:         }
 8672:         my (%newbook,%allpos);
 8673:         if ($context eq 'requestcourses') {
 8674:             foreach my $type ('textbooks','templates') {
 8675:                 @{$allpos{$type}} = (); 
 8676:                 my $invalid;
 8677:                 if ($type eq 'textbooks') {
 8678:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
 8679:                 } else {
 8680:                     $invalid = &mt('Invalid LON-CAPA course for template');
 8681:                 }
 8682:                 if ($env{'form.'.$type.'_addbook'}) {
 8683:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
 8684:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
 8685:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
 8686:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
 8687:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 8688:                         } else {
 8689:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
 8690:                             my $position = $env{'form.'.$type.'_addbook_pos'};
 8691:                             $position =~ s/\D+//g;
 8692:                             if ($position ne '') {
 8693:                                 $allpos{$type}[$position] = $newbook{$type};
 8694:                             }
 8695:                         }
 8696:                     } else {
 8697:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 8698:                     }
 8699:                 }
 8700:             } 
 8701:         }
 8702:         if (ref($domconfig{$action}) eq 'HASH') {
 8703:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 8704:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 8705:                     $changes{'notify'}{'approval'} = 1;
 8706:                 }
 8707:             } else {
 8708:                 if ($confhash{'notify'}{'approval'}) {
 8709:                     $changes{'notify'}{'approval'} = 1;
 8710:                 }
 8711:             }
 8712:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 8713:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 8714:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 8715:                         unless ($confhash{'uniquecode'}{$crstype}) {
 8716:                             $changes{'uniquecode'} = 1;
 8717:                         }
 8718:                     }
 8719:                     unless ($changes{'uniquecode'}) {
 8720:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 8721:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 8722:                                 $changes{'uniquecode'} = 1;
 8723:                             }
 8724:                         }
 8725:                     }
 8726:                } else {
 8727:                    $changes{'uniquecode'} = 1;
 8728:                }
 8729:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 8730:                 $changes{'uniquecode'} = 1;
 8731:             }
 8732:             if ($context eq 'requestcourses') {
 8733:                 foreach my $type ('textbooks','templates') {
 8734:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
 8735:                         my %deletions;
 8736:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
 8737:                         if (@todelete) {
 8738:                             map { $deletions{$_} = 1; } @todelete;
 8739:                         }
 8740:                         my %imgdeletions;
 8741:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
 8742:                         if (@todeleteimages) {
 8743:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
 8744:                         }
 8745:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
 8746:                         for (my $i=0; $i<=$maxnum; $i++) {
 8747:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
 8748:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
 8749:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
 8750:                                 if ($deletions{$key}) {
 8751:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
 8752:                                         #FIXME need to obsolete item in RES space
 8753:                                     }
 8754:                                     next;
 8755:                                 } else {
 8756:                                     my $newpos = $env{'form.'.$itemid};
 8757:                                     $newpos =~ s/\D+//g;
 8758:                                     foreach my $item ('subject','title','publisher','author') {
 8759:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 8760:                                                  ($type eq 'templates'));
 8761:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
 8762:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
 8763:                                             $changes{$type}{$key} = 1;
 8764:                                         }
 8765:                                     }
 8766:                                     $allpos{$type}[$newpos] = $key;
 8767:                                 }
 8768:                                 if ($imgdeletions{$key}) {
 8769:                                     $changes{$type}{$key} = 1;
 8770:                                     #FIXME need to obsolete item in RES space
 8771:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
 8772:                                     my ($cdom,$cnum) = split(/_/,$key);
 8773:                                     my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
 8774:                                                                                   $cdom,$cnum,$type,$configuserok,
 8775:                                                                                   $switchserver,$author_ok);
 8776:                                     if ($imgurl) {
 8777:                                         $confhash{$type}{$key}{'image'} = $imgurl;
 8778:                                         $changes{$type}{$key} = 1; 
 8779:                                     }
 8780:                                     if ($error) {
 8781:                                         &Apache::lonnet::logthis($error);
 8782:                                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8783:                                     } 
 8784:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
 8785:                                     $confhash{$type}{$key}{'image'} = 
 8786:                                         $domconfig{$action}{$type}{$key}{'image'};
 8787:                                 }
 8788:                             }
 8789:                         }
 8790:                     }
 8791:                 }
 8792:             }
 8793:         } else {
 8794:             if ($confhash{'notify'}{'approval'}) {
 8795:                 $changes{'notify'}{'approval'} = 1;
 8796:             }
 8797:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 8798:                 $changes{'uniquecode'} = 1;
 8799:             }
 8800:         }
 8801:         if ($context eq 'requestcourses') {
 8802:             foreach my $type ('textbooks','templates') {
 8803:                 if ($newbook{$type}) {
 8804:                     $changes{$type}{$newbook{$type}} = 1;
 8805:                     foreach my $item ('subject','title','publisher','author') {
 8806:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 8807:                                  ($type eq 'template'));
 8808:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
 8809:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
 8810:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
 8811:                         }
 8812:                     }
 8813:                     if ($type eq 'textbooks') {
 8814:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
 8815:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
 8816:                             my ($imageurl,$error) =
 8817:                                 &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
 8818:                                                         $configuserok,$switchserver,$author_ok);
 8819:                             if ($imageurl) {
 8820:                                 $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
 8821:                             }
 8822:                             if ($error) {
 8823:                                 &Apache::lonnet::logthis($error);
 8824:                                 $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8825:                             }
 8826:                         }
 8827:                     }
 8828:                 }
 8829:                 if (@{$allpos{$type}} > 0) {
 8830:                     my $idx = 0;
 8831:                     foreach my $item (@{$allpos{$type}}) {
 8832:                         if ($item ne '') {
 8833:                             $confhash{$type}{$item}{'order'} = $idx;
 8834:                             if (ref($domconfig{$action}) eq 'HASH') {
 8835:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
 8836:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
 8837:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
 8838:                                             $changes{$type}{$item} = 1;
 8839:                                         }
 8840:                                     }
 8841:                                 }
 8842:                             }
 8843:                             $idx ++;
 8844:                         }
 8845:                     }
 8846:                 }
 8847:             }
 8848:             if (ref($validationitemsref) eq 'ARRAY') {
 8849:                 foreach my $item (@{$validationitemsref}) {
 8850:                     if ($item eq 'fields') {
 8851:                         my @changed;
 8852:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 8853:                         if (@{$confhash{'validation'}{$item}} > 0) {
 8854:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 8855:                         }
 8856:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8857:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8858:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 8859:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 8860:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
 8861:                                 } else {
 8862:                                     @changed = @{$confhash{'validation'}{$item}};
 8863:                                 }
 8864:                             } else {
 8865:                                 @changed = @{$confhash{'validation'}{$item}};
 8866:                             }
 8867:                         } else {
 8868:                             @changed = @{$confhash{'validation'}{$item}};
 8869:                         }
 8870:                         if (@changed) {
 8871:                             if ($confhash{'validation'}{$item}) {
 8872:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 8873:                             } else {
 8874:                                 $changes{'validation'}{$item} = &mt('None');
 8875:                             }
 8876:                         }
 8877:                     } else {
 8878:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 8879:                         if ($item eq 'markup') {
 8880:                             if ($env{'form.requestcourses_validation_'.$item}) {
 8881:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 8882:                             }
 8883:                         }
 8884:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8885:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8886:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 8887:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 8888:                                 }
 8889:                             } else {
 8890:                                 if ($confhash{'validation'}{$item} ne '') {
 8891:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 8892:                                 }
 8893:                             }
 8894:                         } else {
 8895:                             if ($confhash{'validation'}{$item} ne '') {
 8896:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 8897:                             }
 8898:                         }
 8899:                     }
 8900:                 }
 8901:             }
 8902:             if ($env{'form.validationdc'}) {
 8903:                 my $newval = $env{'form.validationdc'};
 8904:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
 8905:                 if (exists($domcoords{$newval})) {
 8906:                     $confhash{'validation'}{'dc'} = $newval;
 8907:                 }
 8908:             }
 8909:             if (ref($confhash{'validation'}) eq 'HASH') {
 8910:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8911:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8912:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 8913:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 8914:                                 if ($confhash{'validation'}{'dc'} eq '') {
 8915:                                     $changes{'validation'}{'dc'} = &mt('None');
 8916:                                 } else {
 8917:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8918:                                 }
 8919:                             }
 8920:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
 8921:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8922:                         }
 8923:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 8924:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8925:                     }
 8926:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 8927:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8928:                 }
 8929:             } else {
 8930:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8931:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8932:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 8933:                             $changes{'validation'}{'dc'} = &mt('None');
 8934:                         }
 8935:                     }
 8936:                 }
 8937:             }
 8938:         }
 8939:     } else {
 8940:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 8941:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 8942:     }
 8943:     foreach my $item (@usertools) {
 8944:         foreach my $type (@{$types},'default','_LC_adv') {
 8945:             my $unset; 
 8946:             if ($context eq 'requestcourses') {
 8947:                 $unset = '0';
 8948:                 if ($type eq '_LC_adv') {
 8949:                     $unset = '';
 8950:                 }
 8951:                 if ($confhash{$item}{$type} eq 'autolimit') {
 8952:                     $confhash{$item}{$type} .= '=';
 8953:                     unless ($limithash{$item}{$type} =~ /\D/) {
 8954:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 8955:                     }
 8956:                 }
 8957:             } elsif ($context eq 'requestauthor') {
 8958:                 $unset = '0';
 8959:                 if ($type eq '_LC_adv') {
 8960:                     $unset = '';
 8961:                 }
 8962:             } else {
 8963:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 8964:                     $confhash{$item}{$type} = 1;
 8965:                 } else {
 8966:                     $confhash{$item}{$type} = 0;
 8967:                 }
 8968:             }
 8969:             if (ref($domconfig{$action}) eq 'HASH') {
 8970:                 if ($action eq 'requestauthor') {
 8971:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 8972:                         $changes{$type} = 1;
 8973:                     }
 8974:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 8975:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 8976:                         $changes{$item}{$type} = 1;
 8977:                     }
 8978:                 } else {
 8979:                     if ($context eq 'requestcourses') {
 8980:                         if ($confhash{$item}{$type} ne $unset) {
 8981:                             $changes{$item}{$type} = 1;
 8982:                         }
 8983:                     } else {
 8984:                         if (!$confhash{$item}{$type}) {
 8985:                             $changes{$item}{$type} = 1;
 8986:                         }
 8987:                     }
 8988:                 }
 8989:             } else {
 8990:                 if ($context eq 'requestcourses') {
 8991:                     if ($confhash{$item}{$type} ne $unset) {
 8992:                         $changes{$item}{$type} = 1;
 8993:                     }
 8994:                 } elsif ($context eq 'requestauthor') {
 8995:                     if ($confhash{$type} ne $unset) {
 8996:                         $changes{$type} = 1;
 8997:                     }
 8998:                 } else {
 8999:                     if (!$confhash{$item}{$type}) {
 9000:                         $changes{$item}{$type} = 1;
 9001:                     }
 9002:                 }
 9003:             }
 9004:         }
 9005:     }
 9006:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 9007:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 9008:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 9009:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 9010:                     if (exists($confhash{'defaultquota'}{$key})) {
 9011:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 9012:                             $changes{'defaultquota'}{$key} = 1;
 9013:                         }
 9014:                     } else {
 9015:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 9016:                     }
 9017:                 }
 9018:             } else {
 9019:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 9020:                     if (exists($confhash{'defaultquota'}{$key})) {
 9021:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 9022:                             $changes{'defaultquota'}{$key} = 1;
 9023:                         }
 9024:                     } else {
 9025:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 9026:                     }
 9027:                 }
 9028:             }
 9029:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 9030:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 9031:                     if (exists($confhash{'authorquota'}{$key})) {
 9032:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 9033:                             $changes{'authorquota'}{$key} = 1;
 9034:                         }
 9035:                     } else {
 9036:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 9037:                     }
 9038:                 }
 9039:             }
 9040:         }
 9041:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 9042:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 9043:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 9044:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 9045:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 9046:                             $changes{'defaultquota'}{$key} = 1;
 9047:                         }
 9048:                     } else {
 9049:                         if (!exists($domconfig{'quotas'}{$key})) {
 9050:                             $changes{'defaultquota'}{$key} = 1;
 9051:                         }
 9052:                     }
 9053:                 } else {
 9054:                     $changes{'defaultquota'}{$key} = 1;
 9055:                 }
 9056:             }
 9057:         }
 9058:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 9059:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 9060:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 9061:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 9062:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 9063:                             $changes{'authorquota'}{$key} = 1;
 9064:                         }
 9065:                     } else {
 9066:                         $changes{'authorquota'}{$key} = 1;
 9067:                     }
 9068:                 } else {
 9069:                     $changes{'authorquota'}{$key} = 1;
 9070:                 }
 9071:             }
 9072:         }
 9073:     }
 9074: 
 9075:     if ($context eq 'requestauthor') {
 9076:         $domdefaults{'requestauthor'} = \%confhash;
 9077:     } else {
 9078:         foreach my $key (keys(%confhash)) {
 9079:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
 9080:                 $domdefaults{$key} = $confhash{$key};
 9081:             }
 9082:         }
 9083:     }
 9084: 
 9085:     my %quotahash = (
 9086:                       $action => { %confhash }
 9087:                     );
 9088:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 9089:                                              $dom);
 9090:     if ($putresult eq 'ok') {
 9091:         if (keys(%changes) > 0) {
 9092:             my $cachetime = 24*60*60;
 9093:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9094:             if (ref($lastactref) eq 'HASH') {
 9095:                 $lastactref->{'domdefaults'} = 1;
 9096:             }
 9097:             $resulttext = &mt('Changes made:').'<ul>';
 9098:             unless (($context eq 'requestcourses') ||
 9099:                     ($context eq 'requestauthor')) {
 9100:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 9101:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 9102:                     foreach my $type (@{$types},'default') {
 9103:                         if (defined($changes{'defaultquota'}{$type})) {
 9104:                             my $typetitle = $usertypes->{$type};
 9105:                             if ($type eq 'default') {
 9106:                                 $typetitle = $othertitle;
 9107:                             }
 9108:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 9109:                         }
 9110:                     }
 9111:                     $resulttext .= '</ul></li>';
 9112:                 }
 9113:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 9114:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 9115:                     foreach my $type (@{$types},'default') {
 9116:                         if (defined($changes{'authorquota'}{$type})) {
 9117:                             my $typetitle = $usertypes->{$type};
 9118:                             if ($type eq 'default') {
 9119:                                 $typetitle = $othertitle;
 9120:                             }
 9121:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 9122:                         }
 9123:                     }
 9124:                     $resulttext .= '</ul></li>';
 9125:                 }
 9126:             }
 9127:             my %newenv;
 9128:             foreach my $item (@usertools) {
 9129:                 my (%haschgs,%inconf);
 9130:                 if ($context eq 'requestauthor') {
 9131:                     %haschgs = %changes;
 9132:                     %inconf = %confhash;
 9133:                 } else {
 9134:                     if (ref($changes{$item}) eq 'HASH') {
 9135:                         %haschgs = %{$changes{$item}};
 9136:                     }
 9137:                     if (ref($confhash{$item}) eq 'HASH') {
 9138:                         %inconf = %{$confhash{$item}};
 9139:                     }
 9140:                 }
 9141:                 if (keys(%haschgs) > 0) {
 9142:                     my $newacc = 
 9143:                         &Apache::lonnet::usertools_access($env{'user.name'},
 9144:                                                           $env{'user.domain'},
 9145:                                                           $item,'reload',$context);
 9146:                     if (($context eq 'requestcourses') ||
 9147:                         ($context eq 'requestauthor')) {
 9148:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 9149:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 9150:                         }
 9151:                     } else {
 9152:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 9153:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 9154:                         }
 9155:                     }
 9156:                     unless ($context eq 'requestauthor') {
 9157:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 9158:                     }
 9159:                     foreach my $type (@{$types},'default','_LC_adv') {
 9160:                         if ($haschgs{$type}) {
 9161:                             my $typetitle = $usertypes->{$type};
 9162:                             if ($type eq 'default') {
 9163:                                 $typetitle = $othertitle;
 9164:                             } elsif ($type eq '_LC_adv') {
 9165:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 9166:                             }
 9167:                             if ($inconf{$type}) {
 9168:                                 if ($context eq 'requestcourses') {
 9169:                                     my $cond;
 9170:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 9171:                                         if ($1 eq '') {
 9172:                                             $cond = &mt('(Automatic processing of any request).');
 9173:                                         } else {
 9174:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 9175:                                         }
 9176:                                     } else { 
 9177:                                         $cond = $conditions{$inconf{$type}};
 9178:                                     }
 9179:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 9180:                                 } elsif ($context eq 'requestauthor') {
 9181:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 9182:                                                              $titles{$inconf{$type}},$typetitle);
 9183: 
 9184:                                 } else {
 9185:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 9186:                                 }
 9187:                             } else {
 9188:                                 if ($type eq '_LC_adv') {
 9189:                                     if ($inconf{$type} eq '0') {
 9190:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 9191:                                     } else { 
 9192:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 9193:                                     }
 9194:                                 } else {
 9195:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 9196:                                 }
 9197:                             }
 9198:                         }
 9199:                     }
 9200:                     unless ($context eq 'requestauthor') {
 9201:                         $resulttext .= '</ul></li>';
 9202:                     }
 9203:                 }
 9204:             }
 9205:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 9206:                 if (ref($changes{'notify'}) eq 'HASH') {
 9207:                     if ($changes{'notify'}{'approval'}) {
 9208:                         if (ref($confhash{'notify'}) eq 'HASH') {
 9209:                             if ($confhash{'notify'}{'approval'}) {
 9210:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 9211:                             } else {
 9212:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 9213:                             }
 9214:                         }
 9215:                     }
 9216:                 }
 9217:             }
 9218:             if ($action eq 'requestcourses') {
 9219:                 my @offon = ('off','on');
 9220:                 if ($changes{'uniquecode'}) {
 9221:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 9222:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 9223:                         $resulttext .= '<li>'.
 9224:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 9225:                                        '</li>';
 9226:                     } else {
 9227:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 9228:                                        '</li>';
 9229:                     }
 9230:                 }
 9231:                 foreach my $type ('textbooks','templates') {
 9232:                     if (ref($changes{$type}) eq 'HASH') {
 9233:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
 9234:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
 9235:                             my %coursehash = &Apache::lonnet::coursedescription($key);
 9236:                             my $coursetitle = $coursehash{'description'};
 9237:                             my $position = $confhash{$type}{$key}{'order'} + 1;
 9238:                             $resulttext .= '<li>';
 9239:                             foreach my $item ('subject','title','publisher','author') {
 9240:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
 9241:                                          ($type eq 'templates'));
 9242:                                 my $name = $item.':';
 9243:                                 $name =~ s/^(\w)/\U$1/;
 9244:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
 9245:                             }
 9246:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 9247:                             if ($type eq 'textbooks') {
 9248:                                 if ($confhash{$type}{$key}{'image'}) {
 9249:                                     $resulttext .= ' '.&mt('Image: [_1]',
 9250:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
 9251:                                                    ' alt="Textbook cover" />').'<br />';
 9252:                                 }
 9253:                             }
 9254:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 9255:                         }
 9256:                         $resulttext .= '</ul></li>';
 9257:                     }
 9258:                 }
 9259:                 if (ref($changes{'validation'}) eq 'HASH') {
 9260:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 9261:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 9262:                         foreach my $item (@{$validationitemsref}) {
 9263:                             if (exists($changes{'validation'}{$item})) {
 9264:                                 if ($item eq 'markup') {
 9265:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9266:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 9267:                                 } else {
 9268:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9269:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 9270:                                 }
 9271:                             }
 9272:                         }
 9273:                         if (exists($changes{'validation'}{'dc'})) {
 9274:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 9275:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 9276:                         }
 9277:                     }
 9278:                 }
 9279:             }
 9280:             $resulttext .= '</ul>';
 9281:             if (keys(%newenv)) {
 9282:                 &Apache::lonnet::appenv(\%newenv);
 9283:             }
 9284:         } else {
 9285:             if ($context eq 'requestcourses') {
 9286:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 9287:             } elsif ($context eq 'requestauthor') {
 9288:                 $resulttext = &mt('No changes made to rights to request author space.');
 9289:             } else {
 9290:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 9291:             }
 9292:         }
 9293:     } else {
 9294:         $resulttext = '<span class="LC_error">'.
 9295: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9296:     }
 9297:     if ($errors) {
 9298:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 9299:                        '<ul>'.$errors.'</ul></p>';
 9300:     }
 9301:     return $resulttext;
 9302: }
 9303: 
 9304: sub process_textbook_image {
 9305:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
 9306:     my $filename = $env{'form.'.$caller.'.filename'};
 9307:     my ($error,$url);
 9308:     my ($width,$height) = (50,50);
 9309:     if ($configuserok eq 'ok') {
 9310:         if ($switchserver) {
 9311:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 9312:                          $switchserver);
 9313:         } elsif ($author_ok eq 'ok') {
 9314:             my ($result,$imageurl) =
 9315:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 9316:                              "$type/$dom/$cnum/cover",$width,$height);
 9317:             if ($result eq 'ok') {
 9318:                 $url = $imageurl;
 9319:             } else {
 9320:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 9321:             }
 9322:         } else {
 9323:             $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);
 9324:         }
 9325:     } else {
 9326:         $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);
 9327:     }
 9328:     return ($url,$error);
 9329: }
 9330: 
 9331: sub modify_ltitools {
 9332:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 9333:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9334:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
 9335:     my $confname = $dom.'-domainconfig';
 9336:     my $servadm = $r->dir_config('lonAdmEMail');
 9337:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 9338:     my (%posslti,%possfield);
 9339:     my @courseroles = ('cc','in','ta','ep','st');
 9340:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 9341:     map { $posslti{$_} = 1; } @ltiroles;
 9342:     my @allfields = ('fullname','firstname','lastname','email','user','roles');
 9343:     map { $possfield{$_} = 1; } @allfields;
 9344:     my %lt = &ltitools_names();
 9345:     if ($env{'form.ltitools_add'}) {
 9346:         my $title = $env{'form.ltitools_add_title'};
 9347:         $title =~ s/(`)/'/g;
 9348:         ($newid,my $error) = &get_ltitools_id($dom,$title);
 9349:         if ($newid) {
 9350:             my $position = $env{'form.ltitools_add_pos'};
 9351:             $position =~ s/\D+//g;
 9352:             if ($position ne '') {
 9353:                 $allpos[$position] = $newid;
 9354:             }
 9355:             $changes{$newid} = 1;
 9356:             foreach my $item ('title','url','key','secret') {
 9357:                 $env{'form.ltitools_add_'.$item} =~ s/(`)/'/g;
 9358:                 if ($env{'form.ltitools_add_'.$item}) {
 9359:                     if (($item eq 'key') || ($item eq 'secret')) {
 9360:                         $encconfig{$newid}{$item} = $env{'form.ltitools_add_'.$item};
 9361:                     } else {
 9362:                         $confhash{$newid}{$item} = $env{'form.ltitools_add_'.$item};
 9363:                     }
 9364:                 }
 9365:             }
 9366:             if ($env{'form.ltitools_add_version'} eq 'LTI-1p0') {
 9367:                 $confhash{$newid}{'version'} = $env{'form.ltitools_add_version'};
 9368:             }
 9369:             if ($env{'form.ltitools_add_msgtype'} eq 'basic-lti-launch-request') {
 9370:                 $confhash{$newid}{'msgtype'} = $env{'form.ltitools_add_msgtype'};
 9371:             }
 9372:             foreach my $item ('width','height','linktext','explanation') {
 9373:                 $env{'form.ltitools_add_'.$item} =~ s/^\s+//;
 9374:                 $env{'form.ltitools_add_'.$item} =~ s/\s+$//;
 9375:                 if (($item eq 'width') || ($item eq 'height')) {
 9376:                     if ($env{'form.ltitools_add_'.$item} =~ /^\d+$/) {
 9377:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
 9378:                     }
 9379:                 } else {
 9380:                     if ($env{'form.ltitools_add_'.$item} ne '') {
 9381:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
 9382:                     }
 9383:                 }
 9384:             }
 9385:             if ($env{'form.ltitools_add_target'} eq 'window') {
 9386:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
 9387:             } elsif ($env{'form.ltitools_add_target'} eq 'tab') {
 9388:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
 9389:             } else {
 9390:                 $confhash{$newid}{'display'}{'target'} = 'iframe';
 9391:             }
 9392:             foreach my $item ('passback','roster') {
 9393:                 if ($env{'form.ltitools_add_'.$item}) {
 9394:                     $confhash{$newid}{$item} = 1;
 9395:                 }
 9396:             }
 9397:             if ($env{'form.ltitools_add_image.filename'} ne '') {
 9398:                 my ($imageurl,$error) =
 9399:                     &process_ltitools_image($r,$dom,$confname,'ltitools_add_image',$newid,
 9400:                                             $configuserok,$switchserver,$author_ok);
 9401:                 if ($imageurl) {
 9402:                     $confhash{$newid}{'image'} = $imageurl;
 9403:                 }
 9404:                 if ($error) {
 9405:                     &Apache::lonnet::logthis($error);
 9406:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9407:                 }
 9408:             }
 9409:             my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_add_fields');
 9410:             foreach my $field (@fields) {
 9411:                 if ($possfield{$field}) {
 9412:                     if ($field eq 'roles') {
 9413:                         foreach my $role (@courseroles) {
 9414:                             my $choice = $env{'form.ltitools_add_roles_'.$role};
 9415:                             if (($choice ne '') && ($posslti{$choice})) {
 9416:                                 $confhash{$newid}{'roles'}{$role} = $choice;
 9417:                                 if ($role eq 'cc') {
 9418:                                     $confhash{$newid}{'roles'}{'co'} = $choice;
 9419:                                 }
 9420:                             }
 9421:                         }
 9422:                     } else {
 9423:                         $confhash{$newid}{'fields'}{$field} = 1;
 9424:                     }
 9425:                 }
 9426:             }
 9427:             my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig');
 9428:             foreach my $item (@courseconfig) {
 9429:                 $confhash{$newid}{'crsconf'}{$item} = 1;
 9430:             }
 9431:             if ($env{'form.ltitools_add_custom'}) {
 9432:                 my $name = $env{'form.ltitools_add_custom_name'};
 9433:                 my $value = $env{'form.ltitools_add_custom_value'};
 9434:                 $value =~ s/(`)/'/g;
 9435:                 $name =~ s/(`)/'/g;
 9436:                 $confhash{$newid}{'custom'}{$name} = $value;
 9437:             }
 9438:         } else {
 9439:             my $error = &mt('Failed to acquire unique ID for new external tool');
 9440:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9441:         }
 9442:     }
 9443:     if (ref($domconfig{$action}) eq 'HASH') {
 9444:         my %deletions;
 9445:         my @todelete = &Apache::loncommon::get_env_multiple('form.ltitools_del');
 9446:         if (@todelete) {
 9447:             map { $deletions{$_} = 1; } @todelete;
 9448:         }
 9449:         my %customadds;
 9450:         my @newcustom = &Apache::loncommon::get_env_multiple('form.ltitools_customadd');
 9451:         if (@newcustom) {
 9452:             map { $customadds{$_} = 1; } @newcustom;
 9453:         }
 9454:         my %imgdeletions;
 9455:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.ltitools_image_del');
 9456:         if (@todeleteimages) {
 9457:             map { $imgdeletions{$_} = 1; } @todeleteimages;
 9458:         }
 9459:         my $maxnum = $env{'form.ltitools_maxnum'};
 9460:         for (my $i=0; $i<=$maxnum; $i++) {
 9461:             my $itemid = $env{'form.ltitools_id_'.$i};
 9462:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
 9463:                 if ($deletions{$itemid}) {
 9464:                     if ($domconfig{$action}{$itemid}{'image'}) {
 9465:                         #FIXME need to obsolete item in RES space
 9466:                     }
 9467:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'title'};
 9468:                     next;
 9469:                 } else {
 9470:                     my $newpos = $env{'form.ltitools_'.$itemid};
 9471:                     $newpos =~ s/\D+//g;
 9472:                     foreach my $item ('title','url') {
 9473:                         $confhash{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
 9474:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
 9475:                             $changes{$itemid} = 1;
 9476:                         }
 9477:                     }
 9478:                     foreach my $item ('key','secret') {
 9479:                         $encconfig{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
 9480:                         if ($domconfig{$action}{$itemid}{$item} ne $encconfig{$itemid}{$item}) {
 9481:                             $changes{$itemid} = 1;
 9482:                         }
 9483:                     }
 9484:                     if ($env{'form.ltitools_version_'.$i} eq 'LTI-1p0') {
 9485:                         $confhash{$itemid}{'version'} = $env{'form.ltitools_version_'.$i};
 9486:                     }
 9487:                     if ($env{'form.ltitools_msgtype_'.$i} eq 'basic-lti-launch-request') {
 9488:                         $confhash{$itemid}{'msgtype'} = $env{'form.ltitools_msgtype_'.$i};
 9489:                     }
 9490:                     foreach my $size ('width','height') {
 9491:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/^\s+//;
 9492:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/\s+$//;
 9493:                         if ($env{'form.ltitools_'.$size.'_'.$i} =~ /^\d+$/) {
 9494:                             $confhash{$itemid}{'display'}{$size} = $env{'form.ltitools_'.$size.'_'.$i};
 9495:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9496:                                 if ($domconfig{$action}{$itemid}{'display'}{$size} ne $confhash{$itemid}{'display'}{$size}) {
 9497:                                     $changes{$itemid} = 1;
 9498:                                 }
 9499:                             } else {
 9500:                                 $changes{$itemid} = 1;
 9501:                             }
 9502:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9503:                             if ($domconfig{$action}{$itemid}{'display'}{$size} ne '') {
 9504:                                 $changes{$itemid} = 1;
 9505:                             }
 9506:                         }
 9507:                     }
 9508:                     foreach my $item ('linktext','explanation') {
 9509:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/^\s+//;
 9510:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/\s+$//;
 9511:                         if ($env{'form.ltitools_'.$item.'_'.$i} ne '') {
 9512:                             $confhash{$itemid}{'display'}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
 9513:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9514:                                 if ($domconfig{$action}{$itemid}{'display'}{$item} ne $confhash{$itemid}{'display'}{$item}) {
 9515:                                     $changes{$itemid} = 1;
 9516:                                 }
 9517:                             } else {
 9518:                                 $changes{$itemid} = 1;
 9519:                             }
 9520:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9521:                             if ($domconfig{$action}{$itemid}{'display'}{$item} ne '') {
 9522:                                 $changes{$itemid} = 1;
 9523:                             }
 9524:                         }
 9525:                     }
 9526:                     if ($env{'form.ltitools_target_'.$i} eq 'window') {
 9527:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
 9528:                     } elsif ($env{'form.ltitools_target_'.$i} eq 'tab') {
 9529:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
 9530:                     } else {
 9531:                         $confhash{$itemid}{'display'}{'target'} = 'iframe';
 9532:                     }
 9533:                     if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9534:                         if ($domconfig{$action}{$itemid}{'display'}{'target'} ne $confhash{$itemid}{'display'}{'target'}) {
 9535:                             $changes{$itemid} = 1;
 9536:                         }
 9537:                     } else {
 9538:                         $changes{$itemid} = 1;
 9539:                     }
 9540:                     foreach my $extra ('passback','roster') {
 9541:                         if ($env{'form.ltitools_'.$extra.'_'.$i}) {
 9542:                             $confhash{$itemid}{$extra} = 1;
 9543:                         }
 9544:                         if ($domconfig{$action}{$itemid}{$extra} ne $confhash{$itemid}{$extra}) {
 9545:                             $changes{$itemid} = 1;
 9546:                         }
 9547:                     }
 9548:                     my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig_'.$i);
 9549:                     foreach my $item ('label','title','target','linktext','explanation') {
 9550:                         if (grep(/^\Q$item\E$/,@courseconfig)) {
 9551:                             $confhash{$itemid}{'crsconf'}{$item} = 1;
 9552:                             if (ref($domconfig{$action}{$itemid}{'crsconf'}) eq 'HASH') {
 9553:                                 if ($domconfig{$action}{$itemid}{'crsconf'}{$item} ne $confhash{$itemid}{'crsconf'}{$item}) {
 9554:                                     $changes{$itemid} = 1;
 9555:                                 }
 9556:                             } else {
 9557:                                 $changes{$itemid} = 1;
 9558:                             }
 9559:                         }
 9560:                     }
 9561:                     my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_fields_'.$i);
 9562:                     foreach my $field (@fields) {
 9563:                         if ($possfield{$field}) {
 9564:                             if ($field eq 'roles') {
 9565:                                 foreach my $role (@courseroles) {
 9566:                                     my $choice = $env{'form.ltitools_roles_'.$role.'_'.$i};
 9567:                                     if (($choice ne '') && ($posslti{$choice})) {
 9568:                                         $confhash{$itemid}{'roles'}{$role} = $choice;
 9569:                                         if ($role eq 'cc') {
 9570:                                             $confhash{$itemid}{'roles'}{'co'} = $choice;
 9571:                                         }
 9572:                                     }
 9573:                                     if (ref($domconfig{$action}{$itemid}{'roles'}) eq 'HASH') {
 9574:                                         if ($domconfig{$action}{$itemid}{'roles'}{$role} ne $confhash{$itemid}{'roles'}{$role}) {
 9575:                                             $changes{$itemid} = 1;
 9576:                                         }
 9577:                                     } elsif ($confhash{$itemid}{'roles'}{$role}) {
 9578:                                         $changes{$itemid} = 1;
 9579:                                     }
 9580:                                 }
 9581:                             } else {
 9582:                                 $confhash{$itemid}{'fields'}{$field} = 1;
 9583:                                 if (ref($domconfig{$action}{$itemid}{'fields'}) eq 'HASH') {
 9584:                                     if ($domconfig{$action}{$itemid}{'fields'}{$field} ne $confhash{$itemid}{'fields'}{$field}) {
 9585:                                         $changes{$itemid} = 1;
 9586:                                     }
 9587:                                 } else {
 9588:                                     $changes{$itemid} = 1;
 9589:                                 }
 9590:                             }
 9591:                         }
 9592:                     }
 9593:                     $allpos[$newpos] = $itemid;
 9594:                 }
 9595:                 if ($imgdeletions{$itemid}) {
 9596:                     $changes{$itemid} = 1;
 9597:                     #FIXME need to obsolete item in RES space
 9598:                 } elsif ($env{'form.ltitools_image_'.$i.'.filename'}) {
 9599:                     my ($imgurl,$error) = &process_ltitools_image($r,$dom,$confname,'ltitools_image_'.$i,
 9600:                                                                  $itemid,$configuserok,$switchserver,
 9601:                                                                  $author_ok);
 9602:                     if ($imgurl) {
 9603:                         $confhash{$itemid}{'image'} = $imgurl;
 9604:                         $changes{$itemid} = 1;
 9605:                     }
 9606:                     if ($error) {
 9607:                         &Apache::lonnet::logthis($error);
 9608:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9609:                     }
 9610:                 } elsif ($domconfig{$action}{$itemid}{'image'}) {
 9611:                     $confhash{$itemid}{'image'} =
 9612:                        $domconfig{$action}{$itemid}{'image'};
 9613:                 }
 9614:                 if ($customadds{$i}) {
 9615:                     my $name = $env{'form.ltitools_custom_name_'.$i};
 9616:                     $name =~ s/(`)/'/g;
 9617:                     $name =~ s/^\s+//;
 9618:                     $name =~ s/\s+$//;
 9619:                     my $value = $env{'form.ltitools_custom_value_'.$i};
 9620:                     $value =~ s/(`)/'/g;
 9621:                     $value =~ s/^\s+//;
 9622:                     $value =~ s/\s+$//;
 9623:                     if ($name ne '') {
 9624:                         $confhash{$itemid}{'custom'}{$name} = $value;
 9625:                         $changes{$itemid} = 1;
 9626:                     }
 9627:                 }
 9628:                 my %customdels;
 9629:                 my @customdeletions = &Apache::loncommon::get_env_multiple('form.ltitools_customdel_'.$i);
 9630:                 if (@customdeletions) {
 9631:                     $changes{$itemid} = 1;
 9632:                 }
 9633:                 map { $customdels{$_} = 1; } @customdeletions;
 9634:                 if (ref($domconfig{$action}{$itemid}{'custom'}) eq 'HASH') {
 9635:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}{'custom'}})) {
 9636:                         unless ($customdels{$key}) {
 9637:                             if ($env{'form.ltitools_customval_'.$key.'_'.$i} ne '') {
 9638:                                 $confhash{$itemid}{'custom'}{$key} = $env{'form.ltitools_customval_'.$key.'_'.$i};
 9639:                             }
 9640:                             if ($domconfig{$action}{$itemid}{'custom'}{$key} ne $env{'form.ltitools_customval_'.$key.'_'.$i}) {
 9641:                                 $changes{$itemid} = 1;
 9642:                             }
 9643:                         }
 9644:                     }
 9645:                 }
 9646:                 unless ($changes{$itemid}) {
 9647:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}})) {
 9648:                         if (ref($domconfig{$action}{$itemid}{$key}) eq 'HASH') {
 9649:                             if (ref($confhash{$itemid}{$key}) eq 'HASH') {
 9650:                                 foreach my $innerkey (keys(%{$domconfig{$action}{$itemid}{$key}})) {
 9651:                                     unless (exists($confhash{$itemid}{$key}{$innerkey})) {
 9652:                                         $changes{$itemid} = 1;
 9653:                                         last;
 9654:                                     }
 9655:                                 }
 9656:                             } elsif (keys(%{$domconfig{$action}{$itemid}{$key}}) > 0) {
 9657:                                 $changes{$itemid} = 1;
 9658:                             }
 9659:                         }
 9660:                         last if ($changes{$itemid});
 9661:                     }
 9662:                 }
 9663:             }
 9664:         }
 9665:     }
 9666:     if (@allpos > 0) {
 9667:         my $idx = 0;
 9668:         foreach my $itemid (@allpos) {
 9669:             if ($itemid ne '') {
 9670:                 $confhash{$itemid}{'order'} = $idx;
 9671:                 if (ref($domconfig{$action}) eq 'HASH') {
 9672:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
 9673:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
 9674:                             $changes{$itemid} = 1;
 9675:                         }
 9676:                     }
 9677:                 }
 9678:                 $idx ++;
 9679:             }
 9680:         }
 9681:     }
 9682:     my %ltitoolshash = (
 9683:                           $action => { %confhash }
 9684:                        );
 9685:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,
 9686:                                              $dom);
 9687:     if ($putresult eq 'ok') {
 9688:         my %ltienchash = (
 9689:                              $action => { %encconfig }
 9690:                          );
 9691:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom);
 9692:         if (keys(%changes) > 0) {
 9693:             my $cachetime = 24*60*60;
 9694:             my %ltiall = %confhash;
 9695:             foreach my $id (keys(%ltiall)) {
 9696:                 if (ref($encconfig{$id}) eq 'HASH') {
 9697:                     foreach my $item ('key','secret') {
 9698:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
 9699:                     }
 9700:                 }
 9701:             }
 9702:             &Apache::lonnet::do_cache_new('ltitools',$dom,\%ltiall,$cachetime);
 9703:             if (ref($lastactref) eq 'HASH') {
 9704:                 $lastactref->{'ltitools'} = 1;
 9705:             }
 9706:             $resulttext = &mt('Changes made:').'<ul>';
 9707:             my %bynum;
 9708:             foreach my $itemid (sort(keys(%changes))) {
 9709:                 my $position = $confhash{$itemid}{'order'};
 9710:                 $bynum{$position} = $itemid;
 9711:             }
 9712:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
 9713:                 my $itemid = $bynum{$pos};
 9714:                 if (ref($confhash{$itemid}) ne 'HASH') {
 9715:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
 9716:                 } else {
 9717:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'title'}.'</b>';
 9718:                     if ($confhash{$itemid}{'image'}) {
 9719:                         $resulttext .= '&nbsp;'.
 9720:                                        '<img src="'.$confhash{$itemid}{'image'}.'"'.
 9721:                                        ' alt="'.&mt('Tool Provider icon').'" />';
 9722:                     }
 9723:                     $resulttext .= '</li><ul>';
 9724:                     my $position = $pos + 1;
 9725:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
 9726:                     foreach my $item ('version','msgtype','url') {
 9727:                         if ($confhash{$itemid}{$item} ne '') {
 9728:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
 9729:                         }
 9730:                     }
 9731:                     if ($encconfig{$itemid}{'key'} ne '') {
 9732:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
 9733:                     }
 9734:                     if ($encconfig{$itemid}{'secret'} ne '') {
 9735:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
 9736:                         my $num = length($encconfig{$itemid}{'secret'});
 9737:                         $resulttext .= ('*'x$num).'</li>';
 9738:                     }
 9739:                     $resulttext .= '<li>'.&mt('Configurable in course:');
 9740:                     my @possconfig = ('label','title','target','linktext','explanation');
 9741:                     my $numconfig = 0;
 9742:                     if (ref($confhash{$itemid}{'crsconf'}) eq 'HASH') {
 9743:                         foreach my $item (@possconfig) {
 9744:                             if ($confhash{$itemid}{'crsconf'}{$item}) {
 9745:                                 $numconfig ++;
 9746:                                 $resulttext .= ' "'.$lt{'crs'.$item}.'"';
 9747:                             }
 9748:                         }
 9749:                     }
 9750:                     if (!$numconfig) {
 9751:                         $resulttext .= &mt('None');
 9752:                     }
 9753:                     $resulttext .= '</li>';
 9754:                     foreach my $item ('passback','roster') {
 9755:                         $resulttext .= '<li>'.$lt{$item}.'&nbsp;';
 9756:                         if ($confhash{$itemid}{$item}) {
 9757:                             $resulttext .= &mt('Yes');
 9758:                         } else {
 9759:                             $resulttext .= &mt('No');
 9760:                         }
 9761:                         $resulttext .= '</li>';
 9762:                     }
 9763:                     if (ref($confhash{$itemid}{'display'}) eq 'HASH') {
 9764:                         my $displaylist;
 9765:                         if ($confhash{$itemid}{'display'}{'target'}) {
 9766:                             $displaylist = &mt('Display target').':&nbsp;'.
 9767:                                            $confhash{$itemid}{'display'}{'target'}.',';
 9768:                         }
 9769:                         foreach my $size ('width','height') {
 9770:                             if ($confhash{$itemid}{'display'}{$size}) {
 9771:                                 $displaylist .= ('&nbsp;'x2).$lt{$size}.':&nbsp;'.
 9772:                                                 $confhash{$itemid}{'display'}{$size}.',';
 9773:                             }
 9774:                         }
 9775:                         if ($displaylist) {
 9776:                             $displaylist =~ s/,$//;
 9777:                             $resulttext .= '<li>'.$displaylist.'</li>';
 9778:                         }
 9779:                         foreach my $item ('linktext','explanation') {
 9780:                             if ($confhash{$itemid}{'display'}{$item}) {
 9781:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{'display'}{$item}.'</li>';
 9782:                             }
 9783:                         }
 9784:                     }
 9785:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
 9786:                         my $fieldlist;
 9787:                         foreach my $field (@allfields) {
 9788:                             if ($confhash{$itemid}{'fields'}{$field}) {
 9789:                                 $fieldlist .= ('&nbsp;'x2).$lt{$field}.',';
 9790:                             }
 9791:                         }
 9792:                         if ($fieldlist) {
 9793:                             $fieldlist =~ s/,$//;
 9794:                             $resulttext .= '<li>'.&mt('Data sent').':'.$fieldlist.'</li>';
 9795:                         }
 9796:                     }
 9797:                     if (ref($confhash{$itemid}{'roles'}) eq 'HASH') {
 9798:                         my $rolemaps;
 9799:                         foreach my $role (@courseroles) {
 9800:                             if ($confhash{$itemid}{'roles'}{$role}) {
 9801:                                 $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
 9802:                                              $confhash{$itemid}{'roles'}{$role}.',';
 9803:                             }
 9804:                         }
 9805:                         if ($rolemaps) {
 9806:                             $rolemaps =~ s/,$//;
 9807:                             $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
 9808:                         }
 9809:                     }
 9810:                     if (ref($confhash{$itemid}{'custom'}) eq 'HASH') {
 9811:                         my $customlist;
 9812:                         if (keys(%{$confhash{$itemid}{'custom'}})) {
 9813:                             foreach my $key (sort(keys(%{$confhash{$itemid}{'custom'}}))) {
 9814:                                 $customlist .= $key.':'.$confhash{$itemid}{'custom'}{$key}.('&nbsp;'x2);
 9815:                             }
 9816:                         }
 9817:                         if ($customlist) {
 9818:                             $resulttext .= '<li>'.&mt('Custom items').':'.$customlist.'</li>';
 9819:                         }
 9820:                     }
 9821:                     $resulttext .= '</ul></li>';
 9822:                 }
 9823:             }
 9824:             $resulttext .= '</ul>';
 9825:         } else {
 9826:             $resulttext = &mt('No changes made.');
 9827:         }
 9828:     } else {
 9829:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
 9830:     }
 9831:     if ($errors) {
 9832:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 9833:                        $errors.'</ul>';
 9834:     }
 9835:     return $resulttext;
 9836: }
 9837: 
 9838: sub process_ltitools_image {
 9839:     my ($r,$dom,$confname,$caller,$itemid,$configuserok,$switchserver,$author_ok) = @_;
 9840:     my $filename = $env{'form.'.$caller.'.filename'};
 9841:     my ($error,$url);
 9842:     my ($width,$height) = (21,21);
 9843:     if ($configuserok eq 'ok') {
 9844:         if ($switchserver) {
 9845:             $error = &mt('Upload of Tool Provider (LTI) icon is not permitted to this server: [_1]',
 9846:                          $switchserver);
 9847:         } elsif ($author_ok eq 'ok') {
 9848:             my ($result,$imageurl,$madethumb) =
 9849:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 9850:                              "ltitools/$itemid/icon",$width,$height);
 9851:             if ($result eq 'ok') {
 9852:                 if ($madethumb) {
 9853:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
 9854:                     my $imagethumb = "$path/tn-".$imagefile;
 9855:                     $url = $imagethumb;
 9856:                 } else {
 9857:                     $url = $imageurl;
 9858:                 }
 9859:             } else {
 9860:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 9861:             }
 9862:         } else {
 9863:             $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);
 9864:         }
 9865:     } else {
 9866:         $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);
 9867:     }
 9868:     return ($url,$error);
 9869: }
 9870: 
 9871: sub get_ltitools_id {
 9872:     my ($cdom,$title) = @_;
 9873:     # get lock on ltitools db
 9874:     my $lockhash = {
 9875:                       lock => $env{'user.name'}.
 9876:                               ':'.$env{'user.domain'},
 9877:                    };
 9878:     my $tries = 0;
 9879:     my $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
 9880:     my ($id,$error);
 9881: 
 9882:     while (($gotlock ne 'ok') && ($tries<10)) {
 9883:         $tries ++;
 9884:         sleep (0.1);
 9885:         $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
 9886:     }
 9887:     if ($gotlock eq 'ok') {
 9888:         my %currids = &Apache::lonnet::dump_dom('ltitools',$cdom);
 9889:         if ($currids{'lock'}) {
 9890:             delete($currids{'lock'});
 9891:             if (keys(%currids)) {
 9892:                 my @curr = sort { $a <=> $b } keys(%currids);
 9893:                 if ($curr[-1] =~ /^\d+$/) {
 9894:                     $id = 1 + $curr[-1];
 9895:                 }
 9896:             } else {
 9897:                 $id = 1;
 9898:             }
 9899:             if ($id) {
 9900:                 unless (&Apache::lonnet::newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
 9901:                     $error = 'nostore';
 9902:                 }
 9903:             } else {
 9904:                 $error = 'nonumber';
 9905:             }
 9906:         }
 9907:         my $dellockoutcome = &Apache::lonnet::del_dom('ltitools',['lock'],$cdom);
 9908:     } else {
 9909:         $error = 'nolock';
 9910:     }
 9911:     return ($id,$error);
 9912: }
 9913: 
 9914: sub modify_autoenroll {
 9915:     my ($dom,$lastactref,%domconfig) = @_;
 9916:     my ($resulttext,%changes);
 9917:     my %currautoenroll;
 9918:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 9919:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 9920:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 9921:         }
 9922:     }
 9923:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 9924:     my %title = ( run => 'Auto-enrollment active',
 9925:                   sender => 'Sender for notification messages',
 9926:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
 9927:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
 9928:     my @offon = ('off','on');
 9929:     my $sender_uname = $env{'form.sender_uname'};
 9930:     my $sender_domain = $env{'form.sender_domain'};
 9931:     if ($sender_domain eq '') {
 9932:         $sender_uname = '';
 9933:     } elsif ($sender_uname eq '') {
 9934:         $sender_domain = '';
 9935:     }
 9936:     my $coowners = $env{'form.autoassign_coowners'};
 9937:     my $failsafe = $env{'form.autoenroll_failsafe'};
 9938:     $failsafe =~ s{^\s+|\s+$}{}g;
 9939:     if ($failsafe =~ /\D/) {
 9940:         undef($failsafe);
 9941:     }
 9942:     my %autoenrollhash =  (
 9943:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 9944:                                        'sender_uname' => $sender_uname,
 9945:                                        'sender_domain' => $sender_domain,
 9946:                                        'co-owners' => $coowners,
 9947:                                        'autofailsafe' => $failsafe,
 9948:                                 }
 9949:                      );
 9950:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 9951:                                              $dom);
 9952:     if ($putresult eq 'ok') {
 9953:         if (exists($currautoenroll{'run'})) {
 9954:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 9955:                  $changes{'run'} = 1;
 9956:              }
 9957:         } elsif ($autorun) {
 9958:             if ($env{'form.autoenroll_run'} ne '1') {
 9959:                  $changes{'run'} = 1;
 9960:             }
 9961:         }
 9962:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 9963:             $changes{'sender'} = 1;
 9964:         }
 9965:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 9966:             $changes{'sender'} = 1;
 9967:         }
 9968:         if ($currautoenroll{'co-owners'} ne '') {
 9969:             if ($currautoenroll{'co-owners'} ne $coowners) {
 9970:                 $changes{'coowners'} = 1;
 9971:             }
 9972:         } elsif ($coowners) {
 9973:             $changes{'coowners'} = 1;
 9974:         }
 9975:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
 9976:             $changes{'autofailsafe'} = 1;
 9977:         }
 9978:         if (keys(%changes) > 0) {
 9979:             $resulttext = &mt('Changes made:').'<ul>';
 9980:             if ($changes{'run'}) {
 9981:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 9982:             }
 9983:             if ($changes{'sender'}) {
 9984:                 if ($sender_uname eq '' || $sender_domain eq '') {
 9985:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 9986:                 } else {
 9987:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 9988:                 }
 9989:             }
 9990:             if ($changes{'coowners'}) {
 9991:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 9992:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 9993:                 if (ref($lastactref) eq 'HASH') {
 9994:                     $lastactref->{'domainconfig'} = 1;
 9995:                 }
 9996:             }
 9997:             if ($changes{'autofailsafe'}) {
 9998:                 if ($failsafe ne '') {
 9999:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$failsafe).'</li>';
10000:                 } else {
10001:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section: deleted');
10002:                 }
10003:                 &Apache::lonnet::get_domain_defaults($dom,1);
10004:                 if (ref($lastactref) eq 'HASH') {
10005:                     $lastactref->{'domdefaults'} = 1;
10006:                 }
10007:             }
10008:             $resulttext .= '</ul>';
10009:         } else {
10010:             $resulttext = &mt('No changes made to auto-enrollment settings');
10011:         }
10012:     } else {
10013:         $resulttext = '<span class="LC_error">'.
10014: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
10015:     }
10016:     return $resulttext;
10017: }
10018: 
10019: sub modify_autoupdate {
10020:     my ($dom,%domconfig) = @_;
10021:     my ($resulttext,%currautoupdate,%fields,%changes);
10022:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
10023:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
10024:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
10025:         }
10026:     }
10027:     my @offon = ('off','on');
10028:     my %title = &Apache::lonlocal::texthash (
10029:                    run => 'Auto-update:',
10030:                    classlists => 'Updates to user information in classlists?'
10031:                 );
10032:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10033:     my %fieldtitles = &Apache::lonlocal::texthash (
10034:                         id => 'Student/Employee ID',
10035:                         permanentemail => 'E-mail address',
10036:                         lastname => 'Last Name',
10037:                         firstname => 'First Name',
10038:                         middlename => 'Middle Name',
10039:                         generation => 'Generation',
10040:                       );
10041:     $othertitle = &mt('All users');
10042:     if (keys(%{$usertypes}) >  0) {
10043:         $othertitle = &mt('Other users');
10044:     }
10045:     foreach my $key (keys(%env)) {
10046:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
10047:             my ($usertype,$item) = ($1,$2);
10048:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
10049:                 if ($usertype eq 'default') {   
10050:                     push(@{$fields{$1}},$2);
10051:                 } elsif (ref($types) eq 'ARRAY') {
10052:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
10053:                         push(@{$fields{$1}},$2);
10054:                     }
10055:                 }
10056:             }
10057:         }
10058:     }
10059:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
10060:     @lockablenames = sort(@lockablenames);
10061:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
10062:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
10063:         if (@changed) {
10064:             $changes{'lockablenames'} = 1;
10065:         }
10066:     } else {
10067:         if (@lockablenames) {
10068:             $changes{'lockablenames'} = 1;
10069:         }
10070:     }
10071:     my %updatehash = (
10072:                       autoupdate => { run => $env{'form.autoupdate_run'},
10073:                                       classlists => $env{'form.classlists'},
10074:                                       fields => {%fields},
10075:                                       lockablenames => \@lockablenames,
10076:                                     }
10077:                      );
10078:     foreach my $key (keys(%currautoupdate)) {
10079:         if (($key eq 'run') || ($key eq 'classlists')) {
10080:             if (exists($updatehash{autoupdate}{$key})) {
10081:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
10082:                     $changes{$key} = 1;
10083:                 }
10084:             }
10085:         } elsif ($key eq 'fields') {
10086:             if (ref($currautoupdate{$key}) eq 'HASH') {
10087:                 foreach my $item (@{$types},'default') {
10088:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
10089:                         my $change = 0;
10090:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
10091:                             if (!exists($fields{$item})) {
10092:                                 $change = 1;
10093:                                 last;
10094:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
10095:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
10096:                                     $change = 1;
10097:                                     last;
10098:                                 }
10099:                             }
10100:                         }
10101:                         if ($change) {
10102:                             push(@{$changes{$key}},$item);
10103:                         }
10104:                     } 
10105:                 }
10106:             }
10107:         } elsif ($key eq 'lockablenames') {
10108:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
10109:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
10110:                 if (@changed) {
10111:                     $changes{'lockablenames'} = 1;
10112:                 }
10113:             } else {
10114:                 if (@lockablenames) {
10115:                     $changes{'lockablenames'} = 1;
10116:                 }
10117:             }
10118:         }
10119:     }
10120:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
10121:         if (@lockablenames) {
10122:             $changes{'lockablenames'} = 1;
10123:         }
10124:     }
10125:     foreach my $item (@{$types},'default') {
10126:         if (defined($fields{$item})) {
10127:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
10128:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
10129:                     my $change = 0;
10130:                     if (ref($fields{$item}) eq 'ARRAY') {
10131:                         foreach my $type (@{$fields{$item}}) {
10132:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
10133:                                 $change = 1;
10134:                                 last;
10135:                             }
10136:                         }
10137:                     }
10138:                     if ($change) {
10139:                         push(@{$changes{'fields'}},$item);
10140:                     }
10141:                 } else {
10142:                     push(@{$changes{'fields'}},$item);
10143:                 }
10144:             } else {
10145:                 push(@{$changes{'fields'}},$item);
10146:             }
10147:         }
10148:     }
10149:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
10150:                                              $dom);
10151:     if ($putresult eq 'ok') {
10152:         if (keys(%changes) > 0) {
10153:             $resulttext = &mt('Changes made:').'<ul>';
10154:             foreach my $key (sort(keys(%changes))) {
10155:                 if ($key eq 'lockablenames') {
10156:                     $resulttext .= '<li>';
10157:                     if (@lockablenames) {
10158:                         $usertypes->{'default'} = $othertitle;
10159:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
10160:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
10161:                     } else {
10162:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
10163:                     }
10164:                     $resulttext .= '</li>';
10165:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
10166:                     foreach my $item (@{$changes{$key}}) {
10167:                         my @newvalues;
10168:                         foreach my $type (@{$fields{$item}}) {
10169:                             push(@newvalues,$fieldtitles{$type});
10170:                         }
10171:                         my $newvaluestr;
10172:                         if (@newvalues > 0) {
10173:                             $newvaluestr = join(', ',@newvalues);
10174:                         } else {
10175:                             $newvaluestr = &mt('none');
10176:                         }
10177:                         if ($item eq 'default') {
10178:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
10179:                         } else {
10180:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
10181:                         }
10182:                     }
10183:                 } else {
10184:                     my $newvalue;
10185:                     if ($key eq 'run') {
10186:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
10187:                     } else {
10188:                         $newvalue = $offon[$env{'form.'.$key}];
10189:                     }
10190:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
10191:                 }
10192:             }
10193:             $resulttext .= '</ul>';
10194:         } else {
10195:             $resulttext = &mt('No changes made to autoupdates');
10196:         }
10197:     } else {
10198:         $resulttext = '<span class="LC_error">'.
10199: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
10200:     }
10201:     return $resulttext;
10202: }
10203: 
10204: sub modify_autocreate {
10205:     my ($dom,%domconfig) = @_;
10206:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
10207:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
10208:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
10209:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
10210:         }
10211:     }
10212:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
10213:                  req => 'Auto-creation of validated requests for official courses',
10214:                  xmldc => 'Identity of course creator of courses from XML files',
10215:                );
10216:     my @types = ('xml','req');
10217:     foreach my $item (@types) {
10218:         $newvals{$item} = $env{'form.autocreate_'.$item};
10219:         $newvals{$item} =~ s/\D//g;
10220:         $newvals{$item} = 0 if ($newvals{$item} eq '');
10221:     }
10222:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
10223:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
10224:     unless (exists($domcoords{$newvals{'xmldc'}})) {
10225:         $newvals{'xmldc'} = '';
10226:     } 
10227:     %autocreatehash =  (
10228:                         autocreate => { xml => $newvals{'xml'},
10229:                                         req => $newvals{'req'},
10230:                                       }
10231:                        );
10232:     if ($newvals{'xmldc'} ne '') {
10233:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
10234:     }
10235:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
10236:                                              $dom);
10237:     if ($putresult eq 'ok') {
10238:         my @items = @types;
10239:         if ($newvals{'xml'}) {
10240:             push(@items,'xmldc');
10241:         }
10242:         foreach my $item (@items) {
10243:             if (exists($currautocreate{$item})) {
10244:                 if ($currautocreate{$item} ne $newvals{$item}) {
10245:                     $changes{$item} = 1;
10246:                 }
10247:             } elsif ($newvals{$item}) {
10248:                 $changes{$item} = 1;
10249:             }
10250:         }
10251:         if (keys(%changes) > 0) {
10252:             my @offon = ('off','on'); 
10253:             $resulttext = &mt('Changes made:').'<ul>';
10254:             foreach my $item (@types) {
10255:                 if ($changes{$item}) {
10256:                     my $newtxt = $offon[$newvals{$item}];
10257:                     $resulttext .= '<li>'.
10258:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
10259:                                        '<b>','</b>').
10260:                                    '</li>';
10261:                 }
10262:             }
10263:             if ($changes{'xmldc'}) {
10264:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
10265:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
10266:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
10267:             }
10268:             $resulttext .= '</ul>';
10269:         } else {
10270:             $resulttext = &mt('No changes made to auto-creation settings');
10271:         }
10272:     } else {
10273:         $resulttext = '<span class="LC_error">'.
10274:             &mt('An error occurred: [_1]',$putresult).'</span>';
10275:     }
10276:     return $resulttext;
10277: }
10278: 
10279: sub modify_directorysrch {
10280:     my ($dom,$lastactref,%domconfig) = @_;
10281:     my ($resulttext,%changes);
10282:     my %currdirsrch;
10283:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
10284:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
10285:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
10286:         }
10287:     }
10288:     my %title = ( available => 'Institutional directory search available',
10289:                   localonly => 'Other domains can search institution',
10290:                   lcavailable => 'LON-CAPA directory search available',
10291:                   lclocalonly => 'Other domains can search LON-CAPA domain',
10292:                   searchby => 'Search types',
10293:                   searchtypes => 'Search latitude');
10294:     my @offon = ('off','on');
10295:     my @otherdoms = ('Yes','No');
10296: 
10297:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
10298:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
10299:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
10300: 
10301:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10302:     if (keys(%{$usertypes}) == 0) {
10303:         @cansearch = ('default');
10304:     } else {
10305:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
10306:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
10307:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
10308:                     push(@{$changes{'cansearch'}},$type);
10309:                 }
10310:             }
10311:             foreach my $type (@cansearch) {
10312:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
10313:                     push(@{$changes{'cansearch'}},$type);
10314:                 }
10315:             }
10316:         } else {
10317:             push(@{$changes{'cansearch'}},@cansearch);
10318:         }
10319:     }
10320: 
10321:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
10322:         foreach my $by (@{$currdirsrch{'searchby'}}) {
10323:             if (!grep(/^\Q$by\E$/,@searchby)) {
10324:                 push(@{$changes{'searchby'}},$by);
10325:             }
10326:         }
10327:         foreach my $by (@searchby) {
10328:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
10329:                 push(@{$changes{'searchby'}},$by);
10330:             }
10331:         }
10332:     } else {
10333:         push(@{$changes{'searchby'}},@searchby);
10334:     }
10335: 
10336:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
10337:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
10338:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
10339:                 push(@{$changes{'searchtypes'}},$type);
10340:             }
10341:         }
10342:         foreach my $type (@searchtypes) {
10343:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
10344:                 push(@{$changes{'searchtypes'}},$type);
10345:             }
10346:         }
10347:     } else {
10348:         if (exists($currdirsrch{'searchtypes'})) {
10349:             foreach my $type (@searchtypes) {  
10350:                 if ($type ne $currdirsrch{'searchtypes'}) { 
10351:                     push(@{$changes{'searchtypes'}},$type);
10352:                 }
10353:             }
10354:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
10355:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
10356:             }   
10357:         } else {
10358:             push(@{$changes{'searchtypes'}},@searchtypes); 
10359:         }
10360:     }
10361: 
10362:     my %dirsrch_hash =  (
10363:             directorysrch => { available => $env{'form.dirsrch_available'},
10364:                                cansearch => \@cansearch,
10365:                                localonly => $env{'form.dirsrch_instlocalonly'},
10366:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
10367:                                lcavailable => $env{'form.dirsrch_domavailable'},
10368:                                searchby => \@searchby,
10369:                                searchtypes => \@searchtypes,
10370:                              }
10371:             );
10372:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
10373:                                              $dom);
10374:     if ($putresult eq 'ok') {
10375:         if (exists($currdirsrch{'available'})) {
10376:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
10377:                  $changes{'available'} = 1;
10378:              }
10379:         } else {
10380:             if ($env{'form.dirsrch_available'} eq '1') {
10381:                 $changes{'available'} = 1;
10382:             }
10383:         }
10384:         if (exists($currdirsrch{'lcavailable'})) {
10385:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
10386:                 $changes{'lcavailable'} = 1;
10387:             }
10388:         } else {
10389:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
10390:                 $changes{'lcavailable'} = 1;
10391:             }
10392:         }
10393:         if (exists($currdirsrch{'localonly'})) {
10394:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
10395:                 $changes{'localonly'} = 1;
10396:             }
10397:         } else {
10398:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
10399:                 $changes{'localonly'} = 1;
10400:             }
10401:         }
10402:         if (exists($currdirsrch{'lclocalonly'})) {
10403:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
10404:                 $changes{'lclocalonly'} = 1;
10405:             }
10406:         } else {
10407:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
10408:                 $changes{'lclocalonly'} = 1;
10409:             }
10410:         }
10411:         if (keys(%changes) > 0) {
10412:             $resulttext = &mt('Changes made:').'<ul>';
10413:             if ($changes{'available'}) {
10414:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
10415:             }
10416:             if ($changes{'lcavailable'}) {
10417:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
10418:             }
10419:             if ($changes{'localonly'}) {
10420:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
10421:             }
10422:             if ($changes{'lclocalonly'}) {
10423:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
10424:             }
10425:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
10426:                 my $chgtext;
10427:                 if (ref($usertypes) eq 'HASH') {
10428:                     if (keys(%{$usertypes}) > 0) {
10429:                         foreach my $type (@{$types}) {
10430:                             if (grep(/^\Q$type\E$/,@cansearch)) {
10431:                                 $chgtext .= $usertypes->{$type}.'; ';
10432:                             }
10433:                         }
10434:                         if (grep(/^default$/,@cansearch)) {
10435:                             $chgtext .= $othertitle;
10436:                         } else {
10437:                             $chgtext =~ s/\; $//;
10438:                         }
10439:                         $resulttext .=
10440:                             '<li>'.
10441:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
10442:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
10443:                             '</li>';
10444:                     }
10445:                 }
10446:             }
10447:             if (ref($changes{'searchby'}) eq 'ARRAY') {
10448:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
10449:                 my $chgtext;
10450:                 foreach my $type (@{$titleorder}) {
10451:                     if (grep(/^\Q$type\E$/,@searchby)) {
10452:                         if (defined($searchtitles->{$type})) {
10453:                             $chgtext .= $searchtitles->{$type}.'; ';
10454:                         }
10455:                     }
10456:                 }
10457:                 $chgtext =~ s/\; $//;
10458:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
10459:             }
10460:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
10461:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
10462:                 my $chgtext;
10463:                 foreach my $type (@{$srchtypeorder}) {
10464:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
10465:                         if (defined($srchtypes_desc->{$type})) {
10466:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
10467:                         }
10468:                     }
10469:                 }
10470:                 $chgtext =~ s/\; $//;
10471:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
10472:             }
10473:             $resulttext .= '</ul>';
10474:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
10475:             if (ref($lastactref) eq 'HASH') {
10476:                 $lastactref->{'directorysrch'} = 1;
10477:             }
10478:         } else {
10479:             $resulttext = &mt('No changes made to directory search settings');
10480:         }
10481:     } else {
10482:         $resulttext = '<span class="LC_error">'.
10483:                       &mt('An error occurred: [_1]',$putresult).'</span>';
10484:     }
10485:     return $resulttext;
10486: }
10487: 
10488: sub modify_contacts {
10489:     my ($dom,$lastactref,%domconfig) = @_;
10490:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
10491:     if (ref($domconfig{'contacts'}) eq 'HASH') {
10492:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
10493:             $currsetting{$key} = $domconfig{'contacts'}{$key};
10494:         }
10495:     }
10496:     my (%others,%to,%bcc,%includestr,%includeloc);
10497:     my @contacts = ('supportemail','adminemail');
10498:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
10499:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail');
10500:     my @toggles = ('reporterrors','reportupdates');
10501:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
10502:     foreach my $type (@mailings) {
10503:         @{$newsetting{$type}} = 
10504:             &Apache::loncommon::get_env_multiple('form.'.$type);
10505:         foreach my $item (@contacts) {
10506:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
10507:                 $contacts_hash{contacts}{$type}{$item} = 1;
10508:             } else {
10509:                 $contacts_hash{contacts}{$type}{$item} = 0;
10510:             }
10511:         }
10512:         $others{$type} = $env{'form.'.$type.'_others'};
10513:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
10514:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10515:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
10516:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
10517:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
10518:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
10519:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
10520:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
10521:             }
10522:         }
10523:     }
10524:     foreach my $item (@contacts) {
10525:         $to{$item} = $env{'form.'.$item};
10526:         $contacts_hash{'contacts'}{$item} = $to{$item};
10527:     }
10528:     foreach my $item (@toggles) {
10529:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
10530:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
10531:         }
10532:     }
10533:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
10534:         foreach my $field (@{$fields}) {
10535:             if (ref($possoptions->{$field}) eq 'ARRAY') {
10536:                 my $value = $env{'form.helpform_'.$field};
10537:                 $value =~ s/^\s+|\s+$//g;
10538:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
10539:                     $contacts_hash{'contacts'}{'helpform'}{$field} = $value;
10540:                     if ($field eq 'screenshot') {
10541:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
10542:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
10543:                             $contacts_hash{'contacts'}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
10544:                         }
10545:                     }
10546:                 }
10547:             }
10548:         }
10549:     }
10550:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10551:     my (@statuses,%usertypeshash,@overrides);
10552:     if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
10553:         @statuses = @{$types};
10554:         if (ref($usertypes) eq 'HASH') {
10555:             %usertypeshash = %{$usertypes};
10556:         }
10557:     }
10558:     if (@statuses) {
10559:         my @possoverrides = &Apache::loncommon::get_env_multiple('form.overrides');
10560:         foreach my $type (@possoverrides) {
10561:             if (($type ne '') && (grep(/^\Q$type\E$/,@statuses))) {
10562:                 push(@overrides,$type);
10563:             }
10564:         }
10565:         if (@overrides) {
10566:             foreach my $type (@overrides) {
10567:                 my @standard = &Apache::loncommon::get_env_multiple('form.override_'.$type);
10568:                 foreach my $item (@contacts) {
10569:                     if (grep(/^\Q$item\E$/,@standard)) {
10570:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 1;
10571:                         $newsetting{'override_'.$type}{$item} = 1;
10572:                     } else {
10573:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 0;
10574:                         $newsetting{'override_'.$type}{$item} = 0;
10575:                     }
10576:                 }
10577:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'others'} = $env{'form.override_'.$type.'_others'};
10578:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
10579:                 $newsetting{'override_'.$type}{'others'} = $env{'form.override_'.$type.'_others'};
10580:                 $newsetting{'override_'.$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
10581:                 if (($env{'form.override_'.$type.'_includestr'} ne '') && ($env{'form.override_'.$type.'_includeloc'} =~ /^s|b$/)) {
10582:                     $includestr{$type} = $env{'form.override_'.$type.'_includestr'};
10583:                     $includeloc{$type} = $env{'form.override_'.$type.'_includeloc'};
10584:                     $contacts_hash{'contacts'}{'overrides'}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
10585:                     $newsetting{'override_'.$type}{'include'} = $contacts_hash{'contacts'}{'overrides'}{$type}{'include'};
10586:                 }
10587:             }
10588:         }
10589:     }
10590:     if (keys(%currsetting) > 0) {
10591:         foreach my $item (@contacts) {
10592:             if ($to{$item} ne $currsetting{$item}) {
10593:                 $changes{$item} = 1;
10594:             }
10595:         }
10596:         foreach my $type (@mailings) {
10597:             foreach my $item (@contacts) {
10598:                 if (ref($currsetting{$type}) eq 'HASH') {
10599:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
10600:                         push(@{$changes{$type}},$item);
10601:                     }
10602:                 } else {
10603:                     push(@{$changes{$type}},@{$newsetting{$type}});
10604:                 }
10605:             }
10606:             if ($others{$type} ne $currsetting{$type}{'others'}) {
10607:                 push(@{$changes{$type}},'others');
10608:             }
10609:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10610:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
10611:                     push(@{$changes{$type}},'bcc'); 
10612:                 }
10613:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
10614:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
10615:                     push(@{$changes{$type}},'include');
10616:                 }
10617:             }
10618:         }
10619:         if (ref($fields) eq 'ARRAY') {
10620:             if (ref($currsetting{'helpform'}) eq 'HASH') {
10621:                 foreach my $field (@{$fields}) {
10622:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
10623:                         push(@{$changes{'helpform'}},$field);
10624:                     }
10625:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10626:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
10627:                             push(@{$changes{'helpform'}},'maxsize');
10628:                         }
10629:                     }
10630:                 }
10631:             } else {
10632:                 foreach my $field (@{$fields}) {
10633:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
10634:                         push(@{$changes{'helpform'}},$field);
10635:                     }
10636:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10637:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
10638:                             push(@{$changes{'helpform'}},'maxsize');
10639:                         }
10640:                     }
10641:                 }
10642:             }
10643:         }
10644:         if (@statuses) {
10645:             if (ref($currsetting{'overrides'}) eq 'HASH') {
10646:                 foreach my $key (keys(%{$currsetting{'overrides'}})) {
10647:                     if (ref($currsetting{'overrides'}{$key}) eq 'HASH') {
10648:                         if (ref($newsetting{'override_'.$key}) eq 'HASH') {
10649:                             foreach my $item (@contacts,'bcc','others','include') {
10650:                                 if ($currsetting{'overrides'}{$key}{$item} ne $newsetting{'override_'.$key}{$item}) {
10651:                                     push(@{$changes{'overrides'}},$key);
10652:                                     last;
10653:                                 }
10654:                             }
10655:                         } else {
10656:                             push(@{$changes{'overrides'}},$key);
10657:                         }
10658:                     }
10659:                 }
10660:                 foreach my $key (@overrides) {
10661:                     unless (exists($currsetting{'overrides'}{$key})) {
10662:                         push(@{$changes{'overrides'}},$key);
10663:                     }
10664:                 }
10665:             } else {
10666:                 foreach my $key (@overrides) {
10667:                     push(@{$changes{'overrides'}},$key);
10668:                 }
10669:             }
10670:         }
10671:     } else {
10672:         my %default;
10673:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
10674:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
10675:         $default{'errormail'} = 'adminemail';
10676:         $default{'packagesmail'} = 'adminemail';
10677:         $default{'helpdeskmail'} = 'supportemail';
10678:         $default{'otherdomsmail'} = 'supportemail';
10679:         $default{'lonstatusmail'} = 'adminemail';
10680:         $default{'requestsmail'} = 'adminemail';
10681:         $default{'updatesmail'} = 'adminemail';
10682:         foreach my $item (@contacts) {
10683:            if ($to{$item} ne $default{$item}) {
10684:                $changes{$item} = 1;
10685:            }
10686:         }
10687:         foreach my $type (@mailings) {
10688:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
10689:                 push(@{$changes{$type}},@{$newsetting{$type}});
10690:             }
10691:             if ($others{$type} ne '') {
10692:                 push(@{$changes{$type}},'others');
10693:             }
10694:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10695:                 if ($bcc{$type} ne '') {
10696:                     push(@{$changes{$type}},'bcc');
10697:                 }
10698:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
10699:                     push(@{$changes{$type}},'include');
10700:                 }
10701:             }
10702:         }
10703:         if (ref($fields) eq 'ARRAY') {
10704:             foreach my $field (@{$fields}) {
10705:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
10706:                     push(@{$changes{'helpform'}},$field);
10707:                 }
10708:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10709:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
10710:                         push(@{$changes{'helpform'}},'maxsize');
10711:                     }
10712:                 }
10713:             }
10714:         }
10715:     }
10716:     foreach my $item (@toggles) {
10717:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
10718:             $changes{$item} = 1;
10719:         } elsif ((!$env{'form.'.$item}) &&
10720:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
10721:             $changes{$item} = 1;
10722:         }
10723:     }
10724:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
10725:                                              $dom);
10726:     if ($putresult eq 'ok') {
10727:         if (keys(%changes) > 0) {
10728:             &Apache::loncommon::devalidate_domconfig_cache($dom);
10729:             if (ref($lastactref) eq 'HASH') {
10730:                 $lastactref->{'domainconfig'} = 1;
10731:             }
10732:             my ($titles,$short_titles)  = &contact_titles();
10733:             $resulttext = &mt('Changes made:').'<ul>';
10734:             foreach my $item (@contacts) {
10735:                 if ($changes{$item}) {
10736:                     $resulttext .= '<li>'.$titles->{$item}.
10737:                                     &mt(' set to: ').
10738:                                     '<span class="LC_cusr_emph">'.
10739:                                     $to{$item}.'</span></li>';
10740:                 }
10741:             }
10742:             foreach my $type (@mailings) {
10743:                 if (ref($changes{$type}) eq 'ARRAY') {
10744:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10745:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
10746:                     } else {
10747:                         $resulttext .= '<li>'.$titles->{$type}.': ';
10748:                     }
10749:                     my @text;
10750:                     foreach my $item (@{$newsetting{$type}}) {
10751:                         push(@text,$short_titles->{$item});
10752:                     }
10753:                     if ($others{$type} ne '') {
10754:                         push(@text,$others{$type});
10755:                     }
10756:                     if (@text) {
10757:                         $resulttext .= '<span class="LC_cusr_emph">'.
10758:                                        join(', ',@text).'</span>';
10759:                     }
10760:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10761:                         if ($bcc{$type} ne '') {
10762:                             my $bcctext;
10763:                             if (@text) {
10764:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
10765:                             } else {
10766:                                 $bcctext = '(Bcc)';
10767:                             }
10768:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
10769:                         } elsif (!@text) {
10770:                             $resulttext .= &mt('No one');
10771:                         }
10772:                         if ($includestr{$type} ne '') {
10773:                             if ($includeloc{$type} eq 'b') {
10774:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
10775:                             } elsif ($includeloc{$type} eq 's') {
10776:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
10777:                             }
10778:                         }
10779:                     } elsif (!@text) {
10780:                         $resulttext .= &mt('No recipients');
10781:                     }
10782:                     $resulttext .= '</li>';
10783:                 }
10784:             }
10785:             if (ref($changes{'overrides'}) eq 'ARRAY') {
10786:                 my @deletions;
10787:                 foreach my $type (@{$changes{'overrides'}}) {
10788:                     if ($usertypeshash{$type}) {
10789:                         if (grep(/^\Q$type\E/,@overrides)) {
10790:                             $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation set for [_1]",
10791:                                                       $usertypeshash{$type}).'<ul><li>';
10792:                             if (ref($newsetting{'override_'.$type}) eq 'HASH') {
10793:                                 my @text;
10794:                                 foreach my $item (@contacts) {
10795:                                     if ($newsetting{'override_'.$type}{$item}) {
10796:                                         push(@text,$short_titles->{$item});
10797:                                     }
10798:                                 }
10799:                                 if ($newsetting{'override_'.$type}{'others'} ne '') {
10800:                                     push(@text,$newsetting{'override_'.$type}{'others'});
10801:                                 }
10802: 
10803:                                 if (@text) {
10804:                                     $resulttext .= &mt('Helpdesk e-mail sent to: [_1]',
10805:                                                        '<span class="LC_cusr_emph">'.join(', ',@text).'</span>');
10806:                                 }
10807:                                 if ($newsetting{'override_'.$type}{'bcc'} ne '') {
10808:                                     my $bcctext;
10809:                                     if (@text) {
10810:                                         $bcctext = '&nbsp;'.&mt('with Bcc to');
10811:                                     } else {
10812:                                         $bcctext = '(Bcc)';
10813:                                     }
10814:                                     $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$newsetting{'override_'.$type}{'bcc'}.'</span>';
10815:                                 } elsif (!@text) {
10816:                                      $resulttext .= &mt('Helpdesk e-mail sent to no one');
10817:                                 }
10818:                                 $resulttext .= '</li>';
10819:                                 if ($newsetting{'override_'.$type}{'include'} ne '') {
10820:                                     my ($loc,$str) = split(/:/,$newsetting{'override_'.$type}{'include'});
10821:                                     if ($loc eq 'b') {
10822:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail body:').' '.&unescape($str).'</li>';
10823:                                     } elsif ($loc eq 's') {
10824:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail subject:').' '.&unescape($str).'</li>';
10825:                                     }
10826:                                 }
10827:                             }
10828:                             $resulttext .= '</li></ul></li>';
10829:                         } else {
10830:                             push(@deletions,$usertypeshash{$type});
10831:                         }
10832:                     }
10833:                 }
10834:                 if (@deletions) {
10835:                     $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation discontinued for: [_1]",
10836:                                               join(', ',@deletions)).'</li>';
10837:                 }
10838:             }
10839:             my @offon = ('off','on');
10840:             if ($changes{'reporterrors'}) {
10841:                 $resulttext .= '<li>'.
10842:                                &mt('E-mail error reports to [_1] set to "'.
10843:                                    $offon[$env{'form.reporterrors'}].'".',
10844:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
10845:                                        &mt('LON-CAPA core group - MSU'),600,500)).
10846:                                '</li>';
10847:             }
10848:             if ($changes{'reportupdates'}) {
10849:                 $resulttext .= '<li>'.
10850:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
10851:                                     $offon[$env{'form.reportupdates'}].'".',
10852:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
10853:                                         &mt('LON-CAPA core group - MSU'),600,500)).
10854:                                 '</li>';
10855:             }
10856:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
10857:                 my (@optional,@required,@unused,$maxsizechg);
10858:                 foreach my $field (@{$changes{'helpform'}}) {
10859:                     if ($field eq 'maxsize') {
10860:                         $maxsizechg = 1;
10861:                         next;
10862:                     }
10863:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
10864:                         push(@optional,$field);
10865:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
10866:                         push(@unused,$field);
10867:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
10868:                         push(@required,$field);
10869:                     }
10870:                 }
10871:                 if (@optional) {
10872:                     $resulttext .= '<li>'.
10873:                                    &mt('Help form fields changed to "Optional": [_1].',
10874:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
10875:                                    '</li>';
10876:                 }
10877:                 if (@required) {
10878:                     $resulttext .= '<li>'.
10879:                                    &mt('Help form fields changed to "Required": [_1].',
10880:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
10881:                                    '</li>';
10882:                 }
10883:                 if (@unused) {
10884:                     $resulttext .= '<li>'.
10885:                                    &mt('Help form fields changed to "Not shown": [_1].',
10886:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
10887:                                    '</li>';
10888:                 }
10889:                 if ($maxsizechg) {
10890:                     $resulttext .= '<li>'.
10891:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
10892:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
10893:                                    '</li>';
10894:                 }
10895:             }
10896:             $resulttext .= '</ul>';
10897:         } else {
10898:             $resulttext = &mt('No changes made to contacts and form settings');
10899:         }
10900:     } else {
10901:         $resulttext = '<span class="LC_error">'.
10902:             &mt('An error occurred: [_1].',$putresult).'</span>';
10903:     }
10904:     return $resulttext;
10905: }
10906: 
10907: sub modify_usercreation {
10908:     my ($dom,%domconfig) = @_;
10909:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
10910:     my $warningmsg;
10911:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
10912:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
10913:             if ($key eq 'cancreate') {
10914:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
10915:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
10916:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
10917:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
10918:                             ($item eq 'recaptchaversion')) {
10919:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
10920:                         } else {
10921:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
10922:                         }
10923:                     }
10924:                 }
10925:             } elsif ($key eq 'email_rule') {
10926:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
10927:             } else {
10928:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
10929:             }
10930:         }
10931:     }
10932:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
10933:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
10934:     my @contexts = ('author','course','requestcrs');
10935:     foreach my $item(@contexts) {
10936:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
10937:     }
10938:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
10939:         foreach my $item (@contexts) {
10940:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
10941:                 push(@{$changes{'cancreate'}},$item);
10942:             }
10943:         }
10944:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
10945:         foreach my $item (@contexts) {
10946:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
10947:                 if ($cancreate{$item} ne 'any') {
10948:                     push(@{$changes{'cancreate'}},$item);
10949:                 }
10950:             } else {
10951:                 if ($cancreate{$item} ne 'none') {
10952:                     push(@{$changes{'cancreate'}},$item);
10953:                 }
10954:             }
10955:         }
10956:     } else {
10957:         foreach my $item (@contexts)  {
10958:             push(@{$changes{'cancreate'}},$item);
10959:         }
10960:     }
10961: 
10962:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
10963:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
10964:             if (!grep(/^\Q$type\E$/,@username_rule)) {
10965:                 push(@{$changes{'username_rule'}},$type);
10966:             }
10967:         }
10968:         foreach my $type (@username_rule) {
10969:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
10970:                 push(@{$changes{'username_rule'}},$type);
10971:             }
10972:         }
10973:     } else {
10974:         push(@{$changes{'username_rule'}},@username_rule);
10975:     }
10976: 
10977:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
10978:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
10979:             if (!grep(/^\Q$type\E$/,@id_rule)) {
10980:                 push(@{$changes{'id_rule'}},$type);
10981:             }
10982:         }
10983:         foreach my $type (@id_rule) {
10984:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
10985:                 push(@{$changes{'id_rule'}},$type);
10986:             }
10987:         }
10988:     } else {
10989:         push(@{$changes{'id_rule'}},@id_rule);
10990:     }
10991: 
10992:     my @authen_contexts = ('author','course','domain');
10993:     my @authtypes = ('int','krb4','krb5','loc');
10994:     my %authhash;
10995:     foreach my $item (@authen_contexts) {
10996:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
10997:         foreach my $auth (@authtypes) {
10998:             if (grep(/^\Q$auth\E$/,@authallowed)) {
10999:                 $authhash{$item}{$auth} = 1;
11000:             } else {
11001:                 $authhash{$item}{$auth} = 0;
11002:             }
11003:         }
11004:     }
11005:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
11006:         foreach my $item (@authen_contexts) {
11007:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
11008:                 foreach my $auth (@authtypes) {
11009:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
11010:                         push(@{$changes{'authtypes'}},$item);
11011:                         last;
11012:                     }
11013:                 }
11014:             }
11015:         }
11016:     } else {
11017:         foreach my $item (@authen_contexts) {
11018:             push(@{$changes{'authtypes'}},$item);
11019:         }
11020:     }
11021: 
11022:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
11023:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
11024:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
11025:     $save_usercreate{'id_rule'} = \@id_rule;
11026:     $save_usercreate{'username_rule'} = \@username_rule,
11027:     $save_usercreate{'authtypes'} = \%authhash;
11028: 
11029:     my %usercreation_hash =  (
11030:         usercreation     => \%save_usercreate,
11031:     );
11032: 
11033:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
11034:                                              $dom);
11035: 
11036:     if ($putresult eq 'ok') {
11037:         if (keys(%changes) > 0) {
11038:             $resulttext = &mt('Changes made:').'<ul>';
11039:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
11040:                 my %lt = &usercreation_types();
11041:                 foreach my $type (@{$changes{'cancreate'}}) {
11042:                     my $chgtext = $lt{$type}.', ';
11043:                     if ($cancreate{$type} eq 'none') {
11044:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
11045:                     } elsif ($cancreate{$type} eq 'any') {
11046:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
11047:                     } elsif ($cancreate{$type} eq 'official') {
11048:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
11049:                     } elsif ($cancreate{$type} eq 'unofficial') {
11050:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
11051:                     }
11052:                     $resulttext .= '<li>'.$chgtext.'</li>';
11053:                 }
11054:             }
11055:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
11056:                 my ($rules,$ruleorder) = 
11057:                     &Apache::lonnet::inst_userrules($dom,'username');
11058:                 my $chgtext = '<ul>';
11059:                 foreach my $type (@username_rule) {
11060:                     if (ref($rules->{$type}) eq 'HASH') {
11061:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
11062:                     }
11063:                 }
11064:                 $chgtext .= '</ul>';
11065:                 if (@username_rule > 0) {
11066:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
11067:                 } else {
11068:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
11069:                 }
11070:             }
11071:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
11072:                 my ($idrules,$idruleorder) = 
11073:                     &Apache::lonnet::inst_userrules($dom,'id');
11074:                 my $chgtext = '<ul>';
11075:                 foreach my $type (@id_rule) {
11076:                     if (ref($idrules->{$type}) eq 'HASH') {
11077:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
11078:                     }
11079:                 }
11080:                 $chgtext .= '</ul>';
11081:                 if (@id_rule > 0) {
11082:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
11083:                 } else {
11084:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
11085:                 }
11086:             }
11087:             my %authname = &authtype_names();
11088:             my %context_title = &context_names();
11089:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
11090:                 my $chgtext = '<ul>';
11091:                 foreach my $type (@{$changes{'authtypes'}}) {
11092:                     my @allowed;
11093:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
11094:                     foreach my $auth (@authtypes) {
11095:                         if ($authhash{$type}{$auth}) {
11096:                             push(@allowed,$authname{$auth});
11097:                         }
11098:                     }
11099:                     if (@allowed > 0) {
11100:                         $chgtext .= join(', ',@allowed).'</li>';
11101:                     } else {
11102:                         $chgtext .= &mt('none').'</li>';
11103:                     }
11104:                 }
11105:                 $chgtext .= '</ul>';
11106:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
11107:                 $resulttext .= '</li>';
11108:             }
11109:             $resulttext .= '</ul>';
11110:         } else {
11111:             $resulttext = &mt('No changes made to user creation settings');
11112:         }
11113:     } else {
11114:         $resulttext = '<span class="LC_error">'.
11115:             &mt('An error occurred: [_1]',$putresult).'</span>';
11116:     }
11117:     if ($warningmsg ne '') {
11118:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
11119:     }
11120:     return $resulttext;
11121: }
11122: 
11123: sub modify_selfcreation {
11124:     my ($dom,%domconfig) = @_;
11125:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%changes,%cancreate);
11126:     my (%save_usercreate,%save_usermodify);
11127:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
11128:     if (ref($types) eq 'ARRAY') {
11129:         $usertypes->{'default'} = $othertitle;
11130:         push(@{$types},'default');
11131:     }
11132: #
11133: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
11134: #
11135:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
11136:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
11137:             if ($key eq 'cancreate') {
11138:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
11139:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
11140:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
11141:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') || 
11142:                             ($item eq 'recaptchaversion') ||
11143:                             ($item eq 'emailusername') || ($item eq 'notify') ||
11144:                             ($item eq 'selfcreateprocessing') || ($item eq 'shibenv')) {
11145:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11146:                         } else {
11147:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11148:                         }
11149:                     }
11150:                 }
11151:             } elsif ($key eq 'email_rule') {
11152:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
11153:             } else {
11154:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
11155:             }
11156:         }
11157:     }
11158: #
11159: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
11160: #
11161:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
11162:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
11163:             if ($key eq 'selfcreate') {
11164:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
11165:             } else {
11166:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
11167:             }
11168:         }
11169:     }
11170: 
11171:     my @contexts = ('selfcreate');
11172:     @{$cancreate{'selfcreate'}} = ();
11173:     %{$cancreate{'emailusername'}} = ();
11174:     @{$cancreate{'statustocreate'}} = ();
11175:     %{$cancreate{'selfcreateprocessing'}} = ();
11176:     %{$cancreate{'shibenv'}} = ();
11177:     my %selfcreatetypes = (
11178:                              sso   => 'users authenticated by institutional single sign on',
11179:                              login => 'users authenticated by institutional log-in',
11180:                              email => 'users who provide a valid e-mail address for use as username',
11181:                           );
11182: #
11183: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
11184: # is permitted.
11185: #
11186: 
11187:     my @statuses;
11188:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
11189:         if (ref($domconfig{'inststatus'}{'inststatusguest'}) eq 'ARRAY') {
11190:             @statuses = @{$domconfig{'inststatus'}{'inststatusguest'}};
11191:         }
11192:     }
11193:     push(@statuses,'default');
11194: 
11195:     foreach my $item ('login','sso','email') {
11196:         if ($item eq 'email') {
11197:             if ($env{'form.cancreate_email'}) {
11198:                 push(@{$cancreate{'selfcreate'}},'email');
11199:                 push(@contexts,'selfcreateprocessing');
11200:                 foreach my $type (@statuses) {
11201:                     if ($type eq 'default') {
11202:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess'};
11203:                     } else { 
11204:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
11205:                     }
11206:                 }
11207:             }
11208:         } else {
11209:             if ($env{'form.cancreate_'.$item}) {
11210:                 push(@{$cancreate{'selfcreate'}},$item);
11211:             }
11212:         }
11213:     }
11214:     my (@email_rule,%userinfo,%savecaptcha);
11215:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
11216: #
11217: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
11218: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
11219: #
11220: 
11221:     if ($env{'form.cancreate_email'}) {
11222:         push(@contexts,'emailusername');
11223:         if (ref($types) eq 'ARRAY') {
11224:             foreach my $type (@{$types}) {
11225:                 if (ref($infofields) eq 'ARRAY') {
11226:                     foreach my $field (@{$infofields}) {
11227:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
11228:                             $cancreate{'emailusername'}{$type}{$field} = $1;
11229:                         }
11230:                     }
11231:                 }
11232:             }
11233:         }
11234: #
11235: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
11236: # queued requests for self-creation of account using e-mail address as username
11237: #
11238: 
11239:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
11240:         @approvalnotify = sort(@approvalnotify);
11241:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
11242:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
11243:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
11244:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
11245:                     push(@{$changes{'cancreate'}},'notify');
11246:                 }
11247:             } else {
11248:                 if ($cancreate{'notify'}{'approval'}) {
11249:                     push(@{$changes{'cancreate'}},'notify');
11250:                 }
11251:             }
11252:         } elsif ($cancreate{'notify'}{'approval'}) {
11253:             push(@{$changes{'cancreate'}},'notify');
11254:         }
11255: 
11256: #
11257: # Retrieve rules (if any) governing types of e-mail address which may be used as a username
11258: #
11259:         @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
11260:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
11261:         if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
11262:             if (@{$curr_usercreation{'email_rule'}} > 0) {
11263:                 foreach my $type (@{$curr_usercreation{'email_rule'}}) {
11264:                     if (!grep(/^\Q$type\E$/,@email_rule)) {
11265:                         push(@{$changes{'email_rule'}},$type);
11266:                     }
11267:                 }
11268:             }
11269:             if (@email_rule > 0) {
11270:                 foreach my $type (@email_rule) {
11271:                     if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
11272:                         push(@{$changes{'email_rule'}},$type);
11273:                     }
11274:                 }
11275:             }
11276:         } elsif (@email_rule > 0) {
11277:             push(@{$changes{'email_rule'}},@email_rule);
11278:         }
11279:     }
11280: #  
11281: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
11282: # institutional log-in.
11283: #
11284:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
11285:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11286:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
11287:                ($domdefaults{'auth_def'} eq 'localauth'))) {
11288:             $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.').' '.
11289:                           &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.');
11290:         }
11291:     }
11292:     my @fields = ('lastname','firstname','middlename','generation',
11293:                   'permanentemail','id');
11294:     my @shibfields = (@fields,'inststatus');
11295:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11296: #
11297: # Where usernames may created for institutional log-in and/or institutional single sign on:
11298: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
11299: # may self-create accounts 
11300: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
11301: # which the user may supply, if institutional data is unavailable.
11302: #
11303:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
11304:         if (ref($types) eq 'ARRAY') {
11305:             if (@{$types} > 1) {
11306:                 @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
11307:                 push(@contexts,'statustocreate');
11308:             } else {
11309:                 undef($cancreate{'statustocreate'});
11310:             } 
11311:             foreach my $type (@{$types}) {
11312:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
11313:                 foreach my $field (@fields) {
11314:                     if (grep(/^\Q$field\E$/,@modifiable)) {
11315:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
11316:                     } else {
11317:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
11318:                     }
11319:                 }
11320:             }
11321:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
11322:                 foreach my $type (@{$types}) {
11323:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
11324:                         foreach my $field (@fields) {
11325:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
11326:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
11327:                                 push(@{$changes{'selfcreate'}},$type);
11328:                                 last;
11329:                             }
11330:                         }
11331:                     }
11332:                 }
11333:             } else {
11334:                 foreach my $type (@{$types}) {
11335:                     push(@{$changes{'selfcreate'}},$type);
11336:                 }
11337:             }
11338:         }
11339:         foreach my $field (@shibfields) {
11340:             if ($env{'form.shibenv_'.$field} ne '') {
11341:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
11342:             }
11343:         }
11344:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
11345:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
11346:                 foreach my $field (@shibfields) {
11347:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
11348:                         push(@{$changes{'cancreate'}},'shibenv');
11349:                     }
11350:                 }
11351:             } else {
11352:                 foreach my $field (@shibfields) {
11353:                     if ($env{'form.shibenv_'.$field}) {
11354:                         push(@{$changes{'cancreate'}},'shibenv');
11355:                         last;
11356:                     }
11357:                 }
11358:             }
11359:         }
11360:     }
11361:     foreach my $item (@contexts) {
11362:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
11363:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
11364:                 if (ref($cancreate{$item}) eq 'ARRAY') {
11365:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
11366:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11367:                             push(@{$changes{'cancreate'}},$item);
11368:                         }
11369:                     }
11370:                 }
11371:             }
11372:             if (ref($cancreate{$item}) eq 'ARRAY') {
11373:                 foreach my $type (@{$cancreate{$item}}) {
11374:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
11375:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11376:                             push(@{$changes{'cancreate'}},$item);
11377:                         }
11378:                     }
11379:                 }
11380:             }
11381:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
11382:             if (ref($cancreate{$item}) eq 'HASH') {
11383:                 foreach my $curr (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
11384:                     if (ref($curr_usercreation{'cancreate'}{$item}{$curr}) eq 'HASH') {
11385:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$curr}})) {
11386:                             unless ($curr_usercreation{'cancreate'}{$item}{$curr}{$field} eq $cancreate{$item}{$curr}{$field}) {
11387:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11388:                                     push(@{$changes{'cancreate'}},$item);
11389:                                 }
11390:                             }
11391:                         }
11392:                     } elsif ($item eq 'selfcreateprocessing') {
11393:                         if ($cancreate{$item}{$curr} ne $curr_usercreation{'cancreate'}{$item}{$curr}) {
11394:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11395:                                 push(@{$changes{'cancreate'}},$item);
11396:                             }
11397:                         }
11398:                     } else {
11399:                         if (!$cancreate{$item}{$curr}) {
11400:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11401:                                 push(@{$changes{'cancreate'}},$item);
11402:                             }
11403:                         }
11404:                     }
11405:                 }
11406:                 foreach my $field (keys(%{$cancreate{$item}})) {
11407:                     if (ref($cancreate{$item}{$field}) eq 'HASH') {
11408:                         foreach my $inner (keys(%{$cancreate{$item}{$field}})) {
11409:                             if (ref($curr_usercreation{'cancreate'}{$item}{$field}) eq 'HASH') {
11410:                                 unless ($curr_usercreation{'cancreate'}{$item}{$field}{$inner} eq $cancreate{$item}{$field}{$inner}) {
11411:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11412:                                         push(@{$changes{'cancreate'}},$item);
11413:                                     }
11414:                                 }
11415:                             } else {
11416:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11417:                                     push(@{$changes{'cancreate'}},$item);
11418:                                 }
11419:                             }
11420:                         }
11421:                     } elsif ($item eq 'selfcreateprocessing') {
11422:                         if ($cancreate{$item}{$field} ne $curr_usercreation{'cancreate'}{$item}{$field}) {
11423:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11424:                                 push(@{$changes{'cancreate'}},$item);
11425:                             }
11426:                         }
11427:                     } else {
11428:                         if (!$curr_usercreation{'cancreate'}{$item}{$field}) {
11429:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11430:                                 push(@{$changes{'cancreate'}},$item);
11431:                             }
11432:                         }
11433:                     }
11434:                 }
11435:             }
11436:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
11437:             if (ref($cancreate{$item}) eq 'ARRAY') {
11438:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
11439:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11440:                         push(@{$changes{'cancreate'}},$item);
11441:                     }
11442:                 }
11443:             } elsif (ref($cancreate{$item}) eq 'HASH') {
11444:                 if (!$cancreate{$item}{$curr_usercreation{'cancreate'}{$item}}) {
11445:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11446:                         push(@{$changes{'cancreate'}},$item);
11447:                     }
11448:                 }
11449:             }
11450:         } elsif ($item eq 'emailusername') {
11451:             if (ref($cancreate{$item}) eq 'HASH') {
11452:                 foreach my $type (keys(%{$cancreate{$item}})) {
11453:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
11454:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
11455:                             if ($cancreate{$item}{$type}{$field}) {
11456:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11457:                                     push(@{$changes{'cancreate'}},$item);
11458:                                 }
11459:                                 last;
11460:                             }
11461:                         }
11462:                     }
11463:                 }
11464:             }
11465:         }
11466:     }
11467: #
11468: # Populate %save_usercreate hash with updates to self-creation configuration.
11469: #
11470:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
11471:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
11472:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
11473:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
11474:     if (ref($cancreate{'notify'}) eq 'HASH') {
11475:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
11476:     }
11477:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
11478:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
11479:     }
11480:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
11481:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
11482:     }
11483:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
11484:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
11485:     }
11486:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
11487:     $save_usercreate{'email_rule'} = \@email_rule;
11488: 
11489:     my %userconfig_hash = (
11490:             usercreation     => \%save_usercreate,
11491:             usermodification => \%save_usermodify,
11492:     );
11493:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
11494:                                              $dom);
11495: #
11496: # Accumulate details of changes to domain cofiguration for self-creation of usernames in $resulttext
11497: #
11498:     if ($putresult eq 'ok') {
11499:         if (keys(%changes) > 0) {
11500:             $resulttext = &mt('Changes made:').'<ul>';
11501:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
11502:                 my %lt = &selfcreation_types();
11503:                 foreach my $type (@{$changes{'cancreate'}}) {
11504:                     my $chgtext;
11505:                     if ($type eq 'selfcreate') {
11506:                         if (@{$cancreate{$type}} == 0) {
11507:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
11508:                         } else {
11509:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
11510:                                         '<ul>';
11511:                             foreach my $case (@{$cancreate{$type}}) {
11512:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
11513:                             }
11514:                             $chgtext .= '</ul>';
11515:                             if (ref($cancreate{$type}) eq 'ARRAY') {
11516:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
11517:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
11518:                                         if (@{$cancreate{'statustocreate'}} == 0) {
11519:                                             $chgtext .= '<br />'.
11520:                                                         '<span class="LC_warning">'.
11521:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
11522:                                                         '</span>';
11523:                                         }
11524:                                     }
11525:                                 }
11526:                             }
11527:                         }
11528:                     } elsif ($type eq 'shibenv') {
11529:                         if (keys(%{$cancreate{$type}}) == 0) {
11530:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information'); 
11531:                         } else {
11532:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
11533:                                         '<ul>';
11534:                             foreach my $field (@shibfields) {
11535:                                 next if ($cancreate{$type}{$field} eq '');
11536:                                 if ($field eq 'inststatus') {
11537:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
11538:                                 } else {
11539:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
11540:                                 }
11541:                             }
11542:                             $chgtext .= '</ul>';
11543:                         }  
11544:                     } elsif ($type eq 'statustocreate') {
11545:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
11546:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
11547:                             if (@{$cancreate{'selfcreate'}} > 0) {
11548:                                 if (@{$cancreate{'statustocreate'}} == 0) {
11549:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
11550:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
11551:                                         $chgtext .= '<br />'.
11552:                                                     '<span class="LC_warning">'.
11553:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
11554:                                                     '</span>';
11555:                                     }
11556:                                 } elsif (ref($usertypes) eq 'HASH') {
11557:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
11558:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
11559:                                     } else {
11560:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
11561:                                     }
11562:                                     $chgtext .= '<ul>';
11563:                                     foreach my $case (@{$cancreate{$type}}) {
11564:                                         if ($case eq 'default') {
11565:                                             $chgtext .= '<li>'.$othertitle.'</li>';
11566:                                         } else {
11567:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
11568:                                         }
11569:                                     }
11570:                                     $chgtext .= '</ul>';
11571:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
11572:                                         $chgtext .= '<br /><span class="LC_warning">'.
11573:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
11574:                                                     '</span>';
11575:                                     }
11576:                                 }
11577:                             } else {
11578:                                 if (@{$cancreate{$type}} == 0) {
11579:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
11580:                                 } else {
11581:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
11582:                                 }
11583:                             }
11584:                         }
11585:                     } elsif ($type eq 'selfcreateprocessing') {
11586:                         my %choices = &Apache::lonlocal::texthash (
11587:                                                                     automatic => 'Automatic approval',
11588:                                                                     approval  => 'Queued for approval',
11589:                                                                   );
11590:                         if (@statuses > 1) {
11591:                             $chgtext .= &mt('Processing of requests to create account with e-mail address as username set as follows:'). 
11592:                                         '<ul>';
11593:                            foreach my $type (@statuses) {
11594:                                if ($type eq 'default') {
11595:                                    $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
11596:                                } else {
11597:                                    $chgtext .= '<li>'.$usertypes->{$type}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
11598:                                }
11599:                            }
11600:                            $chgtext .= '</ul>';
11601:                         } else {
11602:                            $chgtext .= &mt('Processing of requests to create account with e-mail address as username set to: "[_1]"',
11603:                                          $choices{$cancreate{'selfcreateprocessing'}{'default'}});
11604:                         }
11605:                     } elsif ($type eq 'captcha') {
11606:                         if ($savecaptcha{$type} eq 'notused') {
11607:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
11608:                         } else {
11609:                             my %captchas = &captcha_phrases();
11610:                             if ($captchas{$savecaptcha{$type}}) {
11611:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
11612:                             } else {
11613:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
11614:                             }
11615:                         }
11616:                     } elsif ($type eq 'recaptchakeys') {
11617:                         my ($privkey,$pubkey);
11618:                         if (ref($savecaptcha{$type}) eq 'HASH') {
11619:                             $pubkey = $savecaptcha{$type}{'public'};
11620:                             $privkey = $savecaptcha{$type}{'private'};
11621:                         }
11622:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
11623:                         if (!$pubkey) {
11624:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
11625:                         } else {
11626:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
11627:                         }
11628:                         if (!$privkey) {
11629:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
11630:                         } else {
11631:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
11632:                         }
11633:                         $chgtext .= '</ul>';
11634:                     } elsif ($type eq 'recaptchaversion') {
11635:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
11636:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
11637:                         }
11638:                     } elsif ($type eq 'emailusername') {
11639:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
11640:                             if (ref($types) eq 'ARRAY') {
11641:                                 foreach my $type (@{$types}) {
11642:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
11643:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
11644:                                             $chgtext .= &mt('When self-creating account with e-mail as username, the following information will be provided by [_1]:',"'$usertypes->{$type}'").
11645:                                                     '<ul>';
11646:                                             foreach my $field (@{$infofields}) {
11647:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
11648:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
11649:                                                 }
11650:                                             }
11651:                                             $chgtext .= '</ul>';
11652:                                         } else {
11653:                                             $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 />';
11654:                                         }
11655:                                     } else {
11656:                                         $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 />';
11657:                                     }
11658:                                 }
11659:                             }
11660:                         }
11661:                     } elsif ($type eq 'notify') {
11662:                         $chgtext = &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
11663:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
11664:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
11665:                                 if ($cancreate{'notify'}{'approval'}) {
11666:                                     $chgtext = &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
11667:                                 }
11668:                             }
11669:                         }
11670:                     }
11671:                     if ($chgtext) {
11672:                         $resulttext .= '<li>'.$chgtext.'</li>';
11673:                     }
11674:                 }
11675:             }
11676:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
11677:                 my ($emailrules,$emailruleorder) =
11678:                     &Apache::lonnet::inst_userrules($dom,'email');
11679:                 my $chgtext = '<ul>';
11680:                 foreach my $type (@email_rule) {
11681:                     if (ref($emailrules->{$type}) eq 'HASH') {
11682:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
11683:                     }
11684:                 }
11685:                 $chgtext .= '</ul>';
11686:                 if (@email_rule > 0) {
11687:                     $resulttext .= '<li>'.
11688:                                    &mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').
11689:                                        $chgtext.
11690:                                    '</li>';
11691:                 } else {
11692:                     $resulttext .= '<li>'.
11693:                                    &mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').
11694:                                    '</li>';
11695:                 }
11696:             }
11697:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
11698:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
11699:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11700:                 foreach my $type (@{$changes{'selfcreate'}}) {
11701:                     my $typename = $type;
11702:                     if (ref($usertypes) eq 'HASH') {
11703:                         if ($usertypes->{$type} ne '') {
11704:                             $typename = $usertypes->{$type};
11705:                         }
11706:                     }
11707:                     my @modifiable;
11708:                     $resulttext .= '<li>'.
11709:                                     &mt('Self-creation of account by users with status: [_1]',
11710:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
11711:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
11712:                     foreach my $field (@fields) {
11713:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
11714:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
11715:                         }
11716:                     }
11717:                     if (@modifiable > 0) {
11718:                         $resulttext .= join(', ',@modifiable);
11719:                     } else {
11720:                         $resulttext .= &mt('none');
11721:                     }
11722:                     $resulttext .= '</li>';
11723:                 }
11724:                 $resulttext .= '</ul></li>';
11725:             }
11726:             $resulttext .= '</ul>';
11727:         } else {
11728:             $resulttext = &mt('No changes made to self-creation settings');
11729:         }
11730:     } else {
11731:         $resulttext = '<span class="LC_error">'.
11732:             &mt('An error occurred: [_1]',$putresult).'</span>';
11733:     }
11734:     if ($warningmsg ne '') {
11735:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
11736:     }
11737:     return $resulttext;
11738: }
11739: 
11740: sub process_captcha {
11741:     my ($container,$changes,$newsettings,$current) = @_;
11742:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
11743:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
11744:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
11745:         $newsettings->{'captcha'} = 'original';
11746:     }
11747:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
11748:         if ($container eq 'cancreate') {
11749:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
11750:                 push(@{$changes->{'cancreate'}},'captcha');
11751:             } elsif (!defined($changes->{'cancreate'})) {
11752:                 $changes->{'cancreate'} = ['captcha'];
11753:             }
11754:         } else {
11755:             $changes->{'captcha'} = 1;
11756:         }
11757:     }
11758:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
11759:     if ($newsettings->{'captcha'} eq 'recaptcha') {
11760:         $newpub = $env{'form.'.$container.'_recaptchapub'};
11761:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
11762:         $newpub =~ s/[^\w\-]//g;
11763:         $newpriv =~ s/[^\w\-]//g;
11764:         $newsettings->{'recaptchakeys'} = {
11765:                                              public  => $newpub,
11766:                                              private => $newpriv,
11767:                                           };
11768:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
11769:         $newversion =~ s/\D//g;
11770:         if ($newversion ne '2') {
11771:             $newversion = 1;
11772:         }
11773:         $newsettings->{'recaptchaversion'} = $newversion;
11774:     }
11775:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
11776:         $currpub = $current->{'recaptchakeys'}{'public'};
11777:         $currpriv = $current->{'recaptchakeys'}{'private'};
11778:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
11779:             $newsettings->{'recaptchakeys'} = {
11780:                                                  public  => '',
11781:                                                  private => '',
11782:                                               }
11783:         }
11784:     }
11785:     if ($current->{'captcha'} eq 'recaptcha') {
11786:         $currversion = $current->{'recaptchaversion'};
11787:         if ($currversion ne '2') {
11788:             $currversion = 1;
11789:         }
11790:     }
11791:     if ($currversion ne $newversion) {
11792:         if ($container eq 'cancreate') {
11793:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
11794:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
11795:             } elsif (!defined($changes->{'cancreate'})) {
11796:                 $changes->{'cancreate'} = ['recaptchaversion'];
11797:             }
11798:         } else {
11799:             $changes->{'recaptchaversion'} = 1;
11800:         }
11801:     }
11802:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
11803:         if ($container eq 'cancreate') {
11804:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
11805:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
11806:             } elsif (!defined($changes->{'cancreate'})) {
11807:                 $changes->{'cancreate'} = ['recaptchakeys'];
11808:             }
11809:         } else {
11810:             $changes->{'recaptchakeys'} = 1;
11811:         }
11812:     }
11813:     return;
11814: }
11815: 
11816: sub modify_usermodification {
11817:     my ($dom,%domconfig) = @_;
11818:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
11819:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
11820:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
11821:             if ($key eq 'selfcreate') {
11822:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
11823:             } else {  
11824:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
11825:             }
11826:         }
11827:     }
11828:     my @contexts = ('author','course');
11829:     my %context_title = (
11830:                            author => 'In author context',
11831:                            course => 'In course context',
11832:                         );
11833:     my @fields = ('lastname','firstname','middlename','generation',
11834:                   'permanentemail','id');
11835:     my %roles = (
11836:                   author => ['ca','aa'],
11837:                   course => ['st','ep','ta','in','cr'],
11838:                 );
11839:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11840:     foreach my $context (@contexts) {
11841:         foreach my $role (@{$roles{$context}}) {
11842:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
11843:             foreach my $item (@fields) {
11844:                 if (grep(/^\Q$item\E$/,@modifiable)) {
11845:                     $modifyhash{$context}{$role}{$item} = 1;
11846:                 } else {
11847:                     $modifyhash{$context}{$role}{$item} = 0;
11848:                 }
11849:             }
11850:         }
11851:         if (ref($curr_usermodification{$context}) eq 'HASH') {
11852:             foreach my $role (@{$roles{$context}}) {
11853:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
11854:                     foreach my $field (@fields) {
11855:                         if ($modifyhash{$context}{$role}{$field} ne 
11856:                                 $curr_usermodification{$context}{$role}{$field}) {
11857:                             push(@{$changes{$context}},$role);
11858:                             last;
11859:                         }
11860:                     }
11861:                 }
11862:             }
11863:         } else {
11864:             foreach my $context (@contexts) {
11865:                 foreach my $role (@{$roles{$context}}) {
11866:                     push(@{$changes{$context}},$role);
11867:                 }
11868:             }
11869:         }
11870:     }
11871:     my %usermodification_hash =  (
11872:                                    usermodification => \%modifyhash,
11873:                                  );
11874:     my $putresult = &Apache::lonnet::put_dom('configuration',
11875:                                              \%usermodification_hash,$dom);
11876:     if ($putresult eq 'ok') {
11877:         if (keys(%changes) > 0) {
11878:             $resulttext = &mt('Changes made: ').'<ul>';
11879:             foreach my $context (@contexts) {
11880:                 if (ref($changes{$context}) eq 'ARRAY') {
11881:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
11882:                     if (ref($changes{$context}) eq 'ARRAY') {
11883:                         foreach my $role (@{$changes{$context}}) {
11884:                             my $rolename;
11885:                             if ($role eq 'cr') {
11886:                                 $rolename = &mt('Custom');
11887:                             } else {
11888:                                 $rolename = &Apache::lonnet::plaintext($role);
11889:                             }
11890:                             my @modifiable;
11891:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
11892:                             foreach my $field (@fields) {
11893:                                 if ($modifyhash{$context}{$role}{$field}) {
11894:                                     push(@modifiable,$fieldtitles{$field});
11895:                                 }
11896:                             }
11897:                             if (@modifiable > 0) {
11898:                                 $resulttext .= join(', ',@modifiable);
11899:                             } else {
11900:                                 $resulttext .= &mt('none'); 
11901:                             }
11902:                             $resulttext .= '</li>';
11903:                         }
11904:                         $resulttext .= '</ul></li>';
11905:                     }
11906:                 }
11907:             }
11908:             $resulttext .= '</ul>';
11909:         } else {
11910:             $resulttext = &mt('No changes made to user modification settings');
11911:         }
11912:     } else {
11913:         $resulttext = '<span class="LC_error">'.
11914:             &mt('An error occurred: [_1]',$putresult).'</span>';
11915:     }
11916:     return $resulttext;
11917: }
11918: 
11919: sub modify_defaults {
11920:     my ($dom,$lastactref,%domconfig) = @_;
11921:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
11922:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11923:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
11924:                  'portal_def','intauth_cost','intauth_check','intauth_switch');
11925:     my @authtypes = ('internal','krb4','krb5','localauth');
11926:     foreach my $item (@items) {
11927:         $newvalues{$item} = $env{'form.'.$item};
11928:         if ($item eq 'auth_def') {
11929:             if ($newvalues{$item} ne '') {
11930:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
11931:                     push(@errors,$item);
11932:                 }
11933:             }
11934:         } elsif ($item eq 'lang_def') {
11935:             if ($newvalues{$item} ne '') {
11936:                 if ($newvalues{$item} =~ /^(\w+)/) {
11937:                     my $langcode = $1;
11938:                     if ($langcode ne 'x_chef') {
11939:                         if (code2language($langcode) eq '') {
11940:                             push(@errors,$item);
11941:                         }
11942:                     }
11943:                 } else {
11944:                     push(@errors,$item);
11945:                 }
11946:             }
11947:         } elsif ($item eq 'timezone_def') {
11948:             if ($newvalues{$item} ne '') {
11949:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
11950:                     push(@errors,$item);   
11951:                 }
11952:             }
11953:         } elsif ($item eq 'datelocale_def') {
11954:             if ($newvalues{$item} ne '') {
11955:                 my @datelocale_ids = DateTime::Locale->ids();
11956:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
11957:                     push(@errors,$item);
11958:                 }
11959:             }
11960:         } elsif ($item eq 'portal_def') {
11961:             if ($newvalues{$item} ne '') {
11962:                 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])\/?$/) {
11963:                     push(@errors,$item);
11964:                 }
11965:             }
11966:         } elsif ($item eq 'intauth_cost') {
11967:             if ($newvalues{$item} ne '') {
11968:                 if ($newvalues{$item} =~ /\D/) {
11969:                     push(@errors,$item);
11970:                 }
11971:             }
11972:         } elsif ($item eq 'intauth_check') {
11973:             if ($newvalues{$item} ne '') {
11974:                 unless ($newvalues{$item} =~ /^(0|1|2)$/) {
11975:                     push(@errors,$item);
11976:                 }
11977:             }
11978:         } elsif ($item eq 'intauth_switch') {
11979:             if ($newvalues{$item} ne '') {
11980:                 unless ($newvalues{$item} =~ /^(0|1|2)$/) {
11981:                     push(@errors,$item);
11982:                 }
11983:             }
11984:         }
11985:         if (grep(/^\Q$item\E$/,@errors)) {
11986:             $newvalues{$item} = $domdefaults{$item};
11987:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
11988:             $changes{$item} = 1;
11989:         }
11990:         $domdefaults{$item} = $newvalues{$item};
11991:     }
11992:     my %defaults_hash = (
11993:                          defaults => \%newvalues,
11994:                         );
11995:     my $title = &defaults_titles();
11996: 
11997:     my $currinststatus;
11998:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
11999:         $currinststatus = $domconfig{'inststatus'};
12000:     } else {
12001:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12002:         $currinststatus = {
12003:                              inststatustypes => $usertypes,
12004:                              inststatusorder => $types,
12005:                              inststatusguest => [],
12006:                           };
12007:     }
12008:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
12009:     my @allpos;
12010:     my %guests;
12011:     my %alltypes;
12012:     my ($currtitles,$currguests,$currorder);
12013:     if (ref($currinststatus) eq 'HASH') {
12014:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
12015:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
12016:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
12017:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
12018:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
12019:                     }
12020:                 }
12021:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
12022:                     my $position = $env{'form.inststatus_pos_'.$type};
12023:                     $position =~ s/\D+//g;
12024:                     $allpos[$position] = $type;
12025:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
12026:                     $alltypes{$type} =~ s/`//g;
12027:                     if ($env{'form.inststatus_guest_'.$type}) {
12028:                         $guests{$type} = 1;
12029:                     }
12030:                 }
12031:             }
12032:             if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
12033:                 $currguests = join(',',@{$currinststatus->{'inststatusguest'}});
12034:             }
12035:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
12036:             $currtitles =~ s/,$//;
12037:         }
12038:     }
12039:     if ($env{'form.addinststatus'}) {
12040:         my $newtype = $env{'form.addinststatus'};
12041:         $newtype =~ s/\W//g;
12042:         unless (exists($alltypes{$newtype})) {
12043:             if ($env{'form.addinststatus_guest'}) {
12044:                 $guests{$newtype} = 1;
12045:             }
12046:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
12047:             $alltypes{$newtype} =~ s/`//g; 
12048:             my $position = $env{'form.addinststatus_pos'};
12049:             $position =~ s/\D+//g;
12050:             if ($position ne '') {
12051:                 $allpos[$position] = $newtype;
12052:             }
12053:         }
12054:     }
12055:     my (@orderedstatus,@orderedguests);
12056:     foreach my $type (@allpos) {
12057:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
12058:             push(@orderedstatus,$type);
12059:             if ($guests{$type}) {
12060:                 push(@orderedguests,$type);
12061:             }
12062:         }
12063:     }
12064:     foreach my $type (keys(%alltypes)) {
12065:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
12066:             delete($alltypes{$type});
12067:         }
12068:     }
12069:     $defaults_hash{'inststatus'} = {
12070:                                      inststatustypes => \%alltypes,
12071:                                      inststatusorder => \@orderedstatus,
12072:                                      inststatusguest => \@orderedguests,
12073:                                    };
12074:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
12075:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
12076:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
12077:         }
12078:     }
12079:     if ($currorder ne join(',',@orderedstatus)) {
12080:         $changes{'inststatus'}{'inststatusorder'} = 1;
12081:     }
12082:     if ($currguests ne join(',',@orderedguests)) {
12083:         $changes{'inststatus'}{'inststatusguest'} = 1;
12084:     }
12085:     my $newtitles;
12086:     foreach my $item (@orderedstatus) {
12087:         $newtitles .= $alltypes{$item}.',';
12088:     }
12089:     $newtitles =~ s/,$//;
12090:     if ($currtitles ne $newtitles) {
12091:         $changes{'inststatus'}{'inststatustypes'} = 1;
12092:     }
12093:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
12094:                                              $dom);
12095:     if ($putresult eq 'ok') {
12096:         if (keys(%changes) > 0) {
12097:             $resulttext = &mt('Changes made:').'<ul>';
12098:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
12099:             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";
12100:             foreach my $item (sort(keys(%changes))) {
12101:                 if ($item eq 'inststatus') {
12102:                     if (ref($changes{'inststatus'}) eq 'HASH') {
12103:                         if (($changes{'inststatus'}{'inststatustypes'}) || $changes{'inststatus'}{'inststatusorder'}) {
12104:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
12105:                             foreach my $type (@orderedstatus) { 
12106:                                 $resulttext .= $alltypes{$type}.', ';
12107:                             }
12108:                             $resulttext =~ s/, $//;
12109:                             $resulttext .= '</li>';
12110:                         }
12111:                         if ($changes{'inststatus'}{'inststatusguest'}) {
12112:                             $resulttext .= '<li>'; 
12113:                             if (@orderedguests) {
12114:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to:').' ';
12115:                                 foreach my $type (@orderedguests) {
12116:                                     $resulttext .= $alltypes{$type}.', ';
12117:                                 }
12118:                                 $resulttext =~ s/, $//;
12119:                             } else {
12120:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to none.');
12121:                             }
12122:                             $resulttext .= '</li>';
12123:                         }
12124:                     }
12125:                 } else {
12126:                     my $value = $env{'form.'.$item};
12127:                     if ($value eq '') {
12128:                         $value = &mt('none');
12129:                     } elsif ($item eq 'auth_def') {
12130:                         my %authnames = &authtype_names();
12131:                         my %shortauth = (
12132:                                           internal   => 'int',
12133:                                           krb4       => 'krb4',
12134:                                           krb5       => 'krb5',
12135:                                           localauth  => 'loc',
12136:                         );
12137:                         $value = $authnames{$shortauth{$value}};
12138:                     } elsif ($item eq 'intauth_switch') {
12139:                         my %optiondesc = &Apache::lonlocal::texthash (
12140:                                             0 => 'No',
12141:                                             1 => 'Yes',
12142:                                             2 => 'Yes, and copy existing passwd file to passwd.bak file',
12143:                                          );
12144:                         if ($value =~ /^(0|1|2)$/) {
12145:                             $value = $optiondesc{$value};
12146:                         } else {
12147:                             $value = &mt('none -- defaults to No');
12148:                         }
12149:                     } elsif ($item eq 'intauth_check') {
12150:                         my %optiondesc = &Apache::lonlocal::texthash (
12151:                                              0 => 'No',
12152:                                              1 => 'Yes, allow login then update passwd file using default cost (if higher)',
12153:                                              2 => 'Yes, disallow login if stored cost is less than domain default',
12154:                                          );
12155:                         if ($value =~ /^(0|1|2)$/) {
12156:                             $value = $optiondesc{$value};
12157:                         } else {
12158:                             $value = &mt('none -- defaults to No');
12159:                         }
12160:                     }
12161:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
12162:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
12163:                 }
12164:             }
12165:             $resulttext .= '</ul>';
12166:             $mailmsgtext .= "\n";
12167:             my $cachetime = 24*60*60;
12168:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12169:             if (ref($lastactref) eq 'HASH') {
12170:                 $lastactref->{'domdefaults'} = 1;
12171:             }
12172:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
12173:                 my $notify = 1;
12174:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
12175:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
12176:                         $notify = 0;
12177:                     }
12178:                 }
12179:                 if ($notify) {
12180:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
12181:                                                "LON-CAPA Domain Settings Change - $dom",
12182:                                                $mailmsgtext);
12183:                 }
12184:             }
12185:         } else {
12186:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
12187:         }
12188:     } else {
12189:         $resulttext = '<span class="LC_error">'.
12190:             &mt('An error occurred: [_1]',$putresult).'</span>';
12191:     }
12192:     if (@errors > 0) {
12193:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
12194:         foreach my $item (@errors) {
12195:             $resulttext .= ' "'.$title->{$item}.'",';
12196:         }
12197:         $resulttext =~ s/,$//;
12198:     }
12199:     return $resulttext;
12200: }
12201: 
12202: sub modify_scantron {
12203:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
12204:     my ($resulttext,%confhash,%changes,$errors);
12205:     my $custom = 'custom.tab';
12206:     my $default = 'default.tab';
12207:     my $servadm = $r->dir_config('lonAdmEMail');
12208:     my ($configuserok,$author_ok,$switchserver) = 
12209:         &config_check($dom,$confname,$servadm);
12210:     if ($env{'form.scantronformat.filename'} ne '') {
12211:         my $error;
12212:         if ($configuserok eq 'ok') {
12213:             if ($switchserver) {
12214:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
12215:             } else {
12216:                 if ($author_ok eq 'ok') {
12217:                     my ($result,$scantronurl) =
12218:                         &publishlogo($r,'upload','scantronformat',$dom,
12219:                                      $confname,'scantron','','',$custom);
12220:                     if ($result eq 'ok') {
12221:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
12222:                         $changes{'scantronformat'} = 1;
12223:                     } else {
12224:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
12225:                     }
12226:                 } else {
12227:                     $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);
12228:                 }
12229:             }
12230:         } else {
12231:             $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);
12232:         }
12233:         if ($error) {
12234:             &Apache::lonnet::logthis($error);
12235:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12236:         }
12237:     }
12238:     if (ref($domconfig{'scantron'}) eq 'HASH') {
12239:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
12240:             if ($env{'form.scantronformat_del'}) {
12241:                 $confhash{'scantron'}{'scantronformat'} = '';
12242:                 $changes{'scantronformat'} = 1;
12243:             }
12244:         }
12245:     }
12246:     if (keys(%confhash) > 0) {
12247:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
12248:                                                  $dom);
12249:         if ($putresult eq 'ok') {
12250:             if (keys(%changes) > 0) {
12251:                 if (ref($confhash{'scantron'}) eq 'HASH') {
12252:                     $resulttext = &mt('Changes made:').'<ul>';
12253:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
12254:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
12255:                     } else {
12256:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
12257:                     }
12258:                     $resulttext .= '</ul>';
12259:                 } else {
12260:                     $resulttext = &mt('Changes made to bubblesheet format file.');
12261:                 }
12262:                 $resulttext .= '</ul>';
12263:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
12264:                 if (ref($lastactref) eq 'HASH') {
12265:                     $lastactref->{'domainconfig'} = 1;
12266:                 }
12267:             } else {
12268:                 $resulttext = &mt('No changes made to bubblesheet format file');
12269:             }
12270:         } else {
12271:             $resulttext = '<span class="LC_error">'.
12272:                 &mt('An error occurred: [_1]',$putresult).'</span>';
12273:         }
12274:     } else {
12275:         $resulttext = &mt('No changes made to bubblesheet format file'); 
12276:     }
12277:     if ($errors) {
12278:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
12279:                        $errors.'</ul>';
12280:     }
12281:     return $resulttext;
12282: }
12283: 
12284: sub modify_coursecategories {
12285:     my ($dom,$lastactref,%domconfig) = @_;
12286:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
12287:         $cathash);
12288:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
12289:     my @catitems = ('unauth','auth');
12290:     my @cattypes = ('std','domonly','codesrch','none');
12291:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
12292:         $cathash = $domconfig{'coursecategories'}{'cats'};
12293:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
12294:             $changes{'togglecats'} = 1;
12295:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
12296:         }
12297:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
12298:             $changes{'categorize'} = 1;
12299:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
12300:         }
12301:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
12302:             $changes{'togglecatscomm'} = 1;
12303:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
12304:         }
12305:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
12306:             $changes{'categorizecomm'} = 1;
12307:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
12308:         }
12309:         foreach my $item (@catitems) {
12310:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
12311:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
12312:                     $changes{$item} = 1;
12313:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
12314:                 }
12315:             }
12316:         }
12317:     } else {
12318:         $changes{'togglecats'} = 1;
12319:         $changes{'categorize'} = 1;
12320:         $changes{'togglecatscomm'} = 1;
12321:         $changes{'categorizecomm'} = 1;
12322:         $domconfig{'coursecategories'} = {
12323:                                              togglecats => $env{'form.togglecats'},
12324:                                              categorize => $env{'form.categorize'},
12325:                                              togglecatscomm => $env{'form.togglecatscomm'},
12326:                                              categorizecomm => $env{'form.categorizecomm'},
12327:                                          };
12328:         foreach my $item (@catitems) {
12329:             if ($env{'form.coursecat_'.$item} ne 'std') {
12330:                 $changes{$item} = 1;
12331:             }
12332:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
12333:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
12334:             }
12335:         }
12336:     }
12337:     if (ref($cathash) eq 'HASH') {
12338:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
12339:             push (@deletecategory,'instcode::0');
12340:         }
12341:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
12342:             push(@deletecategory,'communities::0');
12343:         }
12344:     }
12345:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
12346:     if (ref($cathash) eq 'HASH') {
12347:         if (@deletecategory > 0) {
12348:             #FIXME Need to remove category from all courses using a deleted category 
12349:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
12350:             foreach my $item (@deletecategory) {
12351:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
12352:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
12353:                     $deletions{$item} = 1;
12354:                     &recurse_cat_deletes($item,$cathash,\%deletions);
12355:                 }
12356:             }
12357:         }
12358:         foreach my $item (keys(%{$cathash})) {
12359:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
12360:             if ($cathash->{$item} ne $env{'form.'.$item}) {
12361:                 $reorderings{$item} = 1;
12362:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
12363:             }
12364:             if ($env{'form.addcategory_name_'.$item} ne '') {
12365:                 my $newcat = $env{'form.addcategory_name_'.$item};
12366:                 my $newdepth = $depth+1;
12367:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
12368:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
12369:                 $adds{$newitem} = 1; 
12370:             }
12371:             if ($env{'form.subcat_'.$item} ne '') {
12372:                 my $newcat = $env{'form.subcat_'.$item};
12373:                 my $newdepth = $depth+1;
12374:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
12375:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
12376:                 $adds{$newitem} = 1;
12377:             }
12378:         }
12379:     }
12380:     if ($env{'form.instcode'} eq '1') {
12381:         if (ref($cathash) eq 'HASH') {
12382:             my $newitem = 'instcode::0';
12383:             if ($cathash->{$newitem} eq '') {  
12384:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
12385:                 $adds{$newitem} = 1;
12386:             }
12387:         } else {
12388:             my $newitem = 'instcode::0';
12389:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
12390:             $adds{$newitem} = 1;
12391:         }
12392:     }
12393:     if ($env{'form.communities'} eq '1') {
12394:         if (ref($cathash) eq 'HASH') {
12395:             my $newitem = 'communities::0';
12396:             if ($cathash->{$newitem} eq '') {
12397:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
12398:                 $adds{$newitem} = 1;
12399:             }
12400:         } else {
12401:             my $newitem = 'communities::0';
12402:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
12403:             $adds{$newitem} = 1;
12404:         }
12405:     }
12406:     if ($env{'form.addcategory_name'} ne '') {
12407:         if (($env{'form.addcategory_name'} ne 'instcode') &&
12408:             ($env{'form.addcategory_name'} ne 'communities')) {
12409:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
12410:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
12411:             $adds{$newitem} = 1;
12412:         }
12413:     }
12414:     my $putresult;
12415:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
12416:         if (keys(%deletions) > 0) {
12417:             foreach my $key (keys(%deletions)) {
12418:                 if ($predelallitems{$key} ne '') {
12419:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
12420:                 }
12421:             }
12422:         }
12423:         my (@chkcats,@chktrails,%chkallitems);
12424:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
12425:         if (ref($chkcats[0]) eq 'ARRAY') {
12426:             my $depth = 0;
12427:             my $chg = 0;
12428:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
12429:                 my $name = $chkcats[0][$i];
12430:                 my $item;
12431:                 if ($name eq '') {
12432:                     $chg ++;
12433:                 } else {
12434:                     $item = &escape($name).'::0';
12435:                     if ($chg) {
12436:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
12437:                     }
12438:                     $depth ++; 
12439:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
12440:                     $depth --;
12441:                 }
12442:             }
12443:         }
12444:     }
12445:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
12446:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
12447:         if ($putresult eq 'ok') {
12448:             my %title = (
12449:                          togglecats     => 'Show/Hide a course in catalog',
12450:                          categorize     => 'Assign a category to a course',
12451:                          togglecatscomm => 'Show/Hide a community in catalog',
12452:                          categorizecomm => 'Assign a category to a community',
12453:                         );
12454:             my %level = (
12455:                          dom  => 'set in Domain ("Modify Course/Community")',
12456:                          crs  => 'set in Course ("Course Configuration")',
12457:                          comm => 'set in Community ("Community Configuration")',
12458:                          none     => 'No catalog',
12459:                          std      => 'Standard catalog',
12460:                          domonly  => 'Domain-only catalog',
12461:                          codesrch => 'Code search form',
12462:                         );
12463:             $resulttext = &mt('Changes made:').'<ul>';
12464:             if ($changes{'togglecats'}) {
12465:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
12466:             }
12467:             if ($changes{'categorize'}) {
12468:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
12469:             }
12470:             if ($changes{'togglecatscomm'}) {
12471:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
12472:             }
12473:             if ($changes{'categorizecomm'}) {
12474:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
12475:             }
12476:             if ($changes{'unauth'}) {
12477:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
12478:             }
12479:             if ($changes{'auth'}) {
12480:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
12481:             }
12482:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
12483:                 my $cathash;
12484:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
12485:                     $cathash = $domconfig{'coursecategories'}{'cats'};
12486:                 } else {
12487:                     $cathash = {};
12488:                 } 
12489:                 my (@cats,@trails,%allitems);
12490:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
12491:                 if (keys(%deletions) > 0) {
12492:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
12493:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
12494:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
12495:                     }
12496:                     $resulttext .= '</ul></li>';
12497:                 }
12498:                 if (keys(%reorderings) > 0) {
12499:                     my %sort_by_trail;
12500:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
12501:                     foreach my $key (keys(%reorderings)) {
12502:                         if ($allitems{$key} ne '') {
12503:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
12504:                         }
12505:                     }
12506:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
12507:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
12508:                     }
12509:                     $resulttext .= '</ul></li>';
12510:                 }
12511:                 if (keys(%adds) > 0) {
12512:                     my %sort_by_trail;
12513:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
12514:                     foreach my $key (keys(%adds)) {
12515:                         if ($allitems{$key} ne '') {
12516:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
12517:                         }
12518:                     }
12519:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
12520:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
12521:                     }
12522:                     $resulttext .= '</ul></li>';
12523:                 }
12524:             }
12525:             $resulttext .= '</ul>';
12526:             if ($changes{'unauth'} || $changes{'auth'}) {
12527:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
12528:                 if ($changes{'auth'}) {
12529:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
12530:                 }
12531:                 if ($changes{'unauth'}) {
12532:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
12533:                 }
12534:                 my $cachetime = 24*60*60;
12535:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12536:                 if (ref($lastactref) eq 'HASH') {
12537:                     $lastactref->{'domdefaults'} = 1;
12538:                 }
12539:             }
12540:         } else {
12541:             $resulttext = '<span class="LC_error">'.
12542:                           &mt('An error occurred: [_1]',$putresult).'</span>';
12543:         }
12544:     } else {
12545:         $resulttext = &mt('No changes made to course and community categories');
12546:     }
12547:     return $resulttext;
12548: }
12549: 
12550: sub modify_serverstatuses {
12551:     my ($dom,%domconfig) = @_;
12552:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
12553:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
12554:         %currserverstatus = %{$domconfig{'serverstatuses'}};
12555:     }
12556:     my @pages = &serverstatus_pages();
12557:     foreach my $type (@pages) {
12558:         $newserverstatus{$type}{'namedusers'} = '';
12559:         $newserverstatus{$type}{'machines'} = '';
12560:         if (defined($env{'form.'.$type.'_namedusers'})) {
12561:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
12562:             my @okusers;
12563:             foreach my $user (@users) {
12564:                 my ($uname,$udom) = split(/:/,$user);
12565:                 if (($udom =~ /^$match_domain$/) &&   
12566:                     (&Apache::lonnet::domain($udom)) &&
12567:                     ($uname =~ /^$match_username$/)) {
12568:                     if (!grep(/^\Q$user\E/,@okusers)) {
12569:                         push(@okusers,$user);
12570:                     }
12571:                 }
12572:             }
12573:             if (@okusers > 0) {
12574:                  @okusers = sort(@okusers);
12575:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
12576:             }
12577:         }
12578:         if (defined($env{'form.'.$type.'_machines'})) {
12579:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
12580:             my @okmachines;
12581:             foreach my $ip (@machines) {
12582:                 my @parts = split(/\./,$ip);
12583:                 next if (@parts < 4);
12584:                 my $badip = 0;
12585:                 for (my $i=0; $i<4; $i++) {
12586:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
12587:                         $badip = 1;
12588:                         last;
12589:                     }
12590:                 }
12591:                 if (!$badip) {
12592:                     push(@okmachines,$ip);     
12593:                 }
12594:             }
12595:             @okmachines = sort(@okmachines);
12596:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
12597:         }
12598:     }
12599:     my %serverstatushash =  (
12600:                                 serverstatuses => \%newserverstatus,
12601:                             );
12602:     foreach my $type (@pages) {
12603:         foreach my $setting ('namedusers','machines') {
12604:             my (@current,@new);
12605:             if (ref($currserverstatus{$type}) eq 'HASH') {
12606:                 if ($currserverstatus{$type}{$setting} ne '') { 
12607:                     @current = split(/,/,$currserverstatus{$type}{$setting});
12608:                 }
12609:             }
12610:             if ($newserverstatus{$type}{$setting} ne '') {
12611:                 @new = split(/,/,$newserverstatus{$type}{$setting});
12612:             }
12613:             if (@current > 0) {
12614:                 if (@new > 0) {
12615:                     foreach my $item (@current) {
12616:                         if (!grep(/^\Q$item\E$/,@new)) {
12617:                             $changes{$type}{$setting} = 1;
12618:                             last;
12619:                         }
12620:                     }
12621:                     foreach my $item (@new) {
12622:                         if (!grep(/^\Q$item\E$/,@current)) {
12623:                             $changes{$type}{$setting} = 1;
12624:                             last;
12625:                         }
12626:                     }
12627:                 } else {
12628:                     $changes{$type}{$setting} = 1;
12629:                 }
12630:             } elsif (@new > 0) {
12631:                 $changes{$type}{$setting} = 1;
12632:             }
12633:         }
12634:     }
12635:     if (keys(%changes) > 0) {
12636:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
12637:         my $putresult = &Apache::lonnet::put_dom('configuration',
12638:                                                  \%serverstatushash,$dom);
12639:         if ($putresult eq 'ok') {
12640:             $resulttext .= &mt('Changes made:').'<ul>';
12641:             foreach my $type (@pages) {
12642:                 if (ref($changes{$type}) eq 'HASH') {
12643:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
12644:                     if ($changes{$type}{'namedusers'}) {
12645:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
12646:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
12647:                         } else {
12648:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
12649:                         }
12650:                     }
12651:                     if ($changes{$type}{'machines'}) {
12652:                         if ($newserverstatus{$type}{'machines'} eq '') {
12653:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
12654:                         } else {
12655:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
12656:                         }
12657: 
12658:                     }
12659:                     $resulttext .= '</ul></li>';
12660:                 }
12661:             }
12662:             $resulttext .= '</ul>';
12663:         } else {
12664:             $resulttext = '<span class="LC_error">'.
12665:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
12666: 
12667:         }
12668:     } else {
12669:         $resulttext = &mt('No changes made to access to server status pages');
12670:     }
12671:     return $resulttext;
12672: }
12673: 
12674: sub modify_helpsettings {
12675:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
12676:     my ($resulttext,$errors,%changes,%helphash);
12677:     my %defaultchecked = ('submitbugs' => 'on');
12678:     my @offon = ('off','on');
12679:     my @toggles = ('submitbugs');
12680:     my %current = ('submitbugs' => '',
12681:                    'adhoc'      => {},
12682:                   );
12683:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
12684:         %current = %{$domconfig{'helpsettings'}};
12685:     }
12686:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12687:     foreach my $item (@toggles) {
12688:         if ($defaultchecked{$item} eq 'on') { 
12689:             if ($current{$item} eq '') {
12690:                 if ($env{'form.'.$item} eq '0') {
12691:                     $changes{$item} = 1;
12692:                 }
12693:             } elsif ($current{$item} ne $env{'form.'.$item}) {
12694:                 $changes{$item} = 1;
12695:             }
12696:         } elsif ($defaultchecked{$item} eq 'off') {
12697:             if ($current{$item} eq '') {
12698:                 if ($env{'form.'.$item} eq '1') {
12699:                     $changes{$item} = 1;
12700:                 }
12701:             } elsif ($current{$item} ne $env{'form.'.$item}) {
12702:                 $changes{$item} = 1;
12703:             }
12704:         }
12705:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
12706:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
12707:         }
12708:     }
12709:     my $maxnum = $env{'form.helproles_maxnum'};
12710:     my $confname = $dom.'-domainconfig';
12711:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
12712:     my (@allpos,%newsettings,%changedprivs,$newrole);
12713:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12714:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
12715:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
12716:     my %lt = &Apache::lonlocal::texthash(
12717:                     s      => 'system',
12718:                     d      => 'domain',
12719:                     order  => 'Display order',
12720:                     access => 'Role usage',
12721:                     all    => 'All with domain helpdesk or helpdesk assistant role',
12722:                     dh     => 'All with domain helpdesk role',
12723:                     da     => 'All with domain helpdesk assistant role',
12724:                     none   => 'None',
12725:                     status => 'Determined based on institutional status',
12726:                     inc    => 'Include all, but exclude specific personnel',
12727:                     exc    => 'Exclude all, but include specific personnel',
12728:     );
12729:     for (my $num=0; $num<=$maxnum; $num++) {
12730:         my ($prefix,$identifier,$rolename,%curr);
12731:         if ($num == $maxnum) {
12732:             next unless ($env{'form.newcusthelp'} == $maxnum);
12733:             $identifier = 'custhelp'.$num;
12734:             $prefix = 'helproles_'.$num;
12735:             $rolename = $env{'form.custhelpname'.$num};
12736:             $rolename=~s/[^A-Za-z0-9]//gs;
12737:             next if ($rolename eq '');
12738:             next if (exists($existing{'rolesdef_'.$rolename}));
12739:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
12740:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
12741:                                                      $newprivs{'c'},$confname,$dom);
12742:             if ($result ne 'ok') {
12743:                 $errors .= '<li><span class="LC_error">'.
12744:                            &mt('An error occurred storing the new custom role: [_1]',
12745:                            $result).'</span></li>';
12746:                 next;
12747:             } else {
12748:                 $changedprivs{$rolename} = \%newprivs;
12749:                 $newrole = $rolename;
12750:             }
12751:         } else {
12752:             $prefix = 'helproles_'.$num;
12753:             $rolename = $env{'form.'.$prefix};
12754:             next if ($rolename eq '');
12755:             next unless (exists($existing{'rolesdef_'.$rolename}));
12756:             $identifier = 'custhelp'.$num;
12757:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
12758:             my %currprivs;
12759:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
12760:                 split(/\_/,$existing{'rolesdef_'.$rolename});
12761:             foreach my $level ('c','d','s') {
12762:                 if ($newprivs{$level} ne $currprivs{$level}) {
12763:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
12764:                                                              $newprivs{'c'},$confname,$dom);
12765:                     if ($result ne 'ok') {
12766:                         $errors .= '<li><span class="LC_error">'.
12767:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
12768:                                        $rolename,$result).'</span></li>';
12769:                     } else {
12770:                         $changedprivs{$rolename} = \%newprivs;
12771:                     }
12772:                     last;
12773:                 }
12774:             }
12775:             if (ref($current{'adhoc'}) eq 'HASH') {
12776:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
12777:                     %curr = %{$current{'adhoc'}{$rolename}};
12778:                 }
12779:             }
12780:         }
12781:         my $newpos = $env{'form.'.$prefix.'_pos'};
12782:         $newpos =~ s/\D+//g;
12783:         $allpos[$newpos] = $rolename;
12784:         my $newdesc = $env{'form.'.$prefix.'_desc'};
12785:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
12786:         if ($curr{'desc'}) {
12787:             if ($curr{'desc'} ne $newdesc) {
12788:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
12789:                 $newsettings{$rolename}{'desc'} = $newdesc;
12790:             }
12791:         } elsif ($newdesc ne '') {
12792:             $changes{'customrole'}{$rolename}{'desc'} = 1;
12793:             $newsettings{$rolename}{'desc'} = $newdesc;
12794:         }
12795:         my $access = $env{'form.'.$prefix.'_access'};
12796:         if (grep(/^\Q$access\E$/,@accesstypes)) {
12797:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
12798:             if ($access eq 'status') {
12799:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
12800:                 if (scalar(@statuses) == 0) {
12801:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
12802:                 } else {
12803:                     my (@shownstatus,$numtypes);
12804:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
12805:                     if (ref($types) eq 'ARRAY') {
12806:                         $numtypes = scalar(@{$types});
12807:                         foreach my $type (sort(@statuses)) {
12808:                             if ($type eq 'default') {
12809:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
12810:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
12811:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
12812:                                 push(@shownstatus,$usertypes->{$type});
12813:                             }
12814:                         }
12815:                     }
12816:                     if (grep(/^default$/,@statuses)) {
12817:                         push(@shownstatus,$othertitle);
12818:                     }
12819:                     if (scalar(@shownstatus) == 1+$numtypes) {
12820:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
12821:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
12822:                     } else {
12823:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
12824:                         if (ref($curr{'status'}) eq 'ARRAY') {
12825:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
12826:                             if (@diffs) {
12827:                                 $changes{'customrole'}{$rolename}{$access} = 1;
12828:                             }
12829:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
12830:                             $changes{'customrole'}{$rolename}{$access} = 1;
12831:                         }
12832:                     }
12833:                 }
12834:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
12835:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
12836:                 my @newspecstaff;
12837:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
12838:                 foreach my $person (sort(@personnel)) {
12839:                     if ($domhelpdesk{$person}) {
12840:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
12841:                     }
12842:                 }
12843:                 if (ref($curr{$access}) eq 'ARRAY') {
12844:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
12845:                     if (@diffs) {
12846:                         $changes{'customrole'}{$rolename}{$access} = 1;
12847:                     }
12848:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
12849:                     $changes{'customrole'}{$rolename}{$access} = 1;
12850:                 }
12851:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
12852:                     my ($uname,$udom) = split(/:/,$person);
12853:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
12854:                 }
12855:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
12856:             }
12857:         } else {
12858:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
12859:         }
12860:         unless ($curr{'access'} eq $access) {
12861:             $changes{'customrole'}{$rolename}{'access'} = 1;
12862:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
12863:         }
12864:     }
12865:     if (@allpos > 0) {
12866:         my $idx = 0;
12867:         foreach my $rolename (@allpos) {
12868:             if ($rolename ne '') {
12869:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
12870:                 if (ref($current{'adhoc'}) eq 'HASH') {
12871:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
12872:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
12873:                             $changes{'customrole'}{$rolename}{'order'} = 1;
12874:                             $newsettings{$rolename}{'order'} = $idx+1;
12875:                         }
12876:                     }
12877:                 }
12878:                 $idx ++;
12879:             }
12880:         }
12881:     }
12882:     my $putresult;
12883:     if (keys(%changes) > 0) {
12884:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
12885:         if ($putresult eq 'ok') {
12886:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
12887:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
12888:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
12889:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
12890:                 }
12891:             }
12892:             my $cachetime = 24*60*60;
12893:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12894:             if (ref($lastactref) eq 'HASH') {
12895:                 $lastactref->{'domdefaults'} = 1;
12896:             }
12897:         } else {
12898:             $errors .= '<li><span class="LC_error">'.
12899:                        &mt('An error occurred storing the settings: [_1]',
12900:                            $putresult).'</span></li>';
12901:         }
12902:     }
12903:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
12904:         $resulttext = &mt('Changes made:').'<ul>';
12905:         my (%shownprivs,@levelorder);
12906:         @levelorder = ('c','d','s');
12907:         if ((keys(%changes)) && ($putresult eq 'ok')) {
12908:             foreach my $item (sort(keys(%changes))) {
12909:                 if ($item eq 'submitbugs') {
12910:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
12911:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
12912:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
12913:                 } elsif ($item eq 'customrole') {
12914:                     if (ref($changes{'customrole'}) eq 'HASH') {
12915:                         my @keyorder = ('order','desc','access','status','exc','inc');
12916:                         my %keytext = &Apache::lonlocal::texthash(
12917:                                                                    order  => 'Order',
12918:                                                                    desc   => 'Role description',
12919:                                                                    access => 'Role usage',
12920:                                                                    status => 'Allowed institutional types',
12921:                                                                    exc    => 'Allowed personnel',
12922:                                                                    inc    => 'Disallowed personnel',
12923:                         );
12924:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
12925:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
12926:                                 if ($role eq $newrole) {
12927:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
12928:                                                               $role).'<ul>';
12929:                                 } else {
12930:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
12931:                                                               $role).'<ul>';
12932:                                 }
12933:                                 foreach my $key (@keyorder) {
12934:                                     if ($changes{'customrole'}{$role}{$key}) {
12935:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
12936:                                                                   $keytext{$key},$newsettings{$role}{$key}).
12937:                                                        '</li>';
12938:                                     }
12939:                                 }
12940:                                 if (ref($changedprivs{$role}) eq 'HASH') {
12941:                                     $shownprivs{$role} = 1;
12942:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
12943:                                     foreach my $level (@levelorder) {
12944:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
12945:                                             next if ($item eq '');
12946:                                             my ($priv) = split(/\&/,$item,2);
12947:                                             if (&Apache::lonnet::plaintext($priv)) {
12948:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
12949:                                                 unless ($level eq 'c') {
12950:                                                     $resulttext .= ' ('.$lt{$level}.')';
12951:                                                 }
12952:                                                 $resulttext .= '</li>';
12953:                                             }
12954:                                         }
12955:                                     }
12956:                                     $resulttext .= '</ul>';
12957:                                 }
12958:                                 $resulttext .= '</ul></li>';
12959:                             }
12960:                         }
12961:                     }
12962:                 }
12963:             }
12964:         }
12965:         if (keys(%changedprivs)) {
12966:             foreach my $role (sort(keys(%changedprivs))) {
12967:                 unless ($shownprivs{$role}) {
12968:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
12969:                                               $role).'<ul>'.
12970:                                    '<li>'.&mt('Privileges set to :').'<ul>';
12971:                     foreach my $level (@levelorder) {
12972:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
12973:                             next if ($item eq '');
12974:                             my ($priv) = split(/\&/,$item,2);
12975:                             if (&Apache::lonnet::plaintext($priv)) {
12976:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
12977:                                 unless ($level eq 'c') {
12978:                                     $resulttext .= ' ('.$lt{$level}.')';
12979:                                 }
12980:                                 $resulttext .= '</li>';
12981:                             }
12982:                         }
12983:                     }
12984:                     $resulttext .= '</ul></li></ul></li>';
12985:                 }
12986:             }
12987:         }
12988:         $resulttext .= '</ul>';
12989:     } else {
12990:         $resulttext = &mt('No changes made to help settings');
12991:     }
12992:     if ($errors) {
12993:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
12994:                                     $errors.'</ul>';
12995:     }
12996:     return $resulttext;
12997: }
12998: 
12999: sub modify_coursedefaults {
13000:     my ($dom,$lastactref,%domconfig) = @_;
13001:     my ($resulttext,$errors,%changes,%defaultshash);
13002:     my %defaultchecked = (
13003:                            'uselcmath'       => 'on',
13004:                            'usejsme'         => 'on'
13005:                          );
13006:     my @toggles = ('uselcmath','usejsme');
13007:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
13008:                    'uploadquota_community','uploadquota_textbook','mysqltables_official',
13009:                    'mysqltables_unofficial','mysqltables_community','mysqltables_textbook');
13010:     my @types = ('official','unofficial','community','textbook');
13011:     my %staticdefaults = (
13012:                            anonsurvey_threshold => 10,
13013:                            uploadquota          => 500,
13014:                            postsubmit           => 60,
13015:                            mysqltables          => 172800,
13016:                          );
13017: 
13018:     $defaultshash{'coursedefaults'} = {};
13019: 
13020:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
13021:         if ($domconfig{'coursedefaults'} eq '') {
13022:             $domconfig{'coursedefaults'} = {};
13023:         }
13024:     }
13025: 
13026:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
13027:         foreach my $item (@toggles) {
13028:             if ($defaultchecked{$item} eq 'on') {
13029:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
13030:                     ($env{'form.'.$item} eq '0')) {
13031:                     $changes{$item} = 1;
13032:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
13033:                     $changes{$item} = 1;
13034:                 }
13035:             } elsif ($defaultchecked{$item} eq 'off') {
13036:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
13037:                     ($env{'form.'.$item} eq '1')) {
13038:                     $changes{$item} = 1;
13039:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
13040:                     $changes{$item} = 1;
13041:                 }
13042:             }
13043:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
13044:         }
13045:         foreach my $item (@numbers) {
13046:             my ($currdef,$newdef);
13047:             $newdef = $env{'form.'.$item};
13048:             if ($item eq 'anonsurvey_threshold') {
13049:                 $currdef = $domconfig{'coursedefaults'}{$item};
13050:                 $newdef =~ s/\D//g;
13051:                 if ($newdef eq '' || $newdef < 1) {
13052:                     $newdef = 1;
13053:                 }
13054:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
13055:             } else {
13056:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
13057:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
13058:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
13059:                 }
13060:                 $newdef =~ s/[^\w.\-]//g;
13061:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
13062:             }
13063:             if ($currdef ne $newdef) {
13064:                 my $staticdef;
13065:                 if ($item eq 'anonsurvey_threshold') {
13066:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
13067:                         $changes{$item} = 1;
13068:                     }
13069:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
13070:                     my $setting = $1;
13071:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
13072:                         $changes{$setting} = 1;
13073:                     }
13074:                 }
13075:             }
13076:         }
13077:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
13078:         my @currclonecode;
13079:         if (ref($currclone) eq 'HASH') {
13080:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
13081:                 @currclonecode = @{$currclone->{'instcode'}};
13082:             }
13083:         }
13084:         my $newclone;
13085:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
13086:             $newclone = $env{'form.canclone'};
13087:         }
13088:         if ($newclone eq 'instcode') {
13089:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
13090:             my (%codedefaults,@code_order,@clonecode);
13091:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
13092:                                                     \@code_order);
13093:             foreach my $item (@code_order) {
13094:                 if (grep(/^\Q$item\E$/,@newcodes)) {
13095:                     push(@clonecode,$item);
13096:                 }
13097:             }
13098:             if (@clonecode) {
13099:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
13100:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
13101:                 if (@diffs) {
13102:                     $changes{'canclone'} = 1;
13103:                 }
13104:             } else {
13105:                 $newclone eq '';
13106:             }
13107:         } elsif ($newclone ne '') {
13108:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
13109:         }
13110:         if ($newclone ne $currclone) {
13111:             $changes{'canclone'} = 1;
13112:         }
13113:         my %credits;
13114:         foreach my $type (@types) {
13115:             unless ($type eq 'community') {
13116:                 $credits{$type} = $env{'form.'.$type.'_credits'};
13117:                 $credits{$type} =~ s/[^\d.]+//g;
13118:             }
13119:         }
13120:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
13121:             ($env{'form.coursecredits'} eq '1')) {
13122:             $changes{'coursecredits'} = 1;
13123:             foreach my $type (keys(%credits)) {
13124:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
13125:             }
13126:         } else {
13127:             if ($env{'form.coursecredits'} eq '1') {
13128:                 foreach my $type (@types) {
13129:                     unless ($type eq 'community') {
13130:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
13131:                             $changes{'coursecredits'} = 1;
13132:                         }
13133:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
13134:                     }
13135:                 }
13136:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
13137:                 foreach my $type (@types) {
13138:                     unless ($type eq 'community') {
13139:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
13140:                             $changes{'coursecredits'} = 1;
13141:                             last;
13142:                         }
13143:                     }
13144:                 }
13145:             }
13146:         }
13147:         if ($env{'form.postsubmit'} eq '1') {
13148:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
13149:             my %currtimeout;
13150:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13151:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
13152:                     $changes{'postsubmit'} = 1;
13153:                 }
13154:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
13155:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
13156:                 }
13157:             } else {
13158:                 $changes{'postsubmit'} = 1;
13159:             }
13160:             foreach my $type (@types) {
13161:                 my $timeout = $env{'form.'.$type.'_timeout'};
13162:                 $timeout =~ s/\D//g;
13163:                 if ($timeout == $staticdefaults{'postsubmit'}) {
13164:                     $timeout = '';
13165:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
13166:                     $timeout = '0';
13167:                 }
13168:                 unless ($timeout eq '') {
13169:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
13170:                 }
13171:                 if (exists($currtimeout{$type})) {
13172:                     if ($timeout ne $currtimeout{$type}) {
13173:                         $changes{'postsubmit'} = 1;
13174:                     }
13175:                 } elsif ($timeout ne '') {
13176:                     $changes{'postsubmit'} = 1;
13177:                 }
13178:             }
13179:         } else {
13180:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
13181:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13182:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
13183:                     $changes{'postsubmit'} = 1;
13184:                 }
13185:             } else {
13186:                 $changes{'postsubmit'} = 1;
13187:             }
13188:         }
13189:     }
13190:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
13191:                                              $dom);
13192:     if ($putresult eq 'ok') {
13193:         if (keys(%changes) > 0) {
13194:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13195:             if (($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
13196:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
13197:                 ($changes{'canclone'}) || ($changes{'mysqltables'})) {
13198:                 foreach my $item ('uselcmath','usejsme') {
13199:                     if ($changes{$item}) {
13200:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
13201:                     }
13202:                 }
13203:                 if ($changes{'coursecredits'}) {
13204:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
13205:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
13206:                             $domdefaults{$type.'credits'} =
13207:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
13208:                         }
13209:                     }
13210:                 }
13211:                 if ($changes{'postsubmit'}) {
13212:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13213:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
13214:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
13215:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
13216:                                 $domdefaults{$type.'postsubtimeout'} =
13217:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
13218:                             }
13219:                         }
13220:                     }
13221:                 }
13222:                 if ($changes{'uploadquota'}) {
13223:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
13224:                         foreach my $type (@types) {
13225:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
13226:                         }
13227:                     }
13228:                 }
13229:                 if ($changes{'canclone'}) {
13230:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
13231:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
13232:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
13233:                             if (@clonecodes) {
13234:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
13235:                             }
13236:                         }
13237:                     } else {
13238:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
13239:                     }
13240:                 }
13241:                 my $cachetime = 24*60*60;
13242:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13243:                 if (ref($lastactref) eq 'HASH') {
13244:                     $lastactref->{'domdefaults'} = 1;
13245:                 }
13246:             }
13247:             $resulttext = &mt('Changes made:').'<ul>';
13248:             foreach my $item (sort(keys(%changes))) {
13249:                 if ($item eq 'uselcmath') {
13250:                     if ($env{'form.'.$item} eq '1') {
13251:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
13252:                     } else {
13253:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
13254:                     }
13255:                 } elsif ($item eq 'usejsme') {
13256:                     if ($env{'form.'.$item} eq '1') {
13257:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
13258:                     } else {
13259:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
13260:                     }
13261:                 } elsif ($item eq 'anonsurvey_threshold') {
13262:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
13263:                 } elsif ($item eq 'uploadquota') {
13264:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
13265:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
13266:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
13267:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
13268:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
13269: 
13270:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
13271:                                        '</ul>'.
13272:                                        '</li>';
13273:                     } else {
13274:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
13275:                     }
13276:                 } elsif ($item eq 'mysqltables') {
13277:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
13278:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
13279:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
13280:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
13281:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
13282:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
13283:                                        '</ul>'.
13284:                                        '</li>';
13285:                     } else {
13286:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
13287:                     }
13288:                 } elsif ($item eq 'postsubmit') {
13289:                     if ($domdefaults{'postsubmit'} eq 'off') {
13290:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
13291:                     } else {
13292:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
13293:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13294:                             $resulttext .= &mt('durations:').'<ul>';
13295:                             foreach my $type (@types) {
13296:                                 $resulttext .= '<li>';
13297:                                 my $timeout;
13298:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
13299:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
13300:                                 }
13301:                                 my $display;
13302:                                 if ($timeout eq '0') {
13303:                                     $display = &mt('unlimited');
13304:                                 } elsif ($timeout eq '') {
13305:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
13306:                                 } else {
13307:                                     $display = &mt('[quant,_1,second]',$timeout);
13308:                                 }
13309:                                 if ($type eq 'community') {
13310:                                     $resulttext .= &mt('Communities');
13311:                                 } elsif ($type eq 'official') {
13312:                                     $resulttext .= &mt('Official courses');
13313:                                 } elsif ($type eq 'unofficial') {
13314:                                     $resulttext .= &mt('Unofficial courses');
13315:                                 } elsif ($type eq 'textbook') {
13316:                                     $resulttext .= &mt('Textbook courses');
13317:                                 }
13318:                                 $resulttext .= ' -- '.$display.'</li>';
13319:                             }
13320:                             $resulttext .= '</ul>';
13321:                         }
13322:                         $resulttext .= '</li>';
13323:                     }
13324:                 } elsif ($item eq 'coursecredits') {
13325:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
13326:                         if (($domdefaults{'officialcredits'} eq '') &&
13327:                             ($domdefaults{'unofficialcredits'} eq '') &&
13328:                             ($domdefaults{'textbookcredits'} eq '')) {
13329:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
13330:                         } else {
13331:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
13332:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
13333:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
13334:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
13335:                                            '</ul>'.
13336:                                            '</li>';
13337:                         }
13338:                     } else {
13339:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
13340:                     }
13341:                 } elsif ($item eq 'canclone') {
13342:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
13343:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
13344:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
13345:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
13346:                         }
13347:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
13348:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
13349:                     } else {
13350:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
13351:                     }
13352:                 }
13353:             }
13354:             $resulttext .= '</ul>';
13355:         } else {
13356:             $resulttext = &mt('No changes made to course defaults');
13357:         }
13358:     } else {
13359:         $resulttext = '<span class="LC_error">'.
13360:             &mt('An error occurred: [_1]',$putresult).'</span>';
13361:     }
13362:     return $resulttext;
13363: }
13364: 
13365: sub modify_selfenrollment {
13366:     my ($dom,$lastactref,%domconfig) = @_;
13367:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
13368:     my @types = ('official','unofficial','community','textbook');
13369:     my %titles = &tool_titles();
13370:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
13371:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
13372:     $ordered{'default'} = ['types','registered','approval','limit'];
13373: 
13374:     my (%roles,%shown,%toplevel);
13375:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
13376: 
13377:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
13378:         if ($domconfig{'selfenrollment'} eq '') {
13379:             $domconfig{'selfenrollment'} = {};
13380:         }
13381:     }
13382:     %toplevel = (
13383:                   admin      => 'Configuration Rights',
13384:                   default    => 'Default settings',
13385:                   validation => 'Validation of self-enrollment requests',
13386:                 );
13387:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
13388: 
13389:     if (ref($ordered{'admin'}) eq 'ARRAY') {
13390:         foreach my $item (@{$ordered{'admin'}}) {
13391:             foreach my $type (@types) {
13392:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
13393:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
13394:                 } else {
13395:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
13396:                 }
13397:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
13398:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
13399:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
13400:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
13401:                             push(@{$changes{'admin'}{$type}},$item);
13402:                         }
13403:                     } else {
13404:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
13405:                             push(@{$changes{'admin'}{$type}},$item);
13406:                         }
13407:                     }
13408:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
13409:                     push(@{$changes{'admin'}{$type}},$item);
13410:                 }
13411:             }
13412:         }
13413:     }
13414: 
13415:     foreach my $item (@{$ordered{'default'}}) {
13416:         foreach my $type (@types) {
13417:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
13418:             if ($item eq 'types') {
13419:                 unless (($value eq 'all') || ($value eq 'dom')) {
13420:                     $value = '';
13421:                 }
13422:             } elsif ($item eq 'registered') {
13423:                 unless ($value eq '1') {
13424:                     $value = 0;
13425:                 }
13426:             } elsif ($item eq 'approval') {
13427:                 unless ($value =~ /^[012]$/) {
13428:                     $value = 0;
13429:                 }
13430:             } else {
13431:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
13432:                     $value = 'none';
13433:                 }
13434:             }
13435:             $selfenrollhash{'default'}{$type}{$item} = $value;
13436:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
13437:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
13438:                     if ($selfenrollhash{'default'}{$type}{$item} ne
13439:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
13440:                          push(@{$changes{'default'}{$type}},$item);
13441:                     }
13442:                 } else {
13443:                     push(@{$changes{'default'}{$type}},$item);
13444:                 }
13445:             } else {
13446:                 push(@{$changes{'default'}{$type}},$item);
13447:             }
13448:             if ($item eq 'limit') {
13449:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
13450:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
13451:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
13452:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
13453:                     }
13454:                 } else {
13455:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
13456:                 }
13457:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
13458:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
13459:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
13460:                          push(@{$changes{'default'}{$type}},'cap');
13461:                     }
13462:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
13463:                     push(@{$changes{'default'}{$type}},'cap');
13464:                 }
13465:             }
13466:         }
13467:     }
13468: 
13469:     foreach my $item (@{$itemsref}) {
13470:         if ($item eq 'fields') {
13471:             my @changed;
13472:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
13473:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
13474:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
13475:             }
13476:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
13477:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
13478:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
13479:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
13480:                 } else {
13481:                     @changed = @{$selfenrollhash{'validation'}{$item}};
13482:                 }
13483:             } else {
13484:                 @changed = @{$selfenrollhash{'validation'}{$item}};
13485:             }
13486:             if (@changed) {
13487:                 if ($selfenrollhash{'validation'}{$item}) { 
13488:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
13489:                 } else {
13490:                     $changes{'validation'}{$item} = &mt('None');
13491:                 }
13492:             }
13493:         } else {
13494:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
13495:             if ($item eq 'markup') {
13496:                if ($env{'form.selfenroll_validation_'.$item}) {
13497:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
13498:                }
13499:             }
13500:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
13501:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
13502:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
13503:                 }
13504:             }
13505:         }
13506:     }
13507: 
13508:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
13509:                                              $dom);
13510:     if ($putresult eq 'ok') {
13511:         if (keys(%changes) > 0) {
13512:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13513:             $resulttext = &mt('Changes made:').'<ul>';
13514:             foreach my $key ('admin','default','validation') {
13515:                 if (ref($changes{$key}) eq 'HASH') {
13516:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
13517:                     if ($key eq 'validation') {
13518:                         foreach my $item (@{$itemsref}) {
13519:                             if (exists($changes{$key}{$item})) {
13520:                                 if ($item eq 'markup') {
13521:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
13522:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
13523:                                 } else {  
13524:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
13525:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
13526:                                 }
13527:                             }
13528:                         }
13529:                     } else {
13530:                         foreach my $type (@types) {
13531:                             if ($type eq 'community') {
13532:                                 $roles{'1'} = &mt('Community personnel');
13533:                             } else {
13534:                                 $roles{'1'} = &mt('Course personnel');
13535:                             }
13536:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
13537:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
13538:                                     if ($key eq 'admin') {
13539:                                         my @mgrdc = ();
13540:                                         if (ref($ordered{$key}) eq 'ARRAY') {
13541:                                             foreach my $item (@{$ordered{'admin'}}) {
13542:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
13543:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
13544:                                                         push(@mgrdc,$item);
13545:                                                     }
13546:                                                 }
13547:                                             }
13548:                                             if (@mgrdc) {
13549:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
13550:                                             } else {
13551:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
13552:                                             }
13553:                                         }
13554:                                     } else {
13555:                                         if (ref($ordered{$key}) eq 'ARRAY') {
13556:                                             foreach my $item (@{$ordered{$key}}) {
13557:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
13558:                                                     $domdefaults{$type.'selfenroll'.$item} =
13559:                                                         $selfenrollhash{$key}{$type}{$item};
13560:                                                 }
13561:                                             }
13562:                                         }
13563:                                     }
13564:                                 }
13565:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
13566:                                 foreach my $item (@{$ordered{$key}}) {
13567:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
13568:                                         $resulttext .= '<li>';
13569:                                         if ($key eq 'admin') {
13570:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
13571:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
13572:                                         } else {
13573:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
13574:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
13575:                                         }
13576:                                         $resulttext .= '</li>';
13577:                                     }
13578:                                 }
13579:                                 $resulttext .= '</ul></li>';
13580:                             }
13581:                         }
13582:                         $resulttext .= '</ul></li>'; 
13583:                     }
13584:                 }
13585:                 if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
13586:                     my $cachetime = 24*60*60;
13587:                     &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13588:                     if (ref($lastactref) eq 'HASH') {
13589:                         $lastactref->{'domdefaults'} = 1;
13590:                     }
13591:                 }
13592:             }
13593:             $resulttext .= '</ul>';
13594:         } else {
13595:             $resulttext = &mt('No changes made to self-enrollment settings');
13596:         }
13597:     } else {
13598:         $resulttext = '<span class="LC_error">'.
13599:             &mt('An error occurred: [_1]',$putresult).'</span>';
13600:     }
13601:     return $resulttext;
13602: }
13603: 
13604: sub modify_usersessions {
13605:     my ($dom,$lastactref,%domconfig) = @_;
13606:     my @hostingtypes = ('version','excludedomain','includedomain');
13607:     my @offloadtypes = ('primary','default');
13608:     my %types = (
13609:                   remote => \@hostingtypes,
13610:                   hosted => \@hostingtypes,
13611:                   spares => \@offloadtypes,
13612:                 );
13613:     my @prefixes = ('remote','hosted','spares');
13614:     my @lcversions = &Apache::lonnet::all_loncaparevs();
13615:     my (%by_ip,%by_location,@intdoms);
13616:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
13617:     my @locations = sort(keys(%by_location));
13618:     my (%defaultshash,%changes);
13619:     foreach my $prefix (@prefixes) {
13620:         $defaultshash{'usersessions'}{$prefix} = {};
13621:     }
13622:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13623:     my $resulttext;
13624:     my %iphost = &Apache::lonnet::get_iphost();
13625:     foreach my $prefix (@prefixes) {
13626:         next if ($prefix eq 'spares');
13627:         foreach my $type (@{$types{$prefix}}) {
13628:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
13629:             if ($type eq 'version') {
13630:                 my $value = $env{'form.'.$prefix.'_'.$type};
13631:                 my $okvalue;
13632:                 if ($value ne '') {
13633:                     if (grep(/^\Q$value\E$/,@lcversions)) {
13634:                         $okvalue = $value;
13635:                     }
13636:                 }
13637:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
13638:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
13639:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
13640:                             if ($inuse == 0) {
13641:                                 $changes{$prefix}{$type} = 1;
13642:                             } else {
13643:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
13644:                                     $changes{$prefix}{$type} = 1;
13645:                                 }
13646:                                 if ($okvalue ne '') {
13647:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13648:                                 } 
13649:                             }
13650:                         } else {
13651:                             if (($inuse == 1) && ($okvalue ne '')) {
13652:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13653:                                 $changes{$prefix}{$type} = 1;
13654:                             }
13655:                         }
13656:                     } else {
13657:                         if (($inuse == 1) && ($okvalue ne '')) {
13658:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13659:                             $changes{$prefix}{$type} = 1;
13660:                         }
13661:                     }
13662:                 } else {
13663:                     if (($inuse == 1) && ($okvalue ne '')) {
13664:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13665:                         $changes{$prefix}{$type} = 1;
13666:                     }
13667:                 }
13668:             } else {
13669:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
13670:                 my @okvals;
13671:                 foreach my $val (@vals) {
13672:                     if ($val =~ /:/) {
13673:                         my @items = split(/:/,$val);
13674:                         foreach my $item (@items) {
13675:                             if (ref($by_location{$item}) eq 'ARRAY') {
13676:                                 push(@okvals,$item);
13677:                             }
13678:                         }
13679:                     } else {
13680:                         if (ref($by_location{$val}) eq 'ARRAY') {
13681:                             push(@okvals,$val);
13682:                         }
13683:                     }
13684:                 }
13685:                 @okvals = sort(@okvals);
13686:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
13687:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
13688:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
13689:                             if ($inuse == 0) {
13690:                                 $changes{$prefix}{$type} = 1; 
13691:                             } else {
13692:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13693:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
13694:                                 if (@changed > 0) {
13695:                                     $changes{$prefix}{$type} = 1;
13696:                                 }
13697:                             }
13698:                         } else {
13699:                             if ($inuse == 1) {
13700:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13701:                                 $changes{$prefix}{$type} = 1;
13702:                             }
13703:                         } 
13704:                     } else {
13705:                         if ($inuse == 1) {
13706:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13707:                             $changes{$prefix}{$type} = 1;
13708:                         }
13709:                     }
13710:                 } else {
13711:                     if ($inuse == 1) {
13712:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13713:                         $changes{$prefix}{$type} = 1;
13714:                     }
13715:                 }
13716:             }
13717:         }
13718:     }
13719: 
13720:     my @alldoms = &Apache::lonnet::all_domains();
13721:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
13722:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
13723:     my $savespares;
13724: 
13725:     foreach my $lonhost (sort(keys(%servers))) {
13726:         my $serverhomeID =
13727:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
13728:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
13729:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
13730:         my %spareschg;
13731:         foreach my $type (@{$types{'spares'}}) {
13732:             my @okspares;
13733:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
13734:             foreach my $server (@checked) {
13735:                 if (&Apache::lonnet::hostname($server) ne '') {
13736:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
13737:                         unless (grep(/^\Q$server\E$/,@okspares)) {
13738:                             push(@okspares,$server);
13739:                         }
13740:                     }
13741:                 }
13742:             }
13743:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
13744:             my $newspare;
13745:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
13746:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
13747:                     $newspare = $new;
13748:                 }
13749:             }
13750:             my @spares;
13751:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
13752:                 @spares = sort(@okspares,$newspare);
13753:             } else {
13754:                 @spares = sort(@okspares);
13755:             }
13756:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
13757:             if (ref($spareid{$lonhost}) eq 'HASH') {
13758:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
13759:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
13760:                     if (@diffs > 0) {
13761:                         $spareschg{$type} = 1;
13762:                     }
13763:                 }
13764:             }
13765:         }
13766:         if (keys(%spareschg) > 0) {
13767:             $changes{'spares'}{$lonhost} = \%spareschg;
13768:         }
13769:     }
13770:     $defaultshash{'usersessions'}{'offloadnow'} = {};
13771:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
13772:     my @okoffload;
13773:     if (@offloadnow) {
13774:         foreach my $server (@offloadnow) {
13775:             if (&Apache::lonnet::hostname($server) ne '') {
13776:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
13777:                     push(@okoffload,$server);
13778:                 }
13779:             }
13780:         }
13781:         if (@okoffload) {
13782:             foreach my $lonhost (@okoffload) {
13783:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
13784:             }
13785:         }
13786:     }
13787:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
13788:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
13789:             if (ref($changes{'spares'}) eq 'HASH') {
13790:                 if (keys(%{$changes{'spares'}}) > 0) {
13791:                     $savespares = 1;
13792:                 }
13793:             }
13794:         } else {
13795:             $savespares = 1;
13796:         }
13797:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
13798:             foreach my $lonhost (keys(%{$domconfig{'usersessions'}{'offloadnow'}})) {
13799:                 unless ($defaultshash{'usersessions'}{'offloadnow'}{$lonhost}) {
13800:                     $changes{'offloadnow'} = 1;
13801:                     last;
13802:                 }
13803:             }
13804:             unless ($changes{'offloadnow'}) {
13805:                 foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{'offloadnow'}})) {
13806:                     unless ($domconfig{'usersessions'}{'offloadnow'}{$lonhost}) {
13807:                         $changes{'offloadnow'} = 1;
13808:                         last;
13809:                     }
13810:                 }
13811:             }
13812:         } elsif (@okoffload) {
13813:             $changes{'offloadnow'} = 1;
13814:         }
13815:     } elsif (@okoffload) {
13816:         $changes{'offloadnow'} = 1;
13817:     }
13818:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
13819:     if ((keys(%changes) > 0) || ($savespares)) {
13820:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
13821:                                                  $dom);
13822:         if ($putresult eq 'ok') {
13823:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
13824:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
13825:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
13826:                 }
13827:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
13828:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
13829:                 }
13830:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
13831:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
13832:                 }
13833:             }
13834:             my $cachetime = 24*60*60;
13835:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13836:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
13837:             if (ref($lastactref) eq 'HASH') {
13838:                 $lastactref->{'domdefaults'} = 1;
13839:                 $lastactref->{'usersessions'} = 1;
13840:             }
13841:             if (keys(%changes) > 0) {
13842:                 my %lt = &usersession_titles();
13843:                 $resulttext = &mt('Changes made:').'<ul>';
13844:                 foreach my $prefix (@prefixes) {
13845:                     if (ref($changes{$prefix}) eq 'HASH') {
13846:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
13847:                         if ($prefix eq 'spares') {
13848:                             if (ref($changes{$prefix}) eq 'HASH') {
13849:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
13850:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
13851:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
13852:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
13853:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
13854:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
13855:                                         foreach my $type (@{$types{$prefix}}) {
13856:                                             if ($changes{$prefix}{$lonhost}{$type}) {
13857:                                                 my $offloadto = &mt('None');
13858:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
13859:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
13860:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
13861:                                                     }
13862:                                                 }
13863:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
13864:                                             }
13865:                                         }
13866:                                     }
13867:                                     $resulttext .= '</li>';
13868:                                 }
13869:                             }
13870:                         } else {
13871:                             foreach my $type (@{$types{$prefix}}) {
13872:                                 if (defined($changes{$prefix}{$type})) {
13873:                                     my $newvalue;
13874:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
13875:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
13876:                                             if ($type eq 'version') {
13877:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
13878:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
13879:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
13880:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
13881:                                                 }
13882:                                             }
13883:                                         }
13884:                                     }
13885:                                     if ($newvalue eq '') {
13886:                                         if ($type eq 'version') {
13887:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
13888:                                         } else {
13889:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
13890:                                         }
13891:                                     } else {
13892:                                         if ($type eq 'version') {
13893:                                             $newvalue .= ' '.&mt('(or later)'); 
13894:                                         }
13895:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
13896:                                     }
13897:                                 }
13898:                             }
13899:                         }
13900:                         $resulttext .= '</ul>';
13901:                     }
13902:                 }
13903:                 if ($changes{'offloadnow'}) {
13904:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
13905:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
13906:                             $resulttext .= '<li>'.&mt('Switch active users on next access, for server(s):').'<ul>';
13907:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
13908:                                 $resulttext .= '<li>'.$lonhost.'</li>';
13909:                             }
13910:                             $resulttext .= '</ul>';
13911:                         } else {
13912:                             $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.');
13913:                         }
13914:                     } else {
13915:                         $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.').'</li>';
13916:                     }
13917:                 }
13918:                 $resulttext .= '</ul>';
13919:             } else {
13920:                 $resulttext = $nochgmsg;
13921:             }
13922:         } else {
13923:             $resulttext = '<span class="LC_error">'.
13924:                           &mt('An error occurred: [_1]',$putresult).'</span>';
13925:         }
13926:     } else {
13927:         $resulttext = $nochgmsg;
13928:     }
13929:     return $resulttext;
13930: }
13931: 
13932: sub modify_loadbalancing {
13933:     my ($dom,%domconfig) = @_;
13934:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
13935:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
13936:     my ($othertitle,$usertypes,$types) =
13937:         &Apache::loncommon::sorted_inst_types($dom);
13938:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
13939:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
13940:     my @sparestypes = ('primary','default');
13941:     my %typetitles = &sparestype_titles();
13942:     my $resulttext;
13943:     my (%currbalancer,%currtargets,%currrules,%existing);
13944:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
13945:         %existing = %{$domconfig{'loadbalancing'}};
13946:     }
13947:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
13948:                               \%currtargets,\%currrules);
13949:     my ($saveloadbalancing,%defaultshash,%changes);
13950:     my ($alltypes,$othertypes,$titles) =
13951:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
13952:     my %ruletitles = &offloadtype_text();
13953:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
13954:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
13955:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
13956:         if ($balancer eq '') {
13957:             next;
13958:         }
13959:         if (!exists($servers{$balancer})) {
13960:             if (exists($currbalancer{$balancer})) {
13961:                 push(@{$changes{'delete'}},$balancer);
13962:             }
13963:             next;
13964:         }
13965:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
13966:             push(@{$changes{'delete'}},$balancer);
13967:             next;
13968:         }
13969:         if (!exists($currbalancer{$balancer})) {
13970:             push(@{$changes{'add'}},$balancer);
13971:         }
13972:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
13973:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
13974:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
13975:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
13976:             $saveloadbalancing = 1;
13977:         }
13978:         foreach my $sparetype (@sparestypes) {
13979:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
13980:             my @offloadto;
13981:             foreach my $target (@targets) {
13982:                 if (($servers{$target}) && ($target ne $balancer)) {
13983:                     if ($sparetype eq 'default') {
13984:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
13985:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
13986:                         }
13987:                     }
13988:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
13989:                         push(@offloadto,$target);
13990:                     }
13991:                 }
13992:             }
13993:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
13994:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
13995:                     push(@offloadto,$balancer);
13996:                 }
13997:             }
13998:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
13999:         }
14000:         if (ref($currtargets{$balancer}) eq 'HASH') {
14001:             foreach my $sparetype (@sparestypes) {
14002:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
14003:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
14004:                     if (@targetdiffs > 0) {
14005:                         $changes{'curr'}{$balancer}{'targets'} = 1;
14006:                     }
14007:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14008:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14009:                         $changes{'curr'}{$balancer}{'targets'} = 1;
14010:                     }
14011:                 }
14012:             }
14013:         } else {
14014:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
14015:                 foreach my $sparetype (@sparestypes) {
14016:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14017:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14018:                             $changes{'curr'}{$balancer}{'targets'} = 1;
14019:                         }
14020:                     }
14021:                 }
14022:             }
14023:         }
14024:         my $ishomedom;
14025:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
14026:             $ishomedom = 1;
14027:         }
14028:         if (ref($alltypes) eq 'ARRAY') {
14029:             foreach my $type (@{$alltypes}) {
14030:                 my $rule;
14031:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
14032:                          (!$ishomedom)) {
14033:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
14034:                 }
14035:                 if ($rule eq 'specific') {
14036:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
14037:                     if (exists($servers{$specifiedhost})) {
14038:                         $rule = $specifiedhost;
14039:                     }
14040:                 }
14041:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
14042:                 if (ref($currrules{$balancer}) eq 'HASH') {
14043:                     if ($rule ne $currrules{$balancer}{$type}) {
14044:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
14045:                     }
14046:                 } elsif ($rule ne '') {
14047:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
14048:                 }
14049:             }
14050:         }
14051:     }
14052:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
14053:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
14054:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
14055:             $defaultshash{'loadbalancing'} = {};
14056:         }
14057:         my $putresult = &Apache::lonnet::put_dom('configuration',
14058:                                                  \%defaultshash,$dom);
14059:         if ($putresult eq 'ok') {
14060:             if (keys(%changes) > 0) {
14061:                 my %toupdate;
14062:                 if (ref($changes{'delete'}) eq 'ARRAY') {
14063:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
14064:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
14065:                         $toupdate{$balancer} = 1;
14066:                     }
14067:                 }
14068:                 if (ref($changes{'add'}) eq 'ARRAY') {
14069:                     foreach my $balancer (sort(@{$changes{'add'}})) {
14070:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
14071:                         $toupdate{$balancer} = 1;
14072:                     }
14073:                 }
14074:                 if (ref($changes{'curr'}) eq 'HASH') {
14075:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
14076:                         $toupdate{$balancer} = 1;
14077:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
14078:                             if ($changes{'curr'}{$balancer}{'targets'}) {
14079:                                 my %offloadstr;
14080:                                 foreach my $sparetype (@sparestypes) {
14081:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14082:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14083:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
14084:                                         }
14085:                                     }
14086:                                 }
14087:                                 if (keys(%offloadstr) == 0) {
14088:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
14089:                                 } else {
14090:                                     my $showoffload;
14091:                                     foreach my $sparetype (@sparestypes) {
14092:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
14093:                                         if (defined($offloadstr{$sparetype})) {
14094:                                             $showoffload .= $offloadstr{$sparetype};
14095:                                         } else {
14096:                                             $showoffload .= &mt('None');
14097:                                         }
14098:                                         $showoffload .= ('&nbsp;'x3);
14099:                                     }
14100:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
14101:                                 }
14102:                             }
14103:                         }
14104:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
14105:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
14106:                                 foreach my $type (@{$alltypes}) {
14107:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
14108:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
14109:                                         my $balancetext;
14110:                                         if ($rule eq '') {
14111:                                             $balancetext =  $ruletitles{'default'};
14112:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
14113:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
14114:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
14115:                                                 foreach my $sparetype (@sparestypes) {
14116:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14117:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
14118:                                                     }
14119:                                                 }
14120:                                                 foreach my $item (@{$alltypes}) {
14121:                                                     next if ($item =~  /^_LC_ipchange/);
14122:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
14123:                                                     if ($hasrule eq 'homeserver') {
14124:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
14125:                                                     } else {
14126:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
14127:                                                             if ($servers{$hasrule}) {
14128:                                                                 $toupdate{$hasrule} = 1;
14129:                                                             }
14130:                                                         }
14131:                                                     }
14132:                                                 }
14133:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
14134:                                                     $balancetext =  $ruletitles{$rule};
14135:                                                 } else {
14136:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
14137:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
14138:                                                     if ($receiver) {
14139:                                                         $toupdate{$receiver};
14140:                                                     }
14141:                                                 }
14142:                                             } else {
14143:                                                 $balancetext =  $ruletitles{$rule};
14144:                                             }
14145:                                         } else {
14146:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
14147:                                         }
14148:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
14149:                                     }
14150:                                 }
14151:                             }
14152:                         }
14153:                         if (keys(%toupdate)) {
14154:                             my %thismachine;
14155:                             my $updatedhere;
14156:                             my $cachetime = 60*60*24;
14157:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
14158:                             foreach my $lonhost (keys(%toupdate)) {
14159:                                 if ($thismachine{$lonhost}) {
14160:                                     unless ($updatedhere) {
14161:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
14162:                                                                       $defaultshash{'loadbalancing'},
14163:                                                                       $cachetime);
14164:                                         $updatedhere = 1;
14165:                                     }
14166:                                 } else {
14167:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
14168:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
14169:                                 }
14170:                             }
14171:                         }
14172:                     }
14173:                 }
14174:                 if ($resulttext ne '') {
14175:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
14176:                 } else {
14177:                     $resulttext = $nochgmsg;
14178:                 }
14179:             } else {
14180:                 $resulttext = $nochgmsg;
14181:             }
14182:         } else {
14183:             $resulttext = '<span class="LC_error">'.
14184:                           &mt('An error occurred: [_1]',$putresult).'</span>';
14185:         }
14186:     } else {
14187:         $resulttext = $nochgmsg;
14188:     }
14189:     return $resulttext;
14190: }
14191: 
14192: sub recurse_check {
14193:     my ($chkcats,$categories,$depth,$name) = @_;
14194:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
14195:         my $chg = 0;
14196:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
14197:             my $category = $chkcats->[$depth]{$name}[$j];
14198:             my $item;
14199:             if ($category eq '') {
14200:                 $chg ++;
14201:             } else {
14202:                 my $deeper = $depth + 1;
14203:                 $item = &escape($category).':'.&escape($name).':'.$depth;
14204:                 if ($chg) {
14205:                     $categories->{$item} -= $chg;
14206:                 }
14207:                 &recurse_check($chkcats,$categories,$deeper,$category);
14208:                 $deeper --;
14209:             }
14210:         }
14211:     }
14212:     return;
14213: }
14214: 
14215: sub recurse_cat_deletes {
14216:     my ($item,$coursecategories,$deletions) = @_;
14217:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
14218:     my $subdepth = $depth + 1;
14219:     if (ref($coursecategories) eq 'HASH') {
14220:         foreach my $subitem (keys(%{$coursecategories})) {
14221:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
14222:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
14223:                 delete($coursecategories->{$subitem});
14224:                 $deletions->{$subitem} = 1;
14225:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
14226:             }
14227:         }
14228:     }
14229:     return;
14230: }
14231: 
14232: sub active_dc_picker {
14233:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
14234:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
14235:     my @domcoord = keys(%domcoords);
14236:     if (keys(%currhash)) {
14237:         foreach my $dc (keys(%currhash)) {
14238:             unless (exists($domcoords{$dc})) {
14239:                 push(@domcoord,$dc);
14240:             }
14241:         }
14242:     }
14243:     @domcoord = sort(@domcoord);
14244:     my $numdcs = scalar(@domcoord);
14245:     my $rows = 0;
14246:     my $table;
14247:     if ($numdcs > 1) {
14248:         $table = '<table>';
14249:         for (my $i=0; $i<@domcoord; $i++) {
14250:             my $rem = $i%($numinrow);
14251:             if ($rem == 0) {
14252:                 if ($i > 0) {
14253:                     $table .= '</tr>';
14254:                 }
14255:                 $table .= '<tr>';
14256:                 $rows ++;
14257:             }
14258:             my $check = '';
14259:             if ($inputtype eq 'radio') {
14260:                 if (keys(%currhash) == 0) {
14261:                     if (!$i) {
14262:                         $check = ' checked="checked"';
14263:                     }
14264:                 } elsif (exists($currhash{$domcoord[$i]})) {
14265:                     $check = ' checked="checked"';
14266:                 }
14267:             } else {
14268:                 if (exists($currhash{$domcoord[$i]})) {
14269:                     $check = ' checked="checked"';
14270:                 }
14271:             }
14272:             if ($i == @domcoord - 1) {
14273:                 my $colsleft = $numinrow - $rem;
14274:                 if ($colsleft > 1) {
14275:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
14276:                 } else {
14277:                     $table .= '<td class="LC_left_item">';
14278:                 }
14279:             } else {
14280:                 $table .= '<td class="LC_left_item">';
14281:             }
14282:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
14283:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
14284:             $table .= '<span class="LC_nobreak"><label>'.
14285:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
14286:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
14287:             if ($user ne $dcname.':'.$dcdom) {
14288:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
14289:             }
14290:             $table .= '</label></span></td>';
14291:         }
14292:         $table .= '</tr></table>';
14293:     } elsif ($numdcs == 1) {
14294:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
14295:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
14296:         if ($inputtype eq 'radio') {
14297:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
14298:             if ($user ne $dcname.':'.$dcdom) {
14299:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
14300:             }
14301:         } else {
14302:             my $check;
14303:             if (exists($currhash{$domcoord[0]})) {
14304:                 $check = ' checked="checked"';
14305:             }
14306:             $table = '<span class="LC_nobreak"><label>'.
14307:                      '<input type="checkbox" name="'.$name.'" '.
14308:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
14309:             if ($user ne $dcname.':'.$dcdom) {
14310:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
14311:             }
14312:             $table .= '</label></span>';
14313:             $rows ++;
14314:         }
14315:     }
14316:     return ($numdcs,$table,$rows);
14317: }
14318: 
14319: sub usersession_titles {
14320:     return &Apache::lonlocal::texthash(
14321:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
14322:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
14323:                spares => 'Servers offloaded to, when busy',
14324:                version => 'LON-CAPA version requirement',
14325:                excludedomain => 'Allow all, but exclude specific domains',
14326:                includedomain => 'Deny all, but include specific domains',
14327:                primary => 'Primary (checked first)',
14328:                default => 'Default',
14329:            );
14330: }
14331: 
14332: sub id_for_thisdom {
14333:     my (%servers) = @_;
14334:     my %altids;
14335:     foreach my $server (keys(%servers)) {
14336:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
14337:         if ($serverhome ne $server) {
14338:             $altids{$serverhome} = $server;
14339:         }
14340:     }
14341:     return %altids;
14342: }
14343: 
14344: sub count_servers {
14345:     my ($currbalancer,%servers) = @_;
14346:     my (@spares,$numspares);
14347:     foreach my $lonhost (sort(keys(%servers))) {
14348:         next if ($currbalancer eq $lonhost);
14349:         push(@spares,$lonhost);
14350:     }
14351:     if ($currbalancer) {
14352:         $numspares = scalar(@spares);
14353:     } else {
14354:         $numspares = scalar(@spares) - 1;
14355:     }
14356:     return ($numspares,@spares);
14357: }
14358: 
14359: sub lonbalance_targets_js {
14360:     my ($dom,$types,$servers,$settings) = @_;
14361:     my $select = &mt('Select');
14362:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
14363:     if (ref($servers) eq 'HASH') {
14364:         $alltargets = join("','",sort(keys(%{$servers})));
14365:         my @homedoms;
14366:         foreach my $server (sort(keys(%{$servers}))) {
14367:             if (&Apache::lonnet::host_domain($server) eq $dom) {
14368:                 push(@homedoms,'1');
14369:             } else {
14370:                 push(@homedoms,'0');
14371:             }
14372:         }
14373:         $allishome = join("','",@homedoms);
14374:     }
14375:     if (ref($types) eq 'ARRAY') {
14376:         if (@{$types} > 0) {
14377:             @alltypes = @{$types};
14378:         }
14379:     }
14380:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
14381:     $allinsttypes = join("','",@alltypes);
14382:     my (%currbalancer,%currtargets,%currrules,%existing);
14383:     if (ref($settings) eq 'HASH') {
14384:         %existing = %{$settings};
14385:     }
14386:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
14387:                               \%currtargets,\%currrules);
14388:     my $balancers = join("','",sort(keys(%currbalancer)));
14389:     return <<"END";
14390: 
14391: <script type="text/javascript">
14392: // <![CDATA[
14393: 
14394: currBalancers = new Array('$balancers');
14395: 
14396: function toggleTargets(balnum) {
14397:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
14398:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
14399:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
14400:     var prevbalancer = prevhostitem.value;
14401:     var baltotal = document.getElementById('loadbalancing_total').value;
14402:     prevhostitem.value = balancer;
14403:     if (prevbalancer != '') {
14404:         var prevIdx = currBalancers.indexOf(prevbalancer);
14405:         if (prevIdx != -1) {
14406:             currBalancers.splice(prevIdx,1);
14407:         }
14408:     }
14409:     if (balancer == '') {
14410:         hideSpares(balnum);
14411:     } else {
14412:         var currIdx = currBalancers.indexOf(balancer);
14413:         if (currIdx == -1) {
14414:             currBalancers.push(balancer);
14415:         }
14416:         var homedoms = new Array('$allishome');
14417:         var ishomedom = homedoms[lonhostitem.selectedIndex];
14418:         showSpares(balancer,ishomedom,balnum);
14419:     }
14420:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
14421:     return;
14422: }
14423: 
14424: function showSpares(balancer,ishomedom,balnum) {
14425:     var alltargets = new Array('$alltargets');
14426:     var insttypes = new Array('$allinsttypes');
14427:     var offloadtypes = new Array('primary','default');
14428: 
14429:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
14430:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
14431:  
14432:     for (var i=0; i<offloadtypes.length; i++) {
14433:         var count = 0;
14434:         for (var j=0; j<alltargets.length; j++) {
14435:             if (alltargets[j] != balancer) {
14436:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
14437:                 item.value = alltargets[j];
14438:                 item.style.textAlign='left';
14439:                 item.style.textFace='normal';
14440:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
14441:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
14442:                     item.disabled = '';
14443:                 } else {
14444:                     item.disabled = 'disabled';
14445:                     item.checked = false;
14446:                 }
14447:                 count ++;
14448:             }
14449:         }
14450:     }
14451:     for (var k=0; k<insttypes.length; k++) {
14452:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
14453:             if (ishomedom == 1) {
14454:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
14455:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
14456:             } else {
14457:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
14458:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
14459:             }
14460:         } else {
14461:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
14462:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
14463:         }
14464:         if ((insttypes[k] != '_LC_external') && 
14465:             ((insttypes[k] != '_LC_internetdom') ||
14466:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
14467:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
14468:             item.options.length = 0;
14469:             item.options[0] = new Option("","",true,true);
14470:             var idx = 0;
14471:             for (var m=0; m<alltargets.length; m++) {
14472:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
14473:                     idx ++;
14474:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
14475:                 }
14476:             }
14477:         }
14478:     }
14479:     return;
14480: }
14481: 
14482: function hideSpares(balnum) {
14483:     var alltargets = new Array('$alltargets');
14484:     var insttypes = new Array('$allinsttypes');
14485:     var offloadtypes = new Array('primary','default');
14486: 
14487:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
14488:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
14489: 
14490:     var total = alltargets.length - 1;
14491:     for (var i=0; i<offloadtypes; i++) {
14492:         for (var j=0; j<total; j++) {
14493:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
14494:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
14495:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
14496:         }
14497:     }
14498:     for (var k=0; k<insttypes.length; k++) {
14499:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
14500:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
14501:         if (insttypes[k] != '_LC_external') {
14502:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
14503:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
14504:         }
14505:     }
14506:     return;
14507: }
14508: 
14509: function checkOffloads(item,balnum,type) {
14510:     var alltargets = new Array('$alltargets');
14511:     var offloadtypes = new Array('primary','default');
14512:     if (item.checked) {
14513:         var total = alltargets.length - 1;
14514:         var other;
14515:         if (type == offloadtypes[0]) {
14516:             other = offloadtypes[1];
14517:         } else {
14518:             other = offloadtypes[0];
14519:         }
14520:         for (var i=0; i<total; i++) {
14521:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
14522:             if (server == item.value) {
14523:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
14524:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
14525:                 }
14526:             }
14527:         }
14528:     }
14529:     return;
14530: }
14531: 
14532: function singleServerToggle(balnum,type) {
14533:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
14534:     if (offloadtoSelIdx == 0) {
14535:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
14536:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
14537: 
14538:     } else {
14539:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
14540:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
14541:     }
14542:     return;
14543: }
14544: 
14545: function balanceruleChange(formname,balnum,type) {
14546:     if (type == '_LC_external') {
14547:         return;
14548:     }
14549:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
14550:     for (var i=0; i<typesRules.length; i++) {
14551:         if (formname.elements[typesRules[i]].checked) {
14552:             if (formname.elements[typesRules[i]].value != 'specific') {
14553:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
14554:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
14555:             } else {
14556:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
14557:             }
14558:         }
14559:     }
14560:     return;
14561: }
14562: 
14563: function balancerDeleteChange(balnum) {
14564:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
14565:     var baltotal = document.getElementById('loadbalancing_total').value;
14566:     var addtarget;
14567:     var removetarget;
14568:     var action = 'delete';
14569:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
14570:         var lonhost = hostitem.value;
14571:         var currIdx = currBalancers.indexOf(lonhost);
14572:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
14573:             if (currIdx != -1) {
14574:                 currBalancers.splice(currIdx,1);
14575:             }
14576:             addtarget = lonhost;
14577:         } else {
14578:             if (currIdx == -1) {
14579:                 currBalancers.push(lonhost);
14580:             }
14581:             removetarget = lonhost;
14582:             action = 'undelete';
14583:         }
14584:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
14585:     }
14586:     return;
14587: }
14588: 
14589: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
14590:     if (baltotal > 1) {
14591:         var offloadtypes = new Array('primary','default');
14592:         var alltargets = new Array('$alltargets');
14593:         var insttypes = new Array('$allinsttypes');
14594:         for (var i=0; i<baltotal; i++) {
14595:             if (i != balnum) {
14596:                 for (var j=0; j<offloadtypes.length; j++) {
14597:                     var total = alltargets.length - 1;
14598:                     for (var k=0; k<total; k++) {
14599:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
14600:                         var server = serveritem.value;
14601:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
14602:                             if (server == addtarget) {
14603:                                 serveritem.disabled = '';
14604:                             }
14605:                         }
14606:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
14607:                             if (server == removetarget) {
14608:                                 serveritem.disabled = 'disabled';
14609:                                 serveritem.checked = false;
14610:                             }
14611:                         }
14612:                     }
14613:                 }
14614:                 for (var j=0; j<insttypes.length; j++) {
14615:                     if (insttypes[j] != '_LC_external') {
14616:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
14617:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
14618:                             var currSel = singleserver.selectedIndex;
14619:                             var currVal = singleserver.options[currSel].value;
14620:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
14621:                                 var numoptions = singleserver.options.length;
14622:                                 var needsnew = 1;
14623:                                 for (var k=0; k<numoptions; k++) {
14624:                                     if (singleserver.options[k] == addtarget) {
14625:                                         needsnew = 0;
14626:                                         break;
14627:                                     }
14628:                                 }
14629:                                 if (needsnew == 1) {
14630:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
14631:                                 }
14632:                             }
14633:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
14634:                                 singleserver.options.length = 0;
14635:                                 if ((currVal) && (currVal != removetarget)) {
14636:                                     singleserver.options[0] = new Option("","",false,false);
14637:                                 } else {
14638:                                     singleserver.options[0] = new Option("","",true,true);
14639:                                 }
14640:                                 var idx = 0;
14641:                                 for (var m=0; m<alltargets.length; m++) {
14642:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
14643:                                         idx ++;
14644:                                         if (currVal == alltargets[m]) {
14645:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
14646:                                         } else {
14647:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
14648:                                         }
14649:                                     }
14650:                                 }
14651:                             }
14652:                         }
14653:                     }
14654:                 }
14655:             }
14656:         }
14657:     }
14658:     return;
14659: }
14660: 
14661: // ]]>
14662: </script>
14663: 
14664: END
14665: }
14666: 
14667: sub new_spares_js {
14668:     my @sparestypes = ('primary','default');
14669:     my $types = join("','",@sparestypes);
14670:     my $select = &mt('Select');
14671:     return <<"END";
14672: 
14673: <script type="text/javascript">
14674: // <![CDATA[
14675: 
14676: function updateNewSpares(formname,lonhost) {
14677:     var types = new Array('$types');
14678:     var include = new Array();
14679:     var exclude = new Array();
14680:     for (var i=0; i<types.length; i++) {
14681:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
14682:         for (var j=0; j<spareboxes.length; j++) {
14683:             if (formname.elements[spareboxes[j]].checked) {
14684:                 exclude.push(formname.elements[spareboxes[j]].value);
14685:             } else {
14686:                 include.push(formname.elements[spareboxes[j]].value);
14687:             }
14688:         }
14689:     }
14690:     for (var i=0; i<types.length; i++) {
14691:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
14692:         var selIdx = newSpare.selectedIndex;
14693:         var currnew = newSpare.options[selIdx].value;
14694:         var okSpares = new Array();
14695:         for (var j=0; j<newSpare.options.length; j++) {
14696:             var possible = newSpare.options[j].value;
14697:             if (possible != '') {
14698:                 if (exclude.indexOf(possible) == -1) {
14699:                     okSpares.push(possible);
14700:                 } else {
14701:                     if (currnew == possible) {
14702:                         selIdx = 0;
14703:                     }
14704:                 }
14705:             }
14706:         }
14707:         for (var k=0; k<include.length; k++) {
14708:             if (okSpares.indexOf(include[k]) == -1) {
14709:                 okSpares.push(include[k]);
14710:             }
14711:         }
14712:         okSpares.sort();
14713:         newSpare.options.length = 0;
14714:         if (selIdx == 0) {
14715:             newSpare.options[0] = new Option("$select","",true,true);
14716:         } else {
14717:             newSpare.options[0] = new Option("$select","",false,false);
14718:         }
14719:         for (var m=0; m<okSpares.length; m++) {
14720:             var idx = m+1;
14721:             var selThis = 0;
14722:             if (selIdx != 0) {
14723:                 if (okSpares[m] == currnew) {
14724:                     selThis = 1;
14725:                 }
14726:             }
14727:             if (selThis == 1) {
14728:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
14729:             } else {
14730:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
14731:             }
14732:         }
14733:     }
14734:     return;
14735: }
14736: 
14737: function checkNewSpares(lonhost,type) {
14738:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
14739:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
14740:     if (chosen != '') { 
14741:         var othertype;
14742:         var othernewSpare;
14743:         if (type == 'primary') {
14744:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
14745:         }
14746:         if (type == 'default') {
14747:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
14748:         }
14749:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
14750:             othernewSpare.selectedIndex = 0;
14751:         }
14752:     }
14753:     return;
14754: }
14755: 
14756: // ]]>
14757: </script>
14758: 
14759: END
14760: 
14761: }
14762: 
14763: sub common_domprefs_js {
14764:     return <<"END";
14765: 
14766: <script type="text/javascript">
14767: // <![CDATA[
14768: 
14769: function getIndicesByName(formname,item) {
14770:     var group = new Array();
14771:     for (var i=0;i<formname.elements.length;i++) {
14772:         if (formname.elements[i].name == item) {
14773:             group.push(formname.elements[i].id);
14774:         }
14775:     }
14776:     return group;
14777: }
14778: 
14779: // ]]>
14780: </script>
14781: 
14782: END
14783: 
14784: }
14785: 
14786: sub recaptcha_js {
14787:     my %lt = &captcha_phrases();
14788:     return <<"END";
14789: 
14790: <script type="text/javascript">
14791: // <![CDATA[
14792: 
14793: function updateCaptcha(caller,context) {
14794:     var privitem;
14795:     var pubitem;
14796:     var privtext;
14797:     var pubtext;
14798:     var versionitem;
14799:     var versiontext;
14800:     if (document.getElementById(context+'_recaptchapub')) {
14801:         pubitem = document.getElementById(context+'_recaptchapub');
14802:     } else {
14803:         return;
14804:     }
14805:     if (document.getElementById(context+'_recaptchapriv')) {
14806:         privitem = document.getElementById(context+'_recaptchapriv');
14807:     } else {
14808:         return;
14809:     }
14810:     if (document.getElementById(context+'_recaptchapubtxt')) {
14811:         pubtext = document.getElementById(context+'_recaptchapubtxt');
14812:     } else {
14813:         return;
14814:     }
14815:     if (document.getElementById(context+'_recaptchaprivtxt')) {
14816:         privtext = document.getElementById(context+'_recaptchaprivtxt');
14817:     } else {
14818:         return;
14819:     }
14820:     if (document.getElementById(context+'_recaptchaversion')) {
14821:         versionitem = document.getElementById(context+'_recaptchaversion');
14822:     } else {
14823:         return;
14824:     }
14825:     if (document.getElementById(context+'_recaptchavertxt')) {
14826:         versiontext = document.getElementById(context+'_recaptchavertxt');
14827:     } else {
14828:         return;
14829:     }
14830:     if (caller.checked) {
14831:         if (caller.value == 'recaptcha') {
14832:             pubitem.type = 'text';
14833:             privitem.type = 'text';
14834:             pubitem.size = '40';
14835:             privitem.size = '40';
14836:             pubtext.innerHTML = "$lt{'pub'}";
14837:             privtext.innerHTML = "$lt{'priv'}";
14838:             versionitem.type = 'text';
14839:             versionitem.size = '3';
14840:             versiontext.innerHTML = "$lt{'ver'}";
14841:         } else {
14842:             pubitem.type = 'hidden';
14843:             privitem.type = 'hidden';
14844:             versionitem.type = 'hidden';
14845:             pubtext.innerHTML = '';
14846:             privtext.innerHTML = '';
14847:             versiontext.innerHTML = '';
14848:         }
14849:     }
14850:     return;
14851: }
14852: 
14853: // ]]>
14854: </script>
14855: 
14856: END
14857: 
14858: }
14859: 
14860: sub toggle_display_js {
14861:     return <<"END";
14862: 
14863: <script type="text/javascript">
14864: // <![CDATA[
14865: 
14866: function toggleDisplay(domForm,caller) {
14867:     if (document.getElementById(caller)) {
14868:         var divitem = document.getElementById(caller);
14869:         var optionsElement = domForm.coursecredits;
14870:         var checkval = 1;
14871:         var dispval = 'block';
14872:         if (caller == 'emailoptions') {
14873:             optionsElement = domForm.cancreate_email; 
14874:         }
14875:         if (caller == 'studentsubmission') {
14876:             optionsElement = domForm.postsubmit;
14877:         }
14878:         if (caller == 'cloneinstcode') {
14879:             optionsElement = domForm.canclone;
14880:             checkval = 'instcode';
14881:         }
14882:         if (optionsElement.length) {
14883:             var currval;
14884:             for (var i=0; i<optionsElement.length; i++) {
14885:                 if (optionsElement[i].checked) {
14886:                    currval = optionsElement[i].value;
14887:                 }
14888:             }
14889:             if (currval == checkval) {
14890:                 divitem.style.display = dispval;
14891:             } else {
14892:                 divitem.style.display = 'none';
14893:             }
14894:         }
14895:     }
14896:     return;
14897: }
14898: 
14899: // ]]>
14900: </script>
14901: 
14902: END
14903: 
14904: }
14905: 
14906: sub captcha_phrases {
14907:     return &Apache::lonlocal::texthash (
14908:                  priv => 'Private key',
14909:                  pub  => 'Public key',
14910:                  original  => 'original (CAPTCHA)',
14911:                  recaptcha => 'successor (ReCAPTCHA)',
14912:                  notused   => 'unused',
14913:                  ver => 'ReCAPTCHA version (1 or 2)',
14914:     );
14915: }
14916: 
14917: sub devalidate_remote_domconfs {
14918:     my ($dom,$cachekeys) = @_;
14919:     return unless (ref($cachekeys) eq 'HASH');
14920:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
14921:     my %thismachine;
14922:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
14923:     my @posscached = ('domainconfig','domdefaults','ltitools','usersessions','directorysrch');
14924:     if (keys(%servers)) {
14925:         foreach my $server (keys(%servers)) {
14926:             next if ($thismachine{$server});
14927:             my @cached;
14928:             foreach my $name (@posscached) {
14929:                 if ($cachekeys->{$name}) {
14930:                     push(@cached,&escape($name).':'.&escape($dom));
14931:                 }
14932:             }
14933:             if (@cached) {
14934:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
14935:             }
14936:         }
14937:     }
14938:     return;
14939: }
14940: 
14941: 1;

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