File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.84.2.9: download - view: text, annotated - select for diffs
Thu Nov 16 23:33:05 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.316.

    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.9 2017/11/16 23:33:05 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:                                 my @args = ('convert','-sample',$size,$input,$output);
 1550:                                 system({$args[0]} @args);
 1551:                                 $showfile = "/$imgdir/tn-".$filename;
 1552:                             }
 1553:                         }
 1554:                     }
 1555:                 }
 1556:             }
 1557:             if ($showfile) {
 1558:                 if ($showfile =~ m{^/(adm|res)/}) {
 1559:                     if ($showfile =~ m{^/res/}) {
 1560:                         my $local_showfile =
 1561:                             &Apache::lonnet::filelocation('',$showfile);
 1562:                         &Apache::lonnet::repcopy($local_showfile);
 1563:                     }
 1564:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1565:                 }
 1566:                 if ($imgfile) {
 1567:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1568:                         if ($imgfile =~ m{^/res/}) {
 1569:                             my $local_imgfile =
 1570:                                 &Apache::lonnet::filelocation('',$imgfile);
 1571:                             &Apache::lonnet::repcopy($local_imgfile);
 1572:                         }
 1573:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1574:                     } else {
 1575:                         $fullsize = $imgfile;
 1576:                     }
 1577:                 }
 1578:                 $datatable .= '<td>';
 1579:                 if ($img eq 'login') {
 1580:                     $datatable .= $login_hdr_pick;
 1581:                 } 
 1582:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1583:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1584:             } else {
 1585:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1586:                               &mt('Upload:').'<br />';
 1587:             }
 1588:         } else {
 1589:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1590:                           &mt('Upload:').'<br />';
 1591:         }
 1592:         if ($switchserver) {
 1593:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1594:         } else {
 1595:             if ($img ne 'login') { # suppress file selection for Log-in header
 1596:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1597:             }
 1598:         }
 1599:         $datatable .= '</td></tr>';
 1600:     }
 1601:     $itemcount ++;
 1602:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1603:     $datatable .= '<tr'.$css_class.'>'.
 1604:                   '<td>'.$choices->{'bgs'}.'</td>';
 1605:     my $bgs_def;
 1606:     foreach my $item (@{$bgs}) {
 1607:         if (!$is_custom->{$item}) {
 1608:             $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>';
 1609:         }
 1610:     }
 1611:     if ($bgs_def) {
 1612:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1613:     } else {
 1614:         $datatable .= '<td>&nbsp;</td>';
 1615:     }
 1616:     $datatable .= '<td class="LC_right_item">'.
 1617:                   '<table border="0"><tr>';
 1618: 
 1619:     foreach my $item (@{$bgs}) {
 1620:         $datatable .= '<td align="center">'.$choices->{$item};
 1621: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1622:         if ($designs->{'bgs'}{$item}) {
 1623:             $datatable .= '&nbsp;';
 1624:         }
 1625:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1626:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1627:     }
 1628:     $datatable .= '</tr></table></td></tr>';
 1629:     $itemcount ++;
 1630:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1631:     $datatable .= '<tr'.$css_class.'>'.
 1632:                   '<td>'.$choices->{'links'}.'</td>';
 1633:     my $links_def;
 1634:     foreach my $item (@{$links}) {
 1635:         if (!$is_custom->{$item}) {
 1636:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1637:         }
 1638:     }
 1639:     if ($links_def) {
 1640:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1641:     } else {
 1642:         $datatable .= '<td>&nbsp;</td>';
 1643:     }
 1644:     $datatable .= '<td class="LC_right_item">'.
 1645:                   '<table border="0"><tr>';
 1646:     foreach my $item (@{$links}) {
 1647: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1648:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1649:         if ($designs->{'links'}{$item}) {
 1650:             $datatable.='&nbsp;';
 1651:         }
 1652:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1653:                       '" /></td>';
 1654:     }
 1655:     $$rowtotal += $itemcount;
 1656:     return $datatable;
 1657: }
 1658: 
 1659: sub logo_display_options {
 1660:     my ($img,$defaults,$designs) = @_;
 1661:     my $checkedon;
 1662:     if (ref($defaults) eq 'HASH') {
 1663:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1664:             if ($defaults->{'showlogo'}{$img}) {
 1665:                 $checkedon = 'checked="checked" ';     
 1666:             }
 1667:         } 
 1668:     }
 1669:     if (ref($designs) eq 'HASH') {
 1670:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1671:             if (defined($designs->{'showlogo'}{$img})) {
 1672:                 if ($designs->{'showlogo'}{$img} == 0) {
 1673:                     $checkedon = '';
 1674:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1675:                     $checkedon = 'checked="checked" ';
 1676:                 }
 1677:             }
 1678:         }
 1679:     }
 1680:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1681:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1682:            &mt('show').'</label>'."\n";
 1683: }
 1684: 
 1685: sub login_header_options  {
 1686:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1687:     my $output = '';
 1688:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1689:         $output .= &mt('Text default(s):').'<br />';
 1690:         if (!$is_custom->{'textcol'}) {
 1691:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1692:                        '&nbsp;&nbsp;&nbsp;';
 1693:         }
 1694:         if (!$is_custom->{'bgcol'}) {
 1695:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1696:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1697:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1698:         }
 1699:         $output .= '<br />';
 1700:     }
 1701:     $output .='<br />';
 1702:     return $output;
 1703: }
 1704: 
 1705: sub login_text_colors {
 1706:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1707:     my $color_menu = '<table border="0"><tr>';
 1708:     foreach my $item (@{$logintext}) {
 1709:         $color_menu .= '<td align="center">'.$choices->{$item};
 1710:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1711:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1712:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1713:     }
 1714:     $color_menu .= '</tr></table><br />';
 1715:     return $color_menu;
 1716: }
 1717: 
 1718: sub image_changes {
 1719:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1720:     my $output;
 1721:     if ($img eq 'login') {
 1722:             # suppress image for Log-in header
 1723:     } elsif (!$is_custom) {
 1724:         if ($img ne 'domlogo') {
 1725:             $output .= &mt('Default image:').'<br />';
 1726:         } else {
 1727:             $output .= &mt('Default in use:').'<br />';
 1728:         }
 1729:     }
 1730:     if ($img eq 'login') { # suppress image for Log-in header
 1731:         $output .= '<td>'.$logincolors;
 1732:     } else {
 1733:         if ($img_import) {
 1734:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1735:         }
 1736:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1737:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1738:         if ($is_custom) {
 1739:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1740:                        '<input type="checkbox" name="'.
 1741:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1742:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1743:         } else {
 1744:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1745:         }
 1746:     }
 1747:     return $output;
 1748: }
 1749: 
 1750: sub print_quotas {
 1751:     my ($dom,$settings,$rowtotal,$action) = @_;
 1752:     my $context;
 1753:     if ($action eq 'quotas') {
 1754:         $context = 'tools';
 1755:     } else {
 1756:         $context = $action;
 1757:     }
 1758:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1759:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1760:     my $typecount = 0;
 1761:     my ($css_class,%titles);
 1762:     if ($context eq 'requestcourses') {
 1763:         @usertools = ('official','unofficial','community','textbook');
 1764:         @options =('norequest','approval','validate','autolimit');
 1765:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1766:         %titles = &courserequest_titles();
 1767:     } elsif ($context eq 'requestauthor') {
 1768:         @usertools = ('author');
 1769:         @options = ('norequest','approval','automatic');
 1770:         %titles = &authorrequest_titles();
 1771:     } else {
 1772:         @usertools = ('aboutme','blog','webdav','portfolio');
 1773:         %titles = &tool_titles();
 1774:     }
 1775:     if (ref($types) eq 'ARRAY') {
 1776:         foreach my $type (@{$types}) {
 1777:             my ($currdefquota,$currauthorquota);
 1778:             unless (($context eq 'requestcourses') ||
 1779:                     ($context eq 'requestauthor')) {
 1780:                 if (ref($settings) eq 'HASH') {
 1781:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1782:                         $currdefquota = $settings->{defaultquota}->{$type};
 1783:                     } else {
 1784:                         $currdefquota = $settings->{$type};
 1785:                     }
 1786:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1787:                         $currauthorquota = $settings->{authorquota}->{$type};
 1788:                     }
 1789:                 }
 1790:             }
 1791:             if (defined($usertypes->{$type})) {
 1792:                 $typecount ++;
 1793:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1794:                 $datatable .= '<tr'.$css_class.'>'.
 1795:                               '<td>'.$usertypes->{$type}.'</td>'.
 1796:                               '<td class="LC_left_item">';
 1797:                 if ($context eq 'requestcourses') {
 1798:                     $datatable .= '<table><tr>';
 1799:                 }
 1800:                 my %cell;  
 1801:                 foreach my $item (@usertools) {
 1802:                     if ($context eq 'requestcourses') {
 1803:                         my ($curroption,$currlimit);
 1804:                         if (ref($settings) eq 'HASH') {
 1805:                             if (ref($settings->{$item}) eq 'HASH') {
 1806:                                 $curroption = $settings->{$item}->{$type};
 1807:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1808:                                     $currlimit = $1; 
 1809:                                 }
 1810:                             }
 1811:                         }
 1812:                         if (!$curroption) {
 1813:                             $curroption = 'norequest';
 1814:                         }
 1815:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1816:                         foreach my $option (@options) {
 1817:                             my $val = $option;
 1818:                             if ($option eq 'norequest') {
 1819:                                 $val = 0;  
 1820:                             }
 1821:                             if ($option eq 'validate') {
 1822:                                 my $canvalidate = 0;
 1823:                                 if (ref($validations{$item}) eq 'HASH') { 
 1824:                                     if ($validations{$item}{$type}) {
 1825:                                         $canvalidate = 1;
 1826:                                     }
 1827:                                 }
 1828:                                 next if (!$canvalidate);
 1829:                             }
 1830:                             my $checked = '';
 1831:                             if ($option eq $curroption) {
 1832:                                 $checked = ' checked="checked"';
 1833:                             } elsif ($option eq 'autolimit') {
 1834:                                 if ($curroption =~ /^autolimit/) {
 1835:                                     $checked = ' checked="checked"';
 1836:                                 }                       
 1837:                             } 
 1838:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1839:                                   '<input type="radio" name="crsreq_'.$item.
 1840:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1841:                                   $titles{$option}.'</label>';
 1842:                             if ($option eq 'autolimit') {
 1843:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1844:                                                 $item.'_limit_'.$type.'" size="1" '.
 1845:                                                 'value="'.$currlimit.'" />';
 1846:                             }
 1847:                             $cell{$item} .= '</span> ';
 1848:                             if ($option eq 'autolimit') {
 1849:                                 $cell{$item} .= $titles{'unlimited'};
 1850:                             }
 1851:                         }
 1852:                     } elsif ($context eq 'requestauthor') {
 1853:                         my $curroption;
 1854:                         if (ref($settings) eq 'HASH') {
 1855:                             $curroption = $settings->{$type};
 1856:                         }
 1857:                         if (!$curroption) {
 1858:                             $curroption = 'norequest';
 1859:                         }
 1860:                         foreach my $option (@options) {
 1861:                             my $val = $option;
 1862:                             if ($option eq 'norequest') {
 1863:                                 $val = 0;
 1864:                             }
 1865:                             my $checked = '';
 1866:                             if ($option eq $curroption) {
 1867:                                 $checked = ' checked="checked"';
 1868:                             }
 1869:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1870:                                   '<input type="radio" name="authorreq_'.$type.
 1871:                                   '" value="'.$val.'"'.$checked.' />'.
 1872:                                   $titles{$option}.'</label></span>&nbsp; ';
 1873:                         }
 1874:                     } else {
 1875:                         my $checked = 'checked="checked" ';
 1876:                         if (ref($settings) eq 'HASH') {
 1877:                             if (ref($settings->{$item}) eq 'HASH') {
 1878:                                 if ($settings->{$item}->{$type} == 0) {
 1879:                                     $checked = '';
 1880:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1881:                                     $checked =  'checked="checked" ';
 1882:                                 }
 1883:                             }
 1884:                         }
 1885:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1886:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1887:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1888:                                       '</label></span>&nbsp; ';
 1889:                     }
 1890:                 }
 1891:                 if ($context eq 'requestcourses') {
 1892:                     $datatable .= '</tr><tr>';
 1893:                     foreach my $item (@usertools) {
 1894:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1895:                     }
 1896:                     $datatable .= '</tr></table>';
 1897:                 }
 1898:                 $datatable .= '</td>';
 1899:                 unless (($context eq 'requestcourses') ||
 1900:                         ($context eq 'requestauthor')) {
 1901:                     $datatable .= 
 1902:                               '<td class="LC_right_item">'.
 1903:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1904:                               '<input type="text" name="quota_'.$type.
 1905:                               '" value="'.$currdefquota.
 1906:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1907:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1908:                               '<input type="text" name="authorquota_'.$type.
 1909:                               '" value="'.$currauthorquota.
 1910:                               '" size="5" /></span></td>';
 1911:                 }
 1912:                 $datatable .= '</tr>';
 1913:             }
 1914:         }
 1915:     }
 1916:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1917:         $defaultquota = '20';
 1918:         $authorquota = '500';
 1919:         if (ref($settings) eq 'HASH') {
 1920:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1921:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1922:             } elsif (defined($settings->{'default'})) {
 1923:                 $defaultquota = $settings->{'default'};
 1924:             }
 1925:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1926:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1927:             }
 1928:         }
 1929:     }
 1930:     $typecount ++;
 1931:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1932:     $datatable .= '<tr'.$css_class.'>'.
 1933:                   '<td>'.$othertitle.'</td>'.
 1934:                   '<td class="LC_left_item">';
 1935:     if ($context eq 'requestcourses') {
 1936:         $datatable .= '<table><tr>';
 1937:     }
 1938:     my %defcell;
 1939:     foreach my $item (@usertools) {
 1940:         if ($context eq 'requestcourses') {
 1941:             my ($curroption,$currlimit);
 1942:             if (ref($settings) eq 'HASH') {
 1943:                 if (ref($settings->{$item}) eq 'HASH') {
 1944:                     $curroption = $settings->{$item}->{'default'};
 1945:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1946:                         $currlimit = $1;
 1947:                     }
 1948:                 }
 1949:             }
 1950:             if (!$curroption) {
 1951:                 $curroption = 'norequest';
 1952:             }
 1953:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1954:             foreach my $option (@options) {
 1955:                 my $val = $option;
 1956:                 if ($option eq 'norequest') {
 1957:                     $val = 0;
 1958:                 }
 1959:                 if ($option eq 'validate') {
 1960:                     my $canvalidate = 0;
 1961:                     if (ref($validations{$item}) eq 'HASH') {
 1962:                         if ($validations{$item}{'default'}) {
 1963:                             $canvalidate = 1;
 1964:                         }
 1965:                     }
 1966:                     next if (!$canvalidate);
 1967:                 }
 1968:                 my $checked = '';
 1969:                 if ($option eq $curroption) {
 1970:                     $checked = ' checked="checked"';
 1971:                 } elsif ($option eq 'autolimit') {
 1972:                     if ($curroption =~ /^autolimit/) {
 1973:                         $checked = ' checked="checked"';
 1974:                     }
 1975:                 }
 1976:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1977:                                   '<input type="radio" name="crsreq_'.$item.
 1978:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1979:                                   $titles{$option}.'</label>';
 1980:                 if ($option eq 'autolimit') {
 1981:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1982:                                        $item.'_limit_default" size="1" '.
 1983:                                        'value="'.$currlimit.'" />';
 1984:                 }
 1985:                 $defcell{$item} .= '</span> ';
 1986:                 if ($option eq 'autolimit') {
 1987:                     $defcell{$item} .= $titles{'unlimited'};
 1988:                 }
 1989:             }
 1990:         } elsif ($context eq 'requestauthor') {
 1991:             my $curroption;
 1992:             if (ref($settings) eq 'HASH') {
 1993:                 $curroption = $settings->{'default'};
 1994:             }
 1995:             if (!$curroption) {
 1996:                 $curroption = 'norequest';
 1997:             }
 1998:             foreach my $option (@options) {
 1999:                 my $val = $option;
 2000:                 if ($option eq 'norequest') {
 2001:                     $val = 0;
 2002:                 }
 2003:                 my $checked = '';
 2004:                 if ($option eq $curroption) {
 2005:                     $checked = ' checked="checked"';
 2006:                 }
 2007:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2008:                               '<input type="radio" name="authorreq_default"'.
 2009:                               ' value="'.$val.'"'.$checked.' />'.
 2010:                               $titles{$option}.'</label></span>&nbsp; ';
 2011:             }
 2012:         } else {
 2013:             my $checked = 'checked="checked" ';
 2014:             if (ref($settings) eq 'HASH') {
 2015:                 if (ref($settings->{$item}) eq 'HASH') {
 2016:                     if ($settings->{$item}->{'default'} == 0) {
 2017:                         $checked = '';
 2018:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2019:                         $checked = 'checked="checked" ';
 2020:                     }
 2021:                 }
 2022:             }
 2023:             $datatable .= '<span class="LC_nobreak"><label>'.
 2024:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2025:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2026:                           '</label></span>&nbsp; ';
 2027:         }
 2028:     }
 2029:     if ($context eq 'requestcourses') {
 2030:         $datatable .= '</tr><tr>';
 2031:         foreach my $item (@usertools) {
 2032:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2033:         }
 2034:         $datatable .= '</tr></table>';
 2035:     }
 2036:     $datatable .= '</td>';
 2037:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2038:         $datatable .= '<td class="LC_right_item">'.
 2039:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2040:                       '<input type="text" name="defaultquota" value="'.
 2041:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2042:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2043:                       '<input type="text" name="authorquota" value="'.
 2044:                       $authorquota.'" size="5" /></span></td>';
 2045:     }
 2046:     $datatable .= '</tr>';
 2047:     $typecount ++;
 2048:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2049:     $datatable .= '<tr'.$css_class.'>'.
 2050:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2051:     if ($context eq 'requestcourses') {
 2052:         $datatable .= &mt('(overrides affiliation, if set)').
 2053:                       '</td>'.
 2054:                       '<td class="LC_left_item">'.
 2055:                       '<table><tr>';
 2056:     } else {
 2057:         $datatable .= &mt('(overrides affiliation, if checked)').
 2058:                       '</td>'.
 2059:                       '<td class="LC_left_item" colspan="2">'.
 2060:                       '<br />';
 2061:     }
 2062:     my %advcell;
 2063:     foreach my $item (@usertools) {
 2064:         if ($context eq 'requestcourses') {
 2065:             my ($curroption,$currlimit);
 2066:             if (ref($settings) eq 'HASH') {
 2067:                 if (ref($settings->{$item}) eq 'HASH') {
 2068:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2069:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2070:                         $currlimit = $1;
 2071:                     }
 2072:                 }
 2073:             }
 2074:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2075:             my $checked = '';
 2076:             if ($curroption eq '') {
 2077:                 $checked = ' checked="checked"';
 2078:             }
 2079:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2080:                                '<input type="radio" name="crsreq_'.$item.
 2081:                                '__LC_adv" value=""'.$checked.' />'.
 2082:                                &mt('No override set').'</label></span>&nbsp; ';
 2083:             foreach my $option (@options) {
 2084:                 my $val = $option;
 2085:                 if ($option eq 'norequest') {
 2086:                     $val = 0;
 2087:                 }
 2088:                 if ($option eq 'validate') {
 2089:                     my $canvalidate = 0;
 2090:                     if (ref($validations{$item}) eq 'HASH') {
 2091:                         if ($validations{$item}{'_LC_adv'}) {
 2092:                             $canvalidate = 1;
 2093:                         }
 2094:                     }
 2095:                     next if (!$canvalidate);
 2096:                 }
 2097:                 my $checked = '';
 2098:                 if ($val eq $curroption) {
 2099:                     $checked = ' checked="checked"';
 2100:                 } elsif ($option eq 'autolimit') {
 2101:                     if ($curroption =~ /^autolimit/) {
 2102:                         $checked = ' checked="checked"';
 2103:                     }
 2104:                 }
 2105:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2106:                                   '<input type="radio" name="crsreq_'.$item.
 2107:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2108:                                   $titles{$option}.'</label>';
 2109:                 if ($option eq 'autolimit') {
 2110:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2111:                                        $item.'_limit__LC_adv" size="1" '.
 2112:                                        'value="'.$currlimit.'" />';
 2113:                 }
 2114:                 $advcell{$item} .= '</span> ';
 2115:                 if ($option eq 'autolimit') {
 2116:                     $advcell{$item} .= $titles{'unlimited'};
 2117:                 }
 2118:             }
 2119:         } elsif ($context eq 'requestauthor') {
 2120:             my $curroption;
 2121:             if (ref($settings) eq 'HASH') {
 2122:                 $curroption = $settings->{'_LC_adv'};
 2123:             }
 2124:             my $checked = '';
 2125:             if ($curroption eq '') {
 2126:                 $checked = ' checked="checked"';
 2127:             }
 2128:             $datatable .= '<span class="LC_nobreak"><label>'.
 2129:                           '<input type="radio" name="authorreq__LC_adv"'.
 2130:                           ' value=""'.$checked.' />'.
 2131:                           &mt('No override set').'</label></span>&nbsp; ';
 2132:             foreach my $option (@options) {
 2133:                 my $val = $option;
 2134:                 if ($option eq 'norequest') {
 2135:                     $val = 0;
 2136:                 }
 2137:                 my $checked = '';
 2138:                 if ($val eq $curroption) {
 2139:                     $checked = ' checked="checked"';
 2140:                 }
 2141:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2142:                               '<input type="radio" name="authorreq__LC_adv"'.
 2143:                               ' value="'.$val.'"'.$checked.' />'.
 2144:                               $titles{$option}.'</label></span>&nbsp; ';
 2145:             }
 2146:         } else {
 2147:             my $checked = 'checked="checked" ';
 2148:             if (ref($settings) eq 'HASH') {
 2149:                 if (ref($settings->{$item}) eq 'HASH') {
 2150:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2151:                         $checked = '';
 2152:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2153:                         $checked = 'checked="checked" ';
 2154:                     }
 2155:                 }
 2156:             }
 2157:             $datatable .= '<span class="LC_nobreak"><label>'.
 2158:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2159:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2160:                           '</label></span>&nbsp; ';
 2161:         }
 2162:     }
 2163:     if ($context eq 'requestcourses') {
 2164:         $datatable .= '</tr><tr>';
 2165:         foreach my $item (@usertools) {
 2166:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2167:         }
 2168:         $datatable .= '</tr></table>';
 2169:     }
 2170:     $datatable .= '</td></tr>';
 2171:     $$rowtotal += $typecount;
 2172:     return $datatable;
 2173: }
 2174: 
 2175: sub print_requestmail {
 2176:     my ($dom,$action,$settings,$rowtotal) = @_;
 2177:     my ($now,$datatable,%currapp);
 2178:     $now = time;
 2179:     if (ref($settings) eq 'HASH') {
 2180:         if (ref($settings->{'notify'}) eq 'HASH') {
 2181:             if ($settings->{'notify'}{'approval'} ne '') {
 2182:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2183:             }
 2184:         }
 2185:     }
 2186:     my $numinrow = 2;
 2187:     my $css_class;
 2188:     $css_class = ($$rowtotal%2? ' class="LC_odd_row"':'');
 2189:     my $text;
 2190:     if ($action eq 'requestcourses') {
 2191:         $text = &mt('Receive notification of course requests requiring approval');
 2192:     } elsif ($action eq 'requestauthor') {
 2193:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2194:     } else {
 2195:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2196:     }
 2197:     $datatable = '<tr'.$css_class.'>'.
 2198:                  ' <td>'.$text.'</td>'.
 2199:                  ' <td class="LC_left_item">';
 2200:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2201:                                                  $action.'notifyapproval',%currapp);
 2202:     if ($numdc > 0) {
 2203:         $datatable .= $table;
 2204:     } else {
 2205:         $datatable .= &mt('There are no active Domain Coordinators');
 2206:     }
 2207:     $datatable .='</td></tr>';
 2208:     return $datatable;
 2209: }
 2210: 
 2211: sub print_studentcode {
 2212:     my ($settings,$rowtotal) = @_;
 2213:     my $rownum = 0; 
 2214:     my ($output,%current);
 2215:     my @crstypes = ('official','unofficial','community','textbook');
 2216:     if (ref($settings) eq 'HASH') {
 2217:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2218:             foreach my $type (@crstypes) {
 2219:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2220:             }
 2221:         }
 2222:     }
 2223:     $output .= '<tr>'.
 2224:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2225:                '<td class="LC_left_item">';
 2226:     foreach my $type (@crstypes) {
 2227:         my $check = ' ';
 2228:         if ($current{$type}) {
 2229:             $check = ' checked="checked" ';
 2230:         }
 2231:         $output .= '<span class="LC_nobreak"><label>'.
 2232:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2233:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2234:     }
 2235:     $output .= '</td></tr>';
 2236:     $$rowtotal ++;
 2237:     return $output;
 2238: }
 2239: 
 2240: sub print_textbookcourses {
 2241:     my ($dom,$type,$settings,$rowtotal) = @_;
 2242:     my $rownum = 0;
 2243:     my $css_class;
 2244:     my $itemcount = 1;
 2245:     my $maxnum = 0;
 2246:     my $bookshash;
 2247:     if (ref($settings) eq 'HASH') {
 2248:         $bookshash = $settings->{$type};
 2249:     }
 2250:     my %ordered;
 2251:     if (ref($bookshash) eq 'HASH') {
 2252:         foreach my $item (keys(%{$bookshash})) {
 2253:             if (ref($bookshash->{$item}) eq 'HASH') {
 2254:                 my $num = $bookshash->{$item}{'order'};
 2255:                 $ordered{$num} = $item;
 2256:             }
 2257:         }
 2258:     }
 2259:     my $confname = $dom.'-domainconfig';
 2260:     my $switchserver = &check_switchserver($dom,$confname);
 2261:     my $maxnum = scalar(keys(%ordered));
 2262:     my $datatable;
 2263:     if (keys(%ordered)) {
 2264:         my @items = sort { $a <=> $b } keys(%ordered);
 2265:         for (my $i=0; $i<@items; $i++) {
 2266:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2267:             my $key = $ordered{$items[$i]};
 2268:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2269:             my $coursetitle = $coursehash{'description'};
 2270:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2271:             if (ref($bookshash->{$key}) eq 'HASH') {
 2272:                 $subject = $bookshash->{$key}->{'subject'};
 2273:                 $title = $bookshash->{$key}->{'title'};
 2274:                 if ($type eq 'textbooks') {
 2275:                     $publisher = $bookshash->{$key}->{'publisher'};
 2276:                     $author = $bookshash->{$key}->{'author'};
 2277:                     $image = $bookshash->{$key}->{'image'};
 2278:                     if ($image ne '') {
 2279:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2280:                         my $imagethumb = "$path/tn-".$imagefile;
 2281:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2282:                     }
 2283:                 }
 2284:             }
 2285:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2286:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2287:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2288:             for (my $k=0; $k<=$maxnum; $k++) {
 2289:                 my $vpos = $k+1;
 2290:                 my $selstr;
 2291:                 if ($k == $i) {
 2292:                     $selstr = ' selected="selected" ';
 2293:                 }
 2294:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2295:             }
 2296:             $datatable .= '</select>'.('&nbsp;'x2).
 2297:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2298:                 &mt('Delete?').'</label></span></td>'.
 2299:                 '<td colspan="2">'.
 2300:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2301:                 ('&nbsp;'x2).
 2302:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2303:             if ($type eq 'textbooks') {
 2304:                 $datatable .= ('&nbsp;'x2).
 2305:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2306:                               ('&nbsp;'x2).
 2307:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2308:                               ('&nbsp;'x2).
 2309:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2310:                 if ($image) {
 2311:                     $datatable .= '<span class="LC_nobreak">'.
 2312:                                   $imgsrc.
 2313:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2314:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2315:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2316:                 }
 2317:                 if ($switchserver) {
 2318:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2319:                 } else {
 2320:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2321:                 }
 2322:             }
 2323:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2324:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2325:                           $coursetitle.'</span></td></tr>'."\n";
 2326:             $itemcount ++;
 2327:         }
 2328:     }
 2329:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2330:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2331:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2332:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2333:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2334:     for (my $k=0; $k<$maxnum+1; $k++) {
 2335:         my $vpos = $k+1;
 2336:         my $selstr;
 2337:         if ($k == $maxnum) {
 2338:             $selstr = ' selected="selected" ';
 2339:         }
 2340:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2341:     }
 2342:     $datatable .= '</select>&nbsp;'."\n".
 2343:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2344:                   '<td colspan="2">'.
 2345:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2346:                   ('&nbsp;'x2).
 2347:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2348:                   ('&nbsp;'x2);
 2349:     if ($type eq 'textbooks') {
 2350:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2351:                       ('&nbsp;'x2).
 2352:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2353:                       ('&nbsp;'x2).
 2354:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2355:         if ($switchserver) {
 2356:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2357:         } else {
 2358:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2359:         }
 2360:     }
 2361:     $datatable .= '</span>'."\n".
 2362:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2363:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2364:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2365:                   &Apache::loncommon::selectcourse_link
 2366:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course');
 2367:                   '</span></td>'."\n".
 2368:                   '</tr>'."\n";
 2369:     $itemcount ++;
 2370:     return $datatable;
 2371: }
 2372: 
 2373: sub textbookcourses_javascript {
 2374:     my ($settings) = @_;
 2375:     return unless(ref($settings) eq 'HASH');
 2376:     my (%ordered,%total,%jstext);
 2377:     foreach my $type ('textbooks','templates') {
 2378:         $total{$type} = 0;
 2379:         if (ref($settings->{$type}) eq 'HASH') {
 2380:             foreach my $item (keys(%{$settings->{$type}})) {
 2381:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2382:                     my $num = $settings->{$type}->{$item}{'order'};
 2383:                     $ordered{$type}{$num} = $item;
 2384:                 }
 2385:             }
 2386:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2387:         }
 2388:         my @jsarray = ();
 2389:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2390:             push(@jsarray,$ordered{$type}{$item});
 2391:         }
 2392:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2393:     }
 2394:     return <<"ENDSCRIPT";
 2395: <script type="text/javascript">
 2396: // <![CDATA[
 2397: function reorderBooks(form,item,caller) {
 2398:     var changedVal;
 2399: $jstext{'textbooks'};
 2400: $jstext{'templates'};
 2401:     var newpos;
 2402:     var maxh;
 2403:     if (caller == 'textbooks') {  
 2404:         newpos = 'textbooks_addbook_pos';
 2405:         maxh = 1 + $total{'textbooks'};
 2406:     } else {
 2407:         newpos = 'templates_addbook_pos';
 2408:         maxh = 1 + $total{'templates'};
 2409:     }
 2410:     var current = new Array;
 2411:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2412:     if (item == newpos) {
 2413:         changedVal = newitemVal;
 2414:     } else {
 2415:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2416:         current[newitemVal] = newpos;
 2417:     }
 2418:     if (caller == 'textbooks') {
 2419:         for (var i=0; i<textbooks.length; i++) {
 2420:             var elementName = 'textbooks_'+textbooks[i];
 2421:             if (elementName != item) {
 2422:                 if (form.elements[elementName]) {
 2423:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2424:                     current[currVal] = elementName;
 2425:                 }
 2426:             }
 2427:         }
 2428:     }
 2429:     if (caller == 'templates') {
 2430:         for (var i=0; i<templates.length; i++) {
 2431:             var elementName = 'templates_'+templates[i];
 2432:             if (elementName != item) {
 2433:                 if (form.elements[elementName]) {
 2434:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2435:                     current[currVal] = elementName;
 2436:                 }
 2437:             }
 2438:         }
 2439:     }
 2440:     var oldVal;
 2441:     for (var j=0; j<maxh; j++) {
 2442:         if (current[j] == undefined) {
 2443:             oldVal = j;
 2444:         }
 2445:     }
 2446:     if (oldVal < changedVal) {
 2447:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2448:            var elementName = current[k];
 2449:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2450:         }
 2451:     } else {
 2452:         for (var k=changedVal; k<oldVal; k++) {
 2453:             var elementName = current[k];
 2454:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2455:         }
 2456:     }
 2457:     return;
 2458: }
 2459: 
 2460: // ]]>
 2461: </script>
 2462: 
 2463: ENDSCRIPT
 2464: }
 2465: 
 2466: sub ltitools_javascript {
 2467:     my ($settings) = @_;
 2468:     return unless(ref($settings) eq 'HASH');
 2469:     my (%ordered,$total,%jstext);
 2470:     $total = 0;
 2471:     foreach my $item (keys(%{$settings})) {
 2472:         if (ref($settings->{$item}) eq 'HASH') {
 2473:             my $num = $settings->{$item}{'order'};
 2474:             $ordered{$num} = $item;
 2475:         }
 2476:     }
 2477:     $total = scalar(keys(%{$settings}));
 2478:     my @jsarray = ();
 2479:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2480:         push(@jsarray,$ordered{$item});
 2481:     }
 2482:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 2483:     return <<"ENDSCRIPT";
 2484: <script type="text/javascript">
 2485: // <![CDATA[
 2486: function reorderLTI(form,item) {
 2487:     var changedVal;
 2488: $jstext
 2489:     var newpos = 'ltitools_add_pos';
 2490:     var maxh = 1 + $total;
 2491:     var current = new Array;
 2492:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2493:     if (item == newpos) {
 2494:         changedVal = newitemVal;
 2495:     } else {
 2496:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2497:         current[newitemVal] = newpos;
 2498:     }
 2499:     for (var i=0; i<ltitools.length; i++) {
 2500:         var elementName = 'ltitools_'+ltitools[i];
 2501:         if (elementName != item) {
 2502:             if (form.elements[elementName]) {
 2503:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2504:                 current[currVal] = elementName;
 2505:             }
 2506:         }
 2507:     }
 2508:     var oldVal;
 2509:     for (var j=0; j<maxh; j++) {
 2510:         if (current[j] == undefined) {
 2511:             oldVal = j;
 2512:         }
 2513:     }
 2514:     if (oldVal < changedVal) {
 2515:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2516:            var elementName = current[k];
 2517:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2518:         }
 2519:     } else {
 2520:         for (var k=changedVal; k<oldVal; k++) {
 2521:             var elementName = current[k];
 2522:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2523:         }
 2524:     }
 2525:     return;
 2526: }
 2527: 
 2528: // ]]>
 2529: </script>
 2530: 
 2531: ENDSCRIPT
 2532: }
 2533: 
 2534: sub print_autoenroll {
 2535:     my ($dom,$settings,$rowtotal) = @_;
 2536:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2537:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,$failsafe);
 2538:     if (ref($settings) eq 'HASH') {
 2539:         if (exists($settings->{'run'})) {
 2540:             if ($settings->{'run'} eq '0') {
 2541:                 $runoff = ' checked="checked" ';
 2542:                 $runon = ' ';
 2543:             } else {
 2544:                 $runon = ' checked="checked" ';
 2545:                 $runoff = ' ';
 2546:             }
 2547:         } else {
 2548:             if ($autorun) {
 2549:                 $runon = ' checked="checked" ';
 2550:                 $runoff = ' ';
 2551:             } else {
 2552:                 $runoff = ' checked="checked" ';
 2553:                 $runon = ' ';
 2554:             }
 2555:         }
 2556:         if (exists($settings->{'co-owners'})) {
 2557:             if ($settings->{'co-owners'} eq '0') {
 2558:                 $coownersoff = ' checked="checked" ';
 2559:                 $coownerson = ' ';
 2560:             } else {
 2561:                 $coownerson = ' checked="checked" ';
 2562:                 $coownersoff = ' ';
 2563:             }
 2564:         } else {
 2565:             $coownersoff = ' checked="checked" ';
 2566:             $coownerson = ' ';
 2567:         }
 2568:         if (exists($settings->{'sender_domain'})) {
 2569:             $defdom = $settings->{'sender_domain'};
 2570:         }
 2571:         if (exists($settings->{'autofailsafe'})) {
 2572:             $failsafe = $settings->{'autofailsafe'};
 2573:         }
 2574:     } else {
 2575:         if ($autorun) {
 2576:             $runon = ' checked="checked" ';
 2577:             $runoff = ' ';
 2578:         } else {
 2579:             $runoff = ' checked="checked" ';
 2580:             $runon = ' ';
 2581:         }
 2582:     }
 2583:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2584:     my $notif_sender;
 2585:     if (ref($settings) eq 'HASH') {
 2586:         $notif_sender = $settings->{'sender_uname'};
 2587:     }
 2588:     my $datatable='<tr class="LC_odd_row">'.
 2589:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2590:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2591:                   '<input type="radio" name="autoenroll_run"'.
 2592:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2593:                   '<label><input type="radio" name="autoenroll_run"'.
 2594:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2595:                   '</tr><tr>'.
 2596:                   '<td>'.&mt('Notification messages - sender').
 2597:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2598:                   &mt('username').':&nbsp;'.
 2599:                   '<input type="text" name="sender_uname" value="'.
 2600:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2601:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2602:                   '<tr class="LC_odd_row">'.
 2603:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2604:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2605:                   '<input type="radio" name="autoassign_coowners"'.
 2606:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2607:                   '<label><input type="radio" name="autoassign_coowners"'.
 2608:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2609:                   '</tr><tr>'.
 2610:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 2611:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2612:                   '<input type="text" name="autoenroll_failsafe"'.
 2613:                   ' value="'.$failsafe.'" size="4" /></td></tr>';
 2614:     $$rowtotal += 4;
 2615:     return $datatable;
 2616: }
 2617: 
 2618: sub print_autoupdate {
 2619:     my ($position,$dom,$settings,$rowtotal) = @_;
 2620:     my $datatable;
 2621:     if ($position eq 'top') {
 2622:         my $updateon = ' ';
 2623:         my $updateoff = ' checked="checked" ';
 2624:         my $classlistson = ' ';
 2625:         my $classlistsoff = ' checked="checked" ';
 2626:         if (ref($settings) eq 'HASH') {
 2627:             if ($settings->{'run'} eq '1') {
 2628:                 $updateon = $updateoff;
 2629:                 $updateoff = ' ';
 2630:             }
 2631:             if ($settings->{'classlists'} eq '1') {
 2632:                 $classlistson = $classlistsoff;
 2633:                 $classlistsoff = ' ';
 2634:             }
 2635:         }
 2636:         my %title = (
 2637:                    run => 'Auto-update active?',
 2638:                    classlists => 'Update information in classlists?',
 2639:                     );
 2640:         $datatable = '<tr class="LC_odd_row">'. 
 2641:                   '<td>'.&mt($title{'run'}).'</td>'.
 2642:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2643:                   '<input type="radio" name="autoupdate_run"'.
 2644:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2645:                   '<label><input type="radio" name="autoupdate_run"'.
 2646:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2647:                   '</tr><tr>'.
 2648:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2649:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2650:                   '<label><input type="radio" name="classlists"'.
 2651:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2652:                   '<label><input type="radio" name="classlists"'.
 2653:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2654:                   '</tr>';
 2655:         $$rowtotal += 2;
 2656:     } elsif ($position eq 'middle') {
 2657:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2658:         my $numinrow = 3;
 2659:         my $locknamesettings;
 2660:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2661:                                      $dom,$numinrow,$othertitle,
 2662:                                     'lockablenames',$rowtotal);
 2663:         $$rowtotal ++;
 2664:     } else {
 2665:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2666:         my @fields = ('lastname','firstname','middlename','generation',
 2667:                       'permanentemail','id');
 2668:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2669:         my $numrows = 0;
 2670:         if (ref($types) eq 'ARRAY') {
 2671:             if (@{$types} > 0) {
 2672:                 $datatable = 
 2673:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2674:                                          \@fields,$types,\$numrows);
 2675:                     $$rowtotal += @{$types}; 
 2676:             }
 2677:         }
 2678:         $datatable .= 
 2679:             &usertype_update_row($settings,{'default' => $othertitle},
 2680:                                  \%fieldtitles,\@fields,['default'],
 2681:                                  \$numrows);
 2682:         $$rowtotal ++;     
 2683:     }
 2684:     return $datatable;
 2685: }
 2686: 
 2687: sub print_autocreate {
 2688:     my ($dom,$settings,$rowtotal) = @_;
 2689:     my (%createon,%createoff,%currhash);
 2690:     my @types = ('xml','req');
 2691:     if (ref($settings) eq 'HASH') {
 2692:         foreach my $item (@types) {
 2693:             $createoff{$item} = ' checked="checked" ';
 2694:             $createon{$item} = ' ';
 2695:             if (exists($settings->{$item})) {
 2696:                 if ($settings->{$item}) {
 2697:                     $createon{$item} = ' checked="checked" ';
 2698:                     $createoff{$item} = ' ';
 2699:                 }
 2700:             }
 2701:         }
 2702:         if ($settings->{'xmldc'} ne '') {
 2703:             $currhash{$settings->{'xmldc'}} = 1;
 2704:         }
 2705:     } else {
 2706:         foreach my $item (@types) {
 2707:             $createoff{$item} = ' checked="checked" ';
 2708:             $createon{$item} = ' ';
 2709:         }
 2710:     }
 2711:     $$rowtotal += 2;
 2712:     my $numinrow = 2;
 2713:     my $datatable='<tr class="LC_odd_row">'.
 2714:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2715:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2716:                   '<input type="radio" name="autocreate_xml"'.
 2717:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2718:                   '<label><input type="radio" name="autocreate_xml"'.
 2719:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2720:                   '</td></tr><tr>'.
 2721:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2722:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2723:                   '<input type="radio" name="autocreate_req"'.
 2724:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2725:                   '<label><input type="radio" name="autocreate_req"'.
 2726:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2727:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2728:                                                    'autocreate_xmldc',%currhash);
 2729:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 2730:     if ($numdc > 1) {
 2731:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 2732:                       '</td><td class="LC_left_item">';
 2733:     } else {
 2734:         $datatable .= &mt('Course creation processed as:').
 2735:                       '</td><td class="LC_right_item">';
 2736:     }
 2737:     $datatable .= $dctable.'</td></tr>';
 2738:     $$rowtotal += $rows;
 2739:     return $datatable;
 2740: }
 2741: 
 2742: sub print_directorysrch {
 2743:     my ($position,$dom,$settings,$rowtotal) = @_;
 2744:     my $datatable;
 2745:     if ($position eq 'top') {
 2746:         my $instsrchon = ' ';
 2747:         my $instsrchoff = ' checked="checked" ';
 2748:         my ($exacton,$containson,$beginson);
 2749:         my $instlocalon = ' ';
 2750:         my $instlocaloff = ' checked="checked" ';
 2751:         if (ref($settings) eq 'HASH') {
 2752:             if ($settings->{'available'} eq '1') {
 2753:                 $instsrchon = $instsrchoff;
 2754:                 $instsrchoff = ' ';
 2755:             }
 2756:             if ($settings->{'localonly'} eq '1') {
 2757:                 $instlocalon = $instlocaloff;
 2758:                 $instlocaloff = ' ';
 2759:             }
 2760:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2761:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 2762:                     if ($type eq 'exact') {
 2763:                         $exacton = ' checked="checked" ';
 2764:                     } elsif ($type eq 'contains') {
 2765:                         $containson = ' checked="checked" ';
 2766:                     } elsif ($type eq 'begins') {
 2767:                         $beginson = ' checked="checked" ';
 2768:                     }
 2769:                 }
 2770:             } else {
 2771:                 if ($settings->{'searchtypes'} eq 'exact') {
 2772:                     $exacton = ' checked="checked" ';
 2773:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 2774:                     $containson = ' checked="checked" ';
 2775:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 2776:                     $exacton = ' checked="checked" ';
 2777:                     $containson = ' checked="checked" ';
 2778:                 }
 2779:             }
 2780:         }
 2781:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2782:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2783: 
 2784:         my $numinrow = 4;
 2785:         my $cansrchrow = 0;
 2786:         $datatable='<tr class="LC_odd_row">'.
 2787:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 2788:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2789:                    '<input type="radio" name="dirsrch_available"'.
 2790:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2791:                    '<label><input type="radio" name="dirsrch_available"'.
 2792:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2793:                    '</tr><tr>'.
 2794:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 2795:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2796:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 2797:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2798:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 2799:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2800:                    '</tr>';
 2801:         $$rowtotal += 2;
 2802:         if (ref($usertypes) eq 'HASH') {
 2803:             if (keys(%{$usertypes}) > 0) {
 2804:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2805:                                              $numinrow,$othertitle,'cansearch',
 2806:                                              $rowtotal);
 2807:                 $cansrchrow = 1;
 2808:             }
 2809:         }
 2810:         if ($cansrchrow) {
 2811:             $$rowtotal ++;
 2812:             $datatable .= '<tr>';
 2813:         } else {
 2814:             $datatable .= '<tr class="LC_odd_row">';
 2815:         }
 2816:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2817:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2818:         foreach my $title (@{$titleorder}) {
 2819:             if (defined($searchtitles->{$title})) {
 2820:                 my $check = ' ';
 2821:                 if (ref($settings) eq 'HASH') {
 2822:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2823:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2824:                             $check = ' checked="checked" ';
 2825:                         }
 2826:                     }
 2827:                 }
 2828:                 $datatable .= '<td class="LC_left_item">'.
 2829:                               '<span class="LC_nobreak"><label>'.
 2830:                               '<input type="checkbox" name="searchby" '.
 2831:                               'value="'.$title.'"'.$check.'/>'.
 2832:                               $searchtitles->{$title}.'</label></span></td>';
 2833:             }
 2834:         }
 2835:         $datatable .= '</tr></table></td></tr>';
 2836:         $$rowtotal ++;
 2837:         if ($cansrchrow) {
 2838:             $datatable .= '<tr class="LC_odd_row">';
 2839:         } else {
 2840:             $datatable .= '<tr>';
 2841:         }
 2842:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2843:                       '<td class="LC_left_item" colspan="2">'.
 2844:                       '<span class="LC_nobreak"><label>'.
 2845:                       '<input type="checkbox" name="searchtypes" '.
 2846:                       $exacton.' value="exact" />'.&mt('Exact match').
 2847:                       '</label>&nbsp;'.
 2848:                       '<label><input type="checkbox" name="searchtypes" '.
 2849:                       $beginson.' value="begins" />'.&mt('Begins with').
 2850:                       '</label>&nbsp;'.
 2851:                       '<label><input type="checkbox" name="searchtypes" '.
 2852:                       $containson.' value="contains" />'.&mt('Contains').
 2853:                       '</label></span></td></tr>';
 2854:         $$rowtotal ++;
 2855:     } else {
 2856:         my $domsrchon = ' checked="checked" ';
 2857:         my $domsrchoff = ' ';
 2858:         my $domlocalon = ' ';
 2859:         my $domlocaloff = ' checked="checked" ';
 2860:         if (ref($settings) eq 'HASH') {
 2861:             if ($settings->{'lclocalonly'} eq '1') {
 2862:                 $domlocalon = $domlocaloff;
 2863:                 $domlocaloff = ' ';
 2864:             }
 2865:             if ($settings->{'lcavailable'} eq '0') {
 2866:                 $domsrchoff = $domsrchon;
 2867:                 $domsrchon = ' ';
 2868:             }
 2869:         }
 2870:         $datatable='<tr class="LC_odd_row">'.
 2871:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 2872:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2873:                       '<input type="radio" name="dirsrch_domavailable"'.
 2874:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2875:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 2876:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2877:                       '</tr><tr>'.
 2878:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 2879:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2880:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 2881:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2882:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 2883:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2884:                       '</tr>';
 2885:         $$rowtotal += 2;
 2886:     }
 2887:     return $datatable;
 2888: }
 2889: 
 2890: sub print_contacts {
 2891:     my ($position,$dom,$settings,$rowtotal) = @_;
 2892:     my $datatable;
 2893:     my @contacts = ('adminemail','supportemail');
 2894:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 2895:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings);
 2896:     if ($position eq 'top') {
 2897:         if (ref($settings) eq 'HASH') {
 2898:             foreach my $item (@contacts) {
 2899:                 if (exists($settings->{$item})) {
 2900:                     $to{$item} = $settings->{$item};
 2901:                 }
 2902:             }
 2903:         }
 2904:     } elsif ($position eq 'middle') {
 2905:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 2906:                      'updatesmail','idconflictsmail');
 2907:         foreach my $type (@mailings) {
 2908:             $otheremails{$type} = '';
 2909:         }
 2910:     } else {
 2911:         @mailings = ('helpdeskmail','otherdomsmail');
 2912:         foreach my $type (@mailings) {
 2913:             $otheremails{$type} = '';
 2914:         }
 2915:         $bccemails{'helpdeskmail'} = '';
 2916:         $bccemails{'otherdomsmail'} = '';
 2917:         $includestr{'helpdeskmail'} = '';
 2918:         $includestr{'otherdomsmail'} = '';
 2919:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 2920:     }
 2921:     if (ref($settings) eq 'HASH') {
 2922:         unless ($position eq 'top') {
 2923:             foreach my $type (@mailings) {
 2924:                 if (exists($settings->{$type})) {
 2925:                     if (ref($settings->{$type}) eq 'HASH') {
 2926:                         foreach my $item (@contacts) {
 2927:                             if ($settings->{$type}{$item}) {
 2928:                                 $checked{$type}{$item} = ' checked="checked" ';
 2929:                             }
 2930:                         }
 2931:                         $otheremails{$type} = $settings->{$type}{'others'};
 2932:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 2933:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 2934:                             if ($settings->{$type}{'include'} ne '') {
 2935:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 2936:                                 $includestr{$type} = &unescape($includestr{$type});
 2937:                             }
 2938:                         }
 2939:                     }
 2940:                 } elsif ($type eq 'lonstatusmail') {
 2941:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2942:                 }
 2943:             }
 2944:         }
 2945:         if ($position eq 'bottom') {
 2946:             foreach my $type (@mailings) {
 2947:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 2948:                 if ($settings->{$type}{'include'} ne '') {
 2949:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 2950:                     $includestr{$type} = &unescape($includestr{$type});
 2951:                 }
 2952:             }
 2953:             if (ref($settings->{'helpform'}) eq 'HASH') {
 2954:                 if (ref($fields) eq 'ARRAY') {
 2955:                     foreach my $field (@{$fields}) {
 2956:                         $currfield{$field} = $settings->{'helpform'}{$field};
 2957:                     }
 2958:                 }
 2959:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 2960:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 2961:                 } else {
 2962:                     $maxsize = '1.0';
 2963:                 }
 2964:             } else {
 2965:                 if (ref($fields) eq 'ARRAY') {
 2966:                     foreach my $field (@{$fields}) {
 2967:                         $currfield{$field} = 'yes';
 2968:                     }
 2969:                 }
 2970:                 $maxsize = '1.0';
 2971:             }
 2972:         }
 2973:     } else {
 2974:         if ($position eq 'top') {
 2975:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2976:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2977:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2978:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2979:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2980:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2981:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2982:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 2983:         } elsif ($position eq 'bottom') {
 2984:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2985:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 2986:             if (ref($fields) eq 'ARRAY') {
 2987:                 foreach my $field (@{$fields}) {
 2988:                     $currfield{$field} = 'yes';
 2989:                 }
 2990:             }
 2991:             $maxsize = '1.0';
 2992:         }
 2993:     }
 2994:     my ($titles,$short_titles) = &contact_titles();
 2995:     my $rownum = 0;
 2996:     my $css_class;
 2997:     if ($position eq 'top') {
 2998:         foreach my $item (@contacts) {
 2999:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3000:             $datatable .= '<tr'.$css_class.'>'. 
 3001:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 3002:                           '</span></td><td class="LC_right_item">'.
 3003:                           '<input type="text" name="'.$item.'" value="'.
 3004:                           $to{$item}.'" /></td></tr>';
 3005:             $rownum ++;
 3006:         }
 3007:     } elsif ($position eq 'bottom') {
 3008:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3009:         $datatable .= '<tr'.$css_class.'>'.
 3010:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 3011:                       &mt('(e-mail, subject, and description always shown)').
 3012:                       '</td><td class="LC_left_item">';
 3013:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 3014:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 3015:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 3016:             foreach my $field (@{$fields}) {
 3017:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 3018:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 3019:                     $datatable .= ' '.&mt('(logged-in users)');
 3020:                 }
 3021:                 $datatable .='</td><td>';
 3022:                 my $clickaction;
 3023:                 if ($field eq 'screenshot') {
 3024:                     $clickaction = ' onclick="screenshotSize(this);"';
 3025:                 }
 3026:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 3027:                     foreach my $option (@{$possoptions->{$field}}) {
 3028:                         my $checked;
 3029:                         if ($currfield{$field} eq $option) {
 3030:                             $checked = ' checked="checked"';
 3031:                         }
 3032:                         $datatable .= '<span class="LC_nobreak"><label>'.
 3033:                                       '<input type="radio" name="helpform_'.$field.'" '.
 3034:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 3035:                                       '</label></span>'.('&nbsp;'x2);
 3036:                     }
 3037:                 }
 3038:                 if ($field eq 'screenshot') {
 3039:                     my $display;
 3040:                     if ($currfield{$field} eq 'no') {
 3041:                         $display = ' style="display:none"';
 3042:                     }
 3043:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.' />'.
 3044:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 3045:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 3046:                 }
 3047:                 $datatable .= '</td></tr>';
 3048:             }
 3049:             $datatable .= '</table>';
 3050:         }
 3051:         $datatable .= '</td></tr>'."\n";
 3052:         $rownum ++;
 3053:     }
 3054:     unless ($position eq 'top') {
 3055:         foreach my $type (@mailings) {
 3056:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3057:             $datatable .= '<tr'.$css_class.'>'.
 3058:                           '<td><span class="LC_nobreak">'.
 3059:                           $titles->{$type}.': </span></td>'.
 3060:                           '<td class="LC_left_item">';
 3061:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3062:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 3063:             }
 3064:             $datatable .= '<span class="LC_nobreak">';
 3065:             foreach my $item (@contacts) {
 3066:                 $datatable .= '<label>'.
 3067:                               '<input type="checkbox" name="'.$type.'"'.
 3068:                               $checked{$type}{$item}.
 3069:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 3070:                               '</label>&nbsp;';
 3071:             }
 3072:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3073:                           '<input type="text" name="'.$type.'_others" '.
 3074:                           'value="'.$otheremails{$type}.'"  />';
 3075:             my %locchecked;
 3076:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3077:                 foreach my $loc ('s','b') {
 3078:                     if ($includeloc{$type} eq $loc) {
 3079:                         $locchecked{$loc} = ' checked="checked"';
 3080:                         last;
 3081:                     }
 3082:                 }
 3083:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3084:                               '<input type="text" name="'.$type.'_bcc" '.
 3085:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 3086:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3087:                               &mt('Text automatically added to e-mail:').' '.
 3088:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br >'.
 3089:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3090:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3091:                               ('&nbsp;'x2).
 3092:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3093:                               '</span></fieldset>';
 3094:             }
 3095:             $datatable .= '</td></tr>'."\n";
 3096:             $rownum ++;
 3097:         }
 3098:     }
 3099:     if ($position eq 'middle') {
 3100:         my %choices;
 3101:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 3102:                                        &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3103:                                        &mt('LON-CAPA core group - MSU'),600,500));
 3104:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 3105:                                         &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3106:                                         &mt('LON-CAPA core group - MSU'),600,500));
 3107:         my @toggles = ('reporterrors','reportupdates');
 3108:         my %defaultchecked = ('reporterrors'  => 'on',
 3109:                               'reportupdates' => 'on');
 3110:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3111:                                                    \%choices,$rownum);
 3112:         $datatable .= $reports;
 3113:     } elsif ($position eq 'bottom') {
 3114:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3115:         my (@posstypes,%usertypeshash);
 3116:         if (ref($types) eq 'ARRAY') {
 3117:             @posstypes = @{$types};
 3118:         }
 3119:         if (@posstypes) {
 3120:             if (ref($usertypes) eq 'HASH') {
 3121:                 %usertypeshash = %{$usertypes};
 3122:             }
 3123:             my @overridden;
 3124:             my $numinrow = 4;
 3125:             if (ref($settings) eq 'HASH') {
 3126:                 if (ref($settings->{'overrides'}) eq 'HASH') {
 3127:                     foreach my $key (sort(keys(%{$settings->{'overrides'}}))) {
 3128:                         if (ref($settings->{'overrides'}{$key}) eq 'HASH') {
 3129:                             push(@overridden,$key);
 3130:                             foreach my $item (@contacts) {
 3131:                                 if ($settings->{'overrides'}{$key}{$item}) {
 3132:                                     $checked{'override_'.$key}{$item} = ' checked="checked" ';
 3133:                                 }
 3134:                             }
 3135:                             $otheremails{'override_'.$key} = $settings->{'overrides'}{$key}{'others'};
 3136:                             $bccemails{'override_'.$key} = $settings->{'overrides'}{$key}{'bcc'};
 3137:                             $includeloc{'override_'.$key} = '';
 3138:                             $includestr{'override_'.$key} = '';
 3139:                             if ($settings->{'overrides'}{$key}{'include'} ne '') {
 3140:                                 ($includeloc{'override_'.$key},$includestr{'override_'.$key}) =
 3141:                                     split(/:/,$settings->{'overrides'}{$key}{'include'},2);
 3142:                                 $includestr{'override_'.$key} = &unescape($includestr{'override_'.$key});
 3143:                             }
 3144:                         }
 3145:                     }
 3146:                 }
 3147:             }
 3148:             my $customclass = 'LC_helpdesk_override';
 3149:             my $optionsprefix = 'LC_options_helpdesk_';
 3150: 
 3151:             my $onclicktypes = "toggleHelpdeskRow(this.form,'overrides','$customclass','$optionsprefix');";
 3152: 
 3153:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 3154:                                          $numinrow,$othertitle,'overrides',
 3155:                                          \$rownum,$onclicktypes,$customclass);
 3156:             $rownum ++;
 3157:             $usertypeshash{'default'} = $othertitle;
 3158:             foreach my $status (@posstypes) {
 3159:                 my $css_class;
 3160:                 if ($rownum%2) {
 3161:                     $css_class = 'LC_odd_row ';
 3162:                 }
 3163:                 $css_class .= $customclass;
 3164:                 my $rowid = $optionsprefix.$status;
 3165:                 my $hidden = 1;
 3166:                 my $currstyle = 'display:none';
 3167:                 if (grep(/^\Q$status\E$/,@overridden)) {
 3168:                     $currstyle = 'display:table-row';
 3169:                     $hidden = 0;
 3170:                 }
 3171:                 my $key = 'override_'.$status;
 3172:                 $datatable .= &overridden_helpdesk($checked{$key},$otheremails{$key},$bccemails{$key},
 3173:                                                   $includeloc{$key},$includestr{$key},$status,$rowid,
 3174:                                                   $usertypeshash{$status},$css_class,$currstyle,
 3175:                                                   \@contacts,$short_titles);
 3176:                 unless ($hidden) {
 3177:                     $rownum ++;
 3178:                 }
 3179:             }
 3180:         }
 3181:     }
 3182:     $$rowtotal += $rownum;
 3183:     return $datatable;
 3184: }
 3185: 
 3186: sub overridden_helpdesk {
 3187:     my ($checked,$otheremails,$bccemails,$includeloc,$includestr,$type,$rowid,
 3188:         $typetitle,$css_class,$rowstyle,$contacts,$short_titles) = @_;
 3189:     my $class = 'LC_left_item';
 3190:     if ($css_class) {
 3191:         $css_class = ' class="'.$css_class.'"';
 3192:     }
 3193:     if ($rowid) {
 3194:         $rowid = ' id="'.$rowid.'"';
 3195:     }
 3196:     if ($rowstyle) {
 3197:         $rowstyle = ' style="'.$rowstyle.'"';
 3198:     }
 3199:     my ($output,$description);
 3200:     $description = &mt('Helpdesk requests from: [_1] in this domain (overrides default)',"<b>$typetitle</b>");
 3201:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 3202:               "<td>$description</td>\n".
 3203:               '<td class="'.$class.'" colspan="2">'.
 3204:               '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>'.
 3205:               '<span class="LC_nobreak">';
 3206:     if (ref($contacts) eq 'ARRAY') {
 3207:         foreach my $item (@{$contacts}) {
 3208:             my $check;
 3209:             if (ref($checked) eq 'HASH') {
 3210:                $check = $checked->{$item};
 3211:             }
 3212:             my $title;
 3213:             if (ref($short_titles) eq 'HASH') {
 3214:                 $title = $short_titles->{$item};
 3215:             }
 3216:             $output .= '<label>'.
 3217:                        '<input type="checkbox" name="override_'.$type.'"'.$check.
 3218:                        ' value="'.$item.'" />'.$title.'</label>&nbsp;';
 3219:         }
 3220:     }
 3221:     $output .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3222:                '<input type="text" name="override_'.$type.'_others" '.
 3223:                'value="'.$otheremails.'"  />';
 3224:     my %locchecked;
 3225:     foreach my $loc ('s','b') {
 3226:         if ($includeloc eq $loc) {
 3227:             $locchecked{$loc} = ' checked="checked"';
 3228:             last;
 3229:         }
 3230:     }
 3231:     $output .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3232:                '<input type="text" name="override_'.$type.'_bcc" '.
 3233:                'value="'.$bccemails.'"  /></fieldset>'.
 3234:                '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3235:                &mt('Text automatically added to e-mail:').' '.
 3236:                '<input type="text" name="override_'.$type.'_includestr" value="'.$includestr.'" /><br >'.
 3237:                '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3238:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3239:                ('&nbsp;'x2).
 3240:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3241:                '</span></fieldset>'.
 3242:                '</td></tr>'."\n";
 3243:     return $output;
 3244: }
 3245: 
 3246: sub contacts_javascript {
 3247:     return <<"ENDSCRIPT";
 3248: 
 3249: <script type="text/javascript">
 3250: // <![CDATA[
 3251: 
 3252: function screenshotSize(field) {
 3253:     if (document.getElementById('help_screenshotsize')) {
 3254:         if (field.value == 'no') {
 3255:             document.getElementById('help_screenshotsize').style.display="none";
 3256:         } else {
 3257:             document.getElementById('help_screenshotsize').style.display="";
 3258:         }
 3259:     }
 3260:     return;
 3261: }
 3262: 
 3263: function toggleHelpdeskRow(form,checkbox,target,prefix,docount) {
 3264:     if (form.elements[checkbox].length != undefined) {
 3265:         var count = 0;
 3266:         if (docount) {
 3267:             for (var i=0; i<form.elements[checkbox].length; i++) {
 3268:                 if (form.elements[checkbox][i].checked) {
 3269:                     count ++;
 3270:                 }
 3271:             }
 3272:         }
 3273:         for (var i=0; i<form.elements[checkbox].length; i++) {
 3274:             var type = form.elements[checkbox][i].value;
 3275:             if (document.getElementById(prefix+type)) {
 3276:                 if (form.elements[checkbox][i].checked) {
 3277:                     document.getElementById(prefix+type).style.display = 'table-row';
 3278:                     if (count % 2 == 1) {
 3279:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 3280:                     } else {
 3281:                         document.getElementById(prefix+type).className = target;
 3282:                     }
 3283:                     count ++;
 3284:                 } else {
 3285:                     document.getElementById(prefix+type).style.display = 'none';
 3286:                 }
 3287:             }
 3288:         }
 3289:     }
 3290:     return;
 3291: }
 3292: 
 3293: 
 3294: // ]]>
 3295: </script>
 3296: 
 3297: ENDSCRIPT
 3298: }
 3299: 
 3300: sub print_helpsettings {
 3301:     my ($position,$dom,$settings,$rowtotal) = @_;
 3302:     my $confname = $dom.'-domainconfig';
 3303:     my $formname = 'display';
 3304:     my ($datatable,$itemcount);
 3305:     if ($position eq 'top') {
 3306:         $itemcount = 1;
 3307:         my (%choices,%defaultchecked,@toggles);
 3308:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 3309:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 3310:                                      &mt('LON-CAPA bug tracker'),600,500));
 3311:         %defaultchecked = ('submitbugs' => 'on');
 3312:         @toggles = ('submitbugs');
 3313:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3314:                                                      \%choices,$itemcount);
 3315:         $$rowtotal ++;
 3316:     } else {
 3317:         my $css_class;
 3318:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 3319:         my (%customroles,%ordered,%current);
 3320:         if (ref($settings) eq 'HASH') {
 3321:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 3322:                 %current = %{$settings->{'adhoc'}};
 3323:             }
 3324:         }
 3325:         my $count = 0;
 3326:         foreach my $key (sort(keys(%existing))) {
 3327:             if ($key=~/^rolesdef\_(\w+)$/) {
 3328:                 my $rolename = $1;
 3329:                 my (%privs,$order);
 3330:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 3331:                 $customroles{$rolename} = \%privs;
 3332:                 if (ref($current{$rolename}) eq 'HASH') {
 3333:                     $order = $current{$rolename}{'order'};
 3334:                 }
 3335:                 if ($order eq '') {
 3336:                     $order = $count;
 3337:                 }
 3338:                 $ordered{$order} = $rolename;
 3339:                 $count++;
 3340:             }
 3341:         }
 3342:         my $maxnum = scalar(keys(%ordered));
 3343:         my @roles_by_num = ();
 3344:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3345:             push(@roles_by_num,$item);
 3346:         }
 3347:         my $context = 'domprefs';
 3348:         my $crstype = 'Course';
 3349:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3350:         my @accesstypes = ('all','dh','da','none');
 3351:         my ($numstatustypes,@jsarray);
 3352:         if (ref($types) eq 'ARRAY') {
 3353:             if (@{$types} > 0) {
 3354:                 $numstatustypes = scalar(@{$types});
 3355:                 push(@accesstypes,'status');
 3356:                 @jsarray = ('bystatus');
 3357:             }
 3358:         }
 3359:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 3360:         if (keys(%domhelpdesk)) {
 3361:             push(@accesstypes,('inc','exc'));
 3362:             push(@jsarray,('notinc','notexc'));
 3363:         }
 3364:         my $hiddenstr = join("','",@jsarray);
 3365:         $datatable .= &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname);
 3366:         my $context = 'domprefs';
 3367:         my $crstype = 'Course';
 3368:         my $prefix = 'helproles_';
 3369:         my $add_class = 'LC_hidden';
 3370:         foreach my $num (@roles_by_num) {
 3371:             my $role = $ordered{$num};
 3372:             my ($desc,$access,@statuses);
 3373:             if (ref($current{$role}) eq 'HASH') {
 3374:                 $desc = $current{$role}{'desc'};
 3375:                 $access = $current{$role}{'access'};
 3376:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 3377:                     @statuses = @{$current{$role}{'insttypes'}};
 3378:                 }
 3379:             }
 3380:             if ($desc eq '') {
 3381:                 $desc = $role;
 3382:             }
 3383:             my $identifier = 'custhelp'.$num;
 3384:             my %full=();
 3385:             my %levels= (
 3386:                          course => {},
 3387:                          domain => {},
 3388:                          system => {},
 3389:                         );
 3390:             my %levelscurrent=(
 3391:                                course => {},
 3392:                                domain => {},
 3393:                                system => {},
 3394:                               );
 3395:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 3396:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3397:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3398:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 3399:             $datatable .= '<tr '.$css_class.'><td valign="top"><b>'.$role.'</b><br />'.
 3400:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 3401:             for (my $k=0; $k<=$maxnum; $k++) {
 3402:                 my $vpos = $k+1;
 3403:                 my $selstr;
 3404:                 if ($k == $num) {
 3405:                     $selstr = ' selected="selected" ';
 3406:                 }
 3407:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3408:             }
 3409:             $datatable .= '</select>'.('&nbsp;'x2).
 3410:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 3411:                           '</td>'.
 3412:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3413:                           &mt('Name shown to users:').
 3414:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 3415:                           '</fieldset>'.
 3416:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 3417:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 3418:                           '<fieldset>'.
 3419:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 3420:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 3421:                                                                    \%levelscurrent,$identifier,
 3422:                                                                    'LC_hidden',$prefix.$num.'_privs').
 3423:                           '</fieldset></td>';
 3424:             $itemcount ++;
 3425:         }
 3426:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3427:         my $newcust = 'custhelp'.$count;
 3428:         my (%privs,%levelscurrent);
 3429:         my %full=();
 3430:         my %levels= (
 3431:                      course => {},
 3432:                      domain => {},
 3433:                      system => {},
 3434:                     );
 3435:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 3436:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3437:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 3438:         $datatable .= '<tr '.$css_class.'><td valign="top"><span class="LC_nobreak"><label>'.
 3439:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 3440:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 3441:         for (my $k=0; $k<$maxnum+1; $k++) {
 3442:             my $vpos = $k+1;
 3443:             my $selstr;
 3444:             if ($k == $maxnum) {
 3445:                 $selstr = ' selected="selected" ';
 3446:             }
 3447:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3448:         }
 3449:         $datatable .= '</select>&nbsp;'."\n".
 3450:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 3451:                       '</label></span></td>'.
 3452:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3453:                       '<span class="LC_nobreak">'.
 3454:                       &mt('Internal name:').
 3455:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 3456:                       '</span>'.('&nbsp;'x4).
 3457:                       '<span class="LC_nobreak">'.
 3458:                       &mt('Name shown to users:').
 3459:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 3460:                       '</span></fieldset>'.
 3461:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 3462:                                              $usertypes,$types,\%domhelpdesk).
 3463:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 3464:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 3465:                                                                 \@templateroles,$newcust).
 3466:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 3467:                                                                \%levelscurrent,$newcust).
 3468:                       '</fieldset></td></tr>';
 3469:         $count ++;
 3470:         $$rowtotal += $count;
 3471:     }
 3472:     return $datatable;
 3473: }
 3474: 
 3475: sub adhocbutton {
 3476:     my ($prefix,$num,$field,$visibility) = @_;
 3477:     my %lt = &Apache::lonlocal::texthash(
 3478:                                           show => 'Show details',
 3479:                                           hide => 'Hide details',
 3480:                                         );
 3481:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 3482:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 3483:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 3484:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 3485: }
 3486: 
 3487: sub helpsettings_javascript {
 3488:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 3489:     return unless(ref($roles_by_num) eq 'ARRAY');
 3490:     my %html_js_lt = &Apache::lonlocal::texthash(
 3491:                                           show => 'Show details',
 3492:                                           hide => 'Hide details',
 3493:                                         );
 3494:     &html_escape(\%html_js_lt);
 3495:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 3496:     return <<"ENDSCRIPT";
 3497: <script type="text/javascript">
 3498: // <![CDATA[
 3499: 
 3500: function reorderHelpRoles(form,item) {
 3501:     var changedVal;
 3502: $jstext
 3503:     var newpos = 'helproles_${total}_pos';
 3504:     var maxh = 1 + $total;
 3505:     var current = new Array();
 3506:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3507:     if (item == newpos) {
 3508:         changedVal = newitemVal;
 3509:     } else {
 3510:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3511:         current[newitemVal] = newpos;
 3512:     }
 3513:     for (var i=0; i<helproles.length; i++) {
 3514:         var elementName = 'helproles_'+helproles[i]+'_pos';
 3515:         if (elementName != item) {
 3516:             if (form.elements[elementName]) {
 3517:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3518:                 current[currVal] = elementName;
 3519:             }
 3520:         }
 3521:     }
 3522:     var oldVal;
 3523:     for (var j=0; j<maxh; j++) {
 3524:         if (current[j] == undefined) {
 3525:             oldVal = j;
 3526:         }
 3527:     }
 3528:     if (oldVal < changedVal) {
 3529:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3530:            var elementName = current[k];
 3531:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3532:         }
 3533:     } else {
 3534:         for (var k=changedVal; k<oldVal; k++) {
 3535:             var elementName = current[k];
 3536:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3537:         }
 3538:     }
 3539:     return;
 3540: }
 3541: 
 3542: function helpdeskAccess(num) {
 3543:     var curraccess = null;
 3544:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 3545:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 3546:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 3547:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 3548:             }
 3549:         }
 3550:     }
 3551:     var shown = Array();
 3552:     var hidden = Array();
 3553:     if (curraccess == 'none') {
 3554:         hidden = Array('$hiddenstr');
 3555:     } else {
 3556:         if (curraccess == 'status') {
 3557:             shown = Array('bystatus');
 3558:             hidden = Array('notinc','notexc');
 3559:         } else {
 3560:             if (curraccess == 'exc') {
 3561:                 shown = Array('notexc');
 3562:                 hidden = Array('notinc','bystatus');
 3563:             }
 3564:             if (curraccess == 'inc') {
 3565:                 shown = Array('notinc');
 3566:                 hidden = Array('notexc','bystatus');
 3567:             }
 3568:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 3569:                 hidden = Array('notinc','notexc','bystatus');
 3570:             }
 3571:         }
 3572:     }
 3573:     if (hidden.length > 0) {
 3574:         for (var i=0; i<hidden.length; i++) {
 3575:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 3576:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 3577:             }
 3578:         }
 3579:     }
 3580:     if (shown.length > 0) {
 3581:         for (var i=0; i<shown.length; i++) {
 3582:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 3583:                 if (shown[i] == 'privs') {
 3584:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 3585:                 } else {
 3586:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 3587:                 }
 3588:             }
 3589:         }
 3590:     }
 3591:     return;
 3592: }
 3593: 
 3594: function toggleHelpdeskItem(num,field) {
 3595:     if (document.getElementById('helproles_'+num+'_'+field)) {
 3596:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 3597:             document.getElementById('helproles_'+num+'_'+field).className =
 3598:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 3599:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3600:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 3601:             }
 3602:         } else {
 3603:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 3604:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3605:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 3606:             }
 3607:         }
 3608:     }
 3609:     return;
 3610: }
 3611: 
 3612: // ]]>
 3613: </script>
 3614: 
 3615: ENDSCRIPT
 3616: }
 3617: 
 3618: sub helpdeskroles_access {
 3619:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 3620:         $usertypes,$types,$domhelpdesk) = @_;
 3621:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 3622:     my %lt = &Apache::lonlocal::texthash(
 3623:                     'rou'    => 'Role usage',
 3624:                     'whi'    => 'Which helpdesk personnel may use this role?',
 3625:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 3626:                     'dh'     => 'All with domain helpdesk role',
 3627:                     'da'     => 'All with domain helpdesk assistant role',
 3628:                     'none'   => 'None',
 3629:                     'status' => 'Determined based on institutional status',
 3630:                     'inc'    => 'Include all, but exclude specific personnel',
 3631:                     'exc'    => 'Exclude all, but include specific personnel',
 3632:                   );
 3633:     my %usecheck = (
 3634:                      all => ' checked="checked"',
 3635:                    );
 3636:     my %displaydiv = (
 3637:                       status => 'none',
 3638:                       inc    => 'none',
 3639:                       exc    => 'none',
 3640:                       priv   => 'block',
 3641:                      );
 3642:     my $output;
 3643:     if (ref($current) eq 'HASH') {
 3644:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 3645:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 3646:                 $usecheck{$current->{access}} = $usecheck{'all'};
 3647:                 delete($usecheck{'all'});
 3648:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 3649:                     my $access = $1;
 3650:                     $displaydiv{$access} = 'inline';
 3651:                 } elsif ($current->{access} eq 'none') {
 3652:                     $displaydiv{'priv'} = 'none';
 3653:                 }
 3654:             }
 3655:         }
 3656:     }
 3657:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 3658:               '<p>'.$lt{'whi'}.'</p>';
 3659:     foreach my $access (@{$accesstypes}) {
 3660:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 3661:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 3662:                    $lt{$access}.'</label>';
 3663:         if ($access eq 'status') {
 3664:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 3665:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 3666:                                                                  $othertitle,$usertypes,$types).
 3667:                        '</div>';
 3668:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 3669:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 3670:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3671:                        '</div>';
 3672:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 3673:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 3674:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3675:                        '</div>';
 3676:         }
 3677:         $output .= '</p>';
 3678:     }
 3679:     $output .= '</fieldset>';
 3680:     return $output;
 3681: }
 3682: 
 3683: sub radiobutton_prefs {
 3684:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 3685:         $additional,$align) = @_;
 3686:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 3687:                    (ref($choices) eq 'HASH'));
 3688: 
 3689:     my (%checkedon,%checkedoff,$datatable,$css_class);
 3690: 
 3691:     foreach my $item (@{$toggles}) {
 3692:         if ($defaultchecked->{$item} eq 'on') {
 3693:             $checkedon{$item} = ' checked="checked" ';
 3694:             $checkedoff{$item} = ' ';
 3695:         } elsif ($defaultchecked->{$item} eq 'off') {
 3696:             $checkedoff{$item} = ' checked="checked" ';
 3697:             $checkedon{$item} = ' ';
 3698:         }
 3699:     }
 3700:     if (ref($settings) eq 'HASH') {
 3701:         foreach my $item (@{$toggles}) {
 3702:             if ($settings->{$item} eq '1') {
 3703:                 $checkedon{$item} =  ' checked="checked" ';
 3704:                 $checkedoff{$item} = ' ';
 3705:             } elsif ($settings->{$item} eq '0') {
 3706:                 $checkedoff{$item} =  ' checked="checked" ';
 3707:                 $checkedon{$item} = ' ';
 3708:             }
 3709:         }
 3710:     }
 3711:     if ($onclick) {
 3712:         $onclick = ' onclick="'.$onclick.'"';
 3713:     }
 3714:     foreach my $item (@{$toggles}) {
 3715:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3716:         $datatable .=
 3717:             '<tr'.$css_class.'><td valign="top">'.
 3718:             '<span class="LC_nobreak">'.$choices->{$item}.
 3719:             '</span></td>';
 3720:         if ($align eq 'left') {
 3721:             $datatable .= '<td class="LC_left_item">';
 3722:         } else {
 3723:             $datatable .= '<td class="LC_right_item">';
 3724:         }
 3725:         $datatable .=
 3726:             '<span class="LC_nobreak">'.
 3727:             '<label><input type="radio" name="'.
 3728:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 3729:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 3730:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 3731:             '</span>'.$additional.
 3732:             '</td>'.
 3733:             '</tr>';
 3734:         $itemcount ++;
 3735:     }
 3736:     return ($datatable,$itemcount);
 3737: }
 3738: 
 3739: sub print_ltitools {
 3740:     my ($dom,$settings,$rowtotal) = @_;
 3741:     my $rownum = 0;
 3742:     my $css_class;
 3743:     my $itemcount = 1;
 3744:     my $maxnum = 0;
 3745:     my %ordered;
 3746:     if (ref($settings) eq 'HASH') {
 3747:         foreach my $item (keys(%{$settings})) {
 3748:             if (ref($settings->{$item}) eq 'HASH') {
 3749:                 my $num = $settings->{$item}{'order'};
 3750:                 $ordered{$num} = $item;
 3751:             }
 3752:         }
 3753:     }
 3754:     my $confname = $dom.'-domainconfig';
 3755:     my $switchserver = &check_switchserver($dom,$confname);
 3756:     my $maxnum = scalar(keys(%ordered));
 3757:     my $datatable = &ltitools_javascript($settings);
 3758:     my %lt = &ltitools_names();
 3759:     my @courseroles = ('cc','in','ta','ep','st');
 3760:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 3761:     my @fields = ('fullname','firstname','lastname','email','user','roles');
 3762:     if (keys(%ordered)) {
 3763:         my @items = sort { $a <=> $b } keys(%ordered);
 3764:         for (my $i=0; $i<@items; $i++) {
 3765:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3766:             my $item = $ordered{$items[$i]};
 3767:             my ($title,$key,$secret,$url,$imgsrc,$version);
 3768:             if (ref($settings->{$item}) eq 'HASH') {
 3769:                 $title = $settings->{$item}->{'title'};
 3770:                 $url = $settings->{$item}->{'url'};
 3771:                 $key = $settings->{$item}->{'key'};
 3772:                 $secret = $settings->{$item}->{'secret'};
 3773:                 my $image = $settings->{$item}->{'image'};
 3774:                 if ($image ne '') {
 3775:                     $imgsrc = '<img src="'.$image.'" alt="'.&mt('Tool Provider icon').'" />';
 3776:                 }
 3777:             }
 3778:             my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'ltitools_".$item."'".');"';
 3779:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 3780:                          .'<select name="ltitools_'.$item.'"'.$chgstr.'>';
 3781:             for (my $k=0; $k<=$maxnum; $k++) {
 3782:                 my $vpos = $k+1;
 3783:                 my $selstr;
 3784:                 if ($k == $i) {
 3785:                     $selstr = ' selected="selected" ';
 3786:                 }
 3787:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3788:             }
 3789:             $datatable .= '</select>'.('&nbsp;'x2).
 3790:                 '<label><input type="checkbox" name="ltitools_del" value="'.$item.'" />'.
 3791:                 &mt('Delete?').'</label></span></td>'.
 3792:                 '<td colspan="2">'.
 3793:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 3794:                 '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="30" name="ltitools_title_'.$i.'" value="'.$title.'" /></span> '.
 3795:                 ('&nbsp;'x2).
 3796:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_version_'.$i.'">'.
 3797:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 3798:                 ('&nbsp;'x2).
 3799:                 '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_msgtype_'.$i.'">'.
 3800:                 '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 3801:                 '<br /><br />'.
 3802:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="30" name="ltitools_url_'.$i.'"'.
 3803:                 ' value="'.$url.'" /></span>'.
 3804:                 ('&nbsp;'x2).
 3805:                 '<span class="LC_nobreak">'.$lt{'key'}.
 3806:                 '<input type="text" size="25" name="ltitools_key_'.$i.'" value="'.$key.'" /></span> '.
 3807:                 ('&nbsp;'x2).
 3808:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 3809:                 '<input type="password" size="20" name="ltitools_secret_'.$i.'" value="'.$secret.'" />'.
 3810:                 '<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>'.
 3811:                 '<input type="hidden" name="ltitools_id_'.$i.'" value="'.$item.'" /></span>'.
 3812:                 '</fieldset>'.
 3813:                 '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 3814:                 '<span class="LC_nobreak">'.&mt('Display target:');
 3815:             my %currdisp;
 3816:             if (ref($settings->{$item}->{'display'}) eq 'HASH') {
 3817:                 if ($settings->{$item}->{'display'}->{'target'} eq 'window') {
 3818:                     $currdisp{'window'} = ' checked="checked"';
 3819:                 } elsif ($settings->{$item}->{'display'}->{'target'} eq 'tab') {
 3820:                     $currdisp{'tab'} = ' checked="checked"';
 3821:                 } else {
 3822:                     $currdisp{'iframe'} = ' checked="checked"';
 3823:                 }
 3824:                 if ($settings->{$item}->{'display'}->{'width'} =~ /^(\d+)$/) {
 3825:                     $currdisp{'width'} = $1;
 3826:                 }
 3827:                 if ($settings->{$item}->{'display'}->{'height'} =~ /^(\d+)$/) {
 3828:                      $currdisp{'height'} = $1;
 3829:                 }
 3830:                 $currdisp{'linktext'} = $settings->{$item}->{'display'}->{'linktext'};
 3831:                 $currdisp{'explanation'} = $settings->{$item}->{'display'}->{'explanation'};
 3832:             } else {
 3833:                 $currdisp{'iframe'} = ' checked="checked"';
 3834:             }
 3835:             foreach my $disp ('iframe','tab','window') {
 3836:                 $datatable .= '<label><input type="radio" name="ltitools_target_'.$i.'" value="'.$disp.'"'.$currdisp{$disp}.' />'.
 3837:                               $lt{$disp}.'</label>'.('&nbsp;'x2);
 3838:             }
 3839:             $datatable .= ('&nbsp;'x4);
 3840:             foreach my $dimen ('width','height') {
 3841:                 $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 3842:                               '<input type="text" name="ltitools_'.$dimen.'_'.$i.'" size="5" value="'.$currdisp{$dimen}.'" /></label>'.
 3843:                               ('&nbsp;'x2);
 3844:             }
 3845:             $datatable .= '<br />'.
 3846:                           '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 3847:                           '<input type="text" name="ltitools_linktext_'.$i.'" size="25" value="'.$currdisp{'linktext'}.'" /></label></div>'.
 3848:                           '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 3849:                           '<textarea name="ltitools_explanation_'.$i.'" rows="5" cols="40">'.$currdisp{'explanation'}.
 3850:                           '</textarea></div><div style=""></div><br />';
 3851:             $datatable .= '<br />';
 3852:             foreach my $extra ('passback','roster') {
 3853:                 my $checkedon = '';
 3854:                 my $checkedoff = ' checked="checked"';
 3855:                 if ($settings->{$item}->{$extra}) {
 3856:                     $checkedon = $checkedoff;
 3857:                     $checkedoff = '';
 3858:                 }
 3859:                 $datatable .= $lt{$extra}.'&nbsp;'.
 3860:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="1"'.$checkedon.' />'.
 3861:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 3862:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="0"'.$checkedoff.' />'.
 3863:                               &mt('No').'</label>'.('&nbsp;'x4);
 3864:             }
 3865:             $datatable .= '<br /><br /><span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 3866:             if ($imgsrc) {
 3867:                 $datatable .= $imgsrc.
 3868:                               '<label><input type="checkbox" name="ltitools_image_del"'.
 3869:                               ' value="'.$item.'" />'.&mt('Delete?').'</label></span> '.
 3870:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 3871:             } else {
 3872:                 $datatable .= '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 3873:             }
 3874:             if ($switchserver) {
 3875:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3876:             } else {
 3877:                 $datatable .= '<input type="file" name="ltitools_image_'.$i.'" value="" />';
 3878:             }
 3879:             $datatable .= '</span></fieldset>';
 3880:             my (%checkedfields,%rolemaps);
 3881:             if (ref($settings->{$item}) eq 'HASH') {
 3882:                 if (ref($settings->{$item}->{'fields'}) eq 'HASH') {
 3883:                     %checkedfields = %{$settings->{$item}->{'fields'}};
 3884:                 }
 3885:                 if (ref($settings->{$item}->{'roles'}) eq 'HASH') {
 3886:                     %rolemaps = %{$settings->{$item}->{'roles'}};
 3887:                     $checkedfields{'roles'} = 1;
 3888:                 }
 3889:             }
 3890:             $datatable .= '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 3891:                           '<span class="LC_nobreak">';
 3892:             foreach my $field (@fields) {
 3893:                 my $checked;
 3894:                 if ($checkedfields{$field}) {
 3895:                     $checked = ' checked="checked"';
 3896:                 }
 3897:                 $datatable .= '<label>'.
 3898:                               '<input type="checkbox" name="ltitools_fields_'.$i.'" value="'.$field.'"'.$checked.' />'.
 3899:                               $lt{$field}.'</label>'.('&nbsp;' x2);
 3900:             }
 3901:             $datatable .= '</span></fieldset>'.
 3902:                           '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 3903:             foreach my $role (@courseroles) {
 3904:                 my ($selected,$selectnone);
 3905:                 if (!$rolemaps{$role}) {
 3906:                     $selectnone = ' selected="selected"';
 3907:                 }
 3908:                 $datatable .= '<td align="center">'.
 3909:                               &Apache::lonnet::plaintext($role,'Course').'<br />'.
 3910:                               '<select name="ltitools_roles_'.$role.'_'.$i.'">'.
 3911:                               '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 3912:                 foreach my $ltirole (@ltiroles) {
 3913:                     unless ($selectnone) {
 3914:                         if ($rolemaps{$role} eq $ltirole) {
 3915:                             $selected = ' selected="selected"';
 3916:                         } else {
 3917:                             $selected = '';
 3918:                         }
 3919:                     }
 3920:                     $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 3921:                 }
 3922:                 $datatable .= '</select></td>';
 3923:             }
 3924:             $datatable .= '</tr></table></fieldset>';
 3925:             my %courseconfig;
 3926:             if (ref($settings->{$item}) eq 'HASH') {
 3927:                 if (ref($settings->{$item}->{'crsconf'}) eq 'HASH') {
 3928:                     %courseconfig = %{$settings->{$item}->{'crsconf'}};
 3929:                 }
 3930:             }
 3931:             $datatable .= '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 3932:             foreach my $item ('label','title','target','linktext','explanation') {
 3933:                 my $checked;
 3934:                 if ($courseconfig{$item}) {
 3935:                     $checked = ' checked="checked"';
 3936:                 }
 3937:                 $datatable .= '<label>'.
 3938:                        '<input type="checkbox" name="ltitools_courseconfig_'.$i.'" value="'.$item.'"'.$checked.' />'.
 3939:                        $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 3940:             }
 3941:             $datatable .= '</span></fieldset>'.
 3942:                           '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 3943:                           '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>';
 3944:             if (ref($settings->{$item}->{'custom'}) eq 'HASH') {
 3945:                 my %custom = %{$settings->{$item}->{'custom'}};
 3946:                 if (keys(%custom) > 0) {
 3947:                     foreach my $key (sort(keys(%custom))) {
 3948:                         $datatable .= '<tr><td><span class="LC_nobreak">'.
 3949:                                       '<label><input type="checkbox" name="ltitools_customdel_'.$i.'" value="'.
 3950:                                       $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 3951:                                       '<td><input type="text" name="ltitools_customval_'.$key.'_'.$i.'"'.
 3952:                                       ' value="'.$custom{$key}.'" /></td></tr>';
 3953:                     }
 3954:                 }
 3955:             }
 3956:             $datatable .= '<tr><td><span class="LC_nobreak">'.
 3957:                           '<label><input type="checkbox" name="ltitools_customadd" value="'.$i.'" />'.
 3958:                           &mt('Add').'</label></span></td><td><input type="text" name="ltitools_custom_name_'.$i.'" />'.
 3959:                           '</td><td><input type="text" name="ltitools_custom_value_'.$i.'" /></td></tr>';
 3960:             $datatable .= '</table></fieldset></td></tr>'."\n";
 3961:             $itemcount ++;
 3962:         }
 3963:     }
 3964:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3965:     my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'ltitools_add_pos'".');"';
 3966:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 3967:                   '<input type="hidden" name="ltitools_maxnum" value="'.$maxnum.'" />'."\n".
 3968:                   '<select name="ltitools_add_pos"'.$chgstr.'>';
 3969:     for (my $k=0; $k<$maxnum+1; $k++) {
 3970:         my $vpos = $k+1;
 3971:         my $selstr;
 3972:         if ($k == $maxnum) {
 3973:             $selstr = ' selected="selected" ';
 3974:         }
 3975:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3976:     }
 3977:     $datatable .= '</select>&nbsp;'."\n".
 3978:                   '<input type="checkbox" name="ltitools_add" value="1" />'.&mt('Add').'</td>'."\n".
 3979:                   '<td colspan="2">'.
 3980:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 3981:                   '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="30" name="ltitools_add_title" value="" /></span> '."\n".
 3982:                   ('&nbsp;'x2).
 3983:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_add_version">'.
 3984:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 3985:                   ('&nbsp;'x2).
 3986:                   '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_add_msgtype">'.
 3987:                   '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 3988:                   '<br />'.
 3989:                   '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="30" name="ltitools_add_url" value="" /></span> '."\n".
 3990:                   ('&nbsp;'x2).
 3991:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="ltitools_add_key" value="" /></span> '."\n".
 3992:                   ('&nbsp;'x2).
 3993:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="ltitools_add_secret" value="" />'.
 3994:                   '<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".
 3995:                   '</fieldset>'.
 3996:                   '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 3997:                   '<span class="LC_nobreak">'.&mt('Display target:');
 3998:     my %defaultdisp;
 3999:     $defaultdisp{'iframe'} = ' checked="checked"';
 4000:     foreach my $disp ('iframe','tab','window') {
 4001:         $datatable .= '<label><input type="radio" name="ltitools_add_target" value="'.$disp.'"'.$defaultdisp{$disp}.' />'.
 4002:                       $lt{$disp}.'</label>'.('&nbsp;'x2);
 4003:     }
 4004:     $datatable .= ('&nbsp;'x4);
 4005:     foreach my $dimen ('width','height') {
 4006:         $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 4007:                       '<input type="text" name="ltitools_add_'.$dimen.'" size="5" /></label>'.
 4008:                       ('&nbsp;'x2);
 4009:     }
 4010:     $datatable .= '<br />'.
 4011:                   '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 4012:                   '<input type="text" name="ltitools_add_linktext" size="5" /></label></div>'.
 4013:                   '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 4014:                   '<textarea name=ltitools_add_explanation" rows="5" cols="40"></textarea>'.
 4015:                   '</div><div style=""></div><br />';
 4016:     foreach my $extra ('passback','roster') {
 4017:         $datatable .= $lt{$extra}.'&nbsp;'.
 4018:                       '<label><input type="radio" name="ltitools_add_'.$extra.'" value="1" />'.
 4019:                       &mt('Yes').'</label>'.('&nbsp;'x2).
 4020:                       '<label><input type="radio" name="ltitools_add_'.$extra.'" value="0" checked="checked" />'.
 4021:                       &mt('No').'</label>'.('&nbsp;'x4);
 4022:     }
 4023:     $datatable .= '<br /><br /><span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;'.
 4024:                   '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 4025:     if ($switchserver) {
 4026:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4027:     } else {
 4028:         $datatable .= '<input type="file" name="ltitools_add_image" value="" />';
 4029:     }
 4030:     $datatable .= '</span></fieldset>'.
 4031:                   '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 4032:                   '<span class="LC_nobreak">';
 4033:     foreach my $field (@fields) {
 4034:         $datatable .= '<label>'.
 4035:                       '<input type="checkbox" name="ltitools_add_fields" value="'.$field.'" />'.
 4036:                       $lt{$field}.'</label>'.('&nbsp;' x2);
 4037:     }
 4038:     $datatable .= '</span></fieldset>'.
 4039:                   '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 4040:     foreach my $role (@courseroles) {
 4041:         my ($checked,$checkednone);
 4042:         $datatable .= '<td align="center">'.
 4043:                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 4044:                       '<select name="ltitools_add_roles_'.$role.'">'.
 4045:                       '<option value="" selected="selected">'.&mt('Select').'</option>';
 4046:         foreach my $ltirole (@ltiroles) {
 4047:             $datatable .= '<option value="'.$ltirole.'">'.$ltirole.'</option>';
 4048:         }
 4049:         $datatable .= '</select></td>';
 4050:     }
 4051:     $datatable .= '</tr></table></fieldset>'.
 4052:                   '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 4053:     foreach my $item ('label','title','target','linktext','explanation') {
 4054:         $datatable .= '<label>'.
 4055:                       '<input type="checkbox" name="ltitools_courseconfig" value="'.$item.'" checked="checked" />'.
 4056:                       $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 4057:     }
 4058:     $datatable .= '</span></fieldset>'.
 4059:                   '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 4060:                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 4061:                   '<tr><td><span class="LC_nobreak">'.
 4062:                   '<label><input type="checkbox" name="ltitools_add_custom" value="1" />'.
 4063:                   &mt('Add').'</label></span></td><td><input type="text" name="ltitools_add_custom_name" />'.
 4064:                   '</td><td><input type="text" name="ltitools_add_custom_value" /></td></tr>'.
 4065:                   '</table></fieldset></td></tr>'."\n".
 4066:                   '</td>'."\n".
 4067:                   '</tr>'."\n";
 4068:     $itemcount ++;
 4069:     return $datatable;
 4070: }
 4071: 
 4072: sub ltitools_names {
 4073:     my %lt = &Apache::lonlocal::texthash(
 4074:                                           'title'          => 'Title',
 4075:                                           'version'        => 'Version',
 4076:                                           'msgtype'        => 'Message Type',
 4077:                                           'url'            => 'URL',
 4078:                                           'key'            => 'Key',
 4079:                                           'secret'         => 'Secret',
 4080:                                           'icon'           => 'Icon',
 4081:                                           'user'           => 'Username:domain',
 4082:                                           'fullname'       => 'Full Name',
 4083:                                           'firstname'      => 'First Name',
 4084:                                           'lastname'       => 'Last Name',
 4085:                                           'email'          => 'E-mail',
 4086:                                           'roles'          => 'Role',
 4087:                                           'window'         => 'Window',
 4088:                                           'tab'            => 'Tab',
 4089:                                           'iframe'         => 'iFrame',
 4090:                                           'height'         => 'Height',
 4091:                                           'width'          => 'Width',
 4092:                                           'linktext'       => 'Default Link Text',
 4093:                                           'explanation'    => 'Default Explanation',
 4094:                                           'passback'       => 'Tool can return grades:',
 4095:                                           'roster'         => 'Tool can retrieve roster:',
 4096:                                           'crstarget'      => 'Display target',
 4097:                                           'crslabel'       => 'Course label',
 4098:                                           'crstitle'       => 'Course title',
 4099:                                           'crslinktext'    => 'Link Text',
 4100:                                           'crsexplanation' => 'Explanation',
 4101:                                         );
 4102: 
 4103:     return %lt;
 4104: }
 4105: 
 4106: sub print_coursedefaults {
 4107:     my ($position,$dom,$settings,$rowtotal) = @_;
 4108:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 4109:     my $itemcount = 1;
 4110:     my %choices =  &Apache::lonlocal::texthash (
 4111:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 4112:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 4113:         coursecredits        => 'Credits can be specified for courses',
 4114:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 4115:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 4116:         postsubmit           => 'Disable submit button/keypress following student submission',
 4117:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 4118:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 4119:     );
 4120:     my %staticdefaults = (
 4121:                            anonsurvey_threshold => 10,
 4122:                            uploadquota          => 500,
 4123:                            postsubmit           => 60,
 4124:                            mysqltables          => 172800,
 4125:                          );
 4126:     if ($position eq 'top') {
 4127:         %defaultchecked = (
 4128:                             'uselcmath'       => 'on',
 4129:                             'usejsme'         => 'on',
 4130:                             'canclone'        => 'none',
 4131:                           );
 4132:         @toggles = ('uselcmath','usejsme');
 4133:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4134:                                                      \%choices,$itemcount);
 4135:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4136:         $datatable .=
 4137:             '<tr'.$css_class.'><td valign="top">'.
 4138:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 4139:             '</span></td><td class="LC_left_item">';
 4140:         my $currcanclone = 'none';
 4141:         my $onclick;
 4142:         my @cloneoptions = ('none','domain');
 4143:         my %clonetitles = (
 4144:                              none     => 'No additional course requesters',
 4145:                              domain   => "Any course requester in course's domain",
 4146:                              instcode => 'Course requests for official courses ...',
 4147:                           );
 4148:         my (%codedefaults,@code_order,@posscodes);
 4149:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 4150:                                                     \@code_order) eq 'ok') {
 4151:             if (@code_order > 0) {
 4152:                 push(@cloneoptions,'instcode');
 4153:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 4154:             }
 4155:         }
 4156:         if (ref($settings) eq 'HASH') {
 4157:             if ($settings->{'canclone'}) {
 4158:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 4159:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 4160:                         if (@code_order > 0) {
 4161:                             $currcanclone = 'instcode';
 4162:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 4163:                         }
 4164:                     }
 4165:                 } elsif ($settings->{'canclone'} eq 'domain') {
 4166:                     $currcanclone = $settings->{'canclone'};
 4167:                 }
 4168:             }
 4169:         }
 4170:         foreach my $option (@cloneoptions) {
 4171:             my ($checked,$additional);
 4172:             if ($currcanclone eq $option) {
 4173:                 $checked = ' checked="checked"';
 4174:             }
 4175:             if ($option eq 'instcode') {
 4176:                 if (@code_order) {
 4177:                     my $show = 'none';
 4178:                     if ($checked) {
 4179:                         $show = 'block';
 4180:                     }
 4181:                     $additional = '<div id="cloneinstcode" style="display:'.$show.'" />'.
 4182:                                   &mt('Institutional codes for new and cloned course have identical:').
 4183:                                   '<br />';
 4184:                     foreach my $item (@code_order) {
 4185:                         my $codechk;
 4186:                         if ($checked) {
 4187:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 4188:                                 $codechk = ' checked="checked"';
 4189:                             }
 4190:                         }
 4191:                         $additional .= '<label>'.
 4192:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 4193:                                        $item.'</label>';
 4194:                     }
 4195:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 4196:                 }
 4197:             }
 4198:             $datatable .=
 4199:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 4200:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 4201:                 '</label>&nbsp;'.$additional.'</span><br />';
 4202:         }
 4203:         $datatable .= '</td>'.
 4204:                       '</tr>';
 4205:         $itemcount ++;
 4206:     } else {
 4207:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4208:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 4209:         my $currusecredits = 0;
 4210:         my $postsubmitclient = 1;
 4211:         my @types = ('official','unofficial','community','textbook');
 4212:         if (ref($settings) eq 'HASH') {
 4213:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 4214:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 4215:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 4216:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 4217:                 }
 4218:             }
 4219:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 4220:                 foreach my $type (@types) {
 4221:                     next if ($type eq 'community');
 4222:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 4223:                     if ($defcredits{$type} ne '') {
 4224:                         $currusecredits = 1;
 4225:                     }
 4226:                 }
 4227:             }
 4228:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 4229:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 4230:                     $postsubmitclient = 0;
 4231:                     foreach my $type (@types) {
 4232:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4233:                     }
 4234:                 } else {
 4235:                     foreach my $type (@types) {
 4236:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 4237:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 4238:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 4239:                             } else {
 4240:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4241:                             }
 4242:                         } else {
 4243:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4244:                         }
 4245:                     }
 4246:                 }
 4247:             } else {
 4248:                 foreach my $type (@types) {
 4249:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4250:                 }
 4251:             }
 4252:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 4253:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 4254:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 4255:                 }
 4256:             } else {
 4257:                 foreach my $type (@types) {
 4258:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 4259:                 }
 4260:             }
 4261:         } else {
 4262:             foreach my $type (@types) {
 4263:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4264:             }
 4265:         }
 4266:         if (!$currdefresponder) {
 4267:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 4268:         } elsif ($currdefresponder < 1) {
 4269:             $currdefresponder = 1;
 4270:         }
 4271:         foreach my $type (@types) {
 4272:             if ($curruploadquota{$type} eq '') {
 4273:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 4274:             }
 4275:         }
 4276:         $datatable .=
 4277:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4278:                 $choices{'anonsurvey_threshold'}.
 4279:                 '</span></td>'.
 4280:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 4281:                 '<input type="text" name="anonsurvey_threshold"'.
 4282:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 4283:                 '</td></tr>'."\n";
 4284:         $itemcount ++;
 4285:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4286:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4287:                       $choices{'uploadquota'}.
 4288:                       '</span></td>'.
 4289:                       '<td align="right" class="LC_right_item">'.
 4290:                       '<table><tr>';
 4291:         foreach my $type (@types) {
 4292:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 4293:                            '<input type="text" name="uploadquota_'.$type.'"'.
 4294:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 4295:         }
 4296:         $datatable .= '</tr></table></td></tr>'."\n";
 4297:         $itemcount ++;
 4298:         my $onclick = "toggleDisplay(this.form,'credits');";
 4299:         my $display = 'none';
 4300:         if ($currusecredits) {
 4301:             $display = 'block';
 4302:         }
 4303:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 4304:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 4305:         foreach my $type (@types) {
 4306:             next if ($type eq 'community');
 4307:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 4308:                            '<input type="text" name="'.$type.'_credits"'.
 4309:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 4310:         }
 4311:         $additional .= '</tr></table></div>'."\n";
 4312:         %defaultchecked = ('coursecredits' => 'off');
 4313:         @toggles = ('coursecredits');
 4314:         my $current = {
 4315:                         'coursecredits' => $currusecredits,
 4316:                       };
 4317:         (my $table,$itemcount) =
 4318:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 4319:                                \%choices,$itemcount,$onclick,$additional,'left');
 4320:         $datatable .= $table;
 4321:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 4322:         my $display = 'none';
 4323:         if ($postsubmitclient) {
 4324:             $display = 'block';
 4325:         }
 4326:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 4327:                       &mt('Number of seconds submit is disabled').'<br />'.
 4328:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 4329:                       '<table><tr>';
 4330:         foreach my $type (@types) {
 4331:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 4332:                            '<input type="text" name="'.$type.'_timeout" value="'.
 4333:                            $deftimeout{$type}.'" size="5" /></td>';
 4334:         }
 4335:         $additional .= '</tr></table></div>'."\n";
 4336:         %defaultchecked = ('postsubmit' => 'on');
 4337:         @toggles = ('postsubmit');
 4338:         $current = {
 4339:                        'postsubmit' => $postsubmitclient,
 4340:                    };
 4341:         ($table,$itemcount) =
 4342:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 4343:                                \%choices,$itemcount,$onclick,$additional,'left');
 4344:         $datatable .= $table;
 4345:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4346:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4347:                       $choices{'mysqltables'}.
 4348:                       '</span></td>'.
 4349:                       '<td align="right" class="LC_right_item">'.
 4350:                       '<table><tr>';
 4351:         foreach my $type (@types) {
 4352:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 4353:                            '<input type="text" name="mysqltables_'.$type.'"'.
 4354:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 4355:         }
 4356:         $datatable .= '</tr></table></td></tr>'."\n";
 4357:         $itemcount ++;
 4358: 
 4359:     }
 4360:     $$rowtotal += $itemcount;
 4361:     return $datatable;
 4362: }
 4363: 
 4364: sub print_selfenrollment {
 4365:     my ($position,$dom,$settings,$rowtotal) = @_;
 4366:     my ($css_class,$datatable);
 4367:     my $itemcount = 1;
 4368:     my @types = ('official','unofficial','community','textbook');
 4369:     if (($position eq 'top') || ($position eq 'middle')) {
 4370:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 4371:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 4372:         my @rows;
 4373:         my $key;
 4374:         if ($position eq 'top') {
 4375:             $key = 'admin'; 
 4376:             if (ref($rowsref) eq 'ARRAY') {
 4377:                 @rows = @{$rowsref};
 4378:             }
 4379:         } elsif ($position eq 'middle') {
 4380:             $key = 'default';
 4381:             @rows = ('types','registered','approval','limit');
 4382:         }
 4383:         foreach my $row (@rows) {
 4384:             if (defined($titlesref->{$row})) {
 4385:                 $itemcount ++;
 4386:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4387:                 $datatable .= '<tr'.$css_class.'>'.
 4388:                               '<td>'.$titlesref->{$row}.'</td>'.
 4389:                               '<td class="LC_left_item">'.
 4390:                               '<table><tr>';
 4391:                 my (%current,%currentcap);
 4392:                 if (ref($settings) eq 'HASH') {
 4393:                     if (ref($settings->{$key}) eq 'HASH') {
 4394:                         foreach my $type (@types) {
 4395:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4396:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 4397:                             }
 4398:                             if (($row eq 'limit') && ($key eq 'default')) {
 4399:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4400:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 4401:                                 }
 4402:                             }
 4403:                         }
 4404:                     }
 4405:                 }
 4406:                 my %roles = (
 4407:                              '0' => &Apache::lonnet::plaintext('dc'),
 4408:                             ); 
 4409:             
 4410:                 foreach my $type (@types) {
 4411:                     unless (($row eq 'registered') && ($key eq 'default')) {
 4412:                         $datatable .= '<th>'.&mt($type).'</th>';
 4413:                     }
 4414:                 }
 4415:                 unless (($row eq 'registered') && ($key eq 'default')) {
 4416:                     $datatable .= '</tr><tr>';
 4417:                 }
 4418:                 foreach my $type (@types) {
 4419:                     if ($type eq 'community') {
 4420:                         $roles{'1'} = &mt('Community personnel');
 4421:                     } else {
 4422:                         $roles{'1'} = &mt('Course personnel');
 4423:                     }
 4424:                     $datatable .= '<td style="vertical-align: top">';
 4425:                     if ($position eq 'top') {
 4426:                         my %checked;
 4427:                         if ($current{$type} eq '0') {
 4428:                             $checked{'0'} = ' checked="checked"';
 4429:                         } else {
 4430:                             $checked{'1'} = ' checked="checked"';
 4431:                         }
 4432:                         foreach my $role ('1','0') {
 4433:                             $datatable .= '<span class="LC_nobreak"><label>'.
 4434:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 4435:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 4436:                                           $roles{$role}.'</label></span> ';
 4437:                         }
 4438:                     } else {
 4439:                         if ($row eq 'types') {
 4440:                             my %checked;
 4441:                             if ($current{$type} =~ /^(all|dom)$/) {
 4442:                                 $checked{$1} = ' checked="checked"';
 4443:                             } else {
 4444:                                 $checked{''} = ' checked="checked"';
 4445:                             }
 4446:                             foreach my $val ('','dom','all') {
 4447:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4448:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4449:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4450:                             }
 4451:                         } elsif ($row eq 'registered') {
 4452:                             my %checked;
 4453:                             if ($current{$type} eq '1') {
 4454:                                 $checked{'1'} = ' checked="checked"';
 4455:                             } else {
 4456:                                 $checked{'0'} = ' checked="checked"';
 4457:                             }
 4458:                             foreach my $val ('0','1') {
 4459:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4460:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4461:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4462:                             }
 4463:                         } elsif ($row eq 'approval') {
 4464:                             my %checked;
 4465:                             if ($current{$type} =~ /^([12])$/) {
 4466:                                 $checked{$1} = ' checked="checked"';
 4467:                             } else {
 4468:                                 $checked{'0'} = ' checked="checked"';
 4469:                             }
 4470:                             for my $val (0..2) {
 4471:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4472:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4473:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4474:                             }
 4475:                         } elsif ($row eq 'limit') {
 4476:                             my %checked;
 4477:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 4478:                                 $checked{$1} = ' checked="checked"';
 4479:                             } else {
 4480:                                 $checked{'none'} = ' checked="checked"';
 4481:                             }
 4482:                             my $cap;
 4483:                             if ($currentcap{$type} =~ /^\d+$/) {
 4484:                                 $cap = $currentcap{$type};
 4485:                             }
 4486:                             foreach my $val ('none','allstudents','selfenrolled') {
 4487:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4488:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4489:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4490:                             }
 4491:                             $datatable .= '<br />'.
 4492:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 4493:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 4494:                                           '</span>'; 
 4495:                         }
 4496:                     }
 4497:                     $datatable .= '</td>';
 4498:                 }
 4499:                 $datatable .= '</tr>';
 4500:             }
 4501:             $datatable .= '</table></td></tr>';
 4502:         }
 4503:     } elsif ($position eq 'bottom') {
 4504:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 4505:     }
 4506:     $$rowtotal += $itemcount;
 4507:     return $datatable;
 4508: }
 4509: 
 4510: sub print_validation_rows {
 4511:     my ($caller,$dom,$settings,$rowtotal) = @_;
 4512:     my ($itemsref,$namesref,$fieldsref);
 4513:     if ($caller eq 'selfenroll') { 
 4514:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 4515:     } elsif ($caller eq 'requestcourses') {
 4516:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 4517:     }
 4518:     my %currvalidation;
 4519:     if (ref($settings) eq 'HASH') {
 4520:         if (ref($settings->{'validation'}) eq 'HASH') {
 4521:             %currvalidation = %{$settings->{'validation'}};
 4522:         }
 4523:     }
 4524:     my $datatable;
 4525:     my $itemcount = 0;
 4526:     foreach my $item (@{$itemsref}) {
 4527:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4528:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4529:                       $namesref->{$item}.
 4530:                       '</span></td>'.
 4531:                       '<td class="LC_left_item">';
 4532:         if (($item eq 'url') || ($item eq 'button')) {
 4533:             $datatable .= '<span class="LC_nobreak">'.
 4534:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 4535:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 4536:         } elsif ($item eq 'fields') {
 4537:             my @currfields;
 4538:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 4539:                 @currfields = @{$currvalidation{$item}};
 4540:             }
 4541:             foreach my $field (@{$fieldsref}) {
 4542:                 my $check = '';
 4543:                 if (grep(/^\Q$field\E$/,@currfields)) {
 4544:                     $check = ' checked="checked"';
 4545:                 }
 4546:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4547:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 4548:                               ' value="'.$field.'"'.$check.' />'.$field.
 4549:                               '</label></span> ';
 4550:             }
 4551:         } elsif ($item eq 'markup') {
 4552:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5" wrap="soft">'.
 4553:                            $currvalidation{$item}.
 4554:                               '</textarea>';
 4555:         }
 4556:         $datatable .= '</td></tr>'."\n";
 4557:         if (ref($rowtotal)) {
 4558:             $itemcount ++;
 4559:         }
 4560:     }
 4561:     if ($caller eq 'requestcourses') {
 4562:         my %currhash;
 4563:         if (ref($settings) eq 'HASH') {
 4564:             if (ref($settings->{'validation'}) eq 'HASH') {
 4565:                 if ($settings->{'validation'}{'dc'} ne '') {
 4566:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 4567:                 }
 4568:             }
 4569:         }
 4570:         my $numinrow = 2;
 4571:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 4572:                                                        'validationdc',%currhash);
 4573:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4574:         $datatable .= '</td></tr><tr'.$css_class.'><td>';
 4575:         if ($numdc > 1) {
 4576:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 4577:         } else {
 4578:             $datatable .=  &mt('Course creation processed as: ');
 4579:         }
 4580:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 4581:         $itemcount ++;
 4582:     }
 4583:     if (ref($rowtotal)) {
 4584:         $$rowtotal += $itemcount;
 4585:     }
 4586:     return $datatable;
 4587: }
 4588: 
 4589: sub print_usersessions {
 4590:     my ($position,$dom,$settings,$rowtotal) = @_;
 4591:     my ($css_class,$datatable,%checked,%choices);
 4592:     my (%by_ip,%by_location,@intdoms);
 4593:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 4594: 
 4595:     my @alldoms = &Apache::lonnet::all_domains();
 4596:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 4597:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4598:     my %altids = &id_for_thisdom(%servers);
 4599:     my $itemcount = 1;
 4600:     if ($position eq 'top') {
 4601:         if (keys(%serverhomes) > 1) {
 4602:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 4603:             my $curroffloadnow;
 4604:             if (ref($settings) eq 'HASH') {
 4605:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 4606:                     $curroffloadnow = $settings->{'offloadnow'};
 4607:                 }
 4608:             }
 4609:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$curroffloadnow,$rowtotal);
 4610:         } else {
 4611:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 4612:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 4613:         }
 4614:     } else {
 4615:         if (keys(%by_location) == 0) {
 4616:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 4617:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 4618:         } else {
 4619:             my %lt = &usersession_titles();
 4620:             my $numinrow = 5;
 4621:             my $prefix;
 4622:             my @types;
 4623:             if ($position eq 'bottom') {
 4624:                 $prefix = 'remote';
 4625:                 @types = ('version','excludedomain','includedomain');
 4626:             } else {
 4627:                 $prefix = 'hosted';
 4628:                 @types = ('excludedomain','includedomain');
 4629:             }
 4630:             my (%current,%checkedon,%checkedoff);
 4631:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 4632:             my @locations = sort(keys(%by_location));
 4633:             foreach my $type (@types) {
 4634:                 $checkedon{$type} = '';
 4635:                 $checkedoff{$type} = ' checked="checked"';
 4636:             }
 4637:             if (ref($settings) eq 'HASH') {
 4638:                 if (ref($settings->{$prefix}) eq 'HASH') {
 4639:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 4640:                         $current{$key} = $settings->{$prefix}{$key};
 4641:                         if ($key eq 'version') {
 4642:                             if ($current{$key} ne '') {
 4643:                                 $checkedon{$key} = ' checked="checked"';
 4644:                                 $checkedoff{$key} = '';
 4645:                             }
 4646:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 4647:                             $checkedon{$key} = ' checked="checked"';
 4648:                             $checkedoff{$key} = '';
 4649:                         }
 4650:                     }
 4651:                 }
 4652:             }
 4653:             foreach my $type (@types) {
 4654:                 next if ($type ne 'version' && !@locations);
 4655:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4656:                 $datatable .= '<tr'.$css_class.'>
 4657:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 4658:                                <span class="LC_nobreak">&nbsp;
 4659:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 4660:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 4661:                 if ($type eq 'version') {
 4662:                     my $selector = '<select name="'.$prefix.'_version">';
 4663:                     foreach my $version (@lcversions) {
 4664:                         my $selected = '';
 4665:                         if ($current{'version'} eq $version) {
 4666:                             $selected = ' selected="selected"';
 4667:                         }
 4668:                         $selector .= ' <option value="'.$version.'"'.
 4669:                                      $selected.'>'.$version.'</option>';
 4670:                     }
 4671:                     $selector .= '</select> ';
 4672:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 4673:                 } else {
 4674:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 4675:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 4676:                                  ' />'.('&nbsp;'x2).
 4677:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 4678:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 4679:                                  "\n".
 4680:                                  '</div><div><table>';
 4681:                     my $rem;
 4682:                     for (my $i=0; $i<@locations; $i++) {
 4683:                         my ($showloc,$value,$checkedtype);
 4684:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 4685:                             my $ip = $by_location{$locations[$i]}->[0];
 4686:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 4687:                                  $value = join(':',@{$by_ip{$ip}});
 4688:                                 $showloc = join(', ',@{$by_ip{$ip}});
 4689:                                 if (ref($current{$type}) eq 'ARRAY') {
 4690:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 4691:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 4692:                                             $checkedtype = ' checked="checked"';
 4693:                                             last;
 4694:                                         }
 4695:                                     }
 4696:                                 }
 4697:                             }
 4698:                         }
 4699:                         $rem = $i%($numinrow);
 4700:                         if ($rem == 0) {
 4701:                             if ($i > 0) {
 4702:                                 $datatable .= '</tr>';
 4703:                             }
 4704:                             $datatable .= '<tr>';
 4705:                         }
 4706:                         $datatable .= '<td class="LC_left_item">'.
 4707:                                       '<span class="LC_nobreak"><label>'.
 4708:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 4709:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 4710:                                       '</label></span></td>';
 4711:                     }
 4712:                     $rem = @locations%($numinrow);
 4713:                     my $colsleft = $numinrow - $rem;
 4714:                     if ($colsleft > 1 ) {
 4715:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4716:                                       '&nbsp;</td>';
 4717:                     } elsif ($colsleft == 1) {
 4718:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4719:                     }
 4720:                     $datatable .= '</tr></table>';
 4721:                 }
 4722:                 $datatable .= '</td></tr>';
 4723:                 $itemcount ++;
 4724:             }
 4725:         }
 4726:     }
 4727:     $$rowtotal += $itemcount;
 4728:     return $datatable;
 4729: }
 4730: 
 4731: sub build_location_hashes {
 4732:     my ($intdoms,$by_ip,$by_location) = @_;
 4733:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 4734:                   (ref($by_location) eq 'HASH')); 
 4735:     my %iphost = &Apache::lonnet::get_iphost();
 4736:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 4737:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 4738:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 4739:         foreach my $id (@{$iphost{$primary_ip}}) {
 4740:             my $intdom = &Apache::lonnet::internet_dom($id);
 4741:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 4742:                 push(@{$intdoms},$intdom);
 4743:             }
 4744:         }
 4745:     }
 4746:     foreach my $ip (keys(%iphost)) {
 4747:         if (ref($iphost{$ip}) eq 'ARRAY') {
 4748:             foreach my $id (@{$iphost{$ip}}) {
 4749:                 my $location = &Apache::lonnet::internet_dom($id);
 4750:                 if ($location) {
 4751:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 4752:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4753:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 4754:                             push(@{$by_ip->{$ip}},$location);
 4755:                         }
 4756:                     } else {
 4757:                         $by_ip->{$ip} = [$location];
 4758:                     }
 4759:                 }
 4760:             }
 4761:         }
 4762:     }
 4763:     foreach my $ip (sort(keys(%{$by_ip}))) {
 4764:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4765:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 4766:             my $first = $by_ip->{$ip}->[0];
 4767:             if (ref($by_location->{$first}) eq 'ARRAY') {
 4768:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 4769:                     push(@{$by_location->{$first}},$ip);
 4770:                 }
 4771:             } else {
 4772:                 $by_location->{$first} = [$ip];
 4773:             }
 4774:         }
 4775:     }
 4776:     return;
 4777: }
 4778: 
 4779: sub current_offloads_to {
 4780:     my ($dom,$settings,$servers) = @_;
 4781:     my (%spareid,%otherdomconfigs);
 4782:     if (ref($servers) eq 'HASH') {
 4783:         foreach my $lonhost (sort(keys(%{$servers}))) {
 4784:             my $gotspares;
 4785:             if (ref($settings) eq 'HASH') {
 4786:                 if (ref($settings->{'spares'}) eq 'HASH') {
 4787:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 4788:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 4789:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 4790:                         $gotspares = 1;
 4791:                     }
 4792:                 }
 4793:             }
 4794:             unless ($gotspares) {
 4795:                 my $gotspares;
 4796:                 my $serverhomeID =
 4797:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 4798:                 my $serverhomedom =
 4799:                     &Apache::lonnet::host_domain($serverhomeID);
 4800:                 if ($serverhomedom ne $dom) {
 4801:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 4802:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4803:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4804:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4805:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4806:                                 $gotspares = 1;
 4807:                             }
 4808:                         }
 4809:                     } else {
 4810:                         $otherdomconfigs{$serverhomedom} =
 4811:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 4812:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 4813:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4814:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4815:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 4816:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4817:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4818:                                         $gotspares = 1;
 4819:                                     }
 4820:                                 }
 4821:                             }
 4822:                         }
 4823:                     }
 4824:                 }
 4825:             }
 4826:             unless ($gotspares) {
 4827:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4828:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4829:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4830:                } else {
 4831:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 4832:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 4833:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4834:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4835:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4836:                     } else {
 4837:                         my %what = (
 4838:                              spareid => 1,
 4839:                         );
 4840:                         my ($result,$returnhash) = 
 4841:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 4842:                         if ($result eq 'ok') { 
 4843:                             if (ref($returnhash) eq 'HASH') {
 4844:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 4845:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 4846:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 4847:                                 }
 4848:                             }
 4849:                         }
 4850:                     }
 4851:                 }
 4852:             }
 4853:         }
 4854:     }
 4855:     return %spareid;
 4856: }
 4857: 
 4858: sub spares_row {
 4859:     my ($dom,$servers,$spareid,$serverhomes,$altids,$curroffloadnow,$rowtotal) = @_;
 4860:     my $css_class;
 4861:     my $numinrow = 4;
 4862:     my $itemcount = 1;
 4863:     my $datatable;
 4864:     my %typetitles = &sparestype_titles();
 4865:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 4866:         foreach my $server (sort(keys(%{$servers}))) {
 4867:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 4868:             my ($othercontrol,$serverdom);
 4869:             if ($serverhome ne $server) {
 4870:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 4871:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 4872:             } else {
 4873:                 $serverdom = &Apache::lonnet::host_domain($server);
 4874:                 if ($serverdom ne $dom) {
 4875:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 4876:                 }
 4877:             }
 4878:             next unless (ref($spareid->{$server}) eq 'HASH');
 4879:             my $checkednow;
 4880:             if (ref($curroffloadnow) eq 'HASH') {
 4881:                 if ($curroffloadnow->{$server}) {
 4882:                     $checkednow = ' checked="checked"';
 4883:                 }
 4884:             }
 4885:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4886:             $datatable .= '<tr'.$css_class.'>
 4887:                            <td rowspan="2">
 4888:                             <span class="LC_nobreak">'.
 4889:                           &mt('[_1] when busy, offloads to:'
 4890:                               ,'<b>'.$server.'</b>').'</span><br />'.
 4891:                           '<span class="LC_nobreak">'."\n".
 4892:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 4893:                           '&nbsp;'.&mt('Switch active users on next access').'</label></span>'.
 4894:                           "\n";
 4895:             my (%current,%canselect);
 4896:             my @choices = 
 4897:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 4898:             foreach my $type ('primary','default') {
 4899:                 if (ref($spareid->{$server}) eq 'HASH') {
 4900:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 4901:                         my @spares = @{$spareid->{$server}{$type}};
 4902:                         if (@spares > 0) {
 4903:                             if ($othercontrol) {
 4904:                                 $current{$type} = join(', ',@spares);
 4905:                             } else {
 4906:                                 $current{$type} .= '<table>';
 4907:                                 my $numspares = scalar(@spares);
 4908:                                 for (my $i=0;  $i<@spares; $i++) {
 4909:                                     my $rem = $i%($numinrow);
 4910:                                     if ($rem == 0) {
 4911:                                         if ($i > 0) {
 4912:                                             $current{$type} .= '</tr>';
 4913:                                         }
 4914:                                         $current{$type} .= '<tr>';
 4915:                                     }
 4916:                                     $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;'.
 4917:                                                        $spareid->{$server}{$type}[$i].
 4918:                                                        '</label></td>'."\n";
 4919:                                 }
 4920:                                 my $rem = @spares%($numinrow);
 4921:                                 my $colsleft = $numinrow - $rem;
 4922:                                 if ($colsleft > 1 ) {
 4923:                                     $current{$type} .= '<td colspan="'.$colsleft.
 4924:                                                        '" class="LC_left_item">'.
 4925:                                                        '&nbsp;</td>';
 4926:                                 } elsif ($colsleft == 1) {
 4927:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 4928:                                 }
 4929:                                 $current{$type} .= '</tr></table>';
 4930:                             }
 4931:                         }
 4932:                     }
 4933:                     if ($current{$type} eq '') {
 4934:                         $current{$type} = &mt('None specified');
 4935:                     }
 4936:                     if ($othercontrol) {
 4937:                         if ($type eq 'primary') {
 4938:                             $canselect{$type} = $othercontrol;
 4939:                         }
 4940:                     } else {
 4941:                         $canselect{$type} = 
 4942:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 4943:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 4944:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 4945:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 4946:                         if (@choices > 0) {
 4947:                             foreach my $lonhost (@choices) {
 4948:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 4949:                             }
 4950:                         }
 4951:                         $canselect{$type} .= '</select>'."\n";
 4952:                     }
 4953:                 } else {
 4954:                     $current{$type} = &mt('Could not be determined');
 4955:                     if ($type eq 'primary') {
 4956:                         $canselect{$type} =  $othercontrol;
 4957:                     }
 4958:                 }
 4959:                 if ($type eq 'default') {
 4960:                     $datatable .= '<tr'.$css_class.'>';
 4961:                 }
 4962:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 4963:                               '<td>'.$current{$type}.'</td>'."\n".
 4964:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 4965:             }
 4966:             $itemcount ++;
 4967:         }
 4968:     }
 4969:     $$rowtotal += $itemcount;
 4970:     return $datatable;
 4971: }
 4972: 
 4973: sub possible_newspares {
 4974:     my ($server,$currspares,$serverhomes,$altids) = @_;
 4975:     my $serverhostname = &Apache::lonnet::hostname($server);
 4976:     my %excluded;
 4977:     if ($serverhostname ne '') {
 4978:         %excluded = (
 4979:                        $serverhostname => 1,
 4980:                     );
 4981:     }
 4982:     if (ref($currspares) eq 'HASH') {
 4983:         foreach my $type (keys(%{$currspares})) {
 4984:             if (ref($currspares->{$type}) eq 'ARRAY') {
 4985:                 if (@{$currspares->{$type}} > 0) {
 4986:                     foreach my $curr (@{$currspares->{$type}}) {
 4987:                         my $hostname = &Apache::lonnet::hostname($curr);
 4988:                         $excluded{$hostname} = 1;
 4989:                     }
 4990:                 }
 4991:             }
 4992:         }
 4993:     }
 4994:     my @choices;
 4995:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 4996:         if (keys(%{$serverhomes}) > 1) {
 4997:             foreach my $name (sort(keys(%{$serverhomes}))) {
 4998:                 unless ($excluded{$name}) {
 4999:                     if (exists($altids->{$serverhomes->{$name}})) {
 5000:                         push(@choices,$altids->{$serverhomes->{$name}});
 5001:                     } else {
 5002:                         push(@choices,$serverhomes->{$name});
 5003:                     }
 5004:                 }
 5005:             }
 5006:         }
 5007:     }
 5008:     return sort(@choices);
 5009: }
 5010: 
 5011: sub print_loadbalancing {
 5012:     my ($dom,$settings,$rowtotal) = @_;
 5013:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 5014:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 5015:     my $numinrow = 1;
 5016:     my $datatable;
 5017:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5018:     my (%currbalancer,%currtargets,%currrules,%existing);
 5019:     if (ref($settings) eq 'HASH') {
 5020:         %existing = %{$settings};
 5021:     }
 5022:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 5023:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 5024:                                   \%currtargets,\%currrules);
 5025:     } else {
 5026:         return;
 5027:     }
 5028:     my ($othertitle,$usertypes,$types) =
 5029:         &Apache::loncommon::sorted_inst_types($dom);
 5030:     my $rownum = 8;
 5031:     if (ref($types) eq 'ARRAY') {
 5032:         $rownum += scalar(@{$types});
 5033:     }
 5034:     my @css_class = ('LC_odd_row','LC_even_row');
 5035:     my $balnum = 0;
 5036:     my $islast;
 5037:     my (@toshow,$disabledtext);
 5038:     if (keys(%currbalancer) > 0) {
 5039:         @toshow = sort(keys(%currbalancer));
 5040:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 5041:             push(@toshow,'');
 5042:         }
 5043:     } else {
 5044:         @toshow = ('');
 5045:         $disabledtext = &mt('No existing load balancer');
 5046:     }
 5047:     foreach my $lonhost (@toshow) {
 5048:         if ($balnum == scalar(@toshow)-1) {
 5049:             $islast = 1;
 5050:         } else {
 5051:             $islast = 0;
 5052:         }
 5053:         my $cssidx = $balnum%2;
 5054:         my $targets_div_style = 'display: none';
 5055:         my $disabled_div_style = 'display: block';
 5056:         my $homedom_div_style = 'display: none';
 5057:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 5058:                       '<td rowspan="'.$rownum.'" valign="top">'.
 5059:                       '<p>';
 5060:         if ($lonhost eq '') {
 5061:             $datatable .= '<span class="LC_nobreak">';
 5062:             if (keys(%currbalancer) > 0) {
 5063:                 $datatable .= &mt('Add balancer:');
 5064:             } else {
 5065:                 $datatable .= &mt('Enable balancer:');
 5066:             }
 5067:             $datatable .= '&nbsp;'.
 5068:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 5069:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 5070:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 5071:                           '<option value="" selected="selected">'.&mt('None').
 5072:                           '</option>'."\n";
 5073:             foreach my $server (sort(keys(%servers))) {
 5074:                 next if ($currbalancer{$server});
 5075:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 5076:             }
 5077:             $datatable .=
 5078:                 '</select>'."\n".
 5079:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 5080:         } else {
 5081:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 5082:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 5083:                            &mt('Stop balancing').'</label>'.
 5084:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 5085:             $targets_div_style = 'display: block';
 5086:             $disabled_div_style = 'display: none';
 5087:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 5088:                 $homedom_div_style = 'display: block';
 5089:             }
 5090:         }
 5091:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 5092:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 5093:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 5094:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 5095:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 5096:         my @sparestypes = ('primary','default');
 5097:         my %typetitles = &sparestype_titles();
 5098:         my %hostherechecked = (
 5099:                                   no => ' checked="checked"',
 5100:                               );
 5101:         foreach my $sparetype (@sparestypes) {
 5102:             my $targettable;
 5103:             for (my $i=0; $i<$numspares; $i++) {
 5104:                 my $checked;
 5105:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 5106:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5107:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5108:                             $checked = ' checked="checked"';
 5109:                         }
 5110:                     }
 5111:                 }
 5112:                 my ($chkboxval,$disabled);
 5113:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 5114:                     $chkboxval = $spares[$i];
 5115:                 }
 5116:                 if (exists($currbalancer{$spares[$i]})) {
 5117:                     $disabled = ' disabled="disabled"';
 5118:                 }
 5119:                 $targettable .=
 5120:                     '<td><span class="LC_nobreak"><label>'.
 5121:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 5122:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 5123:                     '</span></label></span></td>';
 5124:                 my $rem = $i%($numinrow);
 5125:                 if ($rem == 0) {
 5126:                     if (($i > 0) && ($i < $numspares-1)) {
 5127:                         $targettable .= '</tr>';
 5128:                     }
 5129:                     if ($i < $numspares-1) {
 5130:                         $targettable .= '<tr>';
 5131:                     }
 5132:                 }
 5133:             }
 5134:             if ($targettable ne '') {
 5135:                 my $rem = $numspares%($numinrow);
 5136:                 my $colsleft = $numinrow - $rem;
 5137:                 if ($colsleft > 1 ) {
 5138:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5139:                                     '&nbsp;</td>';
 5140:                 } elsif ($colsleft == 1) {
 5141:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 5142:                 }
 5143:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 5144:                                '<table><tr>'.$targettable.'</tr></table><br />';
 5145:             }
 5146:             $hostherechecked{$sparetype} = '';
 5147:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 5148:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5149:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5150:                         $hostherechecked{$sparetype} = ' checked="checked"';
 5151:                         $hostherechecked{'no'} = '';
 5152:                     }
 5153:                 }
 5154:             }
 5155:         }
 5156:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 5157:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 5158:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 5159:         foreach my $sparetype (@sparestypes) {
 5160:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 5161:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 5162:                           '</i></label><br />';
 5163:         }
 5164:         $datatable .= '</div></td></tr>'.
 5165:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 5166:                                            $othertitle,$usertypes,$types,\%servers,
 5167:                                            \%currbalancer,$lonhost,
 5168:                                            $targets_div_style,$homedom_div_style,
 5169:                                            $css_class[$cssidx],$balnum,$islast);
 5170:         $$rowtotal += $rownum;
 5171:         $balnum ++;
 5172:     }
 5173:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 5174:     return $datatable;
 5175: }
 5176: 
 5177: sub get_loadbalancers_config {
 5178:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 5179:     return unless ((ref($servers) eq 'HASH') &&
 5180:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 5181:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 5182:     if (keys(%{$existing}) > 0) {
 5183:         my $oldlonhost;
 5184:         foreach my $key (sort(keys(%{$existing}))) {
 5185:             if ($key eq 'lonhost') {
 5186:                 $oldlonhost = $existing->{'lonhost'};
 5187:                 $currbalancer->{$oldlonhost} = 1;
 5188:             } elsif ($key eq 'targets') {
 5189:                 if ($oldlonhost) {
 5190:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 5191:                 }
 5192:             } elsif ($key eq 'rules') {
 5193:                 if ($oldlonhost) {
 5194:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 5195:                 }
 5196:             } elsif (ref($existing->{$key}) eq 'HASH') {
 5197:                 $currbalancer->{$key} = 1;
 5198:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 5199:                 $currrules->{$key} = $existing->{$key}{'rules'};
 5200:             }
 5201:         }
 5202:     } else {
 5203:         my ($balancerref,$targetsref) =
 5204:                 &Apache::lonnet::get_lonbalancer_config($servers);
 5205:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 5206:             foreach my $server (sort(keys(%{$balancerref}))) {
 5207:                 $currbalancer->{$server} = 1;
 5208:                 $currtargets->{$server} = $targetsref->{$server};
 5209:             }
 5210:         }
 5211:     }
 5212:     return;
 5213: }
 5214: 
 5215: sub loadbalancing_rules {
 5216:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 5217:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 5218:         $css_class,$balnum,$islast) = @_;
 5219:     my $output;
 5220:     my $num = 0;
 5221:     my ($alltypes,$othertypes,$titles) =
 5222:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 5223:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 5224:         foreach my $type (@{$alltypes}) {
 5225:             $num ++;
 5226:             my $current;
 5227:             if (ref($currrules) eq 'HASH') {
 5228:                 $current = $currrules->{$type};
 5229:             }
 5230:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5231:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 5232:                     $current = '';
 5233:                 }
 5234:             }
 5235:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 5236:                                              $servers,$currbalancer,$lonhost,$dom,
 5237:                                              $targets_div_style,$homedom_div_style,
 5238:                                              $css_class,$balnum,$num,$islast);
 5239:         }
 5240:     }
 5241:     return $output;
 5242: }
 5243: 
 5244: sub loadbalancing_titles {
 5245:     my ($dom,$intdom,$usertypes,$types) = @_;
 5246:     my %othertypes = (
 5247:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 5248:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 5249:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 5250:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 5251:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 5252:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 5253:                      );
 5254:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 5255:     my @available;
 5256:     if (ref($types) eq 'ARRAY') {
 5257:         @available = @{$types};
 5258:     }
 5259:     unless (grep(/^default$/,@available)) {
 5260:         push(@available,'default');
 5261:     }
 5262:     unshift(@alltypes,@available);
 5263:     my %titles;
 5264:     foreach my $type (@alltypes) {
 5265:         if ($type =~ /^_LC_/) {
 5266:             $titles{$type} = $othertypes{$type};
 5267:         } elsif ($type eq 'default') {
 5268:             $titles{$type} = &mt('All users from [_1]',$dom);
 5269:             if (ref($types) eq 'ARRAY') {
 5270:                 if (@{$types} > 0) {
 5271:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 5272:                 }
 5273:             }
 5274:         } elsif (ref($usertypes) eq 'HASH') {
 5275:             $titles{$type} = $usertypes->{$type};
 5276:         }
 5277:     }
 5278:     return (\@alltypes,\%othertypes,\%titles);
 5279: }
 5280: 
 5281: sub loadbalance_rule_row {
 5282:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 5283:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 5284:     my @rulenames;
 5285:     my %ruletitles = &offloadtype_text();
 5286:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 5287:         @rulenames = ('balancer','offloadedto','specific');
 5288:     } else {
 5289:         @rulenames = ('default','homeserver');
 5290:         if ($type eq '_LC_external') {
 5291:             push(@rulenames,'externalbalancer');
 5292:         } else {
 5293:             push(@rulenames,'specific');
 5294:         }
 5295:         push(@rulenames,'none');
 5296:     }
 5297:     my $style = $targets_div_style;
 5298:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5299:         $style = $homedom_div_style;
 5300:     }
 5301:     my $space;
 5302:     if ($islast && $num == 1) {
 5303:         $space = '<div display="inline-block">&nbsp;</div>';
 5304:     }
 5305:     my $output =
 5306:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 5307:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 5308:         '<td valaign="top">'.$space.
 5309:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 5310:     for (my $i=0; $i<@rulenames; $i++) {
 5311:         my $rule = $rulenames[$i];
 5312:         my ($checked,$extra);
 5313:         if ($rulenames[$i] eq 'default') {
 5314:             $rule = '';
 5315:         }
 5316:         if ($rulenames[$i] eq 'specific') {
 5317:             if (ref($servers) eq 'HASH') {
 5318:                 my $default;
 5319:                 if (($current ne '') && (exists($servers->{$current}))) {
 5320:                     $checked = ' checked="checked"';
 5321:                 }
 5322:                 unless ($checked) {
 5323:                     $default = ' selected="selected"';
 5324:                 }
 5325:                 $extra =
 5326:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5327:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5328:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 5329:                     '<option value=""'.$default.'></option>'."\n";
 5330:                 foreach my $server (sort(keys(%{$servers}))) {
 5331:                     if (ref($currbalancer) eq 'HASH') {
 5332:                         next if (exists($currbalancer->{$server}));
 5333:                     }
 5334:                     my $selected;
 5335:                     if ($server eq $current) {
 5336:                         $selected = ' selected="selected"';
 5337:                     }
 5338:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 5339:                 }
 5340:                 $extra .= '</select>';
 5341:             }
 5342:         } elsif ($rule eq $current) {
 5343:             $checked = ' checked="checked"';
 5344:         }
 5345:         $output .= '<span class="LC_nobreak"><label>'.
 5346:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 5347:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 5348:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 5349:                    ')"'.$checked.' />&nbsp;';
 5350:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 5351:             $output .= $ruletitles{'particular'};
 5352:         } else {
 5353:             $output .= $ruletitles{$rulenames[$i]};
 5354:         }
 5355:         $output .= '</label>'.$extra.'</span><br />'."\n";
 5356:     }
 5357:     $output .= '</div></td></tr>'."\n";
 5358:     return $output;
 5359: }
 5360: 
 5361: sub offloadtype_text {
 5362:     my %ruletitles = &Apache::lonlocal::texthash (
 5363:            'default'          => 'Offloads to default destinations',
 5364:            'homeserver'       => "Offloads to user's home server",
 5365:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 5366:            'specific'         => 'Offloads to specific server',
 5367:            'none'             => 'No offload',
 5368:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 5369:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 5370:            'particular'       => 'Session hosted (after re-auth) on server:',
 5371:     );
 5372:     return %ruletitles;
 5373: }
 5374: 
 5375: sub sparestype_titles {
 5376:     my %typestitles = &Apache::lonlocal::texthash (
 5377:                           'primary' => 'primary',
 5378:                           'default' => 'default',
 5379:                       );
 5380:     return %typestitles;
 5381: }
 5382: 
 5383: sub contact_titles {
 5384:     my %titles = &Apache::lonlocal::texthash (
 5385:                    'supportemail'    => 'Support E-mail address',
 5386:                    'adminemail'      => 'Default Server Admin E-mail address',
 5387:                    'errormail'       => 'Error reports to be e-mailed to',
 5388:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 5389:                    'helpdeskmail'    => "Helpdesk requests from all users in this domain",
 5390:                    'otherdomsmail'   => 'Helpdesk requests from users in other (unconfigured) domains',
 5391:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 5392:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 5393:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 5394:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 5395:                  );
 5396:     my %short_titles = &Apache::lonlocal::texthash (
 5397:                            adminemail   => 'Admin E-mail address',
 5398:                            supportemail => 'Support E-mail',
 5399:                        );   
 5400:     return (\%titles,\%short_titles);
 5401: }
 5402: 
 5403: sub helpform_fields {
 5404:     my %titles =  &Apache::lonlocal::texthash (
 5405:                        'username'   => 'Name',
 5406:                        'user'       => 'Username/domain',
 5407:                        'phone'      => 'Phone',
 5408:                        'cc'         => 'Cc e-mail',
 5409:                        'course'     => 'Course Details',
 5410:                        'section'    => 'Sections',
 5411:                        'screenshot' => 'File upload',
 5412:     );
 5413:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 5414:     my %possoptions = (
 5415:                         username     => ['yes','no','req'],
 5416:                         phone        => ['yes','no','req'],
 5417:                         user         => ['yes','no'],
 5418:                         cc           => ['yes','no'],
 5419:                         course       => ['yes','no'],
 5420:                         section      => ['yes','no'],
 5421:                         screenshot   => ['yes','no'],
 5422:                       );
 5423:     my %fieldoptions = &Apache::lonlocal::texthash (
 5424:                          'yes'  => 'Optional',
 5425:                          'req'  => 'Required',
 5426:                          'no'   => "Not shown",
 5427:     );
 5428:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 5429: }
 5430: 
 5431: sub tool_titles {
 5432:     my %titles = &Apache::lonlocal::texthash (
 5433:                      aboutme    => 'Personal web page',
 5434:                      blog       => 'Blog',
 5435:                      webdav     => 'WebDAV',
 5436:                      portfolio  => 'Portfolio',
 5437:                      official   => 'Official courses (with institutional codes)',
 5438:                      unofficial => 'Unofficial courses',
 5439:                      community  => 'Communities',
 5440:                      textbook   => 'Textbook courses',
 5441:                  );
 5442:     return %titles;
 5443: }
 5444: 
 5445: sub courserequest_titles {
 5446:     my %titles = &Apache::lonlocal::texthash (
 5447:                                    official   => 'Official',
 5448:                                    unofficial => 'Unofficial',
 5449:                                    community  => 'Communities',
 5450:                                    textbook   => 'Textbook',
 5451:                                    norequest  => 'Not allowed',
 5452:                                    approval   => 'Approval by Dom. Coord.',
 5453:                                    validate   => 'With validation',
 5454:                                    autolimit  => 'Numerical limit',
 5455:                                    unlimited  => '(blank for unlimited)',
 5456:                  );
 5457:     return %titles;
 5458: }
 5459: 
 5460: sub authorrequest_titles {
 5461:     my %titles = &Apache::lonlocal::texthash (
 5462:                                    norequest  => 'Not allowed',
 5463:                                    approval   => 'Approval by Dom. Coord.',
 5464:                                    automatic  => 'Automatic approval',
 5465:                  );
 5466:     return %titles;
 5467: }
 5468: 
 5469: sub courserequest_conditions {
 5470:     my %conditions = &Apache::lonlocal::texthash (
 5471:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 5472:        validate   => '(Processing of request subject to institutional validation).',
 5473:                  );
 5474:     return %conditions;
 5475: }
 5476: 
 5477: 
 5478: sub print_usercreation {
 5479:     my ($position,$dom,$settings,$rowtotal) = @_;
 5480:     my $numinrow = 4;
 5481:     my $datatable;
 5482:     if ($position eq 'top') {
 5483:         $$rowtotal ++;
 5484:         my $rowcount = 0;
 5485:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 5486:         if (ref($rules) eq 'HASH') {
 5487:             if (keys(%{$rules}) > 0) {
 5488:                 $datatable .= &user_formats_row('username',$settings,$rules,
 5489:                                                 $ruleorder,$numinrow,$rowcount);
 5490:                 $$rowtotal ++;
 5491:                 $rowcount ++;
 5492:             }
 5493:         }
 5494:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 5495:         if (ref($idrules) eq 'HASH') {
 5496:             if (keys(%{$idrules}) > 0) {
 5497:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 5498:                                                 $idruleorder,$numinrow,$rowcount);
 5499:                 $$rowtotal ++;
 5500:                 $rowcount ++;
 5501:             }
 5502:         }
 5503:         if ($rowcount == 0) {
 5504:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 5505:             $$rowtotal ++;
 5506:             $rowcount ++;
 5507:         }
 5508:     } elsif ($position eq 'middle') {
 5509:         my @creators = ('author','course','requestcrs');
 5510:         my ($rules,$ruleorder) =
 5511:             &Apache::lonnet::inst_userrules($dom,'username');
 5512:         my %lt = &usercreation_types();
 5513:         my %checked;
 5514:         if (ref($settings) eq 'HASH') {
 5515:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 5516:                 foreach my $item (@creators) {
 5517:                     $checked{$item} = $settings->{'cancreate'}{$item};
 5518:                 }
 5519:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 5520:                 foreach my $item (@creators) {
 5521:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 5522:                         $checked{$item} = 'none';
 5523:                     }
 5524:                 }
 5525:             }
 5526:         }
 5527:         my $rownum = 0;
 5528:         foreach my $item (@creators) {
 5529:             $rownum ++;
 5530:             if ($checked{$item} eq '') {
 5531:                 $checked{$item} = 'any';
 5532:             }
 5533:             my $css_class;
 5534:             if ($rownum%2) {
 5535:                 $css_class = '';
 5536:             } else {
 5537:                 $css_class = ' class="LC_odd_row" ';
 5538:             }
 5539:             $datatable .= '<tr'.$css_class.'>'.
 5540:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 5541:                          '</span></td><td align="right">';
 5542:             my @options = ('any');
 5543:             if (ref($rules) eq 'HASH') {
 5544:                 if (keys(%{$rules}) > 0) {
 5545:                     push(@options,('official','unofficial'));
 5546:                 }
 5547:             }
 5548:             push(@options,'none');
 5549:             foreach my $option (@options) {
 5550:                 my $type = 'radio';
 5551:                 my $check = ' ';
 5552:                 if ($checked{$item} eq $option) {
 5553:                     $check = ' checked="checked" ';
 5554:                 } 
 5555:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5556:                               '<input type="'.$type.'" name="can_createuser_'.
 5557:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 5558:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 5559:             }
 5560:             $datatable .= '</td></tr>';
 5561:         }
 5562:     } else {
 5563:         my @contexts = ('author','course','domain');
 5564:         my @authtypes = ('int','krb4','krb5','loc');
 5565:         my %checked;
 5566:         if (ref($settings) eq 'HASH') {
 5567:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 5568:                 foreach my $item (@contexts) {
 5569:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 5570:                         foreach my $auth (@authtypes) {
 5571:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 5572:                                 $checked{$item}{$auth} = ' checked="checked" ';
 5573:                             }
 5574:                         }
 5575:                     }
 5576:                 }
 5577:             }
 5578:         } else {
 5579:             foreach my $item (@contexts) {
 5580:                 foreach my $auth (@authtypes) {
 5581:                     $checked{$item}{$auth} = ' checked="checked" ';
 5582:                 }
 5583:             }
 5584:         }
 5585:         my %title = &context_names();
 5586:         my %authname = &authtype_names();
 5587:         my $rownum = 0;
 5588:         my $css_class; 
 5589:         foreach my $item (@contexts) {
 5590:             if ($rownum%2) {
 5591:                 $css_class = '';
 5592:             } else {
 5593:                 $css_class = ' class="LC_odd_row" ';
 5594:             }
 5595:             $datatable .=   '<tr'.$css_class.'>'.
 5596:                             '<td>'.$title{$item}.
 5597:                             '</td><td class="LC_left_item">'.
 5598:                             '<span class="LC_nobreak">';
 5599:             foreach my $auth (@authtypes) {
 5600:                 $datatable .= '<label>'. 
 5601:                               '<input type="checkbox" name="'.$item.'_auth" '.
 5602:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 5603:                               $authname{$auth}.'</label>&nbsp;';
 5604:             }
 5605:             $datatable .= '</span></td></tr>';
 5606:             $rownum ++;
 5607:         }
 5608:         $$rowtotal += $rownum;
 5609:     }
 5610:     return $datatable;
 5611: }
 5612: 
 5613: sub print_selfcreation {
 5614:     my ($position,$dom,$settings,$rowtotal) = @_;
 5615:     my (@selfcreate,$createsettings,$processing,$datatable);
 5616:     if (ref($settings) eq 'HASH') {
 5617:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 5618:             $createsettings = $settings->{'cancreate'};
 5619:             if (ref($createsettings) eq 'HASH') {
 5620:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 5621:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 5622:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 5623:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 5624:                         @selfcreate = ('email','login','sso');
 5625:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 5626:                         @selfcreate = ($createsettings->{'selfcreate'});
 5627:                     }
 5628:                 }
 5629:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 5630:                     $processing = $createsettings->{'selfcreateprocessing'};
 5631:                 }
 5632:             }
 5633:         }
 5634:     }
 5635:     my %radiohash;
 5636:     my $numinrow = 4;
 5637:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 5638:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5639:     if ($position eq 'top') {
 5640:         my %choices = &Apache::lonlocal::texthash (
 5641:                                                       cancreate_login      => 'Institutional Login',
 5642:                                                       cancreate_sso        => 'Institutional Single Sign On',
 5643:                                                   );
 5644:         my @toggles = sort(keys(%choices));
 5645:         my %defaultchecked = (
 5646:                                'cancreate_login' => 'off',
 5647:                                'cancreate_sso'   => 'off',
 5648:                              );
 5649:         my ($onclick,$itemcount);
 5650:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5651:                                                      \%choices,$itemcount,$onclick);
 5652:         $$rowtotal += $itemcount;
 5653:         
 5654:         if (ref($usertypes) eq 'HASH') {
 5655:             if (keys(%{$usertypes}) > 0) {
 5656:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 5657:                                              $dom,$numinrow,$othertitle,
 5658:                                              'statustocreate',$rowtotal);
 5659:                 $$rowtotal ++;
 5660:             }
 5661:         }
 5662:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 5663:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5664:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 5665:         my $rem;
 5666:         my $numperrow = 2;
 5667:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 5668:         $datatable .= '<tr'.$css_class.'>'.
 5669:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 5670:                      '<td class="LC_left_item">'."\n".
 5671:                      '<table><tr><td>'."\n";
 5672:         for (my $i=0; $i<@fields; $i++) {
 5673:             $rem = $i%($numperrow);
 5674:             if ($rem == 0) {
 5675:                 if ($i > 0) {
 5676:                     $datatable .= '</tr>';
 5677:                 }
 5678:                 $datatable .= '<tr>';
 5679:             }
 5680:             my $currval;
 5681:             if (ref($createsettings) eq 'HASH') {
 5682:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 5683:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 5684:                 }
 5685:             }
 5686:             $datatable .= '<td class="LC_left_item">'.
 5687:                           '<span class="LC_nobreak">'.
 5688:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 5689:                           'value="'.$currval.'" size="10" />&nbsp;'.
 5690:                           $fieldtitles{$fields[$i]}.'</span></td>';
 5691:         }
 5692:         my $colsleft = $numperrow - $rem;
 5693:         if ($colsleft > 1 ) {
 5694:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5695:                          '&nbsp;</td>';
 5696:         } elsif ($colsleft == 1) {
 5697:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 5698:         }
 5699:         $datatable .= '</tr></table></td></tr>';
 5700:         $$rowtotal ++;
 5701:     } elsif ($position eq 'middle') {
 5702:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 5703:         my @posstypes;
 5704:         if (ref($types) eq 'ARRAY') {
 5705:             @posstypes = @{$types};
 5706:         }
 5707:         unless (grep(/^default$/,@posstypes)) {
 5708:             push(@posstypes,'default');
 5709:         }
 5710:         my %usertypeshash;
 5711:         if (ref($usertypes) eq 'HASH') {
 5712:             %usertypeshash = %{$usertypes};
 5713:         }
 5714:         $usertypeshash{'default'} = $othertitle;
 5715:         foreach my $status (@posstypes) {
 5716:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 5717:                                                    $numinrow,$$rowtotal,\%usertypeshash);
 5718:             $$rowtotal ++;
 5719:         }
 5720:     } else {
 5721:         my %choices = &Apache::lonlocal::texthash (
 5722:                                                       cancreate_email => 'E-mail address as username',
 5723:                                                   );
 5724:         my @toggles = sort(keys(%choices));
 5725:         my %defaultchecked = (
 5726:                                'cancreate_email' => 'off',
 5727:                              );
 5728:         my $itemcount = 0;
 5729:         my $display = 'none';
 5730:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 5731:             $display = 'block';
 5732:         }
 5733:         my $onclick = "toggleDisplay(this.form,'emailoptions');";
 5734:         my $additional = '<div id="emailoptions" style="display: '.$display.'">';
 5735:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5736:         my (@ordered,%usertypeshash);
 5737:         if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
 5738:             @ordered = @{$domdefaults{'inststatusguest'}};
 5739:         }
 5740:         if (@ordered) {
 5741:             unless (grep(/^default$/,@ordered)) {
 5742:                 push(@ordered,'default');
 5743:             }
 5744:             if (ref($usertypes) eq 'HASH') {
 5745:                 %usertypeshash = %{$usertypes};
 5746:             }
 5747:             $usertypeshash{'default'} = $othertitle;
 5748:             $additional .= '<table><tr>';
 5749:             foreach my $status (@ordered) {
 5750:                 $additional .= '<th>'.$usertypeshash{$status}.'</th>';
 5751:             }
 5752:             $additional .= '</tr><tr>';
 5753:             foreach my $status (@ordered) {
 5754:                 $additional .= '<td>'.&email_as_username($rowtotal,$processing,$status).'</td>';
 5755:             }
 5756:             $additional .= '</tr></table>';
 5757:         } else {
 5758:             $usertypeshash{'default'} = $othertitle;
 5759:             $additional .= &email_as_username($rowtotal,$processing);
 5760:         }
 5761:         $additional .= '</div>'."\n";
 5762: 
 5763:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5764:                                                      \%choices,$$rowtotal,$onclick,$additional);
 5765:         $$rowtotal ++;
 5766:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal);
 5767:         $$rowtotal ++;
 5768:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 5769:         $numinrow = 1;
 5770:         foreach my $status (@ordered) {
 5771:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 5772:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,$infotitles);
 5773:             $$rowtotal ++;
 5774:         }
 5775:         my ($emailrules,$emailruleorder) =
 5776:             &Apache::lonnet::inst_userrules($dom,'email');
 5777:         if (ref($emailrules) eq 'HASH') {
 5778:             if (keys(%{$emailrules}) > 0) {
 5779:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 5780:                                                 $emailruleorder,$numinrow,$$rowtotal);
 5781:                 $$rowtotal ++;
 5782:             }
 5783:         }
 5784:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal);
 5785:     }
 5786:     return $datatable;
 5787: }
 5788: 
 5789: sub email_as_username {
 5790:     my ($rowtotal,$processing,$type) = @_;
 5791:     my %choices =
 5792:         &Apache::lonlocal::texthash (
 5793:                                       automatic => 'Automatic approval',
 5794:                                       approval  => 'Queued for approval',
 5795:                                     );
 5796:     my $output;
 5797:     foreach my $option ('automatic','approval') {
 5798:         my $checked;
 5799:         if (ref($processing) eq 'HASH') {
 5800:             if ($type eq '') {   
 5801:                 if (!exists($processing->{'default'})) {
 5802:                     if ($option eq 'automatic') {
 5803:                         $checked = ' checked="checked"';
 5804:                     }
 5805:                 } else {
 5806:                     if ($processing->{'default'} eq $option) {
 5807:                         $checked = ' checked="checked"';
 5808:                     }
 5809:                 }
 5810:             } else {
 5811:                 if (!exists($processing->{$type})) {
 5812:                     if ($option eq 'automatic') {
 5813:                         $checked = ' checked="checked"';
 5814:                     }
 5815:                 } else {
 5816:                     if ($processing->{$type} eq $option) {
 5817:                         $checked = ' checked="checked"';
 5818:                     }
 5819:                 }
 5820:             }
 5821:         } elsif ($option eq 'automatic') {
 5822:             $checked = ' checked="checked"'; 
 5823:         }
 5824:         my $name = 'cancreate_emailprocess';
 5825:         if (($type ne '') && ($type ne 'default')) {
 5826:             $name .= '_'.$type;
 5827:         }
 5828:         $output .= '<span class="LC_nobreak"><label>'.
 5829:                    '<input type="radio" name="'.$name.'"'.
 5830:                    $checked.' value="'.$option.'" />'.
 5831:                    $choices{$option}.'</label></span>';
 5832:         if ($type eq '') {
 5833:             $output .= '&nbsp;';
 5834:         } else {
 5835:             $output .= '<br />';
 5836:         }
 5837:     }
 5838:     $$rowtotal ++;
 5839:     return $output;
 5840: }
 5841: 
 5842: sub captcha_choice {
 5843:     my ($context,$settings,$itemcount) = @_;
 5844:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 5845:         $vertext,$currver); 
 5846:     my %lt = &captcha_phrases();
 5847:     $keyentry = 'hidden';
 5848:     if ($context eq 'cancreate') {
 5849:         $rowname = &mt('CAPTCHA validation');
 5850:     } elsif ($context eq 'login') {
 5851:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 5852:     }
 5853:     if (ref($settings) eq 'HASH') {
 5854:         if ($settings->{'captcha'}) {
 5855:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 5856:         } else {
 5857:             $checked{'original'} = ' checked="checked"';
 5858:         }
 5859:         if ($settings->{'captcha'} eq 'recaptcha') {
 5860:             $pubtext = $lt{'pub'};
 5861:             $privtext = $lt{'priv'};
 5862:             $keyentry = 'text';
 5863:             $vertext = $lt{'ver'};
 5864:             $currver = $settings->{'recaptchaversion'};
 5865:             if ($currver ne '2') {
 5866:                 $currver = 1;
 5867:             }
 5868:         }
 5869:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 5870:             $currpub = $settings->{'recaptchakeys'}{'public'};
 5871:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 5872:         }
 5873:     } else {
 5874:         $checked{'original'} = ' checked="checked"';
 5875:     }
 5876:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5877:     my $output = '<tr'.$css_class.'>'.
 5878:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 5879:                  '<table><tr><td>'."\n";
 5880:     foreach my $option ('original','recaptcha','notused') {
 5881:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 5882:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 5883:                    $lt{$option}.'</label></span>';
 5884:         unless ($option eq 'notused') {
 5885:             $output .= ('&nbsp;'x2)."\n";
 5886:         }
 5887:     }
 5888: #
 5889: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 5890: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 5891: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 5892: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 5893: #
 5894:     $output .= '</td></tr>'."\n".
 5895:                '<tr><td>'."\n".
 5896:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 5897:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 5898:                $currpub.'" size="40" /></span><br />'."\n".
 5899:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 5900:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 5901:                $currpriv.'" size="40" /></span><br />'.
 5902:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 5903:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 5904:                $currver.'" size="3" /></span><br />'.
 5905:                '</td></tr></table>'."\n".
 5906:                '</td></tr>';
 5907:     return $output;
 5908: }
 5909: 
 5910: sub user_formats_row {
 5911:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 5912:     my $output;
 5913:     my %text = (
 5914:                    'username' => 'new usernames',
 5915:                    'id'       => 'IDs',
 5916:                    'email'    => 'self-created accounts (e-mail)',
 5917:                );
 5918:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 5919:     $output = '<tr '.$css_class.'>'.
 5920:               '<td><span class="LC_nobreak">';
 5921:     if ($type eq 'email') {
 5922:         $output .= &mt("Formats disallowed for $text{$type}: ");
 5923:     } else {
 5924:         $output .= &mt("Format rules to check for $text{$type}: ");
 5925:     }
 5926:     $output .= '</span></td>'.
 5927:                '<td class="LC_left_item" colspan="2"><table>';
 5928:     my $rem;
 5929:     if (ref($ruleorder) eq 'ARRAY') {
 5930:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 5931:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 5932:                 my $rem = $i%($numinrow);
 5933:                 if ($rem == 0) {
 5934:                     if ($i > 0) {
 5935:                         $output .= '</tr>';
 5936:                     }
 5937:                     $output .= '<tr>';
 5938:                 }
 5939:                 my $check = ' ';
 5940:                 if (ref($settings) eq 'HASH') {
 5941:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 5942:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 5943:                             $check = ' checked="checked" ';
 5944:                         }
 5945:                     }
 5946:                 }
 5947:                 $output .= '<td class="LC_left_item">'.
 5948:                            '<span class="LC_nobreak"><label>'.
 5949:                            '<input type="checkbox" name="'.$type.'_rule" '.
 5950:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 5951:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 5952:             }
 5953:         }
 5954:         $rem = @{$ruleorder}%($numinrow);
 5955:     }
 5956:     my $colsleft = $numinrow - $rem;
 5957:     if ($colsleft > 1 ) {
 5958:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5959:                    '&nbsp;</td>';
 5960:     } elsif ($colsleft == 1) {
 5961:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 5962:     }
 5963:     $output .= '</tr></table></td></tr>';
 5964:     return $output;
 5965: }
 5966: 
 5967: sub usercreation_types {
 5968:     my %lt = &Apache::lonlocal::texthash (
 5969:                     author     => 'When adding a co-author',
 5970:                     course     => 'When adding a user to a course',
 5971:                     requestcrs => 'When requesting a course',
 5972:                     any        => 'Any',
 5973:                     official   => 'Institutional only ',
 5974:                     unofficial => 'Non-institutional only',
 5975:                     none       => 'None',
 5976:     );
 5977:     return %lt;
 5978: }
 5979: 
 5980: sub selfcreation_types {
 5981:     my %lt = &Apache::lonlocal::texthash (
 5982:                     selfcreate => 'User creates own account',
 5983:                     any        => 'Any',
 5984:                     official   => 'Institutional only ',
 5985:                     unofficial => 'Non-institutional only',
 5986:                     email      => 'E-mail address',
 5987:                     login      => 'Institutional Login',
 5988:                     sso        => 'SSO',
 5989:              );
 5990: }
 5991: 
 5992: sub authtype_names {
 5993:     my %lt = &Apache::lonlocal::texthash(
 5994:                       int    => 'Internal',
 5995:                       krb4   => 'Kerberos 4',
 5996:                       krb5   => 'Kerberos 5',
 5997:                       loc    => 'Local',
 5998:                   );
 5999:     return %lt;
 6000: }
 6001: 
 6002: sub context_names {
 6003:     my %context_title = &Apache::lonlocal::texthash(
 6004:        author => 'Creating users when an Author',
 6005:        course => 'Creating users when in a course',
 6006:        domain => 'Creating users when a Domain Coordinator',
 6007:     );
 6008:     return %context_title;
 6009: }
 6010: 
 6011: sub print_usermodification {
 6012:     my ($position,$dom,$settings,$rowtotal) = @_;
 6013:     my $numinrow = 4;
 6014:     my ($context,$datatable,$rowcount);
 6015:     if ($position eq 'top') {
 6016:         $rowcount = 0;
 6017:         $context = 'author'; 
 6018:         foreach my $role ('ca','aa') {
 6019:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 6020:                                                    $numinrow,$rowcount);
 6021:             $$rowtotal ++;
 6022:             $rowcount ++;
 6023:         }
 6024:     } elsif ($position eq 'bottom') {
 6025:         $context = 'course';
 6026:         $rowcount = 0;
 6027:         foreach my $role ('st','ep','ta','in','cr') {
 6028:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 6029:                                                    $numinrow,$rowcount);
 6030:             $$rowtotal ++;
 6031:             $rowcount ++;
 6032:         }
 6033:     }
 6034:     return $datatable;
 6035: }
 6036: 
 6037: sub print_defaults {
 6038:     my ($position,$dom,$settings,$rowtotal) = @_;
 6039:     my $rownum = 0;
 6040:     my ($datatable,$css_class,$titles);
 6041:     unless ($position eq 'bottom') {
 6042:         $titles = &defaults_titles($dom);
 6043:     }
 6044:     if ($position eq 'top') {
 6045:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 6046:                      'datelocale_def','portal_def');
 6047:         my %defaults;
 6048:         if (ref($settings) eq 'HASH') {
 6049:             %defaults = %{$settings};
 6050:         } else {
 6051:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6052:             foreach my $item (@items) {
 6053:                 $defaults{$item} = $domdefaults{$item};
 6054:             }
 6055:         }
 6056:         foreach my $item (@items) {
 6057:             if ($rownum%2) {
 6058:                 $css_class = '';
 6059:             } else {
 6060:                 $css_class = ' class="LC_odd_row" ';
 6061:             }
 6062:             $datatable .= '<tr'.$css_class.'>'.
 6063:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 6064:                           '</span></td><td class="LC_right_item" colspan="3">';
 6065:             if ($item eq 'auth_def') {
 6066:                 my @authtypes = ('internal','krb4','krb5','localauth');
 6067:                 my %shortauth = (
 6068:                                  internal => 'int',
 6069:                                  krb4 => 'krb4',
 6070:                                  krb5 => 'krb5',
 6071:                                  localauth  => 'loc'
 6072:                                 );
 6073:                 my %authnames = &authtype_names();
 6074:                 foreach my $auth (@authtypes) {
 6075:                     my $checked = ' ';
 6076:                     if ($defaults{$item} eq $auth) {
 6077:                         $checked = ' checked="checked" ';
 6078:                     }
 6079:                     $datatable .= '<label><input type="radio" name="'.$item.
 6080:                                   '" value="'.$auth.'"'.$checked.'/>'.
 6081:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 6082:                 }
 6083:             } elsif ($item eq 'timezone_def') {
 6084:                 my $includeempty = 1;
 6085:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 6086:             } elsif ($item eq 'datelocale_def') {
 6087:                 my $includeempty = 1;
 6088:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 6089:             } elsif ($item eq 'lang_def') {
 6090:                 my $includeempty = 1;
 6091:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 6092:             } else {
 6093:                 my $size;
 6094:                 if ($item eq 'portal_def') {
 6095:                     $size = ' size="25"';
 6096:                 }
 6097:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 6098:                               $defaults{$item}.'"'.$size.' />';
 6099:             }
 6100:             $datatable .= '</td></tr>';
 6101:             $rownum ++;
 6102:         }
 6103:     } elsif ($position eq 'middle') {
 6104:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 6105:         my %defaults;
 6106:         if (ref($settings) eq 'HASH') {
 6107:             %defaults = %{$settings};
 6108:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 6109:                 $defaults{'intauth_cost'} = 10;
 6110:             }
 6111:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 6112:                 $defaults{'intauth_check'} = 0;
 6113:             }
 6114:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 6115:                 $defaults{'intauth_switch'} = 0;
 6116:             }
 6117:         } else {
 6118:             %defaults = (
 6119:                           'intauth_cost'   => 10,
 6120:                           'intauth_check'  => 0,
 6121:                           'intauth_switch' => 0,
 6122:                         );
 6123:         }
 6124:         foreach my $item (@items) {
 6125:             if ($rownum%2) {
 6126:                 $css_class = '';
 6127:             } else {
 6128:                 $css_class = ' class="LC_odd_row" ';
 6129:             }
 6130:             $datatable .= '<tr'.$css_class.'>'.
 6131:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 6132:                           '</span></td><td class="LC_left_item" colspan="3">';
 6133:             if ($item eq 'intauth_switch') {
 6134:                 my @options = (0,1,2);
 6135:                 my %optiondesc = &Apache::lonlocal::texthash (
 6136:                                    0 => 'No',
 6137:                                    1 => 'Yes',
 6138:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 6139:                                  );
 6140:                 $datatable .= '<table width="100%">';
 6141:                 foreach my $option (@options) {
 6142:                     my $checked = ' ';
 6143:                     if ($defaults{$item} eq $option) {
 6144:                         $checked = ' checked="checked"';
 6145:                     }
 6146:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 6147:                                   '<label><input type="radio" name="'.$item.
 6148:                                   '" value="'.$option.'"'.$checked.' />'.
 6149:                                   $optiondesc{$option}.'</label></span></td></tr>';
 6150:                 }
 6151:                 $datatable .= '</table>';
 6152:             } elsif ($item eq 'intauth_check') {
 6153:                 my @options = (0,1,2);
 6154:                 my %optiondesc = &Apache::lonlocal::texthash (
 6155:                                    0 => 'No',
 6156:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 6157:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 6158:                                  );
 6159:                 $datatable .= '<table wisth="100%">';
 6160:                 foreach my $option (@options) {
 6161:                     my $checked = ' ';
 6162:                     my $onclick;
 6163:                     if ($defaults{$item} eq $option) {
 6164:                         $checked = ' checked="checked"';
 6165:                     }
 6166:                     if ($option == 2) {
 6167:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 6168:                     }
 6169:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 6170:                                   '<label><input type="radio" name="'.$item.
 6171:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 6172:                                   $optiondesc{$option}.'</label></span></td></tr>';
 6173:                 }
 6174:                 $datatable .= '</table>';
 6175:             } else {
 6176:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 6177:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />';
 6178:             }
 6179:             $datatable .= '</td></tr>';
 6180:             $rownum ++;
 6181:         }
 6182:     } else {
 6183:         my %defaults;
 6184:         if (ref($settings) eq 'HASH') {
 6185:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH') &&
 6186:                 (ref($settings->{'inststatusguest'}) eq 'ARRAY')) {
 6187:                 my $maxnum = @{$settings->{'inststatusorder'}};
 6188:                 for (my $i=0; $i<$maxnum; $i++) {
 6189:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 6190:                     my $item = $settings->{'inststatusorder'}->[$i];
 6191:                     my $title = $settings->{'inststatustypes'}->{$item};
 6192:                     my $guestok;
 6193:                     if (grep(/^\Q$item\E$/,@{$settings->{'inststatusguest'}})) {
 6194:                         $guestok = 1;
 6195:                     }
 6196:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 6197:                     $datatable .= '<tr'.$css_class.'>'.
 6198:                                   '<td><span class="LC_nobreak">'.
 6199:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 6200:                     for (my $k=0; $k<=$maxnum; $k++) {
 6201:                         my $vpos = $k+1;
 6202:                         my $selstr;
 6203:                         if ($k == $i) {
 6204:                             $selstr = ' selected="selected" ';
 6205:                         }
 6206:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6207:                     }
 6208:                     my ($checkedon,$checkedoff);
 6209:                     $checkedoff = ' checked="checked"';
 6210:                     if ($guestok) {
 6211:                         $checkedon = $checkedoff;
 6212:                         $checkedoff = ''; 
 6213:                     }
 6214:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 6215:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 6216:                                   &mt('delete').'</span></td>'.
 6217:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed:').
 6218:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 6219:                                   '</span></td>'.
 6220:                                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6221:                                   '<label><input type="radio" value="1" name="inststatus_guest_'.$item.'"'.$checkedon.' />'.
 6222:                                   &mt('Yes').'</label>'.('&nbsp;'x2).
 6223:                                   '<label><input type="radio" value="0" name="inststatus_guest_'.$item.'"'.$checkedoff.' />'.
 6224:                                   &mt('No').'</label></span></td></tr>';
 6225:                 }
 6226:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 6227:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 6228:                 $datatable .= '<tr '.$css_class.'>'.
 6229:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 6230:                 for (my $k=0; $k<=$maxnum; $k++) {
 6231:                     my $vpos = $k+1;
 6232:                     my $selstr;
 6233:                     if ($k == $maxnum) {
 6234:                         $selstr = ' selected="selected" ';
 6235:                     }
 6236:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6237:                 }
 6238:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 6239:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 6240:                               '&nbsp;'.&mt('(new)').
 6241:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 6242:                               &mt('Name displayed:').
 6243:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 6244:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 6245:                               '<label><input type="radio" value="1" name="addinststatus_guest" />'.
 6246:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 6247:                               '<label><input type="radio" value="0" name="addinststatus_guest" />'.
 6248:                               &mt('No').'</label></span></td></tr>';
 6249:                               '</tr>'."\n";
 6250:                 $rownum ++;
 6251:             }
 6252:         }
 6253:     }
 6254:     $$rowtotal += $rownum;
 6255:     return $datatable;
 6256: }
 6257: 
 6258: sub get_languages_hash {
 6259:     my %langchoices;
 6260:     foreach my $id (&Apache::loncommon::languageids()) {
 6261:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 6262:         if ($code ne '') {
 6263:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 6264:         }
 6265:     }
 6266:     return %langchoices;
 6267: }
 6268: 
 6269: sub defaults_titles {
 6270:     my ($dom) = @_;
 6271:     my %titles = &Apache::lonlocal::texthash (
 6272:                    'auth_def'      => 'Default authentication type',
 6273:                    'auth_arg_def'  => 'Default authentication argument',
 6274:                    'lang_def'      => 'Default language',
 6275:                    'timezone_def'  => 'Default timezone',
 6276:                    'datelocale_def' => 'Default locale for dates',
 6277:                    'portal_def'     => 'Portal/Default URL',
 6278:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
 6279:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
 6280:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
 6281:                  );
 6282:     if ($dom) {
 6283:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 6284:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 6285:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 6286:         $protocol = 'http' if ($protocol ne 'https');
 6287:         if ($uint_dom) {
 6288:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 6289:                                          $uint_dom);
 6290:         }
 6291:     }
 6292:     return (\%titles);
 6293: }
 6294: 
 6295: sub print_scantronformat {
 6296:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 6297:     my $itemcount = 1;
 6298:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 6299:         %confhash);
 6300:     my $switchserver = &check_switchserver($dom,$confname);
 6301:     my %lt = &Apache::lonlocal::texthash (
 6302:                 default => 'Default bubblesheet format file error',
 6303:                 custom  => 'Custom bubblesheet format file error',
 6304:              );
 6305:     my %scantronfiles = (
 6306:         default => 'default.tab',
 6307:         custom => 'custom.tab',
 6308:     );
 6309:     foreach my $key (keys(%scantronfiles)) {
 6310:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 6311:                               .$scantronfiles{$key};
 6312:     }
 6313:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 6314:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 6315:         if (!$switchserver) {
 6316:             my $servadm = $r->dir_config('lonAdmEMail');
 6317:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 6318:             if ($configuserok eq 'ok') {
 6319:                 if ($author_ok eq 'ok') {
 6320:                     my %legacyfile = (
 6321:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 6322:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 6323:                     );
 6324:                     my %md5chk;
 6325:                     foreach my $type (keys(%legacyfile)) {
 6326:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 6327:                         chomp($md5chk{$type});
 6328:                     }
 6329:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 6330:                         foreach my $type (keys(%legacyfile)) {
 6331:                             ($scantronurls{$type},my $error) = 
 6332:                                 &legacy_scantronformat($r,$dom,$confname,
 6333:                                                  $type,$legacyfile{$type},
 6334:                                                  $scantronurls{$type},
 6335:                                                  $scantronfiles{$type});
 6336:                             if ($error ne '') {
 6337:                                 $error{$type} = $error;
 6338:                             }
 6339:                         }
 6340:                         if (keys(%error) == 0) {
 6341:                             $is_custom = 1;
 6342:                             $confhash{'scantron'}{'scantronformat'} = 
 6343:                                 $scantronurls{'custom'};
 6344:                             my $putresult = 
 6345:                                 &Apache::lonnet::put_dom('configuration',
 6346:                                                          \%confhash,$dom);
 6347:                             if ($putresult ne 'ok') {
 6348:                                 $error{'custom'} = 
 6349:                                     '<span class="LC_error">'.
 6350:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 6351:                             }
 6352:                         }
 6353:                     } else {
 6354:                         ($scantronurls{'default'},my $error) =
 6355:                             &legacy_scantronformat($r,$dom,$confname,
 6356:                                           'default',$legacyfile{'default'},
 6357:                                           $scantronurls{'default'},
 6358:                                           $scantronfiles{'default'});
 6359:                         if ($error eq '') {
 6360:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 6361:                             my $putresult =
 6362:                                 &Apache::lonnet::put_dom('configuration',
 6363:                                                          \%confhash,$dom);
 6364:                             if ($putresult ne 'ok') {
 6365:                                 $error{'default'} =
 6366:                                     '<span class="LC_error">'.
 6367:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 6368:                             }
 6369:                         } else {
 6370:                             $error{'default'} = $error;
 6371:                         }
 6372:                     }
 6373:                 }
 6374:             }
 6375:         } else {
 6376:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 6377:         }
 6378:     }
 6379:     if (ref($settings) eq 'HASH') {
 6380:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 6381:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 6382:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 6383:                 $scantronurl = '';
 6384:             } else {
 6385:                 $scantronurl = $settings->{'scantronformat'};
 6386:             }
 6387:             $is_custom = 1;
 6388:         } else {
 6389:             $scantronurl = $scantronurls{'default'};
 6390:         }
 6391:     } else {
 6392:         if ($is_custom) {
 6393:             $scantronurl = $scantronurls{'custom'};
 6394:         } else {
 6395:             $scantronurl = $scantronurls{'default'};
 6396:         }
 6397:     }
 6398:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6399:     $datatable .= '<tr'.$css_class.'>';
 6400:     if (!$is_custom) {
 6401:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 6402:                       '<span class="LC_nobreak">';
 6403:         if ($scantronurl) {
 6404:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 6405:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 6406:         } else {
 6407:             $datatable = &mt('File unavailable for display');
 6408:         }
 6409:         $datatable .= '</span></td>';
 6410:         if (keys(%error) == 0) { 
 6411:             $datatable .= '<td valign="bottom">';
 6412:             if (!$switchserver) {
 6413:                 $datatable .= &mt('Upload:').'<br />';
 6414:             }
 6415:         } else {
 6416:             my $errorstr;
 6417:             foreach my $key (sort(keys(%error))) {
 6418:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 6419:             }
 6420:             $datatable .= '<td>'.$errorstr;
 6421:         }
 6422:     } else {
 6423:         if (keys(%error) > 0) {
 6424:             my $errorstr;
 6425:             foreach my $key (sort(keys(%error))) {
 6426:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 6427:             } 
 6428:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 6429:         } elsif ($scantronurl) {
 6430:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 6431:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 6432:             $datatable .= '<td><span class="LC_nobreak">'.
 6433:                           $link.
 6434:                           '<label><input type="checkbox" name="scantronformat_del"'.
 6435:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 6436:                           '<td><span class="LC_nobreak">&nbsp;'.
 6437:                           &mt('Replace:').'</span><br />';
 6438:         }
 6439:     }
 6440:     if (keys(%error) == 0) {
 6441:         if ($switchserver) {
 6442:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 6443:         } else {
 6444:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 6445:                          '<input type="file" name="scantronformat" /></span>';
 6446:         }
 6447:     }
 6448:     $datatable .= '</td></tr>';
 6449:     $$rowtotal ++;
 6450:     return $datatable;
 6451: }
 6452: 
 6453: sub legacy_scantronformat {
 6454:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 6455:     my ($url,$error);
 6456:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 6457:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 6458:         (my $result,$url) =
 6459:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 6460:                          '','',$newfile);
 6461:         if ($result ne 'ok') {
 6462:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 6463:         }
 6464:     }
 6465:     return ($url,$error);
 6466: }
 6467: 
 6468: sub print_coursecategories {
 6469:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 6470:     my $datatable;
 6471:     if ($position eq 'top') {
 6472:         my (%checked);
 6473:         my @catitems = ('unauth','auth');
 6474:         my @cattypes = ('std','domonly','codesrch','none');
 6475:         $checked{'unauth'} = 'std';
 6476:         $checked{'auth'} = 'std';
 6477:         if (ref($settings) eq 'HASH') {
 6478:             foreach my $type (@cattypes) {
 6479:                 if ($type eq $settings->{'unauth'}) {
 6480:                     $checked{'unauth'} = $type;
 6481:                 }
 6482:                 if ($type eq $settings->{'auth'}) {
 6483:                     $checked{'auth'} = $type;
 6484:                 }
 6485:             }
 6486:         }
 6487:         my %lt = &Apache::lonlocal::texthash (
 6488:                                                unauth   => 'Catalog type for unauthenticated users',
 6489:                                                auth     => 'Catalog type for authenticated users',
 6490:                                                none     => 'No catalog',
 6491:                                                std      => 'Standard catalog',
 6492:                                                domonly  => 'Domain-only catalog',
 6493:                                                codesrch => "Code search form",
 6494:                                              );
 6495:        my $itemcount = 0;
 6496:        foreach my $item (@catitems) {
 6497:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 6498:            $datatable .= '<tr '.$css_class.'>'.
 6499:                          '<td>'.$lt{$item}.'</td>'.
 6500:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 6501:            foreach my $type (@cattypes) {
 6502:                my $ischecked;
 6503:                if ($checked{$item} eq $type) {
 6504:                    $ischecked=' checked="checked"';
 6505:                }
 6506:                $datatable .= '<label>'.
 6507:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 6508:                              ' />'.$lt{$type}.'</label>&nbsp;';
 6509:            }
 6510:            $datatable .= '</td></tr>';
 6511:            $itemcount ++;
 6512:         }
 6513:         $$rowtotal += $itemcount;
 6514:     } elsif ($position eq 'middle') {
 6515:         my $toggle_cats_crs = ' ';
 6516:         my $toggle_cats_dom = ' checked="checked" ';
 6517:         my $can_cat_crs = ' ';
 6518:         my $can_cat_dom = ' checked="checked" ';
 6519:         my $toggle_catscomm_comm = ' ';
 6520:         my $toggle_catscomm_dom = ' checked="checked" ';
 6521:         my $can_catcomm_comm = ' ';
 6522:         my $can_catcomm_dom = ' checked="checked" ';
 6523: 
 6524:         if (ref($settings) eq 'HASH') {
 6525:             if ($settings->{'togglecats'} eq 'crs') {
 6526:                 $toggle_cats_crs = $toggle_cats_dom;
 6527:                 $toggle_cats_dom = ' ';
 6528:             }
 6529:             if ($settings->{'categorize'} eq 'crs') {
 6530:                 $can_cat_crs = $can_cat_dom;
 6531:                 $can_cat_dom = ' ';
 6532:             }
 6533:             if ($settings->{'togglecatscomm'} eq 'comm') {
 6534:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 6535:                 $toggle_catscomm_dom = ' ';
 6536:             }
 6537:             if ($settings->{'categorizecomm'} eq 'comm') {
 6538:                 $can_catcomm_comm = $can_catcomm_dom;
 6539:                 $can_catcomm_dom = ' ';
 6540:             }
 6541:         }
 6542:         my %title = &Apache::lonlocal::texthash (
 6543:                      togglecats     => 'Show/Hide a course in catalog',
 6544:                      togglecatscomm => 'Show/Hide a community in catalog',
 6545:                      categorize     => 'Assign a category to a course',
 6546:                      categorizecomm => 'Assign a category to a community',
 6547:                     );
 6548:         my %level = &Apache::lonlocal::texthash (
 6549:                      dom  => 'Set in Domain',
 6550:                      crs  => 'Set in Course',
 6551:                      comm => 'Set in Community',
 6552:                     );
 6553:         $datatable = '<tr class="LC_odd_row">'.
 6554:                   '<td>'.$title{'togglecats'}.'</td>'.
 6555:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 6556:                   '<input type="radio" name="togglecats"'.
 6557:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6558:                   '<label><input type="radio" name="togglecats"'.
 6559:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 6560:                   '</tr><tr>'.
 6561:                   '<td>'.$title{'categorize'}.'</td>'.
 6562:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6563:                   '<label><input type="radio" name="categorize"'.
 6564:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6565:                   '<label><input type="radio" name="categorize"'.
 6566:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 6567:                   '</tr><tr class="LC_odd_row">'.
 6568:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 6569:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 6570:                   '<input type="radio" name="togglecatscomm"'.
 6571:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6572:                   '<label><input type="radio" name="togglecatscomm"'.
 6573:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 6574:                   '</tr><tr>'.
 6575:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 6576:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6577:                   '<label><input type="radio" name="categorizecomm"'.
 6578:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6579:                   '<label><input type="radio" name="categorizecomm"'.
 6580:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 6581:                   '</tr>';
 6582:         $$rowtotal += 4;
 6583:     } else {
 6584:         my $css_class;
 6585:         my $itemcount = 1;
 6586:         my $cathash; 
 6587:         if (ref($settings) eq 'HASH') {
 6588:             $cathash = $settings->{'cats'};
 6589:         }
 6590:         if (ref($cathash) eq 'HASH') {
 6591:             my (@cats,@trails,%allitems,%idx,@jsarray);
 6592:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 6593:                                                    \%allitems,\%idx,\@jsarray);
 6594:             my $maxdepth = scalar(@cats);
 6595:             my $colattrib = '';
 6596:             if ($maxdepth > 2) {
 6597:                 $colattrib = ' colspan="2" ';
 6598:             }
 6599:             my @path;
 6600:             if (@cats > 0) {
 6601:                 if (ref($cats[0]) eq 'ARRAY') {
 6602:                     my $numtop = @{$cats[0]};
 6603:                     my $maxnum = $numtop;
 6604:                     my %default_names = (
 6605:                           instcode    => &mt('Official courses'),
 6606:                           communities => &mt('Communities'),
 6607:                     );
 6608: 
 6609:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 6610:                         ($cathash->{'instcode::0'} eq '') ||
 6611:                         (!grep(/^communities$/,@{$cats[0]})) || 
 6612:                         ($cathash->{'communities::0'} eq '')) {
 6613:                         $maxnum ++;
 6614:                     }
 6615:                     my $lastidx;
 6616:                     for (my $i=0; $i<$numtop; $i++) {
 6617:                         my $parent = $cats[0][$i];
 6618:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6619:                         my $item = &escape($parent).'::0';
 6620:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 6621:                         $lastidx = $idx{$item};
 6622:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6623:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 6624:                         for (my $k=0; $k<=$maxnum; $k++) {
 6625:                             my $vpos = $k+1;
 6626:                             my $selstr;
 6627:                             if ($k == $i) {
 6628:                                 $selstr = ' selected="selected" ';
 6629:                             }
 6630:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6631:                         }
 6632:                         $datatable .= '</select></span></td><td>';
 6633:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 6634:                             $datatable .=  '<span class="LC_nobreak">'
 6635:                                            .$default_names{$parent}.'</span>';
 6636:                             if ($parent eq 'instcode') {
 6637:                                 $datatable .= '<br /><span class="LC_nobreak">('
 6638:                                               .&mt('with institutional codes')
 6639:                                               .')</span></td><td'.$colattrib.'>';
 6640:                             } else {
 6641:                                 $datatable .= '<table><tr><td>';
 6642:                             }
 6643:                             $datatable .= '<span class="LC_nobreak">'
 6644:                                           .'<label><input type="radio" name="'
 6645:                                           .$parent.'" value="1" checked="checked" />'
 6646:                                           .&mt('Display').'</label>';
 6647:                             if ($parent eq 'instcode') {
 6648:                                 $datatable .= '&nbsp;';
 6649:                             } else {
 6650:                                 $datatable .= '</span></td></tr><tr><td>'
 6651:                                               .'<span class="LC_nobreak">';
 6652:                             }
 6653:                             $datatable .= '<label><input type="radio" name="'
 6654:                                           .$parent.'" value="0" />'
 6655:                                           .&mt('Do not display').'</label></span>';
 6656:                             if ($parent eq 'communities') {
 6657:                                 $datatable .= '</td></tr></table>';
 6658:                             }
 6659:                             $datatable .= '</td>';
 6660:                         } else {
 6661:                             $datatable .= $parent
 6662:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 6663:                                           .'<input type="checkbox" name="deletecategory" '
 6664:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 6665:                         }
 6666:                         my $depth = 1;
 6667:                         push(@path,$parent);
 6668:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 6669:                         pop(@path);
 6670:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 6671:                         $itemcount ++;
 6672:                     }
 6673:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6674:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 6675:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 6676:                     for (my $k=0; $k<=$maxnum; $k++) {
 6677:                         my $vpos = $k+1;
 6678:                         my $selstr;
 6679:                         if ($k == $numtop) {
 6680:                             $selstr = ' selected="selected" ';
 6681:                         }
 6682:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6683:                     }
 6684:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 6685:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 6686:                                   .'</tr>'."\n";
 6687:                     $itemcount ++;
 6688:                     foreach my $default ('instcode','communities') {
 6689:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 6690:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6691:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 6692:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 6693:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 6694:                             for (my $k=0; $k<=$maxnum; $k++) {
 6695:                                 my $vpos = $k+1;
 6696:                                 my $selstr;
 6697:                                 if ($k == $maxnum) {
 6698:                                     $selstr = ' selected="selected" ';
 6699:                                 }
 6700:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6701:                             }
 6702:                             $datatable .= '</select></span></td>'.
 6703:                                           '<td><span class="LC_nobreak">'.
 6704:                                           $default_names{$default}.'</span>';
 6705:                             if ($default eq 'instcode') {
 6706:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 6707:                                               .&mt('with institutional codes').')</span>';
 6708:                             }
 6709:                             $datatable .= '</td>'
 6710:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 6711:                                           .&mt('Display').'</label>&nbsp;'
 6712:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 6713:                                           .&mt('Do not display').'</label></span></td></tr>';
 6714:                         }
 6715:                     }
 6716:                 }
 6717:             } else {
 6718:                 $datatable .= &initialize_categories($itemcount);
 6719:             }
 6720:         } else {
 6721:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td>'
 6722:                           .&initialize_categories($itemcount);
 6723:         }
 6724:         $$rowtotal += $itemcount;
 6725:     }
 6726:     return $datatable;
 6727: }
 6728: 
 6729: sub print_serverstatuses {
 6730:     my ($dom,$settings,$rowtotal) = @_;
 6731:     my $datatable;
 6732:     my @pages = &serverstatus_pages();
 6733:     my (%namedaccess,%machineaccess);
 6734:     foreach my $type (@pages) {
 6735:         $namedaccess{$type} = '';
 6736:         $machineaccess{$type}= '';
 6737:     }
 6738:     if (ref($settings) eq 'HASH') {
 6739:         foreach my $type (@pages) {
 6740:             if (exists($settings->{$type})) {
 6741:                 if (ref($settings->{$type}) eq 'HASH') {
 6742:                     foreach my $key (keys(%{$settings->{$type}})) {
 6743:                         if ($key eq 'namedusers') {
 6744:                             $namedaccess{$type} = $settings->{$type}->{$key};
 6745:                         } elsif ($key eq 'machines') {
 6746:                             $machineaccess{$type} = $settings->{$type}->{$key};
 6747:                         }
 6748:                     }
 6749:                 }
 6750:             }
 6751:         }
 6752:     }
 6753:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 6754:     my $rownum = 0;
 6755:     my $css_class;
 6756:     foreach my $type (@pages) {
 6757:         $rownum ++;
 6758:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 6759:         $datatable .= '<tr'.$css_class.'>'.
 6760:                       '<td><span class="LC_nobreak">'.
 6761:                       $titles->{$type}.'</span></td>'.
 6762:                       '<td class="LC_left_item">'.
 6763:                       '<input type="text" name="'.$type.'_namedusers" '.
 6764:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 6765:                       '<td class="LC_right_item">'.
 6766:                       '<span class="LC_nobreak">'.
 6767:                       '<input type="text" name="'.$type.'_machines" '.
 6768:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 6769:                       '</td></tr>'."\n";
 6770:     }
 6771:     $$rowtotal += $rownum;
 6772:     return $datatable;
 6773: }
 6774: 
 6775: sub serverstatus_pages {
 6776:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 6777:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 6778:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 6779:             'uniquecodes','diskusage','coursecatalog');
 6780: }
 6781: 
 6782: sub defaults_javascript {
 6783:     my ($settings) = @_;
 6784:     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.');
 6785:     my $intauthcost = &mt('Warning: bcrypt encryption cost for internal authentication must be an integer.');
 6786:     &js_escape(\$intauthcheck);
 6787:     &js_escape(\$intauthcost);
 6788:     my $intauthjs = <<"ENDSCRIPT";
 6789: 
 6790: function warnIntAuth(field) {
 6791:     if (field.name == 'intauth_check') {
 6792:         if (field.value == '2') {
 6793:             alert('$intauthcheck');
 6794:         }
 6795:     }
 6796:     if (field.name == 'intauth_cost') {
 6797:         field.value.replace(/\s/g,'');
 6798:         if (field.value != '') {
 6799:             var regexdigit=/^\\d+\$/;
 6800:             if (!regexdigit.test(field.value)) {
 6801:                 alert('$intauthcost');
 6802:             }
 6803:         }
 6804:     }
 6805:     return;
 6806: }
 6807: 
 6808: ENDSCRIPT
 6809: 
 6810:     if (ref($settings) ne 'HASH') {
 6811:         return &Apache::lonhtmlcommon::scripttag($intauthjs);
 6812:     }
 6813:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 6814:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 6815:         if ($maxnum eq '') {
 6816:             $maxnum = 0;
 6817:         }
 6818:         $maxnum ++;
 6819:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 6820:         return <<"ENDSCRIPT";
 6821: <script type="text/javascript">
 6822: // <![CDATA[
 6823: function reorderTypes(form,caller) {
 6824:     var changedVal;
 6825: $jstext 
 6826:     var newpos = 'addinststatus_pos';
 6827:     var current = new Array;
 6828:     var maxh = $maxnum;
 6829:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 6830:     var oldVal;
 6831:     if (caller == newpos) {
 6832:         changedVal = newitemVal;
 6833:     } else {
 6834:         var curritem = 'inststatus_pos_'+caller;
 6835:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 6836:         current[newitemVal] = newpos;
 6837:     }
 6838:     for (var i=0; i<inststatuses.length; i++) {
 6839:         if (inststatuses[i] != caller) {
 6840:             var elementName = 'inststatus_pos_'+inststatuses[i];
 6841:             if (form.elements[elementName]) {
 6842:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 6843:                 current[currVal] = elementName;
 6844:             }
 6845:         }
 6846:     }
 6847:     for (var j=0; j<maxh; j++) {
 6848:         if (current[j] == undefined) {
 6849:             oldVal = j;
 6850:         }
 6851:     }
 6852:     if (oldVal < changedVal) {
 6853:         for (var k=oldVal+1; k<=changedVal ; k++) {
 6854:            var elementName = current[k];
 6855:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 6856:         }
 6857:     } else {
 6858:         for (var k=changedVal; k<oldVal; k++) {
 6859:             var elementName = current[k];
 6860:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 6861:         }
 6862:     }
 6863:     return;
 6864: }
 6865: 
 6866: $intauthjs
 6867: 
 6868: // ]]>
 6869: </script>
 6870: 
 6871: ENDSCRIPT
 6872:     } else {
 6873:         return &Apache::lonhtmlcommon::scripttag($intauthjs);
 6874:     }
 6875: }
 6876: 
 6877: sub coursecategories_javascript {
 6878:     my ($settings) = @_;
 6879:     my ($output,$jstext,$cathash);
 6880:     if (ref($settings) eq 'HASH') {
 6881:         $cathash = $settings->{'cats'};
 6882:     }
 6883:     if (ref($cathash) eq 'HASH') {
 6884:         my (@cats,@jsarray,%idx);
 6885:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 6886:         if (@jsarray > 0) {
 6887:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 6888:             for (my $i=0; $i<@jsarray; $i++) {
 6889:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 6890:                     my $catstr = join('","',@{$jsarray[$i]});
 6891:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 6892:                 }
 6893:             }
 6894:         }
 6895:     } else {
 6896:         $jstext  = '    var categories = Array(1);'."\n".
 6897:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 6898:     }
 6899:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 6900:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 6901:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
 6902:     &js_escape(\$instcode_reserved);
 6903:     &js_escape(\$communities_reserved);
 6904:     &js_escape(\$choose_again);
 6905:     $output = <<"ENDSCRIPT";
 6906: <script type="text/javascript">
 6907: // <![CDATA[
 6908: function reorderCats(form,parent,item,idx) {
 6909:     var changedVal;
 6910: $jstext
 6911:     var newpos = 'addcategory_pos';
 6912:     if (parent == '') {
 6913:         var has_instcode = 0;
 6914:         var maxtop = categories[idx].length;
 6915:         for (var j=0; j<maxtop; j++) {
 6916:             if (categories[idx][j] == 'instcode::0') {
 6917:                 has_instcode == 1;
 6918:             }
 6919:         }
 6920:         if (has_instcode == 0) {
 6921:             categories[idx][maxtop] = 'instcode_pos';
 6922:         }
 6923:     } else {
 6924:         newpos += '_'+parent;
 6925:     }
 6926:     var maxh = 1 + categories[idx].length;
 6927:     var current = new Array;
 6928:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 6929:     if (item == newpos) {
 6930:         changedVal = newitemVal;
 6931:     } else {
 6932:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 6933:         current[newitemVal] = newpos;
 6934:     }
 6935:     for (var i=0; i<categories[idx].length; i++) {
 6936:         var elementName = categories[idx][i];
 6937:         if (elementName != item) {
 6938:             if (form.elements[elementName]) {
 6939:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 6940:                 current[currVal] = elementName;
 6941:             }
 6942:         }
 6943:     }
 6944:     var oldVal;
 6945:     for (var j=0; j<maxh; j++) {
 6946:         if (current[j] == undefined) {
 6947:             oldVal = j;
 6948:         }
 6949:     }
 6950:     if (oldVal < changedVal) {
 6951:         for (var k=oldVal+1; k<=changedVal ; k++) {
 6952:            var elementName = current[k];
 6953:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 6954:         }
 6955:     } else {
 6956:         for (var k=changedVal; k<oldVal; k++) {
 6957:             var elementName = current[k];
 6958:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 6959:         }
 6960:     }
 6961:     return;
 6962: }
 6963: 
 6964: function categoryCheck(form) {
 6965:     if (form.elements['addcategory_name'].value == 'instcode') {
 6966:         alert('$instcode_reserved\\n$choose_again');
 6967:         return false;
 6968:     }
 6969:     if (form.elements['addcategory_name'].value == 'communities') {
 6970:         alert('$communities_reserved\\n$choose_again');
 6971:         return false;
 6972:     }
 6973:     return true;
 6974: }
 6975: 
 6976: // ]]>
 6977: </script>
 6978: 
 6979: ENDSCRIPT
 6980:     return $output;
 6981: }
 6982: 
 6983: sub initialize_categories {
 6984:     my ($itemcount) = @_;
 6985:     my ($datatable,$css_class,$chgstr);
 6986:     my %default_names = (
 6987:                       instcode    => 'Official courses (with institutional codes)',
 6988:                       communities => 'Communities',
 6989:                         );
 6990:     my $select0 = ' selected="selected"';
 6991:     my $select1 = '';
 6992:     foreach my $default ('instcode','communities') {
 6993:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6994:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 6995:         if ($default eq 'communities') {
 6996:             $select1 = $select0;
 6997:             $select0 = '';
 6998:         }
 6999:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7000:                      .'<select name="'.$default.'_pos">'
 7001:                      .'<option value="0"'.$select0.'>1</option>'
 7002:                      .'<option value="1"'.$select1.'>2</option>'
 7003:                      .'<option value="2">3</option></select>&nbsp;'
 7004:                      .$default_names{$default}
 7005:                      .'</span></td><td><span class="LC_nobreak">'
 7006:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 7007:                      .&mt('Display').'</label>&nbsp;<label>'
 7008:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 7009:                  .'</label></span></td></tr>';
 7010:         $itemcount ++;
 7011:     }
 7012:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7013:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 7014:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7015:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 7016:                   .'<option value="0">1</option>'
 7017:                   .'<option value="1">2</option>'
 7018:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 7019:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 7020:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 7021:     return $datatable;
 7022: }
 7023: 
 7024: sub build_category_rows {
 7025:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 7026:     my ($text,$name,$item,$chgstr);
 7027:     if (ref($cats) eq 'ARRAY') {
 7028:         my $maxdepth = scalar(@{$cats});
 7029:         if (ref($cats->[$depth]) eq 'HASH') {
 7030:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 7031:                 my $numchildren = @{$cats->[$depth]{$parent}};
 7032:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7033:                 $text .= '<td><table class="LC_data_table">';
 7034:                 my ($idxnum,$parent_name,$parent_item);
 7035:                 my $higher = $depth - 1;
 7036:                 if ($higher == 0) {
 7037:                     $parent_name = &escape($parent).'::'.$higher;
 7038:                 } else {
 7039:                     if (ref($path) eq 'ARRAY') {
 7040:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 7041:                     }
 7042:                 }
 7043:                 $parent_item = 'addcategory_pos_'.$parent_name;
 7044:                 for (my $j=0; $j<=$numchildren; $j++) {
 7045:                     if ($j < $numchildren) {
 7046:                         $name = $cats->[$depth]{$parent}[$j];
 7047:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 7048:                         $idxnum = $idx->{$item};
 7049:                     } else {
 7050:                         $name = $parent_name;
 7051:                         $item = $parent_item;
 7052:                     }
 7053:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 7054:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 7055:                     for (my $i=0; $i<=$numchildren; $i++) {
 7056:                         my $vpos = $i+1;
 7057:                         my $selstr;
 7058:                         if ($j == $i) {
 7059:                             $selstr = ' selected="selected" ';
 7060:                         }
 7061:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 7062:                     }
 7063:                     $text .= '</select>&nbsp;';
 7064:                     if ($j < $numchildren) {
 7065:                         my $deeper = $depth+1;
 7066:                         $text .= $name.'&nbsp;'
 7067:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 7068:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 7069:                         if(ref($path) eq 'ARRAY') {
 7070:                             push(@{$path},$name);
 7071:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 7072:                             pop(@{$path});
 7073:                         }
 7074:                     } else {
 7075:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 7076:                         if ($j == $numchildren) {
 7077:                             $text .= $name;
 7078:                         } else {
 7079:                             $text .= $item;
 7080:                         }
 7081:                         $text .= '" value="" />';
 7082:                     }
 7083:                     $text .= '</td></tr>';
 7084:                 }
 7085:                 $text .= '</table></td>';
 7086:             } else {
 7087:                 my $higher = $depth-1;
 7088:                 if ($higher == 0) {
 7089:                     $name = &escape($parent).'::'.$higher;
 7090:                 } else {
 7091:                     if (ref($path) eq 'ARRAY') {
 7092:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 7093:                     }
 7094:                 }
 7095:                 my $colspan;
 7096:                 if ($parent ne 'instcode') {
 7097:                     $colspan = $maxdepth - $depth - 1;
 7098:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 7099:                 }
 7100:             }
 7101:         }
 7102:     }
 7103:     return $text;
 7104: }
 7105: 
 7106: sub modifiable_userdata_row {
 7107:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref) = @_;
 7108:     my ($role,$rolename,$statustype);
 7109:     $role = $item;
 7110:     if ($context eq 'cancreate') {
 7111:         if ($item =~ /^emailusername_(.+)$/) {
 7112:             $statustype = $1;
 7113:             $role = 'emailusername';
 7114:             if (ref($usertypes) eq 'HASH') {
 7115:                 if ($usertypes->{$statustype}) {
 7116:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 7117:                 } else {
 7118:                     $rolename = &mt('Data provided by user');
 7119:                 }
 7120:             }
 7121:         }
 7122:     } elsif ($context eq 'selfcreate') {
 7123:         if (ref($usertypes) eq 'HASH') {
 7124:             $rolename = $usertypes->{$role};
 7125:         } else {
 7126:             $rolename = $role;
 7127:         }
 7128:     } else {
 7129:         if ($role eq 'cr') {
 7130:             $rolename = &mt('Custom role');
 7131:         } else {
 7132:             $rolename = &Apache::lonnet::plaintext($role);
 7133:         }
 7134:     }
 7135:     my (@fields,%fieldtitles);
 7136:     if (ref($fieldsref) eq 'ARRAY') {
 7137:         @fields = @{$fieldsref};
 7138:     } else {
 7139:         @fields = ('lastname','firstname','middlename','generation',
 7140:                    'permanentemail','id');
 7141:     }
 7142:     if ((ref($titlesref) eq 'HASH')) {
 7143:         %fieldtitles = %{$titlesref};
 7144:     } else {
 7145:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7146:     }
 7147:     my $output;
 7148:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 7149:     $output = '<tr '.$css_class.'>'.
 7150:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 7151:               '<td class="LC_left_item" colspan="2"><table>';
 7152:     my $rem;
 7153:     my %checks;
 7154:     if (ref($settings) eq 'HASH') {
 7155:         if (ref($settings->{$context}) eq 'HASH') {
 7156:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 7157:                 my $hashref = $settings->{$context}->{$role};
 7158:                 if ($role eq 'emailusername') {
 7159:                     if ($statustype) {
 7160:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 7161:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 7162:                             if (ref($hashref) eq 'HASH') { 
 7163:                                 foreach my $field (@fields) {
 7164:                                     if ($hashref->{$field}) {
 7165:                                         $checks{$field} = $hashref->{$field};
 7166:                                     }
 7167:                                 }
 7168:                             }
 7169:                         }
 7170:                     }
 7171:                 } else {
 7172:                     if (ref($hashref) eq 'HASH') {
 7173:                         foreach my $field (@fields) {
 7174:                             if ($hashref->{$field}) {
 7175:                                 $checks{$field} = ' checked="checked" ';
 7176:                             }
 7177:                         }
 7178:                     }
 7179:                 }
 7180:             }
 7181:         }
 7182:     }
 7183:      
 7184:     for (my $i=0; $i<@fields; $i++) {
 7185:         my $rem = $i%($numinrow);
 7186:         if ($rem == 0) {
 7187:             if ($i > 0) {
 7188:                 $output .= '</tr>';
 7189:             }
 7190:             $output .= '<tr>';
 7191:         }
 7192:         my $check = ' ';
 7193:         unless ($role eq 'emailusername') {
 7194:             if (exists($checks{$fields[$i]})) {
 7195:                 $check = $checks{$fields[$i]}
 7196:             } else {
 7197:                 if ($role eq 'st') {
 7198:                     if (ref($settings) ne 'HASH') {
 7199:                         $check = ' checked="checked" '; 
 7200:                     }
 7201:                 }
 7202:             }
 7203:         }
 7204:         $output .= '<td class="LC_left_item">'.
 7205:                    '<span class="LC_nobreak">';
 7206:         if ($role eq 'emailusername') {
 7207:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 7208:                 $checks{$fields[$i]} = 'omit';
 7209:             }
 7210:             foreach my $option ('required','optional','omit') {
 7211:                 my $checked='';
 7212:                 if ($checks{$fields[$i]} eq $option) {
 7213:                     $checked='checked="checked" ';
 7214:                 }
 7215:                 $output .= '<label>'.
 7216:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 7217:                            &mt($option).'</label>'.('&nbsp;' x2);
 7218:             }
 7219:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 7220:         } else {
 7221:             $output .= '<label>'.
 7222:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 7223:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 7224:                        '</label>';
 7225:         }
 7226:         $output .= '</span></td>';
 7227:         $rem = @fields%($numinrow);
 7228:     }
 7229:     my $colsleft = $numinrow - $rem;
 7230:     if ($colsleft > 1 ) {
 7231:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7232:                    '&nbsp;</td>';
 7233:     } elsif ($colsleft == 1) {
 7234:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 7235:     }
 7236:     $output .= '</tr></table></td></tr>';
 7237:     return $output;
 7238: }
 7239: 
 7240: sub insttypes_row {
 7241:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
 7242:         $customcss,$rowstyle) = @_;
 7243:     my %lt = &Apache::lonlocal::texthash (
 7244:                       cansearch => 'Users allowed to search',
 7245:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 7246:                       lockablenames => 'User preference to lock name',
 7247:                       overrides     => "Override domain's helpdesk settings based on requester's affiliation",
 7248:              );
 7249:     my $showdom;
 7250:     if ($context eq 'cansearch') {
 7251:         $showdom = ' ('.$dom.')';
 7252:     }
 7253:     my $class = 'LC_left_item';
 7254:     if ($context eq 'statustocreate') {
 7255:         $class = 'LC_right_item';
 7256:     }
 7257:     my $css_class;
 7258:     if ($$rowtotal%2) {
 7259:         $css_class = 'LC_odd_row';
 7260:     }
 7261:     if ($customcss) {
 7262:         $css_class .= ' '.$customcss;
 7263:     }
 7264:     $css_class =~ s/^\s+//;
 7265:     if ($css_class) {
 7266:         $css_class = ' class="'.$css_class.'"';
 7267:     }
 7268:     if ($rowstyle) {
 7269:         $css_class .= ' style="'.$rowstyle.'"';
 7270:     }
 7271:     if ($onclick) {
 7272:         $onclick = 'onclick="'.$onclick.'" ';
 7273:     }
 7274:     my $output = '<tr'.$css_class.'>'.
 7275:                  '<td>'.$lt{$context}.$showdom.
 7276:                  '</td><td class="'.$class.'" colspan="2"><table>';
 7277:     my $rem;
 7278:     if (ref($types) eq 'ARRAY') {
 7279:         for (my $i=0; $i<@{$types}; $i++) {
 7280:             if (defined($usertypes->{$types->[$i]})) {
 7281:                 my $rem = $i%($numinrow);
 7282:                 if ($rem == 0) {
 7283:                     if ($i > 0) {
 7284:                         $output .= '</tr>';
 7285:                     }
 7286:                     $output .= '<tr>';
 7287:                 }
 7288:                 my $check = ' ';
 7289:                 if (ref($settings) eq 'HASH') {
 7290:                     if (ref($settings->{$context}) eq 'ARRAY') {
 7291:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 7292:                             $check = ' checked="checked" ';
 7293:                         }
 7294:                     } elsif (ref($settings->{$context}) eq 'HASH') {
 7295:                         if (ref($settings->{$context}->{$types->[$i]}) eq 'HASH') {
 7296:                             $check = ' checked="checked" ';
 7297:                         }
 7298:                     } elsif ($context eq 'statustocreate') {
 7299:                         $check = ' checked="checked" ';
 7300:                     }
 7301:                 }
 7302:                 $output .= '<td class="LC_left_item">'.
 7303:                            '<span class="LC_nobreak"><label>'.
 7304:                            '<input type="checkbox" name="'.$context.'" '.
 7305:                            'value="'.$types->[$i].'"'.$check.'/>'.
 7306:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 7307:             }
 7308:         }
 7309:         $rem = @{$types}%($numinrow);
 7310:     }
 7311:     my $colsleft = $numinrow - $rem;
 7312:     if ($context eq 'overrides') {
 7313:         if ($colsleft > 1) {
 7314:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 7315:         } else {
 7316:             $output .= '<td class="LC_left_item">';
 7317:         }
 7318:         $output .= '&nbsp;';
 7319:     } else {
 7320:         if (($rem == 0) && (@{$types} > 0)) {
 7321:             $output .= '<tr>';
 7322:         }
 7323:         if ($colsleft > 1) {
 7324:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 7325:         } else {
 7326:             $output .= '<td class="LC_left_item">';
 7327:         }
 7328:         my $defcheck = ' ';
 7329:         if (ref($settings) eq 'HASH') {  
 7330:             if (ref($settings->{$context}) eq 'ARRAY') {
 7331:                 if (grep(/^default$/,@{$settings->{$context}})) {
 7332:                     $defcheck = ' checked="checked" ';
 7333:                 }
 7334:             } elsif ($context eq 'statustocreate') {
 7335:                 $defcheck = ' checked="checked" ';
 7336:             }
 7337:         }
 7338:         $output .= '<span class="LC_nobreak"><label>'.
 7339:                    '<input type="checkbox" name="'.$context.'" '.
 7340:                    'value="default"'.$defcheck.'/>'.
 7341:                    $othertitle.'</label></span>';
 7342:     }
 7343:     $output .= '</td></tr></table></td></tr>';
 7344:     return $output;
 7345: }
 7346: 
 7347: sub sorted_searchtitles {
 7348:     my %searchtitles = &Apache::lonlocal::texthash(
 7349:                          'uname' => 'username',
 7350:                          'lastname' => 'last name',
 7351:                          'lastfirst' => 'last name, first name',
 7352:                      );
 7353:     my @titleorder = ('uname','lastname','lastfirst');
 7354:     return (\%searchtitles,\@titleorder);
 7355: }
 7356: 
 7357: sub sorted_searchtypes {
 7358:     my %srchtypes_desc = (
 7359:                            exact    => 'is exact match',
 7360:                            contains => 'contains ..',
 7361:                            begins   => 'begins with ..',
 7362:                          );
 7363:     my @srchtypeorder = ('exact','begins','contains');
 7364:     return (\%srchtypes_desc,\@srchtypeorder);
 7365: }
 7366: 
 7367: sub usertype_update_row {
 7368:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 7369:     my $datatable;
 7370:     my $numinrow = 4;
 7371:     foreach my $type (@{$types}) {
 7372:         if (defined($usertypes->{$type})) {
 7373:             $$rownums ++;
 7374:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 7375:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 7376:                           '</td><td class="LC_left_item"><table>';
 7377:             for (my $i=0; $i<@{$fields}; $i++) {
 7378:                 my $rem = $i%($numinrow);
 7379:                 if ($rem == 0) {
 7380:                     if ($i > 0) {
 7381:                         $datatable .= '</tr>';
 7382:                     }
 7383:                     $datatable .= '<tr>';
 7384:                 }
 7385:                 my $check = ' ';
 7386:                 if (ref($settings) eq 'HASH') {
 7387:                     if (ref($settings->{'fields'}) eq 'HASH') {
 7388:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 7389:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 7390:                                 $check = ' checked="checked" ';
 7391:                             }
 7392:                         }
 7393:                     }
 7394:                 }
 7395: 
 7396:                 if ($i == @{$fields}-1) {
 7397:                     my $colsleft = $numinrow - $rem;
 7398:                     if ($colsleft > 1) {
 7399:                         $datatable .= '<td colspan="'.$colsleft.'">';
 7400:                     } else {
 7401:                         $datatable .= '<td>';
 7402:                     }
 7403:                 } else {
 7404:                     $datatable .= '<td>';
 7405:                 }
 7406:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7407:                               '<input type="checkbox" name="updateable_'.$type.
 7408:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 7409:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 7410:             }
 7411:             $datatable .= '</tr></table></td></tr>';
 7412:         }
 7413:     }
 7414:     return $datatable;
 7415: }
 7416: 
 7417: sub modify_login {
 7418:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 7419:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 7420:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 7421:     %title = ( coursecatalog => 'Display course catalog',
 7422:                adminmail => 'Display administrator E-mail address',
 7423:                helpdesk  => 'Display "Contact Helpdesk" link',
 7424:                newuser => 'Link for visitors to create a user account',
 7425:                loginheader => 'Log-in box header');
 7426:     @offon = ('off','on');
 7427:     if (ref($domconfig{login}) eq 'HASH') {
 7428:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 7429:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 7430:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 7431:             }
 7432:         }
 7433:     }
 7434:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 7435:                                            \%domconfig,\%loginhash);
 7436:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 7437:     foreach my $item (@toggles) {
 7438:         $loginhash{login}{$item} = $env{'form.'.$item};
 7439:     }
 7440:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 7441:     if (ref($colchanges{'login'}) eq 'HASH') {  
 7442:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 7443:                                          \%loginhash);
 7444:     }
 7445: 
 7446:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7447:     my %domservers = &Apache::lonnet::get_servers($dom);
 7448:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 7449:     if (keys(%servers) > 1) {
 7450:         foreach my $lonhost (keys(%servers)) {
 7451:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 7452:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 7453:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 7454:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 7455:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 7456:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7457:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7458:                         $changes{'loginvia'}{$lonhost} = 1;
 7459:                     } else {
 7460:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 7461:                         $changes{'loginvia'}{$lonhost} = 1;
 7462:                     }
 7463:                 } else {
 7464:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7465:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7466:                         $changes{'loginvia'}{$lonhost} = 1;
 7467:                     }
 7468:                 }
 7469:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 7470:                     foreach my $item (@loginvia_attribs) {
 7471:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 7472:                     }
 7473:                 } else {
 7474:                     foreach my $item (@loginvia_attribs) {
 7475:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 7476:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 7477:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 7478:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 7479:                                 $new = '/';
 7480:                             }
 7481:                         }
 7482:                         if (($item eq 'custompath') && 
 7483:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 7484:                             $new = '';
 7485:                         }
 7486:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 7487:                             $changes{'loginvia'}{$lonhost} = 1;
 7488:                         }
 7489:                         if ($item eq 'exempt') {
 7490:                             $new = &check_exempt_addresses($new);
 7491:                         }
 7492:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 7493:                     }
 7494:                 }
 7495:             } else {
 7496:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7497:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7498:                     $changes{'loginvia'}{$lonhost} = 1;
 7499:                     foreach my $item (@loginvia_attribs) {
 7500:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 7501:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 7502:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 7503:                                 $new = '/';
 7504:                             }
 7505:                         }
 7506:                         if (($item eq 'custompath') && 
 7507:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 7508:                             $new = '';
 7509:                         }
 7510:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 7511:                     }
 7512:                 }
 7513:             }
 7514:         }
 7515:     }
 7516: 
 7517:     my $servadm = $r->dir_config('lonAdmEMail');
 7518:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 7519:     if (ref($domconfig{'login'}) eq 'HASH') {
 7520:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 7521:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 7522:                 if ($lang eq 'nolang') {
 7523:                     push(@currlangs,$lang);
 7524:                 } elsif (defined($langchoices{$lang})) {
 7525:                     push(@currlangs,$lang);
 7526:                 } else {
 7527:                     next;
 7528:                 }
 7529:             }
 7530:         }
 7531:     }
 7532:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 7533:     if (@currlangs > 0) {
 7534:         foreach my $lang (@currlangs) {
 7535:             if (grep(/^\Q$lang\E$/,@delurls)) {
 7536:                 $changes{'helpurl'}{$lang} = 1;
 7537:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 7538:                 $changes{'helpurl'}{$lang} = 1;
 7539:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 7540:                 push(@newlangs,$lang);
 7541:             } else {
 7542:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 7543:             }
 7544:         }
 7545:     }
 7546:     unless (grep(/^nolang$/,@currlangs)) {
 7547:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 7548:             $changes{'helpurl'}{'nolang'} = 1;
 7549:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 7550:             push(@newlangs,'nolang');
 7551:         }
 7552:     }
 7553:     if ($env{'form.loginhelpurl_add_lang'}) {
 7554:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 7555:             ($env{'form.loginhelpurl_add_file.filename'})) {
 7556:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 7557:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 7558:         }
 7559:     }
 7560:     if ((@newlangs > 0) || ($addedfile)) {
 7561:         my $error;
 7562:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 7563:         if ($configuserok eq 'ok') {
 7564:             if ($switchserver) {
 7565:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 7566:             } elsif ($author_ok eq 'ok') {
 7567:                 my @allnew = @newlangs;
 7568:                 if ($addedfile ne '') {
 7569:                     push(@allnew,$addedfile);
 7570:                 }
 7571:                 foreach my $lang (@allnew) {
 7572:                     my $formelem = 'loginhelpurl_'.$lang;
 7573:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 7574:                         $formelem = 'loginhelpurl_add_file';
 7575:                     }
 7576:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 7577:                                                                "help/$lang",'','',$newfile{$lang});
 7578:                     if ($result eq 'ok') {
 7579:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 7580:                         $changes{'helpurl'}{$lang} = 1;
 7581:                     } else {
 7582:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 7583:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 7584:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 7585:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 7586:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 7587:                         }
 7588:                     }
 7589:                 }
 7590:             } else {
 7591:                 $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);
 7592:             }
 7593:         } else {
 7594:             $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);
 7595:         }
 7596:         if ($error) {
 7597:             &Apache::lonnet::logthis($error);
 7598:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7599:         }
 7600:     }
 7601: 
 7602:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
 7603:     if (ref($domconfig{'login'}) eq 'HASH') {
 7604:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
 7605:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
 7606:                 if ($domservers{$lonhost}) {
 7607:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 7608:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
 7609:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
 7610:                     }
 7611:                 }
 7612:             }
 7613:         }
 7614:     }
 7615:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
 7616:     foreach my $lonhost (sort(keys(%domservers))) {
 7617:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 7618:             $changes{'headtag'}{$lonhost} = 1;
 7619:         } else {
 7620:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
 7621:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
 7622:             }
 7623:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
 7624:                 push(@newhosts,$lonhost);
 7625:             } elsif ($currheadtagurls{$lonhost}) {
 7626:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
 7627:                 if ($currexempt{$lonhost}) {
 7628:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
 7629:                         $changes{'headtag'}{$lonhost} = 1;
 7630:                     }
 7631:                 } elsif ($possexempt{$lonhost}) {
 7632:                     $changes{'headtag'}{$lonhost} = 1;
 7633:                 }
 7634:                 if ($possexempt{$lonhost}) {
 7635:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 7636:                 }
 7637:             }
 7638:         }
 7639:     }
 7640:     if (@newhosts) {
 7641:         my $error;
 7642:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 7643:         if ($configuserok eq 'ok') {
 7644:             if ($switchserver) {
 7645:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
 7646:             } elsif ($author_ok eq 'ok') {
 7647:                 foreach my $lonhost (@newhosts) {
 7648:                     my $formelem = 'loginheadtag_'.$lonhost;
 7649:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 7650:                                                                           "login/headtag/$lonhost",'','',
 7651:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
 7652:                     if ($result eq 'ok') {
 7653:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
 7654:                         $changes{'headtag'}{$lonhost} = 1;
 7655:                         if ($possexempt{$lonhost}) {
 7656:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 7657:                         }
 7658:                     } else {
 7659:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
 7660:                                            $newheadtagurls{$lonhost},$result);
 7661:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 7662:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
 7663:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
 7664:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
 7665:                         }
 7666:                     }
 7667:                 }
 7668:             } else {
 7669:                 $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);
 7670:             }
 7671:         } else {
 7672:             $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);
 7673:         }
 7674:         if ($error) {
 7675:             &Apache::lonnet::logthis($error);
 7676:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7677:         }
 7678:     }
 7679:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 7680: 
 7681:     my $defaulthelpfile = '/adm/loginproblems.html';
 7682:     my $defaulttext = &mt('Default in use');
 7683: 
 7684:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 7685:                                              $dom);
 7686:     if ($putresult eq 'ok') {
 7687:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 7688:         my %defaultchecked = (
 7689:                     'coursecatalog' => 'on',
 7690:                     'helpdesk'      => 'on',
 7691:                     'adminmail'     => 'off',
 7692:                     'newuser'       => 'off',
 7693:         );
 7694:         if (ref($domconfig{'login'}) eq 'HASH') {
 7695:             foreach my $item (@toggles) {
 7696:                 if ($defaultchecked{$item} eq 'on') { 
 7697:                     if (($domconfig{'login'}{$item} eq '0') &&
 7698:                         ($env{'form.'.$item} eq '1')) {
 7699:                         $changes{$item} = 1;
 7700:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 7701:                               $domconfig{'login'}{$item} eq '1') &&
 7702:                              ($env{'form.'.$item} eq '0')) {
 7703:                         $changes{$item} = 1;
 7704:                     }
 7705:                 } elsif ($defaultchecked{$item} eq 'off') {
 7706:                     if (($domconfig{'login'}{$item} eq '1') &&
 7707:                         ($env{'form.'.$item} eq '0')) {
 7708:                         $changes{$item} = 1;
 7709:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 7710:                               $domconfig{'login'}{$item} eq '0') &&
 7711:                              ($env{'form.'.$item} eq '1')) {
 7712:                         $changes{$item} = 1;
 7713:                     }
 7714:                 }
 7715:             }
 7716:         }
 7717:         if (keys(%changes) > 0 || $colchgtext) {
 7718:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 7719:             if (ref($lastactref) eq 'HASH') {
 7720:                 $lastactref->{'domainconfig'} = 1;
 7721:             }
 7722:             $resulttext = &mt('Changes made:').'<ul>';
 7723:             foreach my $item (sort(keys(%changes))) {
 7724:                 if ($item eq 'loginvia') {
 7725:                     if (ref($changes{$item}) eq 'HASH') {
 7726:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 7727:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 7728:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 7729:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 7730:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 7731:                                     $protocol = 'http' if ($protocol ne 'https');
 7732:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 7733: 
 7734:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 7735:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 7736:                                     } else {
 7737:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 7738:                                     }
 7739:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 7740:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 7741:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 7742:                                     }
 7743:                                     $resulttext .= '</li>';
 7744:                                 } else {
 7745:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 7746:                                 }
 7747:                             } else {
 7748:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 7749:                             }
 7750:                         }
 7751:                         $resulttext .= '</ul></li>';
 7752:                     }
 7753:                 } elsif ($item eq 'helpurl') {
 7754:                     if (ref($changes{$item}) eq 'HASH') {
 7755:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 7756:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 7757:                                 my ($chg,$link);
 7758:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 7759:                                 if ($lang eq 'nolang') {
 7760:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 7761:                                 } else {
 7762:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 7763:                                 }
 7764:                                 $resulttext .= '<li>'.$chg.'</li>';
 7765:                             } else {
 7766:                                 my $chg;
 7767:                                 if ($lang eq 'nolang') {
 7768:                                     $chg = &mt('custom log-in help file for no preferred language');
 7769:                                 } else {
 7770:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 7771:                                 }
 7772:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 7773:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 7774:                                                       '?inhibitmenu=yes',$chg,600,500).
 7775:                                                '</li>';
 7776:                             }
 7777:                         }
 7778:                     }
 7779:                 } elsif ($item eq 'headtag') {
 7780:                     if (ref($changes{$item}) eq 'HASH') {
 7781:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 7782:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 7783:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
 7784:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 7785:                                 $resulttext .= '<li><a href="'.
 7786:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
 7787:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 7788:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
 7789:                                 if ($possexempt{$lonhost}) {
 7790:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
 7791:                                 } else {
 7792:                                     $resulttext .= &mt('included for any client IP');
 7793:                                 }
 7794:                                 $resulttext .= '</li>';
 7795:                             }
 7796:                         }
 7797:                     }
 7798:                 } elsif ($item eq 'captcha') {
 7799:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7800:                         my $chgtxt;
 7801:                         if ($loginhash{'login'}{$item} eq 'notused') {
 7802:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 7803:                         } else {
 7804:                             my %captchas = &captcha_phrases();
 7805:                             if ($captchas{$loginhash{'login'}{$item}}) {
 7806:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 7807:                             } else {
 7808:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 7809:                             }
 7810:                         }
 7811:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 7812:                     }
 7813:                 } elsif ($item eq 'recaptchakeys') {
 7814:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7815:                         my ($privkey,$pubkey);
 7816:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 7817:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 7818:                             $privkey = $loginhash{'login'}{$item}{'private'};
 7819:                         }
 7820:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 7821:                         if (!$pubkey) {
 7822:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 7823:                         } else {
 7824:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 7825:                         }
 7826:                         if (!$privkey) {
 7827:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 7828:                         } else {
 7829:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 7830:                         }
 7831:                         $chgtxt .= '</ul>';
 7832:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 7833:                     }
 7834:                 } elsif ($item eq 'recaptchaversion') {
 7835:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7836:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
 7837:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
 7838:                                            '</li>';
 7839:                         }
 7840:                     }
 7841:                 } else {
 7842:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 7843:                 }
 7844:             }
 7845:             $resulttext .= $colchgtext.'</ul>';
 7846:         } else {
 7847:             $resulttext = &mt('No changes made to log-in page settings');
 7848:         }
 7849:     } else {
 7850:         $resulttext = '<span class="LC_error">'.
 7851: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7852:     }
 7853:     if ($errors) {
 7854:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 7855:                        $errors.'</ul>';
 7856:     }
 7857:     return $resulttext;
 7858: }
 7859: 
 7860: sub check_exempt_addresses {
 7861:     my ($iplist) = @_;
 7862:     $iplist =~ s/^\s+//;
 7863:     $iplist =~ s/\s+$//;
 7864:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
 7865:     my (@okips,$new);
 7866:     foreach my $ip (@poss_ips) {
 7867:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 7868:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 7869:                 push(@okips,$ip);
 7870:             }
 7871:         }
 7872:     }
 7873:     if (@okips > 0) {
 7874:         $new = join(',',@okips);
 7875:     } else {
 7876:         $new = '';
 7877:     }
 7878:     return $new;
 7879: }
 7880: 
 7881: sub color_font_choices {
 7882:     my %choices =
 7883:         &Apache::lonlocal::texthash (
 7884:             img => "Header",
 7885:             bgs => "Background colors",
 7886:             links => "Link colors",
 7887:             images => "Images",
 7888:             font => "Font color",
 7889:             fontmenu => "Font menu",
 7890:             pgbg => "Page",
 7891:             tabbg => "Header",
 7892:             sidebg => "Border",
 7893:             link => "Link",
 7894:             alink => "Active link",
 7895:             vlink => "Visited link",
 7896:         );
 7897:     return %choices;
 7898: }
 7899: 
 7900: sub modify_rolecolors {
 7901:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 7902:     my ($resulttext,%rolehash);
 7903:     $rolehash{'rolecolors'} = {};
 7904:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 7905:         if ($domconfig{'rolecolors'} eq '') {
 7906:             $domconfig{'rolecolors'} = {};
 7907:         }
 7908:     }
 7909:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 7910:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 7911:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 7912:                                              $dom);
 7913:     if ($putresult eq 'ok') {
 7914:         if (keys(%changes) > 0) {
 7915:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 7916:             if (ref($lastactref) eq 'HASH') {
 7917:                 $lastactref->{'domainconfig'} = 1;
 7918:             }
 7919:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 7920:                                              $rolehash{'rolecolors'});
 7921:         } else {
 7922:             $resulttext = &mt('No changes made to default color schemes');
 7923:         }
 7924:     } else {
 7925:         $resulttext = '<span class="LC_error">'.
 7926: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7927:     }
 7928:     if ($errors) {
 7929:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 7930:                        $errors.'</ul>';
 7931:     }
 7932:     return $resulttext;
 7933: }
 7934: 
 7935: sub modify_colors {
 7936:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 7937:     my (%changes,%choices);
 7938:     my @bgs;
 7939:     my @links = ('link','alink','vlink');
 7940:     my @logintext;
 7941:     my @images;
 7942:     my $servadm = $r->dir_config('lonAdmEMail');
 7943:     my $errors;
 7944:     my %defaults;
 7945:     foreach my $role (@{$roles}) {
 7946:         if ($role eq 'login') {
 7947:             %choices = &login_choices();
 7948:             @logintext = ('textcol','bgcol');
 7949:         } else {
 7950:             %choices = &color_font_choices();
 7951:         }
 7952:         if ($role eq 'login') {
 7953:             @images = ('img','logo','domlogo','login');
 7954:             @bgs = ('pgbg','mainbg','sidebg');
 7955:         } else {
 7956:             @images = ('img');
 7957:             @bgs = ('pgbg','tabbg','sidebg');
 7958:         }
 7959:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 7960:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 7961:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 7962:         }
 7963:         if ($role eq 'login') {
 7964:             foreach my $item (@logintext) {
 7965:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 7966:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 7967:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 7968:                 }
 7969:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 7970:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 7971:                 }
 7972:             }
 7973:         } else {
 7974:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 7975:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 7976:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 7977:             }
 7978:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 7979:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 7980:             }
 7981:         }
 7982:         foreach my $item (@bgs) {
 7983:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 7984:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 7985:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 7986:             }
 7987:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 7988:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 7989:             }
 7990:         }
 7991:         foreach my $item (@links) {
 7992:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 7993:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 7994:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 7995:             }
 7996:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 7997:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 7998:             }
 7999:         }
 8000:         my ($configuserok,$author_ok,$switchserver) = 
 8001:             &config_check($dom,$confname,$servadm);
 8002:         my ($width,$height) = &thumb_dimensions();
 8003:         if (ref($domconfig->{$role}) ne 'HASH') {
 8004:             $domconfig->{$role} = {};
 8005:         }
 8006:         foreach my $img (@images) {
 8007:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 8008:                 if (defined($env{'form.login_showlogo_'.$img})) {
 8009:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 8010:                 } else { 
 8011:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 8012:                 }
 8013:             } 
 8014: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 8015: 		 && !defined($domconfig->{$role}{$img})
 8016: 		 && !$env{'form.'.$role.'_del_'.$img}
 8017: 		 && $env{'form.'.$role.'_import_'.$img}) {
 8018: 		# import the old configured image from the .tab setting
 8019: 		# if they haven't provided a new one 
 8020: 		$domconfig->{$role}{$img} = 
 8021: 		    $env{'form.'.$role.'_import_'.$img};
 8022: 	    }
 8023:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 8024:                 my $error;
 8025:                 if ($configuserok eq 'ok') {
 8026:                     if ($switchserver) {
 8027:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 8028:                     } else {
 8029:                         if ($author_ok eq 'ok') {
 8030:                             my ($result,$logourl) = 
 8031:                                 &publishlogo($r,'upload',$role.'_'.$img,
 8032:                                            $dom,$confname,$img,$width,$height);
 8033:                             if ($result eq 'ok') {
 8034:                                 $confhash->{$role}{$img} = $logourl;
 8035:                                 $changes{$role}{'images'}{$img} = 1;
 8036:                             } else {
 8037:                                 $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);
 8038:                             }
 8039:                         } else {
 8040:                             $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);
 8041:                         }
 8042:                     }
 8043:                 } else {
 8044:                     $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);
 8045:                 }
 8046:                 if ($error) {
 8047:                     &Apache::lonnet::logthis($error);
 8048:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8049:                 }
 8050:             } elsif ($domconfig->{$role}{$img} ne '') {
 8051:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 8052:                     my $error;
 8053:                     if ($configuserok eq 'ok') {
 8054: # is confname an author?
 8055:                         if ($switchserver eq '') {
 8056:                             if ($author_ok eq 'ok') {
 8057:                                 my ($result,$logourl) = 
 8058:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 8059:                                             $dom,$confname,$img,$width,$height);
 8060:                                 if ($result eq 'ok') {
 8061:                                     $confhash->{$role}{$img} = $logourl;
 8062: 				    $changes{$role}{'images'}{$img} = 1;
 8063:                                 }
 8064:                             }
 8065:                         }
 8066:                     }
 8067:                 }
 8068:             }
 8069:         }
 8070:         if (ref($domconfig) eq 'HASH') {
 8071:             if (ref($domconfig->{$role}) eq 'HASH') {
 8072:                 foreach my $img (@images) {
 8073:                     if ($domconfig->{$role}{$img} ne '') {
 8074:                         if ($env{'form.'.$role.'_del_'.$img}) {
 8075:                             $confhash->{$role}{$img} = '';
 8076:                             $changes{$role}{'images'}{$img} = 1;
 8077:                         } else {
 8078:                             if ($confhash->{$role}{$img} eq '') {
 8079:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 8080:                             }
 8081:                         }
 8082:                     } else {
 8083:                         if ($env{'form.'.$role.'_del_'.$img}) {
 8084:                             $confhash->{$role}{$img} = '';
 8085:                             $changes{$role}{'images'}{$img} = 1;
 8086:                         } 
 8087:                     }
 8088:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 8089:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 8090:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 8091:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 8092:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 8093:                             }
 8094:                         } else {
 8095:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 8096:                                 $changes{$role}{'showlogo'}{$img} = 1;
 8097:                             }
 8098:                         }
 8099:                     }
 8100:                 }
 8101:                 if ($domconfig->{$role}{'font'} ne '') {
 8102:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 8103:                         $changes{$role}{'font'} = 1;
 8104:                     }
 8105:                 } else {
 8106:                     if ($confhash->{$role}{'font'}) {
 8107:                         $changes{$role}{'font'} = 1;
 8108:                     }
 8109:                 }
 8110:                 if ($role ne 'login') {
 8111:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 8112:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 8113:                             $changes{$role}{'fontmenu'} = 1;
 8114:                         }
 8115:                     } else {
 8116:                         if ($confhash->{$role}{'fontmenu'}) {
 8117:                             $changes{$role}{'fontmenu'} = 1;
 8118:                         }
 8119:                     }
 8120:                 }
 8121:                 foreach my $item (@bgs) {
 8122:                     if ($domconfig->{$role}{$item} ne '') {
 8123:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8124:                             $changes{$role}{'bgs'}{$item} = 1;
 8125:                         } 
 8126:                     } else {
 8127:                         if ($confhash->{$role}{$item}) {
 8128:                             $changes{$role}{'bgs'}{$item} = 1;
 8129:                         }
 8130:                     }
 8131:                 }
 8132:                 foreach my $item (@links) {
 8133:                     if ($domconfig->{$role}{$item} ne '') {
 8134:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8135:                             $changes{$role}{'links'}{$item} = 1;
 8136:                         }
 8137:                     } else {
 8138:                         if ($confhash->{$role}{$item}) {
 8139:                             $changes{$role}{'links'}{$item} = 1;
 8140:                         }
 8141:                     }
 8142:                 }
 8143:                 foreach my $item (@logintext) {
 8144:                     if ($domconfig->{$role}{$item} ne '') {
 8145:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8146:                             $changes{$role}{'logintext'}{$item} = 1;
 8147:                         }
 8148:                     } else {
 8149:                         if ($confhash->{$role}{$item}) {
 8150:                             $changes{$role}{'logintext'}{$item} = 1;
 8151:                         }
 8152:                     }
 8153:                 }
 8154:             } else {
 8155:                 &default_change_checker($role,\@images,\@links,\@bgs,
 8156:                                         \@logintext,$confhash,\%changes); 
 8157:             }
 8158:         } else {
 8159:             &default_change_checker($role,\@images,\@links,\@bgs,
 8160:                                     \@logintext,$confhash,\%changes); 
 8161:         }
 8162:     }
 8163:     return ($errors,%changes);
 8164: }
 8165: 
 8166: sub config_check {
 8167:     my ($dom,$confname,$servadm) = @_;
 8168:     my ($configuserok,$author_ok,$switchserver,%currroles);
 8169:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 8170:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 8171:                                                    $confname,$servadm);
 8172:     if ($configuserok eq 'ok') {
 8173:         $switchserver = &check_switchserver($dom,$confname);
 8174:         if ($switchserver eq '') {
 8175:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 8176:         }
 8177:     }
 8178:     return ($configuserok,$author_ok,$switchserver);
 8179: }
 8180: 
 8181: sub default_change_checker {
 8182:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 8183:     foreach my $item (@{$links}) {
 8184:         if ($confhash->{$role}{$item}) {
 8185:             $changes->{$role}{'links'}{$item} = 1;
 8186:         }
 8187:     }
 8188:     foreach my $item (@{$bgs}) {
 8189:         if ($confhash->{$role}{$item}) {
 8190:             $changes->{$role}{'bgs'}{$item} = 1;
 8191:         }
 8192:     }
 8193:     foreach my $item (@{$logintext}) {
 8194:         if ($confhash->{$role}{$item}) {
 8195:             $changes->{$role}{'logintext'}{$item} = 1;
 8196:         }
 8197:     }
 8198:     foreach my $img (@{$images}) {
 8199:         if ($env{'form.'.$role.'_del_'.$img}) {
 8200:             $confhash->{$role}{$img} = '';
 8201:             $changes->{$role}{'images'}{$img} = 1;
 8202:         }
 8203:         if ($role eq 'login') {
 8204:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 8205:                 $changes->{$role}{'showlogo'}{$img} = 1;
 8206:             }
 8207:         }
 8208:     }
 8209:     if ($confhash->{$role}{'font'}) {
 8210:         $changes->{$role}{'font'} = 1;
 8211:     }
 8212: }
 8213: 
 8214: sub display_colorchgs {
 8215:     my ($dom,$changes,$roles,$confhash) = @_;
 8216:     my (%choices,$resulttext);
 8217:     if (!grep(/^login$/,@{$roles})) {
 8218:         $resulttext = &mt('Changes made:').'<br />';
 8219:     }
 8220:     foreach my $role (@{$roles}) {
 8221:         if ($role eq 'login') {
 8222:             %choices = &login_choices();
 8223:         } else {
 8224:             %choices = &color_font_choices();
 8225:         }
 8226:         if (ref($changes->{$role}) eq 'HASH') {
 8227:             if ($role ne 'login') {
 8228:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 8229:             }
 8230:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 8231:                 if ($role ne 'login') {
 8232:                     $resulttext .= '<ul>';
 8233:                 }
 8234:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 8235:                     if ($role ne 'login') {
 8236:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 8237:                     }
 8238:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 8239:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 8240:                             if ($confhash->{$role}{$key}{$item}) {
 8241:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 8242:                             } else {
 8243:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 8244:                             }
 8245:                         } elsif ($confhash->{$role}{$item} eq '') {
 8246:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 8247:                         } else {
 8248:                             my $newitem = $confhash->{$role}{$item};
 8249:                             if ($key eq 'images') {
 8250:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 8251:                             }
 8252:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 8253:                         }
 8254:                     }
 8255:                     if ($role ne 'login') {
 8256:                         $resulttext .= '</ul></li>';
 8257:                     }
 8258:                 } else {
 8259:                     if ($confhash->{$role}{$key} eq '') {
 8260:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 8261:                     } else {
 8262:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 8263:                     }
 8264:                 }
 8265:                 if ($role ne 'login') {
 8266:                     $resulttext .= '</ul>';
 8267:                 }
 8268:             }
 8269:         }
 8270:     }
 8271:     return $resulttext;
 8272: }
 8273: 
 8274: sub thumb_dimensions {
 8275:     return ('200','50');
 8276: }
 8277: 
 8278: sub check_dimensions {
 8279:     my ($inputfile) = @_;
 8280:     my ($fullwidth,$fullheight);
 8281:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 8282:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 8283:             my $imageinfo = <PIPE>;
 8284:             if (!close(PIPE)) {
 8285:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 8286:             }
 8287:             chomp($imageinfo);
 8288:             my ($fullsize) = 
 8289:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 8290:             if ($fullsize) {
 8291:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 8292:             }
 8293:         }
 8294:     }
 8295:     return ($fullwidth,$fullheight);
 8296: }
 8297: 
 8298: sub check_configuser {
 8299:     my ($uhome,$dom,$confname,$servadm) = @_;
 8300:     my ($configuserok,%currroles);
 8301:     if ($uhome eq 'no_host') {
 8302:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 8303:         my $configpass = &LONCAPA::Enrollment::create_password();
 8304:         $configuserok = 
 8305:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 8306:                              $configpass,'','','','','',undef,$servadm);
 8307:     } else {
 8308:         $configuserok = 'ok';
 8309:         %currroles = 
 8310:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 8311:     }
 8312:     return ($configuserok,%currroles);
 8313: }
 8314: 
 8315: sub check_authorstatus {
 8316:     my ($dom,$confname,%currroles) = @_;
 8317:     my $author_ok;
 8318:     if (!$currroles{':'.$dom.':au'}) {
 8319:         my $start = time;
 8320:         my $end = 0;
 8321:         $author_ok = 
 8322:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 8323:                                         'au',$end,$start,'','','domconfig');
 8324:     } else {
 8325:         $author_ok = 'ok';
 8326:     }
 8327:     return $author_ok;
 8328: }
 8329: 
 8330: sub publishlogo {
 8331:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 8332:     my ($output,$fname,$logourl);
 8333:     if ($action eq 'upload') {
 8334:         $fname=$env{'form.'.$formname.'.filename'};
 8335:         chop($env{'form.'.$formname});
 8336:     } else {
 8337:         ($fname) = ($formname =~ /([^\/]+)$/);
 8338:     }
 8339:     if ($savefileas ne '') {
 8340:         $fname = $savefileas;
 8341:     }
 8342:     $fname=&Apache::lonnet::clean_filename($fname);
 8343: # See if there is anything left
 8344:     unless ($fname) { return ('error: no uploaded file'); }
 8345:     $fname="$subdir/$fname";
 8346:     my $docroot=$r->dir_config('lonDocRoot');
 8347:     my $filepath="$docroot/priv";
 8348:     my $relpath = "$dom/$confname";
 8349:     my ($fnamepath,$file,$fetchthumb);
 8350:     $file=$fname;
 8351:     if ($fname=~m|/|) {
 8352:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 8353:     }
 8354:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 8355:     my $count;
 8356:     for ($count=5;$count<=$#parts;$count++) {
 8357:         $filepath.="/$parts[$count]";
 8358:         if ((-e $filepath)!=1) {
 8359:             mkdir($filepath,02770);
 8360:         }
 8361:     }
 8362:     # Check for bad extension and disallow upload
 8363:     if ($file=~/\.(\w+)$/ &&
 8364:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 8365:         $output = 
 8366:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 8367:     } elsif ($file=~/\.(\w+)$/ &&
 8368:         !defined(&Apache::loncommon::fileembstyle($1))) {
 8369:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 8370:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 8371:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 8372:     } elsif (-d "$filepath/$file") {
 8373:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 8374:     } else {
 8375:         my $source = $filepath.'/'.$file;
 8376:         my $logfile;
 8377:         if (!open($logfile,">>",$source.'.log')) {
 8378:             return (&mt('No write permission to Authoring Space'));
 8379:         }
 8380:         print $logfile
 8381: "\n================= Publish ".localtime()." ================\n".
 8382: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 8383: # Save the file
 8384:         if (!open(FH,">",$source)) {
 8385:             &Apache::lonnet::logthis('Failed to create '.$source);
 8386:             return (&mt('Failed to create file'));
 8387:         }
 8388:         if ($action eq 'upload') {
 8389:             if (!print FH ($env{'form.'.$formname})) {
 8390:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 8391:                 return (&mt('Failed to write file'));
 8392:             }
 8393:         } else {
 8394:             my $original = &Apache::lonnet::filelocation('',$formname);
 8395:             if(!copy($original,$source)) {
 8396:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 8397:                 return (&mt('Failed to write file'));
 8398:             }
 8399:         }
 8400:         close(FH);
 8401:         chmod(0660, $source); # Permissions to rw-rw---.
 8402: 
 8403:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 8404:         my $copyfile=$targetdir.'/'.$file;
 8405: 
 8406:         my @parts=split(/\//,$targetdir);
 8407:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 8408:         for (my $count=5;$count<=$#parts;$count++) {
 8409:             $path.="/$parts[$count]";
 8410:             if (!-e $path) {
 8411:                 print $logfile "\nCreating directory ".$path;
 8412:                 mkdir($path,02770);
 8413:             }
 8414:         }
 8415:         my $versionresult;
 8416:         if (-e $copyfile) {
 8417:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 8418:         } else {
 8419:             $versionresult = 'ok';
 8420:         }
 8421:         if ($versionresult eq 'ok') {
 8422:             if (copy($source,$copyfile)) {
 8423:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 8424:                 $output = 'ok';
 8425:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 8426:                 push(@{$modified_urls},[$copyfile,$source]);
 8427:                 my $metaoutput = 
 8428:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 8429:                 unless ($registered_cleanup) {
 8430:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 8431:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 8432:                     $registered_cleanup=1;
 8433:                 }
 8434:             } else {
 8435:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 8436:                 $output = &mt('Failed to copy file to RES space').", $!";
 8437:             }
 8438:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 8439:                 my $inputfile = $filepath.'/'.$file;
 8440:                 my $outfile = $filepath.'/'.'tn-'.$file;
 8441:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 8442:                 if ($fullwidth ne '' && $fullheight ne '') { 
 8443:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 8444:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 8445:                         my @args = ('convert','-sample',$thumbsize,$inputfile,$outfile);
 8446:                         system({$args[0]} @args);
 8447:                         chmod(0660, $filepath.'/tn-'.$file);
 8448:                         if (-e $outfile) {
 8449:                             my $copyfile=$targetdir.'/tn-'.$file;
 8450:                             if (copy($outfile,$copyfile)) {
 8451:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 8452:                                 my $thumb_metaoutput = 
 8453:                                     &write_metadata($dom,$confname,$formname,
 8454:                                                     $targetdir,'tn-'.$file,$logfile);
 8455:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 8456:                                 unless ($registered_cleanup) {
 8457:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 8458:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 8459:                                     $registered_cleanup=1;
 8460:                                 }
 8461:                             } else {
 8462:                                 print $logfile "\nUnable to write ".$copyfile.
 8463:                                                ':'.$!."\n";
 8464:                             }
 8465:                         }
 8466:                     }
 8467:                 }
 8468:             }
 8469:         } else {
 8470:             $output = $versionresult;
 8471:         }
 8472:     }
 8473:     return ($output,$logourl);
 8474: }
 8475: 
 8476: sub logo_versioning {
 8477:     my ($targetdir,$file,$logfile) = @_;
 8478:     my $target = $targetdir.'/'.$file;
 8479:     my ($maxversion,$fn,$extn,$output);
 8480:     $maxversion = 0;
 8481:     if ($file =~ /^(.+)\.(\w+)$/) {
 8482:         $fn=$1;
 8483:         $extn=$2;
 8484:     }
 8485:     opendir(DIR,$targetdir);
 8486:     while (my $filename=readdir(DIR)) {
 8487:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 8488:             $maxversion=($1>$maxversion)?$1:$maxversion;
 8489:         }
 8490:     }
 8491:     $maxversion++;
 8492:     print $logfile "\nCreating old version ".$maxversion."\n";
 8493:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 8494:     if (copy($target,$copyfile)) {
 8495:         print $logfile "Copied old target to ".$copyfile."\n";
 8496:         $copyfile=$copyfile.'.meta';
 8497:         if (copy($target.'.meta',$copyfile)) {
 8498:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 8499:             $output = 'ok';
 8500:         } else {
 8501:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 8502:             $output = &mt('Failed to copy old meta').", $!, ";
 8503:         }
 8504:     } else {
 8505:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 8506:         $output = &mt('Failed to copy old target').", $!, ";
 8507:     }
 8508:     return $output;
 8509: }
 8510: 
 8511: sub write_metadata {
 8512:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 8513:     my (%metadatafields,%metadatakeys,$output);
 8514:     $metadatafields{'title'}=$formname;
 8515:     $metadatafields{'creationdate'}=time;
 8516:     $metadatafields{'lastrevisiondate'}=time;
 8517:     $metadatafields{'copyright'}='public';
 8518:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 8519:                                          $env{'user.domain'};
 8520:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 8521:     $metadatafields{'domain'}=$dom;
 8522:     {
 8523:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 8524:         my $mfh;
 8525:         if (open($mfh,">",$targetdir.'/'.$file.'.meta')) {
 8526:             foreach (sort(keys(%metadatafields))) {
 8527:                 unless ($_=~/\./) {
 8528:                     my $unikey=$_;
 8529:                     $unikey=~/^([A-Za-z]+)/;
 8530:                     my $tag=$1;
 8531:                     $tag=~tr/A-Z/a-z/;
 8532:                     print $mfh "\n\<$tag";
 8533:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 8534:                         my $value=$metadatafields{$unikey.'.'.$_};
 8535:                         $value=~s/\"/\'\'/g;
 8536:                         print $mfh ' '.$_.'="'.$value.'"';
 8537:                     }
 8538:                     print $mfh '>'.
 8539:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 8540:                             .'</'.$tag.'>';
 8541:                 }
 8542:             }
 8543:             $output = 'ok';
 8544:             print $logfile "\nWrote metadata";
 8545:             close($mfh);
 8546:         } else {
 8547:             print $logfile "\nFailed to open metadata file";
 8548:             $output = &mt('Could not write metadata');
 8549:         }
 8550:     }
 8551:     return $output;
 8552: }
 8553: 
 8554: sub notifysubscribed {
 8555:     foreach my $targetsource (@{$modified_urls}){
 8556:         next unless (ref($targetsource) eq 'ARRAY');
 8557:         my ($target,$source)=@{$targetsource};
 8558:         if ($source ne '') {
 8559:             if (open(my $logfh,">>",$source.'.log')) {
 8560:                 print $logfh "\nCleanup phase: Notifications\n";
 8561:                 my @subscribed=&subscribed_hosts($target);
 8562:                 foreach my $subhost (@subscribed) {
 8563:                     print $logfh "\nNotifying host ".$subhost.':';
 8564:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 8565:                     print $logfh $reply;
 8566:                 }
 8567:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 8568:                 foreach my $subhost (@subscribedmeta) {
 8569:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 8570:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 8571:                                                         $subhost);
 8572:                     print $logfh $reply;
 8573:                 }
 8574:                 print $logfh "\n============ Done ============\n";
 8575:                 close($logfh);
 8576:             }
 8577:         }
 8578:     }
 8579:     return OK;
 8580: }
 8581: 
 8582: sub subscribed_hosts {
 8583:     my ($target) = @_;
 8584:     my @subscribed;
 8585:     if (open(my $fh,"<","$target.subscription")) {
 8586:         while (my $subline=<$fh>) {
 8587:             if ($subline =~ /^($match_lonid):/) {
 8588:                 my $host = $1;
 8589:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 8590:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 8591:                         push(@subscribed,$host);
 8592:                     }
 8593:                 }
 8594:             }
 8595:         }
 8596:     }
 8597:     return @subscribed;
 8598: }
 8599: 
 8600: sub check_switchserver {
 8601:     my ($dom,$confname) = @_;
 8602:     my ($allowed,$switchserver);
 8603:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 8604:     if ($home eq 'no_host') {
 8605:         $home = &Apache::lonnet::domain($dom,'primary');
 8606:     }
 8607:     my @ids=&Apache::lonnet::current_machine_ids();
 8608:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 8609:     if (!$allowed) {
 8610: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 8611:     }
 8612:     return $switchserver;
 8613: }
 8614: 
 8615: sub modify_quotas {
 8616:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 8617:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 8618:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 8619:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 8620:         $validationfieldsref);
 8621:     if ($action eq 'quotas') {
 8622:         $context = 'tools'; 
 8623:     } else {
 8624:         $context = $action;
 8625:     }
 8626:     if ($context eq 'requestcourses') {
 8627:         @usertools = ('official','unofficial','community','textbook');
 8628:         @options =('norequest','approval','validate','autolimit');
 8629:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 8630:         %titles = &courserequest_titles();
 8631:         $toolregexp = join('|',@usertools);
 8632:         %conditions = &courserequest_conditions();
 8633:         $confname = $dom.'-domainconfig';
 8634:         my $servadm = $r->dir_config('lonAdmEMail');
 8635:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 8636:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 8637:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 8638:     } elsif ($context eq 'requestauthor') {
 8639:         @usertools = ('author');
 8640:         %titles = &authorrequest_titles();
 8641:     } else {
 8642:         @usertools = ('aboutme','blog','webdav','portfolio');
 8643:         %titles = &tool_titles();
 8644:     }
 8645:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8646:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8647:     foreach my $key (keys(%env)) {
 8648:         if ($context eq 'requestcourses') {
 8649:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 8650:                 my $item = $1;
 8651:                 my $type = $2;
 8652:                 if ($type =~ /^limit_(.+)/) {
 8653:                     $limithash{$item}{$1} = $env{$key};
 8654:                 } else {
 8655:                     $confhash{$item}{$type} = $env{$key};
 8656:                 }
 8657:             }
 8658:         } elsif ($context eq 'requestauthor') {
 8659:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 8660:                 $confhash{$1} = $env{$key};
 8661:             }
 8662:         } else {
 8663:             if ($key =~ /^form\.quota_(.+)$/) {
 8664:                 $confhash{'defaultquota'}{$1} = $env{$key};
 8665:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 8666:                 $confhash{'authorquota'}{$1} = $env{$key};
 8667:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 8668:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 8669:             }
 8670:         }
 8671:     }
 8672:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 8673:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 8674:         @approvalnotify = sort(@approvalnotify);
 8675:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 8676:         my @crstypes = ('official','unofficial','community','textbook');
 8677:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 8678:         foreach my $type (@hasuniquecode) {
 8679:             if (grep(/^\Q$type\E$/,@crstypes)) {
 8680:                 $confhash{'uniquecode'}{$type} = 1;
 8681:             }
 8682:         }
 8683:         my (%newbook,%allpos);
 8684:         if ($context eq 'requestcourses') {
 8685:             foreach my $type ('textbooks','templates') {
 8686:                 @{$allpos{$type}} = (); 
 8687:                 my $invalid;
 8688:                 if ($type eq 'textbooks') {
 8689:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
 8690:                 } else {
 8691:                     $invalid = &mt('Invalid LON-CAPA course for template');
 8692:                 }
 8693:                 if ($env{'form.'.$type.'_addbook'}) {
 8694:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
 8695:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
 8696:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
 8697:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
 8698:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 8699:                         } else {
 8700:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
 8701:                             my $position = $env{'form.'.$type.'_addbook_pos'};
 8702:                             $position =~ s/\D+//g;
 8703:                             if ($position ne '') {
 8704:                                 $allpos{$type}[$position] = $newbook{$type};
 8705:                             }
 8706:                         }
 8707:                     } else {
 8708:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 8709:                     }
 8710:                 }
 8711:             } 
 8712:         }
 8713:         if (ref($domconfig{$action}) eq 'HASH') {
 8714:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 8715:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 8716:                     $changes{'notify'}{'approval'} = 1;
 8717:                 }
 8718:             } else {
 8719:                 if ($confhash{'notify'}{'approval'}) {
 8720:                     $changes{'notify'}{'approval'} = 1;
 8721:                 }
 8722:             }
 8723:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 8724:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 8725:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 8726:                         unless ($confhash{'uniquecode'}{$crstype}) {
 8727:                             $changes{'uniquecode'} = 1;
 8728:                         }
 8729:                     }
 8730:                     unless ($changes{'uniquecode'}) {
 8731:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 8732:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 8733:                                 $changes{'uniquecode'} = 1;
 8734:                             }
 8735:                         }
 8736:                     }
 8737:                } else {
 8738:                    $changes{'uniquecode'} = 1;
 8739:                }
 8740:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 8741:                 $changes{'uniquecode'} = 1;
 8742:             }
 8743:             if ($context eq 'requestcourses') {
 8744:                 foreach my $type ('textbooks','templates') {
 8745:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
 8746:                         my %deletions;
 8747:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
 8748:                         if (@todelete) {
 8749:                             map { $deletions{$_} = 1; } @todelete;
 8750:                         }
 8751:                         my %imgdeletions;
 8752:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
 8753:                         if (@todeleteimages) {
 8754:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
 8755:                         }
 8756:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
 8757:                         for (my $i=0; $i<=$maxnum; $i++) {
 8758:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
 8759:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
 8760:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
 8761:                                 if ($deletions{$key}) {
 8762:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
 8763:                                         #FIXME need to obsolete item in RES space
 8764:                                     }
 8765:                                     next;
 8766:                                 } else {
 8767:                                     my $newpos = $env{'form.'.$itemid};
 8768:                                     $newpos =~ s/\D+//g;
 8769:                                     foreach my $item ('subject','title','publisher','author') {
 8770:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 8771:                                                  ($type eq 'templates'));
 8772:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
 8773:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
 8774:                                             $changes{$type}{$key} = 1;
 8775:                                         }
 8776:                                     }
 8777:                                     $allpos{$type}[$newpos] = $key;
 8778:                                 }
 8779:                                 if ($imgdeletions{$key}) {
 8780:                                     $changes{$type}{$key} = 1;
 8781:                                     #FIXME need to obsolete item in RES space
 8782:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
 8783:                                     my ($cdom,$cnum) = split(/_/,$key);
 8784:                                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
 8785:                                         $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
 8786:                                     } else { 
 8787:                                         my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
 8788:                                                                                       $cdom,$cnum,$type,$configuserok,
 8789:                                                                                       $switchserver,$author_ok);
 8790:                                         if ($imgurl) {
 8791:                                             $confhash{$type}{$key}{'image'} = $imgurl;
 8792:                                             $changes{$type}{$key} = 1; 
 8793:                                         }
 8794:                                         if ($error) {
 8795:                                             &Apache::lonnet::logthis($error);
 8796:                                             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8797:                                         }
 8798:                                     } 
 8799:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
 8800:                                     $confhash{$type}{$key}{'image'} = 
 8801:                                         $domconfig{$action}{$type}{$key}{'image'};
 8802:                                 }
 8803:                             }
 8804:                         }
 8805:                     }
 8806:                 }
 8807:             }
 8808:         } else {
 8809:             if ($confhash{'notify'}{'approval'}) {
 8810:                 $changes{'notify'}{'approval'} = 1;
 8811:             }
 8812:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 8813:                 $changes{'uniquecode'} = 1;
 8814:             }
 8815:         }
 8816:         if ($context eq 'requestcourses') {
 8817:             foreach my $type ('textbooks','templates') {
 8818:                 if ($newbook{$type}) {
 8819:                     $changes{$type}{$newbook{$type}} = 1;
 8820:                     foreach my $item ('subject','title','publisher','author') {
 8821:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 8822:                                  ($type eq 'template'));
 8823:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
 8824:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
 8825:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
 8826:                         }
 8827:                     }
 8828:                     if ($type eq 'textbooks') {
 8829:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
 8830:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
 8831:                             my ($imageurl,$error) =
 8832:                                 &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
 8833:                                                         $configuserok,$switchserver,$author_ok);
 8834:                             if ($imageurl) {
 8835:                                 $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
 8836:                             }
 8837:                             if ($error) {
 8838:                                 &Apache::lonnet::logthis($error);
 8839:                                 $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8840:                             }
 8841:                         }
 8842:                     }
 8843:                 }
 8844:                 if (@{$allpos{$type}} > 0) {
 8845:                     my $idx = 0;
 8846:                     foreach my $item (@{$allpos{$type}}) {
 8847:                         if ($item ne '') {
 8848:                             $confhash{$type}{$item}{'order'} = $idx;
 8849:                             if (ref($domconfig{$action}) eq 'HASH') {
 8850:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
 8851:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
 8852:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
 8853:                                             $changes{$type}{$item} = 1;
 8854:                                         }
 8855:                                     }
 8856:                                 }
 8857:                             }
 8858:                             $idx ++;
 8859:                         }
 8860:                     }
 8861:                 }
 8862:             }
 8863:             if (ref($validationitemsref) eq 'ARRAY') {
 8864:                 foreach my $item (@{$validationitemsref}) {
 8865:                     if ($item eq 'fields') {
 8866:                         my @changed;
 8867:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 8868:                         if (@{$confhash{'validation'}{$item}} > 0) {
 8869:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 8870:                         }
 8871:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8872:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8873:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 8874:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 8875:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
 8876:                                 } else {
 8877:                                     @changed = @{$confhash{'validation'}{$item}};
 8878:                                 }
 8879:                             } else {
 8880:                                 @changed = @{$confhash{'validation'}{$item}};
 8881:                             }
 8882:                         } else {
 8883:                             @changed = @{$confhash{'validation'}{$item}};
 8884:                         }
 8885:                         if (@changed) {
 8886:                             if ($confhash{'validation'}{$item}) {
 8887:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 8888:                             } else {
 8889:                                 $changes{'validation'}{$item} = &mt('None');
 8890:                             }
 8891:                         }
 8892:                     } else {
 8893:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 8894:                         if ($item eq 'markup') {
 8895:                             if ($env{'form.requestcourses_validation_'.$item}) {
 8896:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 8897:                             }
 8898:                         }
 8899:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8900:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8901:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 8902:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 8903:                                 }
 8904:                             } else {
 8905:                                 if ($confhash{'validation'}{$item} ne '') {
 8906:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 8907:                                 }
 8908:                             }
 8909:                         } else {
 8910:                             if ($confhash{'validation'}{$item} ne '') {
 8911:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 8912:                             }
 8913:                         }
 8914:                     }
 8915:                 }
 8916:             }
 8917:             if ($env{'form.validationdc'}) {
 8918:                 my $newval = $env{'form.validationdc'};
 8919:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
 8920:                 if (exists($domcoords{$newval})) {
 8921:                     $confhash{'validation'}{'dc'} = $newval;
 8922:                 }
 8923:             }
 8924:             if (ref($confhash{'validation'}) eq 'HASH') {
 8925:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8926:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8927:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 8928:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 8929:                                 if ($confhash{'validation'}{'dc'} eq '') {
 8930:                                     $changes{'validation'}{'dc'} = &mt('None');
 8931:                                 } else {
 8932:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8933:                                 }
 8934:                             }
 8935:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
 8936:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8937:                         }
 8938:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 8939:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8940:                     }
 8941:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 8942:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8943:                 }
 8944:             } else {
 8945:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8946:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8947:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 8948:                             $changes{'validation'}{'dc'} = &mt('None');
 8949:                         }
 8950:                     }
 8951:                 }
 8952:             }
 8953:         }
 8954:     } else {
 8955:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 8956:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 8957:     }
 8958:     foreach my $item (@usertools) {
 8959:         foreach my $type (@{$types},'default','_LC_adv') {
 8960:             my $unset; 
 8961:             if ($context eq 'requestcourses') {
 8962:                 $unset = '0';
 8963:                 if ($type eq '_LC_adv') {
 8964:                     $unset = '';
 8965:                 }
 8966:                 if ($confhash{$item}{$type} eq 'autolimit') {
 8967:                     $confhash{$item}{$type} .= '=';
 8968:                     unless ($limithash{$item}{$type} =~ /\D/) {
 8969:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 8970:                     }
 8971:                 }
 8972:             } elsif ($context eq 'requestauthor') {
 8973:                 $unset = '0';
 8974:                 if ($type eq '_LC_adv') {
 8975:                     $unset = '';
 8976:                 }
 8977:             } else {
 8978:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 8979:                     $confhash{$item}{$type} = 1;
 8980:                 } else {
 8981:                     $confhash{$item}{$type} = 0;
 8982:                 }
 8983:             }
 8984:             if (ref($domconfig{$action}) eq 'HASH') {
 8985:                 if ($action eq 'requestauthor') {
 8986:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 8987:                         $changes{$type} = 1;
 8988:                     }
 8989:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 8990:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 8991:                         $changes{$item}{$type} = 1;
 8992:                     }
 8993:                 } else {
 8994:                     if ($context eq 'requestcourses') {
 8995:                         if ($confhash{$item}{$type} ne $unset) {
 8996:                             $changes{$item}{$type} = 1;
 8997:                         }
 8998:                     } else {
 8999:                         if (!$confhash{$item}{$type}) {
 9000:                             $changes{$item}{$type} = 1;
 9001:                         }
 9002:                     }
 9003:                 }
 9004:             } else {
 9005:                 if ($context eq 'requestcourses') {
 9006:                     if ($confhash{$item}{$type} ne $unset) {
 9007:                         $changes{$item}{$type} = 1;
 9008:                     }
 9009:                 } elsif ($context eq 'requestauthor') {
 9010:                     if ($confhash{$type} ne $unset) {
 9011:                         $changes{$type} = 1;
 9012:                     }
 9013:                 } else {
 9014:                     if (!$confhash{$item}{$type}) {
 9015:                         $changes{$item}{$type} = 1;
 9016:                     }
 9017:                 }
 9018:             }
 9019:         }
 9020:     }
 9021:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 9022:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 9023:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 9024:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 9025:                     if (exists($confhash{'defaultquota'}{$key})) {
 9026:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 9027:                             $changes{'defaultquota'}{$key} = 1;
 9028:                         }
 9029:                     } else {
 9030:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 9031:                     }
 9032:                 }
 9033:             } else {
 9034:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 9035:                     if (exists($confhash{'defaultquota'}{$key})) {
 9036:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 9037:                             $changes{'defaultquota'}{$key} = 1;
 9038:                         }
 9039:                     } else {
 9040:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 9041:                     }
 9042:                 }
 9043:             }
 9044:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 9045:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 9046:                     if (exists($confhash{'authorquota'}{$key})) {
 9047:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 9048:                             $changes{'authorquota'}{$key} = 1;
 9049:                         }
 9050:                     } else {
 9051:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 9052:                     }
 9053:                 }
 9054:             }
 9055:         }
 9056:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 9057:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 9058:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 9059:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 9060:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 9061:                             $changes{'defaultquota'}{$key} = 1;
 9062:                         }
 9063:                     } else {
 9064:                         if (!exists($domconfig{'quotas'}{$key})) {
 9065:                             $changes{'defaultquota'}{$key} = 1;
 9066:                         }
 9067:                     }
 9068:                 } else {
 9069:                     $changes{'defaultquota'}{$key} = 1;
 9070:                 }
 9071:             }
 9072:         }
 9073:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 9074:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 9075:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 9076:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 9077:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 9078:                             $changes{'authorquota'}{$key} = 1;
 9079:                         }
 9080:                     } else {
 9081:                         $changes{'authorquota'}{$key} = 1;
 9082:                     }
 9083:                 } else {
 9084:                     $changes{'authorquota'}{$key} = 1;
 9085:                 }
 9086:             }
 9087:         }
 9088:     }
 9089: 
 9090:     if ($context eq 'requestauthor') {
 9091:         $domdefaults{'requestauthor'} = \%confhash;
 9092:     } else {
 9093:         foreach my $key (keys(%confhash)) {
 9094:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
 9095:                 $domdefaults{$key} = $confhash{$key};
 9096:             }
 9097:         }
 9098:     }
 9099: 
 9100:     my %quotahash = (
 9101:                       $action => { %confhash }
 9102:                     );
 9103:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 9104:                                              $dom);
 9105:     if ($putresult eq 'ok') {
 9106:         if (keys(%changes) > 0) {
 9107:             my $cachetime = 24*60*60;
 9108:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9109:             if (ref($lastactref) eq 'HASH') {
 9110:                 $lastactref->{'domdefaults'} = 1;
 9111:             }
 9112:             $resulttext = &mt('Changes made:').'<ul>';
 9113:             unless (($context eq 'requestcourses') ||
 9114:                     ($context eq 'requestauthor')) {
 9115:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 9116:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 9117:                     foreach my $type (@{$types},'default') {
 9118:                         if (defined($changes{'defaultquota'}{$type})) {
 9119:                             my $typetitle = $usertypes->{$type};
 9120:                             if ($type eq 'default') {
 9121:                                 $typetitle = $othertitle;
 9122:                             }
 9123:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 9124:                         }
 9125:                     }
 9126:                     $resulttext .= '</ul></li>';
 9127:                 }
 9128:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 9129:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 9130:                     foreach my $type (@{$types},'default') {
 9131:                         if (defined($changes{'authorquota'}{$type})) {
 9132:                             my $typetitle = $usertypes->{$type};
 9133:                             if ($type eq 'default') {
 9134:                                 $typetitle = $othertitle;
 9135:                             }
 9136:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 9137:                         }
 9138:                     }
 9139:                     $resulttext .= '</ul></li>';
 9140:                 }
 9141:             }
 9142:             my %newenv;
 9143:             foreach my $item (@usertools) {
 9144:                 my (%haschgs,%inconf);
 9145:                 if ($context eq 'requestauthor') {
 9146:                     %haschgs = %changes;
 9147:                     %inconf = %confhash;
 9148:                 } else {
 9149:                     if (ref($changes{$item}) eq 'HASH') {
 9150:                         %haschgs = %{$changes{$item}};
 9151:                     }
 9152:                     if (ref($confhash{$item}) eq 'HASH') {
 9153:                         %inconf = %{$confhash{$item}};
 9154:                     }
 9155:                 }
 9156:                 if (keys(%haschgs) > 0) {
 9157:                     my $newacc = 
 9158:                         &Apache::lonnet::usertools_access($env{'user.name'},
 9159:                                                           $env{'user.domain'},
 9160:                                                           $item,'reload',$context);
 9161:                     if (($context eq 'requestcourses') ||
 9162:                         ($context eq 'requestauthor')) {
 9163:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 9164:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 9165:                         }
 9166:                     } else {
 9167:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 9168:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 9169:                         }
 9170:                     }
 9171:                     unless ($context eq 'requestauthor') {
 9172:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 9173:                     }
 9174:                     foreach my $type (@{$types},'default','_LC_adv') {
 9175:                         if ($haschgs{$type}) {
 9176:                             my $typetitle = $usertypes->{$type};
 9177:                             if ($type eq 'default') {
 9178:                                 $typetitle = $othertitle;
 9179:                             } elsif ($type eq '_LC_adv') {
 9180:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 9181:                             }
 9182:                             if ($inconf{$type}) {
 9183:                                 if ($context eq 'requestcourses') {
 9184:                                     my $cond;
 9185:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 9186:                                         if ($1 eq '') {
 9187:                                             $cond = &mt('(Automatic processing of any request).');
 9188:                                         } else {
 9189:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 9190:                                         }
 9191:                                     } else { 
 9192:                                         $cond = $conditions{$inconf{$type}};
 9193:                                     }
 9194:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 9195:                                 } elsif ($context eq 'requestauthor') {
 9196:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 9197:                                                              $titles{$inconf{$type}},$typetitle);
 9198: 
 9199:                                 } else {
 9200:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 9201:                                 }
 9202:                             } else {
 9203:                                 if ($type eq '_LC_adv') {
 9204:                                     if ($inconf{$type} eq '0') {
 9205:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 9206:                                     } else { 
 9207:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 9208:                                     }
 9209:                                 } else {
 9210:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 9211:                                 }
 9212:                             }
 9213:                         }
 9214:                     }
 9215:                     unless ($context eq 'requestauthor') {
 9216:                         $resulttext .= '</ul></li>';
 9217:                     }
 9218:                 }
 9219:             }
 9220:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 9221:                 if (ref($changes{'notify'}) eq 'HASH') {
 9222:                     if ($changes{'notify'}{'approval'}) {
 9223:                         if (ref($confhash{'notify'}) eq 'HASH') {
 9224:                             if ($confhash{'notify'}{'approval'}) {
 9225:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 9226:                             } else {
 9227:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 9228:                             }
 9229:                         }
 9230:                     }
 9231:                 }
 9232:             }
 9233:             if ($action eq 'requestcourses') {
 9234:                 my @offon = ('off','on');
 9235:                 if ($changes{'uniquecode'}) {
 9236:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 9237:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 9238:                         $resulttext .= '<li>'.
 9239:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 9240:                                        '</li>';
 9241:                     } else {
 9242:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 9243:                                        '</li>';
 9244:                     }
 9245:                 }
 9246:                 foreach my $type ('textbooks','templates') {
 9247:                     if (ref($changes{$type}) eq 'HASH') {
 9248:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
 9249:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
 9250:                             my %coursehash = &Apache::lonnet::coursedescription($key);
 9251:                             my $coursetitle = $coursehash{'description'};
 9252:                             my $position = $confhash{$type}{$key}{'order'} + 1;
 9253:                             $resulttext .= '<li>';
 9254:                             foreach my $item ('subject','title','publisher','author') {
 9255:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
 9256:                                          ($type eq 'templates'));
 9257:                                 my $name = $item.':';
 9258:                                 $name =~ s/^(\w)/\U$1/;
 9259:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
 9260:                             }
 9261:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 9262:                             if ($type eq 'textbooks') {
 9263:                                 if ($confhash{$type}{$key}{'image'}) {
 9264:                                     $resulttext .= ' '.&mt('Image: [_1]',
 9265:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
 9266:                                                    ' alt="Textbook cover" />').'<br />';
 9267:                                 }
 9268:                             }
 9269:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 9270:                         }
 9271:                         $resulttext .= '</ul></li>';
 9272:                     }
 9273:                 }
 9274:                 if (ref($changes{'validation'}) eq 'HASH') {
 9275:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 9276:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 9277:                         foreach my $item (@{$validationitemsref}) {
 9278:                             if (exists($changes{'validation'}{$item})) {
 9279:                                 if ($item eq 'markup') {
 9280:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9281:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 9282:                                 } else {
 9283:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9284:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 9285:                                 }
 9286:                             }
 9287:                         }
 9288:                         if (exists($changes{'validation'}{'dc'})) {
 9289:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 9290:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 9291:                         }
 9292:                     }
 9293:                 }
 9294:             }
 9295:             $resulttext .= '</ul>';
 9296:             if (keys(%newenv)) {
 9297:                 &Apache::lonnet::appenv(\%newenv);
 9298:             }
 9299:         } else {
 9300:             if ($context eq 'requestcourses') {
 9301:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 9302:             } elsif ($context eq 'requestauthor') {
 9303:                 $resulttext = &mt('No changes made to rights to request author space.');
 9304:             } else {
 9305:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 9306:             }
 9307:         }
 9308:     } else {
 9309:         $resulttext = '<span class="LC_error">'.
 9310: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9311:     }
 9312:     if ($errors) {
 9313:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 9314:                        '<ul>'.$errors.'</ul></p>';
 9315:     }
 9316:     return $resulttext;
 9317: }
 9318: 
 9319: sub process_textbook_image {
 9320:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
 9321:     my $filename = $env{'form.'.$caller.'.filename'};
 9322:     my ($error,$url);
 9323:     my ($width,$height) = (50,50);
 9324:     if ($configuserok eq 'ok') {
 9325:         if ($switchserver) {
 9326:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 9327:                          $switchserver);
 9328:         } elsif ($author_ok eq 'ok') {
 9329:             my ($result,$imageurl) =
 9330:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 9331:                              "$type/$cdom/$cnum/cover",$width,$height);
 9332:             if ($result eq 'ok') {
 9333:                 $url = $imageurl;
 9334:             } else {
 9335:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 9336:             }
 9337:         } else {
 9338:             $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);
 9339:         }
 9340:     } else {
 9341:         $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);
 9342:     }
 9343:     return ($url,$error);
 9344: }
 9345: 
 9346: sub modify_ltitools {
 9347:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 9348:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9349:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
 9350:     my $confname = $dom.'-domainconfig';
 9351:     my $servadm = $r->dir_config('lonAdmEMail');
 9352:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 9353:     my (%posslti,%possfield);
 9354:     my @courseroles = ('cc','in','ta','ep','st');
 9355:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 9356:     map { $posslti{$_} = 1; } @ltiroles;
 9357:     my @allfields = ('fullname','firstname','lastname','email','user','roles');
 9358:     map { $possfield{$_} = 1; } @allfields;
 9359:     my %lt = &ltitools_names();
 9360:     if ($env{'form.ltitools_add'}) {
 9361:         my $title = $env{'form.ltitools_add_title'};
 9362:         $title =~ s/(`)/'/g;
 9363:         ($newid,my $error) = &get_ltitools_id($dom,$title);
 9364:         if ($newid) {
 9365:             my $position = $env{'form.ltitools_add_pos'};
 9366:             $position =~ s/\D+//g;
 9367:             if ($position ne '') {
 9368:                 $allpos[$position] = $newid;
 9369:             }
 9370:             $changes{$newid} = 1;
 9371:             foreach my $item ('title','url','key','secret') {
 9372:                 $env{'form.ltitools_add_'.$item} =~ s/(`)/'/g;
 9373:                 if ($env{'form.ltitools_add_'.$item}) {
 9374:                     if (($item eq 'key') || ($item eq 'secret')) {
 9375:                         $encconfig{$newid}{$item} = $env{'form.ltitools_add_'.$item};
 9376:                     } else {
 9377:                         $confhash{$newid}{$item} = $env{'form.ltitools_add_'.$item};
 9378:                     }
 9379:                 }
 9380:             }
 9381:             if ($env{'form.ltitools_add_version'} eq 'LTI-1p0') {
 9382:                 $confhash{$newid}{'version'} = $env{'form.ltitools_add_version'};
 9383:             }
 9384:             if ($env{'form.ltitools_add_msgtype'} eq 'basic-lti-launch-request') {
 9385:                 $confhash{$newid}{'msgtype'} = $env{'form.ltitools_add_msgtype'};
 9386:             }
 9387:             foreach my $item ('width','height','linktext','explanation') {
 9388:                 $env{'form.ltitools_add_'.$item} =~ s/^\s+//;
 9389:                 $env{'form.ltitools_add_'.$item} =~ s/\s+$//;
 9390:                 if (($item eq 'width') || ($item eq 'height')) {
 9391:                     if ($env{'form.ltitools_add_'.$item} =~ /^\d+$/) {
 9392:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
 9393:                     }
 9394:                 } else {
 9395:                     if ($env{'form.ltitools_add_'.$item} ne '') {
 9396:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
 9397:                     }
 9398:                 }
 9399:             }
 9400:             if ($env{'form.ltitools_add_target'} eq 'window') {
 9401:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
 9402:             } elsif ($env{'form.ltitools_add_target'} eq 'tab') {
 9403:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
 9404:             } else {
 9405:                 $confhash{$newid}{'display'}{'target'} = 'iframe';
 9406:             }
 9407:             foreach my $item ('passback','roster') {
 9408:                 if ($env{'form.ltitools_add_'.$item}) {
 9409:                     $confhash{$newid}{$item} = 1;
 9410:                 }
 9411:             }
 9412:             if ($env{'form.ltitools_add_image.filename'} ne '') {
 9413:                 my ($imageurl,$error) =
 9414:                     &process_ltitools_image($r,$dom,$confname,'ltitools_add_image',$newid,
 9415:                                             $configuserok,$switchserver,$author_ok);
 9416:                 if ($imageurl) {
 9417:                     $confhash{$newid}{'image'} = $imageurl;
 9418:                 }
 9419:                 if ($error) {
 9420:                     &Apache::lonnet::logthis($error);
 9421:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9422:                 }
 9423:             }
 9424:             my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_add_fields');
 9425:             foreach my $field (@fields) {
 9426:                 if ($possfield{$field}) {
 9427:                     if ($field eq 'roles') {
 9428:                         foreach my $role (@courseroles) {
 9429:                             my $choice = $env{'form.ltitools_add_roles_'.$role};
 9430:                             if (($choice ne '') && ($posslti{$choice})) {
 9431:                                 $confhash{$newid}{'roles'}{$role} = $choice;
 9432:                                 if ($role eq 'cc') {
 9433:                                     $confhash{$newid}{'roles'}{'co'} = $choice;
 9434:                                 }
 9435:                             }
 9436:                         }
 9437:                     } else {
 9438:                         $confhash{$newid}{'fields'}{$field} = 1;
 9439:                     }
 9440:                 }
 9441:             }
 9442:             my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig');
 9443:             foreach my $item (@courseconfig) {
 9444:                 $confhash{$newid}{'crsconf'}{$item} = 1;
 9445:             }
 9446:             if ($env{'form.ltitools_add_custom'}) {
 9447:                 my $name = $env{'form.ltitools_add_custom_name'};
 9448:                 my $value = $env{'form.ltitools_add_custom_value'};
 9449:                 $value =~ s/(`)/'/g;
 9450:                 $name =~ s/(`)/'/g;
 9451:                 $confhash{$newid}{'custom'}{$name} = $value;
 9452:             }
 9453:         } else {
 9454:             my $error = &mt('Failed to acquire unique ID for new external tool');
 9455:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9456:         }
 9457:     }
 9458:     if (ref($domconfig{$action}) eq 'HASH') {
 9459:         my %deletions;
 9460:         my @todelete = &Apache::loncommon::get_env_multiple('form.ltitools_del');
 9461:         if (@todelete) {
 9462:             map { $deletions{$_} = 1; } @todelete;
 9463:         }
 9464:         my %customadds;
 9465:         my @newcustom = &Apache::loncommon::get_env_multiple('form.ltitools_customadd');
 9466:         if (@newcustom) {
 9467:             map { $customadds{$_} = 1; } @newcustom;
 9468:         }
 9469:         my %imgdeletions;
 9470:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.ltitools_image_del');
 9471:         if (@todeleteimages) {
 9472:             map { $imgdeletions{$_} = 1; } @todeleteimages;
 9473:         }
 9474:         my $maxnum = $env{'form.ltitools_maxnum'};
 9475:         for (my $i=0; $i<=$maxnum; $i++) {
 9476:             my $itemid = $env{'form.ltitools_id_'.$i};
 9477:             $itemid =~ s/\D+//g;
 9478:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
 9479:                 if ($deletions{$itemid}) {
 9480:                     if ($domconfig{$action}{$itemid}{'image'}) {
 9481:                         #FIXME need to obsolete item in RES space
 9482:                     }
 9483:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'title'};
 9484:                     next;
 9485:                 } else {
 9486:                     my $newpos = $env{'form.ltitools_'.$itemid};
 9487:                     $newpos =~ s/\D+//g;
 9488:                     foreach my $item ('title','url') {
 9489:                         $confhash{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
 9490:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
 9491:                             $changes{$itemid} = 1;
 9492:                         }
 9493:                     }
 9494:                     foreach my $item ('key','secret') {
 9495:                         $encconfig{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
 9496:                         if ($domconfig{$action}{$itemid}{$item} ne $encconfig{$itemid}{$item}) {
 9497:                             $changes{$itemid} = 1;
 9498:                         }
 9499:                     }
 9500:                     if ($env{'form.ltitools_version_'.$i} eq 'LTI-1p0') {
 9501:                         $confhash{$itemid}{'version'} = $env{'form.ltitools_version_'.$i};
 9502:                     }
 9503:                     if ($env{'form.ltitools_msgtype_'.$i} eq 'basic-lti-launch-request') {
 9504:                         $confhash{$itemid}{'msgtype'} = $env{'form.ltitools_msgtype_'.$i};
 9505:                     }
 9506:                     foreach my $size ('width','height') {
 9507:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/^\s+//;
 9508:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/\s+$//;
 9509:                         if ($env{'form.ltitools_'.$size.'_'.$i} =~ /^\d+$/) {
 9510:                             $confhash{$itemid}{'display'}{$size} = $env{'form.ltitools_'.$size.'_'.$i};
 9511:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9512:                                 if ($domconfig{$action}{$itemid}{'display'}{$size} ne $confhash{$itemid}{'display'}{$size}) {
 9513:                                     $changes{$itemid} = 1;
 9514:                                 }
 9515:                             } else {
 9516:                                 $changes{$itemid} = 1;
 9517:                             }
 9518:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9519:                             if ($domconfig{$action}{$itemid}{'display'}{$size} ne '') {
 9520:                                 $changes{$itemid} = 1;
 9521:                             }
 9522:                         }
 9523:                     }
 9524:                     foreach my $item ('linktext','explanation') {
 9525:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/^\s+//;
 9526:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/\s+$//;
 9527:                         if ($env{'form.ltitools_'.$item.'_'.$i} ne '') {
 9528:                             $confhash{$itemid}{'display'}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
 9529:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9530:                                 if ($domconfig{$action}{$itemid}{'display'}{$item} ne $confhash{$itemid}{'display'}{$item}) {
 9531:                                     $changes{$itemid} = 1;
 9532:                                 }
 9533:                             } else {
 9534:                                 $changes{$itemid} = 1;
 9535:                             }
 9536:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9537:                             if ($domconfig{$action}{$itemid}{'display'}{$item} ne '') {
 9538:                                 $changes{$itemid} = 1;
 9539:                             }
 9540:                         }
 9541:                     }
 9542:                     if ($env{'form.ltitools_target_'.$i} eq 'window') {
 9543:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
 9544:                     } elsif ($env{'form.ltitools_target_'.$i} eq 'tab') {
 9545:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
 9546:                     } else {
 9547:                         $confhash{$itemid}{'display'}{'target'} = 'iframe';
 9548:                     }
 9549:                     if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9550:                         if ($domconfig{$action}{$itemid}{'display'}{'target'} ne $confhash{$itemid}{'display'}{'target'}) {
 9551:                             $changes{$itemid} = 1;
 9552:                         }
 9553:                     } else {
 9554:                         $changes{$itemid} = 1;
 9555:                     }
 9556:                     foreach my $extra ('passback','roster') {
 9557:                         if ($env{'form.ltitools_'.$extra.'_'.$i}) {
 9558:                             $confhash{$itemid}{$extra} = 1;
 9559:                         }
 9560:                         if ($domconfig{$action}{$itemid}{$extra} ne $confhash{$itemid}{$extra}) {
 9561:                             $changes{$itemid} = 1;
 9562:                         }
 9563:                     }
 9564:                     my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig_'.$i);
 9565:                     foreach my $item ('label','title','target','linktext','explanation') {
 9566:                         if (grep(/^\Q$item\E$/,@courseconfig)) {
 9567:                             $confhash{$itemid}{'crsconf'}{$item} = 1;
 9568:                             if (ref($domconfig{$action}{$itemid}{'crsconf'}) eq 'HASH') {
 9569:                                 if ($domconfig{$action}{$itemid}{'crsconf'}{$item} ne $confhash{$itemid}{'crsconf'}{$item}) {
 9570:                                     $changes{$itemid} = 1;
 9571:                                 }
 9572:                             } else {
 9573:                                 $changes{$itemid} = 1;
 9574:                             }
 9575:                         }
 9576:                     }
 9577:                     my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_fields_'.$i);
 9578:                     foreach my $field (@fields) {
 9579:                         if ($possfield{$field}) {
 9580:                             if ($field eq 'roles') {
 9581:                                 foreach my $role (@courseroles) {
 9582:                                     my $choice = $env{'form.ltitools_roles_'.$role.'_'.$i};
 9583:                                     if (($choice ne '') && ($posslti{$choice})) {
 9584:                                         $confhash{$itemid}{'roles'}{$role} = $choice;
 9585:                                         if ($role eq 'cc') {
 9586:                                             $confhash{$itemid}{'roles'}{'co'} = $choice;
 9587:                                         }
 9588:                                     }
 9589:                                     if (ref($domconfig{$action}{$itemid}{'roles'}) eq 'HASH') {
 9590:                                         if ($domconfig{$action}{$itemid}{'roles'}{$role} ne $confhash{$itemid}{'roles'}{$role}) {
 9591:                                             $changes{$itemid} = 1;
 9592:                                         }
 9593:                                     } elsif ($confhash{$itemid}{'roles'}{$role}) {
 9594:                                         $changes{$itemid} = 1;
 9595:                                     }
 9596:                                 }
 9597:                             } else {
 9598:                                 $confhash{$itemid}{'fields'}{$field} = 1;
 9599:                                 if (ref($domconfig{$action}{$itemid}{'fields'}) eq 'HASH') {
 9600:                                     if ($domconfig{$action}{$itemid}{'fields'}{$field} ne $confhash{$itemid}{'fields'}{$field}) {
 9601:                                         $changes{$itemid} = 1;
 9602:                                     }
 9603:                                 } else {
 9604:                                     $changes{$itemid} = 1;
 9605:                                 }
 9606:                             }
 9607:                         }
 9608:                     }
 9609:                     $allpos[$newpos] = $itemid;
 9610:                 }
 9611:                 if ($imgdeletions{$itemid}) {
 9612:                     $changes{$itemid} = 1;
 9613:                     #FIXME need to obsolete item in RES space
 9614:                 } elsif ($env{'form.ltitools_image_'.$i.'.filename'}) {
 9615:                     my ($imgurl,$error) = &process_ltitools_image($r,$dom,$confname,'ltitools_image_'.$i,
 9616:                                                                  $itemid,$configuserok,$switchserver,
 9617:                                                                  $author_ok);
 9618:                     if ($imgurl) {
 9619:                         $confhash{$itemid}{'image'} = $imgurl;
 9620:                         $changes{$itemid} = 1;
 9621:                     }
 9622:                     if ($error) {
 9623:                         &Apache::lonnet::logthis($error);
 9624:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9625:                     }
 9626:                 } elsif ($domconfig{$action}{$itemid}{'image'}) {
 9627:                     $confhash{$itemid}{'image'} =
 9628:                        $domconfig{$action}{$itemid}{'image'};
 9629:                 }
 9630:                 if ($customadds{$i}) {
 9631:                     my $name = $env{'form.ltitools_custom_name_'.$i};
 9632:                     $name =~ s/(`)/'/g;
 9633:                     $name =~ s/^\s+//;
 9634:                     $name =~ s/\s+$//;
 9635:                     my $value = $env{'form.ltitools_custom_value_'.$i};
 9636:                     $value =~ s/(`)/'/g;
 9637:                     $value =~ s/^\s+//;
 9638:                     $value =~ s/\s+$//;
 9639:                     if ($name ne '') {
 9640:                         $confhash{$itemid}{'custom'}{$name} = $value;
 9641:                         $changes{$itemid} = 1;
 9642:                     }
 9643:                 }
 9644:                 my %customdels;
 9645:                 my @customdeletions = &Apache::loncommon::get_env_multiple('form.ltitools_customdel_'.$i);
 9646:                 if (@customdeletions) {
 9647:                     $changes{$itemid} = 1;
 9648:                 }
 9649:                 map { $customdels{$_} = 1; } @customdeletions;
 9650:                 if (ref($domconfig{$action}{$itemid}{'custom'}) eq 'HASH') {
 9651:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}{'custom'}})) {
 9652:                         unless ($customdels{$key}) {
 9653:                             if ($env{'form.ltitools_customval_'.$key.'_'.$i} ne '') {
 9654:                                 $confhash{$itemid}{'custom'}{$key} = $env{'form.ltitools_customval_'.$key.'_'.$i};
 9655:                             }
 9656:                             if ($domconfig{$action}{$itemid}{'custom'}{$key} ne $env{'form.ltitools_customval_'.$key.'_'.$i}) {
 9657:                                 $changes{$itemid} = 1;
 9658:                             }
 9659:                         }
 9660:                     }
 9661:                 }
 9662:                 unless ($changes{$itemid}) {
 9663:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}})) {
 9664:                         if (ref($domconfig{$action}{$itemid}{$key}) eq 'HASH') {
 9665:                             if (ref($confhash{$itemid}{$key}) eq 'HASH') {
 9666:                                 foreach my $innerkey (keys(%{$domconfig{$action}{$itemid}{$key}})) {
 9667:                                     unless (exists($confhash{$itemid}{$key}{$innerkey})) {
 9668:                                         $changes{$itemid} = 1;
 9669:                                         last;
 9670:                                     }
 9671:                                 }
 9672:                             } elsif (keys(%{$domconfig{$action}{$itemid}{$key}}) > 0) {
 9673:                                 $changes{$itemid} = 1;
 9674:                             }
 9675:                         }
 9676:                         last if ($changes{$itemid});
 9677:                     }
 9678:                 }
 9679:             }
 9680:         }
 9681:     }
 9682:     if (@allpos > 0) {
 9683:         my $idx = 0;
 9684:         foreach my $itemid (@allpos) {
 9685:             if ($itemid ne '') {
 9686:                 $confhash{$itemid}{'order'} = $idx;
 9687:                 if (ref($domconfig{$action}) eq 'HASH') {
 9688:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
 9689:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
 9690:                             $changes{$itemid} = 1;
 9691:                         }
 9692:                     }
 9693:                 }
 9694:                 $idx ++;
 9695:             }
 9696:         }
 9697:     }
 9698:     my %ltitoolshash = (
 9699:                           $action => { %confhash }
 9700:                        );
 9701:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,
 9702:                                              $dom);
 9703:     if ($putresult eq 'ok') {
 9704:         my %ltienchash = (
 9705:                              $action => { %encconfig }
 9706:                          );
 9707:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom);
 9708:         if (keys(%changes) > 0) {
 9709:             my $cachetime = 24*60*60;
 9710:             my %ltiall = %confhash;
 9711:             foreach my $id (keys(%ltiall)) {
 9712:                 if (ref($encconfig{$id}) eq 'HASH') {
 9713:                     foreach my $item ('key','secret') {
 9714:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
 9715:                     }
 9716:                 }
 9717:             }
 9718:             &Apache::lonnet::do_cache_new('ltitools',$dom,\%ltiall,$cachetime);
 9719:             if (ref($lastactref) eq 'HASH') {
 9720:                 $lastactref->{'ltitools'} = 1;
 9721:             }
 9722:             $resulttext = &mt('Changes made:').'<ul>';
 9723:             my %bynum;
 9724:             foreach my $itemid (sort(keys(%changes))) {
 9725:                 my $position = $confhash{$itemid}{'order'};
 9726:                 $bynum{$position} = $itemid;
 9727:             }
 9728:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
 9729:                 my $itemid = $bynum{$pos};
 9730:                 if (ref($confhash{$itemid}) ne 'HASH') {
 9731:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
 9732:                 } else {
 9733:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'title'}.'</b>';
 9734:                     if ($confhash{$itemid}{'image'}) {
 9735:                         $resulttext .= '&nbsp;'.
 9736:                                        '<img src="'.$confhash{$itemid}{'image'}.'"'.
 9737:                                        ' alt="'.&mt('Tool Provider icon').'" />';
 9738:                     }
 9739:                     $resulttext .= '</li><ul>';
 9740:                     my $position = $pos + 1;
 9741:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
 9742:                     foreach my $item ('version','msgtype','url') {
 9743:                         if ($confhash{$itemid}{$item} ne '') {
 9744:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
 9745:                         }
 9746:                     }
 9747:                     if ($encconfig{$itemid}{'key'} ne '') {
 9748:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
 9749:                     }
 9750:                     if ($encconfig{$itemid}{'secret'} ne '') {
 9751:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
 9752:                         my $num = length($encconfig{$itemid}{'secret'});
 9753:                         $resulttext .= ('*'x$num).'</li>';
 9754:                     }
 9755:                     $resulttext .= '<li>'.&mt('Configurable in course:');
 9756:                     my @possconfig = ('label','title','target','linktext','explanation');
 9757:                     my $numconfig = 0;
 9758:                     if (ref($confhash{$itemid}{'crsconf'}) eq 'HASH') {
 9759:                         foreach my $item (@possconfig) {
 9760:                             if ($confhash{$itemid}{'crsconf'}{$item}) {
 9761:                                 $numconfig ++;
 9762:                                 $resulttext .= ' "'.$lt{'crs'.$item}.'"';
 9763:                             }
 9764:                         }
 9765:                     }
 9766:                     if (!$numconfig) {
 9767:                         $resulttext .= &mt('None');
 9768:                     }
 9769:                     $resulttext .= '</li>';
 9770:                     foreach my $item ('passback','roster') {
 9771:                         $resulttext .= '<li>'.$lt{$item}.'&nbsp;';
 9772:                         if ($confhash{$itemid}{$item}) {
 9773:                             $resulttext .= &mt('Yes');
 9774:                         } else {
 9775:                             $resulttext .= &mt('No');
 9776:                         }
 9777:                         $resulttext .= '</li>';
 9778:                     }
 9779:                     if (ref($confhash{$itemid}{'display'}) eq 'HASH') {
 9780:                         my $displaylist;
 9781:                         if ($confhash{$itemid}{'display'}{'target'}) {
 9782:                             $displaylist = &mt('Display target').':&nbsp;'.
 9783:                                            $confhash{$itemid}{'display'}{'target'}.',';
 9784:                         }
 9785:                         foreach my $size ('width','height') {
 9786:                             if ($confhash{$itemid}{'display'}{$size}) {
 9787:                                 $displaylist .= ('&nbsp;'x2).$lt{$size}.':&nbsp;'.
 9788:                                                 $confhash{$itemid}{'display'}{$size}.',';
 9789:                             }
 9790:                         }
 9791:                         if ($displaylist) {
 9792:                             $displaylist =~ s/,$//;
 9793:                             $resulttext .= '<li>'.$displaylist.'</li>';
 9794:                         }
 9795:                         foreach my $item ('linktext','explanation') {
 9796:                             if ($confhash{$itemid}{'display'}{$item}) {
 9797:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{'display'}{$item}.'</li>';
 9798:                             }
 9799:                         }
 9800:                     }
 9801:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
 9802:                         my $fieldlist;
 9803:                         foreach my $field (@allfields) {
 9804:                             if ($confhash{$itemid}{'fields'}{$field}) {
 9805:                                 $fieldlist .= ('&nbsp;'x2).$lt{$field}.',';
 9806:                             }
 9807:                         }
 9808:                         if ($fieldlist) {
 9809:                             $fieldlist =~ s/,$//;
 9810:                             $resulttext .= '<li>'.&mt('Data sent').':'.$fieldlist.'</li>';
 9811:                         }
 9812:                     }
 9813:                     if (ref($confhash{$itemid}{'roles'}) eq 'HASH') {
 9814:                         my $rolemaps;
 9815:                         foreach my $role (@courseroles) {
 9816:                             if ($confhash{$itemid}{'roles'}{$role}) {
 9817:                                 $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
 9818:                                              $confhash{$itemid}{'roles'}{$role}.',';
 9819:                             }
 9820:                         }
 9821:                         if ($rolemaps) {
 9822:                             $rolemaps =~ s/,$//;
 9823:                             $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
 9824:                         }
 9825:                     }
 9826:                     if (ref($confhash{$itemid}{'custom'}) eq 'HASH') {
 9827:                         my $customlist;
 9828:                         if (keys(%{$confhash{$itemid}{'custom'}})) {
 9829:                             foreach my $key (sort(keys(%{$confhash{$itemid}{'custom'}}))) {
 9830:                                 $customlist .= $key.':'.$confhash{$itemid}{'custom'}{$key}.('&nbsp;'x2);
 9831:                             }
 9832:                         }
 9833:                         if ($customlist) {
 9834:                             $resulttext .= '<li>'.&mt('Custom items').':'.$customlist.'</li>';
 9835:                         }
 9836:                     }
 9837:                     $resulttext .= '</ul></li>';
 9838:                 }
 9839:             }
 9840:             $resulttext .= '</ul>';
 9841:         } else {
 9842:             $resulttext = &mt('No changes made.');
 9843:         }
 9844:     } else {
 9845:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
 9846:     }
 9847:     if ($errors) {
 9848:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 9849:                        $errors.'</ul>';
 9850:     }
 9851:     return $resulttext;
 9852: }
 9853: 
 9854: sub process_ltitools_image {
 9855:     my ($r,$dom,$confname,$caller,$itemid,$configuserok,$switchserver,$author_ok) = @_;
 9856:     my $filename = $env{'form.'.$caller.'.filename'};
 9857:     my ($error,$url);
 9858:     my ($width,$height) = (21,21);
 9859:     if ($configuserok eq 'ok') {
 9860:         if ($switchserver) {
 9861:             $error = &mt('Upload of Tool Provider (LTI) icon is not permitted to this server: [_1]',
 9862:                          $switchserver);
 9863:         } elsif ($author_ok eq 'ok') {
 9864:             my ($result,$imageurl,$madethumb) =
 9865:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 9866:                              "ltitools/$itemid/icon",$width,$height);
 9867:             if ($result eq 'ok') {
 9868:                 if ($madethumb) {
 9869:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
 9870:                     my $imagethumb = "$path/tn-".$imagefile;
 9871:                     $url = $imagethumb;
 9872:                 } else {
 9873:                     $url = $imageurl;
 9874:                 }
 9875:             } else {
 9876:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 9877:             }
 9878:         } else {
 9879:             $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);
 9880:         }
 9881:     } else {
 9882:         $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);
 9883:     }
 9884:     return ($url,$error);
 9885: }
 9886: 
 9887: sub get_ltitools_id {
 9888:     my ($cdom,$title) = @_;
 9889:     # get lock on ltitools db
 9890:     my $lockhash = {
 9891:                       lock => $env{'user.name'}.
 9892:                               ':'.$env{'user.domain'},
 9893:                    };
 9894:     my $tries = 0;
 9895:     my $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
 9896:     my ($id,$error);
 9897: 
 9898:     while (($gotlock ne 'ok') && ($tries<10)) {
 9899:         $tries ++;
 9900:         sleep (0.1);
 9901:         $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
 9902:     }
 9903:     if ($gotlock eq 'ok') {
 9904:         my %currids = &Apache::lonnet::dump_dom('ltitools',$cdom);
 9905:         if ($currids{'lock'}) {
 9906:             delete($currids{'lock'});
 9907:             if (keys(%currids)) {
 9908:                 my @curr = sort { $a <=> $b } keys(%currids);
 9909:                 if ($curr[-1] =~ /^\d+$/) {
 9910:                     $id = 1 + $curr[-1];
 9911:                 }
 9912:             } else {
 9913:                 $id = 1;
 9914:             }
 9915:             if ($id) {
 9916:                 unless (&Apache::lonnet::newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
 9917:                     $error = 'nostore';
 9918:                 }
 9919:             } else {
 9920:                 $error = 'nonumber';
 9921:             }
 9922:         }
 9923:         my $dellockoutcome = &Apache::lonnet::del_dom('ltitools',['lock'],$cdom);
 9924:     } else {
 9925:         $error = 'nolock';
 9926:     }
 9927:     return ($id,$error);
 9928: }
 9929: 
 9930: sub modify_autoenroll {
 9931:     my ($dom,$lastactref,%domconfig) = @_;
 9932:     my ($resulttext,%changes);
 9933:     my %currautoenroll;
 9934:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 9935:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 9936:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 9937:         }
 9938:     }
 9939:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 9940:     my %title = ( run => 'Auto-enrollment active',
 9941:                   sender => 'Sender for notification messages',
 9942:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
 9943:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
 9944:     my @offon = ('off','on');
 9945:     my $sender_uname = $env{'form.sender_uname'};
 9946:     my $sender_domain = $env{'form.sender_domain'};
 9947:     if ($sender_domain eq '') {
 9948:         $sender_uname = '';
 9949:     } elsif ($sender_uname eq '') {
 9950:         $sender_domain = '';
 9951:     }
 9952:     my $coowners = $env{'form.autoassign_coowners'};
 9953:     my $failsafe = $env{'form.autoenroll_failsafe'};
 9954:     $failsafe =~ s{^\s+|\s+$}{}g;
 9955:     if ($failsafe =~ /\D/) {
 9956:         undef($failsafe);
 9957:     }
 9958:     my %autoenrollhash =  (
 9959:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 9960:                                        'sender_uname' => $sender_uname,
 9961:                                        'sender_domain' => $sender_domain,
 9962:                                        'co-owners' => $coowners,
 9963:                                        'autofailsafe' => $failsafe,
 9964:                                 }
 9965:                      );
 9966:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 9967:                                              $dom);
 9968:     if ($putresult eq 'ok') {
 9969:         if (exists($currautoenroll{'run'})) {
 9970:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 9971:                  $changes{'run'} = 1;
 9972:              }
 9973:         } elsif ($autorun) {
 9974:             if ($env{'form.autoenroll_run'} ne '1') {
 9975:                  $changes{'run'} = 1;
 9976:             }
 9977:         }
 9978:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 9979:             $changes{'sender'} = 1;
 9980:         }
 9981:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 9982:             $changes{'sender'} = 1;
 9983:         }
 9984:         if ($currautoenroll{'co-owners'} ne '') {
 9985:             if ($currautoenroll{'co-owners'} ne $coowners) {
 9986:                 $changes{'coowners'} = 1;
 9987:             }
 9988:         } elsif ($coowners) {
 9989:             $changes{'coowners'} = 1;
 9990:         }
 9991:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
 9992:             $changes{'autofailsafe'} = 1;
 9993:         }
 9994:         if (keys(%changes) > 0) {
 9995:             $resulttext = &mt('Changes made:').'<ul>';
 9996:             if ($changes{'run'}) {
 9997:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 9998:             }
 9999:             if ($changes{'sender'}) {
10000:                 if ($sender_uname eq '' || $sender_domain eq '') {
10001:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
10002:                 } else {
10003:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
10004:                 }
10005:             }
10006:             if ($changes{'coowners'}) {
10007:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
10008:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
10009:                 if (ref($lastactref) eq 'HASH') {
10010:                     $lastactref->{'domainconfig'} = 1;
10011:                 }
10012:             }
10013:             if ($changes{'autofailsafe'}) {
10014:                 if ($failsafe ne '') {
10015:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$failsafe).'</li>';
10016:                 } else {
10017:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section: deleted');
10018:                 }
10019:                 &Apache::lonnet::get_domain_defaults($dom,1);
10020:                 if (ref($lastactref) eq 'HASH') {
10021:                     $lastactref->{'domdefaults'} = 1;
10022:                 }
10023:             }
10024:             $resulttext .= '</ul>';
10025:         } else {
10026:             $resulttext = &mt('No changes made to auto-enrollment settings');
10027:         }
10028:     } else {
10029:         $resulttext = '<span class="LC_error">'.
10030: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
10031:     }
10032:     return $resulttext;
10033: }
10034: 
10035: sub modify_autoupdate {
10036:     my ($dom,%domconfig) = @_;
10037:     my ($resulttext,%currautoupdate,%fields,%changes);
10038:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
10039:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
10040:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
10041:         }
10042:     }
10043:     my @offon = ('off','on');
10044:     my %title = &Apache::lonlocal::texthash (
10045:                    run => 'Auto-update:',
10046:                    classlists => 'Updates to user information in classlists?'
10047:                 );
10048:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10049:     my %fieldtitles = &Apache::lonlocal::texthash (
10050:                         id => 'Student/Employee ID',
10051:                         permanentemail => 'E-mail address',
10052:                         lastname => 'Last Name',
10053:                         firstname => 'First Name',
10054:                         middlename => 'Middle Name',
10055:                         generation => 'Generation',
10056:                       );
10057:     $othertitle = &mt('All users');
10058:     if (keys(%{$usertypes}) >  0) {
10059:         $othertitle = &mt('Other users');
10060:     }
10061:     foreach my $key (keys(%env)) {
10062:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
10063:             my ($usertype,$item) = ($1,$2);
10064:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
10065:                 if ($usertype eq 'default') {   
10066:                     push(@{$fields{$1}},$2);
10067:                 } elsif (ref($types) eq 'ARRAY') {
10068:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
10069:                         push(@{$fields{$1}},$2);
10070:                     }
10071:                 }
10072:             }
10073:         }
10074:     }
10075:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
10076:     @lockablenames = sort(@lockablenames);
10077:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
10078:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
10079:         if (@changed) {
10080:             $changes{'lockablenames'} = 1;
10081:         }
10082:     } else {
10083:         if (@lockablenames) {
10084:             $changes{'lockablenames'} = 1;
10085:         }
10086:     }
10087:     my %updatehash = (
10088:                       autoupdate => { run => $env{'form.autoupdate_run'},
10089:                                       classlists => $env{'form.classlists'},
10090:                                       fields => {%fields},
10091:                                       lockablenames => \@lockablenames,
10092:                                     }
10093:                      );
10094:     foreach my $key (keys(%currautoupdate)) {
10095:         if (($key eq 'run') || ($key eq 'classlists')) {
10096:             if (exists($updatehash{autoupdate}{$key})) {
10097:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
10098:                     $changes{$key} = 1;
10099:                 }
10100:             }
10101:         } elsif ($key eq 'fields') {
10102:             if (ref($currautoupdate{$key}) eq 'HASH') {
10103:                 foreach my $item (@{$types},'default') {
10104:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
10105:                         my $change = 0;
10106:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
10107:                             if (!exists($fields{$item})) {
10108:                                 $change = 1;
10109:                                 last;
10110:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
10111:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
10112:                                     $change = 1;
10113:                                     last;
10114:                                 }
10115:                             }
10116:                         }
10117:                         if ($change) {
10118:                             push(@{$changes{$key}},$item);
10119:                         }
10120:                     } 
10121:                 }
10122:             }
10123:         } elsif ($key eq 'lockablenames') {
10124:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
10125:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
10126:                 if (@changed) {
10127:                     $changes{'lockablenames'} = 1;
10128:                 }
10129:             } else {
10130:                 if (@lockablenames) {
10131:                     $changes{'lockablenames'} = 1;
10132:                 }
10133:             }
10134:         }
10135:     }
10136:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
10137:         if (@lockablenames) {
10138:             $changes{'lockablenames'} = 1;
10139:         }
10140:     }
10141:     foreach my $item (@{$types},'default') {
10142:         if (defined($fields{$item})) {
10143:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
10144:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
10145:                     my $change = 0;
10146:                     if (ref($fields{$item}) eq 'ARRAY') {
10147:                         foreach my $type (@{$fields{$item}}) {
10148:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
10149:                                 $change = 1;
10150:                                 last;
10151:                             }
10152:                         }
10153:                     }
10154:                     if ($change) {
10155:                         push(@{$changes{'fields'}},$item);
10156:                     }
10157:                 } else {
10158:                     push(@{$changes{'fields'}},$item);
10159:                 }
10160:             } else {
10161:                 push(@{$changes{'fields'}},$item);
10162:             }
10163:         }
10164:     }
10165:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
10166:                                              $dom);
10167:     if ($putresult eq 'ok') {
10168:         if (keys(%changes) > 0) {
10169:             $resulttext = &mt('Changes made:').'<ul>';
10170:             foreach my $key (sort(keys(%changes))) {
10171:                 if ($key eq 'lockablenames') {
10172:                     $resulttext .= '<li>';
10173:                     if (@lockablenames) {
10174:                         $usertypes->{'default'} = $othertitle;
10175:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
10176:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
10177:                     } else {
10178:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
10179:                     }
10180:                     $resulttext .= '</li>';
10181:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
10182:                     foreach my $item (@{$changes{$key}}) {
10183:                         my @newvalues;
10184:                         foreach my $type (@{$fields{$item}}) {
10185:                             push(@newvalues,$fieldtitles{$type});
10186:                         }
10187:                         my $newvaluestr;
10188:                         if (@newvalues > 0) {
10189:                             $newvaluestr = join(', ',@newvalues);
10190:                         } else {
10191:                             $newvaluestr = &mt('none');
10192:                         }
10193:                         if ($item eq 'default') {
10194:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
10195:                         } else {
10196:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
10197:                         }
10198:                     }
10199:                 } else {
10200:                     my $newvalue;
10201:                     if ($key eq 'run') {
10202:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
10203:                     } else {
10204:                         $newvalue = $offon[$env{'form.'.$key}];
10205:                     }
10206:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
10207:                 }
10208:             }
10209:             $resulttext .= '</ul>';
10210:         } else {
10211:             $resulttext = &mt('No changes made to autoupdates');
10212:         }
10213:     } else {
10214:         $resulttext = '<span class="LC_error">'.
10215: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
10216:     }
10217:     return $resulttext;
10218: }
10219: 
10220: sub modify_autocreate {
10221:     my ($dom,%domconfig) = @_;
10222:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
10223:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
10224:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
10225:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
10226:         }
10227:     }
10228:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
10229:                  req => 'Auto-creation of validated requests for official courses',
10230:                  xmldc => 'Identity of course creator of courses from XML files',
10231:                );
10232:     my @types = ('xml','req');
10233:     foreach my $item (@types) {
10234:         $newvals{$item} = $env{'form.autocreate_'.$item};
10235:         $newvals{$item} =~ s/\D//g;
10236:         $newvals{$item} = 0 if ($newvals{$item} eq '');
10237:     }
10238:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
10239:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
10240:     unless (exists($domcoords{$newvals{'xmldc'}})) {
10241:         $newvals{'xmldc'} = '';
10242:     } 
10243:     %autocreatehash =  (
10244:                         autocreate => { xml => $newvals{'xml'},
10245:                                         req => $newvals{'req'},
10246:                                       }
10247:                        );
10248:     if ($newvals{'xmldc'} ne '') {
10249:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
10250:     }
10251:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
10252:                                              $dom);
10253:     if ($putresult eq 'ok') {
10254:         my @items = @types;
10255:         if ($newvals{'xml'}) {
10256:             push(@items,'xmldc');
10257:         }
10258:         foreach my $item (@items) {
10259:             if (exists($currautocreate{$item})) {
10260:                 if ($currautocreate{$item} ne $newvals{$item}) {
10261:                     $changes{$item} = 1;
10262:                 }
10263:             } elsif ($newvals{$item}) {
10264:                 $changes{$item} = 1;
10265:             }
10266:         }
10267:         if (keys(%changes) > 0) {
10268:             my @offon = ('off','on'); 
10269:             $resulttext = &mt('Changes made:').'<ul>';
10270:             foreach my $item (@types) {
10271:                 if ($changes{$item}) {
10272:                     my $newtxt = $offon[$newvals{$item}];
10273:                     $resulttext .= '<li>'.
10274:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
10275:                                        '<b>','</b>').
10276:                                    '</li>';
10277:                 }
10278:             }
10279:             if ($changes{'xmldc'}) {
10280:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
10281:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
10282:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
10283:             }
10284:             $resulttext .= '</ul>';
10285:         } else {
10286:             $resulttext = &mt('No changes made to auto-creation settings');
10287:         }
10288:     } else {
10289:         $resulttext = '<span class="LC_error">'.
10290:             &mt('An error occurred: [_1]',$putresult).'</span>';
10291:     }
10292:     return $resulttext;
10293: }
10294: 
10295: sub modify_directorysrch {
10296:     my ($dom,$lastactref,%domconfig) = @_;
10297:     my ($resulttext,%changes);
10298:     my %currdirsrch;
10299:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
10300:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
10301:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
10302:         }
10303:     }
10304:     my %title = ( available => 'Institutional directory search available',
10305:                   localonly => 'Other domains can search institution',
10306:                   lcavailable => 'LON-CAPA directory search available',
10307:                   lclocalonly => 'Other domains can search LON-CAPA domain',
10308:                   searchby => 'Search types',
10309:                   searchtypes => 'Search latitude');
10310:     my @offon = ('off','on');
10311:     my @otherdoms = ('Yes','No');
10312: 
10313:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
10314:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
10315:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
10316: 
10317:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10318:     if (keys(%{$usertypes}) == 0) {
10319:         @cansearch = ('default');
10320:     } else {
10321:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
10322:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
10323:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
10324:                     push(@{$changes{'cansearch'}},$type);
10325:                 }
10326:             }
10327:             foreach my $type (@cansearch) {
10328:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
10329:                     push(@{$changes{'cansearch'}},$type);
10330:                 }
10331:             }
10332:         } else {
10333:             push(@{$changes{'cansearch'}},@cansearch);
10334:         }
10335:     }
10336: 
10337:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
10338:         foreach my $by (@{$currdirsrch{'searchby'}}) {
10339:             if (!grep(/^\Q$by\E$/,@searchby)) {
10340:                 push(@{$changes{'searchby'}},$by);
10341:             }
10342:         }
10343:         foreach my $by (@searchby) {
10344:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
10345:                 push(@{$changes{'searchby'}},$by);
10346:             }
10347:         }
10348:     } else {
10349:         push(@{$changes{'searchby'}},@searchby);
10350:     }
10351: 
10352:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
10353:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
10354:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
10355:                 push(@{$changes{'searchtypes'}},$type);
10356:             }
10357:         }
10358:         foreach my $type (@searchtypes) {
10359:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
10360:                 push(@{$changes{'searchtypes'}},$type);
10361:             }
10362:         }
10363:     } else {
10364:         if (exists($currdirsrch{'searchtypes'})) {
10365:             foreach my $type (@searchtypes) {  
10366:                 if ($type ne $currdirsrch{'searchtypes'}) { 
10367:                     push(@{$changes{'searchtypes'}},$type);
10368:                 }
10369:             }
10370:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
10371:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
10372:             }   
10373:         } else {
10374:             push(@{$changes{'searchtypes'}},@searchtypes); 
10375:         }
10376:     }
10377: 
10378:     my %dirsrch_hash =  (
10379:             directorysrch => { available => $env{'form.dirsrch_available'},
10380:                                cansearch => \@cansearch,
10381:                                localonly => $env{'form.dirsrch_instlocalonly'},
10382:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
10383:                                lcavailable => $env{'form.dirsrch_domavailable'},
10384:                                searchby => \@searchby,
10385:                                searchtypes => \@searchtypes,
10386:                              }
10387:             );
10388:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
10389:                                              $dom);
10390:     if ($putresult eq 'ok') {
10391:         if (exists($currdirsrch{'available'})) {
10392:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
10393:                  $changes{'available'} = 1;
10394:              }
10395:         } else {
10396:             if ($env{'form.dirsrch_available'} eq '1') {
10397:                 $changes{'available'} = 1;
10398:             }
10399:         }
10400:         if (exists($currdirsrch{'lcavailable'})) {
10401:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
10402:                 $changes{'lcavailable'} = 1;
10403:             }
10404:         } else {
10405:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
10406:                 $changes{'lcavailable'} = 1;
10407:             }
10408:         }
10409:         if (exists($currdirsrch{'localonly'})) {
10410:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
10411:                 $changes{'localonly'} = 1;
10412:             }
10413:         } else {
10414:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
10415:                 $changes{'localonly'} = 1;
10416:             }
10417:         }
10418:         if (exists($currdirsrch{'lclocalonly'})) {
10419:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
10420:                 $changes{'lclocalonly'} = 1;
10421:             }
10422:         } else {
10423:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
10424:                 $changes{'lclocalonly'} = 1;
10425:             }
10426:         }
10427:         if (keys(%changes) > 0) {
10428:             $resulttext = &mt('Changes made:').'<ul>';
10429:             if ($changes{'available'}) {
10430:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
10431:             }
10432:             if ($changes{'lcavailable'}) {
10433:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
10434:             }
10435:             if ($changes{'localonly'}) {
10436:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
10437:             }
10438:             if ($changes{'lclocalonly'}) {
10439:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
10440:             }
10441:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
10442:                 my $chgtext;
10443:                 if (ref($usertypes) eq 'HASH') {
10444:                     if (keys(%{$usertypes}) > 0) {
10445:                         foreach my $type (@{$types}) {
10446:                             if (grep(/^\Q$type\E$/,@cansearch)) {
10447:                                 $chgtext .= $usertypes->{$type}.'; ';
10448:                             }
10449:                         }
10450:                         if (grep(/^default$/,@cansearch)) {
10451:                             $chgtext .= $othertitle;
10452:                         } else {
10453:                             $chgtext =~ s/\; $//;
10454:                         }
10455:                         $resulttext .=
10456:                             '<li>'.
10457:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
10458:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
10459:                             '</li>';
10460:                     }
10461:                 }
10462:             }
10463:             if (ref($changes{'searchby'}) eq 'ARRAY') {
10464:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
10465:                 my $chgtext;
10466:                 foreach my $type (@{$titleorder}) {
10467:                     if (grep(/^\Q$type\E$/,@searchby)) {
10468:                         if (defined($searchtitles->{$type})) {
10469:                             $chgtext .= $searchtitles->{$type}.'; ';
10470:                         }
10471:                     }
10472:                 }
10473:                 $chgtext =~ s/\; $//;
10474:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
10475:             }
10476:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
10477:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
10478:                 my $chgtext;
10479:                 foreach my $type (@{$srchtypeorder}) {
10480:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
10481:                         if (defined($srchtypes_desc->{$type})) {
10482:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
10483:                         }
10484:                     }
10485:                 }
10486:                 $chgtext =~ s/\; $//;
10487:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
10488:             }
10489:             $resulttext .= '</ul>';
10490:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
10491:             if (ref($lastactref) eq 'HASH') {
10492:                 $lastactref->{'directorysrch'} = 1;
10493:             }
10494:         } else {
10495:             $resulttext = &mt('No changes made to directory search settings');
10496:         }
10497:     } else {
10498:         $resulttext = '<span class="LC_error">'.
10499:                       &mt('An error occurred: [_1]',$putresult).'</span>';
10500:     }
10501:     return $resulttext;
10502: }
10503: 
10504: sub modify_contacts {
10505:     my ($dom,$lastactref,%domconfig) = @_;
10506:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
10507:     if (ref($domconfig{'contacts'}) eq 'HASH') {
10508:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
10509:             $currsetting{$key} = $domconfig{'contacts'}{$key};
10510:         }
10511:     }
10512:     my (%others,%to,%bcc,%includestr,%includeloc);
10513:     my @contacts = ('supportemail','adminemail');
10514:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
10515:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail');
10516:     my @toggles = ('reporterrors','reportupdates');
10517:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
10518:     foreach my $type (@mailings) {
10519:         @{$newsetting{$type}} = 
10520:             &Apache::loncommon::get_env_multiple('form.'.$type);
10521:         foreach my $item (@contacts) {
10522:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
10523:                 $contacts_hash{contacts}{$type}{$item} = 1;
10524:             } else {
10525:                 $contacts_hash{contacts}{$type}{$item} = 0;
10526:             }
10527:         }
10528:         $others{$type} = $env{'form.'.$type.'_others'};
10529:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
10530:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10531:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
10532:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
10533:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
10534:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
10535:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
10536:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
10537:             }
10538:         }
10539:     }
10540:     foreach my $item (@contacts) {
10541:         $to{$item} = $env{'form.'.$item};
10542:         $contacts_hash{'contacts'}{$item} = $to{$item};
10543:     }
10544:     foreach my $item (@toggles) {
10545:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
10546:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
10547:         }
10548:     }
10549:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
10550:         foreach my $field (@{$fields}) {
10551:             if (ref($possoptions->{$field}) eq 'ARRAY') {
10552:                 my $value = $env{'form.helpform_'.$field};
10553:                 $value =~ s/^\s+|\s+$//g;
10554:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
10555:                     $contacts_hash{'contacts'}{'helpform'}{$field} = $value;
10556:                     if ($field eq 'screenshot') {
10557:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
10558:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
10559:                             $contacts_hash{'contacts'}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
10560:                         }
10561:                     }
10562:                 }
10563:             }
10564:         }
10565:     }
10566:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10567:     my (@statuses,%usertypeshash,@overrides);
10568:     if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
10569:         @statuses = @{$types};
10570:         if (ref($usertypes) eq 'HASH') {
10571:             %usertypeshash = %{$usertypes};
10572:         }
10573:     }
10574:     if (@statuses) {
10575:         my @possoverrides = &Apache::loncommon::get_env_multiple('form.overrides');
10576:         foreach my $type (@possoverrides) {
10577:             if (($type ne '') && (grep(/^\Q$type\E$/,@statuses))) {
10578:                 push(@overrides,$type);
10579:             }
10580:         }
10581:         if (@overrides) {
10582:             foreach my $type (@overrides) {
10583:                 my @standard = &Apache::loncommon::get_env_multiple('form.override_'.$type);
10584:                 foreach my $item (@contacts) {
10585:                     if (grep(/^\Q$item\E$/,@standard)) {
10586:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 1;
10587:                         $newsetting{'override_'.$type}{$item} = 1;
10588:                     } else {
10589:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 0;
10590:                         $newsetting{'override_'.$type}{$item} = 0;
10591:                     }
10592:                 }
10593:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'others'} = $env{'form.override_'.$type.'_others'};
10594:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
10595:                 $newsetting{'override_'.$type}{'others'} = $env{'form.override_'.$type.'_others'};
10596:                 $newsetting{'override_'.$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
10597:                 if (($env{'form.override_'.$type.'_includestr'} ne '') && ($env{'form.override_'.$type.'_includeloc'} =~ /^s|b$/)) {
10598:                     $includestr{$type} = $env{'form.override_'.$type.'_includestr'};
10599:                     $includeloc{$type} = $env{'form.override_'.$type.'_includeloc'};
10600:                     $contacts_hash{'contacts'}{'overrides'}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
10601:                     $newsetting{'override_'.$type}{'include'} = $contacts_hash{'contacts'}{'overrides'}{$type}{'include'};
10602:                 }
10603:             }
10604:         }
10605:     }
10606:     if (keys(%currsetting) > 0) {
10607:         foreach my $item (@contacts) {
10608:             if ($to{$item} ne $currsetting{$item}) {
10609:                 $changes{$item} = 1;
10610:             }
10611:         }
10612:         foreach my $type (@mailings) {
10613:             foreach my $item (@contacts) {
10614:                 if (ref($currsetting{$type}) eq 'HASH') {
10615:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
10616:                         push(@{$changes{$type}},$item);
10617:                     }
10618:                 } else {
10619:                     push(@{$changes{$type}},@{$newsetting{$type}});
10620:                 }
10621:             }
10622:             if ($others{$type} ne $currsetting{$type}{'others'}) {
10623:                 push(@{$changes{$type}},'others');
10624:             }
10625:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10626:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
10627:                     push(@{$changes{$type}},'bcc'); 
10628:                 }
10629:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
10630:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
10631:                     push(@{$changes{$type}},'include');
10632:                 }
10633:             }
10634:         }
10635:         if (ref($fields) eq 'ARRAY') {
10636:             if (ref($currsetting{'helpform'}) eq 'HASH') {
10637:                 foreach my $field (@{$fields}) {
10638:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
10639:                         push(@{$changes{'helpform'}},$field);
10640:                     }
10641:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10642:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
10643:                             push(@{$changes{'helpform'}},'maxsize');
10644:                         }
10645:                     }
10646:                 }
10647:             } else {
10648:                 foreach my $field (@{$fields}) {
10649:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
10650:                         push(@{$changes{'helpform'}},$field);
10651:                     }
10652:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10653:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
10654:                             push(@{$changes{'helpform'}},'maxsize');
10655:                         }
10656:                     }
10657:                 }
10658:             }
10659:         }
10660:         if (@statuses) {
10661:             if (ref($currsetting{'overrides'}) eq 'HASH') {
10662:                 foreach my $key (keys(%{$currsetting{'overrides'}})) {
10663:                     if (ref($currsetting{'overrides'}{$key}) eq 'HASH') {
10664:                         if (ref($newsetting{'override_'.$key}) eq 'HASH') {
10665:                             foreach my $item (@contacts,'bcc','others','include') {
10666:                                 if ($currsetting{'overrides'}{$key}{$item} ne $newsetting{'override_'.$key}{$item}) {
10667:                                     push(@{$changes{'overrides'}},$key);
10668:                                     last;
10669:                                 }
10670:                             }
10671:                         } else {
10672:                             push(@{$changes{'overrides'}},$key);
10673:                         }
10674:                     }
10675:                 }
10676:                 foreach my $key (@overrides) {
10677:                     unless (exists($currsetting{'overrides'}{$key})) {
10678:                         push(@{$changes{'overrides'}},$key);
10679:                     }
10680:                 }
10681:             } else {
10682:                 foreach my $key (@overrides) {
10683:                     push(@{$changes{'overrides'}},$key);
10684:                 }
10685:             }
10686:         }
10687:     } else {
10688:         my %default;
10689:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
10690:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
10691:         $default{'errormail'} = 'adminemail';
10692:         $default{'packagesmail'} = 'adminemail';
10693:         $default{'helpdeskmail'} = 'supportemail';
10694:         $default{'otherdomsmail'} = 'supportemail';
10695:         $default{'lonstatusmail'} = 'adminemail';
10696:         $default{'requestsmail'} = 'adminemail';
10697:         $default{'updatesmail'} = 'adminemail';
10698:         foreach my $item (@contacts) {
10699:            if ($to{$item} ne $default{$item}) {
10700:                $changes{$item} = 1;
10701:            }
10702:         }
10703:         foreach my $type (@mailings) {
10704:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
10705:                 push(@{$changes{$type}},@{$newsetting{$type}});
10706:             }
10707:             if ($others{$type} ne '') {
10708:                 push(@{$changes{$type}},'others');
10709:             }
10710:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10711:                 if ($bcc{$type} ne '') {
10712:                     push(@{$changes{$type}},'bcc');
10713:                 }
10714:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
10715:                     push(@{$changes{$type}},'include');
10716:                 }
10717:             }
10718:         }
10719:         if (ref($fields) eq 'ARRAY') {
10720:             foreach my $field (@{$fields}) {
10721:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
10722:                     push(@{$changes{'helpform'}},$field);
10723:                 }
10724:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10725:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
10726:                         push(@{$changes{'helpform'}},'maxsize');
10727:                     }
10728:                 }
10729:             }
10730:         }
10731:     }
10732:     foreach my $item (@toggles) {
10733:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
10734:             $changes{$item} = 1;
10735:         } elsif ((!$env{'form.'.$item}) &&
10736:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
10737:             $changes{$item} = 1;
10738:         }
10739:     }
10740:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
10741:                                              $dom);
10742:     if ($putresult eq 'ok') {
10743:         if (keys(%changes) > 0) {
10744:             &Apache::loncommon::devalidate_domconfig_cache($dom);
10745:             if (ref($lastactref) eq 'HASH') {
10746:                 $lastactref->{'domainconfig'} = 1;
10747:             }
10748:             my ($titles,$short_titles)  = &contact_titles();
10749:             $resulttext = &mt('Changes made:').'<ul>';
10750:             foreach my $item (@contacts) {
10751:                 if ($changes{$item}) {
10752:                     $resulttext .= '<li>'.$titles->{$item}.
10753:                                     &mt(' set to: ').
10754:                                     '<span class="LC_cusr_emph">'.
10755:                                     $to{$item}.'</span></li>';
10756:                 }
10757:             }
10758:             foreach my $type (@mailings) {
10759:                 if (ref($changes{$type}) eq 'ARRAY') {
10760:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10761:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
10762:                     } else {
10763:                         $resulttext .= '<li>'.$titles->{$type}.': ';
10764:                     }
10765:                     my @text;
10766:                     foreach my $item (@{$newsetting{$type}}) {
10767:                         push(@text,$short_titles->{$item});
10768:                     }
10769:                     if ($others{$type} ne '') {
10770:                         push(@text,$others{$type});
10771:                     }
10772:                     if (@text) {
10773:                         $resulttext .= '<span class="LC_cusr_emph">'.
10774:                                        join(', ',@text).'</span>';
10775:                     }
10776:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10777:                         if ($bcc{$type} ne '') {
10778:                             my $bcctext;
10779:                             if (@text) {
10780:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
10781:                             } else {
10782:                                 $bcctext = '(Bcc)';
10783:                             }
10784:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
10785:                         } elsif (!@text) {
10786:                             $resulttext .= &mt('No one');
10787:                         }
10788:                         if ($includestr{$type} ne '') {
10789:                             if ($includeloc{$type} eq 'b') {
10790:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
10791:                             } elsif ($includeloc{$type} eq 's') {
10792:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
10793:                             }
10794:                         }
10795:                     } elsif (!@text) {
10796:                         $resulttext .= &mt('No recipients');
10797:                     }
10798:                     $resulttext .= '</li>';
10799:                 }
10800:             }
10801:             if (ref($changes{'overrides'}) eq 'ARRAY') {
10802:                 my @deletions;
10803:                 foreach my $type (@{$changes{'overrides'}}) {
10804:                     if ($usertypeshash{$type}) {
10805:                         if (grep(/^\Q$type\E/,@overrides)) {
10806:                             $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation set for [_1]",
10807:                                                       $usertypeshash{$type}).'<ul><li>';
10808:                             if (ref($newsetting{'override_'.$type}) eq 'HASH') {
10809:                                 my @text;
10810:                                 foreach my $item (@contacts) {
10811:                                     if ($newsetting{'override_'.$type}{$item}) {
10812:                                         push(@text,$short_titles->{$item});
10813:                                     }
10814:                                 }
10815:                                 if ($newsetting{'override_'.$type}{'others'} ne '') {
10816:                                     push(@text,$newsetting{'override_'.$type}{'others'});
10817:                                 }
10818: 
10819:                                 if (@text) {
10820:                                     $resulttext .= &mt('Helpdesk e-mail sent to: [_1]',
10821:                                                        '<span class="LC_cusr_emph">'.join(', ',@text).'</span>');
10822:                                 }
10823:                                 if ($newsetting{'override_'.$type}{'bcc'} ne '') {
10824:                                     my $bcctext;
10825:                                     if (@text) {
10826:                                         $bcctext = '&nbsp;'.&mt('with Bcc to');
10827:                                     } else {
10828:                                         $bcctext = '(Bcc)';
10829:                                     }
10830:                                     $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$newsetting{'override_'.$type}{'bcc'}.'</span>';
10831:                                 } elsif (!@text) {
10832:                                      $resulttext .= &mt('Helpdesk e-mail sent to no one');
10833:                                 }
10834:                                 $resulttext .= '</li>';
10835:                                 if ($newsetting{'override_'.$type}{'include'} ne '') {
10836:                                     my ($loc,$str) = split(/:/,$newsetting{'override_'.$type}{'include'});
10837:                                     if ($loc eq 'b') {
10838:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail body:').' '.&unescape($str).'</li>';
10839:                                     } elsif ($loc eq 's') {
10840:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail subject:').' '.&unescape($str).'</li>';
10841:                                     }
10842:                                 }
10843:                             }
10844:                             $resulttext .= '</li></ul></li>';
10845:                         } else {
10846:                             push(@deletions,$usertypeshash{$type});
10847:                         }
10848:                     }
10849:                 }
10850:                 if (@deletions) {
10851:                     $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation discontinued for: [_1]",
10852:                                               join(', ',@deletions)).'</li>';
10853:                 }
10854:             }
10855:             my @offon = ('off','on');
10856:             if ($changes{'reporterrors'}) {
10857:                 $resulttext .= '<li>'.
10858:                                &mt('E-mail error reports to [_1] set to "'.
10859:                                    $offon[$env{'form.reporterrors'}].'".',
10860:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
10861:                                        &mt('LON-CAPA core group - MSU'),600,500)).
10862:                                '</li>';
10863:             }
10864:             if ($changes{'reportupdates'}) {
10865:                 $resulttext .= '<li>'.
10866:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
10867:                                     $offon[$env{'form.reportupdates'}].'".',
10868:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
10869:                                         &mt('LON-CAPA core group - MSU'),600,500)).
10870:                                 '</li>';
10871:             }
10872:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
10873:                 my (@optional,@required,@unused,$maxsizechg);
10874:                 foreach my $field (@{$changes{'helpform'}}) {
10875:                     if ($field eq 'maxsize') {
10876:                         $maxsizechg = 1;
10877:                         next;
10878:                     }
10879:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
10880:                         push(@optional,$field);
10881:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
10882:                         push(@unused,$field);
10883:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
10884:                         push(@required,$field);
10885:                     }
10886:                 }
10887:                 if (@optional) {
10888:                     $resulttext .= '<li>'.
10889:                                    &mt('Help form fields changed to "Optional": [_1].',
10890:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
10891:                                    '</li>';
10892:                 }
10893:                 if (@required) {
10894:                     $resulttext .= '<li>'.
10895:                                    &mt('Help form fields changed to "Required": [_1].',
10896:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
10897:                                    '</li>';
10898:                 }
10899:                 if (@unused) {
10900:                     $resulttext .= '<li>'.
10901:                                    &mt('Help form fields changed to "Not shown": [_1].',
10902:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
10903:                                    '</li>';
10904:                 }
10905:                 if ($maxsizechg) {
10906:                     $resulttext .= '<li>'.
10907:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
10908:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
10909:                                    '</li>';
10910:                 }
10911:             }
10912:             $resulttext .= '</ul>';
10913:         } else {
10914:             $resulttext = &mt('No changes made to contacts and form settings');
10915:         }
10916:     } else {
10917:         $resulttext = '<span class="LC_error">'.
10918:             &mt('An error occurred: [_1].',$putresult).'</span>';
10919:     }
10920:     return $resulttext;
10921: }
10922: 
10923: sub modify_usercreation {
10924:     my ($dom,%domconfig) = @_;
10925:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
10926:     my $warningmsg;
10927:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
10928:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
10929:             if ($key eq 'cancreate') {
10930:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
10931:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
10932:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
10933:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
10934:                             ($item eq 'recaptchaversion')) {
10935:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
10936:                         } else {
10937:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
10938:                         }
10939:                     }
10940:                 }
10941:             } elsif ($key eq 'email_rule') {
10942:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
10943:             } else {
10944:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
10945:             }
10946:         }
10947:     }
10948:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
10949:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
10950:     my @contexts = ('author','course','requestcrs');
10951:     foreach my $item(@contexts) {
10952:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
10953:     }
10954:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
10955:         foreach my $item (@contexts) {
10956:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
10957:                 push(@{$changes{'cancreate'}},$item);
10958:             }
10959:         }
10960:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
10961:         foreach my $item (@contexts) {
10962:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
10963:                 if ($cancreate{$item} ne 'any') {
10964:                     push(@{$changes{'cancreate'}},$item);
10965:                 }
10966:             } else {
10967:                 if ($cancreate{$item} ne 'none') {
10968:                     push(@{$changes{'cancreate'}},$item);
10969:                 }
10970:             }
10971:         }
10972:     } else {
10973:         foreach my $item (@contexts)  {
10974:             push(@{$changes{'cancreate'}},$item);
10975:         }
10976:     }
10977: 
10978:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
10979:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
10980:             if (!grep(/^\Q$type\E$/,@username_rule)) {
10981:                 push(@{$changes{'username_rule'}},$type);
10982:             }
10983:         }
10984:         foreach my $type (@username_rule) {
10985:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
10986:                 push(@{$changes{'username_rule'}},$type);
10987:             }
10988:         }
10989:     } else {
10990:         push(@{$changes{'username_rule'}},@username_rule);
10991:     }
10992: 
10993:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
10994:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
10995:             if (!grep(/^\Q$type\E$/,@id_rule)) {
10996:                 push(@{$changes{'id_rule'}},$type);
10997:             }
10998:         }
10999:         foreach my $type (@id_rule) {
11000:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
11001:                 push(@{$changes{'id_rule'}},$type);
11002:             }
11003:         }
11004:     } else {
11005:         push(@{$changes{'id_rule'}},@id_rule);
11006:     }
11007: 
11008:     my @authen_contexts = ('author','course','domain');
11009:     my @authtypes = ('int','krb4','krb5','loc');
11010:     my %authhash;
11011:     foreach my $item (@authen_contexts) {
11012:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
11013:         foreach my $auth (@authtypes) {
11014:             if (grep(/^\Q$auth\E$/,@authallowed)) {
11015:                 $authhash{$item}{$auth} = 1;
11016:             } else {
11017:                 $authhash{$item}{$auth} = 0;
11018:             }
11019:         }
11020:     }
11021:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
11022:         foreach my $item (@authen_contexts) {
11023:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
11024:                 foreach my $auth (@authtypes) {
11025:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
11026:                         push(@{$changes{'authtypes'}},$item);
11027:                         last;
11028:                     }
11029:                 }
11030:             }
11031:         }
11032:     } else {
11033:         foreach my $item (@authen_contexts) {
11034:             push(@{$changes{'authtypes'}},$item);
11035:         }
11036:     }
11037: 
11038:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
11039:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
11040:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
11041:     $save_usercreate{'id_rule'} = \@id_rule;
11042:     $save_usercreate{'username_rule'} = \@username_rule,
11043:     $save_usercreate{'authtypes'} = \%authhash;
11044: 
11045:     my %usercreation_hash =  (
11046:         usercreation     => \%save_usercreate,
11047:     );
11048: 
11049:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
11050:                                              $dom);
11051: 
11052:     if ($putresult eq 'ok') {
11053:         if (keys(%changes) > 0) {
11054:             $resulttext = &mt('Changes made:').'<ul>';
11055:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
11056:                 my %lt = &usercreation_types();
11057:                 foreach my $type (@{$changes{'cancreate'}}) {
11058:                     my $chgtext = $lt{$type}.', ';
11059:                     if ($cancreate{$type} eq 'none') {
11060:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
11061:                     } elsif ($cancreate{$type} eq 'any') {
11062:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
11063:                     } elsif ($cancreate{$type} eq 'official') {
11064:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
11065:                     } elsif ($cancreate{$type} eq 'unofficial') {
11066:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
11067:                     }
11068:                     $resulttext .= '<li>'.$chgtext.'</li>';
11069:                 }
11070:             }
11071:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
11072:                 my ($rules,$ruleorder) = 
11073:                     &Apache::lonnet::inst_userrules($dom,'username');
11074:                 my $chgtext = '<ul>';
11075:                 foreach my $type (@username_rule) {
11076:                     if (ref($rules->{$type}) eq 'HASH') {
11077:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
11078:                     }
11079:                 }
11080:                 $chgtext .= '</ul>';
11081:                 if (@username_rule > 0) {
11082:                     $resulttext .= '<li>'.&mt('Usernames 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 username formats restricted to verified users in the institutional directory.').'</li>'; 
11085:                 }
11086:             }
11087:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
11088:                 my ($idrules,$idruleorder) = 
11089:                     &Apache::lonnet::inst_userrules($dom,'id');
11090:                 my $chgtext = '<ul>';
11091:                 foreach my $type (@id_rule) {
11092:                     if (ref($idrules->{$type}) eq 'HASH') {
11093:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
11094:                     }
11095:                 }
11096:                 $chgtext .= '</ul>';
11097:                 if (@id_rule > 0) {
11098:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
11099:                 } else {
11100:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
11101:                 }
11102:             }
11103:             my %authname = &authtype_names();
11104:             my %context_title = &context_names();
11105:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
11106:                 my $chgtext = '<ul>';
11107:                 foreach my $type (@{$changes{'authtypes'}}) {
11108:                     my @allowed;
11109:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
11110:                     foreach my $auth (@authtypes) {
11111:                         if ($authhash{$type}{$auth}) {
11112:                             push(@allowed,$authname{$auth});
11113:                         }
11114:                     }
11115:                     if (@allowed > 0) {
11116:                         $chgtext .= join(', ',@allowed).'</li>';
11117:                     } else {
11118:                         $chgtext .= &mt('none').'</li>';
11119:                     }
11120:                 }
11121:                 $chgtext .= '</ul>';
11122:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
11123:                 $resulttext .= '</li>';
11124:             }
11125:             $resulttext .= '</ul>';
11126:         } else {
11127:             $resulttext = &mt('No changes made to user creation settings');
11128:         }
11129:     } else {
11130:         $resulttext = '<span class="LC_error">'.
11131:             &mt('An error occurred: [_1]',$putresult).'</span>';
11132:     }
11133:     if ($warningmsg ne '') {
11134:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
11135:     }
11136:     return $resulttext;
11137: }
11138: 
11139: sub modify_selfcreation {
11140:     my ($dom,%domconfig) = @_;
11141:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%changes,%cancreate);
11142:     my (%save_usercreate,%save_usermodify);
11143:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
11144:     if (ref($types) eq 'ARRAY') {
11145:         $usertypes->{'default'} = $othertitle;
11146:         push(@{$types},'default');
11147:     }
11148: #
11149: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
11150: #
11151:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
11152:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
11153:             if ($key eq 'cancreate') {
11154:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
11155:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
11156:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
11157:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') || 
11158:                             ($item eq 'recaptchaversion') ||
11159:                             ($item eq 'emailusername') || ($item eq 'notify') ||
11160:                             ($item eq 'selfcreateprocessing') || ($item eq 'shibenv')) {
11161:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11162:                         } else {
11163:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11164:                         }
11165:                     }
11166:                 }
11167:             } elsif ($key eq 'email_rule') {
11168:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
11169:             } else {
11170:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
11171:             }
11172:         }
11173:     }
11174: #
11175: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
11176: #
11177:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
11178:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
11179:             if ($key eq 'selfcreate') {
11180:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
11181:             } else {
11182:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
11183:             }
11184:         }
11185:     }
11186: 
11187:     my @contexts = ('selfcreate');
11188:     @{$cancreate{'selfcreate'}} = ();
11189:     %{$cancreate{'emailusername'}} = ();
11190:     @{$cancreate{'statustocreate'}} = ();
11191:     %{$cancreate{'selfcreateprocessing'}} = ();
11192:     %{$cancreate{'shibenv'}} = ();
11193:     my %selfcreatetypes = (
11194:                              sso   => 'users authenticated by institutional single sign on',
11195:                              login => 'users authenticated by institutional log-in',
11196:                              email => 'users who provide a valid e-mail address for use as username',
11197:                           );
11198: #
11199: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
11200: # is permitted.
11201: #
11202: 
11203:     my @statuses;
11204:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
11205:         if (ref($domconfig{'inststatus'}{'inststatusguest'}) eq 'ARRAY') {
11206:             @statuses = @{$domconfig{'inststatus'}{'inststatusguest'}};
11207:         }
11208:     }
11209:     push(@statuses,'default');
11210: 
11211:     foreach my $item ('login','sso','email') {
11212:         if ($item eq 'email') {
11213:             if ($env{'form.cancreate_email'}) {
11214:                 push(@{$cancreate{'selfcreate'}},'email');
11215:                 push(@contexts,'selfcreateprocessing');
11216:                 foreach my $type (@statuses) {
11217:                     if ($type eq 'default') {
11218:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess'};
11219:                     } else { 
11220:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
11221:                     }
11222:                 }
11223:             }
11224:         } else {
11225:             if ($env{'form.cancreate_'.$item}) {
11226:                 push(@{$cancreate{'selfcreate'}},$item);
11227:             }
11228:         }
11229:     }
11230:     my (@email_rule,%userinfo,%savecaptcha);
11231:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
11232: #
11233: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
11234: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
11235: #
11236: 
11237:     if ($env{'form.cancreate_email'}) {
11238:         push(@contexts,'emailusername');
11239:         if (ref($types) eq 'ARRAY') {
11240:             foreach my $type (@{$types}) {
11241:                 if (ref($infofields) eq 'ARRAY') {
11242:                     foreach my $field (@{$infofields}) {
11243:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
11244:                             $cancreate{'emailusername'}{$type}{$field} = $1;
11245:                         }
11246:                     }
11247:                 }
11248:             }
11249:         }
11250: #
11251: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
11252: # queued requests for self-creation of account using e-mail address as username
11253: #
11254: 
11255:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
11256:         @approvalnotify = sort(@approvalnotify);
11257:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
11258:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
11259:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
11260:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
11261:                     push(@{$changes{'cancreate'}},'notify');
11262:                 }
11263:             } else {
11264:                 if ($cancreate{'notify'}{'approval'}) {
11265:                     push(@{$changes{'cancreate'}},'notify');
11266:                 }
11267:             }
11268:         } elsif ($cancreate{'notify'}{'approval'}) {
11269:             push(@{$changes{'cancreate'}},'notify');
11270:         }
11271: 
11272: #
11273: # Retrieve rules (if any) governing types of e-mail address which may be used as a username
11274: #
11275:         @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
11276:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
11277:         if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
11278:             if (@{$curr_usercreation{'email_rule'}} > 0) {
11279:                 foreach my $type (@{$curr_usercreation{'email_rule'}}) {
11280:                     if (!grep(/^\Q$type\E$/,@email_rule)) {
11281:                         push(@{$changes{'email_rule'}},$type);
11282:                     }
11283:                 }
11284:             }
11285:             if (@email_rule > 0) {
11286:                 foreach my $type (@email_rule) {
11287:                     if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
11288:                         push(@{$changes{'email_rule'}},$type);
11289:                     }
11290:                 }
11291:             }
11292:         } elsif (@email_rule > 0) {
11293:             push(@{$changes{'email_rule'}},@email_rule);
11294:         }
11295:     }
11296: #  
11297: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
11298: # institutional log-in.
11299: #
11300:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
11301:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11302:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
11303:                ($domdefaults{'auth_def'} eq 'localauth'))) {
11304:             $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.').' '.
11305:                           &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.');
11306:         }
11307:     }
11308:     my @fields = ('lastname','firstname','middlename','generation',
11309:                   'permanentemail','id');
11310:     my @shibfields = (@fields,'inststatus');
11311:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11312: #
11313: # Where usernames may created for institutional log-in and/or institutional single sign on:
11314: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
11315: # may self-create accounts 
11316: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
11317: # which the user may supply, if institutional data is unavailable.
11318: #
11319:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
11320:         if (ref($types) eq 'ARRAY') {
11321:             if (@{$types} > 1) {
11322:                 @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
11323:                 push(@contexts,'statustocreate');
11324:             } else {
11325:                 undef($cancreate{'statustocreate'});
11326:             } 
11327:             foreach my $type (@{$types}) {
11328:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
11329:                 foreach my $field (@fields) {
11330:                     if (grep(/^\Q$field\E$/,@modifiable)) {
11331:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
11332:                     } else {
11333:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
11334:                     }
11335:                 }
11336:             }
11337:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
11338:                 foreach my $type (@{$types}) {
11339:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
11340:                         foreach my $field (@fields) {
11341:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
11342:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
11343:                                 push(@{$changes{'selfcreate'}},$type);
11344:                                 last;
11345:                             }
11346:                         }
11347:                     }
11348:                 }
11349:             } else {
11350:                 foreach my $type (@{$types}) {
11351:                     push(@{$changes{'selfcreate'}},$type);
11352:                 }
11353:             }
11354:         }
11355:         foreach my $field (@shibfields) {
11356:             if ($env{'form.shibenv_'.$field} ne '') {
11357:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
11358:             }
11359:         }
11360:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
11361:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
11362:                 foreach my $field (@shibfields) {
11363:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
11364:                         push(@{$changes{'cancreate'}},'shibenv');
11365:                     }
11366:                 }
11367:             } else {
11368:                 foreach my $field (@shibfields) {
11369:                     if ($env{'form.shibenv_'.$field}) {
11370:                         push(@{$changes{'cancreate'}},'shibenv');
11371:                         last;
11372:                     }
11373:                 }
11374:             }
11375:         }
11376:     }
11377:     foreach my $item (@contexts) {
11378:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
11379:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
11380:                 if (ref($cancreate{$item}) eq 'ARRAY') {
11381:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
11382:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11383:                             push(@{$changes{'cancreate'}},$item);
11384:                         }
11385:                     }
11386:                 }
11387:             }
11388:             if (ref($cancreate{$item}) eq 'ARRAY') {
11389:                 foreach my $type (@{$cancreate{$item}}) {
11390:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
11391:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11392:                             push(@{$changes{'cancreate'}},$item);
11393:                         }
11394:                     }
11395:                 }
11396:             }
11397:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
11398:             if (ref($cancreate{$item}) eq 'HASH') {
11399:                 foreach my $curr (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
11400:                     if (ref($curr_usercreation{'cancreate'}{$item}{$curr}) eq 'HASH') {
11401:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$curr}})) {
11402:                             unless ($curr_usercreation{'cancreate'}{$item}{$curr}{$field} eq $cancreate{$item}{$curr}{$field}) {
11403:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11404:                                     push(@{$changes{'cancreate'}},$item);
11405:                                 }
11406:                             }
11407:                         }
11408:                     } elsif ($item eq 'selfcreateprocessing') {
11409:                         if ($cancreate{$item}{$curr} ne $curr_usercreation{'cancreate'}{$item}{$curr}) {
11410:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11411:                                 push(@{$changes{'cancreate'}},$item);
11412:                             }
11413:                         }
11414:                     } else {
11415:                         if (!$cancreate{$item}{$curr}) {
11416:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11417:                                 push(@{$changes{'cancreate'}},$item);
11418:                             }
11419:                         }
11420:                     }
11421:                 }
11422:                 foreach my $field (keys(%{$cancreate{$item}})) {
11423:                     if (ref($cancreate{$item}{$field}) eq 'HASH') {
11424:                         foreach my $inner (keys(%{$cancreate{$item}{$field}})) {
11425:                             if (ref($curr_usercreation{'cancreate'}{$item}{$field}) eq 'HASH') {
11426:                                 unless ($curr_usercreation{'cancreate'}{$item}{$field}{$inner} eq $cancreate{$item}{$field}{$inner}) {
11427:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11428:                                         push(@{$changes{'cancreate'}},$item);
11429:                                     }
11430:                                 }
11431:                             } else {
11432:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11433:                                     push(@{$changes{'cancreate'}},$item);
11434:                                 }
11435:                             }
11436:                         }
11437:                     } elsif ($item eq 'selfcreateprocessing') {
11438:                         if ($cancreate{$item}{$field} ne $curr_usercreation{'cancreate'}{$item}{$field}) {
11439:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11440:                                 push(@{$changes{'cancreate'}},$item);
11441:                             }
11442:                         }
11443:                     } else {
11444:                         if (!$curr_usercreation{'cancreate'}{$item}{$field}) {
11445:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11446:                                 push(@{$changes{'cancreate'}},$item);
11447:                             }
11448:                         }
11449:                     }
11450:                 }
11451:             }
11452:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
11453:             if (ref($cancreate{$item}) eq 'ARRAY') {
11454:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
11455:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11456:                         push(@{$changes{'cancreate'}},$item);
11457:                     }
11458:                 }
11459:             } elsif (ref($cancreate{$item}) eq 'HASH') {
11460:                 if (!$cancreate{$item}{$curr_usercreation{'cancreate'}{$item}}) {
11461:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11462:                         push(@{$changes{'cancreate'}},$item);
11463:                     }
11464:                 }
11465:             }
11466:         } elsif ($item eq 'emailusername') {
11467:             if (ref($cancreate{$item}) eq 'HASH') {
11468:                 foreach my $type (keys(%{$cancreate{$item}})) {
11469:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
11470:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
11471:                             if ($cancreate{$item}{$type}{$field}) {
11472:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11473:                                     push(@{$changes{'cancreate'}},$item);
11474:                                 }
11475:                                 last;
11476:                             }
11477:                         }
11478:                     }
11479:                 }
11480:             }
11481:         }
11482:     }
11483: #
11484: # Populate %save_usercreate hash with updates to self-creation configuration.
11485: #
11486:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
11487:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
11488:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
11489:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
11490:     if (ref($cancreate{'notify'}) eq 'HASH') {
11491:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
11492:     }
11493:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
11494:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
11495:     }
11496:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
11497:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
11498:     }
11499:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
11500:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
11501:     }
11502:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
11503:     $save_usercreate{'email_rule'} = \@email_rule;
11504: 
11505:     my %userconfig_hash = (
11506:             usercreation     => \%save_usercreate,
11507:             usermodification => \%save_usermodify,
11508:     );
11509:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
11510:                                              $dom);
11511: #
11512: # Accumulate details of changes to domain cofiguration for self-creation of usernames in $resulttext
11513: #
11514:     if ($putresult eq 'ok') {
11515:         if (keys(%changes) > 0) {
11516:             $resulttext = &mt('Changes made:').'<ul>';
11517:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
11518:                 my %lt = &selfcreation_types();
11519:                 foreach my $type (@{$changes{'cancreate'}}) {
11520:                     my $chgtext;
11521:                     if ($type eq 'selfcreate') {
11522:                         if (@{$cancreate{$type}} == 0) {
11523:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
11524:                         } else {
11525:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
11526:                                         '<ul>';
11527:                             foreach my $case (@{$cancreate{$type}}) {
11528:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
11529:                             }
11530:                             $chgtext .= '</ul>';
11531:                             if (ref($cancreate{$type}) eq 'ARRAY') {
11532:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
11533:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
11534:                                         if (@{$cancreate{'statustocreate'}} == 0) {
11535:                                             $chgtext .= '<br />'.
11536:                                                         '<span class="LC_warning">'.
11537:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
11538:                                                         '</span>';
11539:                                         }
11540:                                     }
11541:                                 }
11542:                             }
11543:                         }
11544:                     } elsif ($type eq 'shibenv') {
11545:                         if (keys(%{$cancreate{$type}}) == 0) {
11546:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information'); 
11547:                         } else {
11548:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
11549:                                         '<ul>';
11550:                             foreach my $field (@shibfields) {
11551:                                 next if ($cancreate{$type}{$field} eq '');
11552:                                 if ($field eq 'inststatus') {
11553:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
11554:                                 } else {
11555:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
11556:                                 }
11557:                             }
11558:                             $chgtext .= '</ul>';
11559:                         }  
11560:                     } elsif ($type eq 'statustocreate') {
11561:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
11562:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
11563:                             if (@{$cancreate{'selfcreate'}} > 0) {
11564:                                 if (@{$cancreate{'statustocreate'}} == 0) {
11565:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
11566:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
11567:                                         $chgtext .= '<br />'.
11568:                                                     '<span class="LC_warning">'.
11569:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
11570:                                                     '</span>';
11571:                                     }
11572:                                 } elsif (ref($usertypes) eq 'HASH') {
11573:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
11574:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
11575:                                     } else {
11576:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
11577:                                     }
11578:                                     $chgtext .= '<ul>';
11579:                                     foreach my $case (@{$cancreate{$type}}) {
11580:                                         if ($case eq 'default') {
11581:                                             $chgtext .= '<li>'.$othertitle.'</li>';
11582:                                         } else {
11583:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
11584:                                         }
11585:                                     }
11586:                                     $chgtext .= '</ul>';
11587:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
11588:                                         $chgtext .= '<br /><span class="LC_warning">'.
11589:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
11590:                                                     '</span>';
11591:                                     }
11592:                                 }
11593:                             } else {
11594:                                 if (@{$cancreate{$type}} == 0) {
11595:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
11596:                                 } else {
11597:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
11598:                                 }
11599:                             }
11600:                         }
11601:                     } elsif ($type eq 'selfcreateprocessing') {
11602:                         my %choices = &Apache::lonlocal::texthash (
11603:                                                                     automatic => 'Automatic approval',
11604:                                                                     approval  => 'Queued for approval',
11605:                                                                   );
11606:                         if (@statuses > 1) {
11607:                             $chgtext .= &mt('Processing of requests to create account with e-mail address as username set as follows:'). 
11608:                                         '<ul>';
11609:                            foreach my $type (@statuses) {
11610:                                if ($type eq 'default') {
11611:                                    $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
11612:                                } else {
11613:                                    $chgtext .= '<li>'.$usertypes->{$type}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
11614:                                }
11615:                            }
11616:                            $chgtext .= '</ul>';
11617:                         } else {
11618:                            $chgtext .= &mt('Processing of requests to create account with e-mail address as username set to: "[_1]"',
11619:                                          $choices{$cancreate{'selfcreateprocessing'}{'default'}});
11620:                         }
11621:                     } elsif ($type eq 'captcha') {
11622:                         if ($savecaptcha{$type} eq 'notused') {
11623:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
11624:                         } else {
11625:                             my %captchas = &captcha_phrases();
11626:                             if ($captchas{$savecaptcha{$type}}) {
11627:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
11628:                             } else {
11629:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
11630:                             }
11631:                         }
11632:                     } elsif ($type eq 'recaptchakeys') {
11633:                         my ($privkey,$pubkey);
11634:                         if (ref($savecaptcha{$type}) eq 'HASH') {
11635:                             $pubkey = $savecaptcha{$type}{'public'};
11636:                             $privkey = $savecaptcha{$type}{'private'};
11637:                         }
11638:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
11639:                         if (!$pubkey) {
11640:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
11641:                         } else {
11642:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
11643:                         }
11644:                         if (!$privkey) {
11645:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
11646:                         } else {
11647:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
11648:                         }
11649:                         $chgtext .= '</ul>';
11650:                     } elsif ($type eq 'recaptchaversion') {
11651:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
11652:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
11653:                         }
11654:                     } elsif ($type eq 'emailusername') {
11655:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
11656:                             if (ref($types) eq 'ARRAY') {
11657:                                 foreach my $type (@{$types}) {
11658:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
11659:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
11660:                                             $chgtext .= &mt('When self-creating account with e-mail as username, the following information will be provided by [_1]:',"'$usertypes->{$type}'").
11661:                                                     '<ul>';
11662:                                             foreach my $field (@{$infofields}) {
11663:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
11664:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
11665:                                                 }
11666:                                             }
11667:                                             $chgtext .= '</ul>';
11668:                                         } else {
11669:                                             $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 />';
11670:                                         }
11671:                                     } else {
11672:                                         $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 />';
11673:                                     }
11674:                                 }
11675:                             }
11676:                         }
11677:                     } elsif ($type eq 'notify') {
11678:                         $chgtext = &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
11679:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
11680:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
11681:                                 if ($cancreate{'notify'}{'approval'}) {
11682:                                     $chgtext = &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
11683:                                 }
11684:                             }
11685:                         }
11686:                     }
11687:                     if ($chgtext) {
11688:                         $resulttext .= '<li>'.$chgtext.'</li>';
11689:                     }
11690:                 }
11691:             }
11692:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
11693:                 my ($emailrules,$emailruleorder) =
11694:                     &Apache::lonnet::inst_userrules($dom,'email');
11695:                 my $chgtext = '<ul>';
11696:                 foreach my $type (@email_rule) {
11697:                     if (ref($emailrules->{$type}) eq 'HASH') {
11698:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
11699:                     }
11700:                 }
11701:                 $chgtext .= '</ul>';
11702:                 if (@email_rule > 0) {
11703:                     $resulttext .= '<li>'.
11704:                                    &mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').
11705:                                        $chgtext.
11706:                                    '</li>';
11707:                 } else {
11708:                     $resulttext .= '<li>'.
11709:                                    &mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').
11710:                                    '</li>';
11711:                 }
11712:             }
11713:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
11714:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
11715:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11716:                 foreach my $type (@{$changes{'selfcreate'}}) {
11717:                     my $typename = $type;
11718:                     if (ref($usertypes) eq 'HASH') {
11719:                         if ($usertypes->{$type} ne '') {
11720:                             $typename = $usertypes->{$type};
11721:                         }
11722:                     }
11723:                     my @modifiable;
11724:                     $resulttext .= '<li>'.
11725:                                     &mt('Self-creation of account by users with status: [_1]',
11726:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
11727:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
11728:                     foreach my $field (@fields) {
11729:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
11730:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
11731:                         }
11732:                     }
11733:                     if (@modifiable > 0) {
11734:                         $resulttext .= join(', ',@modifiable);
11735:                     } else {
11736:                         $resulttext .= &mt('none');
11737:                     }
11738:                     $resulttext .= '</li>';
11739:                 }
11740:                 $resulttext .= '</ul></li>';
11741:             }
11742:             $resulttext .= '</ul>';
11743:         } else {
11744:             $resulttext = &mt('No changes made to self-creation settings');
11745:         }
11746:     } else {
11747:         $resulttext = '<span class="LC_error">'.
11748:             &mt('An error occurred: [_1]',$putresult).'</span>';
11749:     }
11750:     if ($warningmsg ne '') {
11751:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
11752:     }
11753:     return $resulttext;
11754: }
11755: 
11756: sub process_captcha {
11757:     my ($container,$changes,$newsettings,$current) = @_;
11758:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
11759:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
11760:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
11761:         $newsettings->{'captcha'} = 'original';
11762:     }
11763:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
11764:         if ($container eq 'cancreate') {
11765:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
11766:                 push(@{$changes->{'cancreate'}},'captcha');
11767:             } elsif (!defined($changes->{'cancreate'})) {
11768:                 $changes->{'cancreate'} = ['captcha'];
11769:             }
11770:         } else {
11771:             $changes->{'captcha'} = 1;
11772:         }
11773:     }
11774:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
11775:     if ($newsettings->{'captcha'} eq 'recaptcha') {
11776:         $newpub = $env{'form.'.$container.'_recaptchapub'};
11777:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
11778:         $newpub =~ s/[^\w\-]//g;
11779:         $newpriv =~ s/[^\w\-]//g;
11780:         $newsettings->{'recaptchakeys'} = {
11781:                                              public  => $newpub,
11782:                                              private => $newpriv,
11783:                                           };
11784:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
11785:         $newversion =~ s/\D//g;
11786:         if ($newversion ne '2') {
11787:             $newversion = 1;
11788:         }
11789:         $newsettings->{'recaptchaversion'} = $newversion;
11790:     }
11791:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
11792:         $currpub = $current->{'recaptchakeys'}{'public'};
11793:         $currpriv = $current->{'recaptchakeys'}{'private'};
11794:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
11795:             $newsettings->{'recaptchakeys'} = {
11796:                                                  public  => '',
11797:                                                  private => '',
11798:                                               }
11799:         }
11800:     }
11801:     if ($current->{'captcha'} eq 'recaptcha') {
11802:         $currversion = $current->{'recaptchaversion'};
11803:         if ($currversion ne '2') {
11804:             $currversion = 1;
11805:         }
11806:     }
11807:     if ($currversion ne $newversion) {
11808:         if ($container eq 'cancreate') {
11809:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
11810:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
11811:             } elsif (!defined($changes->{'cancreate'})) {
11812:                 $changes->{'cancreate'} = ['recaptchaversion'];
11813:             }
11814:         } else {
11815:             $changes->{'recaptchaversion'} = 1;
11816:         }
11817:     }
11818:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
11819:         if ($container eq 'cancreate') {
11820:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
11821:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
11822:             } elsif (!defined($changes->{'cancreate'})) {
11823:                 $changes->{'cancreate'} = ['recaptchakeys'];
11824:             }
11825:         } else {
11826:             $changes->{'recaptchakeys'} = 1;
11827:         }
11828:     }
11829:     return;
11830: }
11831: 
11832: sub modify_usermodification {
11833:     my ($dom,%domconfig) = @_;
11834:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
11835:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
11836:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
11837:             if ($key eq 'selfcreate') {
11838:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
11839:             } else {  
11840:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
11841:             }
11842:         }
11843:     }
11844:     my @contexts = ('author','course');
11845:     my %context_title = (
11846:                            author => 'In author context',
11847:                            course => 'In course context',
11848:                         );
11849:     my @fields = ('lastname','firstname','middlename','generation',
11850:                   'permanentemail','id');
11851:     my %roles = (
11852:                   author => ['ca','aa'],
11853:                   course => ['st','ep','ta','in','cr'],
11854:                 );
11855:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11856:     foreach my $context (@contexts) {
11857:         foreach my $role (@{$roles{$context}}) {
11858:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
11859:             foreach my $item (@fields) {
11860:                 if (grep(/^\Q$item\E$/,@modifiable)) {
11861:                     $modifyhash{$context}{$role}{$item} = 1;
11862:                 } else {
11863:                     $modifyhash{$context}{$role}{$item} = 0;
11864:                 }
11865:             }
11866:         }
11867:         if (ref($curr_usermodification{$context}) eq 'HASH') {
11868:             foreach my $role (@{$roles{$context}}) {
11869:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
11870:                     foreach my $field (@fields) {
11871:                         if ($modifyhash{$context}{$role}{$field} ne 
11872:                                 $curr_usermodification{$context}{$role}{$field}) {
11873:                             push(@{$changes{$context}},$role);
11874:                             last;
11875:                         }
11876:                     }
11877:                 }
11878:             }
11879:         } else {
11880:             foreach my $context (@contexts) {
11881:                 foreach my $role (@{$roles{$context}}) {
11882:                     push(@{$changes{$context}},$role);
11883:                 }
11884:             }
11885:         }
11886:     }
11887:     my %usermodification_hash =  (
11888:                                    usermodification => \%modifyhash,
11889:                                  );
11890:     my $putresult = &Apache::lonnet::put_dom('configuration',
11891:                                              \%usermodification_hash,$dom);
11892:     if ($putresult eq 'ok') {
11893:         if (keys(%changes) > 0) {
11894:             $resulttext = &mt('Changes made: ').'<ul>';
11895:             foreach my $context (@contexts) {
11896:                 if (ref($changes{$context}) eq 'ARRAY') {
11897:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
11898:                     if (ref($changes{$context}) eq 'ARRAY') {
11899:                         foreach my $role (@{$changes{$context}}) {
11900:                             my $rolename;
11901:                             if ($role eq 'cr') {
11902:                                 $rolename = &mt('Custom');
11903:                             } else {
11904:                                 $rolename = &Apache::lonnet::plaintext($role);
11905:                             }
11906:                             my @modifiable;
11907:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
11908:                             foreach my $field (@fields) {
11909:                                 if ($modifyhash{$context}{$role}{$field}) {
11910:                                     push(@modifiable,$fieldtitles{$field});
11911:                                 }
11912:                             }
11913:                             if (@modifiable > 0) {
11914:                                 $resulttext .= join(', ',@modifiable);
11915:                             } else {
11916:                                 $resulttext .= &mt('none'); 
11917:                             }
11918:                             $resulttext .= '</li>';
11919:                         }
11920:                         $resulttext .= '</ul></li>';
11921:                     }
11922:                 }
11923:             }
11924:             $resulttext .= '</ul>';
11925:         } else {
11926:             $resulttext = &mt('No changes made to user modification settings');
11927:         }
11928:     } else {
11929:         $resulttext = '<span class="LC_error">'.
11930:             &mt('An error occurred: [_1]',$putresult).'</span>';
11931:     }
11932:     return $resulttext;
11933: }
11934: 
11935: sub modify_defaults {
11936:     my ($dom,$lastactref,%domconfig) = @_;
11937:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
11938:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11939:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
11940:                  'portal_def','intauth_cost','intauth_check','intauth_switch');
11941:     my @authtypes = ('internal','krb4','krb5','localauth');
11942:     foreach my $item (@items) {
11943:         $newvalues{$item} = $env{'form.'.$item};
11944:         if ($item eq 'auth_def') {
11945:             if ($newvalues{$item} ne '') {
11946:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
11947:                     push(@errors,$item);
11948:                 }
11949:             }
11950:         } elsif ($item eq 'lang_def') {
11951:             if ($newvalues{$item} ne '') {
11952:                 if ($newvalues{$item} =~ /^(\w+)/) {
11953:                     my $langcode = $1;
11954:                     if ($langcode ne 'x_chef') {
11955:                         if (code2language($langcode) eq '') {
11956:                             push(@errors,$item);
11957:                         }
11958:                     }
11959:                 } else {
11960:                     push(@errors,$item);
11961:                 }
11962:             }
11963:         } elsif ($item eq 'timezone_def') {
11964:             if ($newvalues{$item} ne '') {
11965:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
11966:                     push(@errors,$item);   
11967:                 }
11968:             }
11969:         } elsif ($item eq 'datelocale_def') {
11970:             if ($newvalues{$item} ne '') {
11971:                 my @datelocale_ids = DateTime::Locale->ids();
11972:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
11973:                     push(@errors,$item);
11974:                 }
11975:             }
11976:         } elsif ($item eq 'portal_def') {
11977:             if ($newvalues{$item} ne '') {
11978:                 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])\/?$/) {
11979:                     push(@errors,$item);
11980:                 }
11981:             }
11982:         } elsif ($item eq 'intauth_cost') {
11983:             if ($newvalues{$item} ne '') {
11984:                 if ($newvalues{$item} =~ /\D/) {
11985:                     push(@errors,$item);
11986:                 }
11987:             }
11988:         } elsif ($item eq 'intauth_check') {
11989:             if ($newvalues{$item} ne '') {
11990:                 unless ($newvalues{$item} =~ /^(0|1|2)$/) {
11991:                     push(@errors,$item);
11992:                 }
11993:             }
11994:         } elsif ($item eq 'intauth_switch') {
11995:             if ($newvalues{$item} ne '') {
11996:                 unless ($newvalues{$item} =~ /^(0|1|2)$/) {
11997:                     push(@errors,$item);
11998:                 }
11999:             }
12000:         }
12001:         if (grep(/^\Q$item\E$/,@errors)) {
12002:             $newvalues{$item} = $domdefaults{$item};
12003:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
12004:             $changes{$item} = 1;
12005:         }
12006:         $domdefaults{$item} = $newvalues{$item};
12007:     }
12008:     my %defaults_hash = (
12009:                          defaults => \%newvalues,
12010:                         );
12011:     my $title = &defaults_titles();
12012: 
12013:     my $currinststatus;
12014:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
12015:         $currinststatus = $domconfig{'inststatus'};
12016:     } else {
12017:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12018:         $currinststatus = {
12019:                              inststatustypes => $usertypes,
12020:                              inststatusorder => $types,
12021:                              inststatusguest => [],
12022:                           };
12023:     }
12024:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
12025:     my @allpos;
12026:     my %guests;
12027:     my %alltypes;
12028:     my ($currtitles,$currguests,$currorder);
12029:     if (ref($currinststatus) eq 'HASH') {
12030:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
12031:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
12032:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
12033:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
12034:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
12035:                     }
12036:                 }
12037:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
12038:                     my $position = $env{'form.inststatus_pos_'.$type};
12039:                     $position =~ s/\D+//g;
12040:                     $allpos[$position] = $type;
12041:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
12042:                     $alltypes{$type} =~ s/`//g;
12043:                     if ($env{'form.inststatus_guest_'.$type}) {
12044:                         $guests{$type} = 1;
12045:                     }
12046:                 }
12047:             }
12048:             if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
12049:                 $currguests = join(',',@{$currinststatus->{'inststatusguest'}});
12050:             }
12051:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
12052:             $currtitles =~ s/,$//;
12053:         }
12054:     }
12055:     if ($env{'form.addinststatus'}) {
12056:         my $newtype = $env{'form.addinststatus'};
12057:         $newtype =~ s/\W//g;
12058:         unless (exists($alltypes{$newtype})) {
12059:             if ($env{'form.addinststatus_guest'}) {
12060:                 $guests{$newtype} = 1;
12061:             }
12062:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
12063:             $alltypes{$newtype} =~ s/`//g; 
12064:             my $position = $env{'form.addinststatus_pos'};
12065:             $position =~ s/\D+//g;
12066:             if ($position ne '') {
12067:                 $allpos[$position] = $newtype;
12068:             }
12069:         }
12070:     }
12071:     my (@orderedstatus,@orderedguests);
12072:     foreach my $type (@allpos) {
12073:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
12074:             push(@orderedstatus,$type);
12075:             if ($guests{$type}) {
12076:                 push(@orderedguests,$type);
12077:             }
12078:         }
12079:     }
12080:     foreach my $type (keys(%alltypes)) {
12081:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
12082:             delete($alltypes{$type});
12083:         }
12084:     }
12085:     $defaults_hash{'inststatus'} = {
12086:                                      inststatustypes => \%alltypes,
12087:                                      inststatusorder => \@orderedstatus,
12088:                                      inststatusguest => \@orderedguests,
12089:                                    };
12090:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
12091:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
12092:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
12093:         }
12094:     }
12095:     if ($currorder ne join(',',@orderedstatus)) {
12096:         $changes{'inststatus'}{'inststatusorder'} = 1;
12097:     }
12098:     if ($currguests ne join(',',@orderedguests)) {
12099:         $changes{'inststatus'}{'inststatusguest'} = 1;
12100:     }
12101:     my $newtitles;
12102:     foreach my $item (@orderedstatus) {
12103:         $newtitles .= $alltypes{$item}.',';
12104:     }
12105:     $newtitles =~ s/,$//;
12106:     if ($currtitles ne $newtitles) {
12107:         $changes{'inststatus'}{'inststatustypes'} = 1;
12108:     }
12109:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
12110:                                              $dom);
12111:     if ($putresult eq 'ok') {
12112:         if (keys(%changes) > 0) {
12113:             $resulttext = &mt('Changes made:').'<ul>';
12114:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
12115:             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";
12116:             foreach my $item (sort(keys(%changes))) {
12117:                 if ($item eq 'inststatus') {
12118:                     if (ref($changes{'inststatus'}) eq 'HASH') {
12119:                         if (($changes{'inststatus'}{'inststatustypes'}) || $changes{'inststatus'}{'inststatusorder'}) {
12120:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
12121:                             foreach my $type (@orderedstatus) { 
12122:                                 $resulttext .= $alltypes{$type}.', ';
12123:                             }
12124:                             $resulttext =~ s/, $//;
12125:                             $resulttext .= '</li>';
12126:                         }
12127:                         if ($changes{'inststatus'}{'inststatusguest'}) {
12128:                             $resulttext .= '<li>'; 
12129:                             if (@orderedguests) {
12130:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to:').' ';
12131:                                 foreach my $type (@orderedguests) {
12132:                                     $resulttext .= $alltypes{$type}.', ';
12133:                                 }
12134:                                 $resulttext =~ s/, $//;
12135:                             } else {
12136:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to none.');
12137:                             }
12138:                             $resulttext .= '</li>';
12139:                         }
12140:                     }
12141:                 } else {
12142:                     my $value = $env{'form.'.$item};
12143:                     if ($value eq '') {
12144:                         $value = &mt('none');
12145:                     } elsif ($item eq 'auth_def') {
12146:                         my %authnames = &authtype_names();
12147:                         my %shortauth = (
12148:                                           internal   => 'int',
12149:                                           krb4       => 'krb4',
12150:                                           krb5       => 'krb5',
12151:                                           localauth  => 'loc',
12152:                         );
12153:                         $value = $authnames{$shortauth{$value}};
12154:                     } elsif ($item eq 'intauth_switch') {
12155:                         my %optiondesc = &Apache::lonlocal::texthash (
12156:                                             0 => 'No',
12157:                                             1 => 'Yes',
12158:                                             2 => 'Yes, and copy existing passwd file to passwd.bak file',
12159:                                          );
12160:                         if ($value =~ /^(0|1|2)$/) {
12161:                             $value = $optiondesc{$value};
12162:                         } else {
12163:                             $value = &mt('none -- defaults to No');
12164:                         }
12165:                     } elsif ($item eq 'intauth_check') {
12166:                         my %optiondesc = &Apache::lonlocal::texthash (
12167:                                              0 => 'No',
12168:                                              1 => 'Yes, allow login then update passwd file using default cost (if higher)',
12169:                                              2 => 'Yes, disallow login if stored cost is less than domain default',
12170:                                          );
12171:                         if ($value =~ /^(0|1|2)$/) {
12172:                             $value = $optiondesc{$value};
12173:                         } else {
12174:                             $value = &mt('none -- defaults to No');
12175:                         }
12176:                     }
12177:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
12178:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
12179:                 }
12180:             }
12181:             $resulttext .= '</ul>';
12182:             $mailmsgtext .= "\n";
12183:             my $cachetime = 24*60*60;
12184:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12185:             if (ref($lastactref) eq 'HASH') {
12186:                 $lastactref->{'domdefaults'} = 1;
12187:             }
12188:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
12189:                 my $notify = 1;
12190:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
12191:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
12192:                         $notify = 0;
12193:                     }
12194:                 }
12195:                 if ($notify) {
12196:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
12197:                                                "LON-CAPA Domain Settings Change - $dom",
12198:                                                $mailmsgtext);
12199:                 }
12200:             }
12201:         } else {
12202:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
12203:         }
12204:     } else {
12205:         $resulttext = '<span class="LC_error">'.
12206:             &mt('An error occurred: [_1]',$putresult).'</span>';
12207:     }
12208:     if (@errors > 0) {
12209:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
12210:         foreach my $item (@errors) {
12211:             $resulttext .= ' "'.$title->{$item}.'",';
12212:         }
12213:         $resulttext =~ s/,$//;
12214:     }
12215:     return $resulttext;
12216: }
12217: 
12218: sub modify_scantron {
12219:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
12220:     my ($resulttext,%confhash,%changes,$errors);
12221:     my $custom = 'custom.tab';
12222:     my $default = 'default.tab';
12223:     my $servadm = $r->dir_config('lonAdmEMail');
12224:     my ($configuserok,$author_ok,$switchserver) = 
12225:         &config_check($dom,$confname,$servadm);
12226:     if ($env{'form.scantronformat.filename'} ne '') {
12227:         my $error;
12228:         if ($configuserok eq 'ok') {
12229:             if ($switchserver) {
12230:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
12231:             } else {
12232:                 if ($author_ok eq 'ok') {
12233:                     my ($result,$scantronurl) =
12234:                         &publishlogo($r,'upload','scantronformat',$dom,
12235:                                      $confname,'scantron','','',$custom);
12236:                     if ($result eq 'ok') {
12237:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
12238:                         $changes{'scantronformat'} = 1;
12239:                     } else {
12240:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
12241:                     }
12242:                 } else {
12243:                     $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);
12244:                 }
12245:             }
12246:         } else {
12247:             $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);
12248:         }
12249:         if ($error) {
12250:             &Apache::lonnet::logthis($error);
12251:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12252:         }
12253:     }
12254:     if (ref($domconfig{'scantron'}) eq 'HASH') {
12255:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
12256:             if ($env{'form.scantronformat_del'}) {
12257:                 $confhash{'scantron'}{'scantronformat'} = '';
12258:                 $changes{'scantronformat'} = 1;
12259:             }
12260:         }
12261:     }
12262:     if (keys(%confhash) > 0) {
12263:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
12264:                                                  $dom);
12265:         if ($putresult eq 'ok') {
12266:             if (keys(%changes) > 0) {
12267:                 if (ref($confhash{'scantron'}) eq 'HASH') {
12268:                     $resulttext = &mt('Changes made:').'<ul>';
12269:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
12270:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
12271:                     } else {
12272:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
12273:                     }
12274:                     $resulttext .= '</ul>';
12275:                 } else {
12276:                     $resulttext = &mt('Changes made to bubblesheet format file.');
12277:                 }
12278:                 $resulttext .= '</ul>';
12279:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
12280:                 if (ref($lastactref) eq 'HASH') {
12281:                     $lastactref->{'domainconfig'} = 1;
12282:                 }
12283:             } else {
12284:                 $resulttext = &mt('No changes made to bubblesheet format file');
12285:             }
12286:         } else {
12287:             $resulttext = '<span class="LC_error">'.
12288:                 &mt('An error occurred: [_1]',$putresult).'</span>';
12289:         }
12290:     } else {
12291:         $resulttext = &mt('No changes made to bubblesheet format file'); 
12292:     }
12293:     if ($errors) {
12294:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
12295:                        $errors.'</ul>';
12296:     }
12297:     return $resulttext;
12298: }
12299: 
12300: sub modify_coursecategories {
12301:     my ($dom,$lastactref,%domconfig) = @_;
12302:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
12303:         $cathash);
12304:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
12305:     my @catitems = ('unauth','auth');
12306:     my @cattypes = ('std','domonly','codesrch','none');
12307:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
12308:         $cathash = $domconfig{'coursecategories'}{'cats'};
12309:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
12310:             $changes{'togglecats'} = 1;
12311:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
12312:         }
12313:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
12314:             $changes{'categorize'} = 1;
12315:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
12316:         }
12317:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
12318:             $changes{'togglecatscomm'} = 1;
12319:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
12320:         }
12321:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
12322:             $changes{'categorizecomm'} = 1;
12323:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
12324:         }
12325:         foreach my $item (@catitems) {
12326:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
12327:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
12328:                     $changes{$item} = 1;
12329:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
12330:                 }
12331:             }
12332:         }
12333:     } else {
12334:         $changes{'togglecats'} = 1;
12335:         $changes{'categorize'} = 1;
12336:         $changes{'togglecatscomm'} = 1;
12337:         $changes{'categorizecomm'} = 1;
12338:         $domconfig{'coursecategories'} = {
12339:                                              togglecats => $env{'form.togglecats'},
12340:                                              categorize => $env{'form.categorize'},
12341:                                              togglecatscomm => $env{'form.togglecatscomm'},
12342:                                              categorizecomm => $env{'form.categorizecomm'},
12343:                                          };
12344:         foreach my $item (@catitems) {
12345:             if ($env{'form.coursecat_'.$item} ne 'std') {
12346:                 $changes{$item} = 1;
12347:             }
12348:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
12349:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
12350:             }
12351:         }
12352:     }
12353:     if (ref($cathash) eq 'HASH') {
12354:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
12355:             push (@deletecategory,'instcode::0');
12356:         }
12357:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
12358:             push(@deletecategory,'communities::0');
12359:         }
12360:     }
12361:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
12362:     if (ref($cathash) eq 'HASH') {
12363:         if (@deletecategory > 0) {
12364:             #FIXME Need to remove category from all courses using a deleted category 
12365:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
12366:             foreach my $item (@deletecategory) {
12367:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
12368:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
12369:                     $deletions{$item} = 1;
12370:                     &recurse_cat_deletes($item,$cathash,\%deletions);
12371:                 }
12372:             }
12373:         }
12374:         foreach my $item (keys(%{$cathash})) {
12375:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
12376:             if ($cathash->{$item} ne $env{'form.'.$item}) {
12377:                 $reorderings{$item} = 1;
12378:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
12379:             }
12380:             if ($env{'form.addcategory_name_'.$item} ne '') {
12381:                 my $newcat = $env{'form.addcategory_name_'.$item};
12382:                 my $newdepth = $depth+1;
12383:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
12384:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
12385:                 $adds{$newitem} = 1; 
12386:             }
12387:             if ($env{'form.subcat_'.$item} ne '') {
12388:                 my $newcat = $env{'form.subcat_'.$item};
12389:                 my $newdepth = $depth+1;
12390:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
12391:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
12392:                 $adds{$newitem} = 1;
12393:             }
12394:         }
12395:     }
12396:     if ($env{'form.instcode'} eq '1') {
12397:         if (ref($cathash) eq 'HASH') {
12398:             my $newitem = 'instcode::0';
12399:             if ($cathash->{$newitem} eq '') {  
12400:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
12401:                 $adds{$newitem} = 1;
12402:             }
12403:         } else {
12404:             my $newitem = 'instcode::0';
12405:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
12406:             $adds{$newitem} = 1;
12407:         }
12408:     }
12409:     if ($env{'form.communities'} eq '1') {
12410:         if (ref($cathash) eq 'HASH') {
12411:             my $newitem = 'communities::0';
12412:             if ($cathash->{$newitem} eq '') {
12413:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
12414:                 $adds{$newitem} = 1;
12415:             }
12416:         } else {
12417:             my $newitem = 'communities::0';
12418:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
12419:             $adds{$newitem} = 1;
12420:         }
12421:     }
12422:     if ($env{'form.addcategory_name'} ne '') {
12423:         if (($env{'form.addcategory_name'} ne 'instcode') &&
12424:             ($env{'form.addcategory_name'} ne 'communities')) {
12425:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
12426:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
12427:             $adds{$newitem} = 1;
12428:         }
12429:     }
12430:     my $putresult;
12431:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
12432:         if (keys(%deletions) > 0) {
12433:             foreach my $key (keys(%deletions)) {
12434:                 if ($predelallitems{$key} ne '') {
12435:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
12436:                 }
12437:             }
12438:         }
12439:         my (@chkcats,@chktrails,%chkallitems);
12440:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
12441:         if (ref($chkcats[0]) eq 'ARRAY') {
12442:             my $depth = 0;
12443:             my $chg = 0;
12444:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
12445:                 my $name = $chkcats[0][$i];
12446:                 my $item;
12447:                 if ($name eq '') {
12448:                     $chg ++;
12449:                 } else {
12450:                     $item = &escape($name).'::0';
12451:                     if ($chg) {
12452:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
12453:                     }
12454:                     $depth ++; 
12455:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
12456:                     $depth --;
12457:                 }
12458:             }
12459:         }
12460:     }
12461:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
12462:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
12463:         if ($putresult eq 'ok') {
12464:             my %title = (
12465:                          togglecats     => 'Show/Hide a course in catalog',
12466:                          categorize     => 'Assign a category to a course',
12467:                          togglecatscomm => 'Show/Hide a community in catalog',
12468:                          categorizecomm => 'Assign a category to a community',
12469:                         );
12470:             my %level = (
12471:                          dom  => 'set in Domain ("Modify Course/Community")',
12472:                          crs  => 'set in Course ("Course Configuration")',
12473:                          comm => 'set in Community ("Community Configuration")',
12474:                          none     => 'No catalog',
12475:                          std      => 'Standard catalog',
12476:                          domonly  => 'Domain-only catalog',
12477:                          codesrch => 'Code search form',
12478:                         );
12479:             $resulttext = &mt('Changes made:').'<ul>';
12480:             if ($changes{'togglecats'}) {
12481:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
12482:             }
12483:             if ($changes{'categorize'}) {
12484:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
12485:             }
12486:             if ($changes{'togglecatscomm'}) {
12487:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
12488:             }
12489:             if ($changes{'categorizecomm'}) {
12490:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
12491:             }
12492:             if ($changes{'unauth'}) {
12493:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
12494:             }
12495:             if ($changes{'auth'}) {
12496:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
12497:             }
12498:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
12499:                 my $cathash;
12500:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
12501:                     $cathash = $domconfig{'coursecategories'}{'cats'};
12502:                 } else {
12503:                     $cathash = {};
12504:                 } 
12505:                 my (@cats,@trails,%allitems);
12506:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
12507:                 if (keys(%deletions) > 0) {
12508:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
12509:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
12510:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
12511:                     }
12512:                     $resulttext .= '</ul></li>';
12513:                 }
12514:                 if (keys(%reorderings) > 0) {
12515:                     my %sort_by_trail;
12516:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
12517:                     foreach my $key (keys(%reorderings)) {
12518:                         if ($allitems{$key} ne '') {
12519:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
12520:                         }
12521:                     }
12522:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
12523:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
12524:                     }
12525:                     $resulttext .= '</ul></li>';
12526:                 }
12527:                 if (keys(%adds) > 0) {
12528:                     my %sort_by_trail;
12529:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
12530:                     foreach my $key (keys(%adds)) {
12531:                         if ($allitems{$key} ne '') {
12532:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
12533:                         }
12534:                     }
12535:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
12536:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
12537:                     }
12538:                     $resulttext .= '</ul></li>';
12539:                 }
12540:             }
12541:             $resulttext .= '</ul>';
12542:             if ($changes{'unauth'} || $changes{'auth'}) {
12543:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
12544:                 if ($changes{'auth'}) {
12545:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
12546:                 }
12547:                 if ($changes{'unauth'}) {
12548:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
12549:                 }
12550:                 my $cachetime = 24*60*60;
12551:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12552:                 if (ref($lastactref) eq 'HASH') {
12553:                     $lastactref->{'domdefaults'} = 1;
12554:                 }
12555:             }
12556:         } else {
12557:             $resulttext = '<span class="LC_error">'.
12558:                           &mt('An error occurred: [_1]',$putresult).'</span>';
12559:         }
12560:     } else {
12561:         $resulttext = &mt('No changes made to course and community categories');
12562:     }
12563:     return $resulttext;
12564: }
12565: 
12566: sub modify_serverstatuses {
12567:     my ($dom,%domconfig) = @_;
12568:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
12569:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
12570:         %currserverstatus = %{$domconfig{'serverstatuses'}};
12571:     }
12572:     my @pages = &serverstatus_pages();
12573:     foreach my $type (@pages) {
12574:         $newserverstatus{$type}{'namedusers'} = '';
12575:         $newserverstatus{$type}{'machines'} = '';
12576:         if (defined($env{'form.'.$type.'_namedusers'})) {
12577:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
12578:             my @okusers;
12579:             foreach my $user (@users) {
12580:                 my ($uname,$udom) = split(/:/,$user);
12581:                 if (($udom =~ /^$match_domain$/) &&   
12582:                     (&Apache::lonnet::domain($udom)) &&
12583:                     ($uname =~ /^$match_username$/)) {
12584:                     if (!grep(/^\Q$user\E/,@okusers)) {
12585:                         push(@okusers,$user);
12586:                     }
12587:                 }
12588:             }
12589:             if (@okusers > 0) {
12590:                  @okusers = sort(@okusers);
12591:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
12592:             }
12593:         }
12594:         if (defined($env{'form.'.$type.'_machines'})) {
12595:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
12596:             my @okmachines;
12597:             foreach my $ip (@machines) {
12598:                 my @parts = split(/\./,$ip);
12599:                 next if (@parts < 4);
12600:                 my $badip = 0;
12601:                 for (my $i=0; $i<4; $i++) {
12602:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
12603:                         $badip = 1;
12604:                         last;
12605:                     }
12606:                 }
12607:                 if (!$badip) {
12608:                     push(@okmachines,$ip);     
12609:                 }
12610:             }
12611:             @okmachines = sort(@okmachines);
12612:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
12613:         }
12614:     }
12615:     my %serverstatushash =  (
12616:                                 serverstatuses => \%newserverstatus,
12617:                             );
12618:     foreach my $type (@pages) {
12619:         foreach my $setting ('namedusers','machines') {
12620:             my (@current,@new);
12621:             if (ref($currserverstatus{$type}) eq 'HASH') {
12622:                 if ($currserverstatus{$type}{$setting} ne '') { 
12623:                     @current = split(/,/,$currserverstatus{$type}{$setting});
12624:                 }
12625:             }
12626:             if ($newserverstatus{$type}{$setting} ne '') {
12627:                 @new = split(/,/,$newserverstatus{$type}{$setting});
12628:             }
12629:             if (@current > 0) {
12630:                 if (@new > 0) {
12631:                     foreach my $item (@current) {
12632:                         if (!grep(/^\Q$item\E$/,@new)) {
12633:                             $changes{$type}{$setting} = 1;
12634:                             last;
12635:                         }
12636:                     }
12637:                     foreach my $item (@new) {
12638:                         if (!grep(/^\Q$item\E$/,@current)) {
12639:                             $changes{$type}{$setting} = 1;
12640:                             last;
12641:                         }
12642:                     }
12643:                 } else {
12644:                     $changes{$type}{$setting} = 1;
12645:                 }
12646:             } elsif (@new > 0) {
12647:                 $changes{$type}{$setting} = 1;
12648:             }
12649:         }
12650:     }
12651:     if (keys(%changes) > 0) {
12652:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
12653:         my $putresult = &Apache::lonnet::put_dom('configuration',
12654:                                                  \%serverstatushash,$dom);
12655:         if ($putresult eq 'ok') {
12656:             $resulttext .= &mt('Changes made:').'<ul>';
12657:             foreach my $type (@pages) {
12658:                 if (ref($changes{$type}) eq 'HASH') {
12659:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
12660:                     if ($changes{$type}{'namedusers'}) {
12661:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
12662:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
12663:                         } else {
12664:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
12665:                         }
12666:                     }
12667:                     if ($changes{$type}{'machines'}) {
12668:                         if ($newserverstatus{$type}{'machines'} eq '') {
12669:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
12670:                         } else {
12671:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
12672:                         }
12673: 
12674:                     }
12675:                     $resulttext .= '</ul></li>';
12676:                 }
12677:             }
12678:             $resulttext .= '</ul>';
12679:         } else {
12680:             $resulttext = '<span class="LC_error">'.
12681:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
12682: 
12683:         }
12684:     } else {
12685:         $resulttext = &mt('No changes made to access to server status pages');
12686:     }
12687:     return $resulttext;
12688: }
12689: 
12690: sub modify_helpsettings {
12691:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
12692:     my ($resulttext,$errors,%changes,%helphash);
12693:     my %defaultchecked = ('submitbugs' => 'on');
12694:     my @offon = ('off','on');
12695:     my @toggles = ('submitbugs');
12696:     my %current = ('submitbugs' => '',
12697:                    'adhoc'      => {},
12698:                   );
12699:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
12700:         %current = %{$domconfig{'helpsettings'}};
12701:     }
12702:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12703:     foreach my $item (@toggles) {
12704:         if ($defaultchecked{$item} eq 'on') { 
12705:             if ($current{$item} eq '') {
12706:                 if ($env{'form.'.$item} eq '0') {
12707:                     $changes{$item} = 1;
12708:                 }
12709:             } elsif ($current{$item} ne $env{'form.'.$item}) {
12710:                 $changes{$item} = 1;
12711:             }
12712:         } elsif ($defaultchecked{$item} eq 'off') {
12713:             if ($current{$item} eq '') {
12714:                 if ($env{'form.'.$item} eq '1') {
12715:                     $changes{$item} = 1;
12716:                 }
12717:             } elsif ($current{$item} ne $env{'form.'.$item}) {
12718:                 $changes{$item} = 1;
12719:             }
12720:         }
12721:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
12722:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
12723:         }
12724:     }
12725:     my $maxnum = $env{'form.helproles_maxnum'};
12726:     my $confname = $dom.'-domainconfig';
12727:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
12728:     my (@allpos,%newsettings,%changedprivs,$newrole);
12729:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12730:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
12731:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
12732:     my %lt = &Apache::lonlocal::texthash(
12733:                     s      => 'system',
12734:                     d      => 'domain',
12735:                     order  => 'Display order',
12736:                     access => 'Role usage',
12737:                     all    => 'All with domain helpdesk or helpdesk assistant role',
12738:                     dh     => 'All with domain helpdesk role',
12739:                     da     => 'All with domain helpdesk assistant role',
12740:                     none   => 'None',
12741:                     status => 'Determined based on institutional status',
12742:                     inc    => 'Include all, but exclude specific personnel',
12743:                     exc    => 'Exclude all, but include specific personnel',
12744:     );
12745:     for (my $num=0; $num<=$maxnum; $num++) {
12746:         my ($prefix,$identifier,$rolename,%curr);
12747:         if ($num == $maxnum) {
12748:             next unless ($env{'form.newcusthelp'} == $maxnum);
12749:             $identifier = 'custhelp'.$num;
12750:             $prefix = 'helproles_'.$num;
12751:             $rolename = $env{'form.custhelpname'.$num};
12752:             $rolename=~s/[^A-Za-z0-9]//gs;
12753:             next if ($rolename eq '');
12754:             next if (exists($existing{'rolesdef_'.$rolename}));
12755:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
12756:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
12757:                                                      $newprivs{'c'},$confname,$dom);
12758:             if ($result ne 'ok') {
12759:                 $errors .= '<li><span class="LC_error">'.
12760:                            &mt('An error occurred storing the new custom role: [_1]',
12761:                            $result).'</span></li>';
12762:                 next;
12763:             } else {
12764:                 $changedprivs{$rolename} = \%newprivs;
12765:                 $newrole = $rolename;
12766:             }
12767:         } else {
12768:             $prefix = 'helproles_'.$num;
12769:             $rolename = $env{'form.'.$prefix};
12770:             next if ($rolename eq '');
12771:             next unless (exists($existing{'rolesdef_'.$rolename}));
12772:             $identifier = 'custhelp'.$num;
12773:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
12774:             my %currprivs;
12775:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
12776:                 split(/\_/,$existing{'rolesdef_'.$rolename});
12777:             foreach my $level ('c','d','s') {
12778:                 if ($newprivs{$level} ne $currprivs{$level}) {
12779:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
12780:                                                              $newprivs{'c'},$confname,$dom);
12781:                     if ($result ne 'ok') {
12782:                         $errors .= '<li><span class="LC_error">'.
12783:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
12784:                                        $rolename,$result).'</span></li>';
12785:                     } else {
12786:                         $changedprivs{$rolename} = \%newprivs;
12787:                     }
12788:                     last;
12789:                 }
12790:             }
12791:             if (ref($current{'adhoc'}) eq 'HASH') {
12792:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
12793:                     %curr = %{$current{'adhoc'}{$rolename}};
12794:                 }
12795:             }
12796:         }
12797:         my $newpos = $env{'form.'.$prefix.'_pos'};
12798:         $newpos =~ s/\D+//g;
12799:         $allpos[$newpos] = $rolename;
12800:         my $newdesc = $env{'form.'.$prefix.'_desc'};
12801:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
12802:         if ($curr{'desc'}) {
12803:             if ($curr{'desc'} ne $newdesc) {
12804:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
12805:                 $newsettings{$rolename}{'desc'} = $newdesc;
12806:             }
12807:         } elsif ($newdesc ne '') {
12808:             $changes{'customrole'}{$rolename}{'desc'} = 1;
12809:             $newsettings{$rolename}{'desc'} = $newdesc;
12810:         }
12811:         my $access = $env{'form.'.$prefix.'_access'};
12812:         if (grep(/^\Q$access\E$/,@accesstypes)) {
12813:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
12814:             if ($access eq 'status') {
12815:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
12816:                 if (scalar(@statuses) == 0) {
12817:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
12818:                 } else {
12819:                     my (@shownstatus,$numtypes);
12820:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
12821:                     if (ref($types) eq 'ARRAY') {
12822:                         $numtypes = scalar(@{$types});
12823:                         foreach my $type (sort(@statuses)) {
12824:                             if ($type eq 'default') {
12825:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
12826:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
12827:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
12828:                                 push(@shownstatus,$usertypes->{$type});
12829:                             }
12830:                         }
12831:                     }
12832:                     if (grep(/^default$/,@statuses)) {
12833:                         push(@shownstatus,$othertitle);
12834:                     }
12835:                     if (scalar(@shownstatus) == 1+$numtypes) {
12836:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
12837:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
12838:                     } else {
12839:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
12840:                         if (ref($curr{'status'}) eq 'ARRAY') {
12841:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
12842:                             if (@diffs) {
12843:                                 $changes{'customrole'}{$rolename}{$access} = 1;
12844:                             }
12845:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
12846:                             $changes{'customrole'}{$rolename}{$access} = 1;
12847:                         }
12848:                     }
12849:                 }
12850:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
12851:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
12852:                 my @newspecstaff;
12853:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
12854:                 foreach my $person (sort(@personnel)) {
12855:                     if ($domhelpdesk{$person}) {
12856:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
12857:                     }
12858:                 }
12859:                 if (ref($curr{$access}) eq 'ARRAY') {
12860:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
12861:                     if (@diffs) {
12862:                         $changes{'customrole'}{$rolename}{$access} = 1;
12863:                     }
12864:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
12865:                     $changes{'customrole'}{$rolename}{$access} = 1;
12866:                 }
12867:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
12868:                     my ($uname,$udom) = split(/:/,$person);
12869:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
12870:                 }
12871:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
12872:             }
12873:         } else {
12874:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
12875:         }
12876:         unless ($curr{'access'} eq $access) {
12877:             $changes{'customrole'}{$rolename}{'access'} = 1;
12878:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
12879:         }
12880:     }
12881:     if (@allpos > 0) {
12882:         my $idx = 0;
12883:         foreach my $rolename (@allpos) {
12884:             if ($rolename ne '') {
12885:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
12886:                 if (ref($current{'adhoc'}) eq 'HASH') {
12887:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
12888:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
12889:                             $changes{'customrole'}{$rolename}{'order'} = 1;
12890:                             $newsettings{$rolename}{'order'} = $idx+1;
12891:                         }
12892:                     }
12893:                 }
12894:                 $idx ++;
12895:             }
12896:         }
12897:     }
12898:     my $putresult;
12899:     if (keys(%changes) > 0) {
12900:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
12901:         if ($putresult eq 'ok') {
12902:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
12903:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
12904:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
12905:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
12906:                 }
12907:             }
12908:             my $cachetime = 24*60*60;
12909:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12910:             if (ref($lastactref) eq 'HASH') {
12911:                 $lastactref->{'domdefaults'} = 1;
12912:             }
12913:         } else {
12914:             $errors .= '<li><span class="LC_error">'.
12915:                        &mt('An error occurred storing the settings: [_1]',
12916:                            $putresult).'</span></li>';
12917:         }
12918:     }
12919:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
12920:         $resulttext = &mt('Changes made:').'<ul>';
12921:         my (%shownprivs,@levelorder);
12922:         @levelorder = ('c','d','s');
12923:         if ((keys(%changes)) && ($putresult eq 'ok')) {
12924:             foreach my $item (sort(keys(%changes))) {
12925:                 if ($item eq 'submitbugs') {
12926:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
12927:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
12928:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
12929:                 } elsif ($item eq 'customrole') {
12930:                     if (ref($changes{'customrole'}) eq 'HASH') {
12931:                         my @keyorder = ('order','desc','access','status','exc','inc');
12932:                         my %keytext = &Apache::lonlocal::texthash(
12933:                                                                    order  => 'Order',
12934:                                                                    desc   => 'Role description',
12935:                                                                    access => 'Role usage',
12936:                                                                    status => 'Allowed institutional types',
12937:                                                                    exc    => 'Allowed personnel',
12938:                                                                    inc    => 'Disallowed personnel',
12939:                         );
12940:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
12941:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
12942:                                 if ($role eq $newrole) {
12943:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
12944:                                                               $role).'<ul>';
12945:                                 } else {
12946:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
12947:                                                               $role).'<ul>';
12948:                                 }
12949:                                 foreach my $key (@keyorder) {
12950:                                     if ($changes{'customrole'}{$role}{$key}) {
12951:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
12952:                                                                   $keytext{$key},$newsettings{$role}{$key}).
12953:                                                        '</li>';
12954:                                     }
12955:                                 }
12956:                                 if (ref($changedprivs{$role}) eq 'HASH') {
12957:                                     $shownprivs{$role} = 1;
12958:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
12959:                                     foreach my $level (@levelorder) {
12960:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
12961:                                             next if ($item eq '');
12962:                                             my ($priv) = split(/\&/,$item,2);
12963:                                             if (&Apache::lonnet::plaintext($priv)) {
12964:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
12965:                                                 unless ($level eq 'c') {
12966:                                                     $resulttext .= ' ('.$lt{$level}.')';
12967:                                                 }
12968:                                                 $resulttext .= '</li>';
12969:                                             }
12970:                                         }
12971:                                     }
12972:                                     $resulttext .= '</ul>';
12973:                                 }
12974:                                 $resulttext .= '</ul></li>';
12975:                             }
12976:                         }
12977:                     }
12978:                 }
12979:             }
12980:         }
12981:         if (keys(%changedprivs)) {
12982:             foreach my $role (sort(keys(%changedprivs))) {
12983:                 unless ($shownprivs{$role}) {
12984:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
12985:                                               $role).'<ul>'.
12986:                                    '<li>'.&mt('Privileges set to :').'<ul>';
12987:                     foreach my $level (@levelorder) {
12988:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
12989:                             next if ($item eq '');
12990:                             my ($priv) = split(/\&/,$item,2);
12991:                             if (&Apache::lonnet::plaintext($priv)) {
12992:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
12993:                                 unless ($level eq 'c') {
12994:                                     $resulttext .= ' ('.$lt{$level}.')';
12995:                                 }
12996:                                 $resulttext .= '</li>';
12997:                             }
12998:                         }
12999:                     }
13000:                     $resulttext .= '</ul></li></ul></li>';
13001:                 }
13002:             }
13003:         }
13004:         $resulttext .= '</ul>';
13005:     } else {
13006:         $resulttext = &mt('No changes made to help settings');
13007:     }
13008:     if ($errors) {
13009:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
13010:                                     $errors.'</ul>';
13011:     }
13012:     return $resulttext;
13013: }
13014: 
13015: sub modify_coursedefaults {
13016:     my ($dom,$lastactref,%domconfig) = @_;
13017:     my ($resulttext,$errors,%changes,%defaultshash);
13018:     my %defaultchecked = (
13019:                            'uselcmath'       => 'on',
13020:                            'usejsme'         => 'on'
13021:                          );
13022:     my @toggles = ('uselcmath','usejsme');
13023:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
13024:                    'uploadquota_community','uploadquota_textbook','mysqltables_official',
13025:                    'mysqltables_unofficial','mysqltables_community','mysqltables_textbook');
13026:     my @types = ('official','unofficial','community','textbook');
13027:     my %staticdefaults = (
13028:                            anonsurvey_threshold => 10,
13029:                            uploadquota          => 500,
13030:                            postsubmit           => 60,
13031:                            mysqltables          => 172800,
13032:                          );
13033: 
13034:     $defaultshash{'coursedefaults'} = {};
13035: 
13036:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
13037:         if ($domconfig{'coursedefaults'} eq '') {
13038:             $domconfig{'coursedefaults'} = {};
13039:         }
13040:     }
13041: 
13042:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
13043:         foreach my $item (@toggles) {
13044:             if ($defaultchecked{$item} eq 'on') {
13045:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
13046:                     ($env{'form.'.$item} eq '0')) {
13047:                     $changes{$item} = 1;
13048:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
13049:                     $changes{$item} = 1;
13050:                 }
13051:             } elsif ($defaultchecked{$item} eq 'off') {
13052:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
13053:                     ($env{'form.'.$item} eq '1')) {
13054:                     $changes{$item} = 1;
13055:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
13056:                     $changes{$item} = 1;
13057:                 }
13058:             }
13059:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
13060:         }
13061:         foreach my $item (@numbers) {
13062:             my ($currdef,$newdef);
13063:             $newdef = $env{'form.'.$item};
13064:             if ($item eq 'anonsurvey_threshold') {
13065:                 $currdef = $domconfig{'coursedefaults'}{$item};
13066:                 $newdef =~ s/\D//g;
13067:                 if ($newdef eq '' || $newdef < 1) {
13068:                     $newdef = 1;
13069:                 }
13070:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
13071:             } else {
13072:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
13073:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
13074:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
13075:                 }
13076:                 $newdef =~ s/[^\w.\-]//g;
13077:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
13078:             }
13079:             if ($currdef ne $newdef) {
13080:                 my $staticdef;
13081:                 if ($item eq 'anonsurvey_threshold') {
13082:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
13083:                         $changes{$item} = 1;
13084:                     }
13085:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
13086:                     my $setting = $1;
13087:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
13088:                         $changes{$setting} = 1;
13089:                     }
13090:                 }
13091:             }
13092:         }
13093:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
13094:         my @currclonecode;
13095:         if (ref($currclone) eq 'HASH') {
13096:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
13097:                 @currclonecode = @{$currclone->{'instcode'}};
13098:             }
13099:         }
13100:         my $newclone;
13101:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
13102:             $newclone = $env{'form.canclone'};
13103:         }
13104:         if ($newclone eq 'instcode') {
13105:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
13106:             my (%codedefaults,@code_order,@clonecode);
13107:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
13108:                                                     \@code_order);
13109:             foreach my $item (@code_order) {
13110:                 if (grep(/^\Q$item\E$/,@newcodes)) {
13111:                     push(@clonecode,$item);
13112:                 }
13113:             }
13114:             if (@clonecode) {
13115:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
13116:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
13117:                 if (@diffs) {
13118:                     $changes{'canclone'} = 1;
13119:                 }
13120:             } else {
13121:                 $newclone eq '';
13122:             }
13123:         } elsif ($newclone ne '') {
13124:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
13125:         }
13126:         if ($newclone ne $currclone) {
13127:             $changes{'canclone'} = 1;
13128:         }
13129:         my %credits;
13130:         foreach my $type (@types) {
13131:             unless ($type eq 'community') {
13132:                 $credits{$type} = $env{'form.'.$type.'_credits'};
13133:                 $credits{$type} =~ s/[^\d.]+//g;
13134:             }
13135:         }
13136:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
13137:             ($env{'form.coursecredits'} eq '1')) {
13138:             $changes{'coursecredits'} = 1;
13139:             foreach my $type (keys(%credits)) {
13140:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
13141:             }
13142:         } else {
13143:             if ($env{'form.coursecredits'} eq '1') {
13144:                 foreach my $type (@types) {
13145:                     unless ($type eq 'community') {
13146:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
13147:                             $changes{'coursecredits'} = 1;
13148:                         }
13149:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
13150:                     }
13151:                 }
13152:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
13153:                 foreach my $type (@types) {
13154:                     unless ($type eq 'community') {
13155:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
13156:                             $changes{'coursecredits'} = 1;
13157:                             last;
13158:                         }
13159:                     }
13160:                 }
13161:             }
13162:         }
13163:         if ($env{'form.postsubmit'} eq '1') {
13164:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
13165:             my %currtimeout;
13166:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13167:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
13168:                     $changes{'postsubmit'} = 1;
13169:                 }
13170:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
13171:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
13172:                 }
13173:             } else {
13174:                 $changes{'postsubmit'} = 1;
13175:             }
13176:             foreach my $type (@types) {
13177:                 my $timeout = $env{'form.'.$type.'_timeout'};
13178:                 $timeout =~ s/\D//g;
13179:                 if ($timeout == $staticdefaults{'postsubmit'}) {
13180:                     $timeout = '';
13181:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
13182:                     $timeout = '0';
13183:                 }
13184:                 unless ($timeout eq '') {
13185:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
13186:                 }
13187:                 if (exists($currtimeout{$type})) {
13188:                     if ($timeout ne $currtimeout{$type}) {
13189:                         $changes{'postsubmit'} = 1;
13190:                     }
13191:                 } elsif ($timeout ne '') {
13192:                     $changes{'postsubmit'} = 1;
13193:                 }
13194:             }
13195:         } else {
13196:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
13197:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13198:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
13199:                     $changes{'postsubmit'} = 1;
13200:                 }
13201:             } else {
13202:                 $changes{'postsubmit'} = 1;
13203:             }
13204:         }
13205:     }
13206:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
13207:                                              $dom);
13208:     if ($putresult eq 'ok') {
13209:         if (keys(%changes) > 0) {
13210:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13211:             if (($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
13212:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
13213:                 ($changes{'canclone'}) || ($changes{'mysqltables'})) {
13214:                 foreach my $item ('uselcmath','usejsme') {
13215:                     if ($changes{$item}) {
13216:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
13217:                     }
13218:                 }
13219:                 if ($changes{'coursecredits'}) {
13220:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
13221:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
13222:                             $domdefaults{$type.'credits'} =
13223:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
13224:                         }
13225:                     }
13226:                 }
13227:                 if ($changes{'postsubmit'}) {
13228:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13229:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
13230:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
13231:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
13232:                                 $domdefaults{$type.'postsubtimeout'} =
13233:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
13234:                             }
13235:                         }
13236:                     }
13237:                 }
13238:                 if ($changes{'uploadquota'}) {
13239:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
13240:                         foreach my $type (@types) {
13241:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
13242:                         }
13243:                     }
13244:                 }
13245:                 if ($changes{'canclone'}) {
13246:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
13247:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
13248:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
13249:                             if (@clonecodes) {
13250:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
13251:                             }
13252:                         }
13253:                     } else {
13254:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
13255:                     }
13256:                 }
13257:                 my $cachetime = 24*60*60;
13258:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13259:                 if (ref($lastactref) eq 'HASH') {
13260:                     $lastactref->{'domdefaults'} = 1;
13261:                 }
13262:             }
13263:             $resulttext = &mt('Changes made:').'<ul>';
13264:             foreach my $item (sort(keys(%changes))) {
13265:                 if ($item eq 'uselcmath') {
13266:                     if ($env{'form.'.$item} eq '1') {
13267:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
13268:                     } else {
13269:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
13270:                     }
13271:                 } elsif ($item eq 'usejsme') {
13272:                     if ($env{'form.'.$item} eq '1') {
13273:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
13274:                     } else {
13275:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
13276:                     }
13277:                 } elsif ($item eq 'anonsurvey_threshold') {
13278:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
13279:                 } elsif ($item eq 'uploadquota') {
13280:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
13281:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
13282:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
13283:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
13284:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
13285: 
13286:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
13287:                                        '</ul>'.
13288:                                        '</li>';
13289:                     } else {
13290:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
13291:                     }
13292:                 } elsif ($item eq 'mysqltables') {
13293:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
13294:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
13295:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
13296:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
13297:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
13298:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
13299:                                        '</ul>'.
13300:                                        '</li>';
13301:                     } else {
13302:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
13303:                     }
13304:                 } elsif ($item eq 'postsubmit') {
13305:                     if ($domdefaults{'postsubmit'} eq 'off') {
13306:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
13307:                     } else {
13308:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
13309:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13310:                             $resulttext .= &mt('durations:').'<ul>';
13311:                             foreach my $type (@types) {
13312:                                 $resulttext .= '<li>';
13313:                                 my $timeout;
13314:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
13315:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
13316:                                 }
13317:                                 my $display;
13318:                                 if ($timeout eq '0') {
13319:                                     $display = &mt('unlimited');
13320:                                 } elsif ($timeout eq '') {
13321:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
13322:                                 } else {
13323:                                     $display = &mt('[quant,_1,second]',$timeout);
13324:                                 }
13325:                                 if ($type eq 'community') {
13326:                                     $resulttext .= &mt('Communities');
13327:                                 } elsif ($type eq 'official') {
13328:                                     $resulttext .= &mt('Official courses');
13329:                                 } elsif ($type eq 'unofficial') {
13330:                                     $resulttext .= &mt('Unofficial courses');
13331:                                 } elsif ($type eq 'textbook') {
13332:                                     $resulttext .= &mt('Textbook courses');
13333:                                 }
13334:                                 $resulttext .= ' -- '.$display.'</li>';
13335:                             }
13336:                             $resulttext .= '</ul>';
13337:                         }
13338:                         $resulttext .= '</li>';
13339:                     }
13340:                 } elsif ($item eq 'coursecredits') {
13341:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
13342:                         if (($domdefaults{'officialcredits'} eq '') &&
13343:                             ($domdefaults{'unofficialcredits'} eq '') &&
13344:                             ($domdefaults{'textbookcredits'} eq '')) {
13345:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
13346:                         } else {
13347:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
13348:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
13349:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
13350:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
13351:                                            '</ul>'.
13352:                                            '</li>';
13353:                         }
13354:                     } else {
13355:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
13356:                     }
13357:                 } elsif ($item eq 'canclone') {
13358:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
13359:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
13360:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
13361:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
13362:                         }
13363:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
13364:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
13365:                     } else {
13366:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
13367:                     }
13368:                 }
13369:             }
13370:             $resulttext .= '</ul>';
13371:         } else {
13372:             $resulttext = &mt('No changes made to course defaults');
13373:         }
13374:     } else {
13375:         $resulttext = '<span class="LC_error">'.
13376:             &mt('An error occurred: [_1]',$putresult).'</span>';
13377:     }
13378:     return $resulttext;
13379: }
13380: 
13381: sub modify_selfenrollment {
13382:     my ($dom,$lastactref,%domconfig) = @_;
13383:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
13384:     my @types = ('official','unofficial','community','textbook');
13385:     my %titles = &tool_titles();
13386:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
13387:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
13388:     $ordered{'default'} = ['types','registered','approval','limit'];
13389: 
13390:     my (%roles,%shown,%toplevel);
13391:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
13392: 
13393:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
13394:         if ($domconfig{'selfenrollment'} eq '') {
13395:             $domconfig{'selfenrollment'} = {};
13396:         }
13397:     }
13398:     %toplevel = (
13399:                   admin      => 'Configuration Rights',
13400:                   default    => 'Default settings',
13401:                   validation => 'Validation of self-enrollment requests',
13402:                 );
13403:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
13404: 
13405:     if (ref($ordered{'admin'}) eq 'ARRAY') {
13406:         foreach my $item (@{$ordered{'admin'}}) {
13407:             foreach my $type (@types) {
13408:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
13409:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
13410:                 } else {
13411:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
13412:                 }
13413:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
13414:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
13415:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
13416:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
13417:                             push(@{$changes{'admin'}{$type}},$item);
13418:                         }
13419:                     } else {
13420:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
13421:                             push(@{$changes{'admin'}{$type}},$item);
13422:                         }
13423:                     }
13424:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
13425:                     push(@{$changes{'admin'}{$type}},$item);
13426:                 }
13427:             }
13428:         }
13429:     }
13430: 
13431:     foreach my $item (@{$ordered{'default'}}) {
13432:         foreach my $type (@types) {
13433:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
13434:             if ($item eq 'types') {
13435:                 unless (($value eq 'all') || ($value eq 'dom')) {
13436:                     $value = '';
13437:                 }
13438:             } elsif ($item eq 'registered') {
13439:                 unless ($value eq '1') {
13440:                     $value = 0;
13441:                 }
13442:             } elsif ($item eq 'approval') {
13443:                 unless ($value =~ /^[012]$/) {
13444:                     $value = 0;
13445:                 }
13446:             } else {
13447:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
13448:                     $value = 'none';
13449:                 }
13450:             }
13451:             $selfenrollhash{'default'}{$type}{$item} = $value;
13452:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
13453:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
13454:                     if ($selfenrollhash{'default'}{$type}{$item} ne
13455:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
13456:                          push(@{$changes{'default'}{$type}},$item);
13457:                     }
13458:                 } else {
13459:                     push(@{$changes{'default'}{$type}},$item);
13460:                 }
13461:             } else {
13462:                 push(@{$changes{'default'}{$type}},$item);
13463:             }
13464:             if ($item eq 'limit') {
13465:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
13466:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
13467:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
13468:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
13469:                     }
13470:                 } else {
13471:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
13472:                 }
13473:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
13474:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
13475:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
13476:                          push(@{$changes{'default'}{$type}},'cap');
13477:                     }
13478:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
13479:                     push(@{$changes{'default'}{$type}},'cap');
13480:                 }
13481:             }
13482:         }
13483:     }
13484: 
13485:     foreach my $item (@{$itemsref}) {
13486:         if ($item eq 'fields') {
13487:             my @changed;
13488:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
13489:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
13490:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
13491:             }
13492:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
13493:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
13494:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
13495:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
13496:                 } else {
13497:                     @changed = @{$selfenrollhash{'validation'}{$item}};
13498:                 }
13499:             } else {
13500:                 @changed = @{$selfenrollhash{'validation'}{$item}};
13501:             }
13502:             if (@changed) {
13503:                 if ($selfenrollhash{'validation'}{$item}) { 
13504:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
13505:                 } else {
13506:                     $changes{'validation'}{$item} = &mt('None');
13507:                 }
13508:             }
13509:         } else {
13510:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
13511:             if ($item eq 'markup') {
13512:                if ($env{'form.selfenroll_validation_'.$item}) {
13513:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
13514:                }
13515:             }
13516:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
13517:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
13518:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
13519:                 }
13520:             }
13521:         }
13522:     }
13523: 
13524:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
13525:                                              $dom);
13526:     if ($putresult eq 'ok') {
13527:         if (keys(%changes) > 0) {
13528:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13529:             $resulttext = &mt('Changes made:').'<ul>';
13530:             foreach my $key ('admin','default','validation') {
13531:                 if (ref($changes{$key}) eq 'HASH') {
13532:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
13533:                     if ($key eq 'validation') {
13534:                         foreach my $item (@{$itemsref}) {
13535:                             if (exists($changes{$key}{$item})) {
13536:                                 if ($item eq 'markup') {
13537:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
13538:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
13539:                                 } else {  
13540:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
13541:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
13542:                                 }
13543:                             }
13544:                         }
13545:                     } else {
13546:                         foreach my $type (@types) {
13547:                             if ($type eq 'community') {
13548:                                 $roles{'1'} = &mt('Community personnel');
13549:                             } else {
13550:                                 $roles{'1'} = &mt('Course personnel');
13551:                             }
13552:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
13553:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
13554:                                     if ($key eq 'admin') {
13555:                                         my @mgrdc = ();
13556:                                         if (ref($ordered{$key}) eq 'ARRAY') {
13557:                                             foreach my $item (@{$ordered{'admin'}}) {
13558:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
13559:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
13560:                                                         push(@mgrdc,$item);
13561:                                                     }
13562:                                                 }
13563:                                             }
13564:                                             if (@mgrdc) {
13565:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
13566:                                             } else {
13567:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
13568:                                             }
13569:                                         }
13570:                                     } else {
13571:                                         if (ref($ordered{$key}) eq 'ARRAY') {
13572:                                             foreach my $item (@{$ordered{$key}}) {
13573:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
13574:                                                     $domdefaults{$type.'selfenroll'.$item} =
13575:                                                         $selfenrollhash{$key}{$type}{$item};
13576:                                                 }
13577:                                             }
13578:                                         }
13579:                                     }
13580:                                 }
13581:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
13582:                                 foreach my $item (@{$ordered{$key}}) {
13583:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
13584:                                         $resulttext .= '<li>';
13585:                                         if ($key eq 'admin') {
13586:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
13587:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
13588:                                         } else {
13589:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
13590:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
13591:                                         }
13592:                                         $resulttext .= '</li>';
13593:                                     }
13594:                                 }
13595:                                 $resulttext .= '</ul></li>';
13596:                             }
13597:                         }
13598:                         $resulttext .= '</ul></li>'; 
13599:                     }
13600:                 }
13601:                 if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
13602:                     my $cachetime = 24*60*60;
13603:                     &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13604:                     if (ref($lastactref) eq 'HASH') {
13605:                         $lastactref->{'domdefaults'} = 1;
13606:                     }
13607:                 }
13608:             }
13609:             $resulttext .= '</ul>';
13610:         } else {
13611:             $resulttext = &mt('No changes made to self-enrollment settings');
13612:         }
13613:     } else {
13614:         $resulttext = '<span class="LC_error">'.
13615:             &mt('An error occurred: [_1]',$putresult).'</span>';
13616:     }
13617:     return $resulttext;
13618: }
13619: 
13620: sub modify_usersessions {
13621:     my ($dom,$lastactref,%domconfig) = @_;
13622:     my @hostingtypes = ('version','excludedomain','includedomain');
13623:     my @offloadtypes = ('primary','default');
13624:     my %types = (
13625:                   remote => \@hostingtypes,
13626:                   hosted => \@hostingtypes,
13627:                   spares => \@offloadtypes,
13628:                 );
13629:     my @prefixes = ('remote','hosted','spares');
13630:     my @lcversions = &Apache::lonnet::all_loncaparevs();
13631:     my (%by_ip,%by_location,@intdoms);
13632:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
13633:     my @locations = sort(keys(%by_location));
13634:     my (%defaultshash,%changes);
13635:     foreach my $prefix (@prefixes) {
13636:         $defaultshash{'usersessions'}{$prefix} = {};
13637:     }
13638:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13639:     my $resulttext;
13640:     my %iphost = &Apache::lonnet::get_iphost();
13641:     foreach my $prefix (@prefixes) {
13642:         next if ($prefix eq 'spares');
13643:         foreach my $type (@{$types{$prefix}}) {
13644:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
13645:             if ($type eq 'version') {
13646:                 my $value = $env{'form.'.$prefix.'_'.$type};
13647:                 my $okvalue;
13648:                 if ($value ne '') {
13649:                     if (grep(/^\Q$value\E$/,@lcversions)) {
13650:                         $okvalue = $value;
13651:                     }
13652:                 }
13653:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
13654:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
13655:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
13656:                             if ($inuse == 0) {
13657:                                 $changes{$prefix}{$type} = 1;
13658:                             } else {
13659:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
13660:                                     $changes{$prefix}{$type} = 1;
13661:                                 }
13662:                                 if ($okvalue ne '') {
13663:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13664:                                 } 
13665:                             }
13666:                         } else {
13667:                             if (($inuse == 1) && ($okvalue ne '')) {
13668:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13669:                                 $changes{$prefix}{$type} = 1;
13670:                             }
13671:                         }
13672:                     } else {
13673:                         if (($inuse == 1) && ($okvalue ne '')) {
13674:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13675:                             $changes{$prefix}{$type} = 1;
13676:                         }
13677:                     }
13678:                 } else {
13679:                     if (($inuse == 1) && ($okvalue ne '')) {
13680:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13681:                         $changes{$prefix}{$type} = 1;
13682:                     }
13683:                 }
13684:             } else {
13685:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
13686:                 my @okvals;
13687:                 foreach my $val (@vals) {
13688:                     if ($val =~ /:/) {
13689:                         my @items = split(/:/,$val);
13690:                         foreach my $item (@items) {
13691:                             if (ref($by_location{$item}) eq 'ARRAY') {
13692:                                 push(@okvals,$item);
13693:                             }
13694:                         }
13695:                     } else {
13696:                         if (ref($by_location{$val}) eq 'ARRAY') {
13697:                             push(@okvals,$val);
13698:                         }
13699:                     }
13700:                 }
13701:                 @okvals = sort(@okvals);
13702:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
13703:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
13704:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
13705:                             if ($inuse == 0) {
13706:                                 $changes{$prefix}{$type} = 1; 
13707:                             } else {
13708:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13709:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
13710:                                 if (@changed > 0) {
13711:                                     $changes{$prefix}{$type} = 1;
13712:                                 }
13713:                             }
13714:                         } else {
13715:                             if ($inuse == 1) {
13716:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13717:                                 $changes{$prefix}{$type} = 1;
13718:                             }
13719:                         } 
13720:                     } else {
13721:                         if ($inuse == 1) {
13722:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13723:                             $changes{$prefix}{$type} = 1;
13724:                         }
13725:                     }
13726:                 } else {
13727:                     if ($inuse == 1) {
13728:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13729:                         $changes{$prefix}{$type} = 1;
13730:                     }
13731:                 }
13732:             }
13733:         }
13734:     }
13735: 
13736:     my @alldoms = &Apache::lonnet::all_domains();
13737:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
13738:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
13739:     my $savespares;
13740: 
13741:     foreach my $lonhost (sort(keys(%servers))) {
13742:         my $serverhomeID =
13743:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
13744:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
13745:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
13746:         my %spareschg;
13747:         foreach my $type (@{$types{'spares'}}) {
13748:             my @okspares;
13749:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
13750:             foreach my $server (@checked) {
13751:                 if (&Apache::lonnet::hostname($server) ne '') {
13752:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
13753:                         unless (grep(/^\Q$server\E$/,@okspares)) {
13754:                             push(@okspares,$server);
13755:                         }
13756:                     }
13757:                 }
13758:             }
13759:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
13760:             my $newspare;
13761:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
13762:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
13763:                     $newspare = $new;
13764:                 }
13765:             }
13766:             my @spares;
13767:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
13768:                 @spares = sort(@okspares,$newspare);
13769:             } else {
13770:                 @spares = sort(@okspares);
13771:             }
13772:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
13773:             if (ref($spareid{$lonhost}) eq 'HASH') {
13774:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
13775:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
13776:                     if (@diffs > 0) {
13777:                         $spareschg{$type} = 1;
13778:                     }
13779:                 }
13780:             }
13781:         }
13782:         if (keys(%spareschg) > 0) {
13783:             $changes{'spares'}{$lonhost} = \%spareschg;
13784:         }
13785:     }
13786:     $defaultshash{'usersessions'}{'offloadnow'} = {};
13787:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
13788:     my @okoffload;
13789:     if (@offloadnow) {
13790:         foreach my $server (@offloadnow) {
13791:             if (&Apache::lonnet::hostname($server) ne '') {
13792:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
13793:                     push(@okoffload,$server);
13794:                 }
13795:             }
13796:         }
13797:         if (@okoffload) {
13798:             foreach my $lonhost (@okoffload) {
13799:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
13800:             }
13801:         }
13802:     }
13803:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
13804:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
13805:             if (ref($changes{'spares'}) eq 'HASH') {
13806:                 if (keys(%{$changes{'spares'}}) > 0) {
13807:                     $savespares = 1;
13808:                 }
13809:             }
13810:         } else {
13811:             $savespares = 1;
13812:         }
13813:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
13814:             foreach my $lonhost (keys(%{$domconfig{'usersessions'}{'offloadnow'}})) {
13815:                 unless ($defaultshash{'usersessions'}{'offloadnow'}{$lonhost}) {
13816:                     $changes{'offloadnow'} = 1;
13817:                     last;
13818:                 }
13819:             }
13820:             unless ($changes{'offloadnow'}) {
13821:                 foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{'offloadnow'}})) {
13822:                     unless ($domconfig{'usersessions'}{'offloadnow'}{$lonhost}) {
13823:                         $changes{'offloadnow'} = 1;
13824:                         last;
13825:                     }
13826:                 }
13827:             }
13828:         } elsif (@okoffload) {
13829:             $changes{'offloadnow'} = 1;
13830:         }
13831:     } elsif (@okoffload) {
13832:         $changes{'offloadnow'} = 1;
13833:     }
13834:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
13835:     if ((keys(%changes) > 0) || ($savespares)) {
13836:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
13837:                                                  $dom);
13838:         if ($putresult eq 'ok') {
13839:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
13840:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
13841:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
13842:                 }
13843:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
13844:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
13845:                 }
13846:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
13847:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
13848:                 }
13849:             }
13850:             my $cachetime = 24*60*60;
13851:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13852:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
13853:             if (ref($lastactref) eq 'HASH') {
13854:                 $lastactref->{'domdefaults'} = 1;
13855:                 $lastactref->{'usersessions'} = 1;
13856:             }
13857:             if (keys(%changes) > 0) {
13858:                 my %lt = &usersession_titles();
13859:                 $resulttext = &mt('Changes made:').'<ul>';
13860:                 foreach my $prefix (@prefixes) {
13861:                     if (ref($changes{$prefix}) eq 'HASH') {
13862:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
13863:                         if ($prefix eq 'spares') {
13864:                             if (ref($changes{$prefix}) eq 'HASH') {
13865:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
13866:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
13867:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
13868:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
13869:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
13870:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
13871:                                         foreach my $type (@{$types{$prefix}}) {
13872:                                             if ($changes{$prefix}{$lonhost}{$type}) {
13873:                                                 my $offloadto = &mt('None');
13874:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
13875:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
13876:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
13877:                                                     }
13878:                                                 }
13879:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
13880:                                             }
13881:                                         }
13882:                                     }
13883:                                     $resulttext .= '</li>';
13884:                                 }
13885:                             }
13886:                         } else {
13887:                             foreach my $type (@{$types{$prefix}}) {
13888:                                 if (defined($changes{$prefix}{$type})) {
13889:                                     my $newvalue;
13890:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
13891:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
13892:                                             if ($type eq 'version') {
13893:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
13894:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
13895:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
13896:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
13897:                                                 }
13898:                                             }
13899:                                         }
13900:                                     }
13901:                                     if ($newvalue eq '') {
13902:                                         if ($type eq 'version') {
13903:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
13904:                                         } else {
13905:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
13906:                                         }
13907:                                     } else {
13908:                                         if ($type eq 'version') {
13909:                                             $newvalue .= ' '.&mt('(or later)'); 
13910:                                         }
13911:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
13912:                                     }
13913:                                 }
13914:                             }
13915:                         }
13916:                         $resulttext .= '</ul>';
13917:                     }
13918:                 }
13919:                 if ($changes{'offloadnow'}) {
13920:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
13921:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
13922:                             $resulttext .= '<li>'.&mt('Switch active users on next access, for server(s):').'<ul>';
13923:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
13924:                                 $resulttext .= '<li>'.$lonhost.'</li>';
13925:                             }
13926:                             $resulttext .= '</ul>';
13927:                         } else {
13928:                             $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.');
13929:                         }
13930:                     } else {
13931:                         $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.').'</li>';
13932:                     }
13933:                 }
13934:                 $resulttext .= '</ul>';
13935:             } else {
13936:                 $resulttext = $nochgmsg;
13937:             }
13938:         } else {
13939:             $resulttext = '<span class="LC_error">'.
13940:                           &mt('An error occurred: [_1]',$putresult).'</span>';
13941:         }
13942:     } else {
13943:         $resulttext = $nochgmsg;
13944:     }
13945:     return $resulttext;
13946: }
13947: 
13948: sub modify_loadbalancing {
13949:     my ($dom,%domconfig) = @_;
13950:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
13951:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
13952:     my ($othertitle,$usertypes,$types) =
13953:         &Apache::loncommon::sorted_inst_types($dom);
13954:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
13955:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
13956:     my @sparestypes = ('primary','default');
13957:     my %typetitles = &sparestype_titles();
13958:     my $resulttext;
13959:     my (%currbalancer,%currtargets,%currrules,%existing);
13960:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
13961:         %existing = %{$domconfig{'loadbalancing'}};
13962:     }
13963:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
13964:                               \%currtargets,\%currrules);
13965:     my ($saveloadbalancing,%defaultshash,%changes);
13966:     my ($alltypes,$othertypes,$titles) =
13967:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
13968:     my %ruletitles = &offloadtype_text();
13969:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
13970:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
13971:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
13972:         if ($balancer eq '') {
13973:             next;
13974:         }
13975:         if (!exists($servers{$balancer})) {
13976:             if (exists($currbalancer{$balancer})) {
13977:                 push(@{$changes{'delete'}},$balancer);
13978:             }
13979:             next;
13980:         }
13981:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
13982:             push(@{$changes{'delete'}},$balancer);
13983:             next;
13984:         }
13985:         if (!exists($currbalancer{$balancer})) {
13986:             push(@{$changes{'add'}},$balancer);
13987:         }
13988:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
13989:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
13990:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
13991:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
13992:             $saveloadbalancing = 1;
13993:         }
13994:         foreach my $sparetype (@sparestypes) {
13995:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
13996:             my @offloadto;
13997:             foreach my $target (@targets) {
13998:                 if (($servers{$target}) && ($target ne $balancer)) {
13999:                     if ($sparetype eq 'default') {
14000:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
14001:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
14002:                         }
14003:                     }
14004:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
14005:                         push(@offloadto,$target);
14006:                     }
14007:                 }
14008:             }
14009:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
14010:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
14011:                     push(@offloadto,$balancer);
14012:                 }
14013:             }
14014:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
14015:         }
14016:         if (ref($currtargets{$balancer}) eq 'HASH') {
14017:             foreach my $sparetype (@sparestypes) {
14018:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
14019:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
14020:                     if (@targetdiffs > 0) {
14021:                         $changes{'curr'}{$balancer}{'targets'} = 1;
14022:                     }
14023:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14024:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14025:                         $changes{'curr'}{$balancer}{'targets'} = 1;
14026:                     }
14027:                 }
14028:             }
14029:         } else {
14030:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
14031:                 foreach my $sparetype (@sparestypes) {
14032:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14033:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14034:                             $changes{'curr'}{$balancer}{'targets'} = 1;
14035:                         }
14036:                     }
14037:                 }
14038:             }
14039:         }
14040:         my $ishomedom;
14041:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
14042:             $ishomedom = 1;
14043:         }
14044:         if (ref($alltypes) eq 'ARRAY') {
14045:             foreach my $type (@{$alltypes}) {
14046:                 my $rule;
14047:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
14048:                          (!$ishomedom)) {
14049:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
14050:                 }
14051:                 if ($rule eq 'specific') {
14052:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
14053:                     if (exists($servers{$specifiedhost})) {
14054:                         $rule = $specifiedhost;
14055:                     }
14056:                 }
14057:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
14058:                 if (ref($currrules{$balancer}) eq 'HASH') {
14059:                     if ($rule ne $currrules{$balancer}{$type}) {
14060:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
14061:                     }
14062:                 } elsif ($rule ne '') {
14063:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
14064:                 }
14065:             }
14066:         }
14067:     }
14068:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
14069:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
14070:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
14071:             $defaultshash{'loadbalancing'} = {};
14072:         }
14073:         my $putresult = &Apache::lonnet::put_dom('configuration',
14074:                                                  \%defaultshash,$dom);
14075:         if ($putresult eq 'ok') {
14076:             if (keys(%changes) > 0) {
14077:                 my %toupdate;
14078:                 if (ref($changes{'delete'}) eq 'ARRAY') {
14079:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
14080:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
14081:                         $toupdate{$balancer} = 1;
14082:                     }
14083:                 }
14084:                 if (ref($changes{'add'}) eq 'ARRAY') {
14085:                     foreach my $balancer (sort(@{$changes{'add'}})) {
14086:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
14087:                         $toupdate{$balancer} = 1;
14088:                     }
14089:                 }
14090:                 if (ref($changes{'curr'}) eq 'HASH') {
14091:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
14092:                         $toupdate{$balancer} = 1;
14093:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
14094:                             if ($changes{'curr'}{$balancer}{'targets'}) {
14095:                                 my %offloadstr;
14096:                                 foreach my $sparetype (@sparestypes) {
14097:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14098:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14099:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
14100:                                         }
14101:                                     }
14102:                                 }
14103:                                 if (keys(%offloadstr) == 0) {
14104:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
14105:                                 } else {
14106:                                     my $showoffload;
14107:                                     foreach my $sparetype (@sparestypes) {
14108:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
14109:                                         if (defined($offloadstr{$sparetype})) {
14110:                                             $showoffload .= $offloadstr{$sparetype};
14111:                                         } else {
14112:                                             $showoffload .= &mt('None');
14113:                                         }
14114:                                         $showoffload .= ('&nbsp;'x3);
14115:                                     }
14116:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
14117:                                 }
14118:                             }
14119:                         }
14120:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
14121:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
14122:                                 foreach my $type (@{$alltypes}) {
14123:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
14124:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
14125:                                         my $balancetext;
14126:                                         if ($rule eq '') {
14127:                                             $balancetext =  $ruletitles{'default'};
14128:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
14129:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
14130:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
14131:                                                 foreach my $sparetype (@sparestypes) {
14132:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14133:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
14134:                                                     }
14135:                                                 }
14136:                                                 foreach my $item (@{$alltypes}) {
14137:                                                     next if ($item =~  /^_LC_ipchange/);
14138:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
14139:                                                     if ($hasrule eq 'homeserver') {
14140:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
14141:                                                     } else {
14142:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
14143:                                                             if ($servers{$hasrule}) {
14144:                                                                 $toupdate{$hasrule} = 1;
14145:                                                             }
14146:                                                         }
14147:                                                     }
14148:                                                 }
14149:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
14150:                                                     $balancetext =  $ruletitles{$rule};
14151:                                                 } else {
14152:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
14153:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
14154:                                                     if ($receiver) {
14155:                                                         $toupdate{$receiver};
14156:                                                     }
14157:                                                 }
14158:                                             } else {
14159:                                                 $balancetext =  $ruletitles{$rule};
14160:                                             }
14161:                                         } else {
14162:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
14163:                                         }
14164:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
14165:                                     }
14166:                                 }
14167:                             }
14168:                         }
14169:                         if (keys(%toupdate)) {
14170:                             my %thismachine;
14171:                             my $updatedhere;
14172:                             my $cachetime = 60*60*24;
14173:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
14174:                             foreach my $lonhost (keys(%toupdate)) {
14175:                                 if ($thismachine{$lonhost}) {
14176:                                     unless ($updatedhere) {
14177:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
14178:                                                                       $defaultshash{'loadbalancing'},
14179:                                                                       $cachetime);
14180:                                         $updatedhere = 1;
14181:                                     }
14182:                                 } else {
14183:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
14184:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
14185:                                 }
14186:                             }
14187:                         }
14188:                     }
14189:                 }
14190:                 if ($resulttext ne '') {
14191:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
14192:                 } else {
14193:                     $resulttext = $nochgmsg;
14194:                 }
14195:             } else {
14196:                 $resulttext = $nochgmsg;
14197:             }
14198:         } else {
14199:             $resulttext = '<span class="LC_error">'.
14200:                           &mt('An error occurred: [_1]',$putresult).'</span>';
14201:         }
14202:     } else {
14203:         $resulttext = $nochgmsg;
14204:     }
14205:     return $resulttext;
14206: }
14207: 
14208: sub recurse_check {
14209:     my ($chkcats,$categories,$depth,$name) = @_;
14210:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
14211:         my $chg = 0;
14212:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
14213:             my $category = $chkcats->[$depth]{$name}[$j];
14214:             my $item;
14215:             if ($category eq '') {
14216:                 $chg ++;
14217:             } else {
14218:                 my $deeper = $depth + 1;
14219:                 $item = &escape($category).':'.&escape($name).':'.$depth;
14220:                 if ($chg) {
14221:                     $categories->{$item} -= $chg;
14222:                 }
14223:                 &recurse_check($chkcats,$categories,$deeper,$category);
14224:                 $deeper --;
14225:             }
14226:         }
14227:     }
14228:     return;
14229: }
14230: 
14231: sub recurse_cat_deletes {
14232:     my ($item,$coursecategories,$deletions) = @_;
14233:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
14234:     my $subdepth = $depth + 1;
14235:     if (ref($coursecategories) eq 'HASH') {
14236:         foreach my $subitem (keys(%{$coursecategories})) {
14237:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
14238:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
14239:                 delete($coursecategories->{$subitem});
14240:                 $deletions->{$subitem} = 1;
14241:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
14242:             }
14243:         }
14244:     }
14245:     return;
14246: }
14247: 
14248: sub active_dc_picker {
14249:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
14250:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
14251:     my @domcoord = keys(%domcoords);
14252:     if (keys(%currhash)) {
14253:         foreach my $dc (keys(%currhash)) {
14254:             unless (exists($domcoords{$dc})) {
14255:                 push(@domcoord,$dc);
14256:             }
14257:         }
14258:     }
14259:     @domcoord = sort(@domcoord);
14260:     my $numdcs = scalar(@domcoord);
14261:     my $rows = 0;
14262:     my $table;
14263:     if ($numdcs > 1) {
14264:         $table = '<table>';
14265:         for (my $i=0; $i<@domcoord; $i++) {
14266:             my $rem = $i%($numinrow);
14267:             if ($rem == 0) {
14268:                 if ($i > 0) {
14269:                     $table .= '</tr>';
14270:                 }
14271:                 $table .= '<tr>';
14272:                 $rows ++;
14273:             }
14274:             my $check = '';
14275:             if ($inputtype eq 'radio') {
14276:                 if (keys(%currhash) == 0) {
14277:                     if (!$i) {
14278:                         $check = ' checked="checked"';
14279:                     }
14280:                 } elsif (exists($currhash{$domcoord[$i]})) {
14281:                     $check = ' checked="checked"';
14282:                 }
14283:             } else {
14284:                 if (exists($currhash{$domcoord[$i]})) {
14285:                     $check = ' checked="checked"';
14286:                 }
14287:             }
14288:             if ($i == @domcoord - 1) {
14289:                 my $colsleft = $numinrow - $rem;
14290:                 if ($colsleft > 1) {
14291:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
14292:                 } else {
14293:                     $table .= '<td class="LC_left_item">';
14294:                 }
14295:             } else {
14296:                 $table .= '<td class="LC_left_item">';
14297:             }
14298:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
14299:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
14300:             $table .= '<span class="LC_nobreak"><label>'.
14301:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
14302:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
14303:             if ($user ne $dcname.':'.$dcdom) {
14304:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
14305:             }
14306:             $table .= '</label></span></td>';
14307:         }
14308:         $table .= '</tr></table>';
14309:     } elsif ($numdcs == 1) {
14310:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
14311:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
14312:         if ($inputtype eq 'radio') {
14313:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
14314:             if ($user ne $dcname.':'.$dcdom) {
14315:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
14316:             }
14317:         } else {
14318:             my $check;
14319:             if (exists($currhash{$domcoord[0]})) {
14320:                 $check = ' checked="checked"';
14321:             }
14322:             $table = '<span class="LC_nobreak"><label>'.
14323:                      '<input type="checkbox" name="'.$name.'" '.
14324:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
14325:             if ($user ne $dcname.':'.$dcdom) {
14326:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
14327:             }
14328:             $table .= '</label></span>';
14329:             $rows ++;
14330:         }
14331:     }
14332:     return ($numdcs,$table,$rows);
14333: }
14334: 
14335: sub usersession_titles {
14336:     return &Apache::lonlocal::texthash(
14337:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
14338:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
14339:                spares => 'Servers offloaded to, when busy',
14340:                version => 'LON-CAPA version requirement',
14341:                excludedomain => 'Allow all, but exclude specific domains',
14342:                includedomain => 'Deny all, but include specific domains',
14343:                primary => 'Primary (checked first)',
14344:                default => 'Default',
14345:            );
14346: }
14347: 
14348: sub id_for_thisdom {
14349:     my (%servers) = @_;
14350:     my %altids;
14351:     foreach my $server (keys(%servers)) {
14352:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
14353:         if ($serverhome ne $server) {
14354:             $altids{$serverhome} = $server;
14355:         }
14356:     }
14357:     return %altids;
14358: }
14359: 
14360: sub count_servers {
14361:     my ($currbalancer,%servers) = @_;
14362:     my (@spares,$numspares);
14363:     foreach my $lonhost (sort(keys(%servers))) {
14364:         next if ($currbalancer eq $lonhost);
14365:         push(@spares,$lonhost);
14366:     }
14367:     if ($currbalancer) {
14368:         $numspares = scalar(@spares);
14369:     } else {
14370:         $numspares = scalar(@spares) - 1;
14371:     }
14372:     return ($numspares,@spares);
14373: }
14374: 
14375: sub lonbalance_targets_js {
14376:     my ($dom,$types,$servers,$settings) = @_;
14377:     my $select = &mt('Select');
14378:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
14379:     if (ref($servers) eq 'HASH') {
14380:         $alltargets = join("','",sort(keys(%{$servers})));
14381:         my @homedoms;
14382:         foreach my $server (sort(keys(%{$servers}))) {
14383:             if (&Apache::lonnet::host_domain($server) eq $dom) {
14384:                 push(@homedoms,'1');
14385:             } else {
14386:                 push(@homedoms,'0');
14387:             }
14388:         }
14389:         $allishome = join("','",@homedoms);
14390:     }
14391:     if (ref($types) eq 'ARRAY') {
14392:         if (@{$types} > 0) {
14393:             @alltypes = @{$types};
14394:         }
14395:     }
14396:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
14397:     $allinsttypes = join("','",@alltypes);
14398:     my (%currbalancer,%currtargets,%currrules,%existing);
14399:     if (ref($settings) eq 'HASH') {
14400:         %existing = %{$settings};
14401:     }
14402:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
14403:                               \%currtargets,\%currrules);
14404:     my $balancers = join("','",sort(keys(%currbalancer)));
14405:     return <<"END";
14406: 
14407: <script type="text/javascript">
14408: // <![CDATA[
14409: 
14410: currBalancers = new Array('$balancers');
14411: 
14412: function toggleTargets(balnum) {
14413:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
14414:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
14415:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
14416:     var prevbalancer = prevhostitem.value;
14417:     var baltotal = document.getElementById('loadbalancing_total').value;
14418:     prevhostitem.value = balancer;
14419:     if (prevbalancer != '') {
14420:         var prevIdx = currBalancers.indexOf(prevbalancer);
14421:         if (prevIdx != -1) {
14422:             currBalancers.splice(prevIdx,1);
14423:         }
14424:     }
14425:     if (balancer == '') {
14426:         hideSpares(balnum);
14427:     } else {
14428:         var currIdx = currBalancers.indexOf(balancer);
14429:         if (currIdx == -1) {
14430:             currBalancers.push(balancer);
14431:         }
14432:         var homedoms = new Array('$allishome');
14433:         var ishomedom = homedoms[lonhostitem.selectedIndex];
14434:         showSpares(balancer,ishomedom,balnum);
14435:     }
14436:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
14437:     return;
14438: }
14439: 
14440: function showSpares(balancer,ishomedom,balnum) {
14441:     var alltargets = new Array('$alltargets');
14442:     var insttypes = new Array('$allinsttypes');
14443:     var offloadtypes = new Array('primary','default');
14444: 
14445:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
14446:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
14447:  
14448:     for (var i=0; i<offloadtypes.length; i++) {
14449:         var count = 0;
14450:         for (var j=0; j<alltargets.length; j++) {
14451:             if (alltargets[j] != balancer) {
14452:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
14453:                 item.value = alltargets[j];
14454:                 item.style.textAlign='left';
14455:                 item.style.textFace='normal';
14456:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
14457:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
14458:                     item.disabled = '';
14459:                 } else {
14460:                     item.disabled = 'disabled';
14461:                     item.checked = false;
14462:                 }
14463:                 count ++;
14464:             }
14465:         }
14466:     }
14467:     for (var k=0; k<insttypes.length; k++) {
14468:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
14469:             if (ishomedom == 1) {
14470:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
14471:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
14472:             } else {
14473:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
14474:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
14475:             }
14476:         } else {
14477:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
14478:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
14479:         }
14480:         if ((insttypes[k] != '_LC_external') && 
14481:             ((insttypes[k] != '_LC_internetdom') ||
14482:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
14483:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
14484:             item.options.length = 0;
14485:             item.options[0] = new Option("","",true,true);
14486:             var idx = 0;
14487:             for (var m=0; m<alltargets.length; m++) {
14488:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
14489:                     idx ++;
14490:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
14491:                 }
14492:             }
14493:         }
14494:     }
14495:     return;
14496: }
14497: 
14498: function hideSpares(balnum) {
14499:     var alltargets = new Array('$alltargets');
14500:     var insttypes = new Array('$allinsttypes');
14501:     var offloadtypes = new Array('primary','default');
14502: 
14503:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
14504:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
14505: 
14506:     var total = alltargets.length - 1;
14507:     for (var i=0; i<offloadtypes; i++) {
14508:         for (var j=0; j<total; j++) {
14509:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
14510:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
14511:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
14512:         }
14513:     }
14514:     for (var k=0; k<insttypes.length; k++) {
14515:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
14516:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
14517:         if (insttypes[k] != '_LC_external') {
14518:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
14519:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
14520:         }
14521:     }
14522:     return;
14523: }
14524: 
14525: function checkOffloads(item,balnum,type) {
14526:     var alltargets = new Array('$alltargets');
14527:     var offloadtypes = new Array('primary','default');
14528:     if (item.checked) {
14529:         var total = alltargets.length - 1;
14530:         var other;
14531:         if (type == offloadtypes[0]) {
14532:             other = offloadtypes[1];
14533:         } else {
14534:             other = offloadtypes[0];
14535:         }
14536:         for (var i=0; i<total; i++) {
14537:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
14538:             if (server == item.value) {
14539:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
14540:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
14541:                 }
14542:             }
14543:         }
14544:     }
14545:     return;
14546: }
14547: 
14548: function singleServerToggle(balnum,type) {
14549:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
14550:     if (offloadtoSelIdx == 0) {
14551:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
14552:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
14553: 
14554:     } else {
14555:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
14556:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
14557:     }
14558:     return;
14559: }
14560: 
14561: function balanceruleChange(formname,balnum,type) {
14562:     if (type == '_LC_external') {
14563:         return;
14564:     }
14565:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
14566:     for (var i=0; i<typesRules.length; i++) {
14567:         if (formname.elements[typesRules[i]].checked) {
14568:             if (formname.elements[typesRules[i]].value != 'specific') {
14569:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
14570:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
14571:             } else {
14572:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
14573:             }
14574:         }
14575:     }
14576:     return;
14577: }
14578: 
14579: function balancerDeleteChange(balnum) {
14580:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
14581:     var baltotal = document.getElementById('loadbalancing_total').value;
14582:     var addtarget;
14583:     var removetarget;
14584:     var action = 'delete';
14585:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
14586:         var lonhost = hostitem.value;
14587:         var currIdx = currBalancers.indexOf(lonhost);
14588:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
14589:             if (currIdx != -1) {
14590:                 currBalancers.splice(currIdx,1);
14591:             }
14592:             addtarget = lonhost;
14593:         } else {
14594:             if (currIdx == -1) {
14595:                 currBalancers.push(lonhost);
14596:             }
14597:             removetarget = lonhost;
14598:             action = 'undelete';
14599:         }
14600:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
14601:     }
14602:     return;
14603: }
14604: 
14605: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
14606:     if (baltotal > 1) {
14607:         var offloadtypes = new Array('primary','default');
14608:         var alltargets = new Array('$alltargets');
14609:         var insttypes = new Array('$allinsttypes');
14610:         for (var i=0; i<baltotal; i++) {
14611:             if (i != balnum) {
14612:                 for (var j=0; j<offloadtypes.length; j++) {
14613:                     var total = alltargets.length - 1;
14614:                     for (var k=0; k<total; k++) {
14615:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
14616:                         var server = serveritem.value;
14617:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
14618:                             if (server == addtarget) {
14619:                                 serveritem.disabled = '';
14620:                             }
14621:                         }
14622:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
14623:                             if (server == removetarget) {
14624:                                 serveritem.disabled = 'disabled';
14625:                                 serveritem.checked = false;
14626:                             }
14627:                         }
14628:                     }
14629:                 }
14630:                 for (var j=0; j<insttypes.length; j++) {
14631:                     if (insttypes[j] != '_LC_external') {
14632:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
14633:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
14634:                             var currSel = singleserver.selectedIndex;
14635:                             var currVal = singleserver.options[currSel].value;
14636:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
14637:                                 var numoptions = singleserver.options.length;
14638:                                 var needsnew = 1;
14639:                                 for (var k=0; k<numoptions; k++) {
14640:                                     if (singleserver.options[k] == addtarget) {
14641:                                         needsnew = 0;
14642:                                         break;
14643:                                     }
14644:                                 }
14645:                                 if (needsnew == 1) {
14646:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
14647:                                 }
14648:                             }
14649:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
14650:                                 singleserver.options.length = 0;
14651:                                 if ((currVal) && (currVal != removetarget)) {
14652:                                     singleserver.options[0] = new Option("","",false,false);
14653:                                 } else {
14654:                                     singleserver.options[0] = new Option("","",true,true);
14655:                                 }
14656:                                 var idx = 0;
14657:                                 for (var m=0; m<alltargets.length; m++) {
14658:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
14659:                                         idx ++;
14660:                                         if (currVal == alltargets[m]) {
14661:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
14662:                                         } else {
14663:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
14664:                                         }
14665:                                     }
14666:                                 }
14667:                             }
14668:                         }
14669:                     }
14670:                 }
14671:             }
14672:         }
14673:     }
14674:     return;
14675: }
14676: 
14677: // ]]>
14678: </script>
14679: 
14680: END
14681: }
14682: 
14683: sub new_spares_js {
14684:     my @sparestypes = ('primary','default');
14685:     my $types = join("','",@sparestypes);
14686:     my $select = &mt('Select');
14687:     return <<"END";
14688: 
14689: <script type="text/javascript">
14690: // <![CDATA[
14691: 
14692: function updateNewSpares(formname,lonhost) {
14693:     var types = new Array('$types');
14694:     var include = new Array();
14695:     var exclude = new Array();
14696:     for (var i=0; i<types.length; i++) {
14697:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
14698:         for (var j=0; j<spareboxes.length; j++) {
14699:             if (formname.elements[spareboxes[j]].checked) {
14700:                 exclude.push(formname.elements[spareboxes[j]].value);
14701:             } else {
14702:                 include.push(formname.elements[spareboxes[j]].value);
14703:             }
14704:         }
14705:     }
14706:     for (var i=0; i<types.length; i++) {
14707:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
14708:         var selIdx = newSpare.selectedIndex;
14709:         var currnew = newSpare.options[selIdx].value;
14710:         var okSpares = new Array();
14711:         for (var j=0; j<newSpare.options.length; j++) {
14712:             var possible = newSpare.options[j].value;
14713:             if (possible != '') {
14714:                 if (exclude.indexOf(possible) == -1) {
14715:                     okSpares.push(possible);
14716:                 } else {
14717:                     if (currnew == possible) {
14718:                         selIdx = 0;
14719:                     }
14720:                 }
14721:             }
14722:         }
14723:         for (var k=0; k<include.length; k++) {
14724:             if (okSpares.indexOf(include[k]) == -1) {
14725:                 okSpares.push(include[k]);
14726:             }
14727:         }
14728:         okSpares.sort();
14729:         newSpare.options.length = 0;
14730:         if (selIdx == 0) {
14731:             newSpare.options[0] = new Option("$select","",true,true);
14732:         } else {
14733:             newSpare.options[0] = new Option("$select","",false,false);
14734:         }
14735:         for (var m=0; m<okSpares.length; m++) {
14736:             var idx = m+1;
14737:             var selThis = 0;
14738:             if (selIdx != 0) {
14739:                 if (okSpares[m] == currnew) {
14740:                     selThis = 1;
14741:                 }
14742:             }
14743:             if (selThis == 1) {
14744:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
14745:             } else {
14746:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
14747:             }
14748:         }
14749:     }
14750:     return;
14751: }
14752: 
14753: function checkNewSpares(lonhost,type) {
14754:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
14755:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
14756:     if (chosen != '') { 
14757:         var othertype;
14758:         var othernewSpare;
14759:         if (type == 'primary') {
14760:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
14761:         }
14762:         if (type == 'default') {
14763:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
14764:         }
14765:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
14766:             othernewSpare.selectedIndex = 0;
14767:         }
14768:     }
14769:     return;
14770: }
14771: 
14772: // ]]>
14773: </script>
14774: 
14775: END
14776: 
14777: }
14778: 
14779: sub common_domprefs_js {
14780:     return <<"END";
14781: 
14782: <script type="text/javascript">
14783: // <![CDATA[
14784: 
14785: function getIndicesByName(formname,item) {
14786:     var group = new Array();
14787:     for (var i=0;i<formname.elements.length;i++) {
14788:         if (formname.elements[i].name == item) {
14789:             group.push(formname.elements[i].id);
14790:         }
14791:     }
14792:     return group;
14793: }
14794: 
14795: // ]]>
14796: </script>
14797: 
14798: END
14799: 
14800: }
14801: 
14802: sub recaptcha_js {
14803:     my %lt = &captcha_phrases();
14804:     return <<"END";
14805: 
14806: <script type="text/javascript">
14807: // <![CDATA[
14808: 
14809: function updateCaptcha(caller,context) {
14810:     var privitem;
14811:     var pubitem;
14812:     var privtext;
14813:     var pubtext;
14814:     var versionitem;
14815:     var versiontext;
14816:     if (document.getElementById(context+'_recaptchapub')) {
14817:         pubitem = document.getElementById(context+'_recaptchapub');
14818:     } else {
14819:         return;
14820:     }
14821:     if (document.getElementById(context+'_recaptchapriv')) {
14822:         privitem = document.getElementById(context+'_recaptchapriv');
14823:     } else {
14824:         return;
14825:     }
14826:     if (document.getElementById(context+'_recaptchapubtxt')) {
14827:         pubtext = document.getElementById(context+'_recaptchapubtxt');
14828:     } else {
14829:         return;
14830:     }
14831:     if (document.getElementById(context+'_recaptchaprivtxt')) {
14832:         privtext = document.getElementById(context+'_recaptchaprivtxt');
14833:     } else {
14834:         return;
14835:     }
14836:     if (document.getElementById(context+'_recaptchaversion')) {
14837:         versionitem = document.getElementById(context+'_recaptchaversion');
14838:     } else {
14839:         return;
14840:     }
14841:     if (document.getElementById(context+'_recaptchavertxt')) {
14842:         versiontext = document.getElementById(context+'_recaptchavertxt');
14843:     } else {
14844:         return;
14845:     }
14846:     if (caller.checked) {
14847:         if (caller.value == 'recaptcha') {
14848:             pubitem.type = 'text';
14849:             privitem.type = 'text';
14850:             pubitem.size = '40';
14851:             privitem.size = '40';
14852:             pubtext.innerHTML = "$lt{'pub'}";
14853:             privtext.innerHTML = "$lt{'priv'}";
14854:             versionitem.type = 'text';
14855:             versionitem.size = '3';
14856:             versiontext.innerHTML = "$lt{'ver'}";
14857:         } else {
14858:             pubitem.type = 'hidden';
14859:             privitem.type = 'hidden';
14860:             versionitem.type = 'hidden';
14861:             pubtext.innerHTML = '';
14862:             privtext.innerHTML = '';
14863:             versiontext.innerHTML = '';
14864:         }
14865:     }
14866:     return;
14867: }
14868: 
14869: // ]]>
14870: </script>
14871: 
14872: END
14873: 
14874: }
14875: 
14876: sub toggle_display_js {
14877:     return <<"END";
14878: 
14879: <script type="text/javascript">
14880: // <![CDATA[
14881: 
14882: function toggleDisplay(domForm,caller) {
14883:     if (document.getElementById(caller)) {
14884:         var divitem = document.getElementById(caller);
14885:         var optionsElement = domForm.coursecredits;
14886:         var checkval = 1;
14887:         var dispval = 'block';
14888:         if (caller == 'emailoptions') {
14889:             optionsElement = domForm.cancreate_email; 
14890:         }
14891:         if (caller == 'studentsubmission') {
14892:             optionsElement = domForm.postsubmit;
14893:         }
14894:         if (caller == 'cloneinstcode') {
14895:             optionsElement = domForm.canclone;
14896:             checkval = 'instcode';
14897:         }
14898:         if (optionsElement.length) {
14899:             var currval;
14900:             for (var i=0; i<optionsElement.length; i++) {
14901:                 if (optionsElement[i].checked) {
14902:                    currval = optionsElement[i].value;
14903:                 }
14904:             }
14905:             if (currval == checkval) {
14906:                 divitem.style.display = dispval;
14907:             } else {
14908:                 divitem.style.display = 'none';
14909:             }
14910:         }
14911:     }
14912:     return;
14913: }
14914: 
14915: // ]]>
14916: </script>
14917: 
14918: END
14919: 
14920: }
14921: 
14922: sub captcha_phrases {
14923:     return &Apache::lonlocal::texthash (
14924:                  priv => 'Private key',
14925:                  pub  => 'Public key',
14926:                  original  => 'original (CAPTCHA)',
14927:                  recaptcha => 'successor (ReCAPTCHA)',
14928:                  notused   => 'unused',
14929:                  ver => 'ReCAPTCHA version (1 or 2)',
14930:     );
14931: }
14932: 
14933: sub devalidate_remote_domconfs {
14934:     my ($dom,$cachekeys) = @_;
14935:     return unless (ref($cachekeys) eq 'HASH');
14936:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
14937:     my %thismachine;
14938:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
14939:     my @posscached = ('domainconfig','domdefaults','ltitools','usersessions','directorysrch');
14940:     if (keys(%servers)) {
14941:         foreach my $server (keys(%servers)) {
14942:             next if ($thismachine{$server});
14943:             my @cached;
14944:             foreach my $name (@posscached) {
14945:                 if ($cachekeys->{$name}) {
14946:                     push(@cached,&escape($name).':'.&escape($dom));
14947:                 }
14948:             }
14949:             if (@cached) {
14950:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
14951:             }
14952:         }
14953:     }
14954:     return;
14955: }
14956: 
14957: 1;

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