File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.235: download - view: text, annotated - select for diffs
Wed Apr 16 13:32:43 2014 UTC (10 years, 1 month ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Course requests for unofficial courses, textbook courses and communities
  can now include validation.
- Code used to generate validation items for self-enrollment configuration
  moved to &print_validation_rows() to facilitate re-use in course requests
  configuration.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.235 2014/04/16 13:32:43 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: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: #
   28: ###############################################################
   29: ##############################################################
   30: 
   31: =pod
   32: 
   33: =head1 NAME
   34: 
   35: Apache::domainprefs.pm
   36: 
   37: =head1 SYNOPSIS
   38: 
   39: Handles configuration of a LON-CAPA domain.  
   40: 
   41: This is part of the LearningOnline Network with CAPA project
   42: described at http://www.lon-capa.org.
   43: 
   44: 
   45: =head1 OVERVIEW
   46: 
   47: Each institution using LON-CAPA will typically have a single domain designated 
   48: for use by individuals affiliated with the institution.  Accordingly, each domain
   49: may define a default set of logos and a color scheme which can be used to "brand"
   50: the LON-CAPA instance. In addition, an institution will typically have a language
   51: and timezone which are used for the majority of courses.
   52: 
   53: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   54: host of other domain-wide settings which determine the types of functionality
   55: available to users and courses in the domain.
   56: 
   57: There is also a mechanism to configure cataloging of courses in the domain, and
   58: controls on the operation of automated processes which govern such things as
   59: roster updates, user directory updates and processing of course requests.
   60: 
   61: The domain coordination manual which is built dynamically on install/update of 
   62: LON-CAPA from the relevant help items provides more information about domain 
   63: configuration.
   64: 
   65: Most of the domain settings are stored in the configuration.db GDBM file which is
   66: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   67: where $dom is the domain.  The configuration.db stores settings in a number of 
   68: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   69: the domain as files (e.g., image files for logos etc., or plain text files for
   70: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   71: session hosted on the primary library server in the domain, as these files are 
   72: stored in author space belonging to a special $dom-domainconfig user.   
   73: 
   74: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   75: the current settings, and provides an interface to make modifications.
   76: 
   77: =head1 SUBROUTINES
   78: 
   79: =over
   80: 
   81: =item print_quotas()
   82: 
   83: Inputs: 4 
   84: 
   85: $dom,$settings,$rowtotal,$action.
   86: 
   87: $dom is the domain, $settings is a reference to a hash of current settings for
   88: the current context, $rowtotal is a reference to the scalar used to record the 
   89: number of rows displayed on the page, and $action is the context (quotas, 
   90: requestcourses or requestauthor).
   91: 
   92: The print_quotas routine was orginally created to display/store information
   93: about default quota sizes for portfolio spaces for the different types of 
   94: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   95: but is now also used to manage availability of user tools: 
   96: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   97: used by course owners to request creation of a course, and to display/store
   98: default quota sizes for Authoring Spaces.
   99: 
  100: Outputs: 1
  101: 
  102: $datatable  - HTML containing form elements which allow settings to be changed. 
  103: 
  104: In the case of course requests, radio buttons are displayed for each institutional
  105: affiliate type (and also default, and _LC_adv) for each of the course types 
  106: (official, unofficial, community, and textbook).  In each case the radio buttons 
  107: allow the selection of one of four values:
  108: 
  109: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  110: which have the following effects:
  111: 
  112: 0
  113: 
  114: =over
  115: 
  116: - course requests are not allowed for this course types/affiliation
  117: 
  118: =back
  119: 
  120: approval 
  121: 
  122: =over 
  123: 
  124: - course requests must be approved by a Doman Coordinator in the 
  125: course's domain
  126: 
  127: =back
  128: 
  129: validate 
  130: 
  131: =over
  132: 
  133: - an institutional validation (e.g., check requestor is instructor
  134: of record) needs to be passed before the course will be created.  The required
  135: validation is in localenroll.pm on the primary library server for the course 
  136: domain.
  137: 
  138: =back
  139: 
  140: autolimit 
  141: 
  142: =over
  143:  
  144: - course requests will be processed automatically up to a limit of
  145: N requests for the course type for the particular requestor.
  146: If N is undefined, there is no limit to the number of course requests
  147: which a course owner may submit and have processed automatically. 
  148: 
  149: =back
  150: 
  151: =item modify_quotas() 
  152: 
  153: =back
  154: 
  155: =cut
  156: 
  157: package Apache::domainprefs;
  158: 
  159: use strict;
  160: use Apache::Constants qw(:common :http);
  161: use Apache::lonnet;
  162: use Apache::loncommon();
  163: use Apache::lonhtmlcommon();
  164: use Apache::lonlocal;
  165: use Apache::lonmsg();
  166: use Apache::lonconfigsettings;
  167: use Apache::lonuserutils();
  168: use Apache::loncoursequeueadmin();
  169: use LONCAPA qw(:DEFAULT :match);
  170: use LONCAPA::Enrollment;
  171: use LONCAPA::lonauthcgi();
  172: use File::Copy;
  173: use Locale::Language;
  174: use DateTime::TimeZone;
  175: use DateTime::Locale;
  176: 
  177: my $registered_cleanup;
  178: my $modified_urls;
  179: 
  180: sub handler {
  181:     my $r=shift;
  182:     if ($r->header_only) {
  183:         &Apache::loncommon::content_type($r,'text/html');
  184:         $r->send_http_header;
  185:         return OK;
  186:     }
  187: 
  188:     my $context = 'domain';
  189:     my $dom = $env{'request.role.domain'};
  190:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  191:     if (&Apache::lonnet::allowed('mau',$dom)) {
  192:         &Apache::loncommon::content_type($r,'text/html');
  193:         $r->send_http_header;
  194:     } else {
  195:         $env{'user.error.msg'}=
  196:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  197:         return HTTP_NOT_ACCEPTABLE;
  198:     }
  199: 
  200:     $registered_cleanup=0;
  201:     @{$modified_urls}=();
  202: 
  203:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  204:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  205:                                             ['phase','actions']);
  206:     my $phase = 'pickactions';
  207:     if ( exists($env{'form.phase'}) ) {
  208:         $phase = $env{'form.phase'};
  209:     }
  210:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  211:     my %domconfig =
  212:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  213:                 'quotas','autoenroll','autoupdate','autocreate',
  214:                 'directorysrch','usercreation','usermodification',
  215:                 'contacts','defaults','scantron','coursecategories',
  216:                 'serverstatuses','requestcourses','helpsettings',
  217:                 'coursedefaults','usersessions','loadbalancing',
  218:                 'requestauthor','selfenrollment'],$dom);
  219:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  220:                        'autoupdate','autocreate','directorysrch','contacts',
  221:                        'usercreation','selfcreation','usermodification','scantron',
  222:                        'requestcourses','requestauthor','coursecategories',
  223:                        'serverstatuses','helpsettings',
  224:                        'coursedefaults','selfenrollment','usersessions');
  225:     my %existing;
  226:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  227:         %existing = %{$domconfig{'loadbalancing'}};
  228:     }
  229:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  230:         push(@prefs_order,'loadbalancing');
  231:     }
  232:     my %prefs = (
  233:         'rolecolors' =>
  234:                    { text => 'Default color schemes',
  235:                      help => 'Domain_Configuration_Color_Schemes',
  236:                      header => [{col1 => 'Student Settings',
  237:                                  col2 => '',},
  238:                                 {col1 => 'Coordinator Settings',
  239:                                  col2 => '',},
  240:                                 {col1 => 'Author Settings',
  241:                                  col2 => '',},
  242:                                 {col1 => 'Administrator Settings',
  243:                                  col2 => '',}],
  244:                       print => \&print_rolecolors,
  245:                       modify => \&modify_rolecolors,
  246:                     },
  247:         'login' =>
  248:                     { text => 'Log-in page options',
  249:                       help => 'Domain_Configuration_Login_Page',
  250:                       header => [{col1 => 'Log-in Page Items',
  251:                                   col2 => '',},
  252:                                  {col1 => 'Log-in Help',
  253:                                   col2 => 'Value'}],
  254:                       print => \&print_login,
  255:                       modify => \&modify_login,
  256:                     },
  257:         'defaults' => 
  258:                     { text => 'Default authentication/language/timezone/portal',
  259:                       help => 'Domain_Configuration_LangTZAuth',
  260:                       header => [{col1 => 'Setting',
  261:                                   col2 => 'Value'}],
  262:                       print => \&print_defaults,
  263:                       modify => \&modify_defaults,
  264:                     },
  265:         'quotas' => 
  266:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  267:                       help => 'Domain_Configuration_Quotas',
  268:                       header => [{col1 => 'User affiliation',
  269:                                   col2 => 'Available tools',
  270:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  271:                       print => \&print_quotas,
  272:                       modify => \&modify_quotas,
  273:                     },
  274:         'autoenroll' =>
  275:                    { text => 'Auto-enrollment settings',
  276:                      help => 'Domain_Configuration_Auto_Enrollment',
  277:                      header => [{col1 => 'Configuration setting',
  278:                                  col2 => 'Value(s)'}],
  279:                      print => \&print_autoenroll,
  280:                      modify => \&modify_autoenroll,
  281:                    },
  282:         'autoupdate' => 
  283:                    { text => 'Auto-update settings',
  284:                      help => 'Domain_Configuration_Auto_Updates',
  285:                      header => [{col1 => 'Setting',
  286:                                  col2 => 'Value',},
  287:                                 {col1 => 'Setting',
  288:                                  col2 => 'Affiliation'},
  289:                                 {col1 => 'User population',
  290:                                  col2 => 'Updatable user data'}],
  291:                      print => \&print_autoupdate,
  292:                      modify => \&modify_autoupdate,
  293:                   },
  294:         'autocreate' => 
  295:                   { text => 'Auto-course creation settings',
  296:                      help => 'Domain_Configuration_Auto_Creation',
  297:                      header => [{col1 => 'Configuration Setting',
  298:                                  col2 => 'Value',}],
  299:                      print => \&print_autocreate,
  300:                      modify => \&modify_autocreate,
  301:                   },
  302:         'directorysrch' => 
  303:                   { text => 'Institutional directory searches',
  304:                     help => 'Domain_Configuration_InstDirectory_Search',
  305:                     header => [{col1 => 'Setting',
  306:                                 col2 => 'Value',}],
  307:                     print => \&print_directorysrch,
  308:                     modify => \&modify_directorysrch,
  309:                   },
  310:         'contacts' =>
  311:                   { text => 'Contact Information',
  312:                     help => 'Domain_Configuration_Contact_Info',
  313:                     header => [{col1 => 'Setting',
  314:                                 col2 => 'Value',}],
  315:                     print => \&print_contacts,
  316:                     modify => \&modify_contacts,
  317:                   },
  318:         'usercreation' => 
  319:                   { text => 'User creation',
  320:                     help => 'Domain_Configuration_User_Creation',
  321:                     header => [{col1 => 'Format rule type',
  322:                                 col2 => 'Format rules in force'},
  323:                                {col1 => 'User account creation',
  324:                                 col2 => 'Usernames which may be created',},
  325:                                {col1 => 'Context',
  326:                                 col2 => 'Assignable authentication types'}],
  327:                     print => \&print_usercreation,
  328:                     modify => \&modify_usercreation,
  329:                   },
  330:         'selfcreation' => 
  331:                   { text => 'Users self-creating accounts',
  332:                     help => 'Domain_Configuration_Self_Creation', 
  333:                     header => [{col1 => 'Self-creation with institutional username',
  334:                                 col2 => 'Enabled?'},
  335:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  336:                                 col2 => 'Information user can enter'},
  337:                                {col1 => 'Self-creation with e-mail as username',
  338:                                 col2 => 'Settings'}],
  339:                     print => \&print_selfcreation,
  340:                     modify => \&modify_selfcreation,
  341:                   },
  342:         'usermodification' =>
  343:                   { text => 'User modification',
  344:                     help => 'Domain_Configuration_User_Modification',
  345:                     header => [{col1 => 'Target user has role',
  346:                                 col2 => 'User information updatable in author context'},
  347:                                {col1 => 'Target user has role',
  348:                                 col2 => 'User information updatable in course context'}],
  349:                     print => \&print_usermodification,
  350:                     modify => \&modify_usermodification,
  351:                   },
  352:         'scantron' =>
  353:                   { text => 'Bubblesheet format file',
  354:                     help => 'Domain_Configuration_Scantron_Format',
  355:                     header => [ {col1 => 'Item',
  356:                                  col2 => '',
  357:                               }],
  358:                     print => \&print_scantron,
  359:                     modify => \&modify_scantron,
  360:                   },
  361:         'requestcourses' => 
  362:                  {text => 'Request creation of courses',
  363:                   help => 'Domain_Configuration_Request_Courses',
  364:                   header => [{col1 => 'User affiliation',
  365:                               col2 => 'Availability/Processing of requests',},
  366:                              {col1 => 'Setting',
  367:                               col2 => 'Value'},
  368:                              {col1 => 'Available textbooks',
  369:                               col2 => ''},
  370:                              {col1 => 'Validation (not official courses)',
  371:                               col2 => 'Value'},],
  372:                   print => \&print_quotas,
  373:                   modify => \&modify_quotas,
  374:                  },
  375:         'requestauthor' =>
  376:                  {text => 'Request Authoring Space',
  377:                   help => 'Domain_Configuration_Request_Author',
  378:                   header => [{col1 => 'User affiliation',
  379:                               col2 => 'Availability/Processing of requests',},
  380:                              {col1 => 'Setting',
  381:                               col2 => 'Value'}],
  382:                   print => \&print_quotas,
  383:                   modify => \&modify_quotas,
  384:                  },
  385:         'coursecategories' =>
  386:                   { text => 'Cataloging of courses/communities',
  387:                     help => 'Domain_Configuration_Cataloging_Courses',
  388:                     header => [{col1 => 'Category settings',
  389:                                 col2 => '',},
  390:                                {col1 => 'Categories',
  391:                                 col2 => '',
  392:                                }],
  393:                     print => \&print_coursecategories,
  394:                     modify => \&modify_coursecategories,
  395:                   },
  396:         'serverstatuses' =>
  397:                  {text   => 'Access to server status pages',
  398:                   help   => 'Domain_Configuration_Server_Status',
  399:                   header => [{col1 => 'Status Page',
  400:                               col2 => 'Other named users',
  401:                               col3 => 'Specific IPs',
  402:                             }],
  403:                   print => \&print_serverstatuses,
  404:                   modify => \&modify_serverstatuses,
  405:                  },
  406:         'helpsettings' =>
  407:                  {text   => 'Help page settings',
  408:                   help   => 'Domain_Configuration_Help_Settings',
  409:                   header => [{col1 => 'Help Settings (logged-in users)',
  410:                               col2 => 'Value'}],
  411:                   print  => \&print_helpsettings,
  412:                   modify => \&modify_helpsettings,
  413:                  },
  414:         'coursedefaults' => 
  415:                  {text => 'Course/Community defaults',
  416:                   help => 'Domain_Configuration_Course_Defaults',
  417:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  418:                               col2 => 'Value',},
  419:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  420:                               col2 => 'Value',},],
  421:                   print => \&print_coursedefaults,
  422:                   modify => \&modify_coursedefaults,
  423:                  },
  424:         'selfenrollment' => 
  425:                  {text   => 'Self-enrollment in Course/Community',
  426:                   help   => 'Domain_Configuration_Selfenrollment',
  427:                   header => [{col1 => 'Configuration Rights',
  428:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  429:                              {col1 => 'Defaults',
  430:                               col2 => 'Value'},
  431:                              {col1 => 'Self-enrollment validation (optional)',
  432:                               col2 => 'Value'},],
  433:                   print => \&print_selfenrollment,
  434:                   modify => \&modify_selfenrollment,
  435:                  },
  436:         'privacy' => 
  437:                  {text   => 'User Privacy',
  438:                   help   => 'Domain_Configuration_User_Privacy',
  439:                   header => [{col1 => 'Setting',
  440:                               col2 => 'Value',}],
  441:                   print => \&print_privacy,
  442:                   modify => \&modify_privacy,
  443:                  },
  444:         'usersessions' =>
  445:                  {text  => 'User session hosting/offloading',
  446:                   help  => 'Domain_Configuration_User_Sessions',
  447:                   header => [{col1 => 'Domain server',
  448:                               col2 => 'Servers to offload sessions to when busy'},
  449:                              {col1 => 'Hosting of users from other domains',
  450:                               col2 => 'Rules'},
  451:                              {col1 => "Hosting domain's own users elsewhere",
  452:                               col2 => 'Rules'}],
  453:                   print => \&print_usersessions,
  454:                   modify => \&modify_usersessions,
  455:                  },
  456:          'loadbalancing' =>
  457:                  {text  => 'Dedicated Load Balancer(s)',
  458:                   help  => 'Domain_Configuration_Load_Balancing',
  459:                   header => [{col1 => 'Balancers',
  460:                               col2 => 'Default destinations',
  461:                               col3 => 'User affiliation',
  462:                               col4 => 'Overrides'},
  463:                             ],
  464:                   print => \&print_loadbalancing,
  465:                   modify => \&modify_loadbalancing,
  466:                  },
  467:     );
  468:     if (keys(%servers) > 1) {
  469:         $prefs{'login'}  = { text   => 'Log-in page options',
  470:                              help   => 'Domain_Configuration_Login_Page',
  471:                             header => [{col1 => 'Log-in Service',
  472:                                         col2 => 'Server Setting',},
  473:                                        {col1 => 'Log-in Page Items',
  474:                                         col2 => ''},
  475:                                        {col1 => 'Log-in Help',
  476:                                         col2 => 'Value'}],
  477:                             print => \&print_login,
  478:                             modify => \&modify_login,
  479:                            };
  480:     }
  481: 
  482:     my @roles = ('student','coordinator','author','admin');
  483:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  484:     &Apache::lonhtmlcommon::add_breadcrumb
  485:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  486:       text=>"Settings to display/modify"});
  487:     my $confname = $dom.'-domainconfig';
  488: 
  489:     if ($phase eq 'process') {
  490:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  491:                                                               \%prefs,\%domconfig,$confname,\@roles);
  492:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  493:             $r->rflush();
  494:             &devalidate_remote_domconfs($dom,$result);
  495:         }
  496:     } elsif ($phase eq 'display') {
  497:         my $js = &recaptcha_js().
  498:                  &credits_js();
  499:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  500:             my ($othertitle,$usertypes,$types) =
  501:                 &Apache::loncommon::sorted_inst_types($dom);
  502:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  503:                                           $domconfig{'loadbalancing'}).
  504:                    &new_spares_js().
  505:                    &common_domprefs_js().
  506:                    &Apache::loncommon::javascript_array_indexof();
  507:         }
  508:         if (grep(/^requestcourses$/,@actions)) {
  509:             my $javascript_validations;
  510:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  511:             $js .= <<END;
  512: <script type="text/javascript">
  513: $javascript_validations
  514: </script>
  515: $coursebrowserjs
  516: END
  517:         }
  518:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  519:     } else {
  520: # check if domconfig user exists for the domain.
  521:         my $servadm = $r->dir_config('lonAdmEMail');
  522:         my ($configuserok,$author_ok,$switchserver) =
  523:             &config_check($dom,$confname,$servadm);
  524:         unless ($configuserok eq 'ok') {
  525:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  526:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  527:                           $confname).
  528:                       '<br />'
  529:             );
  530:             if ($switchserver) {
  531:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  532:                           '<br />'.
  533:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  534:                           '<br />'.
  535:                           &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).
  536:                           '<br />'.
  537:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  538:                 );
  539:             } else {
  540:                 $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.').
  541:                           '<br />'.
  542:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  543:                 );
  544:             }
  545:             $r->print(&Apache::loncommon::end_page());
  546:             return OK;
  547:         }
  548:         if (keys(%domconfig) == 0) {
  549:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  550:             my @ids=&Apache::lonnet::current_machine_ids();
  551:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  552:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  553:                 my @loginimages = ('img','logo','domlogo','login');
  554:                 my $custom_img_count = 0;
  555:                 foreach my $img (@loginimages) {
  556:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  557:                         $custom_img_count ++;
  558:                     }
  559:                 }
  560:                 foreach my $role (@roles) {
  561:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  562:                         $custom_img_count ++;
  563:                     }
  564:                 }
  565:                 if ($custom_img_count > 0) {
  566:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  567:                     my $switch_server = &check_switchserver($dom,$confname);
  568:                     $r->print(
  569:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  570:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  571:     &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 />'.
  572:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  573:                     if ($switch_server) {
  574:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  575:                     }
  576:                     $r->print(&Apache::loncommon::end_page());
  577:                     return OK;
  578:                 }
  579:             }
  580:         }
  581:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  582:     }
  583:     return OK;
  584: }
  585: 
  586: sub process_changes {
  587:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  588:     my %domconfig;
  589:     if (ref($values) eq 'HASH') {
  590:         %domconfig = %{$values};
  591:     }
  592:     my $output;
  593:     if ($action eq 'login') {
  594:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  595:     } elsif ($action eq 'rolecolors') {
  596:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  597:                                      $lastactref,%domconfig);
  598:     } elsif ($action eq 'quotas') {
  599:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  600:     } elsif ($action eq 'autoenroll') {
  601:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  602:     } elsif ($action eq 'autoupdate') {
  603:         $output = &modify_autoupdate($dom,%domconfig);
  604:     } elsif ($action eq 'autocreate') {
  605:         $output = &modify_autocreate($dom,%domconfig);
  606:     } elsif ($action eq 'directorysrch') {
  607:         $output = &modify_directorysrch($dom,%domconfig);
  608:     } elsif ($action eq 'usercreation') {
  609:         $output = &modify_usercreation($dom,%domconfig);
  610:     } elsif ($action eq 'selfcreation') {
  611:         $output = &modify_selfcreation($dom,%domconfig);
  612:     } elsif ($action eq 'usermodification') {
  613:         $output = &modify_usermodification($dom,%domconfig);
  614:     } elsif ($action eq 'contacts') {
  615:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  616:     } elsif ($action eq 'defaults') {
  617:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  618:     } elsif ($action eq 'scantron') {
  619:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  620:     } elsif ($action eq 'coursecategories') {
  621:         $output = &modify_coursecategories($dom,%domconfig);
  622:     } elsif ($action eq 'serverstatuses') {
  623:         $output = &modify_serverstatuses($dom,%domconfig);
  624:     } elsif ($action eq 'requestcourses') {
  625:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  626:     } elsif ($action eq 'requestauthor') {
  627:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  628:     } elsif ($action eq 'helpsettings') {
  629:         $output = &modify_helpsettings($r,$dom,$confname,%domconfig);
  630:     } elsif ($action eq 'coursedefaults') {
  631:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  632:     } elsif ($action eq 'selfenrollment') {
  633:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  634:     } elsif ($action eq 'usersessions') {
  635:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  636:     } elsif ($action eq 'loadbalancing') {
  637:         $output = &modify_loadbalancing($dom,%domconfig);
  638:     }
  639:     return $output;
  640: }
  641: 
  642: sub print_config_box {
  643:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  644:     my $rowtotal = 0;
  645:     my $output;
  646:     if ($action eq 'coursecategories') {
  647:         $output = &coursecategories_javascript($settings);
  648:     }
  649:     $output .= 
  650:          '<table class="LC_nested_outer">
  651:           <tr>
  652:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  653:            &mt($item->{text}).'&nbsp;'.
  654:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  655:           '</tr>';
  656:     $rowtotal ++;
  657:     my $numheaders = 1;
  658:     if (ref($item->{'header'}) eq 'ARRAY') {
  659:         $numheaders = scalar(@{$item->{'header'}});
  660:     }
  661:     if ($numheaders > 1) {
  662:         my $colspan = '';
  663:         my $rightcolspan = '';
  664:         if (($action eq 'rolecolors') || ($action eq 'coursecategories') ||
  665:             (($action eq 'login') && ($numheaders < 3))) {
  666:             $colspan = ' colspan="2"';
  667:         }
  668:         if ($action eq 'usersessions') {
  669:             $rightcolspan = ' colspan="3"'; 
  670:         }
  671:         $output .= '
  672:           <tr>
  673:            <td>
  674:             <table class="LC_nested">
  675:              <tr class="LC_info_row">
  676:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  677:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  678:              </tr>';
  679:         $rowtotal ++;
  680:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  681:             ($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  682:             ($action eq 'selfenrollment') || ($action eq 'usersessions')) {
  683:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  684:         } elsif ($action eq 'coursecategories') {
  685:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  686:         } elsif ($action eq 'login') {
  687:             if ($numheaders == 3) {
  688:                 $colspan = ' colspan="2"';
  689:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  690:             } else {
  691:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  692:             }
  693:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  694:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  695:         } elsif ($action eq 'rolecolors') {
  696:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  697:         }
  698:         $output .= '
  699:            </table>
  700:           </td>
  701:          </tr>
  702:          <tr>
  703:            <td>
  704:             <table class="LC_nested">
  705:              <tr class="LC_info_row">
  706:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  707:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  708:              </tr>';
  709:             $rowtotal ++;
  710:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  711:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  712:             ($action eq 'usersessions')) {
  713:             $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal).'
  714:            </table>
  715:           </td>
  716:          </tr>
  717:          <tr>
  718:            <td>
  719:             <table class="LC_nested">
  720:              <tr class="LC_info_row">
  721:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  722:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  723:              </tr>'."\n".
  724:             $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  725:             $rowtotal ++;
  726:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults')) {
  727:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  728:         } elsif ($action eq 'coursecategories') {
  729:             $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  730:         } elsif ($action eq 'login') {
  731:             if ($numheaders == 3) {
  732:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  733:            </table>
  734:           </td>
  735:          </tr>
  736:          <tr>
  737:            <td>
  738:             <table class="LC_nested">
  739:              <tr class="LC_info_row">
  740:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  741:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  742:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  743:                 $rowtotal ++;
  744:             } else {
  745:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  746:             }
  747:         } elsif ($action eq 'requestcourses') {
  748:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal).
  749:                        &print_studentcode($settings,\$rowtotal).'
  750:            </table>
  751:           </td>
  752:          </tr>
  753:          <tr>
  754:            <td>
  755:             <table class="LC_nested">
  756:              <tr class="LC_info_row">
  757:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  758:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  759:                        &print_textbookcourses($dom,$settings,\$rowtotal).'
  760:             </table>
  761:            </td>
  762:           </tr>
  763:           <tr>
  764:            <td>
  765:             <table class="LC_nested">
  766:              <tr class="LC_info_row">
  767:               <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  768:               <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  769:              </tr>'.
  770:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
  771:         } elsif ($action eq 'requestauthor') {
  772:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  773:         } elsif ($action eq 'rolecolors') {
  774:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  775:            </table>
  776:           </td>
  777:          </tr>
  778:          <tr>
  779:            <td>
  780:             <table class="LC_nested">
  781:              <tr class="LC_info_row">
  782:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  783:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  784:               <td class="LC_right_item" valign="top">'.
  785:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  786:              </tr>'.
  787:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  788:            </table>
  789:           </td>
  790:          </tr>
  791:          <tr>
  792:            <td>
  793:             <table class="LC_nested">
  794:              <tr class="LC_info_row">
  795:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  796:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  797:              </tr>'.
  798:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  799:             $rowtotal += 2;
  800:         }
  801:     } else {
  802:         $output .= '
  803:           <tr>
  804:            <td>
  805:             <table class="LC_nested">
  806:              <tr class="LC_info_row">';
  807:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  808:             $output .= '  
  809:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  810:         } elsif ($action eq 'serverstatuses') {
  811:             $output .= '
  812:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  813:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  814: 
  815:         } else {
  816:             $output .= '
  817:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  818:         }
  819:         if (defined($item->{'header'}->[0]->{'col3'})) {
  820:             $output .= '<td class="LC_left_item" valign="top">'.
  821:                        &mt($item->{'header'}->[0]->{'col2'});
  822:             if ($action eq 'serverstatuses') {
  823:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  824:             } 
  825:         } else {
  826:             $output .= '<td class="LC_right_item" valign="top">'.
  827:                        &mt($item->{'header'}->[0]->{'col2'});
  828:         }
  829:         $output .= '</td>';
  830:         if ($item->{'header'}->[0]->{'col3'}) {
  831:             if (defined($item->{'header'}->[0]->{'col4'})) {
  832:                 $output .= '<td class="LC_left_item" valign="top">'.
  833:                             &mt($item->{'header'}->[0]->{'col3'});
  834:             } else {
  835:                 $output .= '<td class="LC_right_item" valign="top">'.
  836:                            &mt($item->{'header'}->[0]->{'col3'});
  837:             }
  838:             if ($action eq 'serverstatuses') {
  839:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  840:             }
  841:             $output .= '</td>';
  842:         }
  843:         if ($item->{'header'}->[0]->{'col4'}) {
  844:             $output .= '<td class="LC_right_item" valign="top">'.
  845:                        &mt($item->{'header'}->[0]->{'col4'});
  846:         }
  847:         $output .= '</tr>';
  848:         $rowtotal ++;
  849:         if ($action eq 'quotas') {
  850:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  851:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || ($action eq 'directorysrch') ||
  852:                  ($action eq 'contacts') || ($action eq 'defaults') || ($action eq 'serverstatuses') ||
  853:                  ($action eq 'loadbalancing')) {
  854:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
  855:         } elsif ($action eq 'scantron') {
  856:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  857:         } elsif ($action eq 'helpsettings') {
  858:             $output .= &print_helpsettings($dom,$confname,$settings,\$rowtotal);
  859:         }
  860:     }
  861:     $output .= '
  862:    </table>
  863:   </td>
  864:  </tr>
  865: </table><br />';
  866:     return ($output,$rowtotal);
  867: }
  868: 
  869: sub print_login {
  870:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  871:     my ($css_class,$datatable);
  872:     my %choices = &login_choices();
  873: 
  874:     if ($caller eq 'service') {
  875:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  876:         my $choice = $choices{'disallowlogin'};
  877:         $css_class = ' class="LC_odd_row"';
  878:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  879:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  880:                       '<th>'.$choices{'server'}.'</th>'.
  881:                       '<th>'.$choices{'serverpath'}.'</th>'.
  882:                       '<th>'.$choices{'custompath'}.'</th>'.
  883:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  884:         my %disallowed;
  885:         if (ref($settings) eq 'HASH') {
  886:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  887:                %disallowed = %{$settings->{'loginvia'}};
  888:             }
  889:         }
  890:         foreach my $lonhost (sort(keys(%servers))) {
  891:             my $direct = 'selected="selected"';
  892:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  893:                 if ($disallowed{$lonhost}{'server'} ne '') {
  894:                     $direct = '';
  895:                 }
  896:             }
  897:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  898:                           '<td><select name="'.$lonhost.'_server">'.
  899:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  900:                           '</option>';
  901:             foreach my $hostid (sort(keys(%servers))) {
  902:                 next if ($servers{$hostid} eq $servers{$lonhost});
  903:                 my $selected = '';
  904:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  905:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  906:                         $selected = 'selected="selected"';
  907:                     }
  908:                 }
  909:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  910:                               $servers{$hostid}.'</option>';
  911:             }
  912:             $datatable .= '</select></td>'.
  913:                           '<td><select name="'.$lonhost.'_serverpath">';
  914:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  915:                 my $pathname = $path;
  916:                 if ($path eq 'custom') {
  917:                     $pathname = &mt('Custom Path').' ->';
  918:                 }
  919:                 my $selected = '';
  920:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  921:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  922:                         $selected = 'selected="selected"';
  923:                     }
  924:                 } elsif ($path eq '') {
  925:                     $selected = 'selected="selected"';
  926:                 }
  927:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  928:             }
  929:             $datatable .= '</select></td>';
  930:             my ($custom,$exempt);
  931:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  932:                 $custom = $disallowed{$lonhost}{'custompath'};
  933:                 $exempt = $disallowed{$lonhost}{'exempt'};
  934:             }
  935:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  936:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  937:                           '</tr>';
  938:         }
  939:         $datatable .= '</table></td></tr>';
  940:         return $datatable;
  941:     } elsif ($caller eq 'page') {
  942:         my %defaultchecked = ( 
  943:                                'coursecatalog' => 'on',
  944:                                'helpdesk'      => 'on',
  945:                                'adminmail'     => 'off',
  946:                                'newuser'       => 'off',
  947:                              );
  948:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
  949:         my (%checkedon,%checkedoff);
  950:         foreach my $item (@toggles) {
  951:             if ($defaultchecked{$item} eq 'on') { 
  952:                 $checkedon{$item} = ' checked="checked" ';
  953:                 $checkedoff{$item} = ' ';
  954:             } elsif ($defaultchecked{$item} eq 'off') {
  955:                 $checkedoff{$item} = ' checked="checked" ';
  956:                 $checkedon{$item} = ' ';
  957:             }
  958:         }
  959:         my @images = ('img','logo','domlogo','login');
  960:         my @logintext = ('textcol','bgcol');
  961:         my @bgs = ('pgbg','mainbg','sidebg');
  962:         my @links = ('link','alink','vlink');
  963:         my %designhash = &Apache::loncommon::get_domainconf($dom);
  964:         my %defaultdesign = %Apache::loncommon::defaultdesign;
  965:         my (%is_custom,%designs);
  966:         my %defaults = (
  967:                        font => $defaultdesign{'login.font'},
  968:                        );
  969:         foreach my $item (@images) {
  970:             $defaults{$item} = $defaultdesign{'login.'.$item};
  971:             $defaults{'showlogo'}{$item} = 1;
  972:         }
  973:         foreach my $item (@bgs) {
  974:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
  975:         }
  976:         foreach my $item (@logintext) {
  977:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
  978:         }
  979:         foreach my $item (@links) {
  980:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
  981:         }
  982:         if (ref($settings) eq 'HASH') {
  983:             foreach my $item (@toggles) {
  984:                 if ($settings->{$item} eq '1') {
  985:                     $checkedon{$item} =  ' checked="checked" ';
  986:                     $checkedoff{$item} = ' ';
  987:                 } elsif ($settings->{$item} eq '0') {
  988:                     $checkedoff{$item} =  ' checked="checked" ';
  989:                     $checkedon{$item} = ' ';
  990:                 }
  991:             }
  992:             foreach my $item (@images) {
  993:                 if (defined($settings->{$item})) {
  994:                     $designs{$item} = $settings->{$item};
  995:                     $is_custom{$item} = 1;
  996:                 }
  997:                 if (defined($settings->{'showlogo'}{$item})) {
  998:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
  999:                 }
 1000:             }
 1001:             foreach my $item (@logintext) {
 1002:                 if ($settings->{$item} ne '') {
 1003:                     $designs{'logintext'}{$item} = $settings->{$item};
 1004:                     $is_custom{$item} = 1;
 1005:                 }
 1006:             }
 1007:             if ($settings->{'font'} ne '') {
 1008:                 $designs{'font'} = $settings->{'font'};
 1009:                 $is_custom{'font'} = 1;
 1010:             }
 1011:             foreach my $item (@bgs) {
 1012:                 if ($settings->{$item} ne '') {
 1013:                     $designs{'bgs'}{$item} = $settings->{$item};
 1014:                     $is_custom{$item} = 1;
 1015:                 }
 1016:             }
 1017:             foreach my $item (@links) {
 1018:                 if ($settings->{$item} ne '') {
 1019:                     $designs{'links'}{$item} = $settings->{$item};
 1020:                     $is_custom{$item} = 1;
 1021:                 }
 1022:             }
 1023:         } else {
 1024:             if ($designhash{$dom.'.login.font'} ne '') {
 1025:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1026:                 $is_custom{'font'} = 1;
 1027:             }
 1028:             foreach my $item (@images) {
 1029:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1030:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1031:                     $is_custom{$item} = 1;
 1032:                 }
 1033:             }
 1034:             foreach my $item (@bgs) {
 1035:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1036:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1037:                     $is_custom{$item} = 1;
 1038:                 }
 1039:             }
 1040:             foreach my $item (@links) {
 1041:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1042:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1043:                     $is_custom{$item} = 1;
 1044:                 }
 1045:             }
 1046:         }
 1047:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1048:                                                       logo => 'Institution Logo',
 1049:                                                       domlogo => 'Domain Logo',
 1050:                                                       login => 'Login box');
 1051:         my $itemcount = 1;
 1052:         foreach my $item (@toggles) {
 1053:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1054:             $datatable .=  
 1055:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1056:                 '</td><td>'.
 1057:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1058:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1059:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1060:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1061:                 '</tr>';
 1062:             $itemcount ++;
 1063:         }
 1064:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1065:         $datatable .= '</tr></table></td></tr>';
 1066:     } elsif ($caller eq 'help') {
 1067:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1068:         my $switchserver = &check_switchserver($dom,$confname);
 1069:         my $itemcount = 1;
 1070:         $defaulturl = '/adm/loginproblems.html';
 1071:         $defaulttype = 'default';
 1072:         %lt = &Apache::lonlocal::texthash (
 1073:                      del     => 'Delete?',
 1074:                      rep     => 'Replace:',
 1075:                      upl     => 'Upload:',
 1076:                      default => 'Default',
 1077:                      custom  => 'Custom',
 1078:                                              );
 1079:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1080:         my @currlangs;
 1081:         if (ref($settings) eq 'HASH') {
 1082:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1083:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1084:                     next if ($settings->{'helpurl'}{$key} eq '');
 1085:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1086:                     $type{$key} = 'custom';
 1087:                     unless ($key eq 'nolang') {
 1088:                         push(@currlangs,$key);
 1089:                     }
 1090:                 }
 1091:             } elsif ($settings->{'helpurl'} ne '') {
 1092:                 $type{'nolang'} = 'custom';
 1093:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1094:             }
 1095:         }
 1096:         foreach my $lang ('nolang',sort(@currlangs)) {
 1097:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1098:             $datatable .= '<tr'.$css_class.'>';
 1099:             if ($url{$lang} eq '') {
 1100:                 $url{$lang} = $defaulturl;
 1101:             }
 1102:             if ($type{$lang} eq '') {
 1103:                 $type{$lang} = $defaulttype;
 1104:             }
 1105:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1106:             if ($lang eq 'nolang') {
 1107:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1108:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1109:             } else {
 1110:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1111:                                   $langchoices{$lang},
 1112:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1113:             }
 1114:             $datatable .= '</span></td>'."\n".
 1115:                           '<td class="LC_left_item">';
 1116:             if ($type{$lang} eq 'custom') {
 1117:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1118:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1119:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1120:             } else {
 1121:                 $datatable .= $lt{'upl'};
 1122:             }
 1123:             $datatable .='<br />';
 1124:             if ($switchserver) {
 1125:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1126:             } else {
 1127:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1128:             }
 1129:             $datatable .= '</td></tr>';
 1130:             $itemcount ++;
 1131:         }
 1132:         my @addlangs;
 1133:         foreach my $lang (sort(keys(%langchoices))) {
 1134:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1135:             push(@addlangs,$lang);
 1136:         }
 1137:         if (@addlangs > 0) {
 1138:             my %toadd;
 1139:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1140:             $toadd{''} = &mt('Select');
 1141:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1142:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1143:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1144:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1145:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1146:             if ($switchserver) {
 1147:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1148:             } else {
 1149:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1150:             }
 1151:             $datatable .= '</td></tr>';
 1152:             $itemcount ++;
 1153:         }
 1154:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1155:     }
 1156:     return $datatable;
 1157: }
 1158: 
 1159: sub login_choices {
 1160:     my %choices =
 1161:         &Apache::lonlocal::texthash (
 1162:             coursecatalog => 'Display Course/Community Catalog link?',
 1163:             adminmail     => "Display Administrator's E-mail Address?",
 1164:             helpdesk      => 'Display "Contact Helpdesk" link',
 1165:             disallowlogin => "Login page requests redirected",
 1166:             hostid        => "Server",
 1167:             server        => "Redirect to:",
 1168:             serverpath    => "Path",
 1169:             custompath    => "Custom", 
 1170:             exempt        => "Exempt IP(s)",
 1171:             directlogin   => "No redirect",
 1172:             newuser       => "Link to create a user account",
 1173:             img           => "Header",
 1174:             logo          => "Main Logo",
 1175:             domlogo       => "Domain Logo",
 1176:             login         => "Log-in Header", 
 1177:             textcol       => "Text color",
 1178:             bgcol         => "Box color",
 1179:             bgs           => "Background colors",
 1180:             links         => "Link colors",
 1181:             font          => "Font color",
 1182:             pgbg          => "Header",
 1183:             mainbg        => "Page",
 1184:             sidebg        => "Login box",
 1185:             link          => "Link",
 1186:             alink         => "Active link",
 1187:             vlink         => "Visited link",
 1188:         );
 1189:     return %choices;
 1190: }
 1191: 
 1192: sub print_rolecolors {
 1193:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1194:     my %choices = &color_font_choices();
 1195:     my @bgs = ('pgbg','tabbg','sidebg');
 1196:     my @links = ('link','alink','vlink');
 1197:     my @images = ('img');
 1198:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1199:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1200:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1201:     my (%is_custom,%designs);
 1202:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1203:     if (ref($settings) eq 'HASH') {
 1204:         if (ref($settings->{$role}) eq 'HASH') {
 1205:             if ($settings->{$role}->{'img'} ne '') {
 1206:                 $designs{'img'} = $settings->{$role}->{'img'};
 1207:                 $is_custom{'img'} = 1;
 1208:             }
 1209:             if ($settings->{$role}->{'font'} ne '') {
 1210:                 $designs{'font'} = $settings->{$role}->{'font'};
 1211:                 $is_custom{'font'} = 1;
 1212:             }
 1213:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1214:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1215:                 $is_custom{'fontmenu'} = 1;
 1216:             }
 1217:             foreach my $item (@bgs) {
 1218:                 if ($settings->{$role}->{$item} ne '') {
 1219:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1220:                     $is_custom{$item} = 1;
 1221:                 }
 1222:             }
 1223:             foreach my $item (@links) {
 1224:                 if ($settings->{$role}->{$item} ne '') {
 1225:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1226:                     $is_custom{$item} = 1;
 1227:                 }
 1228:             }
 1229:         }
 1230:     } else {
 1231:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1232:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1233:             $is_custom{'img'} = 1;
 1234:         }
 1235:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1236:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1237:             $is_custom{'fontmenu'} = 1; 
 1238:         }
 1239:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1240:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1241:             $is_custom{'font'} = 1;
 1242:         }
 1243:         foreach my $item (@bgs) {
 1244:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1245:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1246:                 $is_custom{$item} = 1;
 1247:             
 1248:             }
 1249:         }
 1250:         foreach my $item (@links) {
 1251:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1252:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1253:                 $is_custom{$item} = 1;
 1254:             }
 1255:         }
 1256:     }
 1257:     my $itemcount = 1;
 1258:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1259:     $datatable .= '</tr></table></td></tr>';
 1260:     return $datatable;
 1261: }
 1262: 
 1263: sub role_defaults {
 1264:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1265:     my %defaults;
 1266:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1267:         return %defaults;
 1268:     }
 1269:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1270:     if ($role eq 'login') {
 1271:         %defaults = (
 1272:                        font => $defaultdesign{$role.'.font'},
 1273:                     );
 1274:         if (ref($logintext) eq 'ARRAY') {
 1275:             foreach my $item (@{$logintext}) {
 1276:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1277:             }
 1278:         }
 1279:         foreach my $item (@{$images}) {
 1280:             $defaults{'showlogo'}{$item} = 1;
 1281:         }
 1282:     } else {
 1283:         %defaults = (
 1284:                        img => $defaultdesign{$role.'.img'},
 1285:                        font => $defaultdesign{$role.'.font'},
 1286:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1287:                     );
 1288:     }
 1289:     foreach my $item (@{$bgs}) {
 1290:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1291:     }
 1292:     foreach my $item (@{$links}) {
 1293:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1294:     }
 1295:     foreach my $item (@{$images}) {
 1296:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1297:     }
 1298:     return %defaults;
 1299: }
 1300: 
 1301: sub display_color_options {
 1302:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1303:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1304:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1305:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1306:     my $datatable = '<tr'.$css_class.'>'.
 1307:         '<td>'.$choices->{'font'}.'</td>';
 1308:     if (!$is_custom->{'font'}) {
 1309:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1310:     } else {
 1311:         $datatable .= '<td>&nbsp;</td>';
 1312:     }
 1313:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1314: 
 1315:     $datatable .= '<td><span class="LC_nobreak">'.
 1316:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1317:                   ' value="'.$current_color.'" />&nbsp;'.
 1318:                   '&nbsp;</td></tr>';
 1319:     unless ($role eq 'login') { 
 1320:         $datatable .= '<tr'.$css_class.'>'.
 1321:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1322:         if (!$is_custom->{'fontmenu'}) {
 1323:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1324:         } else {
 1325:             $datatable .= '<td>&nbsp;</td>';
 1326:         }
 1327: 	$current_color = $designs->{'fontmenu'} ?
 1328: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1329:         $datatable .= '<td><span class="LC_nobreak">'.
 1330:                       '<input class="colorchooser" type="text" size="10" name="'
 1331: 		      .$role.'_fontmenu"'.
 1332:                       ' value="'.$current_color.'" />&nbsp;'.
 1333:                       '&nbsp;</td></tr>';
 1334:     }
 1335:     my $switchserver = &check_switchserver($dom,$confname);
 1336:     foreach my $img (@{$images}) {
 1337: 	$itemcount ++;
 1338:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1339:         $datatable .= '<tr'.$css_class.'>'.
 1340:                       '<td>'.$choices->{$img};
 1341:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1342:         if ($role eq 'login') {
 1343:             if ($img eq 'login') {
 1344:                 $login_hdr_pick =
 1345:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1346:                 $logincolors =
 1347:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1348:                                        $designs,$defaults);
 1349:             } elsif ($img ne 'domlogo') {
 1350:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1351:             }
 1352:         }
 1353:         $datatable .= '</td>';
 1354:         if ($designs->{$img} ne '') {
 1355:             $imgfile = $designs->{$img};
 1356: 	    $img_import = ($imgfile =~ m{^/adm/});
 1357:         } else {
 1358:             $imgfile = $defaults->{$img};
 1359:         }
 1360:         if ($imgfile) {
 1361:             my ($showfile,$fullsize);
 1362:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1363:                 my $urldir = $1;
 1364:                 my $filename = $2;
 1365:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1366:                 if (@info) {
 1367:                     my $thumbfile = 'tn-'.$filename;
 1368:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1369:                     if (@thumb) {
 1370:                         $showfile = $urldir.'/'.$thumbfile;
 1371:                     } else {
 1372:                         $showfile = $imgfile;
 1373:                     }
 1374:                 } else {
 1375:                     $showfile = '';
 1376:                 }
 1377:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1378:                 $showfile = $imgfile;
 1379:                 my $imgdir = $1;
 1380:                 my $filename = $2;
 1381:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1382:                     $showfile = "/$imgdir/tn-".$filename;
 1383:                 } else {
 1384:                     my $input = $londocroot.$imgfile;
 1385:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1386:                     if (!-e $output) {
 1387:                         my ($width,$height) = &thumb_dimensions();
 1388:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1389:                         if ($fullwidth ne '' && $fullheight ne '') {
 1390:                             if ($fullwidth > $width && $fullheight > $height) { 
 1391:                                 my $size = $width.'x'.$height;
 1392:                                 system("convert -sample $size $input $output");
 1393:                                 $showfile = "/$imgdir/tn-".$filename;
 1394:                             }
 1395:                         }
 1396:                     }
 1397:                 }
 1398:             }
 1399:             if ($showfile) {
 1400:                 if ($showfile =~ m{^/(adm|res)/}) {
 1401:                     if ($showfile =~ m{^/res/}) {
 1402:                         my $local_showfile =
 1403:                             &Apache::lonnet::filelocation('',$showfile);
 1404:                         &Apache::lonnet::repcopy($local_showfile);
 1405:                     }
 1406:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1407:                 }
 1408:                 if ($imgfile) {
 1409:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1410:                         if ($imgfile =~ m{^/res/}) {
 1411:                             my $local_imgfile =
 1412:                                 &Apache::lonnet::filelocation('',$imgfile);
 1413:                             &Apache::lonnet::repcopy($local_imgfile);
 1414:                         }
 1415:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1416:                     } else {
 1417:                         $fullsize = $imgfile;
 1418:                     }
 1419:                 }
 1420:                 $datatable .= '<td>';
 1421:                 if ($img eq 'login') {
 1422:                     $datatable .= $login_hdr_pick;
 1423:                 } 
 1424:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1425:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1426:             } else {
 1427:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1428:                               &mt('Upload:').'<br />';
 1429:             }
 1430:         } else {
 1431:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1432:                           &mt('Upload:').'<br />';
 1433:         }
 1434:         if ($switchserver) {
 1435:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1436:         } else {
 1437:             if ($img ne 'login') { # suppress file selection for Log-in header
 1438:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1439:             }
 1440:         }
 1441:         $datatable .= '</td></tr>';
 1442:     }
 1443:     $itemcount ++;
 1444:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1445:     $datatable .= '<tr'.$css_class.'>'.
 1446:                   '<td>'.$choices->{'bgs'}.'</td>';
 1447:     my $bgs_def;
 1448:     foreach my $item (@{$bgs}) {
 1449:         if (!$is_custom->{$item}) {
 1450:             $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>';
 1451:         }
 1452:     }
 1453:     if ($bgs_def) {
 1454:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1455:     } else {
 1456:         $datatable .= '<td>&nbsp;</td>';
 1457:     }
 1458:     $datatable .= '<td class="LC_right_item">'.
 1459:                   '<table border="0"><tr>';
 1460: 
 1461:     foreach my $item (@{$bgs}) {
 1462:         $datatable .= '<td align="center">'.$choices->{$item};
 1463: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1464:         if ($designs->{'bgs'}{$item}) {
 1465:             $datatable .= '&nbsp;';
 1466:         }
 1467:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1468:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1469:     }
 1470:     $datatable .= '</tr></table></td></tr>';
 1471:     $itemcount ++;
 1472:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1473:     $datatable .= '<tr'.$css_class.'>'.
 1474:                   '<td>'.$choices->{'links'}.'</td>';
 1475:     my $links_def;
 1476:     foreach my $item (@{$links}) {
 1477:         if (!$is_custom->{$item}) {
 1478:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1479:         }
 1480:     }
 1481:     if ($links_def) {
 1482:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1483:     } else {
 1484:         $datatable .= '<td>&nbsp;</td>';
 1485:     }
 1486:     $datatable .= '<td class="LC_right_item">'.
 1487:                   '<table border="0"><tr>';
 1488:     foreach my $item (@{$links}) {
 1489: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1490:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1491:         if ($designs->{'links'}{$item}) {
 1492:             $datatable.='&nbsp;';
 1493:         }
 1494:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1495:                       '" /></td>';
 1496:     }
 1497:     $$rowtotal += $itemcount;
 1498:     return $datatable;
 1499: }
 1500: 
 1501: sub logo_display_options {
 1502:     my ($img,$defaults,$designs) = @_;
 1503:     my $checkedon;
 1504:     if (ref($defaults) eq 'HASH') {
 1505:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1506:             if ($defaults->{'showlogo'}{$img}) {
 1507:                 $checkedon = 'checked="checked" ';     
 1508:             }
 1509:         } 
 1510:     }
 1511:     if (ref($designs) eq 'HASH') {
 1512:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1513:             if (defined($designs->{'showlogo'}{$img})) {
 1514:                 if ($designs->{'showlogo'}{$img} == 0) {
 1515:                     $checkedon = '';
 1516:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1517:                     $checkedon = 'checked="checked" ';
 1518:                 }
 1519:             }
 1520:         }
 1521:     }
 1522:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1523:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1524:            &mt('show').'</label>'."\n";
 1525: }
 1526: 
 1527: sub login_header_options  {
 1528:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1529:     my $output = '';
 1530:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1531:         $output .= &mt('Text default(s):').'<br />';
 1532:         if (!$is_custom->{'textcol'}) {
 1533:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1534:                        '&nbsp;&nbsp;&nbsp;';
 1535:         }
 1536:         if (!$is_custom->{'bgcol'}) {
 1537:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1538:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1539:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1540:         }
 1541:         $output .= '<br />';
 1542:     }
 1543:     $output .='<br />';
 1544:     return $output;
 1545: }
 1546: 
 1547: sub login_text_colors {
 1548:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1549:     my $color_menu = '<table border="0"><tr>';
 1550:     foreach my $item (@{$logintext}) {
 1551:         $color_menu .= '<td align="center">'.$choices->{$item};
 1552:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1553:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1554:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1555:     }
 1556:     $color_menu .= '</tr></table><br />';
 1557:     return $color_menu;
 1558: }
 1559: 
 1560: sub image_changes {
 1561:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1562:     my $output;
 1563:     if ($img eq 'login') {
 1564:             # suppress image for Log-in header
 1565:     } elsif (!$is_custom) {
 1566:         if ($img ne 'domlogo') {
 1567:             $output .= &mt('Default image:').'<br />';
 1568:         } else {
 1569:             $output .= &mt('Default in use:').'<br />';
 1570:         }
 1571:     }
 1572:     if ($img eq 'login') { # suppress image for Log-in header
 1573:         $output .= '<td>'.$logincolors;
 1574:     } else {
 1575:         if ($img_import) {
 1576:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1577:         }
 1578:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1579:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1580:         if ($is_custom) {
 1581:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1582:                        '<input type="checkbox" name="'.
 1583:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1584:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1585:         } else {
 1586:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1587:         }
 1588:     }
 1589:     return $output;
 1590: }
 1591: 
 1592: sub print_quotas {
 1593:     my ($dom,$settings,$rowtotal,$action) = @_;
 1594:     my $context;
 1595:     if ($action eq 'quotas') {
 1596:         $context = 'tools';
 1597:     } else {
 1598:         $context = $action;
 1599:     }
 1600:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1601:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1602:     my $typecount = 0;
 1603:     my ($css_class,%titles);
 1604:     if ($context eq 'requestcourses') {
 1605:         @usertools = ('official','unofficial','community','textbook');
 1606:         @options =('norequest','approval','validate','autolimit');
 1607:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1608:         %titles = &courserequest_titles();
 1609:     } elsif ($context eq 'requestauthor') {
 1610:         @usertools = ('author');
 1611:         @options = ('norequest','approval','automatic');
 1612:         %titles = &authorrequest_titles();
 1613:     } else {
 1614:         @usertools = ('aboutme','blog','webdav','portfolio');
 1615:         %titles = &tool_titles();
 1616:     }
 1617:     if (ref($types) eq 'ARRAY') {
 1618:         foreach my $type (@{$types}) {
 1619:             my ($currdefquota,$currauthorquota);
 1620:             unless (($context eq 'requestcourses') ||
 1621:                     ($context eq 'requestauthor')) {
 1622:                 if (ref($settings) eq 'HASH') {
 1623:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1624:                         $currdefquota = $settings->{defaultquota}->{$type};
 1625:                     } else {
 1626:                         $currdefquota = $settings->{$type};
 1627:                     }
 1628:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1629:                         $currauthorquota = $settings->{authorquota}->{$type};
 1630:                     }
 1631:                 }
 1632:             }
 1633:             if (defined($usertypes->{$type})) {
 1634:                 $typecount ++;
 1635:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1636:                 $datatable .= '<tr'.$css_class.'>'.
 1637:                               '<td>'.$usertypes->{$type}.'</td>'.
 1638:                               '<td class="LC_left_item">';
 1639:                 if ($context eq 'requestcourses') {
 1640:                     $datatable .= '<table><tr>';
 1641:                 }
 1642:                 my %cell;  
 1643:                 foreach my $item (@usertools) {
 1644:                     if ($context eq 'requestcourses') {
 1645:                         my ($curroption,$currlimit);
 1646:                         if (ref($settings) eq 'HASH') {
 1647:                             if (ref($settings->{$item}) eq 'HASH') {
 1648:                                 $curroption = $settings->{$item}->{$type};
 1649:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1650:                                     $currlimit = $1; 
 1651:                                 }
 1652:                             }
 1653:                         }
 1654:                         if (!$curroption) {
 1655:                             $curroption = 'norequest';
 1656:                         }
 1657:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1658:                         foreach my $option (@options) {
 1659:                             my $val = $option;
 1660:                             if ($option eq 'norequest') {
 1661:                                 $val = 0;  
 1662:                             }
 1663:                             if ($option eq 'validate') {
 1664:                                 my $canvalidate = 0;
 1665:                                 if (ref($validations{$item}) eq 'HASH') { 
 1666:                                     if ($validations{$item}{$type}) {
 1667:                                         $canvalidate = 1;
 1668:                                     }
 1669:                                 }
 1670:                                 next if (!$canvalidate);
 1671:                             }
 1672:                             my $checked = '';
 1673:                             if ($option eq $curroption) {
 1674:                                 $checked = ' checked="checked"';
 1675:                             } elsif ($option eq 'autolimit') {
 1676:                                 if ($curroption =~ /^autolimit/) {
 1677:                                     $checked = ' checked="checked"';
 1678:                                 }                       
 1679:                             } 
 1680:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1681:                                   '<input type="radio" name="crsreq_'.$item.
 1682:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1683:                                   $titles{$option}.'</label>';
 1684:                             if ($option eq 'autolimit') {
 1685:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1686:                                                 $item.'_limit_'.$type.'" size="1" '.
 1687:                                                 'value="'.$currlimit.'" />';
 1688:                             }
 1689:                             $cell{$item} .= '</span> ';
 1690:                             if ($option eq 'autolimit') {
 1691:                                 $cell{$item} .= $titles{'unlimited'};
 1692:                             }
 1693:                         }
 1694:                     } elsif ($context eq 'requestauthor') {
 1695:                         my $curroption;
 1696:                         if (ref($settings) eq 'HASH') {
 1697:                             $curroption = $settings->{$type};
 1698:                         }
 1699:                         if (!$curroption) {
 1700:                             $curroption = 'norequest';
 1701:                         }
 1702:                         foreach my $option (@options) {
 1703:                             my $val = $option;
 1704:                             if ($option eq 'norequest') {
 1705:                                 $val = 0;
 1706:                             }
 1707:                             my $checked = '';
 1708:                             if ($option eq $curroption) {
 1709:                                 $checked = ' checked="checked"';
 1710:                             }
 1711:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1712:                                   '<input type="radio" name="authorreq_'.$type.
 1713:                                   '" value="'.$val.'"'.$checked.' />'.
 1714:                                   $titles{$option}.'</label></span>&nbsp; ';
 1715:                         }
 1716:                     } else {
 1717:                         my $checked = 'checked="checked" ';
 1718:                         if (ref($settings) eq 'HASH') {
 1719:                             if (ref($settings->{$item}) eq 'HASH') {
 1720:                                 if ($settings->{$item}->{$type} == 0) {
 1721:                                     $checked = '';
 1722:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1723:                                     $checked =  'checked="checked" ';
 1724:                                 }
 1725:                             }
 1726:                         }
 1727:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1728:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1729:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1730:                                       '</label></span>&nbsp; ';
 1731:                     }
 1732:                 }
 1733:                 if ($context eq 'requestcourses') {
 1734:                     $datatable .= '</tr><tr>';
 1735:                     foreach my $item (@usertools) {
 1736:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1737:                     }
 1738:                     $datatable .= '</tr></table>';
 1739:                 }
 1740:                 $datatable .= '</td>';
 1741:                 unless (($context eq 'requestcourses') ||
 1742:                         ($context eq 'requestauthor')) {
 1743:                     $datatable .= 
 1744:                               '<td class="LC_right_item">'.
 1745:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1746:                               '<input type="text" name="quota_'.$type.
 1747:                               '" value="'.$currdefquota.
 1748:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1749:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1750:                               '<input type="text" name="authorquota_'.$type.
 1751:                               '" value="'.$currauthorquota.
 1752:                               '" size="5" /></span></td>';
 1753:                 }
 1754:                 $datatable .= '</tr>';
 1755:             }
 1756:         }
 1757:     }
 1758:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1759:         $defaultquota = '20';
 1760:         $authorquota = '500';
 1761:         if (ref($settings) eq 'HASH') {
 1762:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1763:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1764:             } elsif (defined($settings->{'default'})) {
 1765:                 $defaultquota = $settings->{'default'};
 1766:             }
 1767:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1768:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1769:             }
 1770:         }
 1771:     }
 1772:     $typecount ++;
 1773:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1774:     $datatable .= '<tr'.$css_class.'>'.
 1775:                   '<td>'.$othertitle.'</td>'.
 1776:                   '<td class="LC_left_item">';
 1777:     if ($context eq 'requestcourses') {
 1778:         $datatable .= '<table><tr>';
 1779:     }
 1780:     my %defcell;
 1781:     foreach my $item (@usertools) {
 1782:         if ($context eq 'requestcourses') {
 1783:             my ($curroption,$currlimit);
 1784:             if (ref($settings) eq 'HASH') {
 1785:                 if (ref($settings->{$item}) eq 'HASH') {
 1786:                     $curroption = $settings->{$item}->{'default'};
 1787:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1788:                         $currlimit = $1;
 1789:                     }
 1790:                 }
 1791:             }
 1792:             if (!$curroption) {
 1793:                 $curroption = 'norequest';
 1794:             }
 1795:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1796:             foreach my $option (@options) {
 1797:                 my $val = $option;
 1798:                 if ($option eq 'norequest') {
 1799:                     $val = 0;
 1800:                 }
 1801:                 if ($option eq 'validate') {
 1802:                     my $canvalidate = 0;
 1803:                     if (ref($validations{$item}) eq 'HASH') {
 1804:                         if ($validations{$item}{'default'}) {
 1805:                             $canvalidate = 1;
 1806:                         }
 1807:                     }
 1808:                     next if (!$canvalidate);
 1809:                 }
 1810:                 my $checked = '';
 1811:                 if ($option eq $curroption) {
 1812:                     $checked = ' checked="checked"';
 1813:                 } elsif ($option eq 'autolimit') {
 1814:                     if ($curroption =~ /^autolimit/) {
 1815:                         $checked = ' checked="checked"';
 1816:                     }
 1817:                 }
 1818:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1819:                                   '<input type="radio" name="crsreq_'.$item.
 1820:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1821:                                   $titles{$option}.'</label>';
 1822:                 if ($option eq 'autolimit') {
 1823:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1824:                                        $item.'_limit_default" size="1" '.
 1825:                                        'value="'.$currlimit.'" />';
 1826:                 }
 1827:                 $defcell{$item} .= '</span> ';
 1828:                 if ($option eq 'autolimit') {
 1829:                     $defcell{$item} .= $titles{'unlimited'};
 1830:                 }
 1831:             }
 1832:         } elsif ($context eq 'requestauthor') {
 1833:             my $curroption;
 1834:             if (ref($settings) eq 'HASH') {
 1835:                 $curroption = $settings->{'default'};
 1836:             }
 1837:             if (!$curroption) {
 1838:                 $curroption = 'norequest';
 1839:             }
 1840:             foreach my $option (@options) {
 1841:                 my $val = $option;
 1842:                 if ($option eq 'norequest') {
 1843:                     $val = 0;
 1844:                 }
 1845:                 my $checked = '';
 1846:                 if ($option eq $curroption) {
 1847:                     $checked = ' checked="checked"';
 1848:                 }
 1849:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1850:                               '<input type="radio" name="authorreq_default"'.
 1851:                               ' value="'.$val.'"'.$checked.' />'.
 1852:                               $titles{$option}.'</label></span>&nbsp; ';
 1853:             }
 1854:         } else {
 1855:             my $checked = 'checked="checked" ';
 1856:             if (ref($settings) eq 'HASH') {
 1857:                 if (ref($settings->{$item}) eq 'HASH') {
 1858:                     if ($settings->{$item}->{'default'} == 0) {
 1859:                         $checked = '';
 1860:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1861:                         $checked = 'checked="checked" ';
 1862:                     }
 1863:                 }
 1864:             }
 1865:             $datatable .= '<span class="LC_nobreak"><label>'.
 1866:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1867:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1868:                           '</label></span>&nbsp; ';
 1869:         }
 1870:     }
 1871:     if ($context eq 'requestcourses') {
 1872:         $datatable .= '</tr><tr>';
 1873:         foreach my $item (@usertools) {
 1874:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1875:         }
 1876:         $datatable .= '</tr></table>';
 1877:     }
 1878:     $datatable .= '</td>';
 1879:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1880:         $datatable .= '<td class="LC_right_item">'.
 1881:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1882:                       '<input type="text" name="defaultquota" value="'.
 1883:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 1884:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1885:                       '<input type="text" name="authorquota" value="'.
 1886:                       $authorquota.'" size="5" /></span></td>';
 1887:     }
 1888:     $datatable .= '</tr>';
 1889:     $typecount ++;
 1890:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1891:     $datatable .= '<tr'.$css_class.'>'.
 1892:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 1893:     if ($context eq 'requestcourses') {
 1894:         $datatable .= &mt('(overrides affiliation, if set)').
 1895:                       '</td>'.
 1896:                       '<td class="LC_left_item">'.
 1897:                       '<table><tr>';
 1898:     } else {
 1899:         $datatable .= &mt('(overrides affiliation, if checked)').
 1900:                       '</td>'.
 1901:                       '<td class="LC_left_item" colspan="2">'.
 1902:                       '<br />';
 1903:     }
 1904:     my %advcell;
 1905:     foreach my $item (@usertools) {
 1906:         if ($context eq 'requestcourses') {
 1907:             my ($curroption,$currlimit);
 1908:             if (ref($settings) eq 'HASH') {
 1909:                 if (ref($settings->{$item}) eq 'HASH') {
 1910:                     $curroption = $settings->{$item}->{'_LC_adv'};
 1911:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1912:                         $currlimit = $1;
 1913:                     }
 1914:                 }
 1915:             }
 1916:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1917:             my $checked = '';
 1918:             if ($curroption eq '') {
 1919:                 $checked = ' checked="checked"';
 1920:             }
 1921:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1922:                                '<input type="radio" name="crsreq_'.$item.
 1923:                                '__LC_adv" value=""'.$checked.' />'.
 1924:                                &mt('No override set').'</label></span>&nbsp; ';
 1925:             foreach my $option (@options) {
 1926:                 my $val = $option;
 1927:                 if ($option eq 'norequest') {
 1928:                     $val = 0;
 1929:                 }
 1930:                 if ($option eq 'validate') {
 1931:                     my $canvalidate = 0;
 1932:                     if (ref($validations{$item}) eq 'HASH') {
 1933:                         if ($validations{$item}{'_LC_adv'}) {
 1934:                             $canvalidate = 1;
 1935:                         }
 1936:                     }
 1937:                     next if (!$canvalidate);
 1938:                 }
 1939:                 my $checked = '';
 1940:                 if ($val eq $curroption) {
 1941:                     $checked = ' checked="checked"';
 1942:                 } elsif ($option eq 'autolimit') {
 1943:                     if ($curroption =~ /^autolimit/) {
 1944:                         $checked = ' checked="checked"';
 1945:                     }
 1946:                 }
 1947:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1948:                                   '<input type="radio" name="crsreq_'.$item.
 1949:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1950:                                   $titles{$option}.'</label>';
 1951:                 if ($option eq 'autolimit') {
 1952:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1953:                                        $item.'_limit__LC_adv" size="1" '.
 1954:                                        'value="'.$currlimit.'" />';
 1955:                 }
 1956:                 $advcell{$item} .= '</span> ';
 1957:                 if ($option eq 'autolimit') {
 1958:                     $advcell{$item} .= $titles{'unlimited'};
 1959:                 }
 1960:             }
 1961:         } elsif ($context eq 'requestauthor') {
 1962:             my $curroption;
 1963:             if (ref($settings) eq 'HASH') {
 1964:                 $curroption = $settings->{'_LC_adv'};
 1965:             }
 1966:             my $checked = '';
 1967:             if ($curroption eq '') {
 1968:                 $checked = ' checked="checked"';
 1969:             }
 1970:             $datatable .= '<span class="LC_nobreak"><label>'.
 1971:                           '<input type="radio" name="authorreq__LC_adv"'.
 1972:                           ' value=""'.$checked.' />'.
 1973:                           &mt('No override set').'</label></span>&nbsp; ';
 1974:             foreach my $option (@options) {
 1975:                 my $val = $option;
 1976:                 if ($option eq 'norequest') {
 1977:                     $val = 0;
 1978:                 }
 1979:                 my $checked = '';
 1980:                 if ($val eq $curroption) {
 1981:                     $checked = ' checked="checked"';
 1982:                 }
 1983:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1984:                               '<input type="radio" name="authorreq__LC_adv"'.
 1985:                               ' value="'.$val.'"'.$checked.' />'.
 1986:                               $titles{$option}.'</label></span>&nbsp; ';
 1987:             }
 1988:         } else {
 1989:             my $checked = 'checked="checked" ';
 1990:             if (ref($settings) eq 'HASH') {
 1991:                 if (ref($settings->{$item}) eq 'HASH') {
 1992:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 1993:                         $checked = '';
 1994:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 1995:                         $checked = 'checked="checked" ';
 1996:                     }
 1997:                 }
 1998:             }
 1999:             $datatable .= '<span class="LC_nobreak"><label>'.
 2000:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2001:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2002:                           '</label></span>&nbsp; ';
 2003:         }
 2004:     }
 2005:     if ($context eq 'requestcourses') {
 2006:         $datatable .= '</tr><tr>';
 2007:         foreach my $item (@usertools) {
 2008:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2009:         }
 2010:         $datatable .= '</tr></table>';
 2011:     }
 2012:     $datatable .= '</td></tr>';
 2013:     $$rowtotal += $typecount;
 2014:     return $datatable;
 2015: }
 2016: 
 2017: sub print_requestmail {
 2018:     my ($dom,$action,$settings,$rowtotal) = @_;
 2019:     my ($now,$datatable,%currapp);
 2020:     $now = time;
 2021:     if (ref($settings) eq 'HASH') {
 2022:         if (ref($settings->{'notify'}) eq 'HASH') {
 2023:             if ($settings->{'notify'}{'approval'} ne '') {
 2024:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2025:             }
 2026:         }
 2027:     }
 2028:     my $numinrow = 2;
 2029:     my $css_class;
 2030:     $css_class = ($$rowtotal%2? ' class="LC_odd_row"':'');
 2031:     my $text;
 2032:     if ($action eq 'requestcourses') {
 2033:         $text = &mt('Receive notification of course requests requiring approval');
 2034:     } elsif ($action eq 'requestauthor') {
 2035:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2036:     } else {
 2037:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2038:     }
 2039:     $datatable = '<tr'.$css_class.'>'.
 2040:                  ' <td>'.$text.'</td>'.
 2041:                  ' <td class="LC_left_item">';
 2042:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2043:                                                  $action.'notifyapproval',%currapp);
 2044:     if ($numdc > 0) {
 2045:         $datatable .= $table;
 2046:     } else {
 2047:         $datatable .= &mt('There are no active Domain Coordinators');
 2048:     }
 2049:     $datatable .='</td></tr>';
 2050:     $$rowtotal += $rows;
 2051:     return $datatable;
 2052: }
 2053: 
 2054: sub print_studentcode {
 2055:     my ($settings,$rowtotal) = @_;
 2056:     my $rownum = 0; 
 2057:     my ($output,%current);
 2058:     my @crstypes = ('official','unofficial','community','textbook');
 2059:     if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2060:         foreach my $type (@crstypes) {
 2061:             $current{$type} = $settings->{'uniquecode'}{$type};
 2062:         }
 2063:     }
 2064:     $output .= '<tr>'.
 2065:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2066:                '<td class="LC_left_item">';
 2067:     foreach my $type (@crstypes) {
 2068:         my $check = ' ';
 2069:         if ($current{$type}) {
 2070:             $check = ' checked="checked" ';
 2071:         }
 2072:         $output .= '<span class="LC_nobreak"><label>'.
 2073:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2074:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2075:     }
 2076:     $output .= '</td></tr>';
 2077:     $$rowtotal ++;
 2078:     return $output;
 2079: }
 2080: 
 2081: sub print_textbookcourses {
 2082:     my ($dom,$settings,$rowtotal) = @_;
 2083:     my $rownum = 0;
 2084:     my $css_class;
 2085:     my $itemcount = 1;
 2086:     my $maxnum = 0;
 2087:     my $bookshash;
 2088:     if (ref($settings) eq 'HASH') {
 2089:         $bookshash = $settings->{'textbooks'};
 2090:     }
 2091:     my %ordered;
 2092:     if (ref($bookshash) eq 'HASH') {
 2093:         foreach my $item (keys(%{$bookshash})) {
 2094:             if (ref($bookshash->{$item}) eq 'HASH') {
 2095:                 my $num = $bookshash->{$item}{'order'};
 2096:                 $ordered{$num} = $item;
 2097:             }
 2098:         }
 2099:     }
 2100:     my $confname = $dom.'-domainconfig';
 2101:     my $switchserver = &check_switchserver($dom,$confname);
 2102:     my $maxnum = scalar(keys(%ordered));
 2103:     my $datatable = &textbookcourses_javascript(\%ordered);
 2104:     if (keys(%ordered)) {
 2105:         my @items = sort { $a <=> $b } keys(%ordered);
 2106:         for (my $i=0; $i<@items; $i++) {
 2107:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2108:             my $key = $ordered{$items[$i]};
 2109:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2110:             my $coursetitle = $coursehash{'description'};
 2111:             my ($subject,$title,$author,$image,$imgsrc,$cdom,$cnum);
 2112:             if (ref($bookshash->{$key}) eq 'HASH') {
 2113:                 $subject = $bookshash->{$key}->{'subject'};
 2114:                 $title = $bookshash->{$key}->{'title'};
 2115:                 $author = $bookshash->{$key}->{'author'};
 2116:                 $image = $bookshash->{$key}->{'image'};
 2117:                 if ($image ne '') {
 2118:                     my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2119:                     my $imagethumb = "$path/tn-".$imagefile;
 2120:                     $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2121:                 }
 2122:             }
 2123:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$key'".');"';
 2124:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2125:                          .'<select name="'.$key.'"'.$chgstr.'>';
 2126:             for (my $k=0; $k<=$maxnum; $k++) {
 2127:                 my $vpos = $k+1;
 2128:                 my $selstr;
 2129:                 if ($k == $i) {
 2130:                     $selstr = ' selected="selected" ';
 2131:                 }
 2132:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2133:             }
 2134:             $datatable .= '</select>'.('&nbsp;'x2).
 2135:                 '<label><input type="checkbox" name="book_del" value="'.$key.'" />'.
 2136:                 &mt('Delete?').'</label></span></td>'.
 2137:                 '<td colspan="2">'.
 2138:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="book_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2139:                 ('&nbsp;'x2).
 2140:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="book_title_'.$i.'" value="'.$title.'" /></span> '.
 2141:                 ('&nbsp;'x2).
 2142:                 '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="book_author_'.$i.'" value="'.$author.'" /></span> '.
 2143:                 ('&nbsp;'x2).
 2144:                 '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2145:             if ($image) {
 2146:                 $datatable .= '<span class="LC_nobreak">'.
 2147:                               $imgsrc.
 2148:                               '<label><input type="checkbox" name="book_image_del"'.
 2149:                               ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2150:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2151:             }
 2152:             if ($switchserver) {
 2153:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2154:             } else {
 2155:                 $datatable .= '<input type="file" name="book_image_'.$i.'" value="" />';
 2156:             }
 2157:             $datatable .= '<input type="hidden" name="book_id_'.$i.'" value="'.$key.'" /></span> '.
 2158:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2159:                           $coursetitle.'</span></td></tr>'."\n";
 2160:             $itemcount ++;
 2161:         }
 2162:     }
 2163:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2164:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'addbook_pos'".');"';
 2165:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2166:                   '<input type="hidden" name="book_maxnum" value="'.$maxnum.'" />'."\n".
 2167:                   '<select name="addbook_pos"'.$chgstr.'>';
 2168:     for (my $k=0; $k<$maxnum+1; $k++) {
 2169:         my $vpos = $k+1;
 2170:         my $selstr;
 2171:         if ($k == $maxnum) {
 2172:             $selstr = ' selected="selected" ';
 2173:         }
 2174:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2175:     }
 2176:     $datatable .= '</select>&nbsp;'."\n".
 2177:                   '<input type="checkbox" name="addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2178:                   '<td colspan="2">'.
 2179:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="addbook_subject" value="" /></span> '."\n".
 2180:                   ('&nbsp;'x2).
 2181:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="addbook_title" value="" /></span> '."\n".
 2182:                   ('&nbsp;'x2).
 2183:                   '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="addbook_author" value="" /></span> '."\n".
 2184:                   ('&nbsp;'x2).
 2185:                   '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2186:     if ($switchserver) {
 2187:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2188:     } else {
 2189:         $datatable .= '<input type="file" name="addbook_image" value="" />';
 2190:     }
 2191:     $datatable .= '</span>'."\n".
 2192:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2193:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},'addbook_cdom').
 2194:                   '<input type="text" size="25" name="addbook_cnum" value="" />'.
 2195:                   &Apache::loncommon::selectcourse_link
 2196:                       ('display','addbook_cnum','addbook_cdom',undef,undef,undef,'Course');
 2197:                   '</span></td>'."\n".
 2198:                   '</tr>'."\n";
 2199:     $itemcount ++;
 2200:     return $datatable;
 2201: }
 2202: 
 2203: sub textbookcourses_javascript {
 2204:     my ($textbooks) = @_;
 2205:     return unless(ref($textbooks) eq 'HASH');
 2206:     my $num = scalar(keys(%{$textbooks}));
 2207:     my @jsarray;
 2208:     foreach my $item (sort {$a <=> $b } (keys(%{$textbooks}))) {
 2209:         push(@jsarray,$textbooks->{$item});
 2210:     }
 2211:     my $jstext = '    var textbooks = Array('."'".join("','",@jsarray)."'".');'."\n";
 2212:     return <<"ENDSCRIPT";
 2213: <script type="text/javascript">
 2214: // <![CDATA[
 2215: function reorderBooks(form,item) {
 2216:     var changedVal;
 2217: $jstext 
 2218:     var newpos = 'addbook_pos';
 2219:     var current = new Array;
 2220:     var maxh = 1 + $num;
 2221:     var current = new Array;
 2222:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2223:     if (item == newpos) {
 2224:         changedVal = newitemVal;
 2225:     } else {
 2226:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2227:         current[newitemVal] = newpos;
 2228:     }
 2229:     for (var i=0; i<textbooks.length; i++) {
 2230:         var elementName = textbooks[i];
 2231:         if (elementName != item) {
 2232:             if (form.elements[elementName]) {
 2233:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2234:                 current[currVal] = elementName;
 2235:             }
 2236:         }
 2237:     }
 2238:     var oldVal;
 2239:     for (var j=0; j<maxh; j++) {
 2240:         if (current[j] == undefined) {
 2241:             oldVal = j;
 2242:         }
 2243:     }
 2244:     if (oldVal < changedVal) {
 2245:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2246:            var elementName = current[k];
 2247:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2248:         }
 2249:     } else {
 2250:         for (var k=changedVal; k<oldVal; k++) {
 2251:             var elementName = current[k];
 2252:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2253:         }
 2254:     }
 2255:     return;
 2256: }
 2257: 
 2258: // ]]>
 2259: </script>
 2260: 
 2261: ENDSCRIPT
 2262: }
 2263: 
 2264: sub print_autoenroll {
 2265:     my ($dom,$settings,$rowtotal) = @_;
 2266:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2267:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 2268:     if (ref($settings) eq 'HASH') {
 2269:         if (exists($settings->{'run'})) {
 2270:             if ($settings->{'run'} eq '0') {
 2271:                 $runoff = ' checked="checked" ';
 2272:                 $runon = ' ';
 2273:             } else {
 2274:                 $runon = ' checked="checked" ';
 2275:                 $runoff = ' ';
 2276:             }
 2277:         } else {
 2278:             if ($autorun) {
 2279:                 $runon = ' checked="checked" ';
 2280:                 $runoff = ' ';
 2281:             } else {
 2282:                 $runoff = ' checked="checked" ';
 2283:                 $runon = ' ';
 2284:             }
 2285:         }
 2286:         if (exists($settings->{'co-owners'})) {
 2287:             if ($settings->{'co-owners'} eq '0') {
 2288:                 $coownersoff = ' checked="checked" ';
 2289:                 $coownerson = ' ';
 2290:             } else {
 2291:                 $coownerson = ' checked="checked" ';
 2292:                 $coownersoff = ' ';
 2293:             }
 2294:         } else {
 2295:             $coownersoff = ' checked="checked" ';
 2296:             $coownerson = ' ';
 2297:         }
 2298:         if (exists($settings->{'sender_domain'})) {
 2299:             $defdom = $settings->{'sender_domain'};
 2300:         }
 2301:     } else {
 2302:         if ($autorun) {
 2303:             $runon = ' checked="checked" ';
 2304:             $runoff = ' ';
 2305:         } else {
 2306:             $runoff = ' checked="checked" ';
 2307:             $runon = ' ';
 2308:         }
 2309:     }
 2310:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2311:     my $notif_sender;
 2312:     if (ref($settings) eq 'HASH') {
 2313:         $notif_sender = $settings->{'sender_uname'};
 2314:     }
 2315:     my $datatable='<tr class="LC_odd_row">'.
 2316:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2317:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2318:                   '<input type="radio" name="autoenroll_run"'.
 2319:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2320:                   '<label><input type="radio" name="autoenroll_run"'.
 2321:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2322:                   '</tr><tr>'.
 2323:                   '<td>'.&mt('Notification messages - sender').
 2324:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2325:                   &mt('username').':&nbsp;'.
 2326:                   '<input type="text" name="sender_uname" value="'.
 2327:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2328:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2329:                   '<tr class="LC_odd_row">'.
 2330:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2331:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2332:                   '<input type="radio" name="autoassign_coowners"'.
 2333:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2334:                   '<label><input type="radio" name="autoassign_coowners"'.
 2335:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2336:                   '</tr>';
 2337:     $$rowtotal += 3;
 2338:     return $datatable;
 2339: }
 2340: 
 2341: sub print_autoupdate {
 2342:     my ($position,$dom,$settings,$rowtotal) = @_;
 2343:     my $datatable;
 2344:     if ($position eq 'top') {
 2345:         my $updateon = ' ';
 2346:         my $updateoff = ' checked="checked" ';
 2347:         my $classlistson = ' ';
 2348:         my $classlistsoff = ' checked="checked" ';
 2349:         if (ref($settings) eq 'HASH') {
 2350:             if ($settings->{'run'} eq '1') {
 2351:                 $updateon = $updateoff;
 2352:                 $updateoff = ' ';
 2353:             }
 2354:             if ($settings->{'classlists'} eq '1') {
 2355:                 $classlistson = $classlistsoff;
 2356:                 $classlistsoff = ' ';
 2357:             }
 2358:         }
 2359:         my %title = (
 2360:                    run => 'Auto-update active?',
 2361:                    classlists => 'Update information in classlists?',
 2362:                     );
 2363:         $datatable = '<tr class="LC_odd_row">'. 
 2364:                   '<td>'.&mt($title{'run'}).'</td>'.
 2365:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2366:                   '<input type="radio" name="autoupdate_run"'.
 2367:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2368:                   '<label><input type="radio" name="autoupdate_run"'.
 2369:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2370:                   '</tr><tr>'.
 2371:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2372:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2373:                   '<label><input type="radio" name="classlists"'.
 2374:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2375:                   '<label><input type="radio" name="classlists"'.
 2376:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2377:                   '</tr>';
 2378:         $$rowtotal += 2;
 2379:     } elsif ($position eq 'middle') {
 2380:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2381:         my $numinrow = 3;
 2382:         my $locknamesettings;
 2383:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2384:                                      $dom,$numinrow,$othertitle,
 2385:                                     'lockablenames');
 2386:         $$rowtotal ++;
 2387:     } else {
 2388:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2389:         my @fields = ('lastname','firstname','middlename','generation',
 2390:                       'permanentemail','id');
 2391:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2392:         my $numrows = 0;
 2393:         if (ref($types) eq 'ARRAY') {
 2394:             if (@{$types} > 0) {
 2395:                 $datatable = 
 2396:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2397:                                          \@fields,$types,\$numrows);
 2398:                     $$rowtotal += @{$types}; 
 2399:             }
 2400:         }
 2401:         $datatable .= 
 2402:             &usertype_update_row($settings,{'default' => $othertitle},
 2403:                                  \%fieldtitles,\@fields,['default'],
 2404:                                  \$numrows);
 2405:         $$rowtotal ++;     
 2406:     }
 2407:     return $datatable;
 2408: }
 2409: 
 2410: sub print_autocreate {
 2411:     my ($dom,$settings,$rowtotal) = @_;
 2412:     my (%createon,%createoff,%currhash);
 2413:     my @types = ('xml','req');
 2414:     if (ref($settings) eq 'HASH') {
 2415:         foreach my $item (@types) {
 2416:             $createoff{$item} = ' checked="checked" ';
 2417:             $createon{$item} = ' ';
 2418:             if (exists($settings->{$item})) {
 2419:                 if ($settings->{$item}) {
 2420:                     $createon{$item} = ' checked="checked" ';
 2421:                     $createoff{$item} = ' ';
 2422:                 }
 2423:             }
 2424:         }
 2425:         if ($settings->{'xmldc'} ne '') {
 2426:             $currhash{$settings->{'xmldc'}} = 1;
 2427:         }
 2428:     } else {
 2429:         foreach my $item (@types) {
 2430:             $createoff{$item} = ' checked="checked" ';
 2431:             $createon{$item} = ' ';
 2432:         }
 2433:     }
 2434:     $$rowtotal += 2;
 2435:     my $numinrow = 2;
 2436:     my $datatable='<tr class="LC_odd_row">'.
 2437:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2438:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2439:                   '<input type="radio" name="autocreate_xml"'.
 2440:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2441:                   '<label><input type="radio" name="autocreate_xml"'.
 2442:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2443:                   '</td></tr><tr>'.
 2444:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2445:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2446:                   '<input type="radio" name="autocreate_req"'.
 2447:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2448:                   '<label><input type="radio" name="autocreate_req"'.
 2449:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2450:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2451:                                                    'autocreate_xmldc',%currhash);
 2452:     if ($numdc > 1) {
 2453:         $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 2454:                       &mt('Course creation processed as: (choose Dom. Coord.)').
 2455:                       '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 2456:     } else {
 2457:         $datatable .= $dctable.'</td></tr>';
 2458:     }
 2459:     $$rowtotal += $rows;
 2460:     return $datatable;
 2461: }
 2462: 
 2463: sub print_directorysrch {
 2464:     my ($dom,$settings,$rowtotal) = @_;
 2465:     my $srchon = ' ';
 2466:     my $srchoff = ' checked="checked" ';
 2467:     my ($exacton,$containson,$beginson);
 2468:     my $localon = ' ';
 2469:     my $localoff = ' checked="checked" ';
 2470:     if (ref($settings) eq 'HASH') {
 2471:         if ($settings->{'available'} eq '1') {
 2472:             $srchon = $srchoff;
 2473:             $srchoff = ' ';
 2474:         }
 2475:         if ($settings->{'localonly'} eq '1') {
 2476:             $localon = $localoff;
 2477:             $localoff = ' ';
 2478:         }
 2479:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2480:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2481:                 if ($type eq 'exact') {
 2482:                     $exacton = ' checked="checked" ';
 2483:                 } elsif ($type eq 'contains') {
 2484:                     $containson = ' checked="checked" ';
 2485:                 } elsif ($type eq 'begins') {
 2486:                     $beginson = ' checked="checked" ';
 2487:                 }
 2488:             }
 2489:         } else {
 2490:             if ($settings->{'searchtypes'} eq 'exact') {
 2491:                 $exacton = ' checked="checked" ';
 2492:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2493:                 $containson = ' checked="checked" ';
 2494:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2495:                 $exacton = ' checked="checked" ';
 2496:                 $containson = ' checked="checked" ';
 2497:             }
 2498:         }
 2499:     }
 2500:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2501:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2502: 
 2503:     my $numinrow = 4;
 2504:     my $cansrchrow = 0;
 2505:     my $datatable='<tr class="LC_odd_row">'.
 2506:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2507:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2508:                   '<input type="radio" name="dirsrch_available"'.
 2509:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2510:                   '<label><input type="radio" name="dirsrch_available"'.
 2511:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2512:                   '</tr><tr>'.
 2513:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2514:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2515:                   '<input type="radio" name="dirsrch_localonly"'.
 2516:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2517:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2518:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2519:                   '</tr>';
 2520:     $$rowtotal += 2;
 2521:     if (ref($usertypes) eq 'HASH') {
 2522:         if (keys(%{$usertypes}) > 0) {
 2523:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2524:                                          $numinrow,$othertitle,'cansearch');
 2525:             $cansrchrow = 1;
 2526:         }
 2527:     }
 2528:     if ($cansrchrow) {
 2529:         $$rowtotal ++;
 2530:         $datatable .= '<tr>';
 2531:     } else {
 2532:         $datatable .= '<tr class="LC_odd_row">';
 2533:     }
 2534:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2535:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2536:     foreach my $title (@{$titleorder}) {
 2537:         if (defined($searchtitles->{$title})) {
 2538:             my $check = ' ';
 2539:             if (ref($settings) eq 'HASH') {
 2540:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2541:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2542:                         $check = ' checked="checked" ';
 2543:                     }
 2544:                 }
 2545:             }
 2546:             $datatable .= '<td class="LC_left_item">'.
 2547:                           '<span class="LC_nobreak"><label>'.
 2548:                           '<input type="checkbox" name="searchby" '.
 2549:                           'value="'.$title.'"'.$check.'/>'.
 2550:                           $searchtitles->{$title}.'</label></span></td>';
 2551:         }
 2552:     }
 2553:     $datatable .= '</tr></table></td></tr>';
 2554:     $$rowtotal ++;
 2555:     if ($cansrchrow) {
 2556:         $datatable .= '<tr class="LC_odd_row">';
 2557:     } else {
 2558:         $datatable .= '<tr>';
 2559:     }
 2560:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2561:                   '<td class="LC_left_item" colspan="2">'.
 2562:                   '<span class="LC_nobreak"><label>'.
 2563:                   '<input type="checkbox" name="searchtypes" '.
 2564:                   $exacton.' value="exact" />'.&mt('Exact match').
 2565:                   '</label>&nbsp;'.
 2566:                   '<label><input type="checkbox" name="searchtypes" '.
 2567:                   $beginson.' value="begins" />'.&mt('Begins with').
 2568:                   '</label>&nbsp;'.
 2569:                   '<label><input type="checkbox" name="searchtypes" '.
 2570:                   $containson.' value="contains" />'.&mt('Contains').
 2571:                   '</label></span></td></tr>';
 2572:     $$rowtotal ++;
 2573:     return $datatable;
 2574: }
 2575: 
 2576: sub print_contacts {
 2577:     my ($dom,$settings,$rowtotal) = @_;
 2578:     my $datatable;
 2579:     my @contacts = ('adminemail','supportemail');
 2580:     my (%checked,%to,%otheremails,%bccemails);
 2581:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2582:                     'requestsmail','updatesmail','idconflictsmail');
 2583:     foreach my $type (@mailings) {
 2584:         $otheremails{$type} = '';
 2585:     }
 2586:     $bccemails{'helpdeskmail'} = '';
 2587:     if (ref($settings) eq 'HASH') {
 2588:         foreach my $item (@contacts) {
 2589:             if (exists($settings->{$item})) {
 2590:                 $to{$item} = $settings->{$item};
 2591:             }
 2592:         }
 2593:         foreach my $type (@mailings) {
 2594:             if (exists($settings->{$type})) {
 2595:                 if (ref($settings->{$type}) eq 'HASH') {
 2596:                     foreach my $item (@contacts) {
 2597:                         if ($settings->{$type}{$item}) {
 2598:                             $checked{$type}{$item} = ' checked="checked" ';
 2599:                         }
 2600:                     }
 2601:                     $otheremails{$type} = $settings->{$type}{'others'};
 2602:                     if ($type eq 'helpdeskmail') {
 2603:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2604:                     }
 2605:                 }
 2606:             } elsif ($type eq 'lonstatusmail') {
 2607:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2608:             }
 2609:         }
 2610:     } else {
 2611:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2612:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2613:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2614:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2615:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2616:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2617:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2618:         $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2619:         $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 2620:     }
 2621:     my ($titles,$short_titles) = &contact_titles();
 2622:     my $rownum = 0;
 2623:     my $css_class;
 2624:     foreach my $item (@contacts) {
 2625:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2626:         $datatable .= '<tr'.$css_class.'>'. 
 2627:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2628:                   '</span></td><td class="LC_right_item">'.
 2629:                   '<input type="text" name="'.$item.'" value="'.
 2630:                   $to{$item}.'" /></td></tr>';
 2631:         $rownum ++;
 2632:     }
 2633:     foreach my $type (@mailings) {
 2634:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2635:         $datatable .= '<tr'.$css_class.'>'.
 2636:                       '<td><span class="LC_nobreak">'.
 2637:                       $titles->{$type}.': </span></td>'.
 2638:                       '<td class="LC_left_item">'.
 2639:                       '<span class="LC_nobreak">';
 2640:         foreach my $item (@contacts) {
 2641:             $datatable .= '<label>'.
 2642:                           '<input type="checkbox" name="'.$type.'"'.
 2643:                           $checked{$type}{$item}.
 2644:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2645:                           '</label>&nbsp;';
 2646:         }
 2647:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2648:                       '<input type="text" name="'.$type.'_others" '.
 2649:                       'value="'.$otheremails{$type}.'"  />';
 2650:         if ($type eq 'helpdeskmail') {
 2651:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2652:                           '<input type="text" name="'.$type.'_bcc" '.
 2653:                           'value="'.$bccemails{$type}.'"  />';
 2654:         }
 2655:         $datatable .= '</td></tr>'."\n";
 2656:         $rownum ++;
 2657:     }
 2658:     my %choices;
 2659:     $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 2660:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2661:                                    &mt('LON-CAPA core group - MSU'),600,500));
 2662:     $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 2663:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2664:                                     &mt('LON-CAPA core group - MSU'),600,500));
 2665:     my @toggles = ('reporterrors','reportupdates');
 2666:     my %defaultchecked = ('reporterrors'  => 'on',
 2667:                           'reportupdates' => 'on');
 2668:     (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2669:                                                \%choices,$rownum);
 2670:     $datatable .= $reports;
 2671:     $$rowtotal += $rownum;
 2672:     return $datatable;
 2673: }
 2674: 
 2675: sub print_helpsettings {
 2676:     my ($dom,$confname,$settings,$rowtotal) = @_;
 2677:     my ($datatable,$itemcount);
 2678:     $itemcount = 1;
 2679:     my (%choices,%defaultchecked,@toggles);
 2680:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2681:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2682:                                  &mt('LON-CAPA bug tracker'),600,500));
 2683:     %defaultchecked = ('submitbugs' => 'on');
 2684:     @toggles = ('submitbugs',);
 2685: 
 2686:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2687:                                                  \%choices,$itemcount);
 2688:     return $datatable;
 2689: }
 2690: 
 2691: sub radiobutton_prefs {
 2692:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 2693:         $additional) = @_;
 2694:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2695:                    (ref($choices) eq 'HASH'));
 2696: 
 2697:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2698: 
 2699:     foreach my $item (@{$toggles}) {
 2700:         if ($defaultchecked->{$item} eq 'on') {
 2701:             $checkedon{$item} = ' checked="checked" ';
 2702:             $checkedoff{$item} = ' ';
 2703:         } elsif ($defaultchecked->{$item} eq 'off') {
 2704:             $checkedoff{$item} = ' checked="checked" ';
 2705:             $checkedon{$item} = ' ';
 2706:         }
 2707:     }
 2708:     if (ref($settings) eq 'HASH') {
 2709:         foreach my $item (@{$toggles}) {
 2710:             if ($settings->{$item} eq '1') {
 2711:                 $checkedon{$item} =  ' checked="checked" ';
 2712:                 $checkedoff{$item} = ' ';
 2713:             } elsif ($settings->{$item} eq '0') {
 2714:                 $checkedoff{$item} =  ' checked="checked" ';
 2715:                 $checkedon{$item} = ' ';
 2716:             }
 2717:         }
 2718:     }
 2719:     if ($onclick) {
 2720:         $onclick = ' onclick="'.$onclick.'"';
 2721:     }
 2722:     foreach my $item (@{$toggles}) {
 2723:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2724:         $datatable .=
 2725:             '<tr'.$css_class.'><td valign="top">'.
 2726:             '<span class="LC_nobreak">'.$choices->{$item}.
 2727:             '</span></td>'.
 2728:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2729:             '<label><input type="radio" name="'.
 2730:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 2731:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2732:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 2733:             '</span>'.$additional.
 2734:             '</td>'.
 2735:             '</tr>';
 2736:         $itemcount ++;
 2737:     }
 2738:     return ($datatable,$itemcount);
 2739: }
 2740: 
 2741: sub print_coursedefaults {
 2742:     my ($position,$dom,$settings,$rowtotal) = @_;
 2743:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 2744:     my $itemcount = 1;
 2745:     my %choices =  &Apache::lonlocal::texthash (
 2746:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 2747:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 2748:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2749:         coursecredits        => 'Credits can be specified for courses',
 2750:     );
 2751:     my %staticdefaults = (
 2752:                            anonsurvey_threshold => 10,
 2753:                            uploadquota          => 500,
 2754:                          );
 2755:     if ($position eq 'top') {
 2756:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2757:         @toggles = ('canuse_pdfforms');
 2758:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2759:                                                  \%choices,$itemcount);
 2760:     } else {
 2761:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2762:         my ($currdefresponder,$def_official_credits,$def_unofficial_credits,$def_textbook_credits,
 2763:             %curruploadquota);
 2764:         my $currusecredits = 0;
 2765:         my @types = ('official','unofficial','community','textbook');
 2766:         if (ref($settings) eq 'HASH') {
 2767:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2768:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 2769:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 2770:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 2771:                 }
 2772:             }
 2773:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 2774:                 $def_official_credits = $settings->{'coursecredits'}->{'official'};
 2775:                 $def_unofficial_credits = $settings->{'coursecredits'}->{'unofficial'};
 2776:                 $def_textbook_credits = $settings->{'coursecredits'}->{'textbook'};
 2777:                 if (($def_official_credits ne '') || ($def_unofficial_credits ne '') ||
 2778:                     ($def_textbook_credits ne '')) {
 2779:                     $currusecredits = 1;
 2780:                 }
 2781:             }
 2782:         }
 2783:         if (!$currdefresponder) {
 2784:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 2785:         } elsif ($currdefresponder < 1) {
 2786:             $currdefresponder = 1;
 2787:         }
 2788:         foreach my $type (@types) {
 2789:             if ($curruploadquota{$type} eq '') {
 2790:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 2791:             }
 2792:         }
 2793:         $datatable .=
 2794:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2795:                 $choices{'anonsurvey_threshold'}.
 2796:                 '</span></td>'.
 2797:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2798:                 '<input type="text" name="anonsurvey_threshold"'.
 2799:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2800:                 '</td></tr>'."\n";
 2801:         $itemcount ++;
 2802:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2803:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2804:                       $choices{'uploadquota'}.
 2805:                       '</span></td>'.
 2806:                       '<td align="right" class="LC_right_item">'.
 2807:                       '<table><tr>';
 2808:         foreach my $type (@types) {
 2809:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 2810:                            '<input type="text" name="uploadquota_'.$type.'"'.
 2811:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 2812:         }
 2813:         $datatable .= '</tr></table></td></tr>'."\n";
 2814:         $itemcount ++;
 2815:         my $onclick = 'toggleCredits(this.form);';
 2816:         my $display = 'none';
 2817:         if ($currusecredits) {
 2818:             $display = 'block';
 2819:         }
 2820:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 2821:                          '<span class="LC_nobreak">'.
 2822:                          &mt('Default credits for official courses [_1]',
 2823:                          '<input type="text" name="official_credits" value="'.
 2824:                          $def_official_credits.'" size="3" />').
 2825:                          '</span><br />'.
 2826:                          '<span class="LC_nobreak">'.
 2827:                          &mt('Default credits for unofficial courses [_1]',
 2828:                          '<input type="text" name="unofficial_credits" value="'.
 2829:                          $def_unofficial_credits.'" size="3" />').
 2830:                          '</span><br />'.
 2831:                          '<span class="LC_nobreak">'.
 2832:                          &mt('Default credits for textbook courses [_1]',
 2833:                          '<input type="text" name="textbook_credits" value="'.
 2834:                          $def_textbook_credits.'" size="3" />').
 2835:                          '</span></div>'."\n";
 2836:         %defaultchecked = ('coursecredits' => 'off');
 2837:         @toggles = ('coursecredits');
 2838:         my $current = {
 2839:                         'coursecredits' => $currusecredits,
 2840:                       };
 2841:         (my $table,$itemcount) =
 2842:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 2843:                                \%choices,$itemcount,$onclick,$additional);
 2844:         $datatable .= $table;
 2845:         $itemcount ++;
 2846:     }
 2847:     $$rowtotal += $itemcount;
 2848:     return $datatable;
 2849: }
 2850: 
 2851: sub print_selfenrollment {
 2852:     my ($position,$dom,$settings,$rowtotal) = @_;
 2853:     my ($css_class,$datatable);
 2854:     my $itemcount = 1;
 2855:     my @types = ('official','unofficial','community','textbook');
 2856:     if (($position eq 'top') || ($position eq 'middle')) {
 2857:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 2858:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 2859:         my @rows;
 2860:         my $key;
 2861:         if ($position eq 'top') {
 2862:             $key = 'admin'; 
 2863:             if (ref($rowsref) eq 'ARRAY') {
 2864:                 @rows = @{$rowsref};
 2865:             }
 2866:         } elsif ($position eq 'middle') {
 2867:             $key = 'default';
 2868:             @rows = ('types','registered','approval','limit');
 2869:         }
 2870:         foreach my $row (@rows) {
 2871:             if (defined($titlesref->{$row})) {
 2872:                 $itemcount ++;
 2873:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2874:                 $datatable .= '<tr'.$css_class.'>'.
 2875:                               '<td>'.$titlesref->{$row}.'</td>'.
 2876:                               '<td class="LC_left_item">'.
 2877:                               '<table><tr>';
 2878:                 my (%current,%currentcap);
 2879:                 if (ref($settings) eq 'HASH') {
 2880:                     if (ref($settings->{$key}) eq 'HASH') {
 2881:                         foreach my $type (@types) {
 2882:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 2883:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 2884:                             }
 2885:                             if (($row eq 'limit') && ($key eq 'default')) {
 2886:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 2887:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 2888:                                 }
 2889:                             }
 2890:                         }
 2891:                     }
 2892:                 }
 2893:                 my %roles = (
 2894:                              '0' => &Apache::lonnet::plaintext('dc'),
 2895:                             ); 
 2896:             
 2897:                 foreach my $type (@types) {
 2898:                     unless (($row eq 'registered') && ($key eq 'default')) {
 2899:                         $datatable .= '<th>'.&mt($type).'</th>';
 2900:                     }
 2901:                 }
 2902:                 unless (($row eq 'registered') && ($key eq 'default')) {
 2903:                     $datatable .= '</tr><tr>';
 2904:                 }
 2905:                 foreach my $type (@types) {
 2906:                     if ($type eq 'community') {
 2907:                         $roles{'1'} = &mt('Community personnel');
 2908:                     } else {
 2909:                         $roles{'1'} = &mt('Course personnel');
 2910:                     }
 2911:                     $datatable .= '<td style="vertical-align: top">';
 2912:                     if ($position eq 'top') {
 2913:                         my %checked;
 2914:                         if ($current{$type} eq '0') {
 2915:                             $checked{'0'} = ' checked="checked"';
 2916:                         } else {
 2917:                             $checked{'1'} = ' checked="checked"';
 2918:                         }
 2919:                         foreach my $role ('1','0') {
 2920:                             $datatable .= '<span class="LC_nobreak"><label>'.
 2921:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 2922:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 2923:                                           $roles{$role}.'</label></span> ';
 2924:                         }
 2925:                     } else {
 2926:                         if ($row eq 'types') {
 2927:                             my %checked;
 2928:                             if ($current{$type} =~ /^(all|dom)$/) {
 2929:                                 $checked{$1} = ' checked="checked"';
 2930:                             } else {
 2931:                                 $checked{''} = ' checked="checked"';
 2932:                             }
 2933:                             foreach my $val ('','dom','all') {
 2934:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 2935:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 2936:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 2937:                             }
 2938:                         } elsif ($row eq 'registered') {
 2939:                             my %checked;
 2940:                             if ($current{$type} eq '1') {
 2941:                                 $checked{'1'} = ' checked="checked"';
 2942:                             } else {
 2943:                                 $checked{'0'} = ' checked="checked"';
 2944:                             }
 2945:                             foreach my $val ('0','1') {
 2946:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 2947:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 2948:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 2949:                             }
 2950:                         } elsif ($row eq 'approval') {
 2951:                             my %checked;
 2952:                             if ($current{$type} =~ /^([12])$/) {
 2953:                                 $checked{$1} = ' checked="checked"';
 2954:                             } else {
 2955:                                 $checked{'0'} = ' checked="checked"';
 2956:                             }
 2957:                             for my $val (0..2) {
 2958:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 2959:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 2960:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 2961:                             }
 2962:                         } elsif ($row eq 'limit') {
 2963:                             my %checked;
 2964:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 2965:                                 $checked{$1} = ' checked="checked"';
 2966:                             } else {
 2967:                                 $checked{'none'} = ' checked="checked"';
 2968:                             }
 2969:                             my $cap;
 2970:                             if ($currentcap{$type} =~ /^\d+$/) {
 2971:                                 $cap = $currentcap{$type};
 2972:                             }
 2973:                             foreach my $val ('none','allstudents','selfenrolled') {
 2974:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 2975:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 2976:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 2977:                             }
 2978:                             $datatable .= '<br />'.
 2979:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 2980:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 2981:                                           '</span>'; 
 2982:                         }
 2983:                     }
 2984:                     $datatable .= '</td>';
 2985:                 }
 2986:                 $datatable .= '</tr>';
 2987:             }
 2988:             $datatable .= '</table></td></tr>';
 2989:         }
 2990:     } elsif ($position eq 'bottom') {
 2991:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 2992:     }
 2993:     $$rowtotal += $itemcount;
 2994:     return $datatable;
 2995: }
 2996: 
 2997: sub print_validation_rows {
 2998:     my ($caller,$dom,$settings,$rowtotal) = @_;
 2999:     my ($itemsref,$namesref,$fieldsref);
 3000:     if ($caller eq 'selfenroll') { 
 3001:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 3002:     } elsif ($caller eq 'requestcourses') {
 3003:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 3004:     }
 3005:     my %currvalidation;
 3006:     if (ref($settings) eq 'HASH') {
 3007:         if (ref($settings->{'validation'}) eq 'HASH') {
 3008:             %currvalidation = %{$settings->{'validation'}};
 3009:         }
 3010:     }
 3011:     my $datatable;
 3012:     my $itemcount = 0;
 3013:     foreach my $item (@{$itemsref}) {
 3014:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3015:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3016:                       $namesref->{$item}.
 3017:                       '</span></td>'.
 3018:                       '<td class="LC_left_item">';
 3019:         if (($item eq 'url') || ($item eq 'button')) {
 3020:             $datatable .= '<span class="LC_nobreak">'.
 3021:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 3022:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 3023:         } elsif ($item eq 'fields') {
 3024:             my @currfields;
 3025:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 3026:                 @currfields = @{$currvalidation{$item}};
 3027:             }
 3028:             foreach my $field (@{$fieldsref}) {
 3029:                 my $check = '';
 3030:                 if (grep(/^\Q$field\E$/,@currfields)) {
 3031:                     $check = ' checked="checked"';
 3032:                 }
 3033:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3034:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 3035:                               ' value="'.$field.'"'.$check.' />'.$field.
 3036:                               '</label></span> ';
 3037:             }
 3038:         } elsif ($item eq 'markup') {
 3039:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5" wrap="soft">'.
 3040:                            $currvalidation{$item}.
 3041:                               '</textarea>';
 3042:         }
 3043:         $datatable .= '</td></tr>'."\n";
 3044:         if (ref($rowtotal)) {
 3045:             $itemcount ++;
 3046:         }
 3047:     }
 3048:     if ($caller eq 'requestcourses') {
 3049:         my %currhash;
 3050:         if (ref($settings->{'validation'}) eq 'HASH') {
 3051:             if ($settings->{'validation'}{'dc'} ne '') {
 3052:                 $currhash{$settings->{'validation'}{'dc'}} = 1;
 3053:             }
 3054:         }
 3055:         my $numinrow = 2;
 3056:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 3057:                                                        'validationdc',%currhash);
 3058:         if ($numdc > 1) {
 3059:             $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 3060:                           &mt('Course creation processed as: (choose Dom. Coord.)').
 3061:                           '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 3062:         } else {
 3063:             $datatable .= $dctable.'</td></tr>';
 3064:         }
 3065:         $itemcount ++;
 3066:     }
 3067:     if (ref($rowtotal)) {
 3068:         $$rowtotal += $itemcount;
 3069:     }
 3070:     return $datatable;
 3071: }
 3072: 
 3073: sub print_usersessions {
 3074:     my ($position,$dom,$settings,$rowtotal) = @_;
 3075:     my ($css_class,$datatable,%checked,%choices);
 3076:     my (%by_ip,%by_location,@intdoms);
 3077:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 3078: 
 3079:     my @alldoms = &Apache::lonnet::all_domains();
 3080:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 3081:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3082:     my %altids = &id_for_thisdom(%servers);
 3083:     my $itemcount = 1;
 3084:     if ($position eq 'top') {
 3085:         if (keys(%serverhomes) > 1) {
 3086:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 3087:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 3088:         } else {
 3089:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3090:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 3091:         }
 3092:     } else {
 3093:         if (keys(%by_location) == 0) {
 3094:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3095:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 3096:         } else {
 3097:             my %lt = &usersession_titles();
 3098:             my $numinrow = 5;
 3099:             my $prefix;
 3100:             my @types;
 3101:             if ($position eq 'bottom') {
 3102:                 $prefix = 'remote';
 3103:                 @types = ('version','excludedomain','includedomain');
 3104:             } else {
 3105:                 $prefix = 'hosted';
 3106:                 @types = ('excludedomain','includedomain');
 3107:             }
 3108:             my (%current,%checkedon,%checkedoff);
 3109:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 3110:             my @locations = sort(keys(%by_location));
 3111:             foreach my $type (@types) {
 3112:                 $checkedon{$type} = '';
 3113:                 $checkedoff{$type} = ' checked="checked"';
 3114:             }
 3115:             if (ref($settings) eq 'HASH') {
 3116:                 if (ref($settings->{$prefix}) eq 'HASH') {
 3117:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 3118:                         $current{$key} = $settings->{$prefix}{$key};
 3119:                         if ($key eq 'version') {
 3120:                             if ($current{$key} ne '') {
 3121:                                 $checkedon{$key} = ' checked="checked"';
 3122:                                 $checkedoff{$key} = '';
 3123:                             }
 3124:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 3125:                             $checkedon{$key} = ' checked="checked"';
 3126:                             $checkedoff{$key} = '';
 3127:                         }
 3128:                     }
 3129:                 }
 3130:             }
 3131:             foreach my $type (@types) {
 3132:                 next if ($type ne 'version' && !@locations);
 3133:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3134:                 $datatable .= '<tr'.$css_class.'>
 3135:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 3136:                                <span class="LC_nobreak">&nbsp;
 3137:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 3138:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 3139:                 if ($type eq 'version') {
 3140:                     my $selector = '<select name="'.$prefix.'_version">';
 3141:                     foreach my $version (@lcversions) {
 3142:                         my $selected = '';
 3143:                         if ($current{'version'} eq $version) {
 3144:                             $selected = ' selected="selected"';
 3145:                         }
 3146:                         $selector .= ' <option value="'.$version.'"'.
 3147:                                      $selected.'>'.$version.'</option>';
 3148:                     }
 3149:                     $selector .= '</select> ';
 3150:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 3151:                 } else {
 3152:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 3153:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 3154:                                  ' />'.('&nbsp;'x2).
 3155:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 3156:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 3157:                                  "\n".
 3158:                                  '</div><div><table>';
 3159:                     my $rem;
 3160:                     for (my $i=0; $i<@locations; $i++) {
 3161:                         my ($showloc,$value,$checkedtype);
 3162:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 3163:                             my $ip = $by_location{$locations[$i]}->[0];
 3164:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 3165:                                  $value = join(':',@{$by_ip{$ip}});
 3166:                                 $showloc = join(', ',@{$by_ip{$ip}});
 3167:                                 if (ref($current{$type}) eq 'ARRAY') {
 3168:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 3169:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 3170:                                             $checkedtype = ' checked="checked"';
 3171:                                             last;
 3172:                                         }
 3173:                                     }
 3174:                                 }
 3175:                             }
 3176:                         }
 3177:                         $rem = $i%($numinrow);
 3178:                         if ($rem == 0) {
 3179:                             if ($i > 0) {
 3180:                                 $datatable .= '</tr>';
 3181:                             }
 3182:                             $datatable .= '<tr>';
 3183:                         }
 3184:                         $datatable .= '<td class="LC_left_item">'.
 3185:                                       '<span class="LC_nobreak"><label>'.
 3186:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 3187:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 3188:                                       '</label></span></td>';
 3189:                     }
 3190:                     $rem = @locations%($numinrow);
 3191:                     my $colsleft = $numinrow - $rem;
 3192:                     if ($colsleft > 1 ) {
 3193:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3194:                                       '&nbsp;</td>';
 3195:                     } elsif ($colsleft == 1) {
 3196:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 3197:                     }
 3198:                     $datatable .= '</tr></table>';
 3199:                 }
 3200:                 $datatable .= '</td></tr>';
 3201:                 $itemcount ++;
 3202:             }
 3203:         }
 3204:     }
 3205:     $$rowtotal += $itemcount;
 3206:     return $datatable;
 3207: }
 3208: 
 3209: sub build_location_hashes {
 3210:     my ($intdoms,$by_ip,$by_location) = @_;
 3211:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 3212:                   (ref($by_location) eq 'HASH')); 
 3213:     my %iphost = &Apache::lonnet::get_iphost();
 3214:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 3215:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 3216:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 3217:         foreach my $id (@{$iphost{$primary_ip}}) {
 3218:             my $intdom = &Apache::lonnet::internet_dom($id);
 3219:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 3220:                 push(@{$intdoms},$intdom);
 3221:             }
 3222:         }
 3223:     }
 3224:     foreach my $ip (keys(%iphost)) {
 3225:         if (ref($iphost{$ip}) eq 'ARRAY') {
 3226:             foreach my $id (@{$iphost{$ip}}) {
 3227:                 my $location = &Apache::lonnet::internet_dom($id);
 3228:                 if ($location) {
 3229:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 3230:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3231:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 3232:                             push(@{$by_ip->{$ip}},$location);
 3233:                         }
 3234:                     } else {
 3235:                         $by_ip->{$ip} = [$location];
 3236:                     }
 3237:                 }
 3238:             }
 3239:         }
 3240:     }
 3241:     foreach my $ip (sort(keys(%{$by_ip}))) {
 3242:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3243:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 3244:             my $first = $by_ip->{$ip}->[0];
 3245:             if (ref($by_location->{$first}) eq 'ARRAY') {
 3246:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 3247:                     push(@{$by_location->{$first}},$ip);
 3248:                 }
 3249:             } else {
 3250:                 $by_location->{$first} = [$ip];
 3251:             }
 3252:         }
 3253:     }
 3254:     return;
 3255: }
 3256: 
 3257: sub current_offloads_to {
 3258:     my ($dom,$settings,$servers) = @_;
 3259:     my (%spareid,%otherdomconfigs);
 3260:     if (ref($servers) eq 'HASH') {
 3261:         foreach my $lonhost (sort(keys(%{$servers}))) {
 3262:             my $gotspares;
 3263:             if (ref($settings) eq 'HASH') {
 3264:                 if (ref($settings->{'spares'}) eq 'HASH') {
 3265:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 3266:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 3267:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 3268:                         $gotspares = 1;
 3269:                     }
 3270:                 }
 3271:             }
 3272:             unless ($gotspares) {
 3273:                 my $gotspares;
 3274:                 my $serverhomeID =
 3275:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 3276:                 my $serverhomedom =
 3277:                     &Apache::lonnet::host_domain($serverhomeID);
 3278:                 if ($serverhomedom ne $dom) {
 3279:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 3280:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3281:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3282:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3283:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3284:                                 $gotspares = 1;
 3285:                             }
 3286:                         }
 3287:                     } else {
 3288:                         $otherdomconfigs{$serverhomedom} =
 3289:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 3290:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 3291:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3292:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3293:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 3294:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3295:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3296:                                         $gotspares = 1;
 3297:                                     }
 3298:                                 }
 3299:                             }
 3300:                         }
 3301:                     }
 3302:                 }
 3303:             }
 3304:             unless ($gotspares) {
 3305:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3306:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3307:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3308:                } else {
 3309:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 3310:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 3311:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3312:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3313:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3314:                     } else {
 3315:                         my %what = (
 3316:                              spareid => 1,
 3317:                         );
 3318:                         my ($result,$returnhash) = 
 3319:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 3320:                         if ($result eq 'ok') { 
 3321:                             if (ref($returnhash) eq 'HASH') {
 3322:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 3323:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 3324:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 3325:                                 }
 3326:                             }
 3327:                         }
 3328:                     }
 3329:                 }
 3330:             }
 3331:         }
 3332:     }
 3333:     return %spareid;
 3334: }
 3335: 
 3336: sub spares_row {
 3337:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 3338:     my $css_class;
 3339:     my $numinrow = 4;
 3340:     my $itemcount = 1;
 3341:     my $datatable;
 3342:     my %typetitles = &sparestype_titles();
 3343:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3344:         foreach my $server (sort(keys(%{$servers}))) {
 3345:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 3346:             my ($othercontrol,$serverdom);
 3347:             if ($serverhome ne $server) {
 3348:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 3349:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3350:             } else {
 3351:                 $serverdom = &Apache::lonnet::host_domain($server);
 3352:                 if ($serverdom ne $dom) {
 3353:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3354:                 }
 3355:             }
 3356:             next unless (ref($spareid->{$server}) eq 'HASH');
 3357:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3358:             $datatable .= '<tr'.$css_class.'>
 3359:                            <td rowspan="2">
 3360:                             <span class="LC_nobreak">'.
 3361:                           &mt('[_1] when busy, offloads to:'
 3362:                               ,'<b>'.$server.'</b>').
 3363:                           "\n";
 3364:             my (%current,%canselect);
 3365:             my @choices = 
 3366:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 3367:             foreach my $type ('primary','default') {
 3368:                 if (ref($spareid->{$server}) eq 'HASH') {
 3369:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 3370:                         my @spares = @{$spareid->{$server}{$type}};
 3371:                         if (@spares > 0) {
 3372:                             if ($othercontrol) {
 3373:                                 $current{$type} = join(', ',@spares);
 3374:                             } else {
 3375:                                 $current{$type} .= '<table>';
 3376:                                 my $numspares = scalar(@spares);
 3377:                                 for (my $i=0;  $i<@spares; $i++) {
 3378:                                     my $rem = $i%($numinrow);
 3379:                                     if ($rem == 0) {
 3380:                                         if ($i > 0) {
 3381:                                             $current{$type} .= '</tr>';
 3382:                                         }
 3383:                                         $current{$type} .= '<tr>';
 3384:                                     }
 3385:                                     $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;'.
 3386:                                                        $spareid->{$server}{$type}[$i].
 3387:                                                        '</label></td>'."\n";
 3388:                                 }
 3389:                                 my $rem = @spares%($numinrow);
 3390:                                 my $colsleft = $numinrow - $rem;
 3391:                                 if ($colsleft > 1 ) {
 3392:                                     $current{$type} .= '<td colspan="'.$colsleft.
 3393:                                                        '" class="LC_left_item">'.
 3394:                                                        '&nbsp;</td>';
 3395:                                 } elsif ($colsleft == 1) {
 3396:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 3397:                                 }
 3398:                                 $current{$type} .= '</tr></table>';
 3399:                             }
 3400:                         }
 3401:                     }
 3402:                     if ($current{$type} eq '') {
 3403:                         $current{$type} = &mt('None specified');
 3404:                     }
 3405:                     if ($othercontrol) {
 3406:                         if ($type eq 'primary') {
 3407:                             $canselect{$type} = $othercontrol;
 3408:                         }
 3409:                     } else {
 3410:                         $canselect{$type} = 
 3411:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 3412:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 3413:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 3414:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 3415:                         if (@choices > 0) {
 3416:                             foreach my $lonhost (@choices) {
 3417:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 3418:                             }
 3419:                         }
 3420:                         $canselect{$type} .= '</select>'."\n";
 3421:                     }
 3422:                 } else {
 3423:                     $current{$type} = &mt('Could not be determined');
 3424:                     if ($type eq 'primary') {
 3425:                         $canselect{$type} =  $othercontrol;
 3426:                     }
 3427:                 }
 3428:                 if ($type eq 'default') {
 3429:                     $datatable .= '<tr'.$css_class.'>';
 3430:                 }
 3431:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 3432:                               '<td>'.$current{$type}.'</td>'."\n".
 3433:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 3434:             }
 3435:             $itemcount ++;
 3436:         }
 3437:     }
 3438:     $$rowtotal += $itemcount;
 3439:     return $datatable;
 3440: }
 3441: 
 3442: sub possible_newspares {
 3443:     my ($server,$currspares,$serverhomes,$altids) = @_;
 3444:     my $serverhostname = &Apache::lonnet::hostname($server);
 3445:     my %excluded;
 3446:     if ($serverhostname ne '') {
 3447:         %excluded = (
 3448:                        $serverhostname => 1,
 3449:                     );
 3450:     }
 3451:     if (ref($currspares) eq 'HASH') {
 3452:         foreach my $type (keys(%{$currspares})) {
 3453:             if (ref($currspares->{$type}) eq 'ARRAY') {
 3454:                 if (@{$currspares->{$type}} > 0) {
 3455:                     foreach my $curr (@{$currspares->{$type}}) {
 3456:                         my $hostname = &Apache::lonnet::hostname($curr);
 3457:                         $excluded{$hostname} = 1;
 3458:                     }
 3459:                 }
 3460:             }
 3461:         }
 3462:     }
 3463:     my @choices;
 3464:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3465:         if (keys(%{$serverhomes}) > 1) {
 3466:             foreach my $name (sort(keys(%{$serverhomes}))) {
 3467:                 unless ($excluded{$name}) {
 3468:                     if (exists($altids->{$serverhomes->{$name}})) {
 3469:                         push(@choices,$altids->{$serverhomes->{$name}});
 3470:                     } else {
 3471:                         push(@choices,$serverhomes->{$name});
 3472:                     }
 3473:                 }
 3474:             }
 3475:         }
 3476:     }
 3477:     return sort(@choices);
 3478: }
 3479: 
 3480: sub print_loadbalancing {
 3481:     my ($dom,$settings,$rowtotal) = @_;
 3482:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 3483:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 3484:     my $numinrow = 1;
 3485:     my $datatable;
 3486:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3487:     my (%currbalancer,%currtargets,%currrules,%existing);
 3488:     if (ref($settings) eq 'HASH') {
 3489:         %existing = %{$settings};
 3490:     }
 3491:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 3492:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 3493:                                   \%currtargets,\%currrules);
 3494:     } else {
 3495:         return;
 3496:     }
 3497:     my ($othertitle,$usertypes,$types) =
 3498:         &Apache::loncommon::sorted_inst_types($dom);
 3499:     my $rownum = 8;
 3500:     if (ref($types) eq 'ARRAY') {
 3501:         $rownum += scalar(@{$types});
 3502:     }
 3503:     my @css_class = ('LC_odd_row','LC_even_row');
 3504:     my $balnum = 0;
 3505:     my $islast;
 3506:     my (@toshow,$disabledtext);
 3507:     if (keys(%currbalancer) > 0) {
 3508:         @toshow = sort(keys(%currbalancer));
 3509:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 3510:             push(@toshow,'');
 3511:         }
 3512:     } else {
 3513:         @toshow = ('');
 3514:         $disabledtext = &mt('No existing load balancer');
 3515:     }
 3516:     foreach my $lonhost (@toshow) {
 3517:         if ($balnum == scalar(@toshow)-1) {
 3518:             $islast = 1;
 3519:         } else {
 3520:             $islast = 0;
 3521:         }
 3522:         my $cssidx = $balnum%2;
 3523:         my $targets_div_style = 'display: none';
 3524:         my $disabled_div_style = 'display: block';
 3525:         my $homedom_div_style = 'display: none';
 3526:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 3527:                       '<td rowspan="'.$rownum.'" valign="top">'.
 3528:                       '<p>';
 3529:         if ($lonhost eq '') {
 3530:             $datatable .= '<span class="LC_nobreak">';
 3531:             if (keys(%currbalancer) > 0) {
 3532:                 $datatable .= &mt('Add balancer:');
 3533:             } else {
 3534:                 $datatable .= &mt('Enable balancer:');
 3535:             }
 3536:             $datatable .= '&nbsp;'.
 3537:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 3538:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 3539:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 3540:                           '<option value="" selected="selected">'.&mt('None').
 3541:                           '</option>'."\n";
 3542:             foreach my $server (sort(keys(%servers))) {
 3543:                 next if ($currbalancer{$server});
 3544:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 3545:             }
 3546:             $datatable .=
 3547:                 '</select>'."\n".
 3548:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 3549:         } else {
 3550:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 3551:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 3552:                            &mt('Stop balancing').'</label>'.
 3553:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 3554:             $targets_div_style = 'display: block';
 3555:             $disabled_div_style = 'display: none';
 3556:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 3557:                 $homedom_div_style = 'display: block';
 3558:             }
 3559:         }
 3560:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 3561:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 3562:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 3563:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 3564:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 3565:         my @sparestypes = ('primary','default');
 3566:         my %typetitles = &sparestype_titles();
 3567:         foreach my $sparetype (@sparestypes) {
 3568:             my $targettable;
 3569:             for (my $i=0; $i<$numspares; $i++) {
 3570:                 my $checked;
 3571:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 3572:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 3573:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 3574:                             $checked = ' checked="checked"';
 3575:                         }
 3576:                     }
 3577:                 }
 3578:                 my ($chkboxval,$disabled);
 3579:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 3580:                     $chkboxval = $spares[$i];
 3581:                 }
 3582:                 if (exists($currbalancer{$spares[$i]})) {
 3583:                     $disabled = ' disabled="disabled"';
 3584:                 }
 3585:                 $targettable .=
 3586:                     '<td><label><input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 3587:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 3588:                     '</span></label></td>';
 3589:                 my $rem = $i%($numinrow);
 3590:                 if ($rem == 0) {
 3591:                     if (($i > 0) && ($i < $numspares-1)) {
 3592:                         $targettable .= '</tr>';
 3593:                     }
 3594:                     if ($i < $numspares-1) {
 3595:                         $targettable .= '<tr>';
 3596:                     }
 3597:                 }
 3598:             }
 3599:             if ($targettable ne '') {
 3600:                 my $rem = $numspares%($numinrow);
 3601:                 my $colsleft = $numinrow - $rem;
 3602:                 if ($colsleft > 1 ) {
 3603:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3604:                                     '&nbsp;</td>';
 3605:                 } elsif ($colsleft == 1) {
 3606:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 3607:                 }
 3608:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 3609:                                '<table><tr>'.$targettable.'</tr></table><br />';
 3610:             }
 3611:         }
 3612:         $datatable .= '</div></td></tr>'.
 3613:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 3614:                                            $othertitle,$usertypes,$types,\%servers,
 3615:                                            \%currbalancer,$lonhost,
 3616:                                            $targets_div_style,$homedom_div_style,
 3617:                                            $css_class[$cssidx],$balnum,$islast);
 3618:         $$rowtotal += $rownum;
 3619:         $balnum ++;
 3620:     }
 3621:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 3622:     return $datatable;
 3623: }
 3624: 
 3625: sub get_loadbalancers_config {
 3626:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 3627:     return unless ((ref($servers) eq 'HASH') &&
 3628:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 3629:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 3630:     if (keys(%{$existing}) > 0) {
 3631:         my $oldlonhost;
 3632:         foreach my $key (sort(keys(%{$existing}))) {
 3633:             if ($key eq 'lonhost') {
 3634:                 $oldlonhost = $existing->{'lonhost'};
 3635:                 $currbalancer->{$oldlonhost} = 1;
 3636:             } elsif ($key eq 'targets') {
 3637:                 if ($oldlonhost) {
 3638:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 3639:                 }
 3640:             } elsif ($key eq 'rules') {
 3641:                 if ($oldlonhost) {
 3642:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 3643:                 }
 3644:             } elsif (ref($existing->{$key}) eq 'HASH') {
 3645:                 $currbalancer->{$key} = 1;
 3646:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 3647:                 $currrules->{$key} = $existing->{$key}{'rules'};
 3648:             }
 3649:         }
 3650:     } else {
 3651:         my ($balancerref,$targetsref) =
 3652:                 &Apache::lonnet::get_lonbalancer_config($servers);
 3653:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 3654:             foreach my $server (sort(keys(%{$balancerref}))) {
 3655:                 $currbalancer->{$server} = 1;
 3656:                 $currtargets->{$server} = $targetsref->{$server};
 3657:             }
 3658:         }
 3659:     }
 3660:     return;
 3661: }
 3662: 
 3663: sub loadbalancing_rules {
 3664:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 3665:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 3666:         $css_class,$balnum,$islast) = @_;
 3667:     my $output;
 3668:     my $num = 0;
 3669:     my ($alltypes,$othertypes,$titles) =
 3670:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 3671:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 3672:         foreach my $type (@{$alltypes}) {
 3673:             $num ++;
 3674:             my $current;
 3675:             if (ref($currrules) eq 'HASH') {
 3676:                 $current = $currrules->{$type};
 3677:             }
 3678:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3679:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 3680:                     $current = '';
 3681:                 }
 3682:             }
 3683:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 3684:                                              $servers,$currbalancer,$lonhost,$dom,
 3685:                                              $targets_div_style,$homedom_div_style,
 3686:                                              $css_class,$balnum,$num,$islast);
 3687:         }
 3688:     }
 3689:     return $output;
 3690: }
 3691: 
 3692: sub loadbalancing_titles {
 3693:     my ($dom,$intdom,$usertypes,$types) = @_;
 3694:     my %othertypes = (
 3695:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 3696:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 3697:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 3698:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 3699:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 3700:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 3701:                      );
 3702:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 3703:     if (ref($types) eq 'ARRAY') {
 3704:         unshift(@alltypes,@{$types},'default');
 3705:     }
 3706:     my %titles;
 3707:     foreach my $type (@alltypes) {
 3708:         if ($type =~ /^_LC_/) {
 3709:             $titles{$type} = $othertypes{$type};
 3710:         } elsif ($type eq 'default') {
 3711:             $titles{$type} = &mt('All users from [_1]',$dom);
 3712:             if (ref($types) eq 'ARRAY') {
 3713:                 if (@{$types} > 0) {
 3714:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 3715:                 }
 3716:             }
 3717:         } elsif (ref($usertypes) eq 'HASH') {
 3718:             $titles{$type} = $usertypes->{$type};
 3719:         }
 3720:     }
 3721:     return (\@alltypes,\%othertypes,\%titles);
 3722: }
 3723: 
 3724: sub loadbalance_rule_row {
 3725:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 3726:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 3727:     my @rulenames;
 3728:     my %ruletitles = &offloadtype_text();
 3729:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 3730:         @rulenames = ('balancer','offloadedto');
 3731:     } else {
 3732:         @rulenames = ('default','homeserver');
 3733:         if ($type eq '_LC_external') {
 3734:             push(@rulenames,'externalbalancer');
 3735:         } else {
 3736:             push(@rulenames,'specific');
 3737:         }
 3738:         push(@rulenames,'none');
 3739:     }
 3740:     my $style = $targets_div_style;
 3741:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3742:         $style = $homedom_div_style;
 3743:     }
 3744:     my $space;
 3745:     if ($islast && $num == 1) {
 3746:         $space = '<div display="inline-block">&nbsp;</div>';
 3747:     }
 3748:     my $output =
 3749:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 3750:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 3751:         '<td valaign="top">'.$space.
 3752:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 3753:     for (my $i=0; $i<@rulenames; $i++) {
 3754:         my $rule = $rulenames[$i];
 3755:         my ($checked,$extra);
 3756:         if ($rulenames[$i] eq 'default') {
 3757:             $rule = '';
 3758:         }
 3759:         if ($rulenames[$i] eq 'specific') {
 3760:             if (ref($servers) eq 'HASH') {
 3761:                 my $default;
 3762:                 if (($current ne '') && (exists($servers->{$current}))) {
 3763:                     $checked = ' checked="checked"';
 3764:                 }
 3765:                 unless ($checked) {
 3766:                     $default = ' selected="selected"';
 3767:                 }
 3768:                 $extra =
 3769:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3770:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3771:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 3772:                     '<option value=""'.$default.'></option>'."\n";
 3773:                 foreach my $server (sort(keys(%{$servers}))) {
 3774:                     if (ref($currbalancer) eq 'HASH') {
 3775:                         next if (exists($currbalancer->{$server}));
 3776:                     }
 3777:                     my $selected;
 3778:                     if ($server eq $current) {
 3779:                         $selected = ' selected="selected"';
 3780:                     }
 3781:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 3782:                 }
 3783:                 $extra .= '</select>';
 3784:             }
 3785:         } elsif ($rule eq $current) {
 3786:             $checked = ' checked="checked"';
 3787:         }
 3788:         $output .= '<span class="LC_nobreak"><label>'.
 3789:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 3790:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 3791:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 3792:                    ')"'.$checked.' />&nbsp;'.$ruletitles{$rulenames[$i]}.
 3793:                    '</label>'.$extra.'</span><br />'."\n";
 3794:     }
 3795:     $output .= '</div></td></tr>'."\n";
 3796:     return $output;
 3797: }
 3798: 
 3799: sub offloadtype_text {
 3800:     my %ruletitles = &Apache::lonlocal::texthash (
 3801:            'default'          => 'Offloads to default destinations',
 3802:            'homeserver'       => "Offloads to user's home server",
 3803:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 3804:            'specific'         => 'Offloads to specific server',
 3805:            'none'             => 'No offload',
 3806:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 3807:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 3808:     );
 3809:     return %ruletitles;
 3810: }
 3811: 
 3812: sub sparestype_titles {
 3813:     my %typestitles = &Apache::lonlocal::texthash (
 3814:                           'primary' => 'primary',
 3815:                           'default' => 'default',
 3816:                       );
 3817:     return %typestitles;
 3818: }
 3819: 
 3820: sub contact_titles {
 3821:     my %titles = &Apache::lonlocal::texthash (
 3822:                    'supportemail' => 'Support E-mail address',
 3823:                    'adminemail'   => 'Default Server Admin E-mail address',
 3824:                    'errormail'    => 'Error reports to be e-mailed to',
 3825:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 3826:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 3827:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 3828:                    'requestsmail' => 'E-mail from course requests requiring approval',
 3829:                    'updatesmail'  => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 3830:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 3831:                  );
 3832:     my %short_titles = &Apache::lonlocal::texthash (
 3833:                            adminemail   => 'Admin E-mail address',
 3834:                            supportemail => 'Support E-mail',
 3835:                        );   
 3836:     return (\%titles,\%short_titles);
 3837: }
 3838: 
 3839: sub tool_titles {
 3840:     my %titles = &Apache::lonlocal::texthash (
 3841:                      aboutme    => 'Personal web page',
 3842:                      blog       => 'Blog',
 3843:                      webdav     => 'WebDAV',
 3844:                      portfolio  => 'Portfolio',
 3845:                      official   => 'Official courses (with institutional codes)',
 3846:                      unofficial => 'Unofficial courses',
 3847:                      community  => 'Communities',
 3848:                      textbook   => 'Textbook courses',
 3849:                  );
 3850:     return %titles;
 3851: }
 3852: 
 3853: sub courserequest_titles {
 3854:     my %titles = &Apache::lonlocal::texthash (
 3855:                                    official   => 'Official',
 3856:                                    unofficial => 'Unofficial',
 3857:                                    community  => 'Communities',
 3858:                                    textbook   => 'Textbook',
 3859:                                    norequest  => 'Not allowed',
 3860:                                    approval   => 'Approval by Dom. Coord.',
 3861:                                    validate   => 'With validation',
 3862:                                    autolimit  => 'Numerical limit',
 3863:                                    unlimited  => '(blank for unlimited)',
 3864:                  );
 3865:     return %titles;
 3866: }
 3867: 
 3868: sub authorrequest_titles {
 3869:     my %titles = &Apache::lonlocal::texthash (
 3870:                                    norequest  => 'Not allowed',
 3871:                                    approval   => 'Approval by Dom. Coord.',
 3872:                                    automatic  => 'Automatic approval',
 3873:                  );
 3874:     return %titles;
 3875: }
 3876: 
 3877: sub courserequest_conditions {
 3878:     my %conditions = &Apache::lonlocal::texthash (
 3879:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 3880:        validate   => '(Processing of request subject to institutional validation).',
 3881:                  );
 3882:     return %conditions;
 3883: }
 3884: 
 3885: 
 3886: sub print_usercreation {
 3887:     my ($position,$dom,$settings,$rowtotal) = @_;
 3888:     my $numinrow = 4;
 3889:     my $datatable;
 3890:     if ($position eq 'top') {
 3891:         $$rowtotal ++;
 3892:         my $rowcount = 0;
 3893:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 3894:         if (ref($rules) eq 'HASH') {
 3895:             if (keys(%{$rules}) > 0) {
 3896:                 $datatable .= &user_formats_row('username',$settings,$rules,
 3897:                                                 $ruleorder,$numinrow,$rowcount);
 3898:                 $$rowtotal ++;
 3899:                 $rowcount ++;
 3900:             }
 3901:         }
 3902:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 3903:         if (ref($idrules) eq 'HASH') {
 3904:             if (keys(%{$idrules}) > 0) {
 3905:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 3906:                                                 $idruleorder,$numinrow,$rowcount);
 3907:                 $$rowtotal ++;
 3908:                 $rowcount ++;
 3909:             }
 3910:         }
 3911:         if ($rowcount == 0) {
 3912:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 3913:             $$rowtotal ++;
 3914:             $rowcount ++;
 3915:         }
 3916:     } elsif ($position eq 'middle') {
 3917:         my @creators = ('author','course','requestcrs');
 3918:         my ($rules,$ruleorder) =
 3919:             &Apache::lonnet::inst_userrules($dom,'username');
 3920:         my %lt = &usercreation_types();
 3921:         my %checked;
 3922:         if (ref($settings) eq 'HASH') {
 3923:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 3924:                 foreach my $item (@creators) {
 3925:                     $checked{$item} = $settings->{'cancreate'}{$item};
 3926:                 }
 3927:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 3928:                 foreach my $item (@creators) {
 3929:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 3930:                         $checked{$item} = 'none';
 3931:                     }
 3932:                 }
 3933:             }
 3934:         }
 3935:         my $rownum = 0;
 3936:         foreach my $item (@creators) {
 3937:             $rownum ++;
 3938:             if ($checked{$item} eq '') {
 3939:                 $checked{$item} = 'any';
 3940:             }
 3941:             my $css_class;
 3942:             if ($rownum%2) {
 3943:                 $css_class = '';
 3944:             } else {
 3945:                 $css_class = ' class="LC_odd_row" ';
 3946:             }
 3947:             $datatable .= '<tr'.$css_class.'>'.
 3948:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 3949:                          '</span></td><td align="right">';
 3950:             my @options = ('any');
 3951:             if (ref($rules) eq 'HASH') {
 3952:                 if (keys(%{$rules}) > 0) {
 3953:                     push(@options,('official','unofficial'));
 3954:                 }
 3955:             }
 3956:             push(@options,'none');
 3957:             foreach my $option (@options) {
 3958:                 my $type = 'radio';
 3959:                 my $check = ' ';
 3960:                 if ($checked{$item} eq $option) {
 3961:                     $check = ' checked="checked" ';
 3962:                 } 
 3963:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3964:                               '<input type="'.$type.'" name="can_createuser_'.
 3965:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 3966:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 3967:             }
 3968:             $datatable .= '</td></tr>';
 3969:         }
 3970:     } else {
 3971:         my @contexts = ('author','course','domain');
 3972:         my @authtypes = ('int','krb4','krb5','loc');
 3973:         my %checked;
 3974:         if (ref($settings) eq 'HASH') {
 3975:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 3976:                 foreach my $item (@contexts) {
 3977:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 3978:                         foreach my $auth (@authtypes) {
 3979:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 3980:                                 $checked{$item}{$auth} = ' checked="checked" ';
 3981:                             }
 3982:                         }
 3983:                     }
 3984:                 }
 3985:             }
 3986:         } else {
 3987:             foreach my $item (@contexts) {
 3988:                 foreach my $auth (@authtypes) {
 3989:                     $checked{$item}{$auth} = ' checked="checked" ';
 3990:                 }
 3991:             }
 3992:         }
 3993:         my %title = &context_names();
 3994:         my %authname = &authtype_names();
 3995:         my $rownum = 0;
 3996:         my $css_class; 
 3997:         foreach my $item (@contexts) {
 3998:             if ($rownum%2) {
 3999:                 $css_class = '';
 4000:             } else {
 4001:                 $css_class = ' class="LC_odd_row" ';
 4002:             }
 4003:             $datatable .=   '<tr'.$css_class.'>'.
 4004:                             '<td>'.$title{$item}.
 4005:                             '</td><td class="LC_left_item">'.
 4006:                             '<span class="LC_nobreak">';
 4007:             foreach my $auth (@authtypes) {
 4008:                 $datatable .= '<label>'. 
 4009:                               '<input type="checkbox" name="'.$item.'_auth" '.
 4010:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 4011:                               $authname{$auth}.'</label>&nbsp;';
 4012:             }
 4013:             $datatable .= '</span></td></tr>';
 4014:             $rownum ++;
 4015:         }
 4016:         $$rowtotal += $rownum;
 4017:     }
 4018:     return $datatable;
 4019: }
 4020: 
 4021: sub print_selfcreation {
 4022:     my ($position,$dom,$settings,$rowtotal) = @_;
 4023:     my (@selfcreate,$createsettings,$datatable);
 4024:     if (ref($settings) eq 'HASH') {
 4025:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 4026:             $createsettings = $settings->{'cancreate'};
 4027:             if (ref($settings->{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
 4028:                 @selfcreate = @{$settings->{'cancreate'}{'selfcreate'}};
 4029:             } elsif ($settings->{'cancreate'}{'selfcreate'} ne '') {
 4030:                 if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 4031:                     @selfcreate = ('email','login','sso');
 4032:                 } elsif ($settings->{'cancreate'}{'selfcreate'} ne 'none') {
 4033:                     @selfcreate = ($settings->{'cancreate'}{'selfcreate'});
 4034:                 }
 4035:             }
 4036:         }
 4037:     }
 4038:     my %radiohash;
 4039:     my $numinrow = 4;
 4040:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 4041:     if ($position eq 'top') {
 4042:         my %choices = &Apache::lonlocal::texthash (
 4043:                                                       cancreate_login      => 'Institutional Login',
 4044:                                                       cancreate_sso        => 'Institutional Single Sign On',
 4045:                                                   );
 4046:         my @toggles = sort(keys(%choices));
 4047:         my %defaultchecked = (
 4048:                                'cancreate_login' => 'off',
 4049:                                'cancreate_sso'   => 'off',
 4050:                              );
 4051:         my ($onclick,$itemcount);
 4052:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 4053:                                                      \%choices,$itemcount,$onclick);
 4054:         $$rowtotal += $itemcount;
 4055:         
 4056:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4057: 
 4058:         if (ref($usertypes) eq 'HASH') {
 4059:             if (keys(%{$usertypes}) > 0) {
 4060:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 4061:                                              $dom,$numinrow,$othertitle,
 4062:                                              'statustocreate',$$rowtotal);
 4063:                 $$rowtotal ++;
 4064:             }
 4065:         }
 4066:     } elsif ($position eq 'middle') {
 4067:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 4068:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4069:         $usertypes->{'default'} = $othertitle;
 4070:         if (ref($types) eq 'ARRAY') {
 4071:             push(@{$types},'default');
 4072:             $usertypes->{'default'} = $othertitle;
 4073:             foreach my $status (@{$types}) {
 4074:                 $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 4075:                                                        $numinrow,$$rowtotal,$usertypes);
 4076:                 $$rowtotal ++;
 4077:             }
 4078:         }
 4079:     } else {
 4080:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 4081:         my %choices =
 4082:             &Apache::lonlocal::texthash(
 4083:                                           email         => 'Approved automatically',
 4084:                                           emailapproval => 'Queued for approval by DC',
 4085:                                           off           => 'Not enabled',
 4086:                                        );
 4087:         $datatable .= '<tr'.$css_class.'>'.
 4088:                       '<td>'.&mt('E-mail address as username').
 4089:                       '</td><td class="LC_left_item">'.
 4090:                       '<span class="LC_nobreak">';
 4091:         foreach my $option ('email','emailapproval','off') {
 4092:             my $checked;
 4093:             if ($option eq 'email') {
 4094:                 if ($radiohash{'cancreate_email'}) {
 4095:                     $checked = 'checked="checked"';
 4096:                 }
 4097:             } elsif ($option eq 'emailapproval') {
 4098:                 if ($radiohash{'cancreate_emailapproval'}) {
 4099:                     $checked = 'checked="checked"';
 4100:                 }
 4101:             } else {
 4102:                 if ((!$radiohash{'cancreate_email'}) && (!$radiohash{'cancreate_emailapproval'})) {
 4103:                     $checked = 'checked="checked"';  
 4104:                 }
 4105:             }
 4106:             $datatable .= '<label>'.
 4107:                           '<input type="radio" name="cancreate_email" '.
 4108:                           $checked.' value="'.$option.'" />'.
 4109:                           $choices{$option}.'</label>&nbsp;';
 4110:         }
 4111:         $$rowtotal ++;
 4112:         $datatable .= '</span></td></tr>'.
 4113:                       &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal);
 4114:         $$rowtotal ++;
 4115:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 4116:         $numinrow = 1;
 4117:         my ($othertitle,$usertypes,$types) =  &Apache::loncommon::sorted_inst_types($dom);
 4118:         $usertypes->{'default'} = $othertitle;
 4119:         if (ref($types) eq 'ARRAY') {
 4120:             push(@{$types},'default');
 4121:             $usertypes->{'default'} = $othertitle;
 4122:             foreach my $status (@{$types}) {
 4123:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 4124:                                                        $numinrow,$$rowtotal,$usertypes,$infofields,$infotitles);
 4125:                 $$rowtotal ++;
 4126:             }
 4127:         }
 4128:         my ($emailrules,$emailruleorder) =
 4129:             &Apache::lonnet::inst_userrules($dom,'email');
 4130:         if (ref($emailrules) eq 'HASH') {
 4131:             if (keys(%{$emailrules}) > 0) {
 4132:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 4133:                                                 $emailruleorder,$numinrow,$$rowtotal);
 4134:                 $$rowtotal ++;
 4135:             }
 4136:         }
 4137:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal);
 4138:     }
 4139:     return $datatable;
 4140: }
 4141: 
 4142: sub captcha_choice {
 4143:     my ($context,$settings,$itemcount) = @_;
 4144:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 4145:     my %lt = &captcha_phrases();
 4146:     $keyentry = 'hidden';
 4147:     if ($context eq 'cancreate') {
 4148:         $rowname = &mt('CAPTCHA validation');
 4149:     } elsif ($context eq 'login') {
 4150:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 4151:     }
 4152:     if (ref($settings) eq 'HASH') {
 4153:         if ($settings->{'captcha'}) {
 4154:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 4155:         } else {
 4156:             $checked{'original'} = ' checked="checked"';
 4157:         }
 4158:         if ($settings->{'captcha'} eq 'recaptcha') {
 4159:             $pubtext = $lt{'pub'};
 4160:             $privtext = $lt{'priv'};
 4161:             $keyentry = 'text';
 4162:         }
 4163:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 4164:             $currpub = $settings->{'recaptchakeys'}{'public'};
 4165:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 4166:         }
 4167:     } else {
 4168:         $checked{'original'} = ' checked="checked"';
 4169:     }
 4170:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4171:     my $output = '<tr'.$css_class.'>'.
 4172:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 4173:                  '<table><tr><td>'."\n";
 4174:     foreach my $option ('original','recaptcha','notused') {
 4175:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 4176:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 4177:                    $lt{$option}.'</label></span>';
 4178:         unless ($option eq 'notused') {
 4179:             $output .= ('&nbsp;'x2)."\n";
 4180:         }
 4181:     }
 4182: #
 4183: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 4184: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 4185: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 4186: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 4187: #
 4188:     $output .= '</td></tr>'."\n".
 4189:                '<tr><td>'."\n".
 4190:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 4191:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 4192:                $currpub.'" size="40" /></span><br />'."\n".
 4193:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 4194:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 4195:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 4196:                '</td></tr>';
 4197:     return $output;
 4198: }
 4199: 
 4200: sub user_formats_row {
 4201:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 4202:     my $output;
 4203:     my %text = (
 4204:                    'username' => 'new usernames',
 4205:                    'id'       => 'IDs',
 4206:                    'email'    => 'self-created accounts (e-mail)',
 4207:                );
 4208:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4209:     $output = '<tr '.$css_class.'>'.
 4210:               '<td><span class="LC_nobreak">';
 4211:     if ($type eq 'email') {
 4212:         $output .= &mt("Formats disallowed for $text{$type}: ");
 4213:     } else {
 4214:         $output .= &mt("Format rules to check for $text{$type}: ");
 4215:     }
 4216:     $output .= '</span></td>'.
 4217:                '<td class="LC_left_item" colspan="2"><table>';
 4218:     my $rem;
 4219:     if (ref($ruleorder) eq 'ARRAY') {
 4220:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 4221:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 4222:                 my $rem = $i%($numinrow);
 4223:                 if ($rem == 0) {
 4224:                     if ($i > 0) {
 4225:                         $output .= '</tr>';
 4226:                     }
 4227:                     $output .= '<tr>';
 4228:                 }
 4229:                 my $check = ' ';
 4230:                 if (ref($settings) eq 'HASH') {
 4231:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 4232:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 4233:                             $check = ' checked="checked" ';
 4234:                         }
 4235:                     }
 4236:                 }
 4237:                 $output .= '<td class="LC_left_item">'.
 4238:                            '<span class="LC_nobreak"><label>'.
 4239:                            '<input type="checkbox" name="'.$type.'_rule" '.
 4240:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 4241:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 4242:             }
 4243:         }
 4244:         $rem = @{$ruleorder}%($numinrow);
 4245:     }
 4246:     my $colsleft = $numinrow - $rem;
 4247:     if ($colsleft > 1 ) {
 4248:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4249:                    '&nbsp;</td>';
 4250:     } elsif ($colsleft == 1) {
 4251:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4252:     }
 4253:     $output .= '</tr></table></td></tr>';
 4254:     return $output;
 4255: }
 4256: 
 4257: sub usercreation_types {
 4258:     my %lt = &Apache::lonlocal::texthash (
 4259:                     author     => 'When adding a co-author',
 4260:                     course     => 'When adding a user to a course',
 4261:                     requestcrs => 'When requesting a course',
 4262:                     any        => 'Any',
 4263:                     official   => 'Institutional only ',
 4264:                     unofficial => 'Non-institutional only',
 4265:                     none       => 'None',
 4266:     );
 4267:     return %lt;
 4268: }
 4269: 
 4270: sub selfcreation_types {
 4271:     my %lt = &Apache::lonlocal::texthash (
 4272:                     selfcreate => 'User creates own account',
 4273:                     any        => 'Any',
 4274:                     official   => 'Institutional only ',
 4275:                     unofficial => 'Non-institutional only',
 4276:                     email      => 'E-mail address',
 4277:                     login      => 'Institutional Login',
 4278:                     sso        => 'SSO',
 4279:              );
 4280: }
 4281: 
 4282: sub authtype_names {
 4283:     my %lt = &Apache::lonlocal::texthash(
 4284:                       int    => 'Internal',
 4285:                       krb4   => 'Kerberos 4',
 4286:                       krb5   => 'Kerberos 5',
 4287:                       loc    => 'Local',
 4288:                   );
 4289:     return %lt;
 4290: }
 4291: 
 4292: sub context_names {
 4293:     my %context_title = &Apache::lonlocal::texthash(
 4294:        author => 'Creating users when an Author',
 4295:        course => 'Creating users when in a course',
 4296:        domain => 'Creating users when a Domain Coordinator',
 4297:     );
 4298:     return %context_title;
 4299: }
 4300: 
 4301: sub print_usermodification {
 4302:     my ($position,$dom,$settings,$rowtotal) = @_;
 4303:     my $numinrow = 4;
 4304:     my ($context,$datatable,$rowcount);
 4305:     if ($position eq 'top') {
 4306:         $rowcount = 0;
 4307:         $context = 'author'; 
 4308:         foreach my $role ('ca','aa') {
 4309:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4310:                                                    $numinrow,$rowcount);
 4311:             $$rowtotal ++;
 4312:             $rowcount ++;
 4313:         }
 4314:     } elsif ($position eq 'bottom') {
 4315:         $context = 'course';
 4316:         $rowcount = 0;
 4317:         foreach my $role ('st','ep','ta','in','cr') {
 4318:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4319:                                                    $numinrow,$rowcount);
 4320:             $$rowtotal ++;
 4321:             $rowcount ++;
 4322:         }
 4323:     }
 4324:     return $datatable;
 4325: }
 4326: 
 4327: sub print_defaults {
 4328:     my ($dom,$settings,$rowtotal) = @_;
 4329:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 4330:                  'datelocale_def','portal_def');
 4331:     my %defaults;
 4332:     if (ref($settings) eq 'HASH') {
 4333:         %defaults = %{$settings};
 4334:     } else {
 4335:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 4336:         foreach my $item (@items) {
 4337:             $defaults{$item} = $domdefaults{$item};
 4338:         }
 4339:     }
 4340:     my $titles = &defaults_titles($dom);
 4341:     my $rownum = 0;
 4342:     my ($datatable,$css_class);
 4343:     foreach my $item (@items) {
 4344:         if ($rownum%2) {
 4345:             $css_class = '';
 4346:         } else {
 4347:             $css_class = ' class="LC_odd_row" ';
 4348:         }
 4349:         $datatable .= '<tr'.$css_class.'>'.
 4350:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 4351:                   '</span></td><td class="LC_right_item">';
 4352:         if ($item eq 'auth_def') {
 4353:             my @authtypes = ('internal','krb4','krb5','localauth');
 4354:             my %shortauth = (
 4355:                              internal => 'int',
 4356:                              krb4 => 'krb4',
 4357:                              krb5 => 'krb5',
 4358:                              localauth  => 'loc'
 4359:                            );
 4360:             my %authnames = &authtype_names();
 4361:             foreach my $auth (@authtypes) {
 4362:                 my $checked = ' ';
 4363:                 if ($defaults{$item} eq $auth) {
 4364:                     $checked = ' checked="checked" ';
 4365:                 }
 4366:                 $datatable .= '<label><input type="radio" name="'.$item.
 4367:                               '" value="'.$auth.'"'.$checked.'/>'.
 4368:                               $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 4369:             }
 4370:         } elsif ($item eq 'timezone_def') {
 4371:             my $includeempty = 1;
 4372:             $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 4373:         } elsif ($item eq 'datelocale_def') {
 4374:             my $includeempty = 1;
 4375:             $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 4376:         } elsif ($item eq 'lang_def') {
 4377:             my %langchoices = &get_languages_hash();
 4378:             $langchoices{''} = 'No language preference';
 4379:             %langchoices = &Apache::lonlocal::texthash(%langchoices);
 4380:             $datatable .= &Apache::loncommon::select_form($defaults{$item},$item,
 4381:                                                           \%langchoices);
 4382:         } else {
 4383:             my $size;
 4384:             if ($item eq 'portal_def') {
 4385:                 $size = ' size="25"';
 4386:             }
 4387:             $datatable .= '<input type="text" name="'.$item.'" value="'.
 4388:                           $defaults{$item}.'"'.$size.' />';
 4389:         }
 4390:         $datatable .= '</td></tr>';
 4391:         $rownum ++;
 4392:     }
 4393:     $$rowtotal += $rownum;
 4394:     return $datatable;
 4395: }
 4396: 
 4397: sub get_languages_hash {
 4398:     my %langchoices;
 4399:     foreach my $id (&Apache::loncommon::languageids()) {
 4400:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 4401:         if ($code ne '') {
 4402:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 4403:         }
 4404:     }
 4405:     return %langchoices;
 4406: }
 4407: 
 4408: sub defaults_titles {
 4409:     my ($dom) = @_;
 4410:     my %titles = &Apache::lonlocal::texthash (
 4411:                    'auth_def'      => 'Default authentication type',
 4412:                    'auth_arg_def'  => 'Default authentication argument',
 4413:                    'lang_def'      => 'Default language',
 4414:                    'timezone_def'  => 'Default timezone',
 4415:                    'datelocale_def' => 'Default locale for dates',
 4416:                    'portal_def'     => 'Portal/Default URL',
 4417:                  );
 4418:     if ($dom) {
 4419:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 4420:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 4421:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 4422:         $protocol = 'http' if ($protocol ne 'https');
 4423:         if ($uint_dom) {
 4424:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 4425:                                          $uint_dom);
 4426:         }
 4427:     }
 4428:     return (\%titles);
 4429: }
 4430: 
 4431: sub print_scantronformat {
 4432:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 4433:     my $itemcount = 1;
 4434:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 4435:         %confhash);
 4436:     my $switchserver = &check_switchserver($dom,$confname);
 4437:     my %lt = &Apache::lonlocal::texthash (
 4438:                 default => 'Default bubblesheet format file error',
 4439:                 custom  => 'Custom bubblesheet format file error',
 4440:              );
 4441:     my %scantronfiles = (
 4442:         default => 'default.tab',
 4443:         custom => 'custom.tab',
 4444:     );
 4445:     foreach my $key (keys(%scantronfiles)) {
 4446:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 4447:                               .$scantronfiles{$key};
 4448:     }
 4449:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 4450:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 4451:         if (!$switchserver) {
 4452:             my $servadm = $r->dir_config('lonAdmEMail');
 4453:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 4454:             if ($configuserok eq 'ok') {
 4455:                 if ($author_ok eq 'ok') {
 4456:                     my %legacyfile = (
 4457:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 4458:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 4459:                     );
 4460:                     my %md5chk;
 4461:                     foreach my $type (keys(%legacyfile)) {
 4462:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 4463:                         chomp($md5chk{$type});
 4464:                     }
 4465:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 4466:                         foreach my $type (keys(%legacyfile)) {
 4467:                             ($scantronurls{$type},my $error) = 
 4468:                                 &legacy_scantronformat($r,$dom,$confname,
 4469:                                                  $type,$legacyfile{$type},
 4470:                                                  $scantronurls{$type},
 4471:                                                  $scantronfiles{$type});
 4472:                             if ($error ne '') {
 4473:                                 $error{$type} = $error;
 4474:                             }
 4475:                         }
 4476:                         if (keys(%error) == 0) {
 4477:                             $is_custom = 1;
 4478:                             $confhash{'scantron'}{'scantronformat'} = 
 4479:                                 $scantronurls{'custom'};
 4480:                             my $putresult = 
 4481:                                 &Apache::lonnet::put_dom('configuration',
 4482:                                                          \%confhash,$dom);
 4483:                             if ($putresult ne 'ok') {
 4484:                                 $error{'custom'} = 
 4485:                                     '<span class="LC_error">'.
 4486:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4487:                             }
 4488:                         }
 4489:                     } else {
 4490:                         ($scantronurls{'default'},my $error) =
 4491:                             &legacy_scantronformat($r,$dom,$confname,
 4492:                                           'default',$legacyfile{'default'},
 4493:                                           $scantronurls{'default'},
 4494:                                           $scantronfiles{'default'});
 4495:                         if ($error eq '') {
 4496:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 4497:                             my $putresult =
 4498:                                 &Apache::lonnet::put_dom('configuration',
 4499:                                                          \%confhash,$dom);
 4500:                             if ($putresult ne 'ok') {
 4501:                                 $error{'default'} =
 4502:                                     '<span class="LC_error">'.
 4503:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4504:                             }
 4505:                         } else {
 4506:                             $error{'default'} = $error;
 4507:                         }
 4508:                     }
 4509:                 }
 4510:             }
 4511:         } else {
 4512:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 4513:         }
 4514:     }
 4515:     if (ref($settings) eq 'HASH') {
 4516:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 4517:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 4518:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 4519:                 $scantronurl = '';
 4520:             } else {
 4521:                 $scantronurl = $settings->{'scantronformat'};
 4522:             }
 4523:             $is_custom = 1;
 4524:         } else {
 4525:             $scantronurl = $scantronurls{'default'};
 4526:         }
 4527:     } else {
 4528:         if ($is_custom) {
 4529:             $scantronurl = $scantronurls{'custom'};
 4530:         } else {
 4531:             $scantronurl = $scantronurls{'default'};
 4532:         }
 4533:     }
 4534:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4535:     $datatable .= '<tr'.$css_class.'>';
 4536:     if (!$is_custom) {
 4537:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 4538:                       '<span class="LC_nobreak">';
 4539:         if ($scantronurl) {
 4540:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 4541:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 4542:         } else {
 4543:             $datatable = &mt('File unavailable for display');
 4544:         }
 4545:         $datatable .= '</span></td>';
 4546:         if (keys(%error) == 0) { 
 4547:             $datatable .= '<td valign="bottom">';
 4548:             if (!$switchserver) {
 4549:                 $datatable .= &mt('Upload:').'<br />';
 4550:             }
 4551:         } else {
 4552:             my $errorstr;
 4553:             foreach my $key (sort(keys(%error))) {
 4554:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4555:             }
 4556:             $datatable .= '<td>'.$errorstr;
 4557:         }
 4558:     } else {
 4559:         if (keys(%error) > 0) {
 4560:             my $errorstr;
 4561:             foreach my $key (sort(keys(%error))) {
 4562:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4563:             } 
 4564:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 4565:         } elsif ($scantronurl) {
 4566:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 4567:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 4568:             $datatable .= '<td><span class="LC_nobreak">'.
 4569:                           $link.
 4570:                           '<label><input type="checkbox" name="scantronformat_del"'.
 4571:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 4572:                           '<td><span class="LC_nobreak">&nbsp;'.
 4573:                           &mt('Replace:').'</span><br />';
 4574:         }
 4575:     }
 4576:     if (keys(%error) == 0) {
 4577:         if ($switchserver) {
 4578:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4579:         } else {
 4580:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 4581:                          '<input type="file" name="scantronformat" /></span>';
 4582:         }
 4583:     }
 4584:     $datatable .= '</td></tr>';
 4585:     $$rowtotal ++;
 4586:     return $datatable;
 4587: }
 4588: 
 4589: sub legacy_scantronformat {
 4590:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 4591:     my ($url,$error);
 4592:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 4593:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 4594:         (my $result,$url) =
 4595:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 4596:                          '','',$newfile);
 4597:         if ($result ne 'ok') {
 4598:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 4599:         }
 4600:     }
 4601:     return ($url,$error);
 4602: }
 4603: 
 4604: sub print_coursecategories {
 4605:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 4606:     my $datatable;
 4607:     if ($position eq 'top') {
 4608:         my $toggle_cats_crs = ' ';
 4609:         my $toggle_cats_dom = ' checked="checked" ';
 4610:         my $can_cat_crs = ' ';
 4611:         my $can_cat_dom = ' checked="checked" ';
 4612:         my $toggle_catscomm_comm = ' ';
 4613:         my $toggle_catscomm_dom = ' checked="checked" ';
 4614:         my $can_catcomm_comm = ' ';
 4615:         my $can_catcomm_dom = ' checked="checked" ';
 4616: 
 4617:         if (ref($settings) eq 'HASH') {
 4618:             if ($settings->{'togglecats'} eq 'crs') {
 4619:                 $toggle_cats_crs = $toggle_cats_dom;
 4620:                 $toggle_cats_dom = ' ';
 4621:             }
 4622:             if ($settings->{'categorize'} eq 'crs') {
 4623:                 $can_cat_crs = $can_cat_dom;
 4624:                 $can_cat_dom = ' ';
 4625:             }
 4626:             if ($settings->{'togglecatscomm'} eq 'comm') {
 4627:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 4628:                 $toggle_catscomm_dom = ' ';
 4629:             }
 4630:             if ($settings->{'categorizecomm'} eq 'comm') {
 4631:                 $can_catcomm_comm = $can_catcomm_dom;
 4632:                 $can_catcomm_dom = ' ';
 4633:             }
 4634:         }
 4635:         my %title = &Apache::lonlocal::texthash (
 4636:                      togglecats     => 'Show/Hide a course in catalog',
 4637:                      togglecatscomm => 'Show/Hide a community in catalog',
 4638:                      categorize     => 'Assign a category to a course',
 4639:                      categorizecomm => 'Assign a category to a community',
 4640:                     );
 4641:         my %level = &Apache::lonlocal::texthash (
 4642:                      dom  => 'Set in Domain',
 4643:                      crs  => 'Set in Course',
 4644:                      comm => 'Set in Community',
 4645:                     );
 4646:         $datatable = '<tr class="LC_odd_row">'.
 4647:                   '<td>'.$title{'togglecats'}.'</td>'.
 4648:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4649:                   '<input type="radio" name="togglecats"'.
 4650:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4651:                   '<label><input type="radio" name="togglecats"'.
 4652:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4653:                   '</tr><tr>'.
 4654:                   '<td>'.$title{'categorize'}.'</td>'.
 4655:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4656:                   '<label><input type="radio" name="categorize"'.
 4657:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4658:                   '<label><input type="radio" name="categorize"'.
 4659:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4660:                   '</tr><tr class="LC_odd_row">'.
 4661:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 4662:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4663:                   '<input type="radio" name="togglecatscomm"'.
 4664:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4665:                   '<label><input type="radio" name="togglecatscomm"'.
 4666:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4667:                   '</tr><tr>'.
 4668:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 4669:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4670:                   '<label><input type="radio" name="categorizecomm"'.
 4671:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4672:                   '<label><input type="radio" name="categorizecomm"'.
 4673:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4674:                   '</tr>';
 4675:         $$rowtotal += 4;
 4676:     } else {
 4677:         my $css_class;
 4678:         my $itemcount = 1;
 4679:         my $cathash; 
 4680:         if (ref($settings) eq 'HASH') {
 4681:             $cathash = $settings->{'cats'};
 4682:         }
 4683:         if (ref($cathash) eq 'HASH') {
 4684:             my (@cats,@trails,%allitems,%idx,@jsarray);
 4685:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 4686:                                                    \%allitems,\%idx,\@jsarray);
 4687:             my $maxdepth = scalar(@cats);
 4688:             my $colattrib = '';
 4689:             if ($maxdepth > 2) {
 4690:                 $colattrib = ' colspan="2" ';
 4691:             }
 4692:             my @path;
 4693:             if (@cats > 0) {
 4694:                 if (ref($cats[0]) eq 'ARRAY') {
 4695:                     my $numtop = @{$cats[0]};
 4696:                     my $maxnum = $numtop;
 4697:                     my %default_names = (
 4698:                           instcode    => &mt('Official courses'),
 4699:                           communities => &mt('Communities'),
 4700:                     );
 4701: 
 4702:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 4703:                         ($cathash->{'instcode::0'} eq '') ||
 4704:                         (!grep(/^communities$/,@{$cats[0]})) || 
 4705:                         ($cathash->{'communities::0'} eq '')) {
 4706:                         $maxnum ++;
 4707:                     }
 4708:                     my $lastidx;
 4709:                     for (my $i=0; $i<$numtop; $i++) {
 4710:                         my $parent = $cats[0][$i];
 4711:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4712:                         my $item = &escape($parent).'::0';
 4713:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 4714:                         $lastidx = $idx{$item};
 4715:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4716:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 4717:                         for (my $k=0; $k<=$maxnum; $k++) {
 4718:                             my $vpos = $k+1;
 4719:                             my $selstr;
 4720:                             if ($k == $i) {
 4721:                                 $selstr = ' selected="selected" ';
 4722:                             }
 4723:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4724:                         }
 4725:                         $datatable .= '</select></span></td><td>';
 4726:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 4727:                             $datatable .=  '<span class="LC_nobreak">'
 4728:                                            .$default_names{$parent}.'</span>';
 4729:                             if ($parent eq 'instcode') {
 4730:                                 $datatable .= '<br /><span class="LC_nobreak">('
 4731:                                               .&mt('with institutional codes')
 4732:                                               .')</span></td><td'.$colattrib.'>';
 4733:                             } else {
 4734:                                 $datatable .= '<table><tr><td>';
 4735:                             }
 4736:                             $datatable .= '<span class="LC_nobreak">'
 4737:                                           .'<label><input type="radio" name="'
 4738:                                           .$parent.'" value="1" checked="checked" />'
 4739:                                           .&mt('Display').'</label>';
 4740:                             if ($parent eq 'instcode') {
 4741:                                 $datatable .= '&nbsp;';
 4742:                             } else {
 4743:                                 $datatable .= '</span></td></tr><tr><td>'
 4744:                                               .'<span class="LC_nobreak">';
 4745:                             }
 4746:                             $datatable .= '<label><input type="radio" name="'
 4747:                                           .$parent.'" value="0" />'
 4748:                                           .&mt('Do not display').'</label></span>';
 4749:                             if ($parent eq 'communities') {
 4750:                                 $datatable .= '</td></tr></table>';
 4751:                             }
 4752:                             $datatable .= '</td>';
 4753:                         } else {
 4754:                             $datatable .= $parent
 4755:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 4756:                                           .'<input type="checkbox" name="deletecategory" '
 4757:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 4758:                         }
 4759:                         my $depth = 1;
 4760:                         push(@path,$parent);
 4761:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 4762:                         pop(@path);
 4763:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 4764:                         $itemcount ++;
 4765:                     }
 4766:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4767:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 4768:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 4769:                     for (my $k=0; $k<=$maxnum; $k++) {
 4770:                         my $vpos = $k+1;
 4771:                         my $selstr;
 4772:                         if ($k == $numtop) {
 4773:                             $selstr = ' selected="selected" ';
 4774:                         }
 4775:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4776:                     }
 4777:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 4778:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 4779:                                   .'</tr>'."\n";
 4780:                     $itemcount ++;
 4781:                     foreach my $default ('instcode','communities') {
 4782:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 4783:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4784:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 4785:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 4786:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 4787:                             for (my $k=0; $k<=$maxnum; $k++) {
 4788:                                 my $vpos = $k+1;
 4789:                                 my $selstr;
 4790:                                 if ($k == $maxnum) {
 4791:                                     $selstr = ' selected="selected" ';
 4792:                                 }
 4793:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4794:                             }
 4795:                             $datatable .= '</select></span></td>'.
 4796:                                           '<td><span class="LC_nobreak">'.
 4797:                                           $default_names{$default}.'</span>';
 4798:                             if ($default eq 'instcode') {
 4799:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 4800:                                               .&mt('with institutional codes').')</span>';
 4801:                             }
 4802:                             $datatable .= '</td>'
 4803:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 4804:                                           .&mt('Display').'</label>&nbsp;'
 4805:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 4806:                                           .&mt('Do not display').'</label></span></td></tr>';
 4807:                         }
 4808:                     }
 4809:                 }
 4810:             } else {
 4811:                 $datatable .= &initialize_categories($itemcount);
 4812:             }
 4813:         } else {
 4814:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[0]->{'col2'}.'</td>'
 4815:                           .&initialize_categories($itemcount);
 4816:         }
 4817:         $$rowtotal += $itemcount;
 4818:     }
 4819:     return $datatable;
 4820: }
 4821: 
 4822: sub print_serverstatuses {
 4823:     my ($dom,$settings,$rowtotal) = @_;
 4824:     my $datatable;
 4825:     my @pages = &serverstatus_pages();
 4826:     my (%namedaccess,%machineaccess);
 4827:     foreach my $type (@pages) {
 4828:         $namedaccess{$type} = '';
 4829:         $machineaccess{$type}= '';
 4830:     }
 4831:     if (ref($settings) eq 'HASH') {
 4832:         foreach my $type (@pages) {
 4833:             if (exists($settings->{$type})) {
 4834:                 if (ref($settings->{$type}) eq 'HASH') {
 4835:                     foreach my $key (keys(%{$settings->{$type}})) {
 4836:                         if ($key eq 'namedusers') {
 4837:                             $namedaccess{$type} = $settings->{$type}->{$key};
 4838:                         } elsif ($key eq 'machines') {
 4839:                             $machineaccess{$type} = $settings->{$type}->{$key};
 4840:                         }
 4841:                     }
 4842:                 }
 4843:             }
 4844:         }
 4845:     }
 4846:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 4847:     my $rownum = 0;
 4848:     my $css_class;
 4849:     foreach my $type (@pages) {
 4850:         $rownum ++;
 4851:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4852:         $datatable .= '<tr'.$css_class.'>'.
 4853:                       '<td><span class="LC_nobreak">'.
 4854:                       $titles->{$type}.'</span></td>'.
 4855:                       '<td class="LC_left_item">'.
 4856:                       '<input type="text" name="'.$type.'_namedusers" '.
 4857:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 4858:                       '<td class="LC_right_item">'.
 4859:                       '<span class="LC_nobreak">'.
 4860:                       '<input type="text" name="'.$type.'_machines" '.
 4861:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 4862:                       '</td></tr>'."\n";
 4863:     }
 4864:     $$rowtotal += $rownum;
 4865:     return $datatable;
 4866: }
 4867: 
 4868: sub serverstatus_pages {
 4869:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 4870:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 4871:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 4872:             'uniquecodes','diskusage');
 4873: }
 4874: 
 4875: sub coursecategories_javascript {
 4876:     my ($settings) = @_;
 4877:     my ($output,$jstext,$cathash);
 4878:     if (ref($settings) eq 'HASH') {
 4879:         $cathash = $settings->{'cats'};
 4880:     }
 4881:     if (ref($cathash) eq 'HASH') {
 4882:         my (@cats,@jsarray,%idx);
 4883:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 4884:         if (@jsarray > 0) {
 4885:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 4886:             for (my $i=0; $i<@jsarray; $i++) {
 4887:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 4888:                     my $catstr = join('","',@{$jsarray[$i]});
 4889:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 4890:                 }
 4891:             }
 4892:         }
 4893:     } else {
 4894:         $jstext  = '    var categories = Array(1);'."\n".
 4895:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 4896:     }
 4897:     my $instcode_reserved = &mt('The name: "instcode" is a reserved category');
 4898:     my $communities_reserved = &mt('The name: "communities" is a reserved category');
 4899:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category'); 
 4900:     $output = <<"ENDSCRIPT";
 4901: <script type="text/javascript">
 4902: // <![CDATA[
 4903: function reorderCats(form,parent,item,idx) {
 4904:     var changedVal;
 4905: $jstext
 4906:     var newpos = 'addcategory_pos';
 4907:     var current = new Array;
 4908:     if (parent == '') {
 4909:         var has_instcode = 0;
 4910:         var maxtop = categories[idx].length;
 4911:         for (var j=0; j<maxtop; j++) {
 4912:             if (categories[idx][j] == 'instcode::0') {
 4913:                 has_instcode == 1;
 4914:             }
 4915:         }
 4916:         if (has_instcode == 0) {
 4917:             categories[idx][maxtop] = 'instcode_pos';
 4918:         }
 4919:     } else {
 4920:         newpos += '_'+parent;
 4921:     }
 4922:     var maxh = 1 + categories[idx].length;
 4923:     var current = new Array;
 4924:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4925:     if (item == newpos) {
 4926:         changedVal = newitemVal;
 4927:     } else {
 4928:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4929:         current[newitemVal] = newpos;
 4930:     }
 4931:     for (var i=0; i<categories[idx].length; i++) {
 4932:         var elementName = categories[idx][i];
 4933:         if (elementName != item) {
 4934:             if (form.elements[elementName]) {
 4935:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4936:                 current[currVal] = elementName;
 4937:             }
 4938:         }
 4939:     }
 4940:     var oldVal;
 4941:     for (var j=0; j<maxh; j++) {
 4942:         if (current[j] == undefined) {
 4943:             oldVal = j;
 4944:         }
 4945:     }
 4946:     if (oldVal < changedVal) {
 4947:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4948:            var elementName = current[k];
 4949:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4950:         }
 4951:     } else {
 4952:         for (var k=changedVal; k<oldVal; k++) {
 4953:             var elementName = current[k];
 4954:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4955:         }
 4956:     }
 4957:     return;
 4958: }
 4959: 
 4960: function categoryCheck(form) {
 4961:     if (form.elements['addcategory_name'].value == 'instcode') {
 4962:         alert('$instcode_reserved\\n$choose_again');
 4963:         return false;
 4964:     }
 4965:     if (form.elements['addcategory_name'].value == 'communities') {
 4966:         alert('$communities_reserved\\n$choose_again');
 4967:         return false;
 4968:     }
 4969:     return true;
 4970: }
 4971: 
 4972: // ]]>
 4973: </script>
 4974: 
 4975: ENDSCRIPT
 4976:     return $output;
 4977: }
 4978: 
 4979: sub initialize_categories {
 4980:     my ($itemcount) = @_;
 4981:     my ($datatable,$css_class,$chgstr);
 4982:     my %default_names = (
 4983:                       instcode    => 'Official courses (with institutional codes)',
 4984:                       communities => 'Communities',
 4985:                         );
 4986:     my $select0 = ' selected="selected"';
 4987:     my $select1 = '';
 4988:     foreach my $default ('instcode','communities') {
 4989:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4990:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 4991:         if ($default eq 'communities') {
 4992:             $select1 = $select0;
 4993:             $select0 = '';
 4994:         }
 4995:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4996:                      .'<select name="'.$default.'_pos">'
 4997:                      .'<option value="0"'.$select0.'>1</option>'
 4998:                      .'<option value="1"'.$select1.'>2</option>'
 4999:                      .'<option value="2">3</option></select>&nbsp;'
 5000:                      .$default_names{$default}
 5001:                      .'</span></td><td><span class="LC_nobreak">'
 5002:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 5003:                      .&mt('Display').'</label>&nbsp;<label>'
 5004:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 5005:                  .'</label></span></td></tr>';
 5006:         $itemcount ++;
 5007:     }
 5008:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5009:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 5010:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5011:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 5012:                   .'<option value="0">1</option>'
 5013:                   .'<option value="1">2</option>'
 5014:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 5015:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 5016:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 5017:     return $datatable;
 5018: }
 5019: 
 5020: sub build_category_rows {
 5021:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 5022:     my ($text,$name,$item,$chgstr);
 5023:     if (ref($cats) eq 'ARRAY') {
 5024:         my $maxdepth = scalar(@{$cats});
 5025:         if (ref($cats->[$depth]) eq 'HASH') {
 5026:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 5027:                 my $numchildren = @{$cats->[$depth]{$parent}};
 5028:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5029:                 $text .= '<td><table class="LC_data_table">';
 5030:                 my ($idxnum,$parent_name,$parent_item);
 5031:                 my $higher = $depth - 1;
 5032:                 if ($higher == 0) {
 5033:                     $parent_name = &escape($parent).'::'.$higher;
 5034:                 } else {
 5035:                     if (ref($path) eq 'ARRAY') {
 5036:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 5037:                     }
 5038:                 }
 5039:                 $parent_item = 'addcategory_pos_'.$parent_name;
 5040:                 for (my $j=0; $j<=$numchildren; $j++) {
 5041:                     if ($j < $numchildren) {
 5042:                         $name = $cats->[$depth]{$parent}[$j];
 5043:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 5044:                         $idxnum = $idx->{$item};
 5045:                     } else {
 5046:                         $name = $parent_name;
 5047:                         $item = $parent_item;
 5048:                     }
 5049:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 5050:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 5051:                     for (my $i=0; $i<=$numchildren; $i++) {
 5052:                         my $vpos = $i+1;
 5053:                         my $selstr;
 5054:                         if ($j == $i) {
 5055:                             $selstr = ' selected="selected" ';
 5056:                         }
 5057:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 5058:                     }
 5059:                     $text .= '</select>&nbsp;';
 5060:                     if ($j < $numchildren) {
 5061:                         my $deeper = $depth+1;
 5062:                         $text .= $name.'&nbsp;'
 5063:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 5064:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 5065:                         if(ref($path) eq 'ARRAY') {
 5066:                             push(@{$path},$name);
 5067:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 5068:                             pop(@{$path});
 5069:                         }
 5070:                     } else {
 5071:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 5072:                         if ($j == $numchildren) {
 5073:                             $text .= $name;
 5074:                         } else {
 5075:                             $text .= $item;
 5076:                         }
 5077:                         $text .= '" value="" />';
 5078:                     }
 5079:                     $text .= '</td></tr>';
 5080:                 }
 5081:                 $text .= '</table></td>';
 5082:             } else {
 5083:                 my $higher = $depth-1;
 5084:                 if ($higher == 0) {
 5085:                     $name = &escape($parent).'::'.$higher;
 5086:                 } else {
 5087:                     if (ref($path) eq 'ARRAY') {
 5088:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 5089:                     }
 5090:                 }
 5091:                 my $colspan;
 5092:                 if ($parent ne 'instcode') {
 5093:                     $colspan = $maxdepth - $depth - 1;
 5094:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 5095:                 }
 5096:             }
 5097:         }
 5098:     }
 5099:     return $text;
 5100: }
 5101: 
 5102: sub modifiable_userdata_row {
 5103:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref) = @_;
 5104:     my ($role,$rolename,$statustype);
 5105:     $role = $item;
 5106:     if ($context eq 'cancreate') {
 5107:         if ($item =~ /^emailusername_(.+)$/) {
 5108:             $statustype = $1;
 5109:             $role = 'emailusername';
 5110:             if (ref($usertypes) eq 'HASH') {
 5111:                 if ($usertypes->{$statustype}) {
 5112:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 5113:                 } else {
 5114:                     $rolename = &mt('Data provided by user');
 5115:                 }
 5116:             }
 5117:         }
 5118:     } elsif ($context eq 'selfcreate') {
 5119:         if (ref($usertypes) eq 'HASH') {
 5120:             $rolename = $usertypes->{$role};
 5121:         } else {
 5122:             $rolename = $role;
 5123:         }
 5124:     } else {
 5125:         if ($role eq 'cr') {
 5126:             $rolename = &mt('Custom role');
 5127:         } else {
 5128:             $rolename = &Apache::lonnet::plaintext($role);
 5129:         }
 5130:     }
 5131:     my (@fields,%fieldtitles);
 5132:     if (ref($fieldsref) eq 'ARRAY') {
 5133:         @fields = @{$fieldsref};
 5134:     } else {
 5135:         @fields = ('lastname','firstname','middlename','generation',
 5136:                    'permanentemail','id');
 5137:     }
 5138:     if ((ref($titlesref) eq 'HASH')) {
 5139:         %fieldtitles = %{$titlesref};
 5140:     } else {
 5141:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5142:     }
 5143:     my $output;
 5144:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 5145:     $output = '<tr '.$css_class.'>'.
 5146:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 5147:               '<td class="LC_left_item" colspan="2"><table>';
 5148:     my $rem;
 5149:     my %checks;
 5150:     if (ref($settings) eq 'HASH') {
 5151:         if (ref($settings->{$context}) eq 'HASH') {
 5152:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 5153:                 my $hashref = $settings->{$context}->{$role};
 5154:                 if ($role eq 'emailusername') {
 5155:                     if ($statustype) {
 5156:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 5157:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 5158:                             if (ref($hashref) eq 'HASH') { 
 5159:                                 foreach my $field (@fields) {
 5160:                                     if ($hashref->{$field}) {
 5161:                                         $checks{$field} = $hashref->{$field};
 5162:                                     }
 5163:                                 }
 5164:                             }
 5165:                         }
 5166:                     }
 5167:                 } else {
 5168:                     if (ref($hashref) eq 'HASH') {
 5169:                         foreach my $field (@fields) {
 5170:                             if ($hashref->{$field}) {
 5171:                                 $checks{$field} = ' checked="checked" ';
 5172:                             }
 5173:                         }
 5174:                     }
 5175:                 }
 5176:             }
 5177:         }
 5178:     }
 5179:      
 5180:     for (my $i=0; $i<@fields; $i++) {
 5181:         my $rem = $i%($numinrow);
 5182:         if ($rem == 0) {
 5183:             if ($i > 0) {
 5184:                 $output .= '</tr>';
 5185:             }
 5186:             $output .= '<tr>';
 5187:         }
 5188:         my $check = ' ';
 5189:         unless ($role eq 'emailusername') {
 5190:             if (exists($checks{$fields[$i]})) {
 5191:                 $check = $checks{$fields[$i]}
 5192:             } else {
 5193:                 if ($role eq 'st') {
 5194:                     if (ref($settings) ne 'HASH') {
 5195:                         $check = ' checked="checked" '; 
 5196:                     }
 5197:                 }
 5198:             }
 5199:         }
 5200:         $output .= '<td class="LC_left_item">'.
 5201:                    '<span class="LC_nobreak">';
 5202:         if ($role eq 'emailusername') {
 5203:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 5204:                 $checks{$fields[$i]} = 'omit';
 5205:             }
 5206:             foreach my $option ('required','optional','omit') {
 5207:                 my $checked='';
 5208:                 if ($checks{$fields[$i]} eq $option) {
 5209:                     $checked='checked="checked" ';
 5210:                 }
 5211:                 $output .= '<label>'.
 5212:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 5213:                            &mt($option).'</label>'.('&nbsp;' x2);
 5214:             }
 5215:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 5216:         } else {
 5217:             $output .= '<label>'.
 5218:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 5219:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 5220:                        '</label>';
 5221:         }
 5222:         $output .= '</span></td>';
 5223:         $rem = @fields%($numinrow);
 5224:     }
 5225:     my $colsleft = $numinrow - $rem;
 5226:     if ($colsleft > 1 ) {
 5227:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5228:                    '&nbsp;</td>';
 5229:     } elsif ($colsleft == 1) {
 5230:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 5231:     }
 5232:     $output .= '</tr></table></td></tr>';
 5233:     return $output;
 5234: }
 5235: 
 5236: sub insttypes_row {
 5237:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rownum) = @_;
 5238:     my %lt = &Apache::lonlocal::texthash (
 5239:                       cansearch => 'Users allowed to search',
 5240:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 5241:                       lockablenames => 'User preference to lock name',
 5242:              );
 5243:     my $showdom;
 5244:     if ($context eq 'cansearch') {
 5245:         $showdom = ' ('.$dom.')';
 5246:     }
 5247:     my $class = 'LC_left_item';
 5248:     if ($context eq 'statustocreate') {
 5249:         $class = 'LC_right_item';
 5250:     }
 5251:     my $css_class = ' class="LC_odd_row"';
 5252:     if ($rownum ne '') { 
 5253:         $css_class = ($rownum%2? ' class="LC_odd_row"':'');
 5254:     }
 5255:     my $output = '<tr'.$css_class.'>'.
 5256:                  '<td>'.$lt{$context}.$showdom.
 5257:                  '</td><td class="'.$class.'" colspan="2"><table>';
 5258:     my $rem;
 5259:     if (ref($types) eq 'ARRAY') {
 5260:         for (my $i=0; $i<@{$types}; $i++) {
 5261:             if (defined($usertypes->{$types->[$i]})) {
 5262:                 my $rem = $i%($numinrow);
 5263:                 if ($rem == 0) {
 5264:                     if ($i > 0) {
 5265:                         $output .= '</tr>';
 5266:                     }
 5267:                     $output .= '<tr>';
 5268:                 }
 5269:                 my $check = ' ';
 5270:                 if (ref($settings) eq 'HASH') {
 5271:                     if (ref($settings->{$context}) eq 'ARRAY') {
 5272:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 5273:                             $check = ' checked="checked" ';
 5274:                         }
 5275:                     } elsif ($context eq 'statustocreate') {
 5276:                         $check = ' checked="checked" ';
 5277:                     }
 5278:                 }
 5279:                 $output .= '<td class="LC_left_item">'.
 5280:                            '<span class="LC_nobreak"><label>'.
 5281:                            '<input type="checkbox" name="'.$context.'" '.
 5282:                            'value="'.$types->[$i].'"'.$check.'/>'.
 5283:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 5284:             }
 5285:         }
 5286:         $rem = @{$types}%($numinrow);
 5287:     }
 5288:     my $colsleft = $numinrow - $rem;
 5289:     if (($rem == 0) && (@{$types} > 0)) {
 5290:         $output .= '<tr>';
 5291:     }
 5292:     if ($colsleft > 1) {
 5293:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5294:     } else {
 5295:         $output .= '<td class="LC_left_item">';
 5296:     }
 5297:     my $defcheck = ' ';
 5298:     if (ref($settings) eq 'HASH') {  
 5299:         if (ref($settings->{$context}) eq 'ARRAY') {
 5300:             if (grep(/^default$/,@{$settings->{$context}})) {
 5301:                 $defcheck = ' checked="checked" ';
 5302:             }
 5303:         } elsif ($context eq 'statustocreate') {
 5304:             $defcheck = ' checked="checked" ';
 5305:         }
 5306:     }
 5307:     $output .= '<span class="LC_nobreak"><label>'.
 5308:                '<input type="checkbox" name="'.$context.'" '.
 5309:                'value="default"'.$defcheck.'/>'.
 5310:                $othertitle.'</label></span></td>'.
 5311:                '</tr></table></td></tr>';
 5312:     return $output;
 5313: }
 5314: 
 5315: sub sorted_searchtitles {
 5316:     my %searchtitles = &Apache::lonlocal::texthash(
 5317:                          'uname' => 'username',
 5318:                          'lastname' => 'last name',
 5319:                          'lastfirst' => 'last name, first name',
 5320:                      );
 5321:     my @titleorder = ('uname','lastname','lastfirst');
 5322:     return (\%searchtitles,\@titleorder);
 5323: }
 5324: 
 5325: sub sorted_searchtypes {
 5326:     my %srchtypes_desc = (
 5327:                            exact    => 'is exact match',
 5328:                            contains => 'contains ..',
 5329:                            begins   => 'begins with ..',
 5330:                          );
 5331:     my @srchtypeorder = ('exact','begins','contains');
 5332:     return (\%srchtypes_desc,\@srchtypeorder);
 5333: }
 5334: 
 5335: sub usertype_update_row {
 5336:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 5337:     my $datatable;
 5338:     my $numinrow = 4;
 5339:     foreach my $type (@{$types}) {
 5340:         if (defined($usertypes->{$type})) {
 5341:             $$rownums ++;
 5342:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 5343:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 5344:                           '</td><td class="LC_left_item"><table>';
 5345:             for (my $i=0; $i<@{$fields}; $i++) {
 5346:                 my $rem = $i%($numinrow);
 5347:                 if ($rem == 0) {
 5348:                     if ($i > 0) {
 5349:                         $datatable .= '</tr>';
 5350:                     }
 5351:                     $datatable .= '<tr>';
 5352:                 }
 5353:                 my $check = ' ';
 5354:                 if (ref($settings) eq 'HASH') {
 5355:                     if (ref($settings->{'fields'}) eq 'HASH') {
 5356:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 5357:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 5358:                                 $check = ' checked="checked" ';
 5359:                             }
 5360:                         }
 5361:                     }
 5362:                 }
 5363: 
 5364:                 if ($i == @{$fields}-1) {
 5365:                     my $colsleft = $numinrow - $rem;
 5366:                     if ($colsleft > 1) {
 5367:                         $datatable .= '<td colspan="'.$colsleft.'">';
 5368:                     } else {
 5369:                         $datatable .= '<td>';
 5370:                     }
 5371:                 } else {
 5372:                     $datatable .= '<td>';
 5373:                 }
 5374:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5375:                               '<input type="checkbox" name="updateable_'.$type.
 5376:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 5377:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 5378:             }
 5379:             $datatable .= '</tr></table></td></tr>';
 5380:         }
 5381:     }
 5382:     return $datatable;
 5383: }
 5384: 
 5385: sub modify_login {
 5386:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 5387:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 5388:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 5389:     %title = ( coursecatalog => 'Display course catalog',
 5390:                adminmail => 'Display administrator E-mail address',
 5391:                helpdesk  => 'Display "Contact Helpdesk" link',
 5392:                newuser => 'Link for visitors to create a user account',
 5393:                loginheader => 'Log-in box header');
 5394:     @offon = ('off','on');
 5395:     if (ref($domconfig{login}) eq 'HASH') {
 5396:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 5397:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 5398:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 5399:             }
 5400:         }
 5401:     }
 5402:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 5403:                                            \%domconfig,\%loginhash);
 5404:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5405:     foreach my $item (@toggles) {
 5406:         $loginhash{login}{$item} = $env{'form.'.$item};
 5407:     }
 5408:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 5409:     if (ref($colchanges{'login'}) eq 'HASH') {  
 5410:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 5411:                                          \%loginhash);
 5412:     }
 5413: 
 5414:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5415:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 5416:     if (keys(%servers) > 1) {
 5417:         foreach my $lonhost (keys(%servers)) {
 5418:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 5419:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 5420:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 5421:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 5422:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 5423:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5424:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5425:                         $changes{'loginvia'}{$lonhost} = 1;
 5426:                     } else {
 5427:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 5428:                         $changes{'loginvia'}{$lonhost} = 1;
 5429:                     }
 5430:                 } else {
 5431:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5432:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5433:                         $changes{'loginvia'}{$lonhost} = 1;
 5434:                     }
 5435:                 }
 5436:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 5437:                     foreach my $item (@loginvia_attribs) {
 5438:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 5439:                     }
 5440:                 } else {
 5441:                     foreach my $item (@loginvia_attribs) {
 5442:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5443:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5444:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 5445:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5446:                                 $new = '/';
 5447:                             }
 5448:                         }
 5449:                         if (($item eq 'custompath') && 
 5450:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5451:                             $new = '';
 5452:                         }
 5453:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 5454:                             $changes{'loginvia'}{$lonhost} = 1;
 5455:                         }
 5456:                         if ($item eq 'exempt') {
 5457:                             $new =~ s/^\s+//;
 5458:                             $new =~ s/\s+$//;
 5459:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 5460:                             my @okips;
 5461:                             foreach my $ip (@poss_ips) {
 5462:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 5463:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 5464:                                         push(@okips,$ip); 
 5465:                                     }
 5466:                                 }
 5467:                             }
 5468:                             if (@okips > 0) {
 5469:                                 $new = join(',',@okips); 
 5470:                             } else {
 5471:                                 $new = ''; 
 5472:                             }
 5473:                         }
 5474:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5475:                     }
 5476:                 }
 5477:             } else {
 5478:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5479:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5480:                     $changes{'loginvia'}{$lonhost} = 1;
 5481:                     foreach my $item (@loginvia_attribs) {
 5482:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5483:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5484:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5485:                                 $new = '/';
 5486:                             }
 5487:                         }
 5488:                         if (($item eq 'custompath') && 
 5489:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5490:                             $new = '';
 5491:                         }
 5492:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5493:                     }
 5494:                 }
 5495:             }
 5496:         }
 5497:     }
 5498: 
 5499:     my $servadm = $r->dir_config('lonAdmEMail');
 5500:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 5501:     if (ref($domconfig{'login'}) eq 'HASH') {
 5502:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 5503:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 5504:                 if ($lang eq 'nolang') {
 5505:                     push(@currlangs,$lang);
 5506:                 } elsif (defined($langchoices{$lang})) {
 5507:                     push(@currlangs,$lang);
 5508:                 } else {
 5509:                     next;
 5510:                 }
 5511:             }
 5512:         }
 5513:     }
 5514:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 5515:     if (@currlangs > 0) {
 5516:         foreach my $lang (@currlangs) {
 5517:             if (grep(/^\Q$lang\E$/,@delurls)) {
 5518:                 $changes{'helpurl'}{$lang} = 1;
 5519:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 5520:                 $changes{'helpurl'}{$lang} = 1;
 5521:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 5522:                 push(@newlangs,$lang);
 5523:             } else {
 5524:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5525:             }
 5526:         }
 5527:     }
 5528:     unless (grep(/^nolang$/,@currlangs)) {
 5529:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 5530:             $changes{'helpurl'}{'nolang'} = 1;
 5531:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 5532:             push(@newlangs,'nolang');
 5533:         }
 5534:     }
 5535:     if ($env{'form.loginhelpurl_add_lang'}) {
 5536:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 5537:             ($env{'form.loginhelpurl_add_file.filename'})) {
 5538:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 5539:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 5540:         }
 5541:     }
 5542:     if ((@newlangs > 0) || ($addedfile)) {
 5543:         my $error;
 5544:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 5545:         if ($configuserok eq 'ok') {
 5546:             if ($switchserver) {
 5547:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 5548:             } elsif ($author_ok eq 'ok') {
 5549:                 my @allnew = @newlangs;
 5550:                 if ($addedfile ne '') {
 5551:                     push(@allnew,$addedfile);
 5552:                 }
 5553:                 foreach my $lang (@allnew) {
 5554:                     my $formelem = 'loginhelpurl_'.$lang;
 5555:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 5556:                         $formelem = 'loginhelpurl_add_file';
 5557:                     }
 5558:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 5559:                                                                "help/$lang",'','',$newfile{$lang});
 5560:                     if ($result eq 'ok') {
 5561:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 5562:                         $changes{'helpurl'}{$lang} = 1;
 5563:                     } else {
 5564:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 5565:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 5566:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 5567:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 5568: 
 5569:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5570:                         }
 5571:                     }
 5572:                 }
 5573:             } else {
 5574:                 $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);
 5575:             }
 5576:         } else {
 5577:             $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);
 5578:         }
 5579:         if ($error) {
 5580:             &Apache::lonnet::logthis($error);
 5581:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5582:         }
 5583:     }
 5584:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 5585: 
 5586:     my $defaulthelpfile = '/adm/loginproblems.html';
 5587:     my $defaulttext = &mt('Default in use');
 5588: 
 5589:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 5590:                                              $dom);
 5591:     if ($putresult eq 'ok') {
 5592:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5593:         my %defaultchecked = (
 5594:                     'coursecatalog' => 'on',
 5595:                     'helpdesk'      => 'on',
 5596:                     'adminmail'     => 'off',
 5597:                     'newuser'       => 'off',
 5598:         );
 5599:         if (ref($domconfig{'login'}) eq 'HASH') {
 5600:             foreach my $item (@toggles) {
 5601:                 if ($defaultchecked{$item} eq 'on') { 
 5602:                     if (($domconfig{'login'}{$item} eq '0') &&
 5603:                         ($env{'form.'.$item} eq '1')) {
 5604:                         $changes{$item} = 1;
 5605:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 5606:                               $domconfig{'login'}{$item} eq '1') &&
 5607:                              ($env{'form.'.$item} eq '0')) {
 5608:                         $changes{$item} = 1;
 5609:                     }
 5610:                 } elsif ($defaultchecked{$item} eq 'off') {
 5611:                     if (($domconfig{'login'}{$item} eq '1') &&
 5612:                         ($env{'form.'.$item} eq '0')) {
 5613:                         $changes{$item} = 1;
 5614:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 5615:                               $domconfig{'login'}{$item} eq '0') &&
 5616:                              ($env{'form.'.$item} eq '1')) {
 5617:                         $changes{$item} = 1;
 5618:                     }
 5619:                 }
 5620:             }
 5621:         }
 5622:         if (keys(%changes) > 0 || $colchgtext) {
 5623:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5624:             if (ref($lastactref) eq 'HASH') {
 5625:                 $lastactref->{'domainconfig'} = 1;
 5626:             }
 5627:             $resulttext = &mt('Changes made:').'<ul>';
 5628:             foreach my $item (sort(keys(%changes))) {
 5629:                 if ($item eq 'loginvia') {
 5630:                     if (ref($changes{$item}) eq 'HASH') {
 5631:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 5632:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 5633:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 5634:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 5635:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 5636:                                     $protocol = 'http' if ($protocol ne 'https');
 5637:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 5638: 
 5639:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 5640:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 5641:                                     } else {
 5642:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 5643:                                     }
 5644:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 5645:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 5646:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 5647:                                     }
 5648:                                     $resulttext .= '</li>';
 5649:                                 } else {
 5650:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 5651:                                 }
 5652:                             } else {
 5653:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 5654:                             }
 5655:                         }
 5656:                         $resulttext .= '</ul></li>';
 5657:                     }
 5658:                 } elsif ($item eq 'helpurl') {
 5659:                     if (ref($changes{$item}) eq 'HASH') {
 5660:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 5661:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 5662:                                 my ($chg,$link);
 5663:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 5664:                                 if ($lang eq 'nolang') {
 5665:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 5666:                                 } else {
 5667:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 5668:                                 }
 5669:                                 $resulttext .= '<li>'.$chg.'</li>';
 5670:                             } else {
 5671:                                 my $chg;
 5672:                                 if ($lang eq 'nolang') {
 5673:                                     $chg = &mt('custom log-in help file for no preferred language');
 5674:                                 } else {
 5675:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 5676:                                 }
 5677:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 5678:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 5679:                                                       '?inhibitmenu=yes',$chg,600,500).
 5680:                                                '</li>';
 5681:                             }
 5682:                         }
 5683:                     }
 5684:                 } elsif ($item eq 'captcha') {
 5685:                     if (ref($loginhash{'login'}) eq 'HASH') {
 5686:                         my $chgtxt;
 5687:                         if ($loginhash{'login'}{$item} eq 'notused') {
 5688:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 5689:                         } else {
 5690:                             my %captchas = &captcha_phrases();
 5691:                             if ($captchas{$loginhash{'login'}{$item}}) {
 5692:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 5693:                             } else {
 5694:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 5695:                             }
 5696:                         }
 5697:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 5698:                     }
 5699:                 } elsif ($item eq 'recaptchakeys') {
 5700:                     if (ref($loginhash{'login'}) eq 'HASH') {
 5701:                         my ($privkey,$pubkey);
 5702:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 5703:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 5704:                             $privkey = $loginhash{'login'}{$item}{'private'};
 5705:                         }
 5706:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 5707:                         if (!$pubkey) {
 5708:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 5709:                         } else {
 5710:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 5711:                         }
 5712:                         if (!$privkey) {
 5713:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 5714:                         } else {
 5715:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 5716:                         }
 5717:                         $chgtxt .= '</ul>';
 5718:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 5719:                     }
 5720:                 } else {
 5721:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 5722:                 }
 5723:             }
 5724:             $resulttext .= $colchgtext.'</ul>';
 5725:         } else {
 5726:             $resulttext = &mt('No changes made to log-in page settings');
 5727:         }
 5728:     } else {
 5729:         $resulttext = '<span class="LC_error">'.
 5730: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5731:     }
 5732:     if ($errors) {
 5733:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 5734:                        $errors.'</ul>';
 5735:     }
 5736:     return $resulttext;
 5737: }
 5738: 
 5739: sub color_font_choices {
 5740:     my %choices =
 5741:         &Apache::lonlocal::texthash (
 5742:             img => "Header",
 5743:             bgs => "Background colors",
 5744:             links => "Link colors",
 5745:             images => "Images",
 5746:             font => "Font color",
 5747:             fontmenu => "Font menu",
 5748:             pgbg => "Page",
 5749:             tabbg => "Header",
 5750:             sidebg => "Border",
 5751:             link => "Link",
 5752:             alink => "Active link",
 5753:             vlink => "Visited link",
 5754:         );
 5755:     return %choices;
 5756: }
 5757: 
 5758: sub modify_rolecolors {
 5759:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 5760:     my ($resulttext,%rolehash);
 5761:     $rolehash{'rolecolors'} = {};
 5762:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 5763:         if ($domconfig{'rolecolors'} eq '') {
 5764:             $domconfig{'rolecolors'} = {};
 5765:         }
 5766:     }
 5767:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 5768:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 5769:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 5770:                                              $dom);
 5771:     if ($putresult eq 'ok') {
 5772:         if (keys(%changes) > 0) {
 5773:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5774:             if (ref($lastactref) eq 'HASH') {
 5775:                 $lastactref->{'domainconfig'} = 1;
 5776:             }
 5777:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 5778:                                              $rolehash{'rolecolors'});
 5779:         } else {
 5780:             $resulttext = &mt('No changes made to default color schemes');
 5781:         }
 5782:     } else {
 5783:         $resulttext = '<span class="LC_error">'.
 5784: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5785:     }
 5786:     if ($errors) {
 5787:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 5788:                        $errors.'</ul>';
 5789:     }
 5790:     return $resulttext;
 5791: }
 5792: 
 5793: sub modify_colors {
 5794:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 5795:     my (%changes,%choices);
 5796:     my @bgs;
 5797:     my @links = ('link','alink','vlink');
 5798:     my @logintext;
 5799:     my @images;
 5800:     my $servadm = $r->dir_config('lonAdmEMail');
 5801:     my $errors;
 5802:     my %defaults;
 5803:     foreach my $role (@{$roles}) {
 5804:         if ($role eq 'login') {
 5805:             %choices = &login_choices();
 5806:             @logintext = ('textcol','bgcol');
 5807:         } else {
 5808:             %choices = &color_font_choices();
 5809:         }
 5810:         if ($role eq 'login') {
 5811:             @images = ('img','logo','domlogo','login');
 5812:             @bgs = ('pgbg','mainbg','sidebg');
 5813:         } else {
 5814:             @images = ('img');
 5815:             @bgs = ('pgbg','tabbg','sidebg');
 5816:         }
 5817:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 5818:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 5819:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 5820:         }
 5821:         if ($role eq 'login') {
 5822:             foreach my $item (@logintext) {
 5823:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 5824:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 5825:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 5826:                 }
 5827:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 5828:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5829:                 }
 5830:             }
 5831:         } else {
 5832:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 5833:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 5834:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 5835:             }
 5836:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 5837:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 5838:             }
 5839:         }
 5840:         foreach my $item (@bgs) {
 5841:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 5842:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 5843:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 5844:             }
 5845:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 5846:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5847:             }
 5848:         }
 5849:         foreach my $item (@links) {
 5850:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 5851:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 5852:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 5853:             }
 5854:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 5855:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5856:             }
 5857:         }
 5858:         my ($configuserok,$author_ok,$switchserver) = 
 5859:             &config_check($dom,$confname,$servadm);
 5860:         my ($width,$height) = &thumb_dimensions();
 5861:         if (ref($domconfig->{$role}) ne 'HASH') {
 5862:             $domconfig->{$role} = {};
 5863:         }
 5864:         foreach my $img (@images) {
 5865:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 5866:                 if (defined($env{'form.login_showlogo_'.$img})) {
 5867:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 5868:                 } else { 
 5869:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 5870:                 }
 5871:             } 
 5872: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 5873: 		 && !defined($domconfig->{$role}{$img})
 5874: 		 && !$env{'form.'.$role.'_del_'.$img}
 5875: 		 && $env{'form.'.$role.'_import_'.$img}) {
 5876: 		# import the old configured image from the .tab setting
 5877: 		# if they haven't provided a new one 
 5878: 		$domconfig->{$role}{$img} = 
 5879: 		    $env{'form.'.$role.'_import_'.$img};
 5880: 	    }
 5881:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 5882:                 my $error;
 5883:                 if ($configuserok eq 'ok') {
 5884:                     if ($switchserver) {
 5885:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 5886:                     } else {
 5887:                         if ($author_ok eq 'ok') {
 5888:                             my ($result,$logourl) = 
 5889:                                 &publishlogo($r,'upload',$role.'_'.$img,
 5890:                                            $dom,$confname,$img,$width,$height);
 5891:                             if ($result eq 'ok') {
 5892:                                 $confhash->{$role}{$img} = $logourl;
 5893:                                 $changes{$role}{'images'}{$img} = 1;
 5894:                             } else {
 5895:                                 $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);
 5896:                             }
 5897:                         } else {
 5898:                             $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);
 5899:                         }
 5900:                     }
 5901:                 } else {
 5902:                     $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);
 5903:                 }
 5904:                 if ($error) {
 5905:                     &Apache::lonnet::logthis($error);
 5906:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5907:                 }
 5908:             } elsif ($domconfig->{$role}{$img} ne '') {
 5909:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 5910:                     my $error;
 5911:                     if ($configuserok eq 'ok') {
 5912: # is confname an author?
 5913:                         if ($switchserver eq '') {
 5914:                             if ($author_ok eq 'ok') {
 5915:                                 my ($result,$logourl) = 
 5916:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 5917:                                             $dom,$confname,$img,$width,$height);
 5918:                                 if ($result eq 'ok') {
 5919:                                     $confhash->{$role}{$img} = $logourl;
 5920: 				    $changes{$role}{'images'}{$img} = 1;
 5921:                                 }
 5922:                             }
 5923:                         }
 5924:                     }
 5925:                 }
 5926:             }
 5927:         }
 5928:         if (ref($domconfig) eq 'HASH') {
 5929:             if (ref($domconfig->{$role}) eq 'HASH') {
 5930:                 foreach my $img (@images) {
 5931:                     if ($domconfig->{$role}{$img} ne '') {
 5932:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5933:                             $confhash->{$role}{$img} = '';
 5934:                             $changes{$role}{'images'}{$img} = 1;
 5935:                         } else {
 5936:                             if ($confhash->{$role}{$img} eq '') {
 5937:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 5938:                             }
 5939:                         }
 5940:                     } else {
 5941:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5942:                             $confhash->{$role}{$img} = '';
 5943:                             $changes{$role}{'images'}{$img} = 1;
 5944:                         } 
 5945:                     }
 5946:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 5947:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 5948:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 5949:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 5950:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 5951:                             }
 5952:                         } else {
 5953:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5954:                                 $changes{$role}{'showlogo'}{$img} = 1;
 5955:                             }
 5956:                         }
 5957:                     }
 5958:                 }
 5959:                 if ($domconfig->{$role}{'font'} ne '') {
 5960:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 5961:                         $changes{$role}{'font'} = 1;
 5962:                     }
 5963:                 } else {
 5964:                     if ($confhash->{$role}{'font'}) {
 5965:                         $changes{$role}{'font'} = 1;
 5966:                     }
 5967:                 }
 5968:                 if ($role ne 'login') {
 5969:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 5970:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 5971:                             $changes{$role}{'fontmenu'} = 1;
 5972:                         }
 5973:                     } else {
 5974:                         if ($confhash->{$role}{'fontmenu'}) {
 5975:                             $changes{$role}{'fontmenu'} = 1;
 5976:                         }
 5977:                     }
 5978:                 }
 5979:                 foreach my $item (@bgs) {
 5980:                     if ($domconfig->{$role}{$item} ne '') {
 5981:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5982:                             $changes{$role}{'bgs'}{$item} = 1;
 5983:                         } 
 5984:                     } else {
 5985:                         if ($confhash->{$role}{$item}) {
 5986:                             $changes{$role}{'bgs'}{$item} = 1;
 5987:                         }
 5988:                     }
 5989:                 }
 5990:                 foreach my $item (@links) {
 5991:                     if ($domconfig->{$role}{$item} ne '') {
 5992:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5993:                             $changes{$role}{'links'}{$item} = 1;
 5994:                         }
 5995:                     } else {
 5996:                         if ($confhash->{$role}{$item}) {
 5997:                             $changes{$role}{'links'}{$item} = 1;
 5998:                         }
 5999:                     }
 6000:                 }
 6001:                 foreach my $item (@logintext) {
 6002:                     if ($domconfig->{$role}{$item} ne '') {
 6003:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6004:                             $changes{$role}{'logintext'}{$item} = 1;
 6005:                         }
 6006:                     } else {
 6007:                         if ($confhash->{$role}{$item}) {
 6008:                             $changes{$role}{'logintext'}{$item} = 1;
 6009:                         }
 6010:                     }
 6011:                 }
 6012:             } else {
 6013:                 &default_change_checker($role,\@images,\@links,\@bgs,
 6014:                                         \@logintext,$confhash,\%changes); 
 6015:             }
 6016:         } else {
 6017:             &default_change_checker($role,\@images,\@links,\@bgs,
 6018:                                     \@logintext,$confhash,\%changes); 
 6019:         }
 6020:     }
 6021:     return ($errors,%changes);
 6022: }
 6023: 
 6024: sub config_check {
 6025:     my ($dom,$confname,$servadm) = @_;
 6026:     my ($configuserok,$author_ok,$switchserver,%currroles);
 6027:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 6028:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 6029:                                                    $confname,$servadm);
 6030:     if ($configuserok eq 'ok') {
 6031:         $switchserver = &check_switchserver($dom,$confname);
 6032:         if ($switchserver eq '') {
 6033:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 6034:         }
 6035:     }
 6036:     return ($configuserok,$author_ok,$switchserver);
 6037: }
 6038: 
 6039: sub default_change_checker {
 6040:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 6041:     foreach my $item (@{$links}) {
 6042:         if ($confhash->{$role}{$item}) {
 6043:             $changes->{$role}{'links'}{$item} = 1;
 6044:         }
 6045:     }
 6046:     foreach my $item (@{$bgs}) {
 6047:         if ($confhash->{$role}{$item}) {
 6048:             $changes->{$role}{'bgs'}{$item} = 1;
 6049:         }
 6050:     }
 6051:     foreach my $item (@{$logintext}) {
 6052:         if ($confhash->{$role}{$item}) {
 6053:             $changes->{$role}{'logintext'}{$item} = 1;
 6054:         }
 6055:     }
 6056:     foreach my $img (@{$images}) {
 6057:         if ($env{'form.'.$role.'_del_'.$img}) {
 6058:             $confhash->{$role}{$img} = '';
 6059:             $changes->{$role}{'images'}{$img} = 1;
 6060:         }
 6061:         if ($role eq 'login') {
 6062:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 6063:                 $changes->{$role}{'showlogo'}{$img} = 1;
 6064:             }
 6065:         }
 6066:     }
 6067:     if ($confhash->{$role}{'font'}) {
 6068:         $changes->{$role}{'font'} = 1;
 6069:     }
 6070: }
 6071: 
 6072: sub display_colorchgs {
 6073:     my ($dom,$changes,$roles,$confhash) = @_;
 6074:     my (%choices,$resulttext);
 6075:     if (!grep(/^login$/,@{$roles})) {
 6076:         $resulttext = &mt('Changes made:').'<br />';
 6077:     }
 6078:     foreach my $role (@{$roles}) {
 6079:         if ($role eq 'login') {
 6080:             %choices = &login_choices();
 6081:         } else {
 6082:             %choices = &color_font_choices();
 6083:         }
 6084:         if (ref($changes->{$role}) eq 'HASH') {
 6085:             if ($role ne 'login') {
 6086:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 6087:             }
 6088:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 6089:                 if ($role ne 'login') {
 6090:                     $resulttext .= '<ul>';
 6091:                 }
 6092:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 6093:                     if ($role ne 'login') {
 6094:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 6095:                     }
 6096:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 6097:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 6098:                             if ($confhash->{$role}{$key}{$item}) {
 6099:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 6100:                             } else {
 6101:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 6102:                             }
 6103:                         } elsif ($confhash->{$role}{$item} eq '') {
 6104:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 6105:                         } else {
 6106:                             my $newitem = $confhash->{$role}{$item};
 6107:                             if ($key eq 'images') {
 6108:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 6109:                             }
 6110:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 6111:                         }
 6112:                     }
 6113:                     if ($role ne 'login') {
 6114:                         $resulttext .= '</ul></li>';
 6115:                     }
 6116:                 } else {
 6117:                     if ($confhash->{$role}{$key} eq '') {
 6118:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 6119:                     } else {
 6120:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 6121:                     }
 6122:                 }
 6123:                 if ($role ne 'login') {
 6124:                     $resulttext .= '</ul>';
 6125:                 }
 6126:             }
 6127:         }
 6128:     }
 6129:     return $resulttext;
 6130: }
 6131: 
 6132: sub thumb_dimensions {
 6133:     return ('200','50');
 6134: }
 6135: 
 6136: sub check_dimensions {
 6137:     my ($inputfile) = @_;
 6138:     my ($fullwidth,$fullheight);
 6139:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 6140:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 6141:             my $imageinfo = <PIPE>;
 6142:             if (!close(PIPE)) {
 6143:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 6144:             }
 6145:             chomp($imageinfo);
 6146:             my ($fullsize) = 
 6147:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 6148:             if ($fullsize) {
 6149:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 6150:             }
 6151:         }
 6152:     }
 6153:     return ($fullwidth,$fullheight);
 6154: }
 6155: 
 6156: sub check_configuser {
 6157:     my ($uhome,$dom,$confname,$servadm) = @_;
 6158:     my ($configuserok,%currroles);
 6159:     if ($uhome eq 'no_host') {
 6160:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 6161:         my $configpass = &LONCAPA::Enrollment::create_password();
 6162:         $configuserok = 
 6163:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 6164:                              $configpass,'','','','','',undef,$servadm);
 6165:     } else {
 6166:         $configuserok = 'ok';
 6167:         %currroles = 
 6168:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 6169:     }
 6170:     return ($configuserok,%currroles);
 6171: }
 6172: 
 6173: sub check_authorstatus {
 6174:     my ($dom,$confname,%currroles) = @_;
 6175:     my $author_ok;
 6176:     if (!$currroles{':'.$dom.':au'}) {
 6177:         my $start = time;
 6178:         my $end = 0;
 6179:         $author_ok = 
 6180:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 6181:                                         'au',$end,$start,'','','domconfig');
 6182:     } else {
 6183:         $author_ok = 'ok';
 6184:     }
 6185:     return $author_ok;
 6186: }
 6187: 
 6188: sub publishlogo {
 6189:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 6190:     my ($output,$fname,$logourl);
 6191:     if ($action eq 'upload') {
 6192:         $fname=$env{'form.'.$formname.'.filename'};
 6193:         chop($env{'form.'.$formname});
 6194:     } else {
 6195:         ($fname) = ($formname =~ /([^\/]+)$/);
 6196:     }
 6197:     if ($savefileas ne '') {
 6198:         $fname = $savefileas;
 6199:     }
 6200:     $fname=&Apache::lonnet::clean_filename($fname);
 6201: # See if there is anything left
 6202:     unless ($fname) { return ('error: no uploaded file'); }
 6203:     $fname="$subdir/$fname";
 6204:     my $docroot=$r->dir_config('lonDocRoot');
 6205:     my $filepath="$docroot/priv";
 6206:     my $relpath = "$dom/$confname";
 6207:     my ($fnamepath,$file,$fetchthumb);
 6208:     $file=$fname;
 6209:     if ($fname=~m|/|) {
 6210:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 6211:     }
 6212:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 6213:     my $count;
 6214:     for ($count=5;$count<=$#parts;$count++) {
 6215:         $filepath.="/$parts[$count]";
 6216:         if ((-e $filepath)!=1) {
 6217:             mkdir($filepath,02770);
 6218:         }
 6219:     }
 6220:     # Check for bad extension and disallow upload
 6221:     if ($file=~/\.(\w+)$/ &&
 6222:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 6223:         $output = 
 6224:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 6225:     } elsif ($file=~/\.(\w+)$/ &&
 6226:         !defined(&Apache::loncommon::fileembstyle($1))) {
 6227:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 6228:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 6229:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 6230:     } elsif (-d "$filepath/$file") {
 6231:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 6232:     } else {
 6233:         my $source = $filepath.'/'.$file;
 6234:         my $logfile;
 6235:         if (!open($logfile,">>$source".'.log')) {
 6236:             return (&mt('No write permission to Authoring Space'));
 6237:         }
 6238:         print $logfile
 6239: "\n================= Publish ".localtime()." ================\n".
 6240: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 6241: # Save the file
 6242:         if (!open(FH,'>'.$source)) {
 6243:             &Apache::lonnet::logthis('Failed to create '.$source);
 6244:             return (&mt('Failed to create file'));
 6245:         }
 6246:         if ($action eq 'upload') {
 6247:             if (!print FH ($env{'form.'.$formname})) {
 6248:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 6249:                 return (&mt('Failed to write file'));
 6250:             }
 6251:         } else {
 6252:             my $original = &Apache::lonnet::filelocation('',$formname);
 6253:             if(!copy($original,$source)) {
 6254:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 6255:                 return (&mt('Failed to write file'));
 6256:             }
 6257:         }
 6258:         close(FH);
 6259:         chmod(0660, $source); # Permissions to rw-rw---.
 6260: 
 6261:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 6262:         my $copyfile=$targetdir.'/'.$file;
 6263: 
 6264:         my @parts=split(/\//,$targetdir);
 6265:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 6266:         for (my $count=5;$count<=$#parts;$count++) {
 6267:             $path.="/$parts[$count]";
 6268:             if (!-e $path) {
 6269:                 print $logfile "\nCreating directory ".$path;
 6270:                 mkdir($path,02770);
 6271:             }
 6272:         }
 6273:         my $versionresult;
 6274:         if (-e $copyfile) {
 6275:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 6276:         } else {
 6277:             $versionresult = 'ok';
 6278:         }
 6279:         if ($versionresult eq 'ok') {
 6280:             if (copy($source,$copyfile)) {
 6281:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 6282:                 $output = 'ok';
 6283:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 6284:                 push(@{$modified_urls},[$copyfile,$source]);
 6285:                 my $metaoutput = 
 6286:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 6287:                 unless ($registered_cleanup) {
 6288:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 6289:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 6290:                     $registered_cleanup=1;
 6291:                 }
 6292:             } else {
 6293:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 6294:                 $output = &mt('Failed to copy file to RES space').", $!";
 6295:             }
 6296:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 6297:                 my $inputfile = $filepath.'/'.$file;
 6298:                 my $outfile = $filepath.'/'.'tn-'.$file;
 6299:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 6300:                 if ($fullwidth ne '' && $fullheight ne '') { 
 6301:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 6302:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 6303:                         system("convert -sample $thumbsize $inputfile $outfile");
 6304:                         chmod(0660, $filepath.'/tn-'.$file);
 6305:                         if (-e $outfile) {
 6306:                             my $copyfile=$targetdir.'/tn-'.$file;
 6307:                             if (copy($outfile,$copyfile)) {
 6308:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 6309:                                 my $thumb_metaoutput = 
 6310:                                     &write_metadata($dom,$confname,$formname,
 6311:                                                     $targetdir,'tn-'.$file,$logfile);
 6312:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 6313:                                 unless ($registered_cleanup) {
 6314:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 6315:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 6316:                                     $registered_cleanup=1;
 6317:                                 }
 6318:                             } else {
 6319:                                 print $logfile "\nUnable to write ".$copyfile.
 6320:                                                ':'.$!."\n";
 6321:                             }
 6322:                         }
 6323:                     }
 6324:                 }
 6325:             }
 6326:         } else {
 6327:             $output = $versionresult;
 6328:         }
 6329:     }
 6330:     return ($output,$logourl);
 6331: }
 6332: 
 6333: sub logo_versioning {
 6334:     my ($targetdir,$file,$logfile) = @_;
 6335:     my $target = $targetdir.'/'.$file;
 6336:     my ($maxversion,$fn,$extn,$output);
 6337:     $maxversion = 0;
 6338:     if ($file =~ /^(.+)\.(\w+)$/) {
 6339:         $fn=$1;
 6340:         $extn=$2;
 6341:     }
 6342:     opendir(DIR,$targetdir);
 6343:     while (my $filename=readdir(DIR)) {
 6344:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 6345:             $maxversion=($1>$maxversion)?$1:$maxversion;
 6346:         }
 6347:     }
 6348:     $maxversion++;
 6349:     print $logfile "\nCreating old version ".$maxversion."\n";
 6350:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 6351:     if (copy($target,$copyfile)) {
 6352:         print $logfile "Copied old target to ".$copyfile."\n";
 6353:         $copyfile=$copyfile.'.meta';
 6354:         if (copy($target.'.meta',$copyfile)) {
 6355:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 6356:             $output = 'ok';
 6357:         } else {
 6358:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 6359:             $output = &mt('Failed to copy old meta').", $!, ";
 6360:         }
 6361:     } else {
 6362:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 6363:         $output = &mt('Failed to copy old target').", $!, ";
 6364:     }
 6365:     return $output;
 6366: }
 6367: 
 6368: sub write_metadata {
 6369:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 6370:     my (%metadatafields,%metadatakeys,$output);
 6371:     $metadatafields{'title'}=$formname;
 6372:     $metadatafields{'creationdate'}=time;
 6373:     $metadatafields{'lastrevisiondate'}=time;
 6374:     $metadatafields{'copyright'}='public';
 6375:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 6376:                                          $env{'user.domain'};
 6377:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 6378:     $metadatafields{'domain'}=$dom;
 6379:     {
 6380:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 6381:         my $mfh;
 6382:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 6383:             foreach (sort(keys(%metadatafields))) {
 6384:                 unless ($_=~/\./) {
 6385:                     my $unikey=$_;
 6386:                     $unikey=~/^([A-Za-z]+)/;
 6387:                     my $tag=$1;
 6388:                     $tag=~tr/A-Z/a-z/;
 6389:                     print $mfh "\n\<$tag";
 6390:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 6391:                         my $value=$metadatafields{$unikey.'.'.$_};
 6392:                         $value=~s/\"/\'\'/g;
 6393:                         print $mfh ' '.$_.'="'.$value.'"';
 6394:                     }
 6395:                     print $mfh '>'.
 6396:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 6397:                             .'</'.$tag.'>';
 6398:                 }
 6399:             }
 6400:             $output = 'ok';
 6401:             print $logfile "\nWrote metadata";
 6402:             close($mfh);
 6403:         } else {
 6404:             print $logfile "\nFailed to open metadata file";
 6405:             $output = &mt('Could not write metadata');
 6406:         }
 6407:     }
 6408:     return $output;
 6409: }
 6410: 
 6411: sub notifysubscribed {
 6412:     foreach my $targetsource (@{$modified_urls}){
 6413:         next unless (ref($targetsource) eq 'ARRAY');
 6414:         my ($target,$source)=@{$targetsource};
 6415:         if ($source ne '') {
 6416:             if (open(my $logfh,'>>'.$source.'.log')) {
 6417:                 print $logfh "\nCleanup phase: Notifications\n";
 6418:                 my @subscribed=&subscribed_hosts($target);
 6419:                 foreach my $subhost (@subscribed) {
 6420:                     print $logfh "\nNotifying host ".$subhost.':';
 6421:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 6422:                     print $logfh $reply;
 6423:                 }
 6424:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 6425:                 foreach my $subhost (@subscribedmeta) {
 6426:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 6427:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 6428:                                                         $subhost);
 6429:                     print $logfh $reply;
 6430:                 }
 6431:                 print $logfh "\n============ Done ============\n";
 6432:                 close($logfh);
 6433:             }
 6434:         }
 6435:     }
 6436:     return OK;
 6437: }
 6438: 
 6439: sub subscribed_hosts {
 6440:     my ($target) = @_;
 6441:     my @subscribed;
 6442:     if (open(my $fh,"<$target.subscription")) {
 6443:         while (my $subline=<$fh>) {
 6444:             if ($subline =~ /^($match_lonid):/) {
 6445:                 my $host = $1;
 6446:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 6447:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 6448:                         push(@subscribed,$host);
 6449:                     }
 6450:                 }
 6451:             }
 6452:         }
 6453:     }
 6454:     return @subscribed;
 6455: }
 6456: 
 6457: sub check_switchserver {
 6458:     my ($dom,$confname) = @_;
 6459:     my ($allowed,$switchserver);
 6460:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 6461:     if ($home eq 'no_host') {
 6462:         $home = &Apache::lonnet::domain($dom,'primary');
 6463:     }
 6464:     my @ids=&Apache::lonnet::current_machine_ids();
 6465:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 6466:     if (!$allowed) {
 6467: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 6468:     }
 6469:     return $switchserver;
 6470: }
 6471: 
 6472: sub modify_quotas {
 6473:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 6474:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 6475:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 6476:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 6477:         $validationfieldsref);
 6478:     if ($action eq 'quotas') {
 6479:         $context = 'tools'; 
 6480:     } else {
 6481:         $context = $action;
 6482:     }
 6483:     if ($context eq 'requestcourses') {
 6484:         @usertools = ('official','unofficial','community','textbook');
 6485:         @options =('norequest','approval','validate','autolimit');
 6486:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 6487:         %titles = &courserequest_titles();
 6488:         $toolregexp = join('|',@usertools);
 6489:         %conditions = &courserequest_conditions();
 6490:         $confname = $dom.'-domainconfig';
 6491:         my $servadm = $r->dir_config('lonAdmEMail');
 6492:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 6493:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 6494:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 6495:     } elsif ($context eq 'requestauthor') {
 6496:         @usertools = ('author');
 6497:         %titles = &authorrequest_titles();
 6498:     } else {
 6499:         @usertools = ('aboutme','blog','webdav','portfolio');
 6500:         %titles = &tool_titles();
 6501:     }
 6502:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6503:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6504:     foreach my $key (keys(%env)) {
 6505:         if ($context eq 'requestcourses') {
 6506:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 6507:                 my $item = $1;
 6508:                 my $type = $2;
 6509:                 if ($type =~ /^limit_(.+)/) {
 6510:                     $limithash{$item}{$1} = $env{$key};
 6511:                 } else {
 6512:                     $confhash{$item}{$type} = $env{$key};
 6513:                 }
 6514:             }
 6515:         } elsif ($context eq 'requestauthor') {
 6516:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 6517:                 $confhash{$1} = $env{$key};
 6518:             }
 6519:         } else {
 6520:             if ($key =~ /^form\.quota_(.+)$/) {
 6521:                 $confhash{'defaultquota'}{$1} = $env{$key};
 6522:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 6523:                 $confhash{'authorquota'}{$1} = $env{$key};
 6524:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 6525:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 6526:             }
 6527:         }
 6528:     }
 6529:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 6530:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 6531:         @approvalnotify = sort(@approvalnotify);
 6532:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 6533:         my @crstypes = ('official','unofficial','community','textbook');
 6534:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 6535:         foreach my $type (@hasuniquecode) {
 6536:             if (grep(/^\Q$type\E$/,@crstypes)) {
 6537:                 $confhash{'uniquecode'}{$type} = 1;
 6538:             }
 6539:         }
 6540:         my ($newbook,@allpos);
 6541:         if ($context eq 'requestcourses') {
 6542:             if ($env{'form.addbook'}) {
 6543:                 if (($env{'form.addbook_cnum'} =~ /^$match_courseid$/) &&
 6544:                     ($env{'form.addbook_cdom'} =~ /^$match_domain$/)) {
 6545:                     if (&Apache::lonnet::homeserver($env{'form.addbook_cnum'},
 6546:                                                     $env{'form.addbook_cdom'}) eq 'no_host') {
 6547:                         $errors .= '<li><span class="LC_error">'.&mt('Invalid LON-CAPA course for textbook').
 6548:                                    '</span></li>';
 6549:                     } else {
 6550:                         $newbook = $env{'form.addbook_cdom'}.'_'.$env{'form.addbook_cnum'};
 6551:                         my $position = $env{'form.addbook_pos'};
 6552:                         $position =~ s/\D+//g;
 6553:                         if ($position ne '') {
 6554:                             $allpos[$position] = $newbook;
 6555:                         }
 6556:                     }
 6557:                 } else {
 6558:                     $errors .= '<li><span class="LC_error">'.&mt('Invalid LON-CAPA course for textbook').
 6559:                                '</span></li>';
 6560:                 }
 6561:             }
 6562: 
 6563:         }
 6564:         if (ref($domconfig{$action}) eq 'HASH') {
 6565:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 6566:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 6567:                     $changes{'notify'}{'approval'} = 1;
 6568:                 }
 6569:             } else {
 6570:                 if ($confhash{'notify'}{'approval'}) {
 6571:                     $changes{'notify'}{'approval'} = 1;
 6572:                 }
 6573:             }
 6574:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 6575:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 6576:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 6577:                         unless ($confhash{'uniquecode'}{$crstype}) {
 6578:                             $changes{'uniquecode'} = 1;
 6579:                         }
 6580:                     }
 6581:                     unless ($changes{'uniquecode'}) {
 6582:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 6583:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 6584:                                 $changes{'uniquecode'} = 1;
 6585:                             }
 6586:                         }
 6587:                     }
 6588:                } else {
 6589:                    $changes{'uniquecode'} = 1;
 6590:                }
 6591:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 6592:                 $changes{'uniquecode'} = 1;
 6593:             }
 6594:             if ($context eq 'requestcourses') {
 6595:                 if (ref($domconfig{$action}{'textbooks'}) eq 'HASH') {
 6596:                     my %deletions;
 6597:                     my @todelete = &Apache::loncommon::get_env_multiple('form.book_del');
 6598:                     if (@todelete) {
 6599:                         map { $deletions{$_} = 1; } @todelete;
 6600:                     }
 6601:                     my %imgdeletions;
 6602:                     my @todeleteimages = &Apache::loncommon::get_env_multiple('form.book_image_del');
 6603:                     if (@todeleteimages) {
 6604:                         map { $imgdeletions{$_} = 1; } @todeleteimages;
 6605:                     }
 6606:                     my $maxnum = $env{'form.book_maxnum'};
 6607:                     for (my $i=0; $i<=$maxnum; $i++) {
 6608:                         my $key = $env{'form.book_id_'.$i};
 6609:                         if (ref($domconfig{$action}{'textbooks'}{$key}) eq 'HASH') {
 6610:                             if ($deletions{$key}) {
 6611:                                 if ($domconfig{$action}{'textbooks'}{$key}{'image'}) {
 6612:                                     #FIXME need to obsolete item in RES space
 6613:                                 }
 6614:                                 next;
 6615:                             } else {
 6616:                                 my $newpos = $env{'form.'.$key};
 6617:                                 $newpos =~ s/\D+//g;
 6618:                                 foreach my $item ('subject','title','author') {
 6619:                                     $confhash{'textbooks'}{$key}{$item} = $env{'form.book_'.$item.'_'.$i};
 6620:                                     if ($domconfig{$action}{'textbooks'}{$key}{$item} ne $confhash{'textbooks'}{$key}{$item}) {
 6621:                                         $changes{'textbooks'}{$key} = 1;
 6622:                                     }
 6623:                                 }
 6624:                                 $allpos[$newpos] = $key;
 6625:                             }
 6626:                             if ($imgdeletions{$key}) {
 6627:                                 $changes{'textbooks'}{$key} = 1;
 6628:                                 #FIXME need to obsolete item in RES space
 6629:                             } elsif ($env{'form.book_image_'.$i.'.filename'}) {
 6630:                                 my ($cdom,$cnum) = split(/_/,$key);
 6631:                                 my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,'book_image_'.$i,
 6632:                                                                               $cdom,$cnum,$configuserok,
 6633:                                                                               $switchserver,$author_ok);
 6634:                                 if ($imgurl) {
 6635:                                     $confhash{'textbooks'}{$key}{'image'} = $imgurl;
 6636:                                     $changes{'textbooks'}{$key} = 1; 
 6637:                                 }
 6638:                                 if ($error) {
 6639:                                     &Apache::lonnet::logthis($error);
 6640:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6641:                                 } 
 6642:                             } elsif ($domconfig{$action}{'textbooks'}{$key}{'image'}) {
 6643:                                 $confhash{'textbooks'}{$key}{'image'} = 
 6644:                                     $domconfig{$action}{'textbooks'}{$key}{'image'};
 6645:                             }
 6646:                         }
 6647:                     }
 6648:                 }
 6649:             }
 6650:         } else {
 6651:             if ($confhash{'notify'}{'approval'}) {
 6652:                 $changes{'notify'}{'approval'} = 1;
 6653:             }
 6654:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 6655:                 $changes{'uniquecode'} = 1;
 6656:             }
 6657:         }
 6658:         if ($context eq 'requestcourses') {
 6659:             if ($newbook) {
 6660:                 $changes{'textbooks'}{$newbook} = 1;
 6661:                 foreach my $item ('subject','title','author') {
 6662:                     $env{'form.addbook_'.$item} =~ s/(`)/'/g;
 6663:                     if ($env{'form.addbook_'.$item}) {
 6664:                         $confhash{'textbooks'}{$newbook}{$item} = $env{'form.addbook_'.$item};
 6665:                     }
 6666:                 }
 6667:                 if ($env{'form.addbook_image.filename'} ne '') {
 6668:                     my ($cdom,$cnum) = split(/_/,$newbook);
 6669:                     my ($imageurl,$error) =
 6670:                         &process_textbook_image($r,$dom,$confname,'addbook_image',$cdom,$cnum,$configuserok,
 6671:                                                 $switchserver,$author_ok);
 6672:                     if ($imageurl) {
 6673:                         $confhash{'textbooks'}{$newbook}{'image'} = $imageurl;
 6674:                     }
 6675:                     if ($error) {
 6676:                         &Apache::lonnet::logthis($error);
 6677:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6678:                     }
 6679:                 }
 6680:             }
 6681:             if (@allpos > 0) {
 6682:                 my $idx = 0;
 6683:                 foreach my $item (@allpos) {
 6684:                     if ($item ne '') {
 6685:                         $confhash{'textbooks'}{$item}{'order'} = $idx;
 6686:                         if (ref($domconfig{$action}) eq 'HASH') {
 6687:                             if (ref($domconfig{$action}{'textbooks'}) eq 'HASH') {
 6688:                                 if (ref($domconfig{$action}{'textbooks'}{$item}) eq 'HASH') {
 6689:                                     if ($domconfig{$action}{'textbooks'}{$item}{'order'} ne $idx) {
 6690:                                         $changes{'textbooks'}{$item} = 1;
 6691:                                     }
 6692:                                 }
 6693:                             }
 6694:                         }
 6695:                         $idx ++;
 6696:                     }
 6697:                 }
 6698:             }
 6699:             if (ref($validationitemsref) eq 'ARRAY') {
 6700:                 foreach my $item (@{$validationitemsref}) {
 6701:                     if ($item eq 'fields') {
 6702:                         my @changed;
 6703:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 6704:                         if (@{$confhash{'validation'}{$item}} > 0) {
 6705:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 6706:                         }
 6707:                         if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 6708:                             if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 6709:                                 @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 6710:                                                                               $domconfig{'requestcourses'}{'validation'}{$item});
 6711:                             } else {
 6712:                                 @changed = @{$confhash{'validation'}{$item}};
 6713:                             }
 6714:                         } else {
 6715:                             @changed = @{$confhash{'validation'}{$item}};
 6716:                         }
 6717:                         if (@changed) {
 6718:                             if ($confhash{'validation'}{$item}) {
 6719:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 6720:                             } else {
 6721:                                 $changes{'validation'}{$item} = &mt('None');
 6722:                             }
 6723:                         }
 6724:                     } else {
 6725:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 6726:                         if ($item eq 'markup') {
 6727:                             if ($env{'form.requestcourses_validation_'.$item}) {
 6728:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 6729:                             }
 6730:                         }
 6731:                         if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 6732:                             if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 6733:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 6734:                             }
 6735:                         } else {
 6736:                             if ($confhash{'validation'}{$item} ne '') {
 6737:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 6738:                             }
 6739:                         }
 6740:                     }
 6741:                 }
 6742:             }
 6743:             if ($env{'form.validationdc'}) {
 6744:                 my $newval = $env{'form.validationdc'};
 6745:                 my %domcoords = &get_active_dcs($dom);
 6746:                 if (exists($domcoords{$newval})) {
 6747:                     $confhash{'validation'}{'dc'} = $newval;
 6748:                 }
 6749:             }
 6750:             if (ref($confhash{'validation'}) eq 'HASH') {
 6751:                 if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 6752:                     if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 6753:                         unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 6754:                             if ($confhash{'validation'}{'dc'} eq '') {
 6755:                                 $changes{'validation'}{'dc'} = &mt('None');
 6756:                             } else {
 6757:                                 $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 6758:                             }
 6759:                         }
 6760:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 6761:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 6762:                     }
 6763:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 6764:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 6765:                 }
 6766:             } elsif (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 6767:                 if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 6768:                     $changes{'validation'}{'dc'} = &mt('None');
 6769:                 }
 6770:             }
 6771:         }
 6772:     } else {
 6773:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 6774:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 6775:     }
 6776:     foreach my $item (@usertools) {
 6777:         foreach my $type (@{$types},'default','_LC_adv') {
 6778:             my $unset; 
 6779:             if ($context eq 'requestcourses') {
 6780:                 $unset = '0';
 6781:                 if ($type eq '_LC_adv') {
 6782:                     $unset = '';
 6783:                 }
 6784:                 if ($confhash{$item}{$type} eq 'autolimit') {
 6785:                     $confhash{$item}{$type} .= '=';
 6786:                     unless ($limithash{$item}{$type} =~ /\D/) {
 6787:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 6788:                     }
 6789:                 }
 6790:             } elsif ($context eq 'requestauthor') {
 6791:                 $unset = '0';
 6792:                 if ($type eq '_LC_adv') {
 6793:                     $unset = '';
 6794:                 }
 6795:             } else {
 6796:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 6797:                     $confhash{$item}{$type} = 1;
 6798:                 } else {
 6799:                     $confhash{$item}{$type} = 0;
 6800:                 }
 6801:             }
 6802:             if (ref($domconfig{$action}) eq 'HASH') {
 6803:                 if ($action eq 'requestauthor') {
 6804:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 6805:                         $changes{$type} = 1;
 6806:                     }
 6807:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 6808:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 6809:                         $changes{$item}{$type} = 1;
 6810:                     }
 6811:                 } else {
 6812:                     if ($context eq 'requestcourses') {
 6813:                         if ($confhash{$item}{$type} ne $unset) {
 6814:                             $changes{$item}{$type} = 1;
 6815:                         }
 6816:                     } else {
 6817:                         if (!$confhash{$item}{$type}) {
 6818:                             $changes{$item}{$type} = 1;
 6819:                         }
 6820:                     }
 6821:                 }
 6822:             } else {
 6823:                 if ($context eq 'requestcourses') {
 6824:                     if ($confhash{$item}{$type} ne $unset) {
 6825:                         $changes{$item}{$type} = 1;
 6826:                     }
 6827:                 } elsif ($context eq 'requestauthor') {
 6828:                     if ($confhash{$type} ne $unset) {
 6829:                         $changes{$type} = 1;
 6830:                     }
 6831:                 } else {
 6832:                     if (!$confhash{$item}{$type}) {
 6833:                         $changes{$item}{$type} = 1;
 6834:                     }
 6835:                 }
 6836:             }
 6837:         }
 6838:     }
 6839:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 6840:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 6841:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 6842:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 6843:                     if (exists($confhash{'defaultquota'}{$key})) {
 6844:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 6845:                             $changes{'defaultquota'}{$key} = 1;
 6846:                         }
 6847:                     } else {
 6848:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 6849:                     }
 6850:                 }
 6851:             } else {
 6852:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 6853:                     if (exists($confhash{'defaultquota'}{$key})) {
 6854:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 6855:                             $changes{'defaultquota'}{$key} = 1;
 6856:                         }
 6857:                     } else {
 6858:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 6859:                     }
 6860:                 }
 6861:             }
 6862:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 6863:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 6864:                     if (exists($confhash{'authorquota'}{$key})) {
 6865:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 6866:                             $changes{'authorquota'}{$key} = 1;
 6867:                         }
 6868:                     } else {
 6869:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 6870:                     }
 6871:                 }
 6872:             }
 6873:         }
 6874:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 6875:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 6876:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 6877:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 6878:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 6879:                             $changes{'defaultquota'}{$key} = 1;
 6880:                         }
 6881:                     } else {
 6882:                         if (!exists($domconfig{'quotas'}{$key})) {
 6883:                             $changes{'defaultquota'}{$key} = 1;
 6884:                         }
 6885:                     }
 6886:                 } else {
 6887:                     $changes{'defaultquota'}{$key} = 1;
 6888:                 }
 6889:             }
 6890:         }
 6891:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 6892:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 6893:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 6894:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 6895:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 6896:                             $changes{'authorquota'}{$key} = 1;
 6897:                         }
 6898:                     } else {
 6899:                         $changes{'authorquota'}{$key} = 1;
 6900:                     }
 6901:                 } else {
 6902:                     $changes{'authorquota'}{$key} = 1;
 6903:                 }
 6904:             }
 6905:         }
 6906:     }
 6907: 
 6908:     if ($context eq 'requestauthor') {
 6909:         $domdefaults{'requestauthor'} = \%confhash;
 6910:     } else {
 6911:         foreach my $key (keys(%confhash)) {
 6912:             unless (($context eq 'requestcourses') && ($key eq 'textbooks')) {
 6913:                 $domdefaults{$key} = $confhash{$key};
 6914:             }
 6915:         }
 6916:     }
 6917: 
 6918:     my %quotahash = (
 6919:                       $action => { %confhash }
 6920:                     );
 6921:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 6922:                                              $dom);
 6923:     if ($putresult eq 'ok') {
 6924:         if (keys(%changes) > 0) {
 6925:             my $cachetime = 24*60*60;
 6926:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 6927:             if (ref($lastactref) eq 'HASH') {
 6928:                 $lastactref->{'domdefaults'} = 1;
 6929:             }
 6930:             $resulttext = &mt('Changes made:').'<ul>';
 6931:             unless (($context eq 'requestcourses') ||
 6932:                     ($context eq 'requestauthor')) {
 6933:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 6934:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 6935:                     foreach my $type (@{$types},'default') {
 6936:                         if (defined($changes{'defaultquota'}{$type})) {
 6937:                             my $typetitle = $usertypes->{$type};
 6938:                             if ($type eq 'default') {
 6939:                                 $typetitle = $othertitle;
 6940:                             }
 6941:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 6942:                         }
 6943:                     }
 6944:                     $resulttext .= '</ul></li>';
 6945:                 }
 6946:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 6947:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 6948:                     foreach my $type (@{$types},'default') {
 6949:                         if (defined($changes{'authorquota'}{$type})) {
 6950:                             my $typetitle = $usertypes->{$type};
 6951:                             if ($type eq 'default') {
 6952:                                 $typetitle = $othertitle;
 6953:                             }
 6954:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 6955:                         }
 6956:                     }
 6957:                     $resulttext .= '</ul></li>';
 6958:                 }
 6959:             }
 6960:             my %newenv;
 6961:             foreach my $item (@usertools) {
 6962:                 my (%haschgs,%inconf);
 6963:                 if ($context eq 'requestauthor') {
 6964:                     %haschgs = %changes;
 6965:                     %inconf = %confhash;
 6966:                 } else {
 6967:                     if (ref($changes{$item}) eq 'HASH') {
 6968:                         %haschgs = %{$changes{$item}};
 6969:                     }
 6970:                     if (ref($confhash{$item}) eq 'HASH') {
 6971:                         %inconf = %{$confhash{$item}};
 6972:                     }
 6973:                 }
 6974:                 if (keys(%haschgs) > 0) {
 6975:                     my $newacc = 
 6976:                         &Apache::lonnet::usertools_access($env{'user.name'},
 6977:                                                           $env{'user.domain'},
 6978:                                                           $item,'reload',$context);
 6979:                     if (($context eq 'requestcourses') ||
 6980:                         ($context eq 'requestauthor')) {
 6981:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 6982:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 6983:                         }
 6984:                     } else {
 6985:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 6986:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 6987:                         }
 6988:                     }
 6989:                     unless ($context eq 'requestauthor') {
 6990:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 6991:                     }
 6992:                     foreach my $type (@{$types},'default','_LC_adv') {
 6993:                         if ($haschgs{$type}) {
 6994:                             my $typetitle = $usertypes->{$type};
 6995:                             if ($type eq 'default') {
 6996:                                 $typetitle = $othertitle;
 6997:                             } elsif ($type eq '_LC_adv') {
 6998:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 6999:                             }
 7000:                             if ($inconf{$type}) {
 7001:                                 if ($context eq 'requestcourses') {
 7002:                                     my $cond;
 7003:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 7004:                                         if ($1 eq '') {
 7005:                                             $cond = &mt('(Automatic processing of any request).');
 7006:                                         } else {
 7007:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 7008:                                         }
 7009:                                     } else { 
 7010:                                         $cond = $conditions{$inconf{$type}};
 7011:                                     }
 7012:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 7013:                                 } elsif ($context eq 'requestauthor') {
 7014:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 7015:                                                              $titles{$inconf{$type}},$typetitle);
 7016: 
 7017:                                 } else {
 7018:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 7019:                                 }
 7020:                             } else {
 7021:                                 if ($type eq '_LC_adv') {
 7022:                                     if ($inconf{$type} eq '0') {
 7023:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 7024:                                     } else { 
 7025:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 7026:                                     }
 7027:                                 } else {
 7028:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 7029:                                 }
 7030:                             }
 7031:                         }
 7032:                     }
 7033:                     unless ($context eq 'requestauthor') {
 7034:                         $resulttext .= '</ul></li>';
 7035:                     }
 7036:                 }
 7037:             }
 7038:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 7039:                 if (ref($changes{'notify'}) eq 'HASH') {
 7040:                     if ($changes{'notify'}{'approval'}) {
 7041:                         if (ref($confhash{'notify'}) eq 'HASH') {
 7042:                             if ($confhash{'notify'}{'approval'}) {
 7043:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 7044:                             } else {
 7045:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 7046:                             }
 7047:                         }
 7048:                     }
 7049:                 }
 7050:             }
 7051:             if ($action eq 'requestcourses') {
 7052:                 my @offon = ('off','on');
 7053:                 if ($changes{'uniquecode'}) {
 7054:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 7055:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 7056:                         $resulttext .= '<li>'.
 7057:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 7058:                                        '</li>';
 7059:                     } else {
 7060:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 7061:                                        '</li>';
 7062:                     }
 7063:                 }
 7064:                 if (ref($changes{'textbooks'}) eq 'HASH') {
 7065:                     $resulttext .= '<li>'.&mt('Available textbooks updated').'<ul>';
 7066:                     foreach my $key (sort(keys(%{$changes{'textbooks'}}))) {
 7067:                         my %coursehash = &Apache::lonnet::coursedescription($key);
 7068:                         my $coursetitle = $coursehash{'description'};
 7069:                         my $position = $confhash{'textbooks'}{$key}{'order'} + 1;
 7070:                         $resulttext .= '<li>';
 7071:                         foreach my $item ('subject','title','author') {
 7072:                             my $name = $item.':';
 7073:                             $name =~ s/^(\w)/\U$1/;
 7074:                             $resulttext .= &mt($name).' '.$confhash{'textbooks'}{$key}{$item}.'<br />';
 7075:                         }
 7076:                         $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 7077:                         if ($confhash{'textbooks'}{$key}{'image'}) {
 7078:                             $resulttext .= ' '.&mt('Image: [_1]',
 7079:                                                '<img src="'.$confhash{'textbooks'}{$key}{'image'}.'"'.
 7080:                                                ' alt="Textbook cover" />').'<br />';
 7081:                         }
 7082:                         $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 7083:                     }
 7084:                     $resulttext .= '</ul></li>';
 7085:                 }
 7086:                 if (ref($changes{'validation'}) eq 'HASH') {
 7087:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 7088:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 7089:                         foreach my $item (@{$validationitemsref}) {
 7090:                             if (exists($changes{'validation'}{$item})) {
 7091:                                 if ($item eq 'markup') {
 7092:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 7093:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 7094:                                 } else {
 7095:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 7096:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 7097:                                 }
 7098:                             }
 7099:                         }
 7100:                         if (exists($changes{'validation'}{'dc'})) {
 7101:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 7102:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 7103:                         }
 7104:                     }
 7105:                 }
 7106:             }
 7107:             $resulttext .= '</ul>';
 7108:             if (keys(%newenv)) {
 7109:                 &Apache::lonnet::appenv(\%newenv);
 7110:             }
 7111:         } else {
 7112:             if ($context eq 'requestcourses') {
 7113:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 7114:             } elsif ($context eq 'requestauthor') {
 7115:                 $resulttext = &mt('No changes made to rights to request author space.');
 7116:             } else {
 7117:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 7118:             }
 7119:         }
 7120:     } else {
 7121:         $resulttext = '<span class="LC_error">'.
 7122: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7123:     }
 7124:     if ($errors) {
 7125:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 7126:                        '<ul>'.$errors.'</ul></p>';
 7127:     }
 7128:     return $resulttext;
 7129: }
 7130: 
 7131: sub process_textbook_image {
 7132:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$configuserok,$switchserver,$author_ok) = @_;
 7133:     my $filename = $env{'form.'.$caller.'.filename'};
 7134:     my ($error,$url);
 7135:     my ($width,$height) = (50,50);
 7136:     if ($configuserok eq 'ok') {
 7137:         if ($switchserver) {
 7138:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 7139:                          $switchserver);
 7140:         } elsif ($author_ok eq 'ok') {
 7141:             my ($result,$imageurl) =
 7142:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 7143:                              "textbooks/$dom/$cnum/cover",$width,$height);
 7144:             if ($result eq 'ok') {
 7145:                 $url = $imageurl;
 7146:             } else {
 7147:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 7148:             }
 7149:         } else {
 7150:             $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);
 7151:         }
 7152:     } else {
 7153:         $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);
 7154:     }
 7155:     return ($url,$error);
 7156: }
 7157: 
 7158: sub modify_autoenroll {
 7159:     my ($dom,$lastactref,%domconfig) = @_;
 7160:     my ($resulttext,%changes);
 7161:     my %currautoenroll;
 7162:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 7163:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 7164:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 7165:         }
 7166:     }
 7167:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 7168:     my %title = ( run => 'Auto-enrollment active',
 7169:                   sender => 'Sender for notification messages',
 7170:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 7171:     my @offon = ('off','on');
 7172:     my $sender_uname = $env{'form.sender_uname'};
 7173:     my $sender_domain = $env{'form.sender_domain'};
 7174:     if ($sender_domain eq '') {
 7175:         $sender_uname = '';
 7176:     } elsif ($sender_uname eq '') {
 7177:         $sender_domain = '';
 7178:     }
 7179:     my $coowners = $env{'form.autoassign_coowners'};
 7180:     my %autoenrollhash =  (
 7181:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 7182:                                        'sender_uname' => $sender_uname,
 7183:                                        'sender_domain' => $sender_domain,
 7184:                                        'co-owners' => $coowners,
 7185:                                 }
 7186:                      );
 7187:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 7188:                                              $dom);
 7189:     if ($putresult eq 'ok') {
 7190:         if (exists($currautoenroll{'run'})) {
 7191:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 7192:                  $changes{'run'} = 1;
 7193:              }
 7194:         } elsif ($autorun) {
 7195:             if ($env{'form.autoenroll_run'} ne '1') {
 7196:                  $changes{'run'} = 1;
 7197:             }
 7198:         }
 7199:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 7200:             $changes{'sender'} = 1;
 7201:         }
 7202:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 7203:             $changes{'sender'} = 1;
 7204:         }
 7205:         if ($currautoenroll{'co-owners'} ne '') {
 7206:             if ($currautoenroll{'co-owners'} ne $coowners) {
 7207:                 $changes{'coowners'} = 1;
 7208:             }
 7209:         } elsif ($coowners) {
 7210:             $changes{'coowners'} = 1;
 7211:         }      
 7212:         if (keys(%changes) > 0) {
 7213:             $resulttext = &mt('Changes made:').'<ul>';
 7214:             if ($changes{'run'}) {
 7215:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 7216:             }
 7217:             if ($changes{'sender'}) {
 7218:                 if ($sender_uname eq '' || $sender_domain eq '') {
 7219:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 7220:                 } else {
 7221:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 7222:                 }
 7223:             }
 7224:             if ($changes{'coowners'}) {
 7225:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 7226:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 7227:                 if (ref($lastactref) eq 'HASH') {
 7228:                     $lastactref->{'domainconfig'} = 1;
 7229:                 }
 7230:             }
 7231:             $resulttext .= '</ul>';
 7232:         } else {
 7233:             $resulttext = &mt('No changes made to auto-enrollment settings');
 7234:         }
 7235:     } else {
 7236:         $resulttext = '<span class="LC_error">'.
 7237: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7238:     }
 7239:     return $resulttext;
 7240: }
 7241: 
 7242: sub modify_autoupdate {
 7243:     my ($dom,%domconfig) = @_;
 7244:     my ($resulttext,%currautoupdate,%fields,%changes);
 7245:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 7246:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 7247:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 7248:         }
 7249:     }
 7250:     my @offon = ('off','on');
 7251:     my %title = &Apache::lonlocal::texthash (
 7252:                    run => 'Auto-update:',
 7253:                    classlists => 'Updates to user information in classlists?'
 7254:                 );
 7255:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7256:     my %fieldtitles = &Apache::lonlocal::texthash (
 7257:                         id => 'Student/Employee ID',
 7258:                         permanentemail => 'E-mail address',
 7259:                         lastname => 'Last Name',
 7260:                         firstname => 'First Name',
 7261:                         middlename => 'Middle Name',
 7262:                         generation => 'Generation',
 7263:                       );
 7264:     $othertitle = &mt('All users');
 7265:     if (keys(%{$usertypes}) >  0) {
 7266:         $othertitle = &mt('Other users');
 7267:     }
 7268:     foreach my $key (keys(%env)) {
 7269:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 7270:             my ($usertype,$item) = ($1,$2);
 7271:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 7272:                 if ($usertype eq 'default') {   
 7273:                     push(@{$fields{$1}},$2);
 7274:                 } elsif (ref($types) eq 'ARRAY') {
 7275:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 7276:                         push(@{$fields{$1}},$2);
 7277:                     }
 7278:                 }
 7279:             }
 7280:         }
 7281:     }
 7282:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 7283:     @lockablenames = sort(@lockablenames);
 7284:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 7285:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 7286:         if (@changed) {
 7287:             $changes{'lockablenames'} = 1;
 7288:         }
 7289:     } else {
 7290:         if (@lockablenames) {
 7291:             $changes{'lockablenames'} = 1;
 7292:         }
 7293:     }
 7294:     my %updatehash = (
 7295:                       autoupdate => { run => $env{'form.autoupdate_run'},
 7296:                                       classlists => $env{'form.classlists'},
 7297:                                       fields => {%fields},
 7298:                                       lockablenames => \@lockablenames,
 7299:                                     }
 7300:                      );
 7301:     foreach my $key (keys(%currautoupdate)) {
 7302:         if (($key eq 'run') || ($key eq 'classlists')) {
 7303:             if (exists($updatehash{autoupdate}{$key})) {
 7304:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 7305:                     $changes{$key} = 1;
 7306:                 }
 7307:             }
 7308:         } elsif ($key eq 'fields') {
 7309:             if (ref($currautoupdate{$key}) eq 'HASH') {
 7310:                 foreach my $item (@{$types},'default') {
 7311:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 7312:                         my $change = 0;
 7313:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 7314:                             if (!exists($fields{$item})) {
 7315:                                 $change = 1;
 7316:                                 last;
 7317:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 7318:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 7319:                                     $change = 1;
 7320:                                     last;
 7321:                                 }
 7322:                             }
 7323:                         }
 7324:                         if ($change) {
 7325:                             push(@{$changes{$key}},$item);
 7326:                         }
 7327:                     } 
 7328:                 }
 7329:             }
 7330:         } elsif ($key eq 'lockablenames') {
 7331:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 7332:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 7333:                 if (@changed) {
 7334:                     $changes{'lockablenames'} = 1;
 7335:                 }
 7336:             } else {
 7337:                 if (@lockablenames) {
 7338:                     $changes{'lockablenames'} = 1;
 7339:                 }
 7340:             }
 7341:         }
 7342:     }
 7343:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 7344:         if (@lockablenames) {
 7345:             $changes{'lockablenames'} = 1;
 7346:         }
 7347:     }
 7348:     foreach my $item (@{$types},'default') {
 7349:         if (defined($fields{$item})) {
 7350:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 7351:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 7352:                     my $change = 0;
 7353:                     if (ref($fields{$item}) eq 'ARRAY') {
 7354:                         foreach my $type (@{$fields{$item}}) {
 7355:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 7356:                                 $change = 1;
 7357:                                 last;
 7358:                             }
 7359:                         }
 7360:                     }
 7361:                     if ($change) {
 7362:                         push(@{$changes{'fields'}},$item);
 7363:                     }
 7364:                 } else {
 7365:                     push(@{$changes{'fields'}},$item);
 7366:                 }
 7367:             } else {
 7368:                 push(@{$changes{'fields'}},$item);
 7369:             }
 7370:         }
 7371:     }
 7372:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 7373:                                              $dom);
 7374:     if ($putresult eq 'ok') {
 7375:         if (keys(%changes) > 0) {
 7376:             $resulttext = &mt('Changes made:').'<ul>';
 7377:             foreach my $key (sort(keys(%changes))) {
 7378:                 if ($key eq 'lockablenames') {
 7379:                     $resulttext .= '<li>';
 7380:                     if (@lockablenames) {
 7381:                         $usertypes->{'default'} = $othertitle;
 7382:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 7383:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 7384:                     } else {
 7385:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 7386:                     }
 7387:                     $resulttext .= '</li>';
 7388:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 7389:                     foreach my $item (@{$changes{$key}}) {
 7390:                         my @newvalues;
 7391:                         foreach my $type (@{$fields{$item}}) {
 7392:                             push(@newvalues,$fieldtitles{$type});
 7393:                         }
 7394:                         my $newvaluestr;
 7395:                         if (@newvalues > 0) {
 7396:                             $newvaluestr = join(', ',@newvalues);
 7397:                         } else {
 7398:                             $newvaluestr = &mt('none');
 7399:                         }
 7400:                         if ($item eq 'default') {
 7401:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 7402:                         } else {
 7403:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 7404:                         }
 7405:                     }
 7406:                 } else {
 7407:                     my $newvalue;
 7408:                     if ($key eq 'run') {
 7409:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 7410:                     } else {
 7411:                         $newvalue = $offon[$env{'form.'.$key}];
 7412:                     }
 7413:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 7414:                 }
 7415:             }
 7416:             $resulttext .= '</ul>';
 7417:         } else {
 7418:             $resulttext = &mt('No changes made to autoupdates');
 7419:         }
 7420:     } else {
 7421:         $resulttext = '<span class="LC_error">'.
 7422: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7423:     }
 7424:     return $resulttext;
 7425: }
 7426: 
 7427: sub modify_autocreate {
 7428:     my ($dom,%domconfig) = @_;
 7429:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 7430:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 7431:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 7432:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 7433:         }
 7434:     }
 7435:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 7436:                  req => 'Auto-creation of validated requests for official courses',
 7437:                  xmldc => 'Identity of course creator of courses from XML files',
 7438:                );
 7439:     my @types = ('xml','req');
 7440:     foreach my $item (@types) {
 7441:         $newvals{$item} = $env{'form.autocreate_'.$item};
 7442:         $newvals{$item} =~ s/\D//g;
 7443:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 7444:     }
 7445:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 7446:     my %domcoords = &get_active_dcs($dom);
 7447:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 7448:         $newvals{'xmldc'} = '';
 7449:     } 
 7450:     %autocreatehash =  (
 7451:                         autocreate => { xml => $newvals{'xml'},
 7452:                                         req => $newvals{'req'},
 7453:                                       }
 7454:                        );
 7455:     if ($newvals{'xmldc'} ne '') {
 7456:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 7457:     }
 7458:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 7459:                                              $dom);
 7460:     if ($putresult eq 'ok') {
 7461:         my @items = @types;
 7462:         if ($newvals{'xml'}) {
 7463:             push(@items,'xmldc');
 7464:         }
 7465:         foreach my $item (@items) {
 7466:             if (exists($currautocreate{$item})) {
 7467:                 if ($currautocreate{$item} ne $newvals{$item}) {
 7468:                     $changes{$item} = 1;
 7469:                 }
 7470:             } elsif ($newvals{$item}) {
 7471:                 $changes{$item} = 1;
 7472:             }
 7473:         }
 7474:         if (keys(%changes) > 0) {
 7475:             my @offon = ('off','on'); 
 7476:             $resulttext = &mt('Changes made:').'<ul>';
 7477:             foreach my $item (@types) {
 7478:                 if ($changes{$item}) {
 7479:                     my $newtxt = $offon[$newvals{$item}];
 7480:                     $resulttext .= '<li>'.
 7481:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
 7482:                                        '<b>','</b>').
 7483:                                    '</li>';
 7484:                 }
 7485:             }
 7486:             if ($changes{'xmldc'}) {
 7487:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 7488:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 7489:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
 7490:             }
 7491:             $resulttext .= '</ul>';
 7492:         } else {
 7493:             $resulttext = &mt('No changes made to auto-creation settings');
 7494:         }
 7495:     } else {
 7496:         $resulttext = '<span class="LC_error">'.
 7497:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7498:     }
 7499:     return $resulttext;
 7500: }
 7501: 
 7502: sub modify_directorysrch {
 7503:     my ($dom,%domconfig) = @_;
 7504:     my ($resulttext,%changes);
 7505:     my %currdirsrch;
 7506:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 7507:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 7508:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 7509:         }
 7510:     }
 7511:     my %title = ( available => 'Directory search available',
 7512:                   localonly => 'Other domains can search',
 7513:                   searchby => 'Search types',
 7514:                   searchtypes => 'Search latitude');
 7515:     my @offon = ('off','on');
 7516:     my @otherdoms = ('Yes','No');
 7517: 
 7518:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 7519:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 7520:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 7521: 
 7522:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7523:     if (keys(%{$usertypes}) == 0) {
 7524:         @cansearch = ('default');
 7525:     } else {
 7526:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 7527:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 7528:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 7529:                     push(@{$changes{'cansearch'}},$type);
 7530:                 }
 7531:             }
 7532:             foreach my $type (@cansearch) {
 7533:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 7534:                     push(@{$changes{'cansearch'}},$type);
 7535:                 }
 7536:             }
 7537:         } else {
 7538:             push(@{$changes{'cansearch'}},@cansearch);
 7539:         }
 7540:     }
 7541: 
 7542:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 7543:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 7544:             if (!grep(/^\Q$by\E$/,@searchby)) {
 7545:                 push(@{$changes{'searchby'}},$by);
 7546:             }
 7547:         }
 7548:         foreach my $by (@searchby) {
 7549:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 7550:                 push(@{$changes{'searchby'}},$by);
 7551:             }
 7552:         }
 7553:     } else {
 7554:         push(@{$changes{'searchby'}},@searchby);
 7555:     }
 7556: 
 7557:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 7558:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 7559:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 7560:                 push(@{$changes{'searchtypes'}},$type);
 7561:             }
 7562:         }
 7563:         foreach my $type (@searchtypes) {
 7564:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 7565:                 push(@{$changes{'searchtypes'}},$type);
 7566:             }
 7567:         }
 7568:     } else {
 7569:         if (exists($currdirsrch{'searchtypes'})) {
 7570:             foreach my $type (@searchtypes) {  
 7571:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 7572:                     push(@{$changes{'searchtypes'}},$type);
 7573:                 }
 7574:             }
 7575:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 7576:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 7577:             }   
 7578:         } else {
 7579:             push(@{$changes{'searchtypes'}},@searchtypes); 
 7580:         }
 7581:     }
 7582: 
 7583:     my %dirsrch_hash =  (
 7584:             directorysrch => { available => $env{'form.dirsrch_available'},
 7585:                                cansearch => \@cansearch,
 7586:                                localonly => $env{'form.dirsrch_localonly'},
 7587:                                searchby => \@searchby,
 7588:                                searchtypes => \@searchtypes,
 7589:                              }
 7590:             );
 7591:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 7592:                                              $dom);
 7593:     if ($putresult eq 'ok') {
 7594:         if (exists($currdirsrch{'available'})) {
 7595:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 7596:                  $changes{'available'} = 1;
 7597:              }
 7598:         } else {
 7599:             if ($env{'form.dirsrch_available'} eq '1') {
 7600:                 $changes{'available'} = 1;
 7601:             }
 7602:         }
 7603:         if (exists($currdirsrch{'localonly'})) {
 7604:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 7605:                  $changes{'localonly'} = 1;
 7606:              }
 7607:         } else {
 7608:             if ($env{'form.dirsrch_localonly'} eq '1') {
 7609:                 $changes{'localonly'} = 1;
 7610:             }
 7611:         }
 7612:         if (keys(%changes) > 0) {
 7613:             $resulttext = &mt('Changes made:').'<ul>';
 7614:             if ($changes{'available'}) {
 7615:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 7616:             }
 7617:             if ($changes{'localonly'}) {
 7618:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 7619:             }
 7620: 
 7621:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 7622:                 my $chgtext;
 7623:                 if (ref($usertypes) eq 'HASH') {
 7624:                     if (keys(%{$usertypes}) > 0) {
 7625:                         foreach my $type (@{$types}) {
 7626:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 7627:                                 $chgtext .= $usertypes->{$type}.'; ';
 7628:                             }
 7629:                         }
 7630:                         if (grep(/^default$/,@cansearch)) {
 7631:                             $chgtext .= $othertitle;
 7632:                         } else {
 7633:                             $chgtext =~ s/\; $//;
 7634:                         }
 7635:                         $resulttext .=
 7636:                             '<li>'.
 7637:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
 7638:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
 7639:                             '</li>';
 7640:                     }
 7641:                 }
 7642:             }
 7643:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 7644:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 7645:                 my $chgtext;
 7646:                 foreach my $type (@{$titleorder}) {
 7647:                     if (grep(/^\Q$type\E$/,@searchby)) {
 7648:                         if (defined($searchtitles->{$type})) {
 7649:                             $chgtext .= $searchtitles->{$type}.'; ';
 7650:                         }
 7651:                     }
 7652:                 }
 7653:                 $chgtext =~ s/\; $//;
 7654:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 7655:             }
 7656:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 7657:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 7658:                 my $chgtext;
 7659:                 foreach my $type (@{$srchtypeorder}) {
 7660:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 7661:                         if (defined($srchtypes_desc->{$type})) {
 7662:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 7663:                         }
 7664:                     }
 7665:                 }
 7666:                 $chgtext =~ s/\; $//;
 7667:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
 7668:             }
 7669:             $resulttext .= '</ul>';
 7670:         } else {
 7671:             $resulttext = &mt('No changes made to institution directory search settings');
 7672:         }
 7673:     } else {
 7674:         $resulttext = '<span class="LC_error">'.
 7675:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 7676:     }
 7677:     return $resulttext;
 7678: }
 7679: 
 7680: sub modify_contacts {
 7681:     my ($dom,$lastactref,%domconfig) = @_;
 7682:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 7683:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 7684:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 7685:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 7686:         }
 7687:     }
 7688:     my (%others,%to,%bcc);
 7689:     my @contacts = ('supportemail','adminemail');
 7690:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 7691:                     'requestsmail','updatesmail','idconflictsmail');
 7692:     my @toggles = ('reporterrors','reportupdates');
 7693:     foreach my $type (@mailings) {
 7694:         @{$newsetting{$type}} = 
 7695:             &Apache::loncommon::get_env_multiple('form.'.$type);
 7696:         foreach my $item (@contacts) {
 7697:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 7698:                 $contacts_hash{contacts}{$type}{$item} = 1;
 7699:             } else {
 7700:                 $contacts_hash{contacts}{$type}{$item} = 0;
 7701:             }
 7702:         }  
 7703:         $others{$type} = $env{'form.'.$type.'_others'};
 7704:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 7705:         if ($type eq 'helpdeskmail') {
 7706:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 7707:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 7708:         }
 7709:     }
 7710:     foreach my $item (@contacts) {
 7711:         $to{$item} = $env{'form.'.$item};
 7712:         $contacts_hash{'contacts'}{$item} = $to{$item};
 7713:     }
 7714:     foreach my $item (@toggles) {
 7715:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
 7716:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
 7717:         }
 7718:     }
 7719:     if (keys(%currsetting) > 0) {
 7720:         foreach my $item (@contacts) {
 7721:             if ($to{$item} ne $currsetting{$item}) {
 7722:                 $changes{$item} = 1;
 7723:             }
 7724:         }
 7725:         foreach my $type (@mailings) {
 7726:             foreach my $item (@contacts) {
 7727:                 if (ref($currsetting{$type}) eq 'HASH') {
 7728:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 7729:                         push(@{$changes{$type}},$item);
 7730:                     }
 7731:                 } else {
 7732:                     push(@{$changes{$type}},@{$newsetting{$type}});
 7733:                 }
 7734:             }
 7735:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 7736:                 push(@{$changes{$type}},'others');
 7737:             }
 7738:             if ($type eq 'helpdeskmail') {   
 7739:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 7740:                     push(@{$changes{$type}},'bcc'); 
 7741:                 }
 7742:             }
 7743:         }
 7744:     } else {
 7745:         my %default;
 7746:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 7747:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 7748:         $default{'errormail'} = 'adminemail';
 7749:         $default{'packagesmail'} = 'adminemail';
 7750:         $default{'helpdeskmail'} = 'supportemail';
 7751:         $default{'lonstatusmail'} = 'adminemail';
 7752:         $default{'requestsmail'} = 'adminemail';
 7753:         $default{'updatesmail'} = 'adminemail';
 7754:         foreach my $item (@contacts) {
 7755:            if ($to{$item} ne $default{$item}) {
 7756:               $changes{$item} = 1;
 7757:            }
 7758:         }
 7759:         foreach my $type (@mailings) {
 7760:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 7761:                
 7762:                 push(@{$changes{$type}},@{$newsetting{$type}});
 7763:             }
 7764:             if ($others{$type} ne '') {
 7765:                 push(@{$changes{$type}},'others');
 7766:             }
 7767:             if ($type eq 'helpdeskmail') {
 7768:                 if ($bcc{$type} ne '') {
 7769:                     push(@{$changes{$type}},'bcc');
 7770:                 }
 7771:             }
 7772:         }
 7773:     }
 7774:     foreach my $item (@toggles) {
 7775:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
 7776:             $changes{$item} = 1;
 7777:         } elsif ((!$env{'form.'.$item}) &&
 7778:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
 7779:             $changes{$item} = 1;
 7780:         }
 7781:     }
 7782:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 7783:                                              $dom);
 7784:     if ($putresult eq 'ok') {
 7785:         if (keys(%changes) > 0) {
 7786:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 7787:             if (ref($lastactref) eq 'HASH') {
 7788:                 $lastactref->{'domainconfig'} = 1;
 7789:             }
 7790:             my ($titles,$short_titles)  = &contact_titles();
 7791:             $resulttext = &mt('Changes made:').'<ul>';
 7792:             foreach my $item (@contacts) {
 7793:                 if ($changes{$item}) {
 7794:                     $resulttext .= '<li>'.$titles->{$item}.
 7795:                                     &mt(' set to: ').
 7796:                                     '<span class="LC_cusr_emph">'.
 7797:                                     $to{$item}.'</span></li>';
 7798:                 }
 7799:             }
 7800:             foreach my $type (@mailings) {
 7801:                 if (ref($changes{$type}) eq 'ARRAY') {
 7802:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 7803:                     my @text;
 7804:                     foreach my $item (@{$newsetting{$type}}) {
 7805:                         push(@text,$short_titles->{$item});
 7806:                     }
 7807:                     if ($others{$type} ne '') {
 7808:                         push(@text,$others{$type});
 7809:                     }
 7810:                     $resulttext .= '<span class="LC_cusr_emph">'.
 7811:                                    join(', ',@text).'</span>';
 7812:                     if ($type eq 'helpdeskmail') {
 7813:                         if ($bcc{$type} ne '') {
 7814:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 7815:                         }
 7816:                     }
 7817:                     $resulttext .= '</li>';
 7818:                 }
 7819:             }
 7820:             my @offon = ('off','on');
 7821:             if ($changes{'reporterrors'}) {
 7822:                 $resulttext .= '<li>'.
 7823:                                &mt('E-mail error reports to [_1] set to "'.
 7824:                                    $offon[$env{'form.reporterrors'}].'".',
 7825:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 7826:                                        &mt('LON-CAPA core group - MSU'),600,500)).
 7827:                                '</li>';
 7828:             }
 7829:             if ($changes{'reportupdates'}) {
 7830:                 $resulttext .= '<li>'.
 7831:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
 7832:                                     $offon[$env{'form.reportupdates'}].'".',
 7833:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 7834:                                         &mt('LON-CAPA core group - MSU'),600,500)).
 7835:                                 '</li>';
 7836:             }
 7837:             $resulttext .= '</ul>';
 7838:         } else {
 7839:             $resulttext = &mt('No changes made to contact information');
 7840:         }
 7841:     } else {
 7842:         $resulttext = '<span class="LC_error">'.
 7843:             &mt('An error occurred: [_1].',$putresult).'</span>';
 7844:     }
 7845:     return $resulttext;
 7846: }
 7847: 
 7848: sub modify_usercreation {
 7849:     my ($dom,%domconfig) = @_;
 7850:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
 7851:     my $warningmsg;
 7852:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 7853:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 7854:             if ($key eq 'cancreate') {
 7855:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 7856:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 7857:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 7858:                             ($item eq 'captcha') || ($item eq 'recaptchakeys')) {
 7859:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 7860:                         } else {
 7861:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 7862:                         }
 7863:                     }
 7864:                 }
 7865:             } elsif ($key eq 'email_rule') {
 7866:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 7867:             } else {
 7868:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 7869:             }
 7870:         }
 7871:     }
 7872:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 7873:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 7874:     my @contexts = ('author','course','requestcrs');
 7875:     foreach my $item(@contexts) {
 7876:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
 7877:     }
 7878:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 7879:         foreach my $item (@contexts) {
 7880:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 7881:                 push(@{$changes{'cancreate'}},$item);
 7882:             }
 7883:         }
 7884:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 7885:         foreach my $item (@contexts) {
 7886:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 7887:                 if ($cancreate{$item} ne 'any') {
 7888:                     push(@{$changes{'cancreate'}},$item);
 7889:                 }
 7890:             } else {
 7891:                 if ($cancreate{$item} ne 'none') {
 7892:                     push(@{$changes{'cancreate'}},$item);
 7893:                 }
 7894:             }
 7895:         }
 7896:     } else {
 7897:         foreach my $item (@contexts)  {
 7898:             push(@{$changes{'cancreate'}},$item);
 7899:         }
 7900:     }
 7901: 
 7902:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 7903:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 7904:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 7905:                 push(@{$changes{'username_rule'}},$type);
 7906:             }
 7907:         }
 7908:         foreach my $type (@username_rule) {
 7909:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 7910:                 push(@{$changes{'username_rule'}},$type);
 7911:             }
 7912:         }
 7913:     } else {
 7914:         push(@{$changes{'username_rule'}},@username_rule);
 7915:     }
 7916: 
 7917:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 7918:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 7919:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 7920:                 push(@{$changes{'id_rule'}},$type);
 7921:             }
 7922:         }
 7923:         foreach my $type (@id_rule) {
 7924:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 7925:                 push(@{$changes{'id_rule'}},$type);
 7926:             }
 7927:         }
 7928:     } else {
 7929:         push(@{$changes{'id_rule'}},@id_rule);
 7930:     }
 7931: 
 7932:     my @authen_contexts = ('author','course','domain');
 7933:     my @authtypes = ('int','krb4','krb5','loc');
 7934:     my %authhash;
 7935:     foreach my $item (@authen_contexts) {
 7936:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 7937:         foreach my $auth (@authtypes) {
 7938:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 7939:                 $authhash{$item}{$auth} = 1;
 7940:             } else {
 7941:                 $authhash{$item}{$auth} = 0;
 7942:             }
 7943:         }
 7944:     }
 7945:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 7946:         foreach my $item (@authen_contexts) {
 7947:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 7948:                 foreach my $auth (@authtypes) {
 7949:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 7950:                         push(@{$changes{'authtypes'}},$item);
 7951:                         last;
 7952:                     }
 7953:                 }
 7954:             }
 7955:         }
 7956:     } else {
 7957:         foreach my $item (@authen_contexts) {
 7958:             push(@{$changes{'authtypes'}},$item);
 7959:         }
 7960:     }
 7961: 
 7962:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
 7963:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
 7964:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
 7965:     $save_usercreate{'id_rule'} = \@id_rule;
 7966:     $save_usercreate{'username_rule'} = \@username_rule,
 7967:     $save_usercreate{'authtypes'} = \%authhash;
 7968: 
 7969:     my %usercreation_hash =  (
 7970:         usercreation     => \%save_usercreate,
 7971:     );
 7972: 
 7973:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 7974:                                              $dom);
 7975: 
 7976:     if ($putresult eq 'ok') {
 7977:         if (keys(%changes) > 0) {
 7978:             $resulttext = &mt('Changes made:').'<ul>';
 7979:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 7980:                 my %lt = &usercreation_types();
 7981:                 foreach my $type (@{$changes{'cancreate'}}) {
 7982:                     my $chgtext = $lt{$type}.', ';
 7983:                     if ($cancreate{$type} eq 'none') {
 7984:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 7985:                     } elsif ($cancreate{$type} eq 'any') {
 7986:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 7987:                     } elsif ($cancreate{$type} eq 'official') {
 7988:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 7989:                     } elsif ($cancreate{$type} eq 'unofficial') {
 7990:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 7991:                     }
 7992:                     $resulttext .= '<li>'.$chgtext.'</li>';
 7993:                 }
 7994:             }
 7995:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 7996:                 my ($rules,$ruleorder) = 
 7997:                     &Apache::lonnet::inst_userrules($dom,'username');
 7998:                 my $chgtext = '<ul>';
 7999:                 foreach my $type (@username_rule) {
 8000:                     if (ref($rules->{$type}) eq 'HASH') {
 8001:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 8002:                     }
 8003:                 }
 8004:                 $chgtext .= '</ul>';
 8005:                 if (@username_rule > 0) {
 8006:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 8007:                 } else {
 8008:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 8009:                 }
 8010:             }
 8011:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 8012:                 my ($idrules,$idruleorder) = 
 8013:                     &Apache::lonnet::inst_userrules($dom,'id');
 8014:                 my $chgtext = '<ul>';
 8015:                 foreach my $type (@id_rule) {
 8016:                     if (ref($idrules->{$type}) eq 'HASH') {
 8017:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 8018:                     }
 8019:                 }
 8020:                 $chgtext .= '</ul>';
 8021:                 if (@id_rule > 0) {
 8022:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 8023:                 } else {
 8024:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 8025:                 }
 8026:             }
 8027:             my %authname = &authtype_names();
 8028:             my %context_title = &context_names();
 8029:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 8030:                 my $chgtext = '<ul>';
 8031:                 foreach my $type (@{$changes{'authtypes'}}) {
 8032:                     my @allowed;
 8033:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 8034:                     foreach my $auth (@authtypes) {
 8035:                         if ($authhash{$type}{$auth}) {
 8036:                             push(@allowed,$authname{$auth});
 8037:                         }
 8038:                     }
 8039:                     if (@allowed > 0) {
 8040:                         $chgtext .= join(', ',@allowed).'</li>';
 8041:                     } else {
 8042:                         $chgtext .= &mt('none').'</li>';
 8043:                     }
 8044:                 }
 8045:                 $chgtext .= '</ul>';
 8046:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 8047:                 $resulttext .= '</li>';
 8048:             }
 8049:             $resulttext .= '</ul>';
 8050:         } else {
 8051:             $resulttext = &mt('No changes made to user creation settings');
 8052:         }
 8053:     } else {
 8054:         $resulttext = '<span class="LC_error">'.
 8055:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8056:     }
 8057:     if ($warningmsg ne '') {
 8058:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 8059:     }
 8060:     return $resulttext;
 8061: }
 8062: 
 8063: sub modify_selfcreation {
 8064:     my ($dom,%domconfig) = @_;
 8065:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%changes,%cancreate);
 8066:     my (%save_usercreate,%save_usermodify);
 8067:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8068:     if (ref($types) eq 'ARRAY') {
 8069:         $usertypes->{'default'} = $othertitle;
 8070:         push(@{$types},'default');
 8071:     }
 8072: #
 8073: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
 8074: #
 8075:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 8076:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 8077:             if ($key eq 'cancreate') {
 8078:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 8079:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 8080:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 8081:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') || 
 8082:                             ($item eq 'emailusername') || ($item eq 'notify')) {
 8083:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8084:                         } else {
 8085:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8086:                         }
 8087:                     }
 8088:                 }
 8089:             } elsif ($key eq 'email_rule') {
 8090:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 8091:             } else {
 8092:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 8093:             }
 8094:         }
 8095:     }
 8096: #
 8097: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
 8098: #
 8099:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 8100:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 8101:             if ($key eq 'selfcreate') {
 8102:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
 8103:             } else {
 8104:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
 8105:             }
 8106:         }
 8107:     }
 8108: 
 8109:     my @contexts = ('selfcreate');
 8110:     @{$cancreate{'selfcreate'}} = ();
 8111:     %{$cancreate{'emailusername'}} = ();
 8112:     @{$cancreate{'statustocreate'}} = ();
 8113:     my %selfcreatetypes = (
 8114:                              sso   => 'users authenticated by institutional single sign on',
 8115:                              login => 'users authenticated by institutional log-in',
 8116:                              email => 'users who provide a valid e-mail address for use as username (automatic creation)',
 8117:                              emailapproval => 'users who provide a valid e-mail address for use as username (queued for Domain Coordinator review)',
 8118:                           );
 8119: #
 8120: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
 8121: # is permitted.
 8122: #
 8123:     foreach my $item ('login','sso','email') {
 8124:         if ($item eq 'email') {
 8125:             if ($env{'form.cancreate_email'} eq 'email') {
 8126:                 push(@{$cancreate{'selfcreate'}},'email');
 8127:             } elsif ($env{'form.cancreate_email'} eq 'emailapproval') {
 8128:                 push(@{$cancreate{'selfcreate'}},'emailapproval');
 8129:             }
 8130:         } else {
 8131:             if ($env{'form.cancreate_'.$item}) {
 8132:                 push(@{$cancreate{'selfcreate'}},$item);
 8133:             }
 8134:         }
 8135:     }
 8136:     my (@email_rule,%userinfo,%savecaptcha);
 8137:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 8138: #
 8139: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
 8140: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
 8141: #
 8142:     if (($env{'form.cancreate_email'} eq 'email') || ($env{'form.cancreate_email'} eq 'emailapproval')) {
 8143:         push(@contexts,'emailusername');
 8144:         if (ref($types) eq 'ARRAY') {
 8145:             foreach my $type (@{$types}) {
 8146:                 if (ref($infofields) eq 'ARRAY') {
 8147:                     foreach my $field (@{$infofields}) {
 8148:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
 8149:                             $cancreate{'emailusername'}{$type}{$field} = $1;
 8150:                         }
 8151:                     }
 8152:                 }
 8153:             }
 8154:         }
 8155: #
 8156: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
 8157: # queued requests for self-creation of account using e-mail address as username
 8158: #
 8159: 
 8160:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
 8161:         @approvalnotify = sort(@approvalnotify);
 8162:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
 8163:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8164:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
 8165:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
 8166:                     push(@{$changes{'cancreate'}},'notify');
 8167:                 }
 8168:             } else {
 8169:                 if ($cancreate{'notify'}{'approval'}) {
 8170:                     push(@{$changes{'cancreate'}},'notify');
 8171:                 }
 8172:             }
 8173:         } elsif ($cancreate{'notify'}{'approval'}) {
 8174:             push(@{$changes{'cancreate'}},'notify');
 8175:         }
 8176: 
 8177: #
 8178: # Retrieve rules (if any) governing types of e-mail address which may be used as a username
 8179: #
 8180:         @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 8181:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
 8182:         if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 8183:             if (@{$curr_usercreation{'email_rule'}} > 0) {
 8184:                 foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 8185:                     if (!grep(/^\Q$type\E$/,@email_rule)) {
 8186:                         push(@{$changes{'email_rule'}},$type);
 8187:                     }
 8188:                 }
 8189:             }
 8190:             if (@email_rule > 0) {
 8191:                 foreach my $type (@email_rule) {
 8192:                     if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 8193:                         push(@{$changes{'email_rule'}},$type);
 8194:                     }
 8195:                 }
 8196:             }
 8197:         } elsif (@email_rule > 0) {
 8198:             push(@{$changes{'email_rule'}},@email_rule);
 8199:         }
 8200:     }
 8201: #  
 8202: # Check if domain default is set appropriately, if selef-creation of accounts is to be available for
 8203: # institutional log-in.
 8204: #
 8205:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
 8206:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8207:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
 8208:                ($domdefaults{'auth_def'} eq 'localauth'))) {
 8209:             $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.').' '.
 8210:                           &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.');
 8211:         }
 8212:     }
 8213:     my @fields = ('lastname','firstname','middlename','generation',
 8214:                   'permanentemail','id');
 8215:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8216: #
 8217: # Where usernames may created for institutional log-in and/or institutional single sign on:
 8218: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
 8219: # may self-create accounts 
 8220: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
 8221: # which the user may supply, if institutional data is unavailable.
 8222: #
 8223:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
 8224:         if (ref($types) eq 'ARRAY') {
 8225:             if (@{$types} > 1) {
 8226:                 @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
 8227:                 push(@contexts,'statustocreate');
 8228:             } else {
 8229:                 undef($cancreate{'statustocreate'});
 8230:             } 
 8231:             foreach my $type (@{$types}) {
 8232:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
 8233:                 foreach my $field (@fields) {
 8234:                     if (grep(/^\Q$field\E$/,@modifiable)) {
 8235:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
 8236:                     } else {
 8237:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
 8238:                     }
 8239:                 }
 8240:             }
 8241:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
 8242:                 foreach my $type (@{$types}) {
 8243:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
 8244:                         foreach my $field (@fields) {
 8245:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
 8246:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
 8247:                                 push(@{$changes{'selfcreate'}},$type);
 8248:                                 last;
 8249:                             }
 8250:                         }
 8251:                     }
 8252:                 }
 8253:             } else {
 8254:                 foreach my $type (@{$types}) {
 8255:                     push(@{$changes{'selfcreate'}},$type);
 8256:                 }
 8257:             }
 8258:         }
 8259:     }
 8260:     foreach my $item (@contexts) {
 8261:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 8262:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 8263:                 if (ref($cancreate{$item}) eq 'ARRAY') {
 8264:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 8265:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8266:                             push(@{$changes{'cancreate'}},$item);
 8267:                         }
 8268:                     }
 8269:                 }
 8270:             }
 8271:             if (ref($cancreate{$item}) eq 'ARRAY') {
 8272:                 foreach my $type (@{$cancreate{$item}}) {
 8273:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 8274:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8275:                             push(@{$changes{'cancreate'}},$item);
 8276:                         }
 8277:                     }
 8278:                 }
 8279:             }
 8280:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
 8281:             if (ref($cancreate{$item}) eq 'HASH') {
 8282:                 foreach my $curr (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
 8283:                     if (ref($curr_usercreation{'cancreate'}{$item}{$curr}) eq 'HASH') {
 8284:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$curr}})) {
 8285:                             unless ($curr_usercreation{'cancreate'}{$item}{$curr}{$field} eq $cancreate{$item}{$curr}{$field}) {
 8286:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8287:                                     push(@{$changes{'cancreate'}},$item);
 8288:                                 }
 8289:                             }
 8290:                         }
 8291:                     } else {
 8292:                         if (!$cancreate{$item}{$curr}) {
 8293:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8294:                                 push(@{$changes{'cancreate'}},$item);
 8295:                             }
 8296:                         }
 8297:                     }
 8298:                 }
 8299:                 foreach my $field (keys(%{$cancreate{$item}})) {
 8300:                     if (ref($cancreate{$item}{$field}) eq 'HASH') {
 8301:                         foreach my $inner (keys(%{$cancreate{$item}{$field}})) {
 8302:                             if (ref($curr_usercreation{'cancreate'}{$item}{$field}) eq 'HASH') {
 8303:                                 unless ($curr_usercreation{'cancreate'}{$item}{$field}{$inner} eq $cancreate{$item}{$field}{$inner}) {
 8304:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8305:                                         push(@{$changes{'cancreate'}},$item);
 8306:                                     }
 8307:                                 }
 8308:                             } else {
 8309:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8310:                                     push(@{$changes{'cancreate'}},$item);
 8311:                                 }
 8312:                             }
 8313:                         }
 8314:                     } else {
 8315:                         if (!$curr_usercreation{'cancreate'}{$item}{$field}) {
 8316:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8317:                                 push(@{$changes{'cancreate'}},$item);
 8318:                             }
 8319:                         }
 8320:                     }
 8321:                 }
 8322:             }
 8323:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
 8324:             if (ref($cancreate{$item}) eq 'ARRAY') {
 8325:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
 8326:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8327:                         push(@{$changes{'cancreate'}},$item);
 8328:                     }
 8329:                 }
 8330:             } elsif (ref($cancreate{$item}) eq 'HASH') {
 8331:                 if (!$cancreate{$item}{$curr_usercreation{'cancreate'}{$item}}) {
 8332:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8333:                         push(@{$changes{'cancreate'}},$item);
 8334:                     }
 8335:                 }
 8336:             }
 8337:         } elsif ($item eq 'emailusername') {
 8338:             if (ref($cancreate{$item}) eq 'HASH') {
 8339:                 foreach my $type (keys(%{$cancreate{$item}})) {
 8340:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
 8341:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
 8342:                             if ($cancreate{$item}{$type}{$field}) {
 8343:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8344:                                     push(@{$changes{'cancreate'}},$item);
 8345:                                 }
 8346:                                 last;
 8347:                             }
 8348:                         }
 8349:                     }
 8350:                 }
 8351:             }
 8352:         }
 8353:     }
 8354: #
 8355: # Populate %save_usercreate hash with updates to self-creation configuration.
 8356: #
 8357:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
 8358:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
 8359:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
 8360:     if (ref($cancreate{'notify'}) eq 'HASH') {
 8361:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
 8362:     }
 8363:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 8364:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
 8365:     }
 8366:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
 8367:     $save_usercreate{'emailrule'} = \@email_rule;
 8368: 
 8369:     my %userconfig_hash = (
 8370:             usercreation     => \%save_usercreate,
 8371:             usermodification => \%save_usermodify,
 8372:     );
 8373:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
 8374:                                              $dom);
 8375: #
 8376: # Accumulate details of changes to domain cofiguration for self-creation of usernames in $resulttext
 8377: #
 8378:     if ($putresult eq 'ok') {
 8379:         if (keys(%changes) > 0) {
 8380:             $resulttext = &mt('Changes made:').'<ul>';
 8381:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8382:                 my %lt = &selfcreation_types();
 8383:                 foreach my $type (@{$changes{'cancreate'}}) {
 8384:                     my $chgtext;
 8385:                     if ($type eq 'selfcreate') {
 8386:                         if (@{$cancreate{$type}} == 0) {
 8387:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
 8388:                         } else {
 8389:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
 8390:                                         '<ul>';
 8391:                             foreach my $case (@{$cancreate{$type}}) {
 8392:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 8393:                             }
 8394:                             $chgtext .= '</ul>';
 8395:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 8396:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 8397:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 8398:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 8399:                                             $chgtext .= '<br />'.
 8400:                                                         '<span class="LC_warning">'.
 8401:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 8402:                                                         '</span>';
 8403:                                         }
 8404:                                     }
 8405:                                 }
 8406:                             }
 8407:                         }
 8408:                     } elsif ($type eq 'statustocreate') {
 8409:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 8410:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 8411:                             if (@{$cancreate{'selfcreate'}} > 0) {
 8412:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 8413:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 8414:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 8415:                                         $chgtext .= '<br />'.
 8416:                                                     '<span class="LC_warning">'.
 8417:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 8418:                                                     '</span>';
 8419:                                     }
 8420:                                 } elsif (ref($usertypes) eq 'HASH') {
 8421:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 8422:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 8423:                                     } else {
 8424:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 8425:                                     }
 8426:                                     $chgtext .= '<ul>';
 8427:                                     foreach my $case (@{$cancreate{$type}}) {
 8428:                                         if ($case eq 'default') {
 8429:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 8430:                                         } else {
 8431:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 8432:                                         }
 8433:                                     }
 8434:                                     $chgtext .= '</ul>';
 8435:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 8436:                                         $chgtext .= '<br /><span class="LC_warning">'.
 8437:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
 8438:                                                     '</span>';
 8439:                                     }
 8440:                                 }
 8441:                             } else {
 8442:                                 if (@{$cancreate{$type}} == 0) {
 8443:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 8444:                                 } else {
 8445:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 8446:                                 }
 8447:                             }
 8448:                         }
 8449:                     } elsif ($type eq 'captcha') {
 8450:                         if ($savecaptcha{$type} eq 'notused') {
 8451:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 8452:                         } else {
 8453:                             my %captchas = &captcha_phrases();
 8454:                             if ($captchas{$savecaptcha{$type}}) {
 8455:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
 8456:                             } else {
 8457:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
 8458:                             }
 8459:                         }
 8460:                     } elsif ($type eq 'recaptchakeys') {
 8461:                         my ($privkey,$pubkey);
 8462:                         if (ref($savecaptcha{$type}) eq 'HASH') {
 8463:                             $pubkey = $savecaptcha{$type}{'public'};
 8464:                             $privkey = $savecaptcha{$type}{'private'};
 8465:                         }
 8466:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 8467:                         if (!$pubkey) {
 8468:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 8469:                         } else {
 8470:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 8471:                         }
 8472:                         if (!$privkey) {
 8473:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 8474:                         } else {
 8475:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 8476:                         }
 8477:                         $chgtext .= '</ul>';
 8478:                     } elsif ($type eq 'emailusername') {
 8479:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
 8480:                             if (ref($types) eq 'ARRAY') {
 8481:                                 foreach my $type (@{$types}) {
 8482:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
 8483:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
 8484:                                             $chgtext .= &mt('When self-creating account with e-mail as username, the following information will be provided by [_1]:',$usertypes->{$type}).
 8485:                                                     '<ul>';
 8486:                                             foreach my $field (@{$infofields}) {
 8487:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
 8488:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
 8489:                                                 }
 8490:                                             }
 8491:                                         }
 8492:                                         $chgtext .= '</ul>';
 8493:                                     } else {
 8494:                                         $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 />';
 8495:                                     }
 8496:                                 }
 8497:                             }
 8498:                         }
 8499:                     } elsif ($type eq 'notify') {
 8500:                         $chgtext = &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
 8501:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8502:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
 8503:                                 if ($cancreate{'notify'}{'approval'}) {
 8504:                                     $chgtext = &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
 8505:                                 }
 8506:                             }
 8507:                         }
 8508:                     }
 8509:                     if ($chgtext) {
 8510:                         $resulttext .= '<li>'.$chgtext.'</li>';
 8511:                     }
 8512:                 }
 8513:             }
 8514:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 8515:                 my ($emailrules,$emailruleorder) =
 8516:                     &Apache::lonnet::inst_userrules($dom,'email');
 8517:                 my $chgtext = '<ul>';
 8518:                 foreach my $type (@email_rule) {
 8519:                     if (ref($emailrules->{$type}) eq 'HASH') {
 8520:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 8521:                     }
 8522:                 }
 8523:                 $chgtext .= '</ul>';
 8524:                 if (@email_rule > 0) {
 8525:                     $resulttext .= '<li>'.
 8526:                                    &mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').
 8527:                                        $chgtext.
 8528:                                    '</li>';
 8529:                 } else {
 8530:                     $resulttext .= '<li>'.
 8531:                                    &mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').
 8532:                                    '</li>';
 8533:                 }
 8534:             }
 8535:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
 8536:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
 8537:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8538:                 foreach my $type (@{$changes{'selfcreate'}}) {
 8539:                     my $typename = $type;
 8540:                     if (ref($usertypes) eq 'HASH') {
 8541:                         if ($usertypes->{$type} ne '') {
 8542:                             $typename = $usertypes->{$type};
 8543:                         }
 8544:                     }
 8545:                     my @modifiable;
 8546:                     $resulttext .= '<li>'.
 8547:                                     &mt('Self-creation of account by users with status: [_1]',
 8548:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
 8549:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
 8550:                     foreach my $field (@fields) {
 8551:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
 8552:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
 8553:                         }
 8554:                     }
 8555:                     if (@modifiable > 0) {
 8556:                         $resulttext .= join(', ',@modifiable);
 8557:                     } else {
 8558:                         $resulttext .= &mt('none');
 8559:                     }
 8560:                     $resulttext .= '</li>';
 8561:                 }
 8562:                 $resulttext .= '</ul></li>';
 8563:             }
 8564:             $resulttext .= '</ul>';
 8565:         } else {
 8566:             $resulttext = &mt('No changes made to self-creation settings');
 8567:         }
 8568:     } else {
 8569:         $resulttext = '<span class="LC_error">'.
 8570:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8571:     }
 8572:     if ($warningmsg ne '') {
 8573:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 8574:     }
 8575:     return $resulttext;
 8576: }
 8577: 
 8578: sub process_captcha {
 8579:     my ($container,$changes,$newsettings,$current) = @_;
 8580:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 8581:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 8582:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 8583:         $newsettings->{'captcha'} = 'original';
 8584:     }
 8585:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 8586:         if ($container eq 'cancreate') {
 8587:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 8588:                 push(@{$changes->{'cancreate'}},'captcha');
 8589:             } elsif (!defined($changes->{'cancreate'})) {
 8590:                 $changes->{'cancreate'} = ['captcha'];
 8591:             }
 8592:         } else {
 8593:             $changes->{'captcha'} = 1;
 8594:         }
 8595:     }
 8596:     my ($newpub,$newpriv,$currpub,$currpriv);
 8597:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 8598:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 8599:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 8600:         $newpub =~ s/\W//g;
 8601:         $newpriv =~ s/\W//g;
 8602:         $newsettings->{'recaptchakeys'} = {
 8603:                                              public  => $newpub,
 8604:                                              private => $newpriv,
 8605:                                           };
 8606:     }
 8607:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 8608:         $currpub = $current->{'recaptchakeys'}{'public'};
 8609:         $currpriv = $current->{'recaptchakeys'}{'private'};
 8610:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
 8611:             $newsettings->{'recaptchakeys'} = {
 8612:                                                  public  => '',
 8613:                                                  private => '',
 8614:                                               }
 8615:         }
 8616:     }
 8617:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 8618:         if ($container eq 'cancreate') {
 8619:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 8620:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
 8621:             } elsif (!defined($changes->{'cancreate'})) {
 8622:                 $changes->{'cancreate'} = ['recaptchakeys'];
 8623:             }
 8624:         } else {
 8625:             $changes->{'recaptchakeys'} = 1;
 8626:         }
 8627:     }
 8628:     return;
 8629: }
 8630: 
 8631: sub modify_usermodification {
 8632:     my ($dom,%domconfig) = @_;
 8633:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
 8634:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 8635:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 8636:             if ($key eq 'selfcreate') {
 8637:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
 8638:             } else {  
 8639:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 8640:             }
 8641:         }
 8642:     }
 8643:     my @contexts = ('author','course');
 8644:     my %context_title = (
 8645:                            author => 'In author context',
 8646:                            course => 'In course context',
 8647:                         );
 8648:     my @fields = ('lastname','firstname','middlename','generation',
 8649:                   'permanentemail','id');
 8650:     my %roles = (
 8651:                   author => ['ca','aa'],
 8652:                   course => ['st','ep','ta','in','cr'],
 8653:                 );
 8654:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8655:     foreach my $context (@contexts) {
 8656:         foreach my $role (@{$roles{$context}}) {
 8657:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 8658:             foreach my $item (@fields) {
 8659:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 8660:                     $modifyhash{$context}{$role}{$item} = 1;
 8661:                 } else {
 8662:                     $modifyhash{$context}{$role}{$item} = 0;
 8663:                 }
 8664:             }
 8665:         }
 8666:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 8667:             foreach my $role (@{$roles{$context}}) {
 8668:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 8669:                     foreach my $field (@fields) {
 8670:                         if ($modifyhash{$context}{$role}{$field} ne 
 8671:                                 $curr_usermodification{$context}{$role}{$field}) {
 8672:                             push(@{$changes{$context}},$role);
 8673:                             last;
 8674:                         }
 8675:                     }
 8676:                 }
 8677:             }
 8678:         } else {
 8679:             foreach my $context (@contexts) {
 8680:                 foreach my $role (@{$roles{$context}}) {
 8681:                     push(@{$changes{$context}},$role);
 8682:                 }
 8683:             }
 8684:         }
 8685:     }
 8686:     my %usermodification_hash =  (
 8687:                                    usermodification => \%modifyhash,
 8688:                                  );
 8689:     my $putresult = &Apache::lonnet::put_dom('configuration',
 8690:                                              \%usermodification_hash,$dom);
 8691:     if ($putresult eq 'ok') {
 8692:         if (keys(%changes) > 0) {
 8693:             $resulttext = &mt('Changes made: ').'<ul>';
 8694:             foreach my $context (@contexts) {
 8695:                 if (ref($changes{$context}) eq 'ARRAY') {
 8696:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 8697:                     if (ref($changes{$context}) eq 'ARRAY') {
 8698:                         foreach my $role (@{$changes{$context}}) {
 8699:                             my $rolename;
 8700:                             if ($role eq 'cr') {
 8701:                                 $rolename = &mt('Custom');
 8702:                             } else {
 8703:                                 $rolename = &Apache::lonnet::plaintext($role);
 8704:                             }
 8705:                             my @modifiable;
 8706:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 8707:                             foreach my $field (@fields) {
 8708:                                 if ($modifyhash{$context}{$role}{$field}) {
 8709:                                     push(@modifiable,$fieldtitles{$field});
 8710:                                 }
 8711:                             }
 8712:                             if (@modifiable > 0) {
 8713:                                 $resulttext .= join(', ',@modifiable);
 8714:                             } else {
 8715:                                 $resulttext .= &mt('none'); 
 8716:                             }
 8717:                             $resulttext .= '</li>';
 8718:                         }
 8719:                         $resulttext .= '</ul></li>';
 8720:                     }
 8721:                 }
 8722:             }
 8723:             $resulttext .= '</ul>';
 8724:         } else {
 8725:             $resulttext = &mt('No changes made to user modification settings');
 8726:         }
 8727:     } else {
 8728:         $resulttext = '<span class="LC_error">'.
 8729:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8730:     }
 8731:     return $resulttext;
 8732: }
 8733: 
 8734: sub modify_defaults {
 8735:     my ($dom,$lastactref,%domconfig) = @_;
 8736:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 8737:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8738:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 8739:     my @authtypes = ('internal','krb4','krb5','localauth');
 8740:     foreach my $item (@items) {
 8741:         $newvalues{$item} = $env{'form.'.$item};
 8742:         if ($item eq 'auth_def') {
 8743:             if ($newvalues{$item} ne '') {
 8744:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 8745:                     push(@errors,$item);
 8746:                 }
 8747:             }
 8748:         } elsif ($item eq 'lang_def') {
 8749:             if ($newvalues{$item} ne '') {
 8750:                 if ($newvalues{$item} =~ /^(\w+)/) {
 8751:                     my $langcode = $1;
 8752:                     if ($langcode ne 'x_chef') {
 8753:                         if (code2language($langcode) eq '') {
 8754:                             push(@errors,$item);
 8755:                         }
 8756:                     }
 8757:                 } else {
 8758:                     push(@errors,$item);
 8759:                 }
 8760:             }
 8761:         } elsif ($item eq 'timezone_def') {
 8762:             if ($newvalues{$item} ne '') {
 8763:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 8764:                     push(@errors,$item);   
 8765:                 }
 8766:             }
 8767:         } elsif ($item eq 'datelocale_def') {
 8768:             if ($newvalues{$item} ne '') {
 8769:                 my @datelocale_ids = DateTime::Locale->ids();
 8770:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 8771:                     push(@errors,$item);
 8772:                 }
 8773:             }
 8774:         } elsif ($item eq 'portal_def') {
 8775:             if ($newvalues{$item} ne '') {
 8776:                 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])\/?$/) {
 8777:                     push(@errors,$item);
 8778:                 }
 8779:             }
 8780:         }
 8781:         if (grep(/^\Q$item\E$/,@errors)) {
 8782:             $newvalues{$item} = $domdefaults{$item};
 8783:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 8784:             $changes{$item} = 1;
 8785:         }
 8786:         $domdefaults{$item} = $newvalues{$item};
 8787:     }
 8788:     my %defaults_hash = (
 8789:                          defaults => \%newvalues,
 8790:                         );
 8791:     my $title = &defaults_titles();
 8792:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 8793:                                              $dom);
 8794:     if ($putresult eq 'ok') {
 8795:         if (keys(%changes) > 0) {
 8796:             $resulttext = &mt('Changes made:').'<ul>';
 8797:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
 8798:             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";
 8799:             foreach my $item (sort(keys(%changes))) {
 8800:                 my $value = $env{'form.'.$item};
 8801:                 if ($value eq '') {
 8802:                     $value = &mt('none');
 8803:                 } elsif ($item eq 'auth_def') {
 8804:                     my %authnames = &authtype_names();
 8805:                     my %shortauth = (
 8806:                              internal => 'int',
 8807:                              krb4 => 'krb4',
 8808:                              krb5 => 'krb5',
 8809:                              localauth  => 'loc',
 8810:                     );
 8811:                     $value = $authnames{$shortauth{$value}};
 8812:                 }
 8813:                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 8814:                 $mailmsgtext .= "$title->{$item} set to $value\n";  
 8815:             }
 8816:             $resulttext .= '</ul>';
 8817:             $mailmsgtext .= "\n";
 8818:             my $cachetime = 24*60*60;
 8819:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 8820:             if (ref($lastactref) eq 'HASH') {
 8821:                 $lastactref->{'domdefaults'} = 1;
 8822:             }
 8823:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 8824:                 my $notify = 1;
 8825:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
 8826:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
 8827:                         $notify = 0;
 8828:                     }
 8829:                 }
 8830:                 if ($notify) {
 8831:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
 8832:                                                "LON-CAPA Domain Settings Change - $dom",
 8833:                                                $mailmsgtext);
 8834:                 }
 8835:             }
 8836:         } else {
 8837:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 8838:         }
 8839:     } else {
 8840:         $resulttext = '<span class="LC_error">'.
 8841:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8842:     }
 8843:     if (@errors > 0) {
 8844:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 8845:         foreach my $item (@errors) {
 8846:             $resulttext .= ' "'.$title->{$item}.'",';
 8847:         }
 8848:         $resulttext =~ s/,$//;
 8849:     }
 8850:     return $resulttext;
 8851: }
 8852: 
 8853: sub modify_scantron {
 8854:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 8855:     my ($resulttext,%confhash,%changes,$errors);
 8856:     my $custom = 'custom.tab';
 8857:     my $default = 'default.tab';
 8858:     my $servadm = $r->dir_config('lonAdmEMail');
 8859:     my ($configuserok,$author_ok,$switchserver) = 
 8860:         &config_check($dom,$confname,$servadm);
 8861:     if ($env{'form.scantronformat.filename'} ne '') {
 8862:         my $error;
 8863:         if ($configuserok eq 'ok') {
 8864:             if ($switchserver) {
 8865:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 8866:             } else {
 8867:                 if ($author_ok eq 'ok') {
 8868:                     my ($result,$scantronurl) =
 8869:                         &publishlogo($r,'upload','scantronformat',$dom,
 8870:                                      $confname,'scantron','','',$custom);
 8871:                     if ($result eq 'ok') {
 8872:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 8873:                         $changes{'scantronformat'} = 1;
 8874:                     } else {
 8875:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 8876:                     }
 8877:                 } else {
 8878:                     $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);
 8879:                 }
 8880:             }
 8881:         } else {
 8882:             $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);
 8883:         }
 8884:         if ($error) {
 8885:             &Apache::lonnet::logthis($error);
 8886:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8887:         }
 8888:     }
 8889:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 8890:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 8891:             if ($env{'form.scantronformat_del'}) {
 8892:                 $confhash{'scantron'}{'scantronformat'} = '';
 8893:                 $changes{'scantronformat'} = 1;
 8894:             }
 8895:         }
 8896:     }
 8897:     if (keys(%confhash) > 0) {
 8898:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 8899:                                                  $dom);
 8900:         if ($putresult eq 'ok') {
 8901:             if (keys(%changes) > 0) {
 8902:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 8903:                     $resulttext = &mt('Changes made:').'<ul>';
 8904:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 8905:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 8906:                     } else {
 8907:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 8908:                     }
 8909:                     $resulttext .= '</ul>';
 8910:                 } else {
 8911:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 8912:                 }
 8913:                 $resulttext .= '</ul>';
 8914:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 8915:                 if (ref($lastactref) eq 'HASH') {
 8916:                     $lastactref->{'domainconfig'} = 1;
 8917:                 }
 8918:             } else {
 8919:                 $resulttext = &mt('No changes made to bubblesheet format file');
 8920:             }
 8921:         } else {
 8922:             $resulttext = '<span class="LC_error">'.
 8923:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 8924:         }
 8925:     } else {
 8926:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 8927:     }
 8928:     if ($errors) {
 8929:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 8930:                        $errors.'</ul>';
 8931:     }
 8932:     return $resulttext;
 8933: }
 8934: 
 8935: sub modify_coursecategories {
 8936:     my ($dom,%domconfig) = @_;
 8937:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 8938:         $cathash);
 8939:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 8940:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 8941:         $cathash = $domconfig{'coursecategories'}{'cats'};
 8942:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 8943:             $changes{'togglecats'} = 1;
 8944:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 8945:         }
 8946:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 8947:             $changes{'categorize'} = 1;
 8948:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 8949:         }
 8950:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 8951:             $changes{'togglecatscomm'} = 1;
 8952:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 8953:         }
 8954:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 8955:             $changes{'categorizecomm'} = 1;
 8956:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 8957:         }
 8958:     } else {
 8959:         $changes{'togglecats'} = 1;
 8960:         $changes{'categorize'} = 1;
 8961:         $changes{'togglecatscomm'} = 1;
 8962:         $changes{'categorizecomm'} = 1;
 8963:         $domconfig{'coursecategories'} = {
 8964:                                              togglecats => $env{'form.togglecats'},
 8965:                                              categorize => $env{'form.categorize'},
 8966:                                              togglecatscomm => $env{'form.togglecatscomm'},
 8967:                                              categorizecomm => $env{'form.categorizecomm'},
 8968:                                          };
 8969:     }
 8970:     if (ref($cathash) eq 'HASH') {
 8971:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 8972:             push (@deletecategory,'instcode::0');
 8973:         }
 8974:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 8975:             push(@deletecategory,'communities::0');
 8976:         }
 8977:     }
 8978:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 8979:     if (ref($cathash) eq 'HASH') {
 8980:         if (@deletecategory > 0) {
 8981:             #FIXME Need to remove category from all courses using a deleted category 
 8982:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 8983:             foreach my $item (@deletecategory) {
 8984:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 8985:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 8986:                     $deletions{$item} = 1;
 8987:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 8988:                 }
 8989:             }
 8990:         }
 8991:         foreach my $item (keys(%{$cathash})) {
 8992:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 8993:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 8994:                 $reorderings{$item} = 1;
 8995:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 8996:             }
 8997:             if ($env{'form.addcategory_name_'.$item} ne '') {
 8998:                 my $newcat = $env{'form.addcategory_name_'.$item};
 8999:                 my $newdepth = $depth+1;
 9000:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 9001:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 9002:                 $adds{$newitem} = 1; 
 9003:             }
 9004:             if ($env{'form.subcat_'.$item} ne '') {
 9005:                 my $newcat = $env{'form.subcat_'.$item};
 9006:                 my $newdepth = $depth+1;
 9007:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 9008:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 9009:                 $adds{$newitem} = 1;
 9010:             }
 9011:         }
 9012:     }
 9013:     if ($env{'form.instcode'} eq '1') {
 9014:         if (ref($cathash) eq 'HASH') {
 9015:             my $newitem = 'instcode::0';
 9016:             if ($cathash->{$newitem} eq '') {  
 9017:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 9018:                 $adds{$newitem} = 1;
 9019:             }
 9020:         } else {
 9021:             my $newitem = 'instcode::0';
 9022:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 9023:             $adds{$newitem} = 1;
 9024:         }
 9025:     }
 9026:     if ($env{'form.communities'} eq '1') {
 9027:         if (ref($cathash) eq 'HASH') {
 9028:             my $newitem = 'communities::0';
 9029:             if ($cathash->{$newitem} eq '') {
 9030:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 9031:                 $adds{$newitem} = 1;
 9032:             }
 9033:         } else {
 9034:             my $newitem = 'communities::0';
 9035:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 9036:             $adds{$newitem} = 1;
 9037:         }
 9038:     }
 9039:     if ($env{'form.addcategory_name'} ne '') {
 9040:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 9041:             ($env{'form.addcategory_name'} ne 'communities')) {
 9042:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 9043:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 9044:             $adds{$newitem} = 1;
 9045:         }
 9046:     }
 9047:     my $putresult;
 9048:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 9049:         if (keys(%deletions) > 0) {
 9050:             foreach my $key (keys(%deletions)) {
 9051:                 if ($predelallitems{$key} ne '') {
 9052:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 9053:                 }
 9054:             }
 9055:         }
 9056:         my (@chkcats,@chktrails,%chkallitems);
 9057:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 9058:         if (ref($chkcats[0]) eq 'ARRAY') {
 9059:             my $depth = 0;
 9060:             my $chg = 0;
 9061:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 9062:                 my $name = $chkcats[0][$i];
 9063:                 my $item;
 9064:                 if ($name eq '') {
 9065:                     $chg ++;
 9066:                 } else {
 9067:                     $item = &escape($name).'::0';
 9068:                     if ($chg) {
 9069:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 9070:                     }
 9071:                     $depth ++; 
 9072:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 9073:                     $depth --;
 9074:                 }
 9075:             }
 9076:         }
 9077:     }
 9078:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 9079:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 9080:         if ($putresult eq 'ok') {
 9081:             my %title = (
 9082:                          togglecats     => 'Show/Hide a course in catalog',
 9083:                          categorize     => 'Assign a category to a course',
 9084:                          togglecatscomm => 'Show/Hide a community in catalog',
 9085:                          categorizecomm => 'Assign a category to a community',
 9086:                         );
 9087:             my %level = (
 9088:                          dom  => 'set in Domain ("Modify Course/Community")',
 9089:                          crs  => 'set in Course ("Course Configuration")',
 9090:                          comm => 'set in Community ("Community Configuration")',
 9091:                         );
 9092:             $resulttext = &mt('Changes made:').'<ul>';
 9093:             if ($changes{'togglecats'}) {
 9094:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 9095:             }
 9096:             if ($changes{'categorize'}) {
 9097:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 9098:             }
 9099:             if ($changes{'togglecatscomm'}) {
 9100:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 9101:             }
 9102:             if ($changes{'categorizecomm'}) {
 9103:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 9104:             }
 9105:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 9106:                 my $cathash;
 9107:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 9108:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 9109:                 } else {
 9110:                     $cathash = {};
 9111:                 } 
 9112:                 my (@cats,@trails,%allitems);
 9113:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 9114:                 if (keys(%deletions) > 0) {
 9115:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 9116:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 9117:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 9118:                     }
 9119:                     $resulttext .= '</ul></li>';
 9120:                 }
 9121:                 if (keys(%reorderings) > 0) {
 9122:                     my %sort_by_trail;
 9123:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 9124:                     foreach my $key (keys(%reorderings)) {
 9125:                         if ($allitems{$key} ne '') {
 9126:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 9127:                         }
 9128:                     }
 9129:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 9130:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 9131:                     }
 9132:                     $resulttext .= '</ul></li>';
 9133:                 }
 9134:                 if (keys(%adds) > 0) {
 9135:                     my %sort_by_trail;
 9136:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 9137:                     foreach my $key (keys(%adds)) {
 9138:                         if ($allitems{$key} ne '') {
 9139:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 9140:                         }
 9141:                     }
 9142:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 9143:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 9144:                     }
 9145:                     $resulttext .= '</ul></li>';
 9146:                 }
 9147:             }
 9148:             $resulttext .= '</ul>';
 9149:         } else {
 9150:             $resulttext = '<span class="LC_error">'.
 9151:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 9152:         }
 9153:     } else {
 9154:         $resulttext = &mt('No changes made to course and community categories');
 9155:     }
 9156:     return $resulttext;
 9157: }
 9158: 
 9159: sub modify_serverstatuses {
 9160:     my ($dom,%domconfig) = @_;
 9161:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 9162:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 9163:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 9164:     }
 9165:     my @pages = &serverstatus_pages();
 9166:     foreach my $type (@pages) {
 9167:         $newserverstatus{$type}{'namedusers'} = '';
 9168:         $newserverstatus{$type}{'machines'} = '';
 9169:         if (defined($env{'form.'.$type.'_namedusers'})) {
 9170:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 9171:             my @okusers;
 9172:             foreach my $user (@users) {
 9173:                 my ($uname,$udom) = split(/:/,$user);
 9174:                 if (($udom =~ /^$match_domain$/) &&   
 9175:                     (&Apache::lonnet::domain($udom)) &&
 9176:                     ($uname =~ /^$match_username$/)) {
 9177:                     if (!grep(/^\Q$user\E/,@okusers)) {
 9178:                         push(@okusers,$user);
 9179:                     }
 9180:                 }
 9181:             }
 9182:             if (@okusers > 0) {
 9183:                  @okusers = sort(@okusers);
 9184:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 9185:             }
 9186:         }
 9187:         if (defined($env{'form.'.$type.'_machines'})) {
 9188:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 9189:             my @okmachines;
 9190:             foreach my $ip (@machines) {
 9191:                 my @parts = split(/\./,$ip);
 9192:                 next if (@parts < 4);
 9193:                 my $badip = 0;
 9194:                 for (my $i=0; $i<4; $i++) {
 9195:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 9196:                         $badip = 1;
 9197:                         last;
 9198:                     }
 9199:                 }
 9200:                 if (!$badip) {
 9201:                     push(@okmachines,$ip);     
 9202:                 }
 9203:             }
 9204:             @okmachines = sort(@okmachines);
 9205:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 9206:         }
 9207:     }
 9208:     my %serverstatushash =  (
 9209:                                 serverstatuses => \%newserverstatus,
 9210:                             );
 9211:     foreach my $type (@pages) {
 9212:         foreach my $setting ('namedusers','machines') {
 9213:             my (@current,@new);
 9214:             if (ref($currserverstatus{$type}) eq 'HASH') {
 9215:                 if ($currserverstatus{$type}{$setting} ne '') { 
 9216:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 9217:                 }
 9218:             }
 9219:             if ($newserverstatus{$type}{$setting} ne '') {
 9220:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 9221:             }
 9222:             if (@current > 0) {
 9223:                 if (@new > 0) {
 9224:                     foreach my $item (@current) {
 9225:                         if (!grep(/^\Q$item\E$/,@new)) {
 9226:                             $changes{$type}{$setting} = 1;
 9227:                             last;
 9228:                         }
 9229:                     }
 9230:                     foreach my $item (@new) {
 9231:                         if (!grep(/^\Q$item\E$/,@current)) {
 9232:                             $changes{$type}{$setting} = 1;
 9233:                             last;
 9234:                         }
 9235:                     }
 9236:                 } else {
 9237:                     $changes{$type}{$setting} = 1;
 9238:                 }
 9239:             } elsif (@new > 0) {
 9240:                 $changes{$type}{$setting} = 1;
 9241:             }
 9242:         }
 9243:     }
 9244:     if (keys(%changes) > 0) {
 9245:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 9246:         my $putresult = &Apache::lonnet::put_dom('configuration',
 9247:                                                  \%serverstatushash,$dom);
 9248:         if ($putresult eq 'ok') {
 9249:             $resulttext .= &mt('Changes made:').'<ul>';
 9250:             foreach my $type (@pages) {
 9251:                 if (ref($changes{$type}) eq 'HASH') {
 9252:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 9253:                     if ($changes{$type}{'namedusers'}) {
 9254:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 9255:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 9256:                         } else {
 9257:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 9258:                         }
 9259:                     }
 9260:                     if ($changes{$type}{'machines'}) {
 9261:                         if ($newserverstatus{$type}{'machines'} eq '') {
 9262:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 9263:                         } else {
 9264:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 9265:                         }
 9266: 
 9267:                     }
 9268:                     $resulttext .= '</ul></li>';
 9269:                 }
 9270:             }
 9271:             $resulttext .= '</ul>';
 9272:         } else {
 9273:             $resulttext = '<span class="LC_error">'.
 9274:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 9275: 
 9276:         }
 9277:     } else {
 9278:         $resulttext = &mt('No changes made to access to server status pages');
 9279:     }
 9280:     return $resulttext;
 9281: }
 9282: 
 9283: sub modify_helpsettings {
 9284:     my ($r,$dom,$confname,%domconfig) = @_;
 9285:     my ($resulttext,$errors,%changes,%helphash);
 9286:     my %defaultchecked = ('submitbugs' => 'on');
 9287:     my @offon = ('off','on');
 9288:     my @toggles = ('submitbugs');
 9289:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 9290:         foreach my $item (@toggles) {
 9291:             if ($defaultchecked{$item} eq 'on') { 
 9292:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 9293:                     if ($env{'form.'.$item} eq '0') {
 9294:                         $changes{$item} = 1;
 9295:                     }
 9296:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 9297:                     $changes{$item} = 1;
 9298:                 }
 9299:             } elsif ($defaultchecked{$item} eq 'off') {
 9300:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 9301:                     if ($env{'form.'.$item} eq '1') {
 9302:                         $changes{$item} = 1;
 9303:                     }
 9304:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 9305:                     $changes{$item} = 1;
 9306:                 }
 9307:             }
 9308:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
 9309:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
 9310:             }
 9311:         }
 9312:     }
 9313:     my $putresult;
 9314:     if (keys(%changes) > 0) {
 9315:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
 9316:         if ($putresult eq 'ok') {
 9317:             $resulttext = &mt('Changes made:').'<ul>';
 9318:             foreach my $item (sort(keys(%changes))) {
 9319:                 if ($item eq 'submitbugs') {
 9320:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
 9321:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 9322:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
 9323:                 }
 9324:             }
 9325:             $resulttext .= '</ul>';
 9326:         } else {
 9327:             $resulttext = &mt('No changes made to help settings');
 9328:             $errors .= '<li><span class="LC_error">'.
 9329:                        &mt('An error occurred storing the settings: [_1]',
 9330:                            $putresult).'</span></li>';
 9331:         }
 9332:     }
 9333:     if ($errors) {
 9334:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 9335:                        $errors.'</ul>';
 9336:     }
 9337:     return $resulttext;
 9338: }
 9339: 
 9340: sub modify_coursedefaults {
 9341:     my ($dom,$lastactref,%domconfig) = @_;
 9342:     my ($resulttext,$errors,%changes,%defaultshash);
 9343:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 9344:     my @toggles = ('canuse_pdfforms');
 9345:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
 9346:                    'uploadquota_community','uploadquota_textbook');
 9347:     my @types = ('official','unofficial','community','textbook');
 9348:     my %staticdefaults = (
 9349:                            anonsurvey_threshold => 10,
 9350:                            uploadquota          => 500,
 9351:                          );
 9352: 
 9353:     $defaultshash{'coursedefaults'} = {};
 9354: 
 9355:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
 9356:         if ($domconfig{'coursedefaults'} eq '') {
 9357:             $domconfig{'coursedefaults'} = {};
 9358:         }
 9359:     }
 9360: 
 9361:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 9362:         foreach my $item (@toggles) {
 9363:             if ($defaultchecked{$item} eq 'on') {
 9364:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 9365:                     ($env{'form.'.$item} eq '0')) {
 9366:                     $changes{$item} = 1;
 9367:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 9368:                     $changes{$item} = 1;
 9369:                 }
 9370:             } elsif ($defaultchecked{$item} eq 'off') {
 9371:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 9372:                     ($env{'form.'.$item} eq '1')) {
 9373:                     $changes{$item} = 1;
 9374:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 9375:                     $changes{$item} = 1;
 9376:                 }
 9377:             }
 9378:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
 9379:         }
 9380:         foreach my $item (@numbers) {
 9381:             my ($currdef,$newdef);
 9382:             $newdef = $env{'form.'.$item};
 9383:             if ($item eq 'anonsurvey_threshold') {
 9384:                 $currdef = $domconfig{'coursedefaults'}{$item};
 9385:                 $newdef =~ s/\D//g;
 9386:                 if ($newdef eq '' || $newdef < 1) {
 9387:                     $newdef = 1;
 9388:                 }
 9389:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
 9390:             } else {
 9391:                 my ($type) = ($item =~ /^\Quploadquota_\E(\w+)$/);
 9392:                 if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 9393:                     $currdef = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
 9394:                 }
 9395:                 $newdef =~ s/[^\w.\-]//g;
 9396:                 $defaultshash{'coursedefaults'}{'uploadquota'}{$type} = $newdef;
 9397:             }
 9398:             if ($currdef ne $newdef) {
 9399:                 my $staticdef;
 9400:                 if ($item eq 'anonsurvey_threshold') {
 9401:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
 9402:                         $changes{$item} = 1;
 9403:                     }
 9404:                 } else {
 9405:                     unless (($currdef eq '') && ($newdef == $staticdefaults{'uploadquota'})) {
 9406:                         $changes{'uploadquota'} = 1;
 9407:                     }
 9408:                 }
 9409:             }
 9410:         }
 9411: 
 9412:         my $officialcreds = $env{'form.official_credits'};
 9413:         $officialcreds =~ s/[^\d.]+//g;
 9414:         my $unofficialcreds = $env{'form.unofficial_credits'};
 9415:         $unofficialcreds =~ s/[^\d.]+//g;
 9416:         my $textbookcreds = $env{'form.textbook_credits'};
 9417:         $textbookcreds =~ s/[^\d.]+//g;
 9418:         if (ref($domconfig{'coursedefaults'}{'coursecredits'} ne 'HASH') &&
 9419:                 ($env{'form.coursecredits'} eq '1')) {
 9420:                 $changes{'coursecredits'} = 1;
 9421:         } else {
 9422:             if (($domconfig{'coursedefaults'}{'coursecredits'}{'official'} ne $officialcreds)  ||
 9423:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'unofficial'} ne $unofficialcreds) ||
 9424:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'textbook'} ne $textbookcreds)) {
 9425:                 $changes{'coursecredits'} = 1;
 9426:             }
 9427:         }
 9428:         $defaultshash{'coursedefaults'}{'coursecredits'} = {
 9429:             official   => $officialcreds,
 9430:             unofficial => $unofficialcreds,
 9431:             textbook   => $textbookcreds,
 9432:         }
 9433:     }
 9434:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 9435:                                              $dom);
 9436:     if ($putresult eq 'ok') {
 9437:         if (keys(%changes) > 0) {
 9438:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9439:             if (($changes{'canuse_pdfforms'}) || ($changes{'coursecredits'}) || 
 9440:                 ($changes{'uploadquota'})) { 
 9441:                 if ($changes{'canuse_pdfforms'}) {
 9442:                     $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
 9443:                 }
 9444:                 if ($changes{'coursecredits'}) {
 9445:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 9446:                         $domdefaults{'officialcredits'} =
 9447:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'official'};
 9448:                         $domdefaults{'unofficialcredits'} =
 9449:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'};
 9450:                         $domdefaults{'textbookcredits'} =
 9451:                             $domdefaults{'coursedefaults'}{'coursecredits'}{'textbook'};
 9452:                     }
 9453:                 }
 9454:                 if ($changes{'uploadquota'}) {
 9455:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 9456:                         foreach my $type (@types) {
 9457:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
 9458:                         }
 9459:                     }
 9460:                 }
 9461:                 my $cachetime = 24*60*60;
 9462:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9463:                 if (ref($lastactref) eq 'HASH') {
 9464:                     $lastactref->{'domdefaults'} = 1;
 9465:                 }
 9466:             }
 9467:             $resulttext = &mt('Changes made:').'<ul>';
 9468:             foreach my $item (sort(keys(%changes))) {
 9469:                 if ($item eq 'canuse_pdfforms') {
 9470:                     if ($env{'form.'.$item} eq '1') {
 9471:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
 9472:                     } else {
 9473:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
 9474:                     }
 9475:                 } elsif ($item eq 'anonsurvey_threshold') {
 9476:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
 9477:                 } elsif ($item eq 'uploadquota') {
 9478:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 9479:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
 9480:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
 9481:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
 9482:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
 9483: 
 9484:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
 9485:                                        '</ul>'.
 9486:                                        '</li>';
 9487:                     } else {
 9488:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
 9489:                     }
 9490:                 } elsif ($item eq 'coursecredits') {
 9491:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 9492:                         if (($domdefaults{'officialcredits'} eq '') &&
 9493:                             ($domdefaults{'unofficialcredits'} eq '') &&
 9494:                             ($domdefaults{'textbookcredits'} eq '')) {
 9495:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
 9496:                         } else {
 9497:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
 9498:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
 9499:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
 9500:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
 9501:                                            '</ul>'.
 9502:                                            '</li>';
 9503:                         }
 9504:                     } else {
 9505:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
 9506:                     }
 9507:                 }
 9508:             }
 9509:             $resulttext .= '</ul>';
 9510:         } else {
 9511:             $resulttext = &mt('No changes made to course defaults');
 9512:         }
 9513:     } else {
 9514:         $resulttext = '<span class="LC_error">'.
 9515:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9516:     }
 9517:     return $resulttext;
 9518: }
 9519: 
 9520: sub modify_selfenrollment {
 9521:     my ($dom,$lastactref,%domconfig) = @_;
 9522:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
 9523:     my @types = ('official','unofficial','community','textbook');
 9524:     my %titles = &tool_titles();
 9525:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 9526:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 9527:     $ordered{'default'} = ['types','registered','approval','limit'];
 9528: 
 9529:     my (%roles,%shown,%toplevel);
 9530:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
 9531: 
 9532:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
 9533:         if ($domconfig{'selfenrollment'} eq '') {
 9534:             $domconfig{'selfenrollment'} = {};
 9535:         }
 9536:     }
 9537:     %toplevel = (
 9538:                   admin      => 'Configuration Rights',
 9539:                   default    => 'Default settings',
 9540:                   validation => 'Validation of self-enrollment requests',
 9541:                 );
 9542:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 9543: 
 9544:     if (ref($ordered{'admin'}) eq 'ARRAY') {
 9545:         foreach my $item (@{$ordered{'admin'}}) {
 9546:             foreach my $type (@types) {
 9547:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
 9548:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
 9549:                 } else {
 9550:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
 9551:                 }
 9552:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
 9553:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
 9554:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
 9555:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
 9556:                             push(@{$changes{'admin'}{$type}},$item);
 9557:                         }
 9558:                     } else {
 9559:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
 9560:                             push(@{$changes{'admin'}{$type}},$item);
 9561:                         }
 9562:                     }
 9563:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
 9564:                     push(@{$changes{'admin'}{$type}},$item);
 9565:                 }
 9566:             }
 9567:         }
 9568:     }
 9569: 
 9570:     foreach my $item (@{$ordered{'default'}}) {
 9571:         foreach my $type (@types) {
 9572:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
 9573:             if ($item eq 'types') {
 9574:                 unless (($value eq 'all') || ($value eq 'dom')) {
 9575:                     $value = '';
 9576:                 }
 9577:             } elsif ($item eq 'registered') {
 9578:                 unless ($value eq '1') {
 9579:                     $value = 0;
 9580:                 }
 9581:             } elsif ($item eq 'approval') {
 9582:                 unless ($value =~ /^[012]$/) {
 9583:                     $value = 0;
 9584:                 }
 9585:             } else {
 9586:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
 9587:                     $value = 'none';
 9588:                 }
 9589:             }
 9590:             $selfenrollhash{'default'}{$type}{$item} = $value;
 9591:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
 9592:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
 9593:                     if ($selfenrollhash{'default'}{$type}{$item} ne
 9594:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
 9595:                          push(@{$changes{'default'}{$type}},$item);
 9596:                     }
 9597:                 } else {
 9598:                     push(@{$changes{'default'}{$type}},$item);
 9599:                 }
 9600:             } else {
 9601:                 push(@{$changes{'default'}{$type}},$item);
 9602:             }
 9603:             if ($item eq 'limit') {
 9604:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
 9605:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
 9606:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
 9607:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
 9608:                     }
 9609:                 } else {
 9610:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
 9611:                 }
 9612:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
 9613:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
 9614:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
 9615:                          push(@{$changes{'default'}{$type}},'cap');
 9616:                     }
 9617:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
 9618:                     push(@{$changes{'default'}{$type}},'cap');
 9619:                 }
 9620:             }
 9621:         }
 9622:     }
 9623: 
 9624:     foreach my $item (@{$itemsref}) {
 9625:         if ($item eq 'fields') {
 9626:             my @changed;
 9627:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
 9628:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
 9629:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
 9630:             }
 9631:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
 9632:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
 9633:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
 9634:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
 9635:                 } else {
 9636:                     @changed = @{$selfenrollhash{'validation'}{$item}};
 9637:                 }
 9638:             } else {
 9639:                 @changed = @{$selfenrollhash{'validation'}{$item}};
 9640:             }
 9641:             if (@changed) {
 9642:                 if ($selfenrollhash{'validation'}{$item}) { 
 9643:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
 9644:                 } else {
 9645:                     $changes{'validation'}{$item} = &mt('None');
 9646:                 }
 9647:             }
 9648:         } else {
 9649:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
 9650:             if ($item eq 'markup') {
 9651:                if ($env{'form.selfenroll_validation_'.$item}) {
 9652:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 9653:                }
 9654:             }
 9655:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
 9656:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
 9657:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
 9658:                 }
 9659:             }
 9660:         }
 9661:     }
 9662: 
 9663:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
 9664:                                              $dom);
 9665:     if ($putresult eq 'ok') {
 9666:         if (keys(%changes) > 0) {
 9667:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9668:             $resulttext = &mt('Changes made:').'<ul>';
 9669:             foreach my $key ('admin','default','validation') {
 9670:                 if (ref($changes{$key}) eq 'HASH') {
 9671:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
 9672:                     if ($key eq 'validation') {
 9673:                         foreach my $item (@{$itemsref}) {
 9674:                             if (exists($changes{$key}{$item})) {
 9675:                                 if ($item eq 'markup') {
 9676:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
 9677:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
 9678:                                 } else {  
 9679:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
 9680:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
 9681:                                 }
 9682:                             }
 9683:                         }
 9684:                     } else {
 9685:                         foreach my $type (@types) {
 9686:                             if ($type eq 'community') {
 9687:                                 $roles{'1'} = &mt('Community personnel');
 9688:                             } else {
 9689:                                 $roles{'1'} = &mt('Course personnel');
 9690:                             }
 9691:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
 9692:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
 9693:                                     if ($key eq 'admin') {
 9694:                                         my @mgrdc = ();
 9695:                                         if (ref($ordered{$key}) eq 'ARRAY') {
 9696:                                             foreach my $item (@{$ordered{'admin'}}) {
 9697:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
 9698:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
 9699:                                                         push(@mgrdc,$item);
 9700:                                                     }
 9701:                                                 }
 9702:                                             }
 9703:                                             if (@mgrdc) {
 9704:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
 9705:                                             } else {
 9706:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
 9707:                                             }
 9708:                                         }
 9709:                                     } else {
 9710:                                         if (ref($ordered{$key}) eq 'ARRAY') {
 9711:                                             foreach my $item (@{$ordered{$key}}) {
 9712:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
 9713:                                                     $domdefaults{$type.'selfenroll'.$item} =
 9714:                                                         $selfenrollhash{$key}{$type}{$item};
 9715:                                                 }
 9716:                                             }
 9717:                                         }
 9718:                                     }
 9719:                                 }
 9720:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
 9721:                                 foreach my $item (@{$ordered{$key}}) {
 9722:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
 9723:                                         $resulttext .= '<li>';
 9724:                                         if ($key eq 'admin') {
 9725:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
 9726:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
 9727:                                         } else {
 9728:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
 9729:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
 9730:                                         }
 9731:                                         $resulttext .= '</li>';
 9732:                                     }
 9733:                                 }
 9734:                                 $resulttext .= '</ul></li>';
 9735:                             }
 9736:                         }
 9737:                         $resulttext .= '</ul></li>'; 
 9738:                     }
 9739:                 }
 9740:                 if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
 9741:                     my $cachetime = 24*60*60;
 9742:                     &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9743:                     if (ref($lastactref) eq 'HASH') {
 9744:                         $lastactref->{'domdefaults'} = 1;
 9745:                     }
 9746:                 }
 9747:             }
 9748:             $resulttext .= '</ul>';
 9749:         } else {
 9750:             $resulttext = &mt('No changes made to self-enrollment settings');
 9751:         }
 9752:     } else {
 9753:         $resulttext = '<span class="LC_error">'.
 9754:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9755:     }
 9756:     return $resulttext;
 9757: }
 9758: 
 9759: sub modify_usersessions {
 9760:     my ($dom,$lastactref,%domconfig) = @_;
 9761:     my @hostingtypes = ('version','excludedomain','includedomain');
 9762:     my @offloadtypes = ('primary','default');
 9763:     my %types = (
 9764:                   remote => \@hostingtypes,
 9765:                   hosted => \@hostingtypes,
 9766:                   spares => \@offloadtypes,
 9767:                 );
 9768:     my @prefixes = ('remote','hosted','spares');
 9769:     my @lcversions = &Apache::lonnet::all_loncaparevs();
 9770:     my (%by_ip,%by_location,@intdoms);
 9771:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 9772:     my @locations = sort(keys(%by_location));
 9773:     my (%defaultshash,%changes);
 9774:     foreach my $prefix (@prefixes) {
 9775:         $defaultshash{'usersessions'}{$prefix} = {};
 9776:     }
 9777:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9778:     my $resulttext;
 9779:     my %iphost = &Apache::lonnet::get_iphost();
 9780:     foreach my $prefix (@prefixes) {
 9781:         next if ($prefix eq 'spares');
 9782:         foreach my $type (@{$types{$prefix}}) {
 9783:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
 9784:             if ($type eq 'version') {
 9785:                 my $value = $env{'form.'.$prefix.'_'.$type};
 9786:                 my $okvalue;
 9787:                 if ($value ne '') {
 9788:                     if (grep(/^\Q$value\E$/,@lcversions)) {
 9789:                         $okvalue = $value;
 9790:                     }
 9791:                 }
 9792:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 9793:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 9794:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
 9795:                             if ($inuse == 0) {
 9796:                                 $changes{$prefix}{$type} = 1;
 9797:                             } else {
 9798:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
 9799:                                     $changes{$prefix}{$type} = 1;
 9800:                                 }
 9801:                                 if ($okvalue ne '') {
 9802:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 9803:                                 } 
 9804:                             }
 9805:                         } else {
 9806:                             if (($inuse == 1) && ($okvalue ne '')) {
 9807:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 9808:                                 $changes{$prefix}{$type} = 1;
 9809:                             }
 9810:                         }
 9811:                     } else {
 9812:                         if (($inuse == 1) && ($okvalue ne '')) {
 9813:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 9814:                             $changes{$prefix}{$type} = 1;
 9815:                         }
 9816:                     }
 9817:                 } else {
 9818:                     if (($inuse == 1) && ($okvalue ne '')) {
 9819:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 9820:                         $changes{$prefix}{$type} = 1;
 9821:                     }
 9822:                 }
 9823:             } else {
 9824:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
 9825:                 my @okvals;
 9826:                 foreach my $val (@vals) {
 9827:                     if ($val =~ /:/) {
 9828:                         my @items = split(/:/,$val);
 9829:                         foreach my $item (@items) {
 9830:                             if (ref($by_location{$item}) eq 'ARRAY') {
 9831:                                 push(@okvals,$item);
 9832:                             }
 9833:                         }
 9834:                     } else {
 9835:                         if (ref($by_location{$val}) eq 'ARRAY') {
 9836:                             push(@okvals,$val);
 9837:                         }
 9838:                     }
 9839:                 }
 9840:                 @okvals = sort(@okvals);
 9841:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 9842:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 9843:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 9844:                             if ($inuse == 0) {
 9845:                                 $changes{$prefix}{$type} = 1; 
 9846:                             } else {
 9847:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 9848:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
 9849:                                 if (@changed > 0) {
 9850:                                     $changes{$prefix}{$type} = 1;
 9851:                                 }
 9852:                             }
 9853:                         } else {
 9854:                             if ($inuse == 1) {
 9855:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 9856:                                 $changes{$prefix}{$type} = 1;
 9857:                             }
 9858:                         } 
 9859:                     } else {
 9860:                         if ($inuse == 1) {
 9861:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 9862:                             $changes{$prefix}{$type} = 1;
 9863:                         }
 9864:                     }
 9865:                 } else {
 9866:                     if ($inuse == 1) {
 9867:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 9868:                         $changes{$prefix}{$type} = 1;
 9869:                     }
 9870:                 }
 9871:             }
 9872:         }
 9873:     }
 9874: 
 9875:     my @alldoms = &Apache::lonnet::all_domains();
 9876:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 9877:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
 9878:     my $savespares;
 9879: 
 9880:     foreach my $lonhost (sort(keys(%servers))) {
 9881:         my $serverhomeID =
 9882:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
 9883:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
 9884:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
 9885:         my %spareschg;
 9886:         foreach my $type (@{$types{'spares'}}) {
 9887:             my @okspares;
 9888:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
 9889:             foreach my $server (@checked) {
 9890:                 if (&Apache::lonnet::hostname($server) ne '') {
 9891:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
 9892:                         unless (grep(/^\Q$server\E$/,@okspares)) {
 9893:                             push(@okspares,$server);
 9894:                         }
 9895:                     }
 9896:                 }
 9897:             }
 9898:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
 9899:             my $newspare;
 9900:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
 9901:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
 9902:                     $newspare = $new;
 9903:                 }
 9904:             }
 9905:             my @spares;
 9906:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
 9907:                 @spares = sort(@okspares,$newspare);
 9908:             } else {
 9909:                 @spares = sort(@okspares);
 9910:             }
 9911:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
 9912:             if (ref($spareid{$lonhost}) eq 'HASH') {
 9913:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
 9914:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
 9915:                     if (@diffs > 0) {
 9916:                         $spareschg{$type} = 1;
 9917:                     }
 9918:                 }
 9919:             }
 9920:         }
 9921:         if (keys(%spareschg) > 0) {
 9922:             $changes{'spares'}{$lonhost} = \%spareschg;
 9923:         }
 9924:     }
 9925: 
 9926:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 9927:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 9928:             if (ref($changes{'spares'}) eq 'HASH') {
 9929:                 if (keys(%{$changes{'spares'}}) > 0) {
 9930:                     $savespares = 1;
 9931:                 }
 9932:             }
 9933:         } else {
 9934:             $savespares = 1;
 9935:         }
 9936:     }
 9937: 
 9938:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
 9939:     if ((keys(%changes) > 0) || ($savespares)) {
 9940:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 9941:                                                  $dom);
 9942:         if ($putresult eq 'ok') {
 9943:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 9944:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
 9945:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
 9946:                 }
 9947:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
 9948:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
 9949:                 }
 9950:             }
 9951:             my $cachetime = 24*60*60;
 9952:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9953:             if (ref($lastactref) eq 'HASH') {
 9954:                 $lastactref->{'domdefaults'} = 1;
 9955:             }
 9956:             if (keys(%changes) > 0) {
 9957:                 my %lt = &usersession_titles();
 9958:                 $resulttext = &mt('Changes made:').'<ul>';
 9959:                 foreach my $prefix (@prefixes) {
 9960:                     if (ref($changes{$prefix}) eq 'HASH') {
 9961:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
 9962:                         if ($prefix eq 'spares') {
 9963:                             if (ref($changes{$prefix}) eq 'HASH') {
 9964:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
 9965:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
 9966:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
 9967:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
 9968:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
 9969:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
 9970:                                         foreach my $type (@{$types{$prefix}}) {
 9971:                                             if ($changes{$prefix}{$lonhost}{$type}) {
 9972:                                                 my $offloadto = &mt('None');
 9973:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
 9974:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
 9975:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
 9976:                                                     }
 9977:                                                 }
 9978:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
 9979:                                             }
 9980:                                         }
 9981:                                     }
 9982:                                     $resulttext .= '</li>';
 9983:                                 }
 9984:                             }
 9985:                         } else {
 9986:                             foreach my $type (@{$types{$prefix}}) {
 9987:                                 if (defined($changes{$prefix}{$type})) {
 9988:                                     my $newvalue;
 9989:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 9990:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
 9991:                                             if ($type eq 'version') {
 9992:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
 9993:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 9994:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
 9995:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
 9996:                                                 }
 9997:                                             }
 9998:                                         }
 9999:                                     }
10000:                                     if ($newvalue eq '') {
10001:                                         if ($type eq 'version') {
10002:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
10003:                                         } else {
10004:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
10005:                                         }
10006:                                     } else {
10007:                                         if ($type eq 'version') {
10008:                                             $newvalue .= ' '.&mt('(or later)'); 
10009:                                         }
10010:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
10011:                                     }
10012:                                 }
10013:                             }
10014:                         }
10015:                         $resulttext .= '</ul>';
10016:                     }
10017:                 }
10018:                 $resulttext .= '</ul>';
10019:             } else {
10020:                 $resulttext = $nochgmsg;
10021:             }
10022:         } else {
10023:             $resulttext = '<span class="LC_error">'.
10024:                           &mt('An error occurred: [_1]',$putresult).'</span>';
10025:         }
10026:     } else {
10027:         $resulttext = $nochgmsg;
10028:     }
10029:     return $resulttext;
10030: }
10031: 
10032: sub modify_loadbalancing {
10033:     my ($dom,%domconfig) = @_;
10034:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
10035:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
10036:     my ($othertitle,$usertypes,$types) =
10037:         &Apache::loncommon::sorted_inst_types($dom);
10038:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
10039:     my @sparestypes = ('primary','default');
10040:     my %typetitles = &sparestype_titles();
10041:     my $resulttext;
10042:     my (%currbalancer,%currtargets,%currrules,%existing);
10043:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
10044:         %existing = %{$domconfig{'loadbalancing'}};
10045:     }
10046:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
10047:                               \%currtargets,\%currrules);
10048:     my ($saveloadbalancing,%defaultshash,%changes);
10049:     my ($alltypes,$othertypes,$titles) =
10050:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
10051:     my %ruletitles = &offloadtype_text();
10052:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
10053:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
10054:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
10055:         if ($balancer eq '') {
10056:             next;
10057:         }
10058:         if (!exists($servers{$balancer})) {
10059:             if (exists($currbalancer{$balancer})) {
10060:                 push(@{$changes{'delete'}},$balancer);
10061:             }
10062:             next;
10063:         }
10064:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
10065:             push(@{$changes{'delete'}},$balancer);
10066:             next;
10067:         }
10068:         if (!exists($currbalancer{$balancer})) {
10069:             push(@{$changes{'add'}},$balancer);
10070:         }
10071:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
10072:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
10073:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
10074:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
10075:             $saveloadbalancing = 1;
10076:         }
10077:         foreach my $sparetype (@sparestypes) {
10078:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
10079:             my @offloadto;
10080:             foreach my $target (@targets) {
10081:                 if (($servers{$target}) && ($target ne $balancer)) {
10082:                     if ($sparetype eq 'default') {
10083:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
10084:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
10085:                         }
10086:                     }
10087:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
10088:                         push(@offloadto,$target);
10089:                     }
10090:                 }
10091:                 $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
10092:             }
10093:         }
10094:         if (ref($currtargets{$balancer}) eq 'HASH') {
10095:             foreach my $sparetype (@sparestypes) {
10096:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
10097:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
10098:                     if (@targetdiffs > 0) {
10099:                         $changes{'curr'}{$balancer}{'targets'} = 1;
10100:                     }
10101:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10102:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10103:                         $changes{'curr'}{$balancer}{'targets'} = 1;
10104:                     }
10105:                 }
10106:             }
10107:         } else {
10108:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
10109:                 foreach my $sparetype (@sparestypes) {
10110:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10111:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10112:                             $changes{'curr'}{$balancer}{'targets'} = 1;
10113:                         }
10114:                     }
10115:                 }
10116:             }
10117:         }
10118:         my $ishomedom;
10119:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
10120:             $ishomedom = 1;
10121:         }
10122:         if (ref($alltypes) eq 'ARRAY') {
10123:             foreach my $type (@{$alltypes}) {
10124:                 my $rule;
10125:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
10126:                          (!$ishomedom)) {
10127:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
10128:                 }
10129:                 if ($rule eq 'specific') {
10130:                     $rule = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
10131:                 }
10132:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
10133:                 if (ref($currrules{$balancer}) eq 'HASH') {
10134:                     if ($rule ne $currrules{$balancer}{$type}) {
10135:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
10136:                     }
10137:                 } elsif ($rule ne '') {
10138:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
10139:                 }
10140:             }
10141:         }
10142:     }
10143:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
10144:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
10145:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
10146:             $defaultshash{'loadbalancing'} = {};
10147:         }
10148:         my $putresult = &Apache::lonnet::put_dom('configuration',
10149:                                                  \%defaultshash,$dom);
10150:         if ($putresult eq 'ok') {
10151:             if (keys(%changes) > 0) {
10152:                 if (ref($changes{'delete'}) eq 'ARRAY') {
10153:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
10154:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
10155:                         my $cachekey = &escape('loadbalancing').':'.&escape($dom);
10156:                         &Apache::lonnet::remote_devalidate_cache($balancer,[$cachekey]);
10157:                     }
10158:                 }
10159:                 if (ref($changes{'add'}) eq 'ARRAY') {
10160:                     foreach my $balancer (sort(@{$changes{'add'}})) {
10161:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
10162:                     }
10163:                 }
10164:                 if (ref($changes{'curr'}) eq 'HASH') {
10165:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
10166:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
10167:                             if ($changes{'curr'}{$balancer}{'targets'}) {
10168:                                 my %offloadstr;
10169:                                 foreach my $sparetype (@sparestypes) {
10170:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10171:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10172:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
10173:                                         }
10174:                                     }
10175:                                 }
10176:                                 if (keys(%offloadstr) == 0) {
10177:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
10178:                                 } else {
10179:                                     my $showoffload;
10180:                                     foreach my $sparetype (@sparestypes) {
10181:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
10182:                                         if (defined($offloadstr{$sparetype})) {
10183:                                             $showoffload .= $offloadstr{$sparetype};
10184:                                         } else {
10185:                                             $showoffload .= &mt('None');
10186:                                         }
10187:                                         $showoffload .= ('&nbsp;'x3);
10188:                                     }
10189:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
10190:                                 }
10191:                             }
10192:                         }
10193:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
10194:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
10195:                                 foreach my $type (@{$alltypes}) {
10196:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
10197:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
10198:                                         my $balancetext;
10199:                                         if ($rule eq '') {
10200:                                             $balancetext =  $ruletitles{'default'};
10201:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
10202:                                                  ($rule eq 'balancer') || ($rule eq 'offloadedto')) {
10203:                                             $balancetext =  $ruletitles{$rule};
10204:                                         } else {
10205:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
10206:                                         }
10207:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
10208:                                     }
10209:                                 }
10210:                             }
10211:                         }
10212:                         my $cachekey = &escape('loadbalancing').':'.&escape($dom);
10213:                         &Apache::lonnet::remote_devalidate_cache($balancer,[$cachekey]);
10214:                     }
10215:                 }
10216:                 if ($resulttext ne '') {
10217:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
10218:                 } else {
10219:                     $resulttext = $nochgmsg;
10220:                 }
10221:             } else {
10222:                 $resulttext = $nochgmsg;
10223:             }
10224:         } else {
10225:             $resulttext = '<span class="LC_error">'.
10226:                           &mt('An error occurred: [_1]',$putresult).'</span>';
10227:         }
10228:     } else {
10229:         $resulttext = $nochgmsg;
10230:     }
10231:     return $resulttext;
10232: }
10233: 
10234: sub recurse_check {
10235:     my ($chkcats,$categories,$depth,$name) = @_;
10236:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
10237:         my $chg = 0;
10238:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
10239:             my $category = $chkcats->[$depth]{$name}[$j];
10240:             my $item;
10241:             if ($category eq '') {
10242:                 $chg ++;
10243:             } else {
10244:                 my $deeper = $depth + 1;
10245:                 $item = &escape($category).':'.&escape($name).':'.$depth;
10246:                 if ($chg) {
10247:                     $categories->{$item} -= $chg;
10248:                 }
10249:                 &recurse_check($chkcats,$categories,$deeper,$category);
10250:                 $deeper --;
10251:             }
10252:         }
10253:     }
10254:     return;
10255: }
10256: 
10257: sub recurse_cat_deletes {
10258:     my ($item,$coursecategories,$deletions) = @_;
10259:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
10260:     my $subdepth = $depth + 1;
10261:     if (ref($coursecategories) eq 'HASH') {
10262:         foreach my $subitem (keys(%{$coursecategories})) {
10263:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
10264:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
10265:                 delete($coursecategories->{$subitem});
10266:                 $deletions->{$subitem} = 1;
10267:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
10268:             }
10269:         }
10270:     }
10271:     return;
10272: }
10273: 
10274: sub get_active_dcs {
10275:     my ($dom) = @_;
10276:     my $now = time;
10277:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
10278:     my %domcoords;
10279:     my $numdcs = 0;
10280:     foreach my $server (keys(%dompersonnel)) {
10281:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
10282:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
10283:             $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
10284:         }
10285:     }
10286:     return %domcoords;
10287: }
10288: 
10289: sub active_dc_picker {
10290:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
10291:     my %domcoords = &get_active_dcs($dom);
10292:     my @domcoord = keys(%domcoords);
10293:     if (keys(%currhash)) {
10294:         foreach my $dc (keys(%currhash)) {
10295:             unless (exists($domcoords{$dc})) {
10296:                 push(@domcoord,$dc);
10297:             }
10298:         }
10299:     }
10300:     @domcoord = sort(@domcoord);
10301:     my $numdcs = scalar(@domcoord);
10302:     my $rows = 0;
10303:     my $table;
10304:     if ($numdcs > 1) {
10305:         $table = '<table>';
10306:         for (my $i=0; $i<@domcoord; $i++) {
10307:             my $rem = $i%($numinrow);
10308:             if ($rem == 0) {
10309:                 if ($i > 0) {
10310:                     $table .= '</tr>';
10311:                 }
10312:                 $table .= '<tr>';
10313:                 $rows ++;
10314:             }
10315:             my $check = '';
10316:             if ($inputtype eq 'radio') {
10317:                 if (keys(%currhash) == 0) {
10318:                     if (!$i) {
10319:                         $check = ' checked="checked"';
10320:                     }
10321:                 } elsif (exists($currhash{$domcoord[$i]})) {
10322:                     $check = ' checked="checked"';
10323:                 }
10324:             } else {
10325:                 if (exists($currhash{$domcoord[$i]})) {
10326:                     $check = ' checked="checked"';
10327:                 }
10328:             }
10329:             if ($i == @domcoord - 1) {
10330:                 my $colsleft = $numinrow - $rem;
10331:                 if ($colsleft > 1) {
10332:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
10333:                 } else {
10334:                     $table .= '<td class="LC_left_item">';
10335:                 }
10336:             } else {
10337:                 $table .= '<td class="LC_left_item">';
10338:             }
10339:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
10340:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
10341:             $table .= '<span class="LC_nobreak"><label>'.
10342:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
10343:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
10344:             if ($user ne $dcname.':'.$dcdom) {
10345:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
10346:             }
10347:             $table .= '</label></span></td>';
10348:         }
10349:         $table .= '</tr></table>';
10350:     } elsif ($numdcs == 1) {
10351:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
10352:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
10353:         if ($inputtype eq 'radio') {
10354:             $table .= '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
10355:             if ($user ne $dcname.':'.$dcdom) {
10356:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
10357:             }
10358:         } else {
10359:             my $check;
10360:             if (exists($currhash{$domcoord[0]})) {
10361:                 $check = ' checked="checked"';
10362:             }
10363:             $table .= '<span class="LC_nobreak"><label>'.
10364:                       '<input type="checkbox" name="'.$name.'" '.
10365:                       'value="'.$domcoord[0].'"'.$check.' />'.$user;
10366:             if ($user ne $dcname.':'.$dcdom) {
10367:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
10368:             }
10369:             $table .= '</label></span>';
10370:             $rows ++;
10371:         }
10372:     }
10373:     return ($numdcs,$table,$rows);
10374: }
10375: 
10376: sub usersession_titles {
10377:     return &Apache::lonlocal::texthash(
10378:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
10379:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
10380:                spares => 'Servers offloaded to, when busy',
10381:                version => 'LON-CAPA version requirement',
10382:                excludedomain => 'Allow all, but exclude specific domains',
10383:                includedomain => 'Deny all, but include specific domains',
10384:                primary => 'Primary (checked first)',
10385:                default => 'Default',
10386:            );
10387: }
10388: 
10389: sub id_for_thisdom {
10390:     my (%servers) = @_;
10391:     my %altids;
10392:     foreach my $server (keys(%servers)) {
10393:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
10394:         if ($serverhome ne $server) {
10395:             $altids{$serverhome} = $server;
10396:         }
10397:     }
10398:     return %altids;
10399: }
10400: 
10401: sub count_servers {
10402:     my ($currbalancer,%servers) = @_;
10403:     my (@spares,$numspares);
10404:     foreach my $lonhost (sort(keys(%servers))) {
10405:         next if ($currbalancer eq $lonhost);
10406:         push(@spares,$lonhost);
10407:     }
10408:     if ($currbalancer) {
10409:         $numspares = scalar(@spares);
10410:     } else {
10411:         $numspares = scalar(@spares) - 1;
10412:     }
10413:     return ($numspares,@spares);
10414: }
10415: 
10416: sub lonbalance_targets_js {
10417:     my ($dom,$types,$servers,$settings) = @_;
10418:     my $select = &mt('Select');
10419:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
10420:     if (ref($servers) eq 'HASH') {
10421:         $alltargets = join("','",sort(keys(%{$servers})));
10422:         my @homedoms;
10423:         foreach my $server (sort(keys(%{$servers}))) {
10424:             if (&Apache::lonnet::host_domain($server) eq $dom) {
10425:                 push(@homedoms,'1');
10426:             } else {
10427:                 push(@homedoms,'0');
10428:             }
10429:         }
10430:         $allishome = join("','",@homedoms);
10431:     }
10432:     if (ref($types) eq 'ARRAY') {
10433:         if (@{$types} > 0) {
10434:             @alltypes = @{$types};
10435:         }
10436:     }
10437:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
10438:     $allinsttypes = join("','",@alltypes);
10439:     my (%currbalancer,%currtargets,%currrules,%existing);
10440:     if (ref($settings) eq 'HASH') {
10441:         %existing = %{$settings};
10442:     }
10443:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
10444:                               \%currtargets,\%currrules);
10445:     my $balancers = join("','",sort(keys(%currbalancer)));
10446:     return <<"END";
10447: 
10448: <script type="text/javascript">
10449: // <![CDATA[
10450: 
10451: currBalancers = new Array('$balancers');
10452: 
10453: function toggleTargets(balnum) {
10454:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
10455:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
10456:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
10457:     var prevbalancer = prevhostitem.value;
10458:     var baltotal = document.getElementById('loadbalancing_total').value;
10459:     prevhostitem.value = balancer;
10460:     if (prevbalancer != '') {
10461:         var prevIdx = currBalancers.indexOf(prevbalancer);
10462:         if (prevIdx != -1) {
10463:             currBalancers.splice(prevIdx,1);
10464:         }
10465:     }
10466:     if (balancer == '') {
10467:         hideSpares(balnum);
10468:     } else {
10469:         var currIdx = currBalancers.indexOf(balancer);
10470:         if (currIdx == -1) {
10471:             currBalancers.push(balancer);
10472:         }
10473:         var homedoms = new Array('$allishome');
10474:         var ishomedom = homedoms[lonhostitem.selectedIndex];
10475:         showSpares(balancer,ishomedom,balnum);
10476:     }
10477:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
10478:     return;
10479: }
10480: 
10481: function showSpares(balancer,ishomedom,balnum) {
10482:     var alltargets = new Array('$alltargets');
10483:     var insttypes = new Array('$allinsttypes');
10484:     var offloadtypes = new Array('primary','default');
10485: 
10486:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
10487:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
10488:  
10489:     for (var i=0; i<offloadtypes.length; i++) {
10490:         var count = 0;
10491:         for (var j=0; j<alltargets.length; j++) {
10492:             if (alltargets[j] != balancer) {
10493:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
10494:                 item.value = alltargets[j];
10495:                 item.style.textAlign='left';
10496:                 item.style.textFace='normal';
10497:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
10498:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
10499:                     item.disabled = '';
10500:                 } else {
10501:                     item.disabled = 'disabled';
10502:                     item.checked = false;
10503:                 }
10504:                 count ++;
10505:             }
10506:         }
10507:     }
10508:     for (var k=0; k<insttypes.length; k++) {
10509:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
10510:             if (ishomedom == 1) {
10511:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
10512:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
10513:             } else {
10514:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
10515:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
10516:             }
10517:         } else {
10518:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
10519:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
10520:         }
10521:         if ((insttypes[k] != '_LC_external') && 
10522:             ((insttypes[k] != '_LC_internetdom') ||
10523:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
10524:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
10525:             item.options.length = 0;
10526:             item.options[0] = new Option("","",true,true);
10527:             var idx = 0;
10528:             for (var m=0; m<alltargets.length; m++) {
10529:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
10530:                     idx ++;
10531:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
10532:                 }
10533:             }
10534:         }
10535:     }
10536:     return;
10537: }
10538: 
10539: function hideSpares(balnum) {
10540:     var alltargets = new Array('$alltargets');
10541:     var insttypes = new Array('$allinsttypes');
10542:     var offloadtypes = new Array('primary','default');
10543: 
10544:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
10545:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
10546: 
10547:     var total = alltargets.length - 1;
10548:     for (var i=0; i<offloadtypes; i++) {
10549:         for (var j=0; j<total; j++) {
10550:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
10551:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
10552:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
10553:         }
10554:     }
10555:     for (var k=0; k<insttypes.length; k++) {
10556:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
10557:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
10558:         if (insttypes[k] != '_LC_external') {
10559:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
10560:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
10561:         }
10562:     }
10563:     return;
10564: }
10565: 
10566: function checkOffloads(item,balnum,type) {
10567:     var alltargets = new Array('$alltargets');
10568:     var offloadtypes = new Array('primary','default');
10569:     if (item.checked) {
10570:         var total = alltargets.length - 1;
10571:         var other;
10572:         if (type == offloadtypes[0]) {
10573:             other = offloadtypes[1];
10574:         } else {
10575:             other = offloadtypes[0];
10576:         }
10577:         for (var i=0; i<total; i++) {
10578:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
10579:             if (server == item.value) {
10580:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
10581:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
10582:                 }
10583:             }
10584:         }
10585:     }
10586:     return;
10587: }
10588: 
10589: function singleServerToggle(balnum,type) {
10590:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
10591:     if (offloadtoSelIdx == 0) {
10592:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
10593:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
10594: 
10595:     } else {
10596:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
10597:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
10598:     }
10599:     return;
10600: }
10601: 
10602: function balanceruleChange(formname,balnum,type) {
10603:     if (type == '_LC_external') {
10604:         return;
10605:     }
10606:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
10607:     for (var i=0; i<typesRules.length; i++) {
10608:         if (formname.elements[typesRules[i]].checked) {
10609:             if (formname.elements[typesRules[i]].value != 'specific') {
10610:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
10611:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
10612:             } else {
10613:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
10614:             }
10615:         }
10616:     }
10617:     return;
10618: }
10619: 
10620: function balancerDeleteChange(balnum) {
10621:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
10622:     var baltotal = document.getElementById('loadbalancing_total').value;
10623:     var addtarget;
10624:     var removetarget;
10625:     var action = 'delete';
10626:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
10627:         var lonhost = hostitem.value;
10628:         var currIdx = currBalancers.indexOf(lonhost);
10629:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
10630:             if (currIdx != -1) {
10631:                 currBalancers.splice(currIdx,1);
10632:             }
10633:             addtarget = lonhost;
10634:         } else {
10635:             if (currIdx == -1) {
10636:                 currBalancers.push(lonhost);
10637:             }
10638:             removetarget = lonhost;
10639:             action = 'undelete';
10640:         }
10641:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
10642:     }
10643:     return;
10644: }
10645: 
10646: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
10647:     if (baltotal > 1) {
10648:         var offloadtypes = new Array('primary','default');
10649:         var alltargets = new Array('$alltargets');
10650:         var insttypes = new Array('$allinsttypes');
10651:         for (var i=0; i<baltotal; i++) {
10652:             if (i != balnum) {
10653:                 for (var j=0; j<offloadtypes.length; j++) {
10654:                     var total = alltargets.length - 1;
10655:                     for (var k=0; k<total; k++) {
10656:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
10657:                         var server = serveritem.value;
10658:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
10659:                             if (server == addtarget) {
10660:                                 serveritem.disabled = '';
10661:                             }
10662:                         }
10663:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
10664:                             if (server == removetarget) {
10665:                                 serveritem.disabled = 'disabled';
10666:                                 serveritem.checked = false;
10667:                             }
10668:                         }
10669:                     }
10670:                 }
10671:                 for (var j=0; j<insttypes.length; j++) {
10672:                     if (insttypes[j] != '_LC_external') {
10673:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
10674:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
10675:                             var currSel = singleserver.selectedIndex;
10676:                             var currVal = singleserver.options[currSel].value;
10677:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
10678:                                 var numoptions = singleserver.options.length;
10679:                                 var needsnew = 1;
10680:                                 for (var k=0; k<numoptions; k++) {
10681:                                     if (singleserver.options[k] == addtarget) {
10682:                                         needsnew = 0;
10683:                                         break;
10684:                                     }
10685:                                 }
10686:                                 if (needsnew == 1) {
10687:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
10688:                                 }
10689:                             }
10690:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
10691:                                 singleserver.options.length = 0;
10692:                                 if ((currVal) && (currVal != removetarget)) {
10693:                                     singleserver.options[0] = new Option("","",false,false);
10694:                                 } else {
10695:                                     singleserver.options[0] = new Option("","",true,true);
10696:                                 }
10697:                                 var idx = 0;
10698:                                 for (var m=0; m<alltargets.length; m++) {
10699:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
10700:                                         idx ++;
10701:                                         if (currVal == alltargets[m]) {
10702:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
10703:                                         } else {
10704:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
10705:                                         }
10706:                                     }
10707:                                 }
10708:                             }
10709:                         }
10710:                     }
10711:                 }
10712:             }
10713:         }
10714:     }
10715:     return;
10716: }
10717: 
10718: // ]]>
10719: </script>
10720: 
10721: END
10722: }
10723: 
10724: sub new_spares_js {
10725:     my @sparestypes = ('primary','default');
10726:     my $types = join("','",@sparestypes);
10727:     my $select = &mt('Select');
10728:     return <<"END";
10729: 
10730: <script type="text/javascript">
10731: // <![CDATA[
10732: 
10733: function updateNewSpares(formname,lonhost) {
10734:     var types = new Array('$types');
10735:     var include = new Array();
10736:     var exclude = new Array();
10737:     for (var i=0; i<types.length; i++) {
10738:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
10739:         for (var j=0; j<spareboxes.length; j++) {
10740:             if (formname.elements[spareboxes[j]].checked) {
10741:                 exclude.push(formname.elements[spareboxes[j]].value);
10742:             } else {
10743:                 include.push(formname.elements[spareboxes[j]].value);
10744:             }
10745:         }
10746:     }
10747:     for (var i=0; i<types.length; i++) {
10748:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
10749:         var selIdx = newSpare.selectedIndex;
10750:         var currnew = newSpare.options[selIdx].value;
10751:         var okSpares = new Array();
10752:         for (var j=0; j<newSpare.options.length; j++) {
10753:             var possible = newSpare.options[j].value;
10754:             if (possible != '') {
10755:                 if (exclude.indexOf(possible) == -1) {
10756:                     okSpares.push(possible);
10757:                 } else {
10758:                     if (currnew == possible) {
10759:                         selIdx = 0;
10760:                     }
10761:                 }
10762:             }
10763:         }
10764:         for (var k=0; k<include.length; k++) {
10765:             if (okSpares.indexOf(include[k]) == -1) {
10766:                 okSpares.push(include[k]);
10767:             }
10768:         }
10769:         okSpares.sort();
10770:         newSpare.options.length = 0;
10771:         if (selIdx == 0) {
10772:             newSpare.options[0] = new Option("$select","",true,true);
10773:         } else {
10774:             newSpare.options[0] = new Option("$select","",false,false);
10775:         }
10776:         for (var m=0; m<okSpares.length; m++) {
10777:             var idx = m+1;
10778:             var selThis = 0;
10779:             if (selIdx != 0) {
10780:                 if (okSpares[m] == currnew) {
10781:                     selThis = 1;
10782:                 }
10783:             }
10784:             if (selThis == 1) {
10785:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
10786:             } else {
10787:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
10788:             }
10789:         }
10790:     }
10791:     return;
10792: }
10793: 
10794: function checkNewSpares(lonhost,type) {
10795:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
10796:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
10797:     if (chosen != '') { 
10798:         var othertype;
10799:         var othernewSpare;
10800:         if (type == 'primary') {
10801:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
10802:         }
10803:         if (type == 'default') {
10804:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
10805:         }
10806:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
10807:             othernewSpare.selectedIndex = 0;
10808:         }
10809:     }
10810:     return;
10811: }
10812: 
10813: // ]]>
10814: </script>
10815: 
10816: END
10817: 
10818: }
10819: 
10820: sub common_domprefs_js {
10821:     return <<"END";
10822: 
10823: <script type="text/javascript">
10824: // <![CDATA[
10825: 
10826: function getIndicesByName(formname,item) {
10827:     var group = new Array();
10828:     for (var i=0;i<formname.elements.length;i++) {
10829:         if (formname.elements[i].name == item) {
10830:             group.push(formname.elements[i].id);
10831:         }
10832:     }
10833:     return group;
10834: }
10835: 
10836: // ]]>
10837: </script>
10838: 
10839: END
10840: 
10841: }
10842: 
10843: sub recaptcha_js {
10844:     my %lt = &captcha_phrases();
10845:     return <<"END";
10846: 
10847: <script type="text/javascript">
10848: // <![CDATA[
10849: 
10850: function updateCaptcha(caller,context) {
10851:     var privitem;
10852:     var pubitem;
10853:     var privtext;
10854:     var pubtext;
10855:     if (document.getElementById(context+'_recaptchapub')) {
10856:         pubitem = document.getElementById(context+'_recaptchapub');
10857:     } else {
10858:         return;
10859:     }
10860:     if (document.getElementById(context+'_recaptchapriv')) {
10861:         privitem = document.getElementById(context+'_recaptchapriv');
10862:     } else {
10863:         return;
10864:     }
10865:     if (document.getElementById(context+'_recaptchapubtxt')) {
10866:         pubtext = document.getElementById(context+'_recaptchapubtxt');
10867:     } else {
10868:         return;
10869:     }
10870:     if (document.getElementById(context+'_recaptchaprivtxt')) {
10871:         privtext = document.getElementById(context+'_recaptchaprivtxt');
10872:     } else {
10873:         return;
10874:     }
10875:     if (caller.checked) {
10876:         if (caller.value == 'recaptcha') {
10877:             pubitem.type = 'text';
10878:             privitem.type = 'text';
10879:             pubitem.size = '40';
10880:             privitem.size = '40';
10881:             pubtext.innerHTML = "$lt{'pub'}";
10882:             privtext.innerHTML = "$lt{'priv'}";
10883:         } else {
10884:             pubitem.type = 'hidden';
10885:             privitem.type = 'hidden';
10886:             pubtext.innerHTML = '';
10887:             privtext.innerHTML = '';
10888:         }
10889:     }
10890:     return;
10891: }
10892: 
10893: // ]]>
10894: </script>
10895: 
10896: END
10897: 
10898: }
10899: 
10900: sub credits_js {
10901:     return <<"END";
10902: 
10903: <script type="text/javascript">
10904: // <![CDATA[
10905: 
10906: function toggleCredits(domForm) {
10907:     if (document.getElementById('credits')) {
10908:         creditsitem = document.getElementById('credits');
10909:         var creditsLength = domForm.coursecredits.length;
10910:         if (creditsLength) {
10911:             var currval;
10912:             for (var i=0; i<creditsLength; i++) {
10913:                 if (domForm.coursecredits[i].checked) {
10914:                    currval = domForm.coursecredits[i].value;
10915:                 }
10916:             }
10917:             if (currval == 1) {
10918:                 creditsitem.style.display = 'block';
10919:             } else {
10920:                 creditsitem.style.display = 'none';
10921:             }
10922:         }
10923:     }
10924:     return;
10925: }
10926: 
10927: // ]]>
10928: </script>
10929: 
10930: END
10931: 
10932: }
10933: 
10934: sub captcha_phrases {
10935:     return &Apache::lonlocal::texthash (
10936:                  priv => 'Private key',
10937:                  pub  => 'Public key',
10938:                  original  => 'original (CAPTCHA)',
10939:                  recaptcha => 'successor (ReCAPTCHA)',
10940:                  notused   => 'unused',
10941:     );
10942: }
10943: 
10944: sub devalidate_remote_domconfs {
10945:     my ($dom,$cachekeys) = @_;
10946:     return unless (ref($cachekeys) eq 'HASH');
10947:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
10948:     my %thismachine;
10949:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
10950:     my @posscached = ('domainconfig','domdefaults');
10951:     if (keys(%servers) > 1) {
10952:         foreach my $server (keys(%servers)) {
10953:             next if ($thismachine{$server});
10954:             my @cached;
10955:             foreach my $name (@posscached) {
10956:                 if ($cachekeys->{$name}) {
10957:                     push(@cached,&escape($name).':'.&escape($dom));
10958:                 }
10959:             }
10960:             if (@cached) {
10961:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
10962:             }
10963:         }
10964:     }
10965:     return;
10966: }
10967: 
10968: 1;

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