File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.45: download - view: text, annotated - select for diffs
Mon May 5 23:34:42 2014 UTC (10 years ago) by raeburn
Branches: version_2_11_X
Diff to branchpoint 1.160: preferred, unified
- For 2.11
  - Backport 1.241.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.160.6.45 2014/05/05 23:34:42 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','coursedefaults',
  217:                 'usersessions','loadbalancing','requestauthor',
  218:                 'selfenrollment','inststatus'],$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','coursedefaults','selfenrollment',
  224:                        '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/types',
  259:                       help => 'Domain_Configuration_LangTZAuth',
  260:                       header => [{col1 => 'Setting',
  261:                                   col2 => 'Value'},
  262:                                  {col1 => 'Institutional user types',
  263:                                   col2 => 'Assignable to e-mail usernames'}],
  264:                       print => \&print_defaults,
  265:                       modify => \&modify_defaults,
  266:                     },
  267:         'quotas' => 
  268:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  269:                       help => 'Domain_Configuration_Quotas',
  270:                       header => [{col1 => 'User affiliation',
  271:                                   col2 => 'Available tools',
  272:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  273:                       print => \&print_quotas,
  274:                       modify => \&modify_quotas,
  275:                     },
  276:         'autoenroll' =>
  277:                    { text => 'Auto-enrollment settings',
  278:                      help => 'Domain_Configuration_Auto_Enrollment',
  279:                      header => [{col1 => 'Configuration setting',
  280:                                  col2 => 'Value(s)'}],
  281:                      print => \&print_autoenroll,
  282:                      modify => \&modify_autoenroll,
  283:                    },
  284:         'autoupdate' => 
  285:                    { text => 'Auto-update settings',
  286:                      help => 'Domain_Configuration_Auto_Updates',
  287:                      header => [{col1 => 'Setting',
  288:                                  col2 => 'Value',},
  289:                                 {col1 => 'Setting',
  290:                                  col2 => 'Affiliation'},
  291:                                 {col1 => 'User population',
  292:                                  col2 => 'Updatable user data'}],
  293:                      print => \&print_autoupdate,
  294:                      modify => \&modify_autoupdate,
  295:                   },
  296:         'autocreate' => 
  297:                   { text => 'Auto-course creation settings',
  298:                      help => 'Domain_Configuration_Auto_Creation',
  299:                      header => [{col1 => 'Configuration Setting',
  300:                                  col2 => 'Value',}],
  301:                      print => \&print_autocreate,
  302:                      modify => \&modify_autocreate,
  303:                   },
  304:         'directorysrch' => 
  305:                   { text => 'Institutional directory searches',
  306:                     help => 'Domain_Configuration_InstDirectory_Search',
  307:                     header => [{col1 => 'Setting',
  308:                                 col2 => 'Value',}],
  309:                     print => \&print_directorysrch,
  310:                     modify => \&modify_directorysrch,
  311:                   },
  312:         'contacts' =>
  313:                   { text => 'Contact Information',
  314:                     help => 'Domain_Configuration_Contact_Info',
  315:                     header => [{col1 => 'Setting',
  316:                                 col2 => 'Value',}],
  317:                     print => \&print_contacts,
  318:                     modify => \&modify_contacts,
  319:                   },
  320:         'usercreation' => 
  321:                   { text => 'User creation',
  322:                     help => 'Domain_Configuration_User_Creation',
  323:                     header => [{col1 => 'Format rule type',
  324:                                 col2 => 'Format rules in force'},
  325:                                {col1 => 'User account creation',
  326:                                 col2 => 'Usernames which may be created',},
  327:                                {col1 => 'Context',
  328:                                 col2 => 'Assignable authentication types'}],
  329:                     print => \&print_usercreation,
  330:                     modify => \&modify_usercreation,
  331:                   },
  332:         'selfcreation' => 
  333:                   { text => 'Users self-creating accounts',
  334:                     help => 'Domain_Configuration_Self_Creation', 
  335:                     header => [{col1 => 'Self-creation with institutional username',
  336:                                 col2 => 'Enabled?'},
  337:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  338:                                 col2 => 'Information user can enter'},
  339:                                {col1 => 'Self-creation with e-mail as username',
  340:                                 col2 => 'Settings'}],
  341:                     print => \&print_selfcreation,
  342:                     modify => \&modify_selfcreation,
  343:                   },
  344:         'usermodification' =>
  345:                   { text => 'User modification',
  346:                     help => 'Domain_Configuration_User_Modification',
  347:                     header => [{col1 => 'Target user has role',
  348:                                 col2 => 'User information updatable in author context'},
  349:                                {col1 => 'Target user has role',
  350:                                 col2 => 'User information updatable in course context'}],
  351:                     print => \&print_usermodification,
  352:                     modify => \&modify_usermodification,
  353:                   },
  354:         'scantron' =>
  355:                   { text => 'Bubblesheet format file',
  356:                     help => 'Domain_Configuration_Scantron_Format',
  357:                     header => [ {col1 => 'Item',
  358:                                  col2 => '',
  359:                               }],
  360:                     print => \&print_scantron,
  361:                     modify => \&modify_scantron,
  362:                   },
  363:         'requestcourses' => 
  364:                  {text => 'Request creation of courses',
  365:                   help => 'Domain_Configuration_Request_Courses',
  366:                   header => [{col1 => 'User affiliation',
  367:                               col2 => 'Availability/Processing of requests',},
  368:                              {col1 => 'Setting',
  369:                               col2 => 'Value'},
  370:                              {col1 => 'Available textbooks',
  371:                               col2 => ''},
  372:                              {col1 => 'Validation (not official courses)',
  373:                               col2 => 'Value'},],
  374:                   print => \&print_quotas,
  375:                   modify => \&modify_quotas,
  376:                  },
  377:         'requestauthor' =>
  378:                  {text => 'Request Authoring Space',
  379:                   help => 'Domain_Configuration_Request_Author',
  380:                   header => [{col1 => 'User affiliation',
  381:                               col2 => 'Availability/Processing of requests',},
  382:                              {col1 => 'Setting',
  383:                               col2 => 'Value'}],
  384:                   print => \&print_quotas,
  385:                   modify => \&modify_quotas,
  386:                  },
  387:         'coursecategories' =>
  388:                   { text => 'Cataloging of courses/communities',
  389:                     help => 'Domain_Configuration_Cataloging_Courses',
  390:                     header => [{col1 => 'Catalog type/availability',
  391:                                 col2 => '',},
  392:                                {col1 => 'Category settings for standard catalog',
  393:                                 col2 => '',},
  394:                                {col1 => 'Categories',
  395:                                 col2 => '',
  396:                                }],
  397:                     print => \&print_coursecategories,
  398:                     modify => \&modify_coursecategories,
  399:                   },
  400:         'serverstatuses' =>
  401:                  {text   => 'Access to server status pages',
  402:                   help   => 'Domain_Configuration_Server_Status',
  403:                   header => [{col1 => 'Status Page',
  404:                               col2 => 'Other named users',
  405:                               col3 => 'Specific IPs',
  406:                             }],
  407:                   print => \&print_serverstatuses,
  408:                   modify => \&modify_serverstatuses,
  409:                  },
  410:         'coursedefaults' => 
  411:                  {text => 'Course/Community defaults',
  412:                   help => 'Domain_Configuration_Course_Defaults',
  413:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  414:                               col2 => 'Value',},
  415:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  416:                               col2 => 'Value',},],
  417:                   print => \&print_coursedefaults,
  418:                   modify => \&modify_coursedefaults,
  419:                  },
  420:         'selfenrollment' => 
  421:                  {text   => 'Self-enrollment in Course/Community',
  422:                   help   => 'Domain_Configuration_Selfenrollment',
  423:                   header => [{col1 => 'Configuration Rights',
  424:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  425:                              {col1 => 'Defaults',
  426:                               col2 => 'Value'},
  427:                              {col1 => 'Self-enrollment validation (optional)',
  428:                               col2 => 'Value'},],
  429:                   print => \&print_selfenrollment,
  430:                   modify => \&modify_selfenrollment,
  431:                  },
  432:         'usersessions' =>
  433:                  {text  => 'User session hosting/offloading',
  434:                   help  => 'Domain_Configuration_User_Sessions',
  435:                   header => [{col1 => 'Domain server',
  436:                               col2 => 'Servers to offload sessions to when busy'},
  437:                              {col1 => 'Hosting of users from other domains',
  438:                               col2 => 'Rules'},
  439:                              {col1 => "Hosting domain's own users elsewhere",
  440:                               col2 => 'Rules'}],
  441:                   print => \&print_usersessions,
  442:                   modify => \&modify_usersessions,
  443:                  },
  444:          'loadbalancing' =>
  445:                  {text  => 'Dedicated Load Balancer(s)',
  446:                   help  => 'Domain_Configuration_Load_Balancing',
  447:                   header => [{col1 => 'Balancers',
  448:                               col2 => 'Default destinations',
  449:                               col3 => 'User affiliation',
  450:                               col4 => 'Overrides'},
  451:                             ],
  452:                   print => \&print_loadbalancing,
  453:                   modify => \&modify_loadbalancing,
  454:                  },
  455:     );
  456:     if (keys(%servers) > 1) {
  457:         $prefs{'login'}  = { text   => 'Log-in page options',
  458:                              help   => 'Domain_Configuration_Login_Page',
  459:                             header => [{col1 => 'Log-in Service',
  460:                                         col2 => 'Server Setting',},
  461:                                        {col1 => 'Log-in Page Items',
  462:                                         col2 => ''},
  463:                                        {col1 => 'Log-in Help',
  464:                                         col2 => 'Value'}],
  465:                             print => \&print_login,
  466:                             modify => \&modify_login,
  467:                            };
  468:     }
  469: 
  470:     my @roles = ('student','coordinator','author','admin');
  471:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  472:     &Apache::lonhtmlcommon::add_breadcrumb
  473:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  474:       text=>"Settings to display/modify"});
  475:     my $confname = $dom.'-domainconfig';
  476: 
  477:     if ($phase eq 'process') {
  478:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  479:                                                               \%prefs,\%domconfig,$confname,\@roles);
  480:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  481:             $r->rflush();
  482:             &devalidate_remote_domconfs($dom,$result);
  483:         }
  484:     } elsif ($phase eq 'display') {
  485:         my $js = &recaptcha_js().
  486:                  &toggle_display_js();
  487:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  488:             my ($othertitle,$usertypes,$types) =
  489:                 &Apache::loncommon::sorted_inst_types($dom);
  490:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  491:                                           $domconfig{'loadbalancing'}).
  492:                    &new_spares_js().
  493:                    &common_domprefs_js().
  494:                    &Apache::loncommon::javascript_array_indexof();
  495:         }
  496:         if (grep(/^requestcourses$/,@actions)) {
  497:             my $javascript_validations;
  498:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  499:             $js .= <<END;
  500: <script type="text/javascript">
  501: $javascript_validations
  502: </script>
  503: $coursebrowserjs
  504: END
  505:         }
  506:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  507:     } else {
  508: # check if domconfig user exists for the domain.
  509:         my $servadm = $r->dir_config('lonAdmEMail');
  510:         my ($configuserok,$author_ok,$switchserver) =
  511:             &config_check($dom,$confname,$servadm);
  512:         unless ($configuserok eq 'ok') {
  513:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  514:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  515:                           $confname).
  516:                       '<br />'
  517:             );
  518:             if ($switchserver) {
  519:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  520:                           '<br />'.
  521:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  522:                           '<br />'.
  523:                           &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).
  524:                           '<br />'.
  525:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  526:                 );
  527:             } else {
  528:                 $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.').
  529:                           '<br />'.
  530:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  531:                 );
  532:             }
  533:             $r->print(&Apache::loncommon::end_page());
  534:             return OK;
  535:         }
  536:         if (keys(%domconfig) == 0) {
  537:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  538:             my @ids=&Apache::lonnet::current_machine_ids();
  539:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  540:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  541:                 my @loginimages = ('img','logo','domlogo','login');
  542:                 my $custom_img_count = 0;
  543:                 foreach my $img (@loginimages) {
  544:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  545:                         $custom_img_count ++;
  546:                     }
  547:                 }
  548:                 foreach my $role (@roles) {
  549:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  550:                         $custom_img_count ++;
  551:                     }
  552:                 }
  553:                 if ($custom_img_count > 0) {
  554:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  555:                     my $switch_server = &check_switchserver($dom,$confname);
  556:                     $r->print(
  557:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  558:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  559:     &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 />'.
  560:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  561:                     if ($switch_server) {
  562:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  563:                     }
  564:                     $r->print(&Apache::loncommon::end_page());
  565:                     return OK;
  566:                 }
  567:             }
  568:         }
  569:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  570:     }
  571:     return OK;
  572: }
  573: 
  574: sub process_changes {
  575:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  576:     my %domconfig;
  577:     if (ref($values) eq 'HASH') {
  578:         %domconfig = %{$values};
  579:     }
  580:     my $output;
  581:     if ($action eq 'login') {
  582:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  583:     } elsif ($action eq 'rolecolors') {
  584:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  585:                                      $lastactref,%domconfig);
  586:     } elsif ($action eq 'quotas') {
  587:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  588:     } elsif ($action eq 'autoenroll') {
  589:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  590:     } elsif ($action eq 'autoupdate') {
  591:         $output = &modify_autoupdate($dom,%domconfig);
  592:     } elsif ($action eq 'autocreate') {
  593:         $output = &modify_autocreate($dom,%domconfig);
  594:     } elsif ($action eq 'directorysrch') {
  595:         $output = &modify_directorysrch($dom,%domconfig);
  596:     } elsif ($action eq 'usercreation') {
  597:         $output = &modify_usercreation($dom,%domconfig);
  598:     } elsif ($action eq 'selfcreation') {
  599:         $output = &modify_selfcreation($dom,%domconfig);
  600:     } elsif ($action eq 'usermodification') {
  601:         $output = &modify_usermodification($dom,%domconfig);
  602:     } elsif ($action eq 'contacts') {
  603:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  604:     } elsif ($action eq 'defaults') {
  605:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  606:     } elsif ($action eq 'scantron') {
  607:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  608:     } elsif ($action eq 'coursecategories') {
  609:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  610:     } elsif ($action eq 'serverstatuses') {
  611:         $output = &modify_serverstatuses($dom,%domconfig);
  612:     } elsif ($action eq 'requestcourses') {
  613:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  614:     } elsif ($action eq 'requestauthor') {
  615:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  616:     } elsif ($action eq 'coursedefaults') {
  617:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  618:     } elsif ($action eq 'selfenrollment') {
  619:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  620:     } elsif ($action eq 'usersessions') {
  621:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  622:     } elsif ($action eq 'loadbalancing') {
  623:         $output = &modify_loadbalancing($dom,%domconfig);
  624:     }
  625:     return $output;
  626: }
  627: 
  628: sub print_config_box {
  629:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  630:     my $rowtotal = 0;
  631:     my $output;
  632:     if ($action eq 'coursecategories') {
  633:         $output = &coursecategories_javascript($settings);
  634:     } elsif ($action eq 'defaults') {
  635:         $output = &defaults_javascript($settings); 
  636:     }
  637:     $output .=
  638:          '<table class="LC_nested_outer">
  639:           <tr>
  640:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  641:            &mt($item->{text}).'&nbsp;'.
  642:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  643:           '</tr>';
  644:     $rowtotal ++;
  645:     my $numheaders = 1;
  646:     if (ref($item->{'header'}) eq 'ARRAY') {
  647:         $numheaders = scalar(@{$item->{'header'}});
  648:     }
  649:     if ($numheaders > 1) {
  650:         my $colspan = '';
  651:         my $rightcolspan = '';
  652:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  653:             (($action eq 'login') && ($numheaders < 3))) {
  654:             $colspan = ' colspan="2"';
  655:         }
  656:         if ($action eq 'usersessions') {
  657:             $rightcolspan = ' colspan="3"'; 
  658:         }
  659:         $output .= '
  660:           <tr>
  661:            <td>
  662:             <table class="LC_nested">
  663:              <tr class="LC_info_row">
  664:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  665:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  666:              </tr>';
  667:         $rowtotal ++;
  668:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  669:             ($action eq 'usermodification') || ($action eq 'defaults') ||
  670:             ($action eq 'selfenrollment') || ($action eq 'usersessions')) {
  671:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  672:         } elsif ($action eq 'coursecategories') {
  673:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  674:         } elsif ($action eq 'login') {
  675:             if ($numheaders == 3) {
  676:                 $colspan = ' colspan="2"';
  677:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  678:             } else {
  679:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  680:             }
  681:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  682:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  683:         } elsif ($action eq 'rolecolors') {
  684:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  685:         }
  686:         $output .= '
  687:            </table>
  688:           </td>
  689:          </tr>
  690:          <tr>
  691:            <td>
  692:             <table class="LC_nested">
  693:              <tr class="LC_info_row">
  694:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  695:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  696:              </tr>';
  697:             $rowtotal ++;
  698:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  699:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  700:             ($action eq 'usersessions') || ($action eq 'coursecategories')) {
  701:             if ($action eq 'coursecategories') {
  702:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  703:                 $colspan = ' colspan="2"';
  704:             } else {
  705:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  706:             }
  707:             $output .= '
  708:            </table>
  709:           </td>
  710:          </tr>
  711:          <tr>
  712:            <td>
  713:             <table class="LC_nested">
  714:              <tr class="LC_info_row">
  715:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  716:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  717:              </tr>'."\n";
  718:             if ($action eq 'coursecategories') {
  719:                 $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  720:             } else {
  721:                 $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  722:             }
  723:             $rowtotal ++;
  724:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  725:                   ($action eq 'defaults')) {
  726:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  727:         } elsif ($action eq 'login') {
  728:             if ($numheaders == 3) {
  729:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  730:            </table>
  731:           </td>
  732:          </tr>
  733:          <tr>
  734:            <td>
  735:             <table class="LC_nested">
  736:              <tr class="LC_info_row">
  737:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  738:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  739:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  740:                 $rowtotal ++;
  741:             } else {
  742:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  743:             }
  744:         } elsif ($action eq 'requestcourses') {
  745:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal).
  746:                        &print_studentcode($settings,\$rowtotal).'
  747:            </table>
  748:           </td>
  749:          </tr>
  750:          <tr>
  751:            <td>
  752:             <table class="LC_nested">
  753:              <tr class="LC_info_row">
  754:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  755:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  756:                        &print_textbookcourses($dom,$settings,\$rowtotal).'
  757:             </table>
  758:            </td>
  759:           </tr>
  760:           <tr>
  761:            <td>
  762:             <table class="LC_nested">
  763:              <tr class="LC_info_row">
  764:               <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  765:               <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  766:              </tr>'.
  767:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
  768:         } elsif ($action eq 'requestauthor') {
  769:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  770:         } elsif ($action eq 'rolecolors') {
  771:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  772:            </table>
  773:           </td>
  774:          </tr>
  775:          <tr>
  776:            <td>
  777:             <table class="LC_nested">
  778:              <tr class="LC_info_row">
  779:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  780:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  781:               <td class="LC_right_item" valign="top">'.
  782:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  783:              </tr>'.
  784:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  785:            </table>
  786:           </td>
  787:          </tr>
  788:          <tr>
  789:            <td>
  790:             <table class="LC_nested">
  791:              <tr class="LC_info_row">
  792:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  793:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  794:              </tr>'.
  795:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  796:             $rowtotal += 2;
  797:         }
  798:     } else {
  799:         $output .= '
  800:           <tr>
  801:            <td>
  802:             <table class="LC_nested">
  803:              <tr class="LC_info_row">';
  804:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  805:             $output .= '  
  806:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  807:         } elsif ($action eq 'serverstatuses') {
  808:             $output .= '
  809:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  810:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  811: 
  812:         } else {
  813:             $output .= '
  814:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  815:         }
  816:         if (defined($item->{'header'}->[0]->{'col3'})) {
  817:             $output .= '<td class="LC_left_item" valign="top">'.
  818:                        &mt($item->{'header'}->[0]->{'col2'});
  819:             if ($action eq 'serverstatuses') {
  820:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  821:             } 
  822:         } else {
  823:             $output .= '<td class="LC_right_item" valign="top">'.
  824:                        &mt($item->{'header'}->[0]->{'col2'});
  825:         }
  826:         $output .= '</td>';
  827:         if ($item->{'header'}->[0]->{'col3'}) {
  828:             if (defined($item->{'header'}->[0]->{'col4'})) {
  829:                 $output .= '<td class="LC_left_item" valign="top">'.
  830:                             &mt($item->{'header'}->[0]->{'col3'});
  831:             } else {
  832:                 $output .= '<td class="LC_right_item" valign="top">'.
  833:                            &mt($item->{'header'}->[0]->{'col3'});
  834:             }
  835:             if ($action eq 'serverstatuses') {
  836:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  837:             }
  838:             $output .= '</td>';
  839:         }
  840:         if ($item->{'header'}->[0]->{'col4'}) {
  841:             $output .= '<td class="LC_right_item" valign="top">'.
  842:                        &mt($item->{'header'}->[0]->{'col4'});
  843:         }
  844:         $output .= '</tr>';
  845:         $rowtotal ++;
  846:         if ($action eq 'quotas') {
  847:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  848:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || ($action eq 'directorysrch') ||
  849:                  ($action eq 'contacts') || ($action eq 'serverstatuses') || ($action eq 'loadbalancing')) {
  850:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
  851:         } elsif ($action eq 'scantron') {
  852:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  853:         } elsif ($action eq 'helpsettings') {
  854:             $output .= &print_helpsettings($dom,$confname,$settings,\$rowtotal);
  855:         }
  856:     }
  857:     $output .= '
  858:    </table>
  859:   </td>
  860:  </tr>
  861: </table><br />';
  862:     return ($output,$rowtotal);
  863: }
  864: 
  865: sub print_login {
  866:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  867:     my ($css_class,$datatable);
  868:     my %choices = &login_choices();
  869: 
  870:     if ($caller eq 'service') {
  871:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  872:         my $choice = $choices{'disallowlogin'};
  873:         $css_class = ' class="LC_odd_row"';
  874:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  875:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  876:                       '<th>'.$choices{'server'}.'</th>'.
  877:                       '<th>'.$choices{'serverpath'}.'</th>'.
  878:                       '<th>'.$choices{'custompath'}.'</th>'.
  879:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  880:         my %disallowed;
  881:         if (ref($settings) eq 'HASH') {
  882:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  883:                %disallowed = %{$settings->{'loginvia'}};
  884:             }
  885:         }
  886:         foreach my $lonhost (sort(keys(%servers))) {
  887:             my $direct = 'selected="selected"';
  888:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  889:                 if ($disallowed{$lonhost}{'server'} ne '') {
  890:                     $direct = '';
  891:                 }
  892:             }
  893:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  894:                           '<td><select name="'.$lonhost.'_server">'.
  895:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  896:                           '</option>';
  897:             foreach my $hostid (sort(keys(%servers))) {
  898:                 next if ($servers{$hostid} eq $servers{$lonhost});
  899:                 my $selected = '';
  900:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  901:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  902:                         $selected = 'selected="selected"';
  903:                     }
  904:                 }
  905:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  906:                               $servers{$hostid}.'</option>';
  907:             }
  908:             $datatable .= '</select></td>'.
  909:                           '<td><select name="'.$lonhost.'_serverpath">';
  910:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  911:                 my $pathname = $path;
  912:                 if ($path eq 'custom') {
  913:                     $pathname = &mt('Custom Path').' ->';
  914:                 }
  915:                 my $selected = '';
  916:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  917:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  918:                         $selected = 'selected="selected"';
  919:                     }
  920:                 } elsif ($path eq '') {
  921:                     $selected = 'selected="selected"';
  922:                 }
  923:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  924:             }
  925:             $datatable .= '</select></td>';
  926:             my ($custom,$exempt);
  927:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  928:                 $custom = $disallowed{$lonhost}{'custompath'};
  929:                 $exempt = $disallowed{$lonhost}{'exempt'};
  930:             }
  931:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  932:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  933:                           '</tr>';
  934:         }
  935:         $datatable .= '</table></td></tr>';
  936:         return $datatable;
  937:     } elsif ($caller eq 'page') {
  938:         my %defaultchecked = ( 
  939:                                'coursecatalog' => 'on',
  940:                                'helpdesk'      => 'on',
  941:                                'adminmail'     => 'off',
  942:                                'newuser'       => 'off',
  943:                              );
  944:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
  945:         my (%checkedon,%checkedoff);
  946:         foreach my $item (@toggles) {
  947:             if ($defaultchecked{$item} eq 'on') { 
  948:                 $checkedon{$item} = ' checked="checked" ';
  949:                 $checkedoff{$item} = ' ';
  950:             } elsif ($defaultchecked{$item} eq 'off') {
  951:                 $checkedoff{$item} = ' checked="checked" ';
  952:                 $checkedon{$item} = ' ';
  953:             }
  954:         }
  955:         my @images = ('img','logo','domlogo','login');
  956:         my @logintext = ('textcol','bgcol');
  957:         my @bgs = ('pgbg','mainbg','sidebg');
  958:         my @links = ('link','alink','vlink');
  959:         my %designhash = &Apache::loncommon::get_domainconf($dom);
  960:         my %defaultdesign = %Apache::loncommon::defaultdesign;
  961:         my (%is_custom,%designs);
  962:         my %defaults = (
  963:                        font => $defaultdesign{'login.font'},
  964:                        );
  965:         foreach my $item (@images) {
  966:             $defaults{$item} = $defaultdesign{'login.'.$item};
  967:             $defaults{'showlogo'}{$item} = 1;
  968:         }
  969:         foreach my $item (@bgs) {
  970:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
  971:         }
  972:         foreach my $item (@logintext) {
  973:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
  974:         }
  975:         foreach my $item (@links) {
  976:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
  977:         }
  978:         if (ref($settings) eq 'HASH') {
  979:             foreach my $item (@toggles) {
  980:                 if ($settings->{$item} eq '1') {
  981:                     $checkedon{$item} =  ' checked="checked" ';
  982:                     $checkedoff{$item} = ' ';
  983:                 } elsif ($settings->{$item} eq '0') {
  984:                     $checkedoff{$item} =  ' checked="checked" ';
  985:                     $checkedon{$item} = ' ';
  986:                 }
  987:             }
  988:             foreach my $item (@images) {
  989:                 if (defined($settings->{$item})) {
  990:                     $designs{$item} = $settings->{$item};
  991:                     $is_custom{$item} = 1;
  992:                 }
  993:                 if (defined($settings->{'showlogo'}{$item})) {
  994:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
  995:                 }
  996:             }
  997:             foreach my $item (@logintext) {
  998:                 if ($settings->{$item} ne '') {
  999:                     $designs{'logintext'}{$item} = $settings->{$item};
 1000:                     $is_custom{$item} = 1;
 1001:                 }
 1002:             }
 1003:             if ($settings->{'font'} ne '') {
 1004:                 $designs{'font'} = $settings->{'font'};
 1005:                 $is_custom{'font'} = 1;
 1006:             }
 1007:             foreach my $item (@bgs) {
 1008:                 if ($settings->{$item} ne '') {
 1009:                     $designs{'bgs'}{$item} = $settings->{$item};
 1010:                     $is_custom{$item} = 1;
 1011:                 }
 1012:             }
 1013:             foreach my $item (@links) {
 1014:                 if ($settings->{$item} ne '') {
 1015:                     $designs{'links'}{$item} = $settings->{$item};
 1016:                     $is_custom{$item} = 1;
 1017:                 }
 1018:             }
 1019:         } else {
 1020:             if ($designhash{$dom.'.login.font'} ne '') {
 1021:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1022:                 $is_custom{'font'} = 1;
 1023:             }
 1024:             foreach my $item (@images) {
 1025:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1026:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1027:                     $is_custom{$item} = 1;
 1028:                 }
 1029:             }
 1030:             foreach my $item (@bgs) {
 1031:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1032:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1033:                     $is_custom{$item} = 1;
 1034:                 }
 1035:             }
 1036:             foreach my $item (@links) {
 1037:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1038:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1039:                     $is_custom{$item} = 1;
 1040:                 }
 1041:             }
 1042:         }
 1043:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1044:                                                       logo => 'Institution Logo',
 1045:                                                       domlogo => 'Domain Logo',
 1046:                                                       login => 'Login box');
 1047:         my $itemcount = 1;
 1048:         foreach my $item (@toggles) {
 1049:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1050:             $datatable .=  
 1051:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1052:                 '</td><td>'.
 1053:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1054:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1055:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1056:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1057:                 '</tr>';
 1058:             $itemcount ++;
 1059:         }
 1060:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1061:         $datatable .= '</tr></table></td></tr>';
 1062:     } elsif ($caller eq 'help') {
 1063:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1064:         my $switchserver = &check_switchserver($dom,$confname);
 1065:         my $itemcount = 1;
 1066:         $defaulturl = '/adm/loginproblems.html';
 1067:         $defaulttype = 'default';
 1068:         %lt = &Apache::lonlocal::texthash (
 1069:                      del     => 'Delete?',
 1070:                      rep     => 'Replace:',
 1071:                      upl     => 'Upload:',
 1072:                      default => 'Default',
 1073:                      custom  => 'Custom',
 1074:                                              );
 1075:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1076:         my @currlangs;
 1077:         if (ref($settings) eq 'HASH') {
 1078:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1079:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1080:                     next if ($settings->{'helpurl'}{$key} eq '');
 1081:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1082:                     $type{$key} = 'custom';
 1083:                     unless ($key eq 'nolang') {
 1084:                         push(@currlangs,$key);
 1085:                     }
 1086:                 }
 1087:             } elsif ($settings->{'helpurl'} ne '') {
 1088:                 $type{'nolang'} = 'custom';
 1089:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1090:             }
 1091:         }
 1092:         foreach my $lang ('nolang',sort(@currlangs)) {
 1093:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1094:             $datatable .= '<tr'.$css_class.'>';
 1095:             if ($url{$lang} eq '') {
 1096:                 $url{$lang} = $defaulturl;
 1097:             }
 1098:             if ($type{$lang} eq '') {
 1099:                 $type{$lang} = $defaulttype;
 1100:             }
 1101:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1102:             if ($lang eq 'nolang') {
 1103:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1104:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1105:             } else {
 1106:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1107:                                   $langchoices{$lang},
 1108:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1109:             }
 1110:             $datatable .= '</span></td>'."\n".
 1111:                           '<td class="LC_left_item">';
 1112:             if ($type{$lang} eq 'custom') {
 1113:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1114:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1115:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1116:             } else {
 1117:                 $datatable .= $lt{'upl'};
 1118:             }
 1119:             $datatable .='<br />';
 1120:             if ($switchserver) {
 1121:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1122:             } else {
 1123:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1124:             }
 1125:             $datatable .= '</td></tr>';
 1126:             $itemcount ++;
 1127:         }
 1128:         my @addlangs;
 1129:         foreach my $lang (sort(keys(%langchoices))) {
 1130:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1131:             push(@addlangs,$lang);
 1132:         }
 1133:         if (@addlangs > 0) {
 1134:             my %toadd;
 1135:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1136:             $toadd{''} = &mt('Select');
 1137:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1138:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1139:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1140:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1141:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1142:             if ($switchserver) {
 1143:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1144:             } else {
 1145:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1146:             }
 1147:             $datatable .= '</td></tr>';
 1148:             $itemcount ++;
 1149:         }
 1150:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1151:     }
 1152:     return $datatable;
 1153: }
 1154: 
 1155: sub login_choices {
 1156:     my %choices =
 1157:         &Apache::lonlocal::texthash (
 1158:             coursecatalog => 'Display Course/Community Catalog link?',
 1159:             adminmail     => "Display Administrator's E-mail Address?",
 1160:             helpdesk      => 'Display "Contact Helpdesk" link',
 1161:             disallowlogin => "Login page requests redirected",
 1162:             hostid        => "Server",
 1163:             server        => "Redirect to:",
 1164:             serverpath    => "Path",
 1165:             custompath    => "Custom", 
 1166:             exempt        => "Exempt IP(s)",
 1167:             directlogin   => "No redirect",
 1168:             newuser       => "Link to create a user account",
 1169:             img           => "Header",
 1170:             logo          => "Main Logo",
 1171:             domlogo       => "Domain Logo",
 1172:             login         => "Log-in Header", 
 1173:             textcol       => "Text color",
 1174:             bgcol         => "Box color",
 1175:             bgs           => "Background colors",
 1176:             links         => "Link colors",
 1177:             font          => "Font color",
 1178:             pgbg          => "Header",
 1179:             mainbg        => "Page",
 1180:             sidebg        => "Login box",
 1181:             link          => "Link",
 1182:             alink         => "Active link",
 1183:             vlink         => "Visited link",
 1184:         );
 1185:     return %choices;
 1186: }
 1187: 
 1188: sub print_rolecolors {
 1189:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1190:     my %choices = &color_font_choices();
 1191:     my @bgs = ('pgbg','tabbg','sidebg');
 1192:     my @links = ('link','alink','vlink');
 1193:     my @images = ('img');
 1194:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1195:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1196:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1197:     my (%is_custom,%designs);
 1198:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1199:     if (ref($settings) eq 'HASH') {
 1200:         if (ref($settings->{$role}) eq 'HASH') {
 1201:             if ($settings->{$role}->{'img'} ne '') {
 1202:                 $designs{'img'} = $settings->{$role}->{'img'};
 1203:                 $is_custom{'img'} = 1;
 1204:             }
 1205:             if ($settings->{$role}->{'font'} ne '') {
 1206:                 $designs{'font'} = $settings->{$role}->{'font'};
 1207:                 $is_custom{'font'} = 1;
 1208:             }
 1209:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1210:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1211:                 $is_custom{'fontmenu'} = 1;
 1212:             }
 1213:             foreach my $item (@bgs) {
 1214:                 if ($settings->{$role}->{$item} ne '') {
 1215:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1216:                     $is_custom{$item} = 1;
 1217:                 }
 1218:             }
 1219:             foreach my $item (@links) {
 1220:                 if ($settings->{$role}->{$item} ne '') {
 1221:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1222:                     $is_custom{$item} = 1;
 1223:                 }
 1224:             }
 1225:         }
 1226:     } else {
 1227:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1228:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1229:             $is_custom{'img'} = 1;
 1230:         }
 1231:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1232:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1233:             $is_custom{'fontmenu'} = 1; 
 1234:         }
 1235:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1236:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1237:             $is_custom{'font'} = 1;
 1238:         }
 1239:         foreach my $item (@bgs) {
 1240:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1241:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1242:                 $is_custom{$item} = 1;
 1243:             
 1244:             }
 1245:         }
 1246:         foreach my $item (@links) {
 1247:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1248:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1249:                 $is_custom{$item} = 1;
 1250:             }
 1251:         }
 1252:     }
 1253:     my $itemcount = 1;
 1254:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1255:     $datatable .= '</tr></table></td></tr>';
 1256:     return $datatable;
 1257: }
 1258: 
 1259: sub role_defaults {
 1260:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1261:     my %defaults;
 1262:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1263:         return %defaults;
 1264:     }
 1265:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1266:     if ($role eq 'login') {
 1267:         %defaults = (
 1268:                        font => $defaultdesign{$role.'.font'},
 1269:                     );
 1270:         if (ref($logintext) eq 'ARRAY') {
 1271:             foreach my $item (@{$logintext}) {
 1272:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1273:             }
 1274:         }
 1275:         foreach my $item (@{$images}) {
 1276:             $defaults{'showlogo'}{$item} = 1;
 1277:         }
 1278:     } else {
 1279:         %defaults = (
 1280:                        img => $defaultdesign{$role.'.img'},
 1281:                        font => $defaultdesign{$role.'.font'},
 1282:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1283:                     );
 1284:     }
 1285:     foreach my $item (@{$bgs}) {
 1286:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1287:     }
 1288:     foreach my $item (@{$links}) {
 1289:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1290:     }
 1291:     foreach my $item (@{$images}) {
 1292:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1293:     }
 1294:     return %defaults;
 1295: }
 1296: 
 1297: sub display_color_options {
 1298:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1299:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1300:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1301:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1302:     my $datatable = '<tr'.$css_class.'>'.
 1303:         '<td>'.$choices->{'font'}.'</td>';
 1304:     if (!$is_custom->{'font'}) {
 1305:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1306:     } else {
 1307:         $datatable .= '<td>&nbsp;</td>';
 1308:     }
 1309:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1310: 
 1311:     $datatable .= '<td><span class="LC_nobreak">'.
 1312:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1313:                   ' value="'.$current_color.'" />&nbsp;'.
 1314:                   '&nbsp;</td></tr>';
 1315:     unless ($role eq 'login') { 
 1316:         $datatable .= '<tr'.$css_class.'>'.
 1317:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1318:         if (!$is_custom->{'fontmenu'}) {
 1319:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1320:         } else {
 1321:             $datatable .= '<td>&nbsp;</td>';
 1322:         }
 1323: 	$current_color = $designs->{'fontmenu'} ?
 1324: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1325:         $datatable .= '<td><span class="LC_nobreak">'.
 1326:                       '<input class="colorchooser" type="text" size="10" name="'
 1327: 		      .$role.'_fontmenu"'.
 1328:                       ' value="'.$current_color.'" />&nbsp;'.
 1329:                       '&nbsp;</td></tr>';
 1330:     }
 1331:     my $switchserver = &check_switchserver($dom,$confname);
 1332:     foreach my $img (@{$images}) {
 1333: 	$itemcount ++;
 1334:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1335:         $datatable .= '<tr'.$css_class.'>'.
 1336:                       '<td>'.$choices->{$img};
 1337:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1338:         if ($role eq 'login') {
 1339:             if ($img eq 'login') {
 1340:                 $login_hdr_pick =
 1341:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1342:                 $logincolors =
 1343:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1344:                                        $designs,$defaults);
 1345:             } elsif ($img ne 'domlogo') {
 1346:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1347:             }
 1348:         }
 1349:         $datatable .= '</td>';
 1350:         if ($designs->{$img} ne '') {
 1351:             $imgfile = $designs->{$img};
 1352: 	    $img_import = ($imgfile =~ m{^/adm/});
 1353:         } else {
 1354:             $imgfile = $defaults->{$img};
 1355:         }
 1356:         if ($imgfile) {
 1357:             my ($showfile,$fullsize);
 1358:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1359:                 my $urldir = $1;
 1360:                 my $filename = $2;
 1361:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1362:                 if (@info) {
 1363:                     my $thumbfile = 'tn-'.$filename;
 1364:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1365:                     if (@thumb) {
 1366:                         $showfile = $urldir.'/'.$thumbfile;
 1367:                     } else {
 1368:                         $showfile = $imgfile;
 1369:                     }
 1370:                 } else {
 1371:                     $showfile = '';
 1372:                 }
 1373:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1374:                 $showfile = $imgfile;
 1375:                 my $imgdir = $1;
 1376:                 my $filename = $2;
 1377:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1378:                     $showfile = "/$imgdir/tn-".$filename;
 1379:                 } else {
 1380:                     my $input = $londocroot.$imgfile;
 1381:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1382:                     if (!-e $output) {
 1383:                         my ($width,$height) = &thumb_dimensions();
 1384:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1385:                         if ($fullwidth ne '' && $fullheight ne '') {
 1386:                             if ($fullwidth > $width && $fullheight > $height) { 
 1387:                                 my $size = $width.'x'.$height;
 1388:                                 system("convert -sample $size $input $output");
 1389:                                 $showfile = "/$imgdir/tn-".$filename;
 1390:                             }
 1391:                         }
 1392:                     }
 1393:                 }
 1394:             }
 1395:             if ($showfile) {
 1396:                 if ($showfile =~ m{^/(adm|res)/}) {
 1397:                     if ($showfile =~ m{^/res/}) {
 1398:                         my $local_showfile =
 1399:                             &Apache::lonnet::filelocation('',$showfile);
 1400:                         &Apache::lonnet::repcopy($local_showfile);
 1401:                     }
 1402:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1403:                 }
 1404:                 if ($imgfile) {
 1405:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1406:                         if ($imgfile =~ m{^/res/}) {
 1407:                             my $local_imgfile =
 1408:                                 &Apache::lonnet::filelocation('',$imgfile);
 1409:                             &Apache::lonnet::repcopy($local_imgfile);
 1410:                         }
 1411:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1412:                     } else {
 1413:                         $fullsize = $imgfile;
 1414:                     }
 1415:                 }
 1416:                 $datatable .= '<td>';
 1417:                 if ($img eq 'login') {
 1418:                     $datatable .= $login_hdr_pick;
 1419:                 } 
 1420:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1421:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1422:             } else {
 1423:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1424:                               &mt('Upload:').'<br />';
 1425:             }
 1426:         } else {
 1427:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1428:                           &mt('Upload:').'<br />';
 1429:         }
 1430:         if ($switchserver) {
 1431:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1432:         } else {
 1433:             if ($img ne 'login') { # suppress file selection for Log-in header
 1434:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1435:             }
 1436:         }
 1437:         $datatable .= '</td></tr>';
 1438:     }
 1439:     $itemcount ++;
 1440:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1441:     $datatable .= '<tr'.$css_class.'>'.
 1442:                   '<td>'.$choices->{'bgs'}.'</td>';
 1443:     my $bgs_def;
 1444:     foreach my $item (@{$bgs}) {
 1445:         if (!$is_custom->{$item}) {
 1446:             $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>';
 1447:         }
 1448:     }
 1449:     if ($bgs_def) {
 1450:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1451:     } else {
 1452:         $datatable .= '<td>&nbsp;</td>';
 1453:     }
 1454:     $datatable .= '<td class="LC_right_item">'.
 1455:                   '<table border="0"><tr>';
 1456: 
 1457:     foreach my $item (@{$bgs}) {
 1458:         $datatable .= '<td align="center">'.$choices->{$item};
 1459: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1460:         if ($designs->{'bgs'}{$item}) {
 1461:             $datatable .= '&nbsp;';
 1462:         }
 1463:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1464:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1465:     }
 1466:     $datatable .= '</tr></table></td></tr>';
 1467:     $itemcount ++;
 1468:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1469:     $datatable .= '<tr'.$css_class.'>'.
 1470:                   '<td>'.$choices->{'links'}.'</td>';
 1471:     my $links_def;
 1472:     foreach my $item (@{$links}) {
 1473:         if (!$is_custom->{$item}) {
 1474:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1475:         }
 1476:     }
 1477:     if ($links_def) {
 1478:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1479:     } else {
 1480:         $datatable .= '<td>&nbsp;</td>';
 1481:     }
 1482:     $datatable .= '<td class="LC_right_item">'.
 1483:                   '<table border="0"><tr>';
 1484:     foreach my $item (@{$links}) {
 1485: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1486:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1487:         if ($designs->{'links'}{$item}) {
 1488:             $datatable.='&nbsp;';
 1489:         }
 1490:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1491:                       '" /></td>';
 1492:     }
 1493:     $$rowtotal += $itemcount;
 1494:     return $datatable;
 1495: }
 1496: 
 1497: sub logo_display_options {
 1498:     my ($img,$defaults,$designs) = @_;
 1499:     my $checkedon;
 1500:     if (ref($defaults) eq 'HASH') {
 1501:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1502:             if ($defaults->{'showlogo'}{$img}) {
 1503:                 $checkedon = 'checked="checked" ';     
 1504:             }
 1505:         } 
 1506:     }
 1507:     if (ref($designs) eq 'HASH') {
 1508:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1509:             if (defined($designs->{'showlogo'}{$img})) {
 1510:                 if ($designs->{'showlogo'}{$img} == 0) {
 1511:                     $checkedon = '';
 1512:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1513:                     $checkedon = 'checked="checked" ';
 1514:                 }
 1515:             }
 1516:         }
 1517:     }
 1518:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1519:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1520:            &mt('show').'</label>'."\n";
 1521: }
 1522: 
 1523: sub login_header_options  {
 1524:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1525:     my $output = '';
 1526:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1527:         $output .= &mt('Text default(s):').'<br />';
 1528:         if (!$is_custom->{'textcol'}) {
 1529:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1530:                        '&nbsp;&nbsp;&nbsp;';
 1531:         }
 1532:         if (!$is_custom->{'bgcol'}) {
 1533:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1534:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1535:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1536:         }
 1537:         $output .= '<br />';
 1538:     }
 1539:     $output .='<br />';
 1540:     return $output;
 1541: }
 1542: 
 1543: sub login_text_colors {
 1544:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1545:     my $color_menu = '<table border="0"><tr>';
 1546:     foreach my $item (@{$logintext}) {
 1547:         $color_menu .= '<td align="center">'.$choices->{$item};
 1548:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1549:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1550:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1551:     }
 1552:     $color_menu .= '</tr></table><br />';
 1553:     return $color_menu;
 1554: }
 1555: 
 1556: sub image_changes {
 1557:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1558:     my $output;
 1559:     if ($img eq 'login') {
 1560:             # suppress image for Log-in header
 1561:     } elsif (!$is_custom) {
 1562:         if ($img ne 'domlogo') {
 1563:             $output .= &mt('Default image:').'<br />';
 1564:         } else {
 1565:             $output .= &mt('Default in use:').'<br />';
 1566:         }
 1567:     }
 1568:     if ($img eq 'login') { # suppress image for Log-in header
 1569:         $output .= '<td>'.$logincolors;
 1570:     } else {
 1571:         if ($img_import) {
 1572:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1573:         }
 1574:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1575:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1576:         if ($is_custom) {
 1577:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1578:                        '<input type="checkbox" name="'.
 1579:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1580:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1581:         } else {
 1582:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1583:         }
 1584:     }
 1585:     return $output;
 1586: }
 1587: 
 1588: sub print_quotas {
 1589:     my ($dom,$settings,$rowtotal,$action) = @_;
 1590:     my $context;
 1591:     if ($action eq 'quotas') {
 1592:         $context = 'tools';
 1593:     } else {
 1594:         $context = $action;
 1595:     }
 1596:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1597:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1598:     my $typecount = 0;
 1599:     my ($css_class,%titles);
 1600:     if ($context eq 'requestcourses') {
 1601:         @usertools = ('official','unofficial','community','textbook');
 1602:         @options =('norequest','approval','validate','autolimit');
 1603:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1604:         %titles = &courserequest_titles();
 1605:     } elsif ($context eq 'requestauthor') {
 1606:         @usertools = ('author');
 1607:         @options = ('norequest','approval','automatic');
 1608:         %titles = &authorrequest_titles();
 1609:     } else {
 1610:         @usertools = ('aboutme','blog','webdav','portfolio');
 1611:         %titles = &tool_titles();
 1612:     }
 1613:     if (ref($types) eq 'ARRAY') {
 1614:         foreach my $type (@{$types}) {
 1615:             my ($currdefquota,$currauthorquota);
 1616:             unless (($context eq 'requestcourses') ||
 1617:                     ($context eq 'requestauthor')) {
 1618:                 if (ref($settings) eq 'HASH') {
 1619:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1620:                         $currdefquota = $settings->{defaultquota}->{$type};
 1621:                     } else {
 1622:                         $currdefquota = $settings->{$type};
 1623:                     }
 1624:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1625:                         $currauthorquota = $settings->{authorquota}->{$type};
 1626:                     }
 1627:                 }
 1628:             }
 1629:             if (defined($usertypes->{$type})) {
 1630:                 $typecount ++;
 1631:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1632:                 $datatable .= '<tr'.$css_class.'>'.
 1633:                               '<td>'.$usertypes->{$type}.'</td>'.
 1634:                               '<td class="LC_left_item">';
 1635:                 if ($context eq 'requestcourses') {
 1636:                     $datatable .= '<table><tr>';
 1637:                 }
 1638:                 my %cell;  
 1639:                 foreach my $item (@usertools) {
 1640:                     if ($context eq 'requestcourses') {
 1641:                         my ($curroption,$currlimit);
 1642:                         if (ref($settings) eq 'HASH') {
 1643:                             if (ref($settings->{$item}) eq 'HASH') {
 1644:                                 $curroption = $settings->{$item}->{$type};
 1645:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1646:                                     $currlimit = $1; 
 1647:                                 }
 1648:                             }
 1649:                         }
 1650:                         if (!$curroption) {
 1651:                             $curroption = 'norequest';
 1652:                         }
 1653:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1654:                         foreach my $option (@options) {
 1655:                             my $val = $option;
 1656:                             if ($option eq 'norequest') {
 1657:                                 $val = 0;  
 1658:                             }
 1659:                             if ($option eq 'validate') {
 1660:                                 my $canvalidate = 0;
 1661:                                 if (ref($validations{$item}) eq 'HASH') { 
 1662:                                     if ($validations{$item}{$type}) {
 1663:                                         $canvalidate = 1;
 1664:                                     }
 1665:                                 }
 1666:                                 next if (!$canvalidate);
 1667:                             }
 1668:                             my $checked = '';
 1669:                             if ($option eq $curroption) {
 1670:                                 $checked = ' checked="checked"';
 1671:                             } elsif ($option eq 'autolimit') {
 1672:                                 if ($curroption =~ /^autolimit/) {
 1673:                                     $checked = ' checked="checked"';
 1674:                                 }                       
 1675:                             } 
 1676:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1677:                                   '<input type="radio" name="crsreq_'.$item.
 1678:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1679:                                   $titles{$option}.'</label>';
 1680:                             if ($option eq 'autolimit') {
 1681:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1682:                                                 $item.'_limit_'.$type.'" size="1" '.
 1683:                                                 'value="'.$currlimit.'" />';
 1684:                             }
 1685:                             $cell{$item} .= '</span> ';
 1686:                             if ($option eq 'autolimit') {
 1687:                                 $cell{$item} .= $titles{'unlimited'};
 1688:                             }
 1689:                         }
 1690:                     } elsif ($context eq 'requestauthor') {
 1691:                         my $curroption;
 1692:                         if (ref($settings) eq 'HASH') {
 1693:                             $curroption = $settings->{$type};
 1694:                         }
 1695:                         if (!$curroption) {
 1696:                             $curroption = 'norequest';
 1697:                         }
 1698:                         foreach my $option (@options) {
 1699:                             my $val = $option;
 1700:                             if ($option eq 'norequest') {
 1701:                                 $val = 0;
 1702:                             }
 1703:                             my $checked = '';
 1704:                             if ($option eq $curroption) {
 1705:                                 $checked = ' checked="checked"';
 1706:                             }
 1707:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1708:                                   '<input type="radio" name="authorreq_'.$type.
 1709:                                   '" value="'.$val.'"'.$checked.' />'.
 1710:                                   $titles{$option}.'</label></span>&nbsp; ';
 1711:                         }
 1712:                     } else {
 1713:                         my $checked = 'checked="checked" ';
 1714:                         if (ref($settings) eq 'HASH') {
 1715:                             if (ref($settings->{$item}) eq 'HASH') {
 1716:                                 if ($settings->{$item}->{$type} == 0) {
 1717:                                     $checked = '';
 1718:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1719:                                     $checked =  'checked="checked" ';
 1720:                                 }
 1721:                             }
 1722:                         }
 1723:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1724:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1725:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1726:                                       '</label></span>&nbsp; ';
 1727:                     }
 1728:                 }
 1729:                 if ($context eq 'requestcourses') {
 1730:                     $datatable .= '</tr><tr>';
 1731:                     foreach my $item (@usertools) {
 1732:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1733:                     }
 1734:                     $datatable .= '</tr></table>';
 1735:                 }
 1736:                 $datatable .= '</td>';
 1737:                 unless (($context eq 'requestcourses') ||
 1738:                         ($context eq 'requestauthor')) {
 1739:                     $datatable .= 
 1740:                               '<td class="LC_right_item">'.
 1741:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1742:                               '<input type="text" name="quota_'.$type.
 1743:                               '" value="'.$currdefquota.
 1744:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1745:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1746:                               '<input type="text" name="authorquota_'.$type.
 1747:                               '" value="'.$currauthorquota.
 1748:                               '" size="5" /></span></td>';
 1749:                 }
 1750:                 $datatable .= '</tr>';
 1751:             }
 1752:         }
 1753:     }
 1754:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1755:         $defaultquota = '20';
 1756:         $authorquota = '500';
 1757:         if (ref($settings) eq 'HASH') {
 1758:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1759:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1760:             } elsif (defined($settings->{'default'})) {
 1761:                 $defaultquota = $settings->{'default'};
 1762:             }
 1763:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1764:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1765:             }
 1766:         }
 1767:     }
 1768:     $typecount ++;
 1769:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1770:     $datatable .= '<tr'.$css_class.'>'.
 1771:                   '<td>'.$othertitle.'</td>'.
 1772:                   '<td class="LC_left_item">';
 1773:     if ($context eq 'requestcourses') {
 1774:         $datatable .= '<table><tr>';
 1775:     }
 1776:     my %defcell;
 1777:     foreach my $item (@usertools) {
 1778:         if ($context eq 'requestcourses') {
 1779:             my ($curroption,$currlimit);
 1780:             if (ref($settings) eq 'HASH') {
 1781:                 if (ref($settings->{$item}) eq 'HASH') {
 1782:                     $curroption = $settings->{$item}->{'default'};
 1783:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1784:                         $currlimit = $1;
 1785:                     }
 1786:                 }
 1787:             }
 1788:             if (!$curroption) {
 1789:                 $curroption = 'norequest';
 1790:             }
 1791:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1792:             foreach my $option (@options) {
 1793:                 my $val = $option;
 1794:                 if ($option eq 'norequest') {
 1795:                     $val = 0;
 1796:                 }
 1797:                 if ($option eq 'validate') {
 1798:                     my $canvalidate = 0;
 1799:                     if (ref($validations{$item}) eq 'HASH') {
 1800:                         if ($validations{$item}{'default'}) {
 1801:                             $canvalidate = 1;
 1802:                         }
 1803:                     }
 1804:                     next if (!$canvalidate);
 1805:                 }
 1806:                 my $checked = '';
 1807:                 if ($option eq $curroption) {
 1808:                     $checked = ' checked="checked"';
 1809:                 } elsif ($option eq 'autolimit') {
 1810:                     if ($curroption =~ /^autolimit/) {
 1811:                         $checked = ' checked="checked"';
 1812:                     }
 1813:                 }
 1814:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1815:                                   '<input type="radio" name="crsreq_'.$item.
 1816:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1817:                                   $titles{$option}.'</label>';
 1818:                 if ($option eq 'autolimit') {
 1819:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1820:                                        $item.'_limit_default" size="1" '.
 1821:                                        'value="'.$currlimit.'" />';
 1822:                 }
 1823:                 $defcell{$item} .= '</span> ';
 1824:                 if ($option eq 'autolimit') {
 1825:                     $defcell{$item} .= $titles{'unlimited'};
 1826:                 }
 1827:             }
 1828:         } elsif ($context eq 'requestauthor') {
 1829:             my $curroption;
 1830:             if (ref($settings) eq 'HASH') {
 1831:                 $curroption = $settings->{'default'};
 1832:             }
 1833:             if (!$curroption) {
 1834:                 $curroption = 'norequest';
 1835:             }
 1836:             foreach my $option (@options) {
 1837:                 my $val = $option;
 1838:                 if ($option eq 'norequest') {
 1839:                     $val = 0;
 1840:                 }
 1841:                 my $checked = '';
 1842:                 if ($option eq $curroption) {
 1843:                     $checked = ' checked="checked"';
 1844:                 }
 1845:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1846:                               '<input type="radio" name="authorreq_default"'.
 1847:                               ' value="'.$val.'"'.$checked.' />'.
 1848:                               $titles{$option}.'</label></span>&nbsp; ';
 1849:             }
 1850:         } else {
 1851:             my $checked = 'checked="checked" ';
 1852:             if (ref($settings) eq 'HASH') {
 1853:                 if (ref($settings->{$item}) eq 'HASH') {
 1854:                     if ($settings->{$item}->{'default'} == 0) {
 1855:                         $checked = '';
 1856:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1857:                         $checked = 'checked="checked" ';
 1858:                     }
 1859:                 }
 1860:             }
 1861:             $datatable .= '<span class="LC_nobreak"><label>'.
 1862:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1863:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1864:                           '</label></span>&nbsp; ';
 1865:         }
 1866:     }
 1867:     if ($context eq 'requestcourses') {
 1868:         $datatable .= '</tr><tr>';
 1869:         foreach my $item (@usertools) {
 1870:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1871:         }
 1872:         $datatable .= '</tr></table>';
 1873:     }
 1874:     $datatable .= '</td>';
 1875:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1876:         $datatable .= '<td class="LC_right_item">'.
 1877:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1878:                       '<input type="text" name="defaultquota" value="'.
 1879:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 1880:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1881:                       '<input type="text" name="authorquota" value="'.
 1882:                       $authorquota.'" size="5" /></span></td>';
 1883:     }
 1884:     $datatable .= '</tr>';
 1885:     $typecount ++;
 1886:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1887:     $datatable .= '<tr'.$css_class.'>'.
 1888:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 1889:     if ($context eq 'requestcourses') {
 1890:         $datatable .= &mt('(overrides affiliation, if set)').
 1891:                       '</td>'.
 1892:                       '<td class="LC_left_item">'.
 1893:                       '<table><tr>';
 1894:     } else {
 1895:         $datatable .= &mt('(overrides affiliation, if checked)').
 1896:                       '</td>'.
 1897:                       '<td class="LC_left_item" colspan="2">'.
 1898:                       '<br />';
 1899:     }
 1900:     my %advcell;
 1901:     foreach my $item (@usertools) {
 1902:         if ($context eq 'requestcourses') {
 1903:             my ($curroption,$currlimit);
 1904:             if (ref($settings) eq 'HASH') {
 1905:                 if (ref($settings->{$item}) eq 'HASH') {
 1906:                     $curroption = $settings->{$item}->{'_LC_adv'};
 1907:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1908:                         $currlimit = $1;
 1909:                     }
 1910:                 }
 1911:             }
 1912:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1913:             my $checked = '';
 1914:             if ($curroption eq '') {
 1915:                 $checked = ' checked="checked"';
 1916:             }
 1917:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1918:                                '<input type="radio" name="crsreq_'.$item.
 1919:                                '__LC_adv" value=""'.$checked.' />'.
 1920:                                &mt('No override set').'</label></span>&nbsp; ';
 1921:             foreach my $option (@options) {
 1922:                 my $val = $option;
 1923:                 if ($option eq 'norequest') {
 1924:                     $val = 0;
 1925:                 }
 1926:                 if ($option eq 'validate') {
 1927:                     my $canvalidate = 0;
 1928:                     if (ref($validations{$item}) eq 'HASH') {
 1929:                         if ($validations{$item}{'_LC_adv'}) {
 1930:                             $canvalidate = 1;
 1931:                         }
 1932:                     }
 1933:                     next if (!$canvalidate);
 1934:                 }
 1935:                 my $checked = '';
 1936:                 if ($val eq $curroption) {
 1937:                     $checked = ' checked="checked"';
 1938:                 } elsif ($option eq 'autolimit') {
 1939:                     if ($curroption =~ /^autolimit/) {
 1940:                         $checked = ' checked="checked"';
 1941:                     }
 1942:                 }
 1943:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1944:                                   '<input type="radio" name="crsreq_'.$item.
 1945:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1946:                                   $titles{$option}.'</label>';
 1947:                 if ($option eq 'autolimit') {
 1948:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1949:                                        $item.'_limit__LC_adv" size="1" '.
 1950:                                        'value="'.$currlimit.'" />';
 1951:                 }
 1952:                 $advcell{$item} .= '</span> ';
 1953:                 if ($option eq 'autolimit') {
 1954:                     $advcell{$item} .= $titles{'unlimited'};
 1955:                 }
 1956:             }
 1957:         } elsif ($context eq 'requestauthor') {
 1958:             my $curroption;
 1959:             if (ref($settings) eq 'HASH') {
 1960:                 $curroption = $settings->{'_LC_adv'};
 1961:             }
 1962:             my $checked = '';
 1963:             if ($curroption eq '') {
 1964:                 $checked = ' checked="checked"';
 1965:             }
 1966:             $datatable .= '<span class="LC_nobreak"><label>'.
 1967:                           '<input type="radio" name="authorreq__LC_adv"'.
 1968:                           ' value=""'.$checked.' />'.
 1969:                           &mt('No override set').'</label></span>&nbsp; ';
 1970:             foreach my $option (@options) {
 1971:                 my $val = $option;
 1972:                 if ($option eq 'norequest') {
 1973:                     $val = 0;
 1974:                 }
 1975:                 my $checked = '';
 1976:                 if ($val eq $curroption) {
 1977:                     $checked = ' checked="checked"';
 1978:                 }
 1979:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1980:                               '<input type="radio" name="authorreq__LC_adv"'.
 1981:                               ' value="'.$val.'"'.$checked.' />'.
 1982:                               $titles{$option}.'</label></span>&nbsp; ';
 1983:             }
 1984:         } else {
 1985:             my $checked = 'checked="checked" ';
 1986:             if (ref($settings) eq 'HASH') {
 1987:                 if (ref($settings->{$item}) eq 'HASH') {
 1988:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 1989:                         $checked = '';
 1990:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 1991:                         $checked = 'checked="checked" ';
 1992:                     }
 1993:                 }
 1994:             }
 1995:             $datatable .= '<span class="LC_nobreak"><label>'.
 1996:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1997:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 1998:                           '</label></span>&nbsp; ';
 1999:         }
 2000:     }
 2001:     if ($context eq 'requestcourses') {
 2002:         $datatable .= '</tr><tr>';
 2003:         foreach my $item (@usertools) {
 2004:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2005:         }
 2006:         $datatable .= '</tr></table>';
 2007:     }
 2008:     $datatable .= '</td></tr>';
 2009:     $$rowtotal += $typecount;
 2010:     return $datatable;
 2011: }
 2012: 
 2013: sub print_requestmail {
 2014:     my ($dom,$action,$settings,$rowtotal) = @_;
 2015:     my ($now,$datatable,%currapp);
 2016:     $now = time;
 2017:     if (ref($settings) eq 'HASH') {
 2018:         if (ref($settings->{'notify'}) eq 'HASH') {
 2019:             if ($settings->{'notify'}{'approval'} ne '') {
 2020:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2021:             }
 2022:         }
 2023:     }
 2024:     my $numinrow = 2;
 2025:     my $css_class;
 2026:     $css_class = ($$rowtotal%2? ' class="LC_odd_row"':'');
 2027:     my $text;
 2028:     if ($action eq 'requestcourses') {
 2029:         $text = &mt('Receive notification of course requests requiring approval');
 2030:     } elsif ($action eq 'requestauthor') {
 2031:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2032:     } else {
 2033:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2034:     }
 2035:     $datatable = '<tr'.$css_class.'>'.
 2036:                  ' <td>'.$text.'</td>'.
 2037:                  ' <td class="LC_left_item">';
 2038:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2039:                                                  $action.'notifyapproval',%currapp);
 2040:     if ($numdc > 0) {
 2041:         $datatable .= $table;
 2042:     } else {
 2043:         $datatable .= &mt('There are no active Domain Coordinators');
 2044:     }
 2045:     $datatable .='</td></tr>';
 2046:     $$rowtotal += $rows;
 2047:     return $datatable;
 2048: }
 2049: 
 2050: sub print_studentcode {
 2051:     my ($settings,$rowtotal) = @_;
 2052:     my $rownum = 0; 
 2053:     my ($output,%current);
 2054:     my @crstypes = ('official','unofficial','community','textbook');
 2055:     if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2056:         foreach my $type (@crstypes) {
 2057:             $current{$type} = $settings->{'uniquecode'}{$type};
 2058:         }
 2059:     }
 2060:     $output .= '<tr>'.
 2061:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2062:                '<td class="LC_left_item">';
 2063:     foreach my $type (@crstypes) {
 2064:         my $check = ' ';
 2065:         if ($current{$type}) {
 2066:             $check = ' checked="checked" ';
 2067:         }
 2068:         $output .= '<span class="LC_nobreak"><label>'.
 2069:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2070:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2071:     }
 2072:     $output .= '</td></tr>';
 2073:     $$rowtotal ++;
 2074:     return $output;
 2075: }
 2076: 
 2077: sub print_textbookcourses {
 2078:     my ($dom,$settings,$rowtotal) = @_;
 2079:     my $rownum = 0;
 2080:     my $css_class;
 2081:     my $itemcount = 1;
 2082:     my $maxnum = 0;
 2083:     my $bookshash;
 2084:     if (ref($settings) eq 'HASH') {
 2085:         $bookshash = $settings->{'textbooks'};
 2086:     }
 2087:     my %ordered;
 2088:     if (ref($bookshash) eq 'HASH') {
 2089:         foreach my $item (keys(%{$bookshash})) {
 2090:             if (ref($bookshash->{$item}) eq 'HASH') {
 2091:                 my $num = $bookshash->{$item}{'order'};
 2092:                 $ordered{$num} = $item;
 2093:             }
 2094:         }
 2095:     }
 2096:     my $confname = $dom.'-domainconfig';
 2097:     my $switchserver = &check_switchserver($dom,$confname);
 2098:     $maxnum = scalar(keys(%ordered));
 2099:     my $datatable = &textbookcourses_javascript(\%ordered);
 2100:     if (keys(%ordered)) {
 2101:         my @items = sort { $a <=> $b } keys(%ordered);
 2102:         for (my $i=0; $i<@items; $i++) {
 2103:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2104:             my $key = $ordered{$items[$i]};
 2105:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2106:             my $coursetitle = $coursehash{'description'};
 2107:             my ($subject,$title,$author,$image,$imgsrc,$cdom,$cnum);
 2108:             if (ref($bookshash->{$key}) eq 'HASH') {
 2109:                 $subject = $bookshash->{$key}->{'subject'};
 2110:                 $title = $bookshash->{$key}->{'title'};
 2111:                 $author = $bookshash->{$key}->{'author'};
 2112:                 $image = $bookshash->{$key}->{'image'};
 2113:                 if ($image ne '') {
 2114:                     my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2115:                     my $imagethumb = "$path/tn-".$imagefile;
 2116:                     $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2117:                 }
 2118:             }
 2119:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$key'".');"';
 2120:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2121:                          .'<select name="'.$key.'"'.$chgstr.'>';
 2122:             for (my $k=0; $k<=$maxnum; $k++) {
 2123:                 my $vpos = $k+1;
 2124:                 my $selstr;
 2125:                 if ($k == $i) {
 2126:                     $selstr = ' selected="selected" ';
 2127:                 }
 2128:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2129:             }
 2130:             $datatable .= '</select>'.('&nbsp;'x2).
 2131:                 '<label><input type="checkbox" name="book_del" value="'.$key.'" />'.
 2132:                 &mt('Delete?').'</label></span></td>'.
 2133:                 '<td colspan="2">'.
 2134:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="book_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2135:                 ('&nbsp;'x2).
 2136:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="book_title_'.$i.'" value="'.$title.'" /></span> '.
 2137:                 ('&nbsp;'x2).
 2138:                 '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="book_author_'.$i.'" value="'.$author.'" /></span> '.
 2139:                 ('&nbsp;'x2).
 2140:                 '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2141:             if ($image) {
 2142:                 $datatable .= '<span class="LC_nobreak">'.
 2143:                               $imgsrc.
 2144:                               '<label><input type="checkbox" name="book_image_del"'.
 2145:                               ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2146:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2147:             }
 2148:             if ($switchserver) {
 2149:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2150:             } else {
 2151:                 $datatable .= '<input type="file" name="book_image_'.$i.'" value="" />';
 2152:             }
 2153:             $datatable .= '<input type="hidden" name="book_id_'.$i.'" value="'.$key.'" /></span> '.
 2154:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2155:                           $coursetitle.'</span></td></tr>'."\n";
 2156:             $itemcount ++;
 2157:         }
 2158:     }
 2159:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2160:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'addbook_pos'".');"';
 2161:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2162:                   '<input type="hidden" name="book_maxnum" value="'.$maxnum.'" />'."\n".
 2163:                   '<select name="addbook_pos"'.$chgstr.'>';
 2164:     for (my $k=0; $k<$maxnum+1; $k++) {
 2165:         my $vpos = $k+1;
 2166:         my $selstr;
 2167:         if ($k == $maxnum) {
 2168:             $selstr = ' selected="selected" ';
 2169:         }
 2170:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2171:     }
 2172:     $datatable .= '</select>&nbsp;'."\n".
 2173:                   '<input type="checkbox" name="addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2174:                   '<td colspan="2">'.
 2175:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="addbook_subject" value="" /></span> '."\n".
 2176:                   ('&nbsp;'x2).
 2177:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="addbook_title" value="" /></span> '."\n".
 2178:                   ('&nbsp;'x2).
 2179:                   '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="addbook_author" value="" /></span> '."\n".
 2180:                   ('&nbsp;'x2).
 2181:                   '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2182:     if ($switchserver) {
 2183:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2184:     } else {
 2185:         $datatable .= '<input type="file" name="addbook_image" value="" />';
 2186:     }
 2187:     $datatable .= '</span>'."\n".
 2188:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2189:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},'addbook_cdom').
 2190:                   '<input type="text" size="25" name="addbook_cnum" value="" />'.
 2191:                   &Apache::loncommon::selectcourse_link
 2192:                       ('display','addbook_cnum','addbook_cdom',undef,undef,undef,'Course');
 2193:                   '</span></td>'."\n".
 2194:                   '</tr>'."\n";
 2195:     $itemcount ++;
 2196:     return $datatable;
 2197: }
 2198: 
 2199: sub textbookcourses_javascript {
 2200:     my ($textbooks) = @_;
 2201:     return unless(ref($textbooks) eq 'HASH');
 2202:     my $num = scalar(keys(%{$textbooks}));
 2203:     my @jsarray;
 2204:     foreach my $item (sort {$a <=> $b } (keys(%{$textbooks}))) {
 2205:         push(@jsarray,$textbooks->{$item});
 2206:     }
 2207:     my $jstext = '    var textbooks = Array('."'".join("','",@jsarray)."'".');'."\n";
 2208:     return <<"ENDSCRIPT";
 2209: <script type="text/javascript">
 2210: // <![CDATA[
 2211: function reorderBooks(form,item) {
 2212:     var changedVal;
 2213: $jstext 
 2214:     var newpos = 'addbook_pos';
 2215:     var current = new Array;
 2216:     var maxh = 1 + $num;
 2217:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2218:     if (item == newpos) {
 2219:         changedVal = newitemVal;
 2220:     } else {
 2221:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2222:         current[newitemVal] = newpos;
 2223:     }
 2224:     for (var i=0; i<textbooks.length; i++) {
 2225:         var elementName = textbooks[i];
 2226:         if (elementName != item) {
 2227:             if (form.elements[elementName]) {
 2228:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2229:                 current[currVal] = elementName;
 2230:             }
 2231:         }
 2232:     }
 2233:     var oldVal;
 2234:     for (var j=0; j<maxh; j++) {
 2235:         if (current[j] == undefined) {
 2236:             oldVal = j;
 2237:         }
 2238:     }
 2239:     if (oldVal < changedVal) {
 2240:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2241:            var elementName = current[k];
 2242:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2243:         }
 2244:     } else {
 2245:         for (var k=changedVal; k<oldVal; k++) {
 2246:             var elementName = current[k];
 2247:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2248:         }
 2249:     }
 2250:     return;
 2251: }
 2252: 
 2253: // ]]>
 2254: </script>
 2255: 
 2256: ENDSCRIPT
 2257: }
 2258: 
 2259: sub print_autoenroll {
 2260:     my ($dom,$settings,$rowtotal) = @_;
 2261:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2262:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 2263:     if (ref($settings) eq 'HASH') {
 2264:         if (exists($settings->{'run'})) {
 2265:             if ($settings->{'run'} eq '0') {
 2266:                 $runoff = ' checked="checked" ';
 2267:                 $runon = ' ';
 2268:             } else {
 2269:                 $runon = ' checked="checked" ';
 2270:                 $runoff = ' ';
 2271:             }
 2272:         } else {
 2273:             if ($autorun) {
 2274:                 $runon = ' checked="checked" ';
 2275:                 $runoff = ' ';
 2276:             } else {
 2277:                 $runoff = ' checked="checked" ';
 2278:                 $runon = ' ';
 2279:             }
 2280:         }
 2281:         if (exists($settings->{'co-owners'})) {
 2282:             if ($settings->{'co-owners'} eq '0') {
 2283:                 $coownersoff = ' checked="checked" ';
 2284:                 $coownerson = ' ';
 2285:             } else {
 2286:                 $coownerson = ' checked="checked" ';
 2287:                 $coownersoff = ' ';
 2288:             }
 2289:         } else {
 2290:             $coownersoff = ' checked="checked" ';
 2291:             $coownerson = ' ';
 2292:         }
 2293:         if (exists($settings->{'sender_domain'})) {
 2294:             $defdom = $settings->{'sender_domain'};
 2295:         }
 2296:     } else {
 2297:         if ($autorun) {
 2298:             $runon = ' checked="checked" ';
 2299:             $runoff = ' ';
 2300:         } else {
 2301:             $runoff = ' checked="checked" ';
 2302:             $runon = ' ';
 2303:         }
 2304:     }
 2305:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2306:     my $notif_sender;
 2307:     if (ref($settings) eq 'HASH') {
 2308:         $notif_sender = $settings->{'sender_uname'};
 2309:     }
 2310:     my $datatable='<tr class="LC_odd_row">'.
 2311:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2312:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2313:                   '<input type="radio" name="autoenroll_run"'.
 2314:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2315:                   '<label><input type="radio" name="autoenroll_run"'.
 2316:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2317:                   '</tr><tr>'.
 2318:                   '<td>'.&mt('Notification messages - sender').
 2319:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2320:                   &mt('username').':&nbsp;'.
 2321:                   '<input type="text" name="sender_uname" value="'.
 2322:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2323:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2324:                   '<tr class="LC_odd_row">'.
 2325:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2326:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2327:                   '<input type="radio" name="autoassign_coowners"'.
 2328:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2329:                   '<label><input type="radio" name="autoassign_coowners"'.
 2330:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2331:                   '</tr>';
 2332:     $$rowtotal += 3;
 2333:     return $datatable;
 2334: }
 2335: 
 2336: sub print_autoupdate {
 2337:     my ($position,$dom,$settings,$rowtotal) = @_;
 2338:     my $datatable;
 2339:     if ($position eq 'top') {
 2340:         my $updateon = ' ';
 2341:         my $updateoff = ' checked="checked" ';
 2342:         my $classlistson = ' ';
 2343:         my $classlistsoff = ' checked="checked" ';
 2344:         if (ref($settings) eq 'HASH') {
 2345:             if ($settings->{'run'} eq '1') {
 2346:                 $updateon = $updateoff;
 2347:                 $updateoff = ' ';
 2348:             }
 2349:             if ($settings->{'classlists'} eq '1') {
 2350:                 $classlistson = $classlistsoff;
 2351:                 $classlistsoff = ' ';
 2352:             }
 2353:         }
 2354:         my %title = (
 2355:                    run => 'Auto-update active?',
 2356:                    classlists => 'Update information in classlists?',
 2357:                     );
 2358:         $datatable = '<tr class="LC_odd_row">'. 
 2359:                   '<td>'.&mt($title{'run'}).'</td>'.
 2360:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2361:                   '<input type="radio" name="autoupdate_run"'.
 2362:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2363:                   '<label><input type="radio" name="autoupdate_run"'.
 2364:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2365:                   '</tr><tr>'.
 2366:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2367:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2368:                   '<label><input type="radio" name="classlists"'.
 2369:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2370:                   '<label><input type="radio" name="classlists"'.
 2371:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2372:                   '</tr>';
 2373:         $$rowtotal += 2;
 2374:     } elsif ($position eq 'middle') {
 2375:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2376:         my $numinrow = 3;
 2377:         my $locknamesettings;
 2378:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2379:                                      $dom,$numinrow,$othertitle,
 2380:                                     'lockablenames');
 2381:         $$rowtotal ++;
 2382:     } else {
 2383:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2384:         my @fields = ('lastname','firstname','middlename','generation',
 2385:                       'permanentemail','id');
 2386:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2387:         my $numrows = 0;
 2388:         if (ref($types) eq 'ARRAY') {
 2389:             if (@{$types} > 0) {
 2390:                 $datatable = 
 2391:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2392:                                          \@fields,$types,\$numrows);
 2393:                     $$rowtotal += @{$types}; 
 2394:             }
 2395:         }
 2396:         $datatable .= 
 2397:             &usertype_update_row($settings,{'default' => $othertitle},
 2398:                                  \%fieldtitles,\@fields,['default'],
 2399:                                  \$numrows);
 2400:         $$rowtotal ++;     
 2401:     }
 2402:     return $datatable;
 2403: }
 2404: 
 2405: sub print_autocreate {
 2406:     my ($dom,$settings,$rowtotal) = @_;
 2407:     my (%createon,%createoff,%currhash);
 2408:     my @types = ('xml','req');
 2409:     if (ref($settings) eq 'HASH') {
 2410:         foreach my $item (@types) {
 2411:             $createoff{$item} = ' checked="checked" ';
 2412:             $createon{$item} = ' ';
 2413:             if (exists($settings->{$item})) {
 2414:                 if ($settings->{$item}) {
 2415:                     $createon{$item} = ' checked="checked" ';
 2416:                     $createoff{$item} = ' ';
 2417:                 }
 2418:             }
 2419:         }
 2420:         if ($settings->{'xmldc'} ne '') {
 2421:             $currhash{$settings->{'xmldc'}} = 1;
 2422:         }
 2423:     } else {
 2424:         foreach my $item (@types) {
 2425:             $createoff{$item} = ' checked="checked" ';
 2426:             $createon{$item} = ' ';
 2427:         }
 2428:     }
 2429:     $$rowtotal += 2;
 2430:     my $numinrow = 2;
 2431:     my $datatable='<tr class="LC_odd_row">'.
 2432:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2433:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2434:                   '<input type="radio" name="autocreate_xml"'.
 2435:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2436:                   '<label><input type="radio" name="autocreate_xml"'.
 2437:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2438:                   '</td></tr><tr>'.
 2439:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2440:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2441:                   '<input type="radio" name="autocreate_req"'.
 2442:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2443:                   '<label><input type="radio" name="autocreate_req"'.
 2444:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2445:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2446:                                                    'autocreate_xmldc',%currhash);
 2447:     if ($numdc > 1) {
 2448:         $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 2449:                       &mt('Course creation processed as: (choose Dom. Coord.)').
 2450:                       '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 2451:     } else {
 2452:         $datatable .= $dctable.'</td></tr>';
 2453:     }
 2454:     $$rowtotal += $rows;
 2455:     return $datatable;
 2456: }
 2457: 
 2458: sub print_directorysrch {
 2459:     my ($dom,$settings,$rowtotal) = @_;
 2460:     my $srchon = ' ';
 2461:     my $srchoff = ' checked="checked" ';
 2462:     my ($exacton,$containson,$beginson);
 2463:     my $localon = ' ';
 2464:     my $localoff = ' checked="checked" ';
 2465:     if (ref($settings) eq 'HASH') {
 2466:         if ($settings->{'available'} eq '1') {
 2467:             $srchon = $srchoff;
 2468:             $srchoff = ' ';
 2469:         }
 2470:         if ($settings->{'localonly'} eq '1') {
 2471:             $localon = $localoff;
 2472:             $localoff = ' ';
 2473:         }
 2474:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2475:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2476:                 if ($type eq 'exact') {
 2477:                     $exacton = ' checked="checked" ';
 2478:                 } elsif ($type eq 'contains') {
 2479:                     $containson = ' checked="checked" ';
 2480:                 } elsif ($type eq 'begins') {
 2481:                     $beginson = ' checked="checked" ';
 2482:                 }
 2483:             }
 2484:         } else {
 2485:             if ($settings->{'searchtypes'} eq 'exact') {
 2486:                 $exacton = ' checked="checked" ';
 2487:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2488:                 $containson = ' checked="checked" ';
 2489:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2490:                 $exacton = ' checked="checked" ';
 2491:                 $containson = ' checked="checked" ';
 2492:             }
 2493:         }
 2494:     }
 2495:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2496:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2497: 
 2498:     my $numinrow = 4;
 2499:     my $cansrchrow = 0;
 2500:     my $datatable='<tr class="LC_odd_row">'.
 2501:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2502:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2503:                   '<input type="radio" name="dirsrch_available"'.
 2504:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2505:                   '<label><input type="radio" name="dirsrch_available"'.
 2506:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2507:                   '</tr><tr>'.
 2508:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2509:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2510:                   '<input type="radio" name="dirsrch_localonly"'.
 2511:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2512:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2513:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2514:                   '</tr>';
 2515:     $$rowtotal += 2;
 2516:     if (ref($usertypes) eq 'HASH') {
 2517:         if (keys(%{$usertypes}) > 0) {
 2518:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2519:                                          $numinrow,$othertitle,'cansearch');
 2520:             $cansrchrow = 1;
 2521:         }
 2522:     }
 2523:     if ($cansrchrow) {
 2524:         $$rowtotal ++;
 2525:         $datatable .= '<tr>';
 2526:     } else {
 2527:         $datatable .= '<tr class="LC_odd_row">';
 2528:     }
 2529:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2530:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2531:     foreach my $title (@{$titleorder}) {
 2532:         if (defined($searchtitles->{$title})) {
 2533:             my $check = ' ';
 2534:             if (ref($settings) eq 'HASH') {
 2535:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2536:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2537:                         $check = ' checked="checked" ';
 2538:                     }
 2539:                 }
 2540:             }
 2541:             $datatable .= '<td class="LC_left_item">'.
 2542:                           '<span class="LC_nobreak"><label>'.
 2543:                           '<input type="checkbox" name="searchby" '.
 2544:                           'value="'.$title.'"'.$check.'/>'.
 2545:                           $searchtitles->{$title}.'</label></span></td>';
 2546:         }
 2547:     }
 2548:     $datatable .= '</tr></table></td></tr>';
 2549:     $$rowtotal ++;
 2550:     if ($cansrchrow) {
 2551:         $datatable .= '<tr class="LC_odd_row">';
 2552:     } else {
 2553:         $datatable .= '<tr>';
 2554:     }
 2555:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2556:                   '<td class="LC_left_item" colspan="2">'.
 2557:                   '<span class="LC_nobreak"><label>'.
 2558:                   '<input type="checkbox" name="searchtypes" '.
 2559:                   $exacton.' value="exact" />'.&mt('Exact match').
 2560:                   '</label>&nbsp;'.
 2561:                   '<label><input type="checkbox" name="searchtypes" '.
 2562:                   $beginson.' value="begins" />'.&mt('Begins with').
 2563:                   '</label>&nbsp;'.
 2564:                   '<label><input type="checkbox" name="searchtypes" '.
 2565:                   $containson.' value="contains" />'.&mt('Contains').
 2566:                   '</label></span></td></tr>';
 2567:     $$rowtotal ++;
 2568:     return $datatable;
 2569: }
 2570: 
 2571: sub print_contacts {
 2572:     my ($dom,$settings,$rowtotal) = @_;
 2573:     my $datatable;
 2574:     my @contacts = ('adminemail','supportemail');
 2575:     my (%checked,%to,%otheremails,%bccemails);
 2576:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2577:                     'requestsmail','updatesmail','idconflictsmail');
 2578:     foreach my $type (@mailings) {
 2579:         $otheremails{$type} = '';
 2580:     }
 2581:     $bccemails{'helpdeskmail'} = '';
 2582:     if (ref($settings) eq 'HASH') {
 2583:         foreach my $item (@contacts) {
 2584:             if (exists($settings->{$item})) {
 2585:                 $to{$item} = $settings->{$item};
 2586:             }
 2587:         }
 2588:         foreach my $type (@mailings) {
 2589:             if (exists($settings->{$type})) {
 2590:                 if (ref($settings->{$type}) eq 'HASH') {
 2591:                     foreach my $item (@contacts) {
 2592:                         if ($settings->{$type}{$item}) {
 2593:                             $checked{$type}{$item} = ' checked="checked" ';
 2594:                         }
 2595:                     }
 2596:                     $otheremails{$type} = $settings->{$type}{'others'};
 2597:                     if ($type eq 'helpdeskmail') {
 2598:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2599:                     }
 2600:                 }
 2601:             } elsif ($type eq 'lonstatusmail') {
 2602:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2603:             }
 2604:         }
 2605:     } else {
 2606:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2607:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2608:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2609:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2610:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2611:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2612:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2613:         $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2614:         $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 2615:     }
 2616:     my ($titles,$short_titles) = &contact_titles();
 2617:     my $rownum = 0;
 2618:     my $css_class;
 2619:     foreach my $item (@contacts) {
 2620:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2621:         $datatable .= '<tr'.$css_class.'>'. 
 2622:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2623:                   '</span></td><td class="LC_right_item">'.
 2624:                   '<input type="text" name="'.$item.'" value="'.
 2625:                   $to{$item}.'" /></td></tr>';
 2626:         $rownum ++;
 2627:     }
 2628:     foreach my $type (@mailings) {
 2629:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2630:         $datatable .= '<tr'.$css_class.'>'.
 2631:                       '<td><span class="LC_nobreak">'.
 2632:                       $titles->{$type}.': </span></td>'.
 2633:                       '<td class="LC_left_item">'.
 2634:                       '<span class="LC_nobreak">';
 2635:         foreach my $item (@contacts) {
 2636:             $datatable .= '<label>'.
 2637:                           '<input type="checkbox" name="'.$type.'"'.
 2638:                           $checked{$type}{$item}.
 2639:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2640:                           '</label>&nbsp;';
 2641:         }
 2642:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2643:                       '<input type="text" name="'.$type.'_others" '.
 2644:                       'value="'.$otheremails{$type}.'"  />';
 2645:         if ($type eq 'helpdeskmail') {
 2646:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2647:                           '<input type="text" name="'.$type.'_bcc" '.
 2648:                           'value="'.$bccemails{$type}.'"  />';
 2649:         }
 2650:         $datatable .= '</td></tr>'."\n";
 2651:         $rownum ++;
 2652:     }
 2653:     my %choices;
 2654:     $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 2655:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2656:                                    &mt('LON-CAPA core group - MSU'),600,500));
 2657:     $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 2658:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2659:                                     &mt('LON-CAPA core group - MSU'),600,500));
 2660:     my @toggles = ('reporterrors','reportupdates');
 2661:     my %defaultchecked = ('reporterrors'  => 'on',
 2662:                           'reportupdates' => 'on');
 2663:     (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2664:                                                \%choices,$rownum);
 2665:     $datatable .= $reports;
 2666:     $$rowtotal += $rownum;
 2667:     return $datatable;
 2668: }
 2669: 
 2670: sub print_helpsettings {
 2671:     my ($dom,$confname,$settings,$rowtotal) = @_;
 2672:     my ($datatable,$itemcount);
 2673:     $itemcount = 1;
 2674:     my (%choices,%defaultchecked,@toggles);
 2675:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2676:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2677:                                  &mt('LON-CAPA bug tracker'),600,500));
 2678:     %defaultchecked = ('submitbugs' => 'on');
 2679:     @toggles = ('submitbugs',);
 2680: 
 2681:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2682:                                                  \%choices,$itemcount);
 2683:     return $datatable;
 2684: }
 2685: 
 2686: sub radiobutton_prefs {
 2687:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 2688:         $additional) = @_;
 2689:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2690:                    (ref($choices) eq 'HASH'));
 2691: 
 2692:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2693: 
 2694:     foreach my $item (@{$toggles}) {
 2695:         if ($defaultchecked->{$item} eq 'on') {
 2696:             $checkedon{$item} = ' checked="checked" ';
 2697:             $checkedoff{$item} = ' ';
 2698:         } elsif ($defaultchecked->{$item} eq 'off') {
 2699:             $checkedoff{$item} = ' checked="checked" ';
 2700:             $checkedon{$item} = ' ';
 2701:         }
 2702:     }
 2703:     if (ref($settings) eq 'HASH') {
 2704:         foreach my $item (@{$toggles}) {
 2705:             if ($settings->{$item} eq '1') {
 2706:                 $checkedon{$item} =  ' checked="checked" ';
 2707:                 $checkedoff{$item} = ' ';
 2708:             } elsif ($settings->{$item} eq '0') {
 2709:                 $checkedoff{$item} =  ' checked="checked" ';
 2710:                 $checkedon{$item} = ' ';
 2711:             }
 2712:         }
 2713:     }
 2714:     if ($onclick) {
 2715:         $onclick = ' onclick="'.$onclick.'"';
 2716:     }
 2717:     foreach my $item (@{$toggles}) {
 2718:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2719:         $datatable .=
 2720:             '<tr'.$css_class.'><td valign="top">'.
 2721:             '<span class="LC_nobreak">'.$choices->{$item}.
 2722:             '</span></td>'.
 2723:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2724:             '<label><input type="radio" name="'.
 2725:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 2726:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2727:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 2728:             '</span>'.$additional.
 2729:             '</td>'.
 2730:             '</tr>';
 2731:         $itemcount ++;
 2732:     }
 2733:     return ($datatable,$itemcount);
 2734: }
 2735: 
 2736: sub print_coursedefaults {
 2737:     my ($position,$dom,$settings,$rowtotal) = @_;
 2738:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 2739:     my $itemcount = 1;
 2740:     my %choices =  &Apache::lonlocal::texthash (
 2741:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 2742:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 2743:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2744:         coursecredits        => 'Credits can be specified for courses',
 2745:     );
 2746:     my %staticdefaults = (
 2747:                            anonsurvey_threshold => 10,
 2748:                            uploadquota          => 500,
 2749:                          );
 2750:     if ($position eq 'top') {
 2751:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2752:         @toggles = ('canuse_pdfforms');
 2753:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2754:                                                  \%choices,$itemcount);
 2755:     } else {
 2756:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2757:         my ($currdefresponder,$def_official_credits,$def_unofficial_credits,$def_textbook_credits,
 2758:             %curruploadquota);
 2759:         my $currusecredits = 0;
 2760:         my @types = ('official','unofficial','community','textbook');
 2761:         if (ref($settings) eq 'HASH') {
 2762:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2763:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 2764:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 2765:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 2766:                 }
 2767:             }
 2768:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 2769:                 $def_official_credits = $settings->{'coursecredits'}->{'official'};
 2770:                 $def_unofficial_credits = $settings->{'coursecredits'}->{'unofficial'};
 2771:                 $def_textbook_credits = $settings->{'coursecredits'}->{'textbook'};
 2772:                 if (($def_official_credits ne '') || ($def_unofficial_credits ne '') ||
 2773:                     ($def_textbook_credits ne '')) {
 2774:                     $currusecredits = 1;
 2775:                 }
 2776:             }
 2777:         }
 2778:         if (!$currdefresponder) {
 2779:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 2780:         } elsif ($currdefresponder < 1) {
 2781:             $currdefresponder = 1;
 2782:         }
 2783:         foreach my $type (@types) {
 2784:             if ($curruploadquota{$type} eq '') {
 2785:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 2786:             }
 2787:         }
 2788:         $datatable .=
 2789:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2790:                 $choices{'anonsurvey_threshold'}.
 2791:                 '</span></td>'.
 2792:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2793:                 '<input type="text" name="anonsurvey_threshold"'.
 2794:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2795:                 '</td></tr>'."\n";
 2796:         $itemcount ++;
 2797:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2798:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2799:                       $choices{'uploadquota'}.
 2800:                       '</span></td>'.
 2801:                       '<td align="right" class="LC_right_item">'.
 2802:                       '<table><tr>';
 2803:         foreach my $type (@types) {
 2804:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 2805:                            '<input type="text" name="uploadquota_'.$type.'"'.
 2806:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 2807:         }
 2808:         $datatable .= '</tr></table></td></tr>'."\n";
 2809:         $itemcount ++;
 2810:         my $onclick = "toggleDisplay(this.form,'credits');";
 2811:         my $display = 'none';
 2812:         if ($currusecredits) {
 2813:             $display = 'block';
 2814:         }
 2815:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 2816:                          '<span class="LC_nobreak">'.
 2817:                          &mt('Default credits for official courses [_1]',
 2818:                          '<input type="text" name="official_credits" value="'.
 2819:                          $def_official_credits.'" size="3" />').
 2820:                          '</span><br />'.
 2821:                          '<span class="LC_nobreak">'.
 2822:                          &mt('Default credits for unofficial courses [_1]',
 2823:                          '<input type="text" name="unofficial_credits" value="'.
 2824:                          $def_unofficial_credits.'" size="3" />').
 2825:                          '</span><br />'.
 2826:                          '<span class="LC_nobreak">'.
 2827:                          &mt('Default credits for textbook courses [_1]',
 2828:                          '<input type="text" name="textbook_credits" value="'.
 2829:                          $def_textbook_credits.'" size="3" />').
 2830:                          '</span></div>'."\n";
 2831:         %defaultchecked = ('coursecredits' => 'off');
 2832:         @toggles = ('coursecredits');
 2833:         my $current = {
 2834:                         'coursecredits' => $currusecredits,
 2835:                       };
 2836:         (my $table,$itemcount) =
 2837:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 2838:                                \%choices,$itemcount,$onclick,$additional);
 2839:         $datatable .= $table;
 2840:         $itemcount ++;
 2841:     }
 2842:     $$rowtotal += $itemcount;
 2843:     return $datatable;
 2844: }
 2845: 
 2846: sub print_selfenrollment {
 2847:     my ($position,$dom,$settings,$rowtotal) = @_;
 2848:     my ($css_class,$datatable);
 2849:     my $itemcount = 1;
 2850:     my @types = ('official','unofficial','community','textbook');
 2851:     if (($position eq 'top') || ($position eq 'middle')) {
 2852:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 2853:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 2854:         my @rows;
 2855:         my $key;
 2856:         if ($position eq 'top') {
 2857:             $key = 'admin'; 
 2858:             if (ref($rowsref) eq 'ARRAY') {
 2859:                 @rows = @{$rowsref};
 2860:             }
 2861:         } elsif ($position eq 'middle') {
 2862:             $key = 'default';
 2863:             @rows = ('types','registered','approval','limit');
 2864:         }
 2865:         foreach my $row (@rows) {
 2866:             if (defined($titlesref->{$row})) {
 2867:                 $itemcount ++;
 2868:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2869:                 $datatable .= '<tr'.$css_class.'>'.
 2870:                               '<td>'.$titlesref->{$row}.'</td>'.
 2871:                               '<td class="LC_left_item">'.
 2872:                               '<table><tr>';
 2873:                 my (%current,%currentcap);
 2874:                 if (ref($settings) eq 'HASH') {
 2875:                     if (ref($settings->{$key}) eq 'HASH') {
 2876:                         foreach my $type (@types) {
 2877:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 2878:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 2879:                             }
 2880:                             if (($row eq 'limit') && ($key eq 'default')) {
 2881:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 2882:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 2883:                                 }
 2884:                             }
 2885:                         }
 2886:                     }
 2887:                 }
 2888:                 my %roles = (
 2889:                              '0' => &Apache::lonnet::plaintext('dc'),
 2890:                             ); 
 2891:             
 2892:                 foreach my $type (@types) {
 2893:                     unless (($row eq 'registered') && ($key eq 'default')) {
 2894:                         $datatable .= '<th>'.&mt($type).'</th>';
 2895:                     }
 2896:                 }
 2897:                 unless (($row eq 'registered') && ($key eq 'default')) {
 2898:                     $datatable .= '</tr><tr>';
 2899:                 }
 2900:                 foreach my $type (@types) {
 2901:                     if ($type eq 'community') {
 2902:                         $roles{'1'} = &mt('Community personnel');
 2903:                     } else {
 2904:                         $roles{'1'} = &mt('Course personnel');
 2905:                     }
 2906:                     $datatable .= '<td style="vertical-align: top">';
 2907:                     if ($position eq 'top') {
 2908:                         my %checked;
 2909:                         if ($current{$type} eq '0') {
 2910:                             $checked{'0'} = ' checked="checked"';
 2911:                         } else {
 2912:                             $checked{'1'} = ' checked="checked"';
 2913:                         }
 2914:                         foreach my $role ('1','0') {
 2915:                             $datatable .= '<span class="LC_nobreak"><label>'.
 2916:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 2917:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 2918:                                           $roles{$role}.'</label></span> ';
 2919:                         }
 2920:                     } else {
 2921:                         if ($row eq 'types') {
 2922:                             my %checked;
 2923:                             if ($current{$type} =~ /^(all|dom)$/) {
 2924:                                 $checked{$1} = ' checked="checked"';
 2925:                             } else {
 2926:                                 $checked{''} = ' checked="checked"';
 2927:                             }
 2928:                             foreach my $val ('','dom','all') {
 2929:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 2930:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 2931:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 2932:                             }
 2933:                         } elsif ($row eq 'registered') {
 2934:                             my %checked;
 2935:                             if ($current{$type} eq '1') {
 2936:                                 $checked{'1'} = ' checked="checked"';
 2937:                             } else {
 2938:                                 $checked{'0'} = ' checked="checked"';
 2939:                             }
 2940:                             foreach my $val ('0','1') {
 2941:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 2942:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 2943:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 2944:                             }
 2945:                         } elsif ($row eq 'approval') {
 2946:                             my %checked;
 2947:                             if ($current{$type} =~ /^([12])$/) {
 2948:                                 $checked{$1} = ' checked="checked"';
 2949:                             } else {
 2950:                                 $checked{'0'} = ' checked="checked"';
 2951:                             }
 2952:                             for my $val (0..2) {
 2953:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 2954:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 2955:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 2956:                             }
 2957:                         } elsif ($row eq 'limit') {
 2958:                             my %checked;
 2959:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 2960:                                 $checked{$1} = ' checked="checked"';
 2961:                             } else {
 2962:                                 $checked{'none'} = ' checked="checked"';
 2963:                             }
 2964:                             my $cap;
 2965:                             if ($currentcap{$type} =~ /^\d+$/) {
 2966:                                 $cap = $currentcap{$type};
 2967:                             }
 2968:                             foreach my $val ('none','allstudents','selfenrolled') {
 2969:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 2970:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 2971:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 2972:                             }
 2973:                             $datatable .= '<br />'.
 2974:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 2975:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 2976:                                           '</span>'; 
 2977:                         }
 2978:                     }
 2979:                     $datatable .= '</td>';
 2980:                 }
 2981:                 $datatable .= '</tr>';
 2982:             }
 2983:             $datatable .= '</table></td></tr>';
 2984:         }
 2985:     } elsif ($position eq 'bottom') {
 2986:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 2987:     }
 2988:     $$rowtotal += $itemcount;
 2989:     return $datatable;
 2990: }
 2991: 
 2992: sub print_validation_rows {
 2993:     my ($caller,$dom,$settings,$rowtotal) = @_;
 2994:     my ($itemsref,$namesref,$fieldsref);
 2995:     if ($caller eq 'selfenroll') { 
 2996:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 2997:     } elsif ($caller eq 'requestcourses') {
 2998:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 2999:     }
 3000:     my %currvalidation;
 3001:     if (ref($settings) eq 'HASH') {
 3002:         if (ref($settings->{'validation'}) eq 'HASH') {
 3003:             %currvalidation = %{$settings->{'validation'}};
 3004:         }
 3005:     }
 3006:     my $datatable;
 3007:     my $itemcount = 0;
 3008:     foreach my $item (@{$itemsref}) {
 3009:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3010:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3011:                       $namesref->{$item}.
 3012:                       '</span></td>'.
 3013:                       '<td class="LC_left_item">';
 3014:         if (($item eq 'url') || ($item eq 'button')) {
 3015:             $datatable .= '<span class="LC_nobreak">'.
 3016:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 3017:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 3018:         } elsif ($item eq 'fields') {
 3019:             my @currfields;
 3020:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 3021:                 @currfields = @{$currvalidation{$item}};
 3022:             }
 3023:             foreach my $field (@{$fieldsref}) {
 3024:                 my $check = '';
 3025:                 if (grep(/^\Q$field\E$/,@currfields)) {
 3026:                     $check = ' checked="checked"';
 3027:                 }
 3028:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3029:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 3030:                               ' value="'.$field.'"'.$check.' />'.$field.
 3031:                               '</label></span> ';
 3032:             }
 3033:         } elsif ($item eq 'markup') {
 3034:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5" wrap="soft">'.
 3035:                            $currvalidation{$item}.
 3036:                               '</textarea>';
 3037:         }
 3038:         $datatable .= '</td></tr>'."\n";
 3039:         if (ref($rowtotal)) {
 3040:             $itemcount ++;
 3041:         }
 3042:     }
 3043:     if ($caller eq 'requestcourses') {
 3044:         my %currhash;
 3045:         if (ref($settings->{'validation'}) eq 'HASH') {
 3046:             if ($settings->{'validation'}{'dc'} ne '') {
 3047:                 $currhash{$settings->{'validation'}{'dc'}} = 1;
 3048:             }
 3049:         }
 3050:         my $numinrow = 2;
 3051:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 3052:                                                        'validationdc',%currhash);
 3053:         if ($numdc > 1) {
 3054:             $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 3055:                           &mt('Course creation processed as: (choose Dom. Coord.)').
 3056:                           '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 3057:         } else {
 3058:             $datatable .= $dctable.'</td></tr>';
 3059:         }
 3060:         $itemcount ++;
 3061:     }
 3062:     if (ref($rowtotal)) {
 3063:         $$rowtotal += $itemcount;
 3064:     }
 3065:     return $datatable;
 3066: }
 3067: 
 3068: sub print_usersessions {
 3069:     my ($position,$dom,$settings,$rowtotal) = @_;
 3070:     my ($css_class,$datatable,%checked,%choices);
 3071:     my (%by_ip,%by_location,@intdoms);
 3072:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 3073: 
 3074:     my @alldoms = &Apache::lonnet::all_domains();
 3075:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 3076:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3077:     my %altids = &id_for_thisdom(%servers);
 3078:     my $itemcount = 1;
 3079:     if ($position eq 'top') {
 3080:         if (keys(%serverhomes) > 1) {
 3081:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 3082:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 3083:         } else {
 3084:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3085:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 3086:         }
 3087:     } else {
 3088:         if (keys(%by_location) == 0) {
 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 institution.');
 3091:         } else {
 3092:             my %lt = &usersession_titles();
 3093:             my $numinrow = 5;
 3094:             my $prefix;
 3095:             my @types;
 3096:             if ($position eq 'bottom') {
 3097:                 $prefix = 'remote';
 3098:                 @types = ('version','excludedomain','includedomain');
 3099:             } else {
 3100:                 $prefix = 'hosted';
 3101:                 @types = ('excludedomain','includedomain');
 3102:             }
 3103:             my (%current,%checkedon,%checkedoff);
 3104:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 3105:             my @locations = sort(keys(%by_location));
 3106:             foreach my $type (@types) {
 3107:                 $checkedon{$type} = '';
 3108:                 $checkedoff{$type} = ' checked="checked"';
 3109:             }
 3110:             if (ref($settings) eq 'HASH') {
 3111:                 if (ref($settings->{$prefix}) eq 'HASH') {
 3112:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 3113:                         $current{$key} = $settings->{$prefix}{$key};
 3114:                         if ($key eq 'version') {
 3115:                             if ($current{$key} ne '') {
 3116:                                 $checkedon{$key} = ' checked="checked"';
 3117:                                 $checkedoff{$key} = '';
 3118:                             }
 3119:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 3120:                             $checkedon{$key} = ' checked="checked"';
 3121:                             $checkedoff{$key} = '';
 3122:                         }
 3123:                     }
 3124:                 }
 3125:             }
 3126:             foreach my $type (@types) {
 3127:                 next if ($type ne 'version' && !@locations);
 3128:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3129:                 $datatable .= '<tr'.$css_class.'>
 3130:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 3131:                                <span class="LC_nobreak">&nbsp;
 3132:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 3133:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 3134:                 if ($type eq 'version') {
 3135:                     my $selector = '<select name="'.$prefix.'_version">';
 3136:                     foreach my $version (@lcversions) {
 3137:                         my $selected = '';
 3138:                         if ($current{'version'} eq $version) {
 3139:                             $selected = ' selected="selected"';
 3140:                         }
 3141:                         $selector .= ' <option value="'.$version.'"'.
 3142:                                      $selected.'>'.$version.'</option>';
 3143:                     }
 3144:                     $selector .= '</select> ';
 3145:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 3146:                 } else {
 3147:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 3148:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 3149:                                  ' />'.('&nbsp;'x2).
 3150:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 3151:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 3152:                                  "\n".
 3153:                                  '</div><div><table>';
 3154:                     my $rem;
 3155:                     for (my $i=0; $i<@locations; $i++) {
 3156:                         my ($showloc,$value,$checkedtype);
 3157:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 3158:                             my $ip = $by_location{$locations[$i]}->[0];
 3159:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 3160:                                  $value = join(':',@{$by_ip{$ip}});
 3161:                                 $showloc = join(', ',@{$by_ip{$ip}});
 3162:                                 if (ref($current{$type}) eq 'ARRAY') {
 3163:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 3164:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 3165:                                             $checkedtype = ' checked="checked"';
 3166:                                             last;
 3167:                                         }
 3168:                                     }
 3169:                                 }
 3170:                             }
 3171:                         }
 3172:                         $rem = $i%($numinrow);
 3173:                         if ($rem == 0) {
 3174:                             if ($i > 0) {
 3175:                                 $datatable .= '</tr>';
 3176:                             }
 3177:                             $datatable .= '<tr>';
 3178:                         }
 3179:                         $datatable .= '<td class="LC_left_item">'.
 3180:                                       '<span class="LC_nobreak"><label>'.
 3181:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 3182:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 3183:                                       '</label></span></td>';
 3184:                     }
 3185:                     $rem = @locations%($numinrow);
 3186:                     my $colsleft = $numinrow - $rem;
 3187:                     if ($colsleft > 1 ) {
 3188:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3189:                                       '&nbsp;</td>';
 3190:                     } elsif ($colsleft == 1) {
 3191:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 3192:                     }
 3193:                     $datatable .= '</tr></table>';
 3194:                 }
 3195:                 $datatable .= '</td></tr>';
 3196:                 $itemcount ++;
 3197:             }
 3198:         }
 3199:     }
 3200:     $$rowtotal += $itemcount;
 3201:     return $datatable;
 3202: }
 3203: 
 3204: sub build_location_hashes {
 3205:     my ($intdoms,$by_ip,$by_location) = @_;
 3206:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 3207:                   (ref($by_location) eq 'HASH')); 
 3208:     my %iphost = &Apache::lonnet::get_iphost();
 3209:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 3210:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 3211:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 3212:         foreach my $id (@{$iphost{$primary_ip}}) {
 3213:             my $intdom = &Apache::lonnet::internet_dom($id);
 3214:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 3215:                 push(@{$intdoms},$intdom);
 3216:             }
 3217:         }
 3218:     }
 3219:     foreach my $ip (keys(%iphost)) {
 3220:         if (ref($iphost{$ip}) eq 'ARRAY') {
 3221:             foreach my $id (@{$iphost{$ip}}) {
 3222:                 my $location = &Apache::lonnet::internet_dom($id);
 3223:                 if ($location) {
 3224:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 3225:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3226:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 3227:                             push(@{$by_ip->{$ip}},$location);
 3228:                         }
 3229:                     } else {
 3230:                         $by_ip->{$ip} = [$location];
 3231:                     }
 3232:                 }
 3233:             }
 3234:         }
 3235:     }
 3236:     foreach my $ip (sort(keys(%{$by_ip}))) {
 3237:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3238:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 3239:             my $first = $by_ip->{$ip}->[0];
 3240:             if (ref($by_location->{$first}) eq 'ARRAY') {
 3241:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 3242:                     push(@{$by_location->{$first}},$ip);
 3243:                 }
 3244:             } else {
 3245:                 $by_location->{$first} = [$ip];
 3246:             }
 3247:         }
 3248:     }
 3249:     return;
 3250: }
 3251: 
 3252: sub current_offloads_to {
 3253:     my ($dom,$settings,$servers) = @_;
 3254:     my (%spareid,%otherdomconfigs);
 3255:     if (ref($servers) eq 'HASH') {
 3256:         foreach my $lonhost (sort(keys(%{$servers}))) {
 3257:             my $gotspares;
 3258:             if (ref($settings) eq 'HASH') {
 3259:                 if (ref($settings->{'spares'}) eq 'HASH') {
 3260:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 3261:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 3262:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 3263:                         $gotspares = 1;
 3264:                     }
 3265:                 }
 3266:             }
 3267:             unless ($gotspares) {
 3268:                 my $gotspares;
 3269:                 my $serverhomeID =
 3270:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 3271:                 my $serverhomedom =
 3272:                     &Apache::lonnet::host_domain($serverhomeID);
 3273:                 if ($serverhomedom ne $dom) {
 3274:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 3275:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3276:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3277:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3278:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3279:                                 $gotspares = 1;
 3280:                             }
 3281:                         }
 3282:                     } else {
 3283:                         $otherdomconfigs{$serverhomedom} =
 3284:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 3285:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 3286:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3287:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3288:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 3289:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3290:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3291:                                         $gotspares = 1;
 3292:                                     }
 3293:                                 }
 3294:                             }
 3295:                         }
 3296:                     }
 3297:                 }
 3298:             }
 3299:             unless ($gotspares) {
 3300:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3301:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3302:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3303:                } else {
 3304:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 3305:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 3306:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3307:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3308:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3309:                     } else {
 3310:                         my %what = (
 3311:                              spareid => 1,
 3312:                         );
 3313:                         my ($result,$returnhash) = 
 3314:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 3315:                         if ($result eq 'ok') { 
 3316:                             if (ref($returnhash) eq 'HASH') {
 3317:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 3318:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 3319:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 3320:                                 }
 3321:                             }
 3322:                         }
 3323:                     }
 3324:                 }
 3325:             }
 3326:         }
 3327:     }
 3328:     return %spareid;
 3329: }
 3330: 
 3331: sub spares_row {
 3332:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 3333:     my $css_class;
 3334:     my $numinrow = 4;
 3335:     my $itemcount = 1;
 3336:     my $datatable;
 3337:     my %typetitles = &sparestype_titles();
 3338:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3339:         foreach my $server (sort(keys(%{$servers}))) {
 3340:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 3341:             my ($othercontrol,$serverdom);
 3342:             if ($serverhome ne $server) {
 3343:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 3344:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3345:             } else {
 3346:                 $serverdom = &Apache::lonnet::host_domain($server);
 3347:                 if ($serverdom ne $dom) {
 3348:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3349:                 }
 3350:             }
 3351:             next unless (ref($spareid->{$server}) eq 'HASH');
 3352:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3353:             $datatable .= '<tr'.$css_class.'>
 3354:                            <td rowspan="2">
 3355:                             <span class="LC_nobreak">'.
 3356:                           &mt('[_1] when busy, offloads to:'
 3357:                               ,'<b>'.$server.'</b>').
 3358:                           "\n";
 3359:             my (%current,%canselect);
 3360:             my @choices = 
 3361:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 3362:             foreach my $type ('primary','default') {
 3363:                 if (ref($spareid->{$server}) eq 'HASH') {
 3364:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 3365:                         my @spares = @{$spareid->{$server}{$type}};
 3366:                         if (@spares > 0) {
 3367:                             if ($othercontrol) {
 3368:                                 $current{$type} = join(', ',@spares);
 3369:                             } else {
 3370:                                 $current{$type} .= '<table>';
 3371:                                 my $numspares = scalar(@spares);
 3372:                                 for (my $i=0;  $i<@spares; $i++) {
 3373:                                     my $rem = $i%($numinrow);
 3374:                                     if ($rem == 0) {
 3375:                                         if ($i > 0) {
 3376:                                             $current{$type} .= '</tr>';
 3377:                                         }
 3378:                                         $current{$type} .= '<tr>';
 3379:                                     }
 3380:                                     $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;'.
 3381:                                                        $spareid->{$server}{$type}[$i].
 3382:                                                        '</label></td>'."\n";
 3383:                                 }
 3384:                                 my $rem = @spares%($numinrow);
 3385:                                 my $colsleft = $numinrow - $rem;
 3386:                                 if ($colsleft > 1 ) {
 3387:                                     $current{$type} .= '<td colspan="'.$colsleft.
 3388:                                                        '" class="LC_left_item">'.
 3389:                                                        '&nbsp;</td>';
 3390:                                 } elsif ($colsleft == 1) {
 3391:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 3392:                                 }
 3393:                                 $current{$type} .= '</tr></table>';
 3394:                             }
 3395:                         }
 3396:                     }
 3397:                     if ($current{$type} eq '') {
 3398:                         $current{$type} = &mt('None specified');
 3399:                     }
 3400:                     if ($othercontrol) {
 3401:                         if ($type eq 'primary') {
 3402:                             $canselect{$type} = $othercontrol;
 3403:                         }
 3404:                     } else {
 3405:                         $canselect{$type} = 
 3406:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 3407:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 3408:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 3409:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 3410:                         if (@choices > 0) {
 3411:                             foreach my $lonhost (@choices) {
 3412:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 3413:                             }
 3414:                         }
 3415:                         $canselect{$type} .= '</select>'."\n";
 3416:                     }
 3417:                 } else {
 3418:                     $current{$type} = &mt('Could not be determined');
 3419:                     if ($type eq 'primary') {
 3420:                         $canselect{$type} =  $othercontrol;
 3421:                     }
 3422:                 }
 3423:                 if ($type eq 'default') {
 3424:                     $datatable .= '<tr'.$css_class.'>';
 3425:                 }
 3426:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 3427:                               '<td>'.$current{$type}.'</td>'."\n".
 3428:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 3429:             }
 3430:             $itemcount ++;
 3431:         }
 3432:     }
 3433:     $$rowtotal += $itemcount;
 3434:     return $datatable;
 3435: }
 3436: 
 3437: sub possible_newspares {
 3438:     my ($server,$currspares,$serverhomes,$altids) = @_;
 3439:     my $serverhostname = &Apache::lonnet::hostname($server);
 3440:     my %excluded;
 3441:     if ($serverhostname ne '') {
 3442:         %excluded = (
 3443:                        $serverhostname => 1,
 3444:                     );
 3445:     }
 3446:     if (ref($currspares) eq 'HASH') {
 3447:         foreach my $type (keys(%{$currspares})) {
 3448:             if (ref($currspares->{$type}) eq 'ARRAY') {
 3449:                 if (@{$currspares->{$type}} > 0) {
 3450:                     foreach my $curr (@{$currspares->{$type}}) {
 3451:                         my $hostname = &Apache::lonnet::hostname($curr);
 3452:                         $excluded{$hostname} = 1;
 3453:                     }
 3454:                 }
 3455:             }
 3456:         }
 3457:     }
 3458:     my @choices;
 3459:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3460:         if (keys(%{$serverhomes}) > 1) {
 3461:             foreach my $name (sort(keys(%{$serverhomes}))) {
 3462:                 unless ($excluded{$name}) {
 3463:                     if (exists($altids->{$serverhomes->{$name}})) {
 3464:                         push(@choices,$altids->{$serverhomes->{$name}});
 3465:                     } else {
 3466:                         push(@choices,$serverhomes->{$name});
 3467:                     }
 3468:                 }
 3469:             }
 3470:         }
 3471:     }
 3472:     return sort(@choices);
 3473: }
 3474: 
 3475: sub print_loadbalancing {
 3476:     my ($dom,$settings,$rowtotal) = @_;
 3477:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 3478:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 3479:     my $numinrow = 1;
 3480:     my $datatable;
 3481:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3482:     my (%currbalancer,%currtargets,%currrules,%existing);
 3483:     if (ref($settings) eq 'HASH') {
 3484:         %existing = %{$settings};
 3485:     }
 3486:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 3487:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 3488:                                   \%currtargets,\%currrules);
 3489:     } else {
 3490:         return;
 3491:     }
 3492:     my ($othertitle,$usertypes,$types) =
 3493:         &Apache::loncommon::sorted_inst_types($dom);
 3494:     my $rownum = 8;
 3495:     if (ref($types) eq 'ARRAY') {
 3496:         $rownum += scalar(@{$types});
 3497:     }
 3498:     my @css_class = ('LC_odd_row','LC_even_row');
 3499:     my $balnum = 0;
 3500:     my $islast;
 3501:     my (@toshow,$disabledtext);
 3502:     if (keys(%currbalancer) > 0) {
 3503:         @toshow = sort(keys(%currbalancer));
 3504:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 3505:             push(@toshow,'');
 3506:         }
 3507:     } else {
 3508:         @toshow = ('');
 3509:         $disabledtext = &mt('No existing load balancer');
 3510:     }
 3511:     foreach my $lonhost (@toshow) {
 3512:         if ($balnum == scalar(@toshow)-1) {
 3513:             $islast = 1;
 3514:         } else {
 3515:             $islast = 0;
 3516:         }
 3517:         my $cssidx = $balnum%2;
 3518:         my $targets_div_style = 'display: none';
 3519:         my $disabled_div_style = 'display: block';
 3520:         my $homedom_div_style = 'display: none';
 3521:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 3522:                       '<td rowspan="'.$rownum.'" valign="top">'.
 3523:                       '<p>';
 3524:         if ($lonhost eq '') {
 3525:             $datatable .= '<span class="LC_nobreak">';
 3526:             if (keys(%currbalancer) > 0) {
 3527:                 $datatable .= &mt('Add balancer:');
 3528:             } else {
 3529:                 $datatable .= &mt('Enable balancer:');
 3530:             }
 3531:             $datatable .= '&nbsp;'.
 3532:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 3533:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 3534:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 3535:                           '<option value="" selected="selected">'.&mt('None').
 3536:                           '</option>'."\n";
 3537:             foreach my $server (sort(keys(%servers))) {
 3538:                 next if ($currbalancer{$server});
 3539:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 3540:             }
 3541:             $datatable .=
 3542:                 '</select>'."\n".
 3543:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 3544:         } else {
 3545:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 3546:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 3547:                            &mt('Stop balancing').'</label>'.
 3548:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 3549:             $targets_div_style = 'display: block';
 3550:             $disabled_div_style = 'display: none';
 3551:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 3552:                 $homedom_div_style = 'display: block';
 3553:             }
 3554:         }
 3555:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 3556:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 3557:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 3558:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 3559:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 3560:         my @sparestypes = ('primary','default');
 3561:         my %typetitles = &sparestype_titles();
 3562:         foreach my $sparetype (@sparestypes) {
 3563:             my $targettable;
 3564:             for (my $i=0; $i<$numspares; $i++) {
 3565:                 my $checked;
 3566:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 3567:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 3568:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 3569:                             $checked = ' checked="checked"';
 3570:                         }
 3571:                     }
 3572:                 }
 3573:                 my ($chkboxval,$disabled);
 3574:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 3575:                     $chkboxval = $spares[$i];
 3576:                 }
 3577:                 if (exists($currbalancer{$spares[$i]})) {
 3578:                     $disabled = ' disabled="disabled"';
 3579:                 }
 3580:                 $targettable .=
 3581:                     '<td><label><input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 3582:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 3583:                     '</span></label></td>';
 3584:                 my $rem = $i%($numinrow);
 3585:                 if ($rem == 0) {
 3586:                     if (($i > 0) && ($i < $numspares-1)) {
 3587:                         $targettable .= '</tr>';
 3588:                     }
 3589:                     if ($i < $numspares-1) {
 3590:                         $targettable .= '<tr>';
 3591:                     }
 3592:                 }
 3593:             }
 3594:             if ($targettable ne '') {
 3595:                 my $rem = $numspares%($numinrow);
 3596:                 my $colsleft = $numinrow - $rem;
 3597:                 if ($colsleft > 1 ) {
 3598:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3599:                                     '&nbsp;</td>';
 3600:                 } elsif ($colsleft == 1) {
 3601:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 3602:                 }
 3603:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 3604:                                '<table><tr>'.$targettable.'</tr></table><br />';
 3605:             }
 3606:         }
 3607:         $datatable .= '</div></td></tr>'.
 3608:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 3609:                                            $othertitle,$usertypes,$types,\%servers,
 3610:                                            \%currbalancer,$lonhost,
 3611:                                            $targets_div_style,$homedom_div_style,
 3612:                                            $css_class[$cssidx],$balnum,$islast);
 3613:         $$rowtotal += $rownum;
 3614:         $balnum ++;
 3615:     }
 3616:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 3617:     return $datatable;
 3618: }
 3619: 
 3620: sub get_loadbalancers_config {
 3621:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 3622:     return unless ((ref($servers) eq 'HASH') &&
 3623:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 3624:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 3625:     if (keys(%{$existing}) > 0) {
 3626:         my $oldlonhost;
 3627:         foreach my $key (sort(keys(%{$existing}))) {
 3628:             if ($key eq 'lonhost') {
 3629:                 $oldlonhost = $existing->{'lonhost'};
 3630:                 $currbalancer->{$oldlonhost} = 1;
 3631:             } elsif ($key eq 'targets') {
 3632:                 if ($oldlonhost) {
 3633:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 3634:                 }
 3635:             } elsif ($key eq 'rules') {
 3636:                 if ($oldlonhost) {
 3637:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 3638:                 }
 3639:             } elsif (ref($existing->{$key}) eq 'HASH') {
 3640:                 $currbalancer->{$key} = 1;
 3641:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 3642:                 $currrules->{$key} = $existing->{$key}{'rules'};
 3643:             }
 3644:         }
 3645:     } else {
 3646:         my ($balancerref,$targetsref) =
 3647:                 &Apache::lonnet::get_lonbalancer_config($servers);
 3648:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 3649:             foreach my $server (sort(keys(%{$balancerref}))) {
 3650:                 $currbalancer->{$server} = 1;
 3651:                 $currtargets->{$server} = $targetsref->{$server};
 3652:             }
 3653:         }
 3654:     }
 3655:     return;
 3656: }
 3657: 
 3658: sub loadbalancing_rules {
 3659:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 3660:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 3661:         $css_class,$balnum,$islast) = @_;
 3662:     my $output;
 3663:     my $num = 0;
 3664:     my ($alltypes,$othertypes,$titles) =
 3665:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 3666:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 3667:         foreach my $type (@{$alltypes}) {
 3668:             $num ++;
 3669:             my $current;
 3670:             if (ref($currrules) eq 'HASH') {
 3671:                 $current = $currrules->{$type};
 3672:             }
 3673:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3674:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 3675:                     $current = '';
 3676:                 }
 3677:             }
 3678:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 3679:                                              $servers,$currbalancer,$lonhost,$dom,
 3680:                                              $targets_div_style,$homedom_div_style,
 3681:                                              $css_class,$balnum,$num,$islast);
 3682:         }
 3683:     }
 3684:     return $output;
 3685: }
 3686: 
 3687: sub loadbalancing_titles {
 3688:     my ($dom,$intdom,$usertypes,$types) = @_;
 3689:     my %othertypes = (
 3690:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 3691:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 3692:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 3693:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 3694:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 3695:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 3696:                      );
 3697:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 3698:     if (ref($types) eq 'ARRAY') {
 3699:         unshift(@alltypes,@{$types},'default');
 3700:     }
 3701:     my %titles;
 3702:     foreach my $type (@alltypes) {
 3703:         if ($type =~ /^_LC_/) {
 3704:             $titles{$type} = $othertypes{$type};
 3705:         } elsif ($type eq 'default') {
 3706:             $titles{$type} = &mt('All users from [_1]',$dom);
 3707:             if (ref($types) eq 'ARRAY') {
 3708:                 if (@{$types} > 0) {
 3709:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 3710:                 }
 3711:             }
 3712:         } elsif (ref($usertypes) eq 'HASH') {
 3713:             $titles{$type} = $usertypes->{$type};
 3714:         }
 3715:     }
 3716:     return (\@alltypes,\%othertypes,\%titles);
 3717: }
 3718: 
 3719: sub loadbalance_rule_row {
 3720:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 3721:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 3722:     my @rulenames;
 3723:     my %ruletitles = &offloadtype_text();
 3724:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 3725:         @rulenames = ('balancer','offloadedto');
 3726:     } else {
 3727:         @rulenames = ('default','homeserver');
 3728:         if ($type eq '_LC_external') {
 3729:             push(@rulenames,'externalbalancer');
 3730:         } else {
 3731:             push(@rulenames,'specific');
 3732:         }
 3733:         push(@rulenames,'none');
 3734:     }
 3735:     my $style = $targets_div_style;
 3736:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3737:         $style = $homedom_div_style;
 3738:     }
 3739:     my $space;
 3740:     if ($islast && $num == 1) {
 3741:         $space = '<div display="inline-block">&nbsp;</div>';
 3742:     }
 3743:     my $output =
 3744:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 3745:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 3746:         '<td valaign="top">'.$space.
 3747:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 3748:     for (my $i=0; $i<@rulenames; $i++) {
 3749:         my $rule = $rulenames[$i];
 3750:         my ($checked,$extra);
 3751:         if ($rulenames[$i] eq 'default') {
 3752:             $rule = '';
 3753:         }
 3754:         if ($rulenames[$i] eq 'specific') {
 3755:             if (ref($servers) eq 'HASH') {
 3756:                 my $default;
 3757:                 if (($current ne '') && (exists($servers->{$current}))) {
 3758:                     $checked = ' checked="checked"';
 3759:                 }
 3760:                 unless ($checked) {
 3761:                     $default = ' selected="selected"';
 3762:                 }
 3763:                 $extra =
 3764:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3765:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3766:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 3767:                     '<option value=""'.$default.'></option>'."\n";
 3768:                 foreach my $server (sort(keys(%{$servers}))) {
 3769:                     if (ref($currbalancer) eq 'HASH') {
 3770:                         next if (exists($currbalancer->{$server}));
 3771:                     }
 3772:                     my $selected;
 3773:                     if ($server eq $current) {
 3774:                         $selected = ' selected="selected"';
 3775:                     }
 3776:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 3777:                 }
 3778:                 $extra .= '</select>';
 3779:             }
 3780:         } elsif ($rule eq $current) {
 3781:             $checked = ' checked="checked"';
 3782:         }
 3783:         $output .= '<span class="LC_nobreak"><label>'.
 3784:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 3785:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 3786:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 3787:                    ')"'.$checked.' />&nbsp;'.$ruletitles{$rulenames[$i]}.
 3788:                    '</label>'.$extra.'</span><br />'."\n";
 3789:     }
 3790:     $output .= '</div></td></tr>'."\n";
 3791:     return $output;
 3792: }
 3793: 
 3794: sub offloadtype_text {
 3795:     my %ruletitles = &Apache::lonlocal::texthash (
 3796:            'default'          => 'Offloads to default destinations',
 3797:            'homeserver'       => "Offloads to user's home server",
 3798:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 3799:            'specific'         => 'Offloads to specific server',
 3800:            'none'             => 'No offload',
 3801:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 3802:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 3803:     );
 3804:     return %ruletitles;
 3805: }
 3806: 
 3807: sub sparestype_titles {
 3808:     my %typestitles = &Apache::lonlocal::texthash (
 3809:                           'primary' => 'primary',
 3810:                           'default' => 'default',
 3811:                       );
 3812:     return %typestitles;
 3813: }
 3814: 
 3815: sub contact_titles {
 3816:     my %titles = &Apache::lonlocal::texthash (
 3817:                    'supportemail' => 'Support E-mail address',
 3818:                    'adminemail'   => 'Default Server Admin E-mail address',
 3819:                    'errormail'    => 'Error reports to be e-mailed to',
 3820:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 3821:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 3822:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 3823:                    'requestsmail' => 'E-mail from course requests requiring approval',
 3824:                    'updatesmail'  => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 3825:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 3826:                  );
 3827:     my %short_titles = &Apache::lonlocal::texthash (
 3828:                            adminemail   => 'Admin E-mail address',
 3829:                            supportemail => 'Support E-mail',
 3830:                        );   
 3831:     return (\%titles,\%short_titles);
 3832: }
 3833: 
 3834: sub tool_titles {
 3835:     my %titles = &Apache::lonlocal::texthash (
 3836:                      aboutme    => 'Personal web page',
 3837:                      blog       => 'Blog',
 3838:                      webdav     => 'WebDAV',
 3839:                      portfolio  => 'Portfolio',
 3840:                      official   => 'Official courses (with institutional codes)',
 3841:                      unofficial => 'Unofficial courses',
 3842:                      community  => 'Communities',
 3843:                      textbook   => 'Textbook courses',
 3844:                  );
 3845:     return %titles;
 3846: }
 3847: 
 3848: sub courserequest_titles {
 3849:     my %titles = &Apache::lonlocal::texthash (
 3850:                                    official   => 'Official',
 3851:                                    unofficial => 'Unofficial',
 3852:                                    community  => 'Communities',
 3853:                                    textbook   => 'Textbook',
 3854:                                    norequest  => 'Not allowed',
 3855:                                    approval   => 'Approval by Dom. Coord.',
 3856:                                    validate   => 'With validation',
 3857:                                    autolimit  => 'Numerical limit',
 3858:                                    unlimited  => '(blank for unlimited)',
 3859:                  );
 3860:     return %titles;
 3861: }
 3862: 
 3863: sub authorrequest_titles {
 3864:     my %titles = &Apache::lonlocal::texthash (
 3865:                                    norequest  => 'Not allowed',
 3866:                                    approval   => 'Approval by Dom. Coord.',
 3867:                                    automatic  => 'Automatic approval',
 3868:                  );
 3869:     return %titles;
 3870: }
 3871: 
 3872: sub courserequest_conditions {
 3873:     my %conditions = &Apache::lonlocal::texthash (
 3874:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 3875:        validate   => '(Processing of request subject to institutional validation).',
 3876:                  );
 3877:     return %conditions;
 3878: }
 3879: 
 3880: 
 3881: sub print_usercreation {
 3882:     my ($position,$dom,$settings,$rowtotal) = @_;
 3883:     my $numinrow = 4;
 3884:     my $datatable;
 3885:     if ($position eq 'top') {
 3886:         $$rowtotal ++;
 3887:         my $rowcount = 0;
 3888:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 3889:         if (ref($rules) eq 'HASH') {
 3890:             if (keys(%{$rules}) > 0) {
 3891:                 $datatable .= &user_formats_row('username',$settings,$rules,
 3892:                                                 $ruleorder,$numinrow,$rowcount);
 3893:                 $$rowtotal ++;
 3894:                 $rowcount ++;
 3895:             }
 3896:         }
 3897:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 3898:         if (ref($idrules) eq 'HASH') {
 3899:             if (keys(%{$idrules}) > 0) {
 3900:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 3901:                                                 $idruleorder,$numinrow,$rowcount);
 3902:                 $$rowtotal ++;
 3903:                 $rowcount ++;
 3904:             }
 3905:         }
 3906:         if ($rowcount == 0) {
 3907:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 3908:             $$rowtotal ++;
 3909:             $rowcount ++;
 3910:         }
 3911:     } elsif ($position eq 'middle') {
 3912:         my @creators = ('author','course','requestcrs');
 3913:         my ($rules,$ruleorder) =
 3914:             &Apache::lonnet::inst_userrules($dom,'username');
 3915:         my %lt = &usercreation_types();
 3916:         my %checked;
 3917:         if (ref($settings) eq 'HASH') {
 3918:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 3919:                 foreach my $item (@creators) {
 3920:                     $checked{$item} = $settings->{'cancreate'}{$item};
 3921:                 }
 3922:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 3923:                 foreach my $item (@creators) {
 3924:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 3925:                         $checked{$item} = 'none';
 3926:                     }
 3927:                 }
 3928:             }
 3929:         }
 3930:         my $rownum = 0;
 3931:         foreach my $item (@creators) {
 3932:             $rownum ++;
 3933:             if ($checked{$item} eq '') {
 3934:                 $checked{$item} = 'any';
 3935:             }
 3936:             my $css_class;
 3937:             if ($rownum%2) {
 3938:                 $css_class = '';
 3939:             } else {
 3940:                 $css_class = ' class="LC_odd_row" ';
 3941:             }
 3942:             $datatable .= '<tr'.$css_class.'>'.
 3943:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 3944:                          '</span></td><td align="right">';
 3945:             my @options = ('any');
 3946:             if (ref($rules) eq 'HASH') {
 3947:                 if (keys(%{$rules}) > 0) {
 3948:                     push(@options,('official','unofficial'));
 3949:                 }
 3950:             }
 3951:             push(@options,'none');
 3952:             foreach my $option (@options) {
 3953:                 my $type = 'radio';
 3954:                 my $check = ' ';
 3955:                 if ($checked{$item} eq $option) {
 3956:                     $check = ' checked="checked" ';
 3957:                 } 
 3958:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3959:                               '<input type="'.$type.'" name="can_createuser_'.
 3960:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 3961:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 3962:             }
 3963:             $datatable .= '</td></tr>';
 3964:         }
 3965:     } else {
 3966:         my @contexts = ('author','course','domain');
 3967:         my @authtypes = ('int','krb4','krb5','loc');
 3968:         my %checked;
 3969:         if (ref($settings) eq 'HASH') {
 3970:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 3971:                 foreach my $item (@contexts) {
 3972:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 3973:                         foreach my $auth (@authtypes) {
 3974:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 3975:                                 $checked{$item}{$auth} = ' checked="checked" ';
 3976:                             }
 3977:                         }
 3978:                     }
 3979:                 }
 3980:             }
 3981:         } else {
 3982:             foreach my $item (@contexts) {
 3983:                 foreach my $auth (@authtypes) {
 3984:                     $checked{$item}{$auth} = ' checked="checked" ';
 3985:                 }
 3986:             }
 3987:         }
 3988:         my %title = &context_names();
 3989:         my %authname = &authtype_names();
 3990:         my $rownum = 0;
 3991:         my $css_class; 
 3992:         foreach my $item (@contexts) {
 3993:             if ($rownum%2) {
 3994:                 $css_class = '';
 3995:             } else {
 3996:                 $css_class = ' class="LC_odd_row" ';
 3997:             }
 3998:             $datatable .=   '<tr'.$css_class.'>'.
 3999:                             '<td>'.$title{$item}.
 4000:                             '</td><td class="LC_left_item">'.
 4001:                             '<span class="LC_nobreak">';
 4002:             foreach my $auth (@authtypes) {
 4003:                 $datatable .= '<label>'. 
 4004:                               '<input type="checkbox" name="'.$item.'_auth" '.
 4005:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 4006:                               $authname{$auth}.'</label>&nbsp;';
 4007:             }
 4008:             $datatable .= '</span></td></tr>';
 4009:             $rownum ++;
 4010:         }
 4011:         $$rowtotal += $rownum;
 4012:     }
 4013:     return $datatable;
 4014: }
 4015: 
 4016: sub print_selfcreation {
 4017:     my ($position,$dom,$settings,$rowtotal) = @_;
 4018:     my (@selfcreate,$createsettings,$processing,$datatable);
 4019:     if (ref($settings) eq 'HASH') {
 4020:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 4021:             $createsettings = $settings->{'cancreate'};
 4022:             if (ref($createsettings) eq 'HASH') {
 4023:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 4024:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 4025:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 4026:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 4027:                         @selfcreate = ('email','login','sso');
 4028:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 4029:                         @selfcreate = ($createsettings->{'selfcreate'});
 4030:                     }
 4031:                 }
 4032:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 4033:                     $processing = $createsettings->{'selfcreateprocessing'};
 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:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 4067:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4068:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 4069:         my $rem;
 4070:         my $numperrow = 2;
 4071:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 4072:         $datatable .= '<tr'.$css_class.'>'.
 4073:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 4074:                      '<td class="LC_left_item">'."\n".
 4075:                      '<table><tr><td>'."\n";
 4076:         for (my $i=0; $i<@fields; $i++) {
 4077:             $rem = $i%($numperrow);
 4078:             if ($rem == 0) {
 4079:                 if ($i > 0) {
 4080:                     $datatable .= '</tr>';
 4081:                 }
 4082:                 $datatable .= '<tr>';
 4083:             }
 4084:             my $currval;
 4085:             if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 4086:                 $currval = $createsettings->{'shibenv'}{$fields[$i]};
 4087:             }
 4088:             $datatable .= '<td class="LC_left_item">'.
 4089:                           '<span class="LC_nobreak">'.
 4090:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 4091:                           'value="'.$currval.'" size="10" />&nbsp;'.
 4092:                           $fieldtitles{$fields[$i]}.'</span></td>';
 4093:         }
 4094:         my $colsleft = $numperrow - $rem;
 4095:         if ($colsleft > 1 ) {
 4096:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4097:                          '&nbsp;</td>';
 4098:         } elsif ($colsleft == 1) {
 4099:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4100:         }
 4101:         $datatable .= '</tr></table></td></tr>';
 4102:         $$rowtotal ++;
 4103:     } elsif ($position eq 'middle') {
 4104:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 4105:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4106:         $usertypes->{'default'} = $othertitle;
 4107:         if (ref($types) eq 'ARRAY') {
 4108:             push(@{$types},'default');
 4109:             $usertypes->{'default'} = $othertitle;
 4110:             foreach my $status (@{$types}) {
 4111:                 $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 4112:                                                        $numinrow,$$rowtotal,$usertypes);
 4113:                 $$rowtotal ++;
 4114:             }
 4115:         }
 4116:     } else {
 4117:         my %choices = &Apache::lonlocal::texthash (
 4118:                                                       cancreate_email => 'E-mail address as username',
 4119:                                                   );
 4120:         my @toggles = sort(keys(%choices));
 4121:         my %defaultchecked = (
 4122:                                'cancreate_email' => 'off',
 4123:                              );
 4124:         my $itemcount = 0;
 4125:         my $display = 'none';
 4126:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 4127:             $display = 'block';
 4128:         }
 4129:         my $onclick = "toggleDisplay(this.form,'emailoptions');";
 4130:         my $additional = '<div id="emailoptions" style="display: '.$display.'">';
 4131:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 4132:         my $usertypes = {};
 4133:         my $order = [];
 4134:         if ((ref($domdefaults{'inststatustypes'}) eq 'HASH') && (ref($domdefaults{'inststatusguest'}) eq 'ARRAY')) {
 4135:             $usertypes = $domdefaults{'inststatustypes'};
 4136:             $order = $domdefaults{'inststatusguest'};
 4137:         }
 4138:         if (ref($order) eq 'ARRAY') {
 4139:             push(@{$order},'default');
 4140:             if (@{$order} > 1) {
 4141:                 $usertypes->{'default'} = &mt('Other users');
 4142:                 $additional .= '<table><tr>';
 4143:                 foreach my $status (@{$order}) {
 4144:                     $additional .= '<th>'.$usertypes->{$status}.'</th>';
 4145:                 }
 4146:                 $additional .= '</tr><tr>';
 4147:                 foreach my $status (@{$order}) {
 4148:                     $additional .= '<td>'.&email_as_username($rowtotal,$processing,$status).'</td>';
 4149:                 }
 4150:                 $additional .= '</tr></table>';
 4151:             } else {
 4152:                 $usertypes->{'default'} = &mt('All users');
 4153:                 $additional .= &email_as_username($rowtotal,$processing);
 4154:             }
 4155:         }
 4156:         $additional .= '</div>'."\n";
 4157: 
 4158:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 4159:                                                      \%choices,$$rowtotal,$onclick,$additional);
 4160:         $$rowtotal ++;
 4161:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal);
 4162:         $$rowtotal ++;
 4163:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 4164:         $numinrow = 1;
 4165:         if (ref($order) eq 'ARRAY') {
 4166:             foreach my $status (@{$order}) {
 4167:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 4168:                                                        $numinrow,$$rowtotal,$usertypes,$infofields,$infotitles);
 4169:                 $$rowtotal ++;
 4170:             }
 4171:         }
 4172:         my ($emailrules,$emailruleorder) =
 4173:             &Apache::lonnet::inst_userrules($dom,'email');
 4174:         if (ref($emailrules) eq 'HASH') {
 4175:             if (keys(%{$emailrules}) > 0) {
 4176:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 4177:                                                 $emailruleorder,$numinrow,$$rowtotal);
 4178:                 $$rowtotal ++;
 4179:             }
 4180:         }
 4181:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal);
 4182:     }
 4183:     return $datatable;
 4184: }
 4185: 
 4186: sub email_as_username {
 4187:     my ($rowtotal,$processing,$type) = @_;
 4188:     my %choices =
 4189:         &Apache::lonlocal::texthash (
 4190:                                       automatic => 'Automatic approval',
 4191:                                       approval  => 'Queued for approval',
 4192:                                     );
 4193:     my $output;
 4194:     foreach my $option ('automatic','approval') {
 4195:         my $checked;
 4196:         if (ref($processing) eq 'HASH') {
 4197:             if ($type eq '') {   
 4198:                 if (!exists($processing->{'default'})) {
 4199:                     if ($option eq 'automatic') {
 4200:                         $checked = ' checked="checked"';
 4201:                     }
 4202:                 } else {
 4203:                     if ($processing->{'default'} eq $option) {
 4204:                         $checked = ' checked="checked"';
 4205:                     }
 4206:                 }
 4207:             } else {
 4208:                 if (!exists($processing->{$type})) {
 4209:                     if ($option eq 'automatic') {
 4210:                         $checked = ' checked="checked"';
 4211:                     }
 4212:                 } else {
 4213:                     if ($processing->{$type} eq $option) {
 4214:                         $checked = ' checked="checked"';
 4215:                     }
 4216:                 }
 4217:             }
 4218:         } elsif ($option eq 'automatic') {
 4219:             $checked = ' checked="checked"'; 
 4220:         }
 4221:         my $name = 'cancreate_emailprocess';
 4222:         if (($type ne '') && ($type ne 'default')) {
 4223:             $name .= '_'.$type;
 4224:         }
 4225:         $output .= '<span class="LC_nobreak"><label>'.
 4226:                    '<input type="radio" name="'.$name.'"'.
 4227:                    $checked.' value="'.$option.'" />'.
 4228:                    $choices{$option}.'</label></span>';
 4229:         if ($type eq '') {
 4230:             $output .= '&nbsp;';
 4231:         } else {
 4232:             $output .= '<br />';
 4233:         }
 4234:     }
 4235:     $$rowtotal ++;
 4236:     return $output;
 4237: }
 4238: 
 4239: sub captcha_choice {
 4240:     my ($context,$settings,$itemcount) = @_;
 4241:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 4242:     my %lt = &captcha_phrases();
 4243:     $keyentry = 'hidden';
 4244:     if ($context eq 'cancreate') {
 4245:         $rowname = &mt('CAPTCHA validation');
 4246:     } elsif ($context eq 'login') {
 4247:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 4248:     }
 4249:     if (ref($settings) eq 'HASH') {
 4250:         if ($settings->{'captcha'}) {
 4251:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 4252:         } else {
 4253:             $checked{'original'} = ' checked="checked"';
 4254:         }
 4255:         if ($settings->{'captcha'} eq 'recaptcha') {
 4256:             $pubtext = $lt{'pub'};
 4257:             $privtext = $lt{'priv'};
 4258:             $keyentry = 'text';
 4259:         }
 4260:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 4261:             $currpub = $settings->{'recaptchakeys'}{'public'};
 4262:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 4263:         }
 4264:     } else {
 4265:         $checked{'original'} = ' checked="checked"';
 4266:     }
 4267:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4268:     my $output = '<tr'.$css_class.'>'.
 4269:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 4270:                  '<table><tr><td>'."\n";
 4271:     foreach my $option ('original','recaptcha','notused') {
 4272:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 4273:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 4274:                    $lt{$option}.'</label></span>';
 4275:         unless ($option eq 'notused') {
 4276:             $output .= ('&nbsp;'x2)."\n";
 4277:         }
 4278:     }
 4279: #
 4280: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 4281: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 4282: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 4283: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 4284: #
 4285:     $output .= '</td></tr>'."\n".
 4286:                '<tr><td>'."\n".
 4287:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 4288:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 4289:                $currpub.'" size="40" /></span><br />'."\n".
 4290:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 4291:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 4292:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 4293:                '</td></tr>';
 4294:     return $output;
 4295: }
 4296: 
 4297: sub user_formats_row {
 4298:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 4299:     my $output;
 4300:     my %text = (
 4301:                    'username' => 'new usernames',
 4302:                    'id'       => 'IDs',
 4303:                    'email'    => 'self-created accounts (e-mail)',
 4304:                );
 4305:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4306:     $output = '<tr '.$css_class.'>'.
 4307:               '<td><span class="LC_nobreak">';
 4308:     if ($type eq 'email') {
 4309:         $output .= &mt("Formats disallowed for $text{$type}: ");
 4310:     } else {
 4311:         $output .= &mt("Format rules to check for $text{$type}: ");
 4312:     }
 4313:     $output .= '</span></td>'.
 4314:                '<td class="LC_left_item" colspan="2"><table>';
 4315:     my $rem;
 4316:     if (ref($ruleorder) eq 'ARRAY') {
 4317:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 4318:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 4319:                 my $rem = $i%($numinrow);
 4320:                 if ($rem == 0) {
 4321:                     if ($i > 0) {
 4322:                         $output .= '</tr>';
 4323:                     }
 4324:                     $output .= '<tr>';
 4325:                 }
 4326:                 my $check = ' ';
 4327:                 if (ref($settings) eq 'HASH') {
 4328:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 4329:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 4330:                             $check = ' checked="checked" ';
 4331:                         }
 4332:                     }
 4333:                 }
 4334:                 $output .= '<td class="LC_left_item">'.
 4335:                            '<span class="LC_nobreak"><label>'.
 4336:                            '<input type="checkbox" name="'.$type.'_rule" '.
 4337:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 4338:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 4339:             }
 4340:         }
 4341:         $rem = @{$ruleorder}%($numinrow);
 4342:     }
 4343:     my $colsleft = $numinrow - $rem;
 4344:     if ($colsleft > 1 ) {
 4345:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4346:                    '&nbsp;</td>';
 4347:     } elsif ($colsleft == 1) {
 4348:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4349:     }
 4350:     $output .= '</tr></table></td></tr>';
 4351:     return $output;
 4352: }
 4353: 
 4354: sub usercreation_types {
 4355:     my %lt = &Apache::lonlocal::texthash (
 4356:                     author     => 'When adding a co-author',
 4357:                     course     => 'When adding a user to a course',
 4358:                     requestcrs => 'When requesting a course',
 4359:                     any        => 'Any',
 4360:                     official   => 'Institutional only ',
 4361:                     unofficial => 'Non-institutional only',
 4362:                     none       => 'None',
 4363:     );
 4364:     return %lt;
 4365: }
 4366: 
 4367: sub selfcreation_types {
 4368:     my %lt = &Apache::lonlocal::texthash (
 4369:                     selfcreate => 'User creates own account',
 4370:                     any        => 'Any',
 4371:                     official   => 'Institutional only ',
 4372:                     unofficial => 'Non-institutional only',
 4373:                     email      => 'E-mail address',
 4374:                     login      => 'Institutional Login',
 4375:                     sso        => 'SSO',
 4376:              );
 4377: }
 4378: 
 4379: sub authtype_names {
 4380:     my %lt = &Apache::lonlocal::texthash(
 4381:                       int    => 'Internal',
 4382:                       krb4   => 'Kerberos 4',
 4383:                       krb5   => 'Kerberos 5',
 4384:                       loc    => 'Local',
 4385:                   );
 4386:     return %lt;
 4387: }
 4388: 
 4389: sub context_names {
 4390:     my %context_title = &Apache::lonlocal::texthash(
 4391:        author => 'Creating users when an Author',
 4392:        course => 'Creating users when in a course',
 4393:        domain => 'Creating users when a Domain Coordinator',
 4394:     );
 4395:     return %context_title;
 4396: }
 4397: 
 4398: sub print_usermodification {
 4399:     my ($position,$dom,$settings,$rowtotal) = @_;
 4400:     my $numinrow = 4;
 4401:     my ($context,$datatable,$rowcount);
 4402:     if ($position eq 'top') {
 4403:         $rowcount = 0;
 4404:         $context = 'author'; 
 4405:         foreach my $role ('ca','aa') {
 4406:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4407:                                                    $numinrow,$rowcount);
 4408:             $$rowtotal ++;
 4409:             $rowcount ++;
 4410:         }
 4411:     } elsif ($position eq 'bottom') {
 4412:         $context = 'course';
 4413:         $rowcount = 0;
 4414:         foreach my $role ('st','ep','ta','in','cr') {
 4415:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4416:                                                    $numinrow,$rowcount);
 4417:             $$rowtotal ++;
 4418:             $rowcount ++;
 4419:         }
 4420:     }
 4421:     return $datatable;
 4422: }
 4423: 
 4424: sub print_defaults {
 4425:     my ($position,$dom,$settings,$rowtotal) = @_;
 4426:     my $rownum = 0;
 4427:     my ($datatable,$css_class);
 4428:     if ($position eq 'top') {
 4429:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 4430:                      'datelocale_def','portal_def');
 4431:         my %defaults;
 4432:         if (ref($settings) eq 'HASH') {
 4433:             %defaults = %{$settings};
 4434:         } else {
 4435:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 4436:             foreach my $item (@items) {
 4437:                 $defaults{$item} = $domdefaults{$item};
 4438:             }
 4439:         }
 4440:         my $titles = &defaults_titles($dom);
 4441:         foreach my $item (@items) {
 4442:             if ($rownum%2) {
 4443:                 $css_class = '';
 4444:             } else {
 4445:                 $css_class = ' class="LC_odd_row" ';
 4446:             }
 4447:             $datatable .= '<tr'.$css_class.'>'.
 4448:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 4449:                           '</span></td><td class="LC_right_item" colspan="3">';
 4450:             if ($item eq 'auth_def') {
 4451:                 my @authtypes = ('internal','krb4','krb5','localauth');
 4452:                 my %shortauth = (
 4453:                                  internal => 'int',
 4454:                                  krb4 => 'krb4',
 4455:                                  krb5 => 'krb5',
 4456:                                  localauth  => 'loc'
 4457:                                 );
 4458:                 my %authnames = &authtype_names();
 4459:                 foreach my $auth (@authtypes) {
 4460:                     my $checked = ' ';
 4461:                     if ($defaults{$item} eq $auth) {
 4462:                         $checked = ' checked="checked" ';
 4463:                     }
 4464:                     $datatable .= '<label><input type="radio" name="'.$item.
 4465:                                   '" value="'.$auth.'"'.$checked.'/>'.
 4466:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 4467:                 }
 4468:             } elsif ($item eq 'timezone_def') {
 4469:                 my $includeempty = 1;
 4470:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 4471:             } elsif ($item eq 'datelocale_def') {
 4472:                 my $includeempty = 1;
 4473:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 4474:             } elsif ($item eq 'lang_def') {
 4475:                 my %langchoices = &get_languages_hash();
 4476:                 $langchoices{''} = 'No language preference';
 4477:                 %langchoices = &Apache::lonlocal::texthash(%langchoices);
 4478:                 $datatable .= &Apache::loncommon::select_form($defaults{$item},$item,
 4479:                                                               \%langchoices);
 4480:             } else {
 4481:                 my $size;
 4482:                 if ($item eq 'portal_def') {
 4483:                     $size = ' size="25"';
 4484:                 }
 4485:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 4486:                               $defaults{$item}.'"'.$size.' />';
 4487:             }
 4488:             $datatable .= '</td></tr>';
 4489:             $rownum ++;
 4490:         }
 4491:     } else {
 4492:         my (%defaults);
 4493:         if (ref($settings) eq 'HASH') {
 4494:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH') &&
 4495:                 (ref($settings->{'inststatusguest'}) eq 'ARRAY')) {
 4496:                 my $maxnum = @{$settings->{'inststatusorder'}};
 4497:                 for (my $i=0; $i<$maxnum; $i++) {
 4498:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 4499:                     my $item = $settings->{'inststatusorder'}->[$i];
 4500:                     my $title = $settings->{'inststatustypes'}->{$item};
 4501:                     my $guestok;
 4502:                     if (grep(/^\Q$item\E$/,@{$settings->{'inststatusguest'}})) {
 4503:                         $guestok = 1;
 4504:                     }
 4505:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 4506:                     $datatable .= '<tr'.$css_class.'>'.
 4507:                                   '<td><span class="LC_nobreak">'.
 4508:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 4509:                     for (my $k=0; $k<=$maxnum; $k++) {
 4510:                         my $vpos = $k+1;
 4511:                         my $selstr;
 4512:                         if ($k == $i) {
 4513:                             $selstr = ' selected="selected" ';
 4514:                         }
 4515:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4516:                     }
 4517:                     my ($checkedon,$checkedoff);
 4518:                     $checkedoff = ' checked="checked"';
 4519:                     if ($guestok) {
 4520:                         $checkedon = $checkedoff;
 4521:                         $checkedoff = ''; 
 4522:                     }
 4523:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 4524:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 4525:                                   &mt('delete').'</span></td>'.
 4526:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed:').
 4527:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 4528:                                   '</span></td>'.
 4529:                                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4530:                                   '<label><input type="radio" value="1" name="inststatus_guest_'.$item.'"'.$checkedon.' />'.
 4531:                                   &mt('Yes').'</label>'.('&nbsp;'x2).
 4532:                                   '<label><input type="radio" value="0" name="inststatus_guest_'.$item.'"'.$checkedoff.' />'.
 4533:                                   &mt('No').'</label></span></td></tr>';
 4534:                 }
 4535:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 4536:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 4537:                 $datatable .= '<tr '.$css_class.'>'.
 4538:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 4539:                 for (my $k=0; $k<=$maxnum; $k++) {
 4540:                     my $vpos = $k+1;
 4541:                     my $selstr;
 4542:                     if ($k == $maxnum) {
 4543:                         $selstr = ' selected="selected" ';
 4544:                     }
 4545:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4546:                 }
 4547:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 4548:                               '<input type="text" size="10" name="addinststatus" value="" /></span>'.
 4549:                               '&nbsp;'.&mt('(new)').
 4550:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 4551:                               &mt('Name displayed:').
 4552:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 4553:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 4554:                               '<label><input type="radio" value="1" name="addinststatus_guest" />'.
 4555:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 4556:                               '<label><input type="radio" value="0" name="addinststatus_guest" />'.
 4557:                               &mt('No').'</label></span></td></tr>';
 4558:                               '</tr>'."\n";
 4559:                 $rownum ++;
 4560:             }
 4561:         }
 4562:     }
 4563:     $$rowtotal += $rownum;
 4564:     return $datatable;
 4565: }
 4566: 
 4567: sub get_languages_hash {
 4568:     my %langchoices;
 4569:     foreach my $id (&Apache::loncommon::languageids()) {
 4570:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 4571:         if ($code ne '') {
 4572:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 4573:         }
 4574:     }
 4575:     return %langchoices;
 4576: }
 4577: 
 4578: sub defaults_titles {
 4579:     my ($dom) = @_;
 4580:     my %titles = &Apache::lonlocal::texthash (
 4581:                    'auth_def'      => 'Default authentication type',
 4582:                    'auth_arg_def'  => 'Default authentication argument',
 4583:                    'lang_def'      => 'Default language',
 4584:                    'timezone_def'  => 'Default timezone',
 4585:                    'datelocale_def' => 'Default locale for dates',
 4586:                    'portal_def'     => 'Portal/Default URL',
 4587:                  );
 4588:     if ($dom) {
 4589:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 4590:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 4591:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 4592:         $protocol = 'http' if ($protocol ne 'https');
 4593:         if ($uint_dom) {
 4594:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 4595:                                          $uint_dom);
 4596:         }
 4597:     }
 4598:     return (\%titles);
 4599: }
 4600: 
 4601: sub print_scantronformat {
 4602:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 4603:     my $itemcount = 1;
 4604:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 4605:         %confhash);
 4606:     my $switchserver = &check_switchserver($dom,$confname);
 4607:     my %lt = &Apache::lonlocal::texthash (
 4608:                 default => 'Default bubblesheet format file error',
 4609:                 custom  => 'Custom bubblesheet format file error',
 4610:              );
 4611:     my %scantronfiles = (
 4612:         default => 'default.tab',
 4613:         custom => 'custom.tab',
 4614:     );
 4615:     foreach my $key (keys(%scantronfiles)) {
 4616:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 4617:                               .$scantronfiles{$key};
 4618:     }
 4619:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 4620:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 4621:         if (!$switchserver) {
 4622:             my $servadm = $r->dir_config('lonAdmEMail');
 4623:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 4624:             if ($configuserok eq 'ok') {
 4625:                 if ($author_ok eq 'ok') {
 4626:                     my %legacyfile = (
 4627:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 4628:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 4629:                     );
 4630:                     my %md5chk;
 4631:                     foreach my $type (keys(%legacyfile)) {
 4632:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 4633:                         chomp($md5chk{$type});
 4634:                     }
 4635:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 4636:                         foreach my $type (keys(%legacyfile)) {
 4637:                             ($scantronurls{$type},my $error) = 
 4638:                                 &legacy_scantronformat($r,$dom,$confname,
 4639:                                                  $type,$legacyfile{$type},
 4640:                                                  $scantronurls{$type},
 4641:                                                  $scantronfiles{$type});
 4642:                             if ($error ne '') {
 4643:                                 $error{$type} = $error;
 4644:                             }
 4645:                         }
 4646:                         if (keys(%error) == 0) {
 4647:                             $is_custom = 1;
 4648:                             $confhash{'scantron'}{'scantronformat'} = 
 4649:                                 $scantronurls{'custom'};
 4650:                             my $putresult = 
 4651:                                 &Apache::lonnet::put_dom('configuration',
 4652:                                                          \%confhash,$dom);
 4653:                             if ($putresult ne 'ok') {
 4654:                                 $error{'custom'} = 
 4655:                                     '<span class="LC_error">'.
 4656:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4657:                             }
 4658:                         }
 4659:                     } else {
 4660:                         ($scantronurls{'default'},my $error) =
 4661:                             &legacy_scantronformat($r,$dom,$confname,
 4662:                                           'default',$legacyfile{'default'},
 4663:                                           $scantronurls{'default'},
 4664:                                           $scantronfiles{'default'});
 4665:                         if ($error eq '') {
 4666:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 4667:                             my $putresult =
 4668:                                 &Apache::lonnet::put_dom('configuration',
 4669:                                                          \%confhash,$dom);
 4670:                             if ($putresult ne 'ok') {
 4671:                                 $error{'default'} =
 4672:                                     '<span class="LC_error">'.
 4673:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4674:                             }
 4675:                         } else {
 4676:                             $error{'default'} = $error;
 4677:                         }
 4678:                     }
 4679:                 }
 4680:             }
 4681:         } else {
 4682:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 4683:         }
 4684:     }
 4685:     if (ref($settings) eq 'HASH') {
 4686:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 4687:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 4688:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 4689:                 $scantronurl = '';
 4690:             } else {
 4691:                 $scantronurl = $settings->{'scantronformat'};
 4692:             }
 4693:             $is_custom = 1;
 4694:         } else {
 4695:             $scantronurl = $scantronurls{'default'};
 4696:         }
 4697:     } else {
 4698:         if ($is_custom) {
 4699:             $scantronurl = $scantronurls{'custom'};
 4700:         } else {
 4701:             $scantronurl = $scantronurls{'default'};
 4702:         }
 4703:     }
 4704:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4705:     $datatable .= '<tr'.$css_class.'>';
 4706:     if (!$is_custom) {
 4707:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 4708:                       '<span class="LC_nobreak">';
 4709:         if ($scantronurl) {
 4710:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 4711:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 4712:         } else {
 4713:             $datatable = &mt('File unavailable for display');
 4714:         }
 4715:         $datatable .= '</span></td>';
 4716:         if (keys(%error) == 0) { 
 4717:             $datatable .= '<td valign="bottom">';
 4718:             if (!$switchserver) {
 4719:                 $datatable .= &mt('Upload:').'<br />';
 4720:             }
 4721:         } else {
 4722:             my $errorstr;
 4723:             foreach my $key (sort(keys(%error))) {
 4724:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4725:             }
 4726:             $datatable .= '<td>'.$errorstr;
 4727:         }
 4728:     } else {
 4729:         if (keys(%error) > 0) {
 4730:             my $errorstr;
 4731:             foreach my $key (sort(keys(%error))) {
 4732:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4733:             } 
 4734:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 4735:         } elsif ($scantronurl) {
 4736:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 4737:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 4738:             $datatable .= '<td><span class="LC_nobreak">'.
 4739:                           $link.
 4740:                           '<label><input type="checkbox" name="scantronformat_del"'.
 4741:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 4742:                           '<td><span class="LC_nobreak">&nbsp;'.
 4743:                           &mt('Replace:').'</span><br />';
 4744:         }
 4745:     }
 4746:     if (keys(%error) == 0) {
 4747:         if ($switchserver) {
 4748:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4749:         } else {
 4750:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 4751:                          '<input type="file" name="scantronformat" /></span>';
 4752:         }
 4753:     }
 4754:     $datatable .= '</td></tr>';
 4755:     $$rowtotal ++;
 4756:     return $datatable;
 4757: }
 4758: 
 4759: sub legacy_scantronformat {
 4760:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 4761:     my ($url,$error);
 4762:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 4763:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 4764:         (my $result,$url) =
 4765:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 4766:                          '','',$newfile);
 4767:         if ($result ne 'ok') {
 4768:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 4769:         }
 4770:     }
 4771:     return ($url,$error);
 4772: }
 4773: 
 4774: sub print_coursecategories {
 4775:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 4776:     my $datatable;
 4777:     if ($position eq 'top') {
 4778:         my (%checked);
 4779:         my @catitems = ('unauth','auth');
 4780:         my @cattypes = ('std','domonly','codesrch','none');
 4781:         $checked{'unauth'} = 'std';
 4782:         $checked{'auth'} = 'std';
 4783:         if (ref($settings) eq 'HASH') {
 4784:             foreach my $type (@cattypes) {
 4785:                 if ($type eq $settings->{'unauth'}) {
 4786:                     $checked{'unauth'} = $type;
 4787:                 }
 4788:                 if ($type eq $settings->{'auth'}) {
 4789:                     $checked{'auth'} = $type;
 4790:                 }
 4791:             }
 4792:         }
 4793:         my %lt = &Apache::lonlocal::texthash (
 4794:                                                unauth   => 'Catalog type for unauthenticated users',
 4795:                                                auth     => 'Catalog type for authenticated users',
 4796:                                                none     => 'No catalog',
 4797:                                                std      => 'Standard catalog',
 4798:                                                domonly  => 'Domain-only catalog',
 4799:                                                codesrch => "Code search form",
 4800:                                              );
 4801:        my $itemcount = 0;
 4802:        foreach my $item (@catitems) {
 4803:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 4804:            $datatable .= '<tr '.$css_class.'>'.
 4805:                          '<td>'.$lt{$item}.'</td>'.
 4806:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 4807:            foreach my $type (@cattypes) {
 4808:                my $ischecked;
 4809:                if ($checked{$item} eq $type) {
 4810:                    $ischecked=' checked="checked"';
 4811:                }
 4812:                $datatable .= '<label>'.
 4813:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 4814:                              ' />'.$lt{$type}.'</label>&nbsp;';
 4815:            }
 4816:            $datatable .= '</td></tr>';
 4817:            $itemcount ++;
 4818:         }
 4819:         $$rowtotal += $itemcount;
 4820:     } elsif ($position eq 'middle') {
 4821:         my $toggle_cats_crs = ' ';
 4822:         my $toggle_cats_dom = ' checked="checked" ';
 4823:         my $can_cat_crs = ' ';
 4824:         my $can_cat_dom = ' checked="checked" ';
 4825:         my $toggle_catscomm_comm = ' ';
 4826:         my $toggle_catscomm_dom = ' checked="checked" ';
 4827:         my $can_catcomm_comm = ' ';
 4828:         my $can_catcomm_dom = ' checked="checked" ';
 4829: 
 4830:         if (ref($settings) eq 'HASH') {
 4831:             if ($settings->{'togglecats'} eq 'crs') {
 4832:                 $toggle_cats_crs = $toggle_cats_dom;
 4833:                 $toggle_cats_dom = ' ';
 4834:             }
 4835:             if ($settings->{'categorize'} eq 'crs') {
 4836:                 $can_cat_crs = $can_cat_dom;
 4837:                 $can_cat_dom = ' ';
 4838:             }
 4839:             if ($settings->{'togglecatscomm'} eq 'comm') {
 4840:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 4841:                 $toggle_catscomm_dom = ' ';
 4842:             }
 4843:             if ($settings->{'categorizecomm'} eq 'comm') {
 4844:                 $can_catcomm_comm = $can_catcomm_dom;
 4845:                 $can_catcomm_dom = ' ';
 4846:             }
 4847:         }
 4848:         my %title = &Apache::lonlocal::texthash (
 4849:                      togglecats     => 'Show/Hide a course in catalog',
 4850:                      togglecatscomm => 'Show/Hide a community in catalog',
 4851:                      categorize     => 'Assign a category to a course',
 4852:                      categorizecomm => 'Assign a category to a community',
 4853:                     );
 4854:         my %level = &Apache::lonlocal::texthash (
 4855:                      dom  => 'Set in Domain',
 4856:                      crs  => 'Set in Course',
 4857:                      comm => 'Set in Community',
 4858:                     );
 4859:         $datatable = '<tr class="LC_odd_row">'.
 4860:                   '<td>'.$title{'togglecats'}.'</td>'.
 4861:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4862:                   '<input type="radio" name="togglecats"'.
 4863:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4864:                   '<label><input type="radio" name="togglecats"'.
 4865:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4866:                   '</tr><tr>'.
 4867:                   '<td>'.$title{'categorize'}.'</td>'.
 4868:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4869:                   '<label><input type="radio" name="categorize"'.
 4870:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4871:                   '<label><input type="radio" name="categorize"'.
 4872:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4873:                   '</tr><tr class="LC_odd_row">'.
 4874:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 4875:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4876:                   '<input type="radio" name="togglecatscomm"'.
 4877:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4878:                   '<label><input type="radio" name="togglecatscomm"'.
 4879:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4880:                   '</tr><tr>'.
 4881:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 4882:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4883:                   '<label><input type="radio" name="categorizecomm"'.
 4884:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4885:                   '<label><input type="radio" name="categorizecomm"'.
 4886:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4887:                   '</tr>';
 4888:         $$rowtotal += 4;
 4889:     } else {
 4890:         my $css_class;
 4891:         my $itemcount = 1;
 4892:         my $cathash; 
 4893:         if (ref($settings) eq 'HASH') {
 4894:             $cathash = $settings->{'cats'};
 4895:         }
 4896:         if (ref($cathash) eq 'HASH') {
 4897:             my (@cats,@trails,%allitems,%idx,@jsarray);
 4898:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 4899:                                                    \%allitems,\%idx,\@jsarray);
 4900:             my $maxdepth = scalar(@cats);
 4901:             my $colattrib = '';
 4902:             if ($maxdepth > 2) {
 4903:                 $colattrib = ' colspan="2" ';
 4904:             }
 4905:             my @path;
 4906:             if (@cats > 0) {
 4907:                 if (ref($cats[0]) eq 'ARRAY') {
 4908:                     my $numtop = @{$cats[0]};
 4909:                     my $maxnum = $numtop;
 4910:                     my %default_names = (
 4911:                           instcode    => &mt('Official courses'),
 4912:                           communities => &mt('Communities'),
 4913:                     );
 4914: 
 4915:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 4916:                         ($cathash->{'instcode::0'} eq '') ||
 4917:                         (!grep(/^communities$/,@{$cats[0]})) || 
 4918:                         ($cathash->{'communities::0'} eq '')) {
 4919:                         $maxnum ++;
 4920:                     }
 4921:                     my $lastidx;
 4922:                     for (my $i=0; $i<$numtop; $i++) {
 4923:                         my $parent = $cats[0][$i];
 4924:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4925:                         my $item = &escape($parent).'::0';
 4926:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 4927:                         $lastidx = $idx{$item};
 4928:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4929:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 4930:                         for (my $k=0; $k<=$maxnum; $k++) {
 4931:                             my $vpos = $k+1;
 4932:                             my $selstr;
 4933:                             if ($k == $i) {
 4934:                                 $selstr = ' selected="selected" ';
 4935:                             }
 4936:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4937:                         }
 4938:                         $datatable .= '</select></span></td><td>';
 4939:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 4940:                             $datatable .=  '<span class="LC_nobreak">'
 4941:                                            .$default_names{$parent}.'</span>';
 4942:                             if ($parent eq 'instcode') {
 4943:                                 $datatable .= '<br /><span class="LC_nobreak">('
 4944:                                               .&mt('with institutional codes')
 4945:                                               .')</span></td><td'.$colattrib.'>';
 4946:                             } else {
 4947:                                 $datatable .= '<table><tr><td>';
 4948:                             }
 4949:                             $datatable .= '<span class="LC_nobreak">'
 4950:                                           .'<label><input type="radio" name="'
 4951:                                           .$parent.'" value="1" checked="checked" />'
 4952:                                           .&mt('Display').'</label>';
 4953:                             if ($parent eq 'instcode') {
 4954:                                 $datatable .= '&nbsp;';
 4955:                             } else {
 4956:                                 $datatable .= '</span></td></tr><tr><td>'
 4957:                                               .'<span class="LC_nobreak">';
 4958:                             }
 4959:                             $datatable .= '<label><input type="radio" name="'
 4960:                                           .$parent.'" value="0" />'
 4961:                                           .&mt('Do not display').'</label></span>';
 4962:                             if ($parent eq 'communities') {
 4963:                                 $datatable .= '</td></tr></table>';
 4964:                             }
 4965:                             $datatable .= '</td>';
 4966:                         } else {
 4967:                             $datatable .= $parent
 4968:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 4969:                                           .'<input type="checkbox" name="deletecategory" '
 4970:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 4971:                         }
 4972:                         my $depth = 1;
 4973:                         push(@path,$parent);
 4974:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 4975:                         pop(@path);
 4976:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 4977:                         $itemcount ++;
 4978:                     }
 4979:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4980:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 4981:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 4982:                     for (my $k=0; $k<=$maxnum; $k++) {
 4983:                         my $vpos = $k+1;
 4984:                         my $selstr;
 4985:                         if ($k == $numtop) {
 4986:                             $selstr = ' selected="selected" ';
 4987:                         }
 4988:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4989:                     }
 4990:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 4991:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 4992:                                   .'</tr>'."\n";
 4993:                     $itemcount ++;
 4994:                     foreach my $default ('instcode','communities') {
 4995:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 4996:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4997:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 4998:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 4999:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 5000:                             for (my $k=0; $k<=$maxnum; $k++) {
 5001:                                 my $vpos = $k+1;
 5002:                                 my $selstr;
 5003:                                 if ($k == $maxnum) {
 5004:                                     $selstr = ' selected="selected" ';
 5005:                                 }
 5006:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5007:                             }
 5008:                             $datatable .= '</select></span></td>'.
 5009:                                           '<td><span class="LC_nobreak">'.
 5010:                                           $default_names{$default}.'</span>';
 5011:                             if ($default eq 'instcode') {
 5012:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 5013:                                               .&mt('with institutional codes').')</span>';
 5014:                             }
 5015:                             $datatable .= '</td>'
 5016:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 5017:                                           .&mt('Display').'</label>&nbsp;'
 5018:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 5019:                                           .&mt('Do not display').'</label></span></td></tr>';
 5020:                         }
 5021:                     }
 5022:                 }
 5023:             } else {
 5024:                 $datatable .= &initialize_categories($itemcount);
 5025:             }
 5026:         } else {
 5027:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td>'
 5028:                           .&initialize_categories($itemcount);
 5029:         }
 5030:         $$rowtotal += $itemcount;
 5031:     }
 5032:     return $datatable;
 5033: }
 5034: 
 5035: sub print_serverstatuses {
 5036:     my ($dom,$settings,$rowtotal) = @_;
 5037:     my $datatable;
 5038:     my @pages = &serverstatus_pages();
 5039:     my (%namedaccess,%machineaccess);
 5040:     foreach my $type (@pages) {
 5041:         $namedaccess{$type} = '';
 5042:         $machineaccess{$type}= '';
 5043:     }
 5044:     if (ref($settings) eq 'HASH') {
 5045:         foreach my $type (@pages) {
 5046:             if (exists($settings->{$type})) {
 5047:                 if (ref($settings->{$type}) eq 'HASH') {
 5048:                     foreach my $key (keys(%{$settings->{$type}})) {
 5049:                         if ($key eq 'namedusers') {
 5050:                             $namedaccess{$type} = $settings->{$type}->{$key};
 5051:                         } elsif ($key eq 'machines') {
 5052:                             $machineaccess{$type} = $settings->{$type}->{$key};
 5053:                         }
 5054:                     }
 5055:                 }
 5056:             }
 5057:         }
 5058:     }
 5059:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 5060:     my $rownum = 0;
 5061:     my $css_class;
 5062:     foreach my $type (@pages) {
 5063:         $rownum ++;
 5064:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 5065:         $datatable .= '<tr'.$css_class.'>'.
 5066:                       '<td><span class="LC_nobreak">'.
 5067:                       $titles->{$type}.'</span></td>'.
 5068:                       '<td class="LC_left_item">'.
 5069:                       '<input type="text" name="'.$type.'_namedusers" '.
 5070:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 5071:                       '<td class="LC_right_item">'.
 5072:                       '<span class="LC_nobreak">'.
 5073:                       '<input type="text" name="'.$type.'_machines" '.
 5074:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 5075:                       '</td></tr>'."\n";
 5076:     }
 5077:     $$rowtotal += $rownum;
 5078:     return $datatable;
 5079: }
 5080: 
 5081: sub serverstatus_pages {
 5082:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 5083:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 5084:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 5085:             'uniquecodes','diskusage');
 5086: }
 5087: 
 5088: sub defaults_javascript {
 5089:     my ($settings) = @_;
 5090:     my ($output,$jstext); 
 5091:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 5092:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 5093:         if ($maxnum eq '') {
 5094:             $maxnum = 0;
 5095:         }
 5096:         $maxnum ++;
 5097:         $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 5098:         return <<"ENDSCRIPT";
 5099: <script type="text/javascript">
 5100: // <![CDATA[
 5101: function reorderTypes(form,caller) {
 5102:     var changedVal;
 5103: $jstext 
 5104:     var newpos = 'addinststatus_pos';
 5105:     var current = new Array;
 5106:     var maxh = $maxnum;
 5107:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 5108:     var oldVal;
 5109:     if (caller == newpos) {
 5110:         changedVal = newitemVal;
 5111:     } else {
 5112:         var curritem = 'inststatus_pos_'+caller;
 5113:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 5114:         current[newitemVal] = newpos;
 5115:     }
 5116:     for (var i=0; i<inststatuses.length; i++) {
 5117:         if (inststatuses[i] != caller) {
 5118:             var elementName = 'inststatus_pos_'+inststatuses[i];
 5119:             if (form.elements[elementName]) {
 5120:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 5121:                 current[currVal] = elementName;
 5122:             }
 5123:         }
 5124:     }
 5125:     for (var j=0; j<maxh; j++) {
 5126:         if (current[j] == undefined) {
 5127:             oldVal = j;
 5128:         }
 5129:     }
 5130:     if (oldVal < changedVal) {
 5131:         for (var k=oldVal+1; k<=changedVal ; k++) {
 5132:            var elementName = current[k];
 5133:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 5134:         }
 5135:     } else {
 5136:         for (var k=changedVal; k<oldVal; k++) {
 5137:             var elementName = current[k];
 5138:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 5139:         }
 5140:     }
 5141:     return;
 5142: }
 5143: 
 5144: // ]]>
 5145: </script>
 5146: 
 5147: ENDSCRIPT
 5148:     }
 5149: }
 5150: 
 5151: sub coursecategories_javascript {
 5152:     my ($settings) = @_;
 5153:     my ($output,$jstext,$cathash);
 5154:     if (ref($settings) eq 'HASH') {
 5155:         $cathash = $settings->{'cats'};
 5156:     }
 5157:     if (ref($cathash) eq 'HASH') {
 5158:         my (@cats,@jsarray,%idx);
 5159:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 5160:         if (@jsarray > 0) {
 5161:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 5162:             for (my $i=0; $i<@jsarray; $i++) {
 5163:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 5164:                     my $catstr = join('","',@{$jsarray[$i]});
 5165:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 5166:                 }
 5167:             }
 5168:         }
 5169:     } else {
 5170:         $jstext  = '    var categories = Array(1);'."\n".
 5171:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 5172:     }
 5173:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 5174:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 5175:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category.'); 
 5176:     $output = <<"ENDSCRIPT";
 5177: <script type="text/javascript">
 5178: // <![CDATA[
 5179: function reorderCats(form,parent,item,idx) {
 5180:     var changedVal;
 5181: $jstext
 5182:     var newpos = 'addcategory_pos';
 5183:     if (parent == '') {
 5184:         var has_instcode = 0;
 5185:         var maxtop = categories[idx].length;
 5186:         for (var j=0; j<maxtop; j++) {
 5187:             if (categories[idx][j] == 'instcode::0') {
 5188:                 has_instcode == 1;
 5189:             }
 5190:         }
 5191:         if (has_instcode == 0) {
 5192:             categories[idx][maxtop] = 'instcode_pos';
 5193:         }
 5194:     } else {
 5195:         newpos += '_'+parent;
 5196:     }
 5197:     var maxh = 1 + categories[idx].length;
 5198:     var current = new Array;
 5199:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 5200:     if (item == newpos) {
 5201:         changedVal = newitemVal;
 5202:     } else {
 5203:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 5204:         current[newitemVal] = newpos;
 5205:     }
 5206:     for (var i=0; i<categories[idx].length; i++) {
 5207:         var elementName = categories[idx][i];
 5208:         if (elementName != item) {
 5209:             if (form.elements[elementName]) {
 5210:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 5211:                 current[currVal] = elementName;
 5212:             }
 5213:         }
 5214:     }
 5215:     var oldVal;
 5216:     for (var j=0; j<maxh; j++) {
 5217:         if (current[j] == undefined) {
 5218:             oldVal = j;
 5219:         }
 5220:     }
 5221:     if (oldVal < changedVal) {
 5222:         for (var k=oldVal+1; k<=changedVal ; k++) {
 5223:            var elementName = current[k];
 5224:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 5225:         }
 5226:     } else {
 5227:         for (var k=changedVal; k<oldVal; k++) {
 5228:             var elementName = current[k];
 5229:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 5230:         }
 5231:     }
 5232:     return;
 5233: }
 5234: 
 5235: function categoryCheck(form) {
 5236:     if (form.elements['addcategory_name'].value == 'instcode') {
 5237:         alert('$instcode_reserved\\n$choose_again');
 5238:         return false;
 5239:     }
 5240:     if (form.elements['addcategory_name'].value == 'communities') {
 5241:         alert('$communities_reserved\\n$choose_again');
 5242:         return false;
 5243:     }
 5244:     return true;
 5245: }
 5246: 
 5247: // ]]>
 5248: </script>
 5249: 
 5250: ENDSCRIPT
 5251:     return $output;
 5252: }
 5253: 
 5254: sub initialize_categories {
 5255:     my ($itemcount) = @_;
 5256:     my ($datatable,$css_class,$chgstr);
 5257:     my %default_names = (
 5258:                       instcode    => 'Official courses (with institutional codes)',
 5259:                       communities => 'Communities',
 5260:                         );
 5261:     my $select0 = ' selected="selected"';
 5262:     my $select1 = '';
 5263:     foreach my $default ('instcode','communities') {
 5264:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5265:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 5266:         if ($default eq 'communities') {
 5267:             $select1 = $select0;
 5268:             $select0 = '';
 5269:         }
 5270:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5271:                      .'<select name="'.$default.'_pos">'
 5272:                      .'<option value="0"'.$select0.'>1</option>'
 5273:                      .'<option value="1"'.$select1.'>2</option>'
 5274:                      .'<option value="2">3</option></select>&nbsp;'
 5275:                      .$default_names{$default}
 5276:                      .'</span></td><td><span class="LC_nobreak">'
 5277:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 5278:                      .&mt('Display').'</label>&nbsp;<label>'
 5279:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 5280:                  .'</label></span></td></tr>';
 5281:         $itemcount ++;
 5282:     }
 5283:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5284:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 5285:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5286:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 5287:                   .'<option value="0">1</option>'
 5288:                   .'<option value="1">2</option>'
 5289:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 5290:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 5291:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 5292:     return $datatable;
 5293: }
 5294: 
 5295: sub build_category_rows {
 5296:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 5297:     my ($text,$name,$item,$chgstr);
 5298:     if (ref($cats) eq 'ARRAY') {
 5299:         my $maxdepth = scalar(@{$cats});
 5300:         if (ref($cats->[$depth]) eq 'HASH') {
 5301:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 5302:                 my $numchildren = @{$cats->[$depth]{$parent}};
 5303:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5304:                 $text .= '<td><table class="LC_data_table">';
 5305:                 my ($idxnum,$parent_name,$parent_item);
 5306:                 my $higher = $depth - 1;
 5307:                 if ($higher == 0) {
 5308:                     $parent_name = &escape($parent).'::'.$higher;
 5309:                 } else {
 5310:                     if (ref($path) eq 'ARRAY') {
 5311:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 5312:                     }
 5313:                 }
 5314:                 $parent_item = 'addcategory_pos_'.$parent_name;
 5315:                 for (my $j=0; $j<=$numchildren; $j++) {
 5316:                     if ($j < $numchildren) {
 5317:                         $name = $cats->[$depth]{$parent}[$j];
 5318:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 5319:                         $idxnum = $idx->{$item};
 5320:                     } else {
 5321:                         $name = $parent_name;
 5322:                         $item = $parent_item;
 5323:                     }
 5324:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 5325:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 5326:                     for (my $i=0; $i<=$numchildren; $i++) {
 5327:                         my $vpos = $i+1;
 5328:                         my $selstr;
 5329:                         if ($j == $i) {
 5330:                             $selstr = ' selected="selected" ';
 5331:                         }
 5332:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 5333:                     }
 5334:                     $text .= '</select>&nbsp;';
 5335:                     if ($j < $numchildren) {
 5336:                         my $deeper = $depth+1;
 5337:                         $text .= $name.'&nbsp;'
 5338:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 5339:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 5340:                         if(ref($path) eq 'ARRAY') {
 5341:                             push(@{$path},$name);
 5342:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 5343:                             pop(@{$path});
 5344:                         }
 5345:                     } else {
 5346:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 5347:                         if ($j == $numchildren) {
 5348:                             $text .= $name;
 5349:                         } else {
 5350:                             $text .= $item;
 5351:                         }
 5352:                         $text .= '" value="" />';
 5353:                     }
 5354:                     $text .= '</td></tr>';
 5355:                 }
 5356:                 $text .= '</table></td>';
 5357:             } else {
 5358:                 my $higher = $depth-1;
 5359:                 if ($higher == 0) {
 5360:                     $name = &escape($parent).'::'.$higher;
 5361:                 } else {
 5362:                     if (ref($path) eq 'ARRAY') {
 5363:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 5364:                     }
 5365:                 }
 5366:                 my $colspan;
 5367:                 if ($parent ne 'instcode') {
 5368:                     $colspan = $maxdepth - $depth - 1;
 5369:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 5370:                 }
 5371:             }
 5372:         }
 5373:     }
 5374:     return $text;
 5375: }
 5376: 
 5377: sub modifiable_userdata_row {
 5378:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref) = @_;
 5379:     my ($role,$rolename,$statustype);
 5380:     $role = $item;
 5381:     if ($context eq 'cancreate') {
 5382:         if ($item =~ /^emailusername_(.+)$/) {
 5383:             $statustype = $1;
 5384:             $role = 'emailusername';
 5385:             if (ref($usertypes) eq 'HASH') {
 5386:                 if ($usertypes->{$statustype}) {
 5387:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 5388:                 } else {
 5389:                     $rolename = &mt('Data provided by user');
 5390:                 }
 5391:             }
 5392:         }
 5393:     } elsif ($context eq 'selfcreate') {
 5394:         if (ref($usertypes) eq 'HASH') {
 5395:             $rolename = $usertypes->{$role};
 5396:         } else {
 5397:             $rolename = $role;
 5398:         }
 5399:     } else {
 5400:         if ($role eq 'cr') {
 5401:             $rolename = &mt('Custom role');
 5402:         } else {
 5403:             $rolename = &Apache::lonnet::plaintext($role);
 5404:         }
 5405:     }
 5406:     my (@fields,%fieldtitles);
 5407:     if (ref($fieldsref) eq 'ARRAY') {
 5408:         @fields = @{$fieldsref};
 5409:     } else {
 5410:         @fields = ('lastname','firstname','middlename','generation',
 5411:                    'permanentemail','id');
 5412:     }
 5413:     if ((ref($titlesref) eq 'HASH')) {
 5414:         %fieldtitles = %{$titlesref};
 5415:     } else {
 5416:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5417:     }
 5418:     my $output;
 5419:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 5420:     $output = '<tr '.$css_class.'>'.
 5421:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 5422:               '<td class="LC_left_item" colspan="2"><table>';
 5423:     my $rem;
 5424:     my %checks;
 5425:     if (ref($settings) eq 'HASH') {
 5426:         if (ref($settings->{$context}) eq 'HASH') {
 5427:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 5428:                 my $hashref = $settings->{$context}->{$role};
 5429:                 if ($role eq 'emailusername') {
 5430:                     if ($statustype) {
 5431:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 5432:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 5433:                             if (ref($hashref) eq 'HASH') { 
 5434:                                 foreach my $field (@fields) {
 5435:                                     if ($hashref->{$field}) {
 5436:                                         $checks{$field} = $hashref->{$field};
 5437:                                     }
 5438:                                 }
 5439:                             }
 5440:                         }
 5441:                     }
 5442:                 } else {
 5443:                     if (ref($hashref) eq 'HASH') {
 5444:                         foreach my $field (@fields) {
 5445:                             if ($hashref->{$field}) {
 5446:                                 $checks{$field} = ' checked="checked" ';
 5447:                             }
 5448:                         }
 5449:                     }
 5450:                 }
 5451:             }
 5452:         }
 5453:     }
 5454:      
 5455:     for (my $i=0; $i<@fields; $i++) {
 5456:         my $rem = $i%($numinrow);
 5457:         if ($rem == 0) {
 5458:             if ($i > 0) {
 5459:                 $output .= '</tr>';
 5460:             }
 5461:             $output .= '<tr>';
 5462:         }
 5463:         my $check = ' ';
 5464:         unless ($role eq 'emailusername') {
 5465:             if (exists($checks{$fields[$i]})) {
 5466:                 $check = $checks{$fields[$i]}
 5467:             } else {
 5468:                 if ($role eq 'st') {
 5469:                     if (ref($settings) ne 'HASH') {
 5470:                         $check = ' checked="checked" '; 
 5471:                     }
 5472:                 }
 5473:             }
 5474:         }
 5475:         $output .= '<td class="LC_left_item">'.
 5476:                    '<span class="LC_nobreak">';
 5477:         if ($role eq 'emailusername') {
 5478:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 5479:                 $checks{$fields[$i]} = 'omit';
 5480:             }
 5481:             foreach my $option ('required','optional','omit') {
 5482:                 my $checked='';
 5483:                 if ($checks{$fields[$i]} eq $option) {
 5484:                     $checked='checked="checked" ';
 5485:                 }
 5486:                 $output .= '<label>'.
 5487:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 5488:                            &mt($option).'</label>'.('&nbsp;' x2);
 5489:             }
 5490:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 5491:         } else {
 5492:             $output .= '<label>'.
 5493:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 5494:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 5495:                        '</label>';
 5496:         }
 5497:         $output .= '</span></td>';
 5498:         $rem = @fields%($numinrow);
 5499:     }
 5500:     my $colsleft = $numinrow - $rem;
 5501:     if ($colsleft > 1 ) {
 5502:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5503:                    '&nbsp;</td>';
 5504:     } elsif ($colsleft == 1) {
 5505:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 5506:     }
 5507:     $output .= '</tr></table></td></tr>';
 5508:     return $output;
 5509: }
 5510: 
 5511: sub insttypes_row {
 5512:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rownum) = @_;
 5513:     my %lt = &Apache::lonlocal::texthash (
 5514:                       cansearch => 'Users allowed to search',
 5515:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 5516:                       lockablenames => 'User preference to lock name',
 5517:              );
 5518:     my $showdom;
 5519:     if ($context eq 'cansearch') {
 5520:         $showdom = ' ('.$dom.')';
 5521:     }
 5522:     my $class = 'LC_left_item';
 5523:     if ($context eq 'statustocreate') {
 5524:         $class = 'LC_right_item';
 5525:     }
 5526:     my $css_class = ' class="LC_odd_row"';
 5527:     if ($rownum ne '') { 
 5528:         $css_class = ($rownum%2? ' class="LC_odd_row"':'');
 5529:     }
 5530:     my $output = '<tr'.$css_class.'>'.
 5531:                  '<td>'.$lt{$context}.$showdom.
 5532:                  '</td><td class="'.$class.'" colspan="2"><table>';
 5533:     my $rem;
 5534:     if (ref($types) eq 'ARRAY') {
 5535:         for (my $i=0; $i<@{$types}; $i++) {
 5536:             if (defined($usertypes->{$types->[$i]})) {
 5537:                 my $rem = $i%($numinrow);
 5538:                 if ($rem == 0) {
 5539:                     if ($i > 0) {
 5540:                         $output .= '</tr>';
 5541:                     }
 5542:                     $output .= '<tr>';
 5543:                 }
 5544:                 my $check = ' ';
 5545:                 if (ref($settings) eq 'HASH') {
 5546:                     if (ref($settings->{$context}) eq 'ARRAY') {
 5547:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 5548:                             $check = ' checked="checked" ';
 5549:                         }
 5550:                     } elsif ($context eq 'statustocreate') {
 5551:                         $check = ' checked="checked" ';
 5552:                     }
 5553:                 }
 5554:                 $output .= '<td class="LC_left_item">'.
 5555:                            '<span class="LC_nobreak"><label>'.
 5556:                            '<input type="checkbox" name="'.$context.'" '.
 5557:                            'value="'.$types->[$i].'"'.$check.'/>'.
 5558:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 5559:             }
 5560:         }
 5561:         $rem = @{$types}%($numinrow);
 5562:     }
 5563:     my $colsleft = $numinrow - $rem;
 5564:     if (($rem == 0) && (@{$types} > 0)) {
 5565:         $output .= '<tr>';
 5566:     }
 5567:     if ($colsleft > 1) {
 5568:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5569:     } else {
 5570:         $output .= '<td class="LC_left_item">';
 5571:     }
 5572:     my $defcheck = ' ';
 5573:     if (ref($settings) eq 'HASH') {  
 5574:         if (ref($settings->{$context}) eq 'ARRAY') {
 5575:             if (grep(/^default$/,@{$settings->{$context}})) {
 5576:                 $defcheck = ' checked="checked" ';
 5577:             }
 5578:         } elsif ($context eq 'statustocreate') {
 5579:             $defcheck = ' checked="checked" ';
 5580:         }
 5581:     }
 5582:     $output .= '<span class="LC_nobreak"><label>'.
 5583:                '<input type="checkbox" name="'.$context.'" '.
 5584:                'value="default"'.$defcheck.'/>'.
 5585:                $othertitle.'</label></span></td>'.
 5586:                '</tr></table></td></tr>';
 5587:     return $output;
 5588: }
 5589: 
 5590: sub sorted_searchtitles {
 5591:     my %searchtitles = &Apache::lonlocal::texthash(
 5592:                          'uname' => 'username',
 5593:                          'lastname' => 'last name',
 5594:                          'lastfirst' => 'last name, first name',
 5595:                      );
 5596:     my @titleorder = ('uname','lastname','lastfirst');
 5597:     return (\%searchtitles,\@titleorder);
 5598: }
 5599: 
 5600: sub sorted_searchtypes {
 5601:     my %srchtypes_desc = (
 5602:                            exact    => 'is exact match',
 5603:                            contains => 'contains ..',
 5604:                            begins   => 'begins with ..',
 5605:                          );
 5606:     my @srchtypeorder = ('exact','begins','contains');
 5607:     return (\%srchtypes_desc,\@srchtypeorder);
 5608: }
 5609: 
 5610: sub usertype_update_row {
 5611:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 5612:     my $datatable;
 5613:     my $numinrow = 4;
 5614:     foreach my $type (@{$types}) {
 5615:         if (defined($usertypes->{$type})) {
 5616:             $$rownums ++;
 5617:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 5618:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 5619:                           '</td><td class="LC_left_item"><table>';
 5620:             for (my $i=0; $i<@{$fields}; $i++) {
 5621:                 my $rem = $i%($numinrow);
 5622:                 if ($rem == 0) {
 5623:                     if ($i > 0) {
 5624:                         $datatable .= '</tr>';
 5625:                     }
 5626:                     $datatable .= '<tr>';
 5627:                 }
 5628:                 my $check = ' ';
 5629:                 if (ref($settings) eq 'HASH') {
 5630:                     if (ref($settings->{'fields'}) eq 'HASH') {
 5631:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 5632:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 5633:                                 $check = ' checked="checked" ';
 5634:                             }
 5635:                         }
 5636:                     }
 5637:                 }
 5638: 
 5639:                 if ($i == @{$fields}-1) {
 5640:                     my $colsleft = $numinrow - $rem;
 5641:                     if ($colsleft > 1) {
 5642:                         $datatable .= '<td colspan="'.$colsleft.'">';
 5643:                     } else {
 5644:                         $datatable .= '<td>';
 5645:                     }
 5646:                 } else {
 5647:                     $datatable .= '<td>';
 5648:                 }
 5649:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5650:                               '<input type="checkbox" name="updateable_'.$type.
 5651:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 5652:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 5653:             }
 5654:             $datatable .= '</tr></table></td></tr>';
 5655:         }
 5656:     }
 5657:     return $datatable;
 5658: }
 5659: 
 5660: sub modify_login {
 5661:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 5662:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 5663:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 5664:     %title = ( coursecatalog => 'Display course catalog',
 5665:                adminmail => 'Display administrator E-mail address',
 5666:                helpdesk  => 'Display "Contact Helpdesk" link',
 5667:                newuser => 'Link for visitors to create a user account',
 5668:                loginheader => 'Log-in box header');
 5669:     @offon = ('off','on');
 5670:     if (ref($domconfig{login}) eq 'HASH') {
 5671:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 5672:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 5673:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 5674:             }
 5675:         }
 5676:     }
 5677:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 5678:                                            \%domconfig,\%loginhash);
 5679:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5680:     foreach my $item (@toggles) {
 5681:         $loginhash{login}{$item} = $env{'form.'.$item};
 5682:     }
 5683:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 5684:     if (ref($colchanges{'login'}) eq 'HASH') {  
 5685:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 5686:                                          \%loginhash);
 5687:     }
 5688: 
 5689:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5690:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 5691:     if (keys(%servers) > 1) {
 5692:         foreach my $lonhost (keys(%servers)) {
 5693:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 5694:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 5695:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 5696:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 5697:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 5698:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5699:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5700:                         $changes{'loginvia'}{$lonhost} = 1;
 5701:                     } else {
 5702:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 5703:                         $changes{'loginvia'}{$lonhost} = 1;
 5704:                     }
 5705:                 } else {
 5706:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5707:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5708:                         $changes{'loginvia'}{$lonhost} = 1;
 5709:                     }
 5710:                 }
 5711:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 5712:                     foreach my $item (@loginvia_attribs) {
 5713:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 5714:                     }
 5715:                 } else {
 5716:                     foreach my $item (@loginvia_attribs) {
 5717:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5718:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5719:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 5720:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5721:                                 $new = '/';
 5722:                             }
 5723:                         }
 5724:                         if (($item eq 'custompath') && 
 5725:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5726:                             $new = '';
 5727:                         }
 5728:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 5729:                             $changes{'loginvia'}{$lonhost} = 1;
 5730:                         }
 5731:                         if ($item eq 'exempt') {
 5732:                             $new =~ s/^\s+//;
 5733:                             $new =~ s/\s+$//;
 5734:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 5735:                             my @okips;
 5736:                             foreach my $ip (@poss_ips) {
 5737:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 5738:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 5739:                                         push(@okips,$ip); 
 5740:                                     }
 5741:                                 }
 5742:                             }
 5743:                             if (@okips > 0) {
 5744:                                 $new = join(',',@okips); 
 5745:                             } else {
 5746:                                 $new = ''; 
 5747:                             }
 5748:                         }
 5749:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5750:                     }
 5751:                 }
 5752:             } else {
 5753:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5754:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5755:                     $changes{'loginvia'}{$lonhost} = 1;
 5756:                     foreach my $item (@loginvia_attribs) {
 5757:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5758:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5759:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5760:                                 $new = '/';
 5761:                             }
 5762:                         }
 5763:                         if (($item eq 'custompath') && 
 5764:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5765:                             $new = '';
 5766:                         }
 5767:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5768:                     }
 5769:                 }
 5770:             }
 5771:         }
 5772:     }
 5773: 
 5774:     my $servadm = $r->dir_config('lonAdmEMail');
 5775:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 5776:     if (ref($domconfig{'login'}) eq 'HASH') {
 5777:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 5778:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 5779:                 if ($lang eq 'nolang') {
 5780:                     push(@currlangs,$lang);
 5781:                 } elsif (defined($langchoices{$lang})) {
 5782:                     push(@currlangs,$lang);
 5783:                 } else {
 5784:                     next;
 5785:                 }
 5786:             }
 5787:         }
 5788:     }
 5789:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 5790:     if (@currlangs > 0) {
 5791:         foreach my $lang (@currlangs) {
 5792:             if (grep(/^\Q$lang\E$/,@delurls)) {
 5793:                 $changes{'helpurl'}{$lang} = 1;
 5794:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 5795:                 $changes{'helpurl'}{$lang} = 1;
 5796:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 5797:                 push(@newlangs,$lang);
 5798:             } else {
 5799:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5800:             }
 5801:         }
 5802:     }
 5803:     unless (grep(/^nolang$/,@currlangs)) {
 5804:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 5805:             $changes{'helpurl'}{'nolang'} = 1;
 5806:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 5807:             push(@newlangs,'nolang');
 5808:         }
 5809:     }
 5810:     if ($env{'form.loginhelpurl_add_lang'}) {
 5811:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 5812:             ($env{'form.loginhelpurl_add_file.filename'})) {
 5813:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 5814:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 5815:         }
 5816:     }
 5817:     if ((@newlangs > 0) || ($addedfile)) {
 5818:         my $error;
 5819:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 5820:         if ($configuserok eq 'ok') {
 5821:             if ($switchserver) {
 5822:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 5823:             } elsif ($author_ok eq 'ok') {
 5824:                 my @allnew = @newlangs;
 5825:                 if ($addedfile ne '') {
 5826:                     push(@allnew,$addedfile);
 5827:                 }
 5828:                 foreach my $lang (@allnew) {
 5829:                     my $formelem = 'loginhelpurl_'.$lang;
 5830:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 5831:                         $formelem = 'loginhelpurl_add_file';
 5832:                     }
 5833:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 5834:                                                                "help/$lang",'','',$newfile{$lang});
 5835:                     if ($result eq 'ok') {
 5836:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 5837:                         $changes{'helpurl'}{$lang} = 1;
 5838:                     } else {
 5839:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 5840:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 5841:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 5842:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 5843: 
 5844:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5845:                         }
 5846:                     }
 5847:                 }
 5848:             } else {
 5849:                 $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);
 5850:             }
 5851:         } else {
 5852:             $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);
 5853:         }
 5854:         if ($error) {
 5855:             &Apache::lonnet::logthis($error);
 5856:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5857:         }
 5858:     }
 5859:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 5860: 
 5861:     my $defaulthelpfile = '/adm/loginproblems.html';
 5862:     my $defaulttext = &mt('Default in use');
 5863: 
 5864:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 5865:                                              $dom);
 5866:     if ($putresult eq 'ok') {
 5867:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5868:         my %defaultchecked = (
 5869:                     'coursecatalog' => 'on',
 5870:                     'helpdesk'      => 'on',
 5871:                     'adminmail'     => 'off',
 5872:                     'newuser'       => 'off',
 5873:         );
 5874:         if (ref($domconfig{'login'}) eq 'HASH') {
 5875:             foreach my $item (@toggles) {
 5876:                 if ($defaultchecked{$item} eq 'on') { 
 5877:                     if (($domconfig{'login'}{$item} eq '0') &&
 5878:                         ($env{'form.'.$item} eq '1')) {
 5879:                         $changes{$item} = 1;
 5880:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 5881:                               $domconfig{'login'}{$item} eq '1') &&
 5882:                              ($env{'form.'.$item} eq '0')) {
 5883:                         $changes{$item} = 1;
 5884:                     }
 5885:                 } elsif ($defaultchecked{$item} eq 'off') {
 5886:                     if (($domconfig{'login'}{$item} eq '1') &&
 5887:                         ($env{'form.'.$item} eq '0')) {
 5888:                         $changes{$item} = 1;
 5889:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 5890:                               $domconfig{'login'}{$item} eq '0') &&
 5891:                              ($env{'form.'.$item} eq '1')) {
 5892:                         $changes{$item} = 1;
 5893:                     }
 5894:                 }
 5895:             }
 5896:         }
 5897:         if (keys(%changes) > 0 || $colchgtext) {
 5898:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5899:             if (ref($lastactref) eq 'HASH') {
 5900:                 $lastactref->{'domainconfig'} = 1;
 5901:             }
 5902:             $resulttext = &mt('Changes made:').'<ul>';
 5903:             foreach my $item (sort(keys(%changes))) {
 5904:                 if ($item eq 'loginvia') {
 5905:                     if (ref($changes{$item}) eq 'HASH') {
 5906:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 5907:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 5908:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 5909:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 5910:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 5911:                                     $protocol = 'http' if ($protocol ne 'https');
 5912:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 5913: 
 5914:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 5915:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 5916:                                     } else {
 5917:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 5918:                                     }
 5919:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 5920:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 5921:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 5922:                                     }
 5923:                                     $resulttext .= '</li>';
 5924:                                 } else {
 5925:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 5926:                                 }
 5927:                             } else {
 5928:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 5929:                             }
 5930:                         }
 5931:                         $resulttext .= '</ul></li>';
 5932:                     }
 5933:                 } elsif ($item eq 'helpurl') {
 5934:                     if (ref($changes{$item}) eq 'HASH') {
 5935:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 5936:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 5937:                                 my ($chg,$link);
 5938:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 5939:                                 if ($lang eq 'nolang') {
 5940:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 5941:                                 } else {
 5942:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 5943:                                 }
 5944:                                 $resulttext .= '<li>'.$chg.'</li>';
 5945:                             } else {
 5946:                                 my $chg;
 5947:                                 if ($lang eq 'nolang') {
 5948:                                     $chg = &mt('custom log-in help file for no preferred language');
 5949:                                 } else {
 5950:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 5951:                                 }
 5952:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 5953:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 5954:                                                       '?inhibitmenu=yes',$chg,600,500).
 5955:                                                '</li>';
 5956:                             }
 5957:                         }
 5958:                     }
 5959:                 } elsif ($item eq 'captcha') {
 5960:                     if (ref($loginhash{'login'}) eq 'HASH') {
 5961:                         my $chgtxt;
 5962:                         if ($loginhash{'login'}{$item} eq 'notused') {
 5963:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 5964:                         } else {
 5965:                             my %captchas = &captcha_phrases();
 5966:                             if ($captchas{$loginhash{'login'}{$item}}) {
 5967:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 5968:                             } else {
 5969:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 5970:                             }
 5971:                         }
 5972:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 5973:                     }
 5974:                 } elsif ($item eq 'recaptchakeys') {
 5975:                     if (ref($loginhash{'login'}) eq 'HASH') {
 5976:                         my ($privkey,$pubkey);
 5977:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 5978:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 5979:                             $privkey = $loginhash{'login'}{$item}{'private'};
 5980:                         }
 5981:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 5982:                         if (!$pubkey) {
 5983:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 5984:                         } else {
 5985:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 5986:                         }
 5987:                         if (!$privkey) {
 5988:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 5989:                         } else {
 5990:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 5991:                         }
 5992:                         $chgtxt .= '</ul>';
 5993:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 5994:                     }
 5995:                 } else {
 5996:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 5997:                 }
 5998:             }
 5999:             $resulttext .= $colchgtext.'</ul>';
 6000:         } else {
 6001:             $resulttext = &mt('No changes made to log-in page settings');
 6002:         }
 6003:     } else {
 6004:         $resulttext = '<span class="LC_error">'.
 6005: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6006:     }
 6007:     if ($errors) {
 6008:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 6009:                        $errors.'</ul>';
 6010:     }
 6011:     return $resulttext;
 6012: }
 6013: 
 6014: sub color_font_choices {
 6015:     my %choices =
 6016:         &Apache::lonlocal::texthash (
 6017:             img => "Header",
 6018:             bgs => "Background colors",
 6019:             links => "Link colors",
 6020:             images => "Images",
 6021:             font => "Font color",
 6022:             fontmenu => "Font menu",
 6023:             pgbg => "Page",
 6024:             tabbg => "Header",
 6025:             sidebg => "Border",
 6026:             link => "Link",
 6027:             alink => "Active link",
 6028:             vlink => "Visited link",
 6029:         );
 6030:     return %choices;
 6031: }
 6032: 
 6033: sub modify_rolecolors {
 6034:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 6035:     my ($resulttext,%rolehash);
 6036:     $rolehash{'rolecolors'} = {};
 6037:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 6038:         if ($domconfig{'rolecolors'} eq '') {
 6039:             $domconfig{'rolecolors'} = {};
 6040:         }
 6041:     }
 6042:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 6043:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 6044:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 6045:                                              $dom);
 6046:     if ($putresult eq 'ok') {
 6047:         if (keys(%changes) > 0) {
 6048:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 6049:             if (ref($lastactref) eq 'HASH') {
 6050:                 $lastactref->{'domainconfig'} = 1;
 6051:             }
 6052:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 6053:                                              $rolehash{'rolecolors'});
 6054:         } else {
 6055:             $resulttext = &mt('No changes made to default color schemes');
 6056:         }
 6057:     } else {
 6058:         $resulttext = '<span class="LC_error">'.
 6059: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6060:     }
 6061:     if ($errors) {
 6062:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 6063:                        $errors.'</ul>';
 6064:     }
 6065:     return $resulttext;
 6066: }
 6067: 
 6068: sub modify_colors {
 6069:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 6070:     my (%changes,%choices);
 6071:     my @bgs;
 6072:     my @links = ('link','alink','vlink');
 6073:     my @logintext;
 6074:     my @images;
 6075:     my $servadm = $r->dir_config('lonAdmEMail');
 6076:     my $errors;
 6077:     my %defaults;
 6078:     foreach my $role (@{$roles}) {
 6079:         if ($role eq 'login') {
 6080:             %choices = &login_choices();
 6081:             @logintext = ('textcol','bgcol');
 6082:         } else {
 6083:             %choices = &color_font_choices();
 6084:         }
 6085:         if ($role eq 'login') {
 6086:             @images = ('img','logo','domlogo','login');
 6087:             @bgs = ('pgbg','mainbg','sidebg');
 6088:         } else {
 6089:             @images = ('img');
 6090:             @bgs = ('pgbg','tabbg','sidebg');
 6091:         }
 6092:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 6093:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 6094:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 6095:         }
 6096:         if ($role eq 'login') {
 6097:             foreach my $item (@logintext) {
 6098:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6099:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6100:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6101:                 }
 6102:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 6103:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6104:                 }
 6105:             }
 6106:         } else {
 6107:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 6108:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 6109:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 6110:             }
 6111:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 6112:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 6113:             }
 6114:         }
 6115:         foreach my $item (@bgs) {
 6116:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6117:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6118:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6119:             }
 6120:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 6121:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6122:             }
 6123:         }
 6124:         foreach my $item (@links) {
 6125:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6126:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6127:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6128:             }
 6129:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 6130:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6131:             }
 6132:         }
 6133:         my ($configuserok,$author_ok,$switchserver) = 
 6134:             &config_check($dom,$confname,$servadm);
 6135:         my ($width,$height) = &thumb_dimensions();
 6136:         if (ref($domconfig->{$role}) ne 'HASH') {
 6137:             $domconfig->{$role} = {};
 6138:         }
 6139:         foreach my $img (@images) {
 6140:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 6141:                 if (defined($env{'form.login_showlogo_'.$img})) {
 6142:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 6143:                 } else { 
 6144:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 6145:                 }
 6146:             } 
 6147: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 6148: 		 && !defined($domconfig->{$role}{$img})
 6149: 		 && !$env{'form.'.$role.'_del_'.$img}
 6150: 		 && $env{'form.'.$role.'_import_'.$img}) {
 6151: 		# import the old configured image from the .tab setting
 6152: 		# if they haven't provided a new one 
 6153: 		$domconfig->{$role}{$img} = 
 6154: 		    $env{'form.'.$role.'_import_'.$img};
 6155: 	    }
 6156:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 6157:                 my $error;
 6158:                 if ($configuserok eq 'ok') {
 6159:                     if ($switchserver) {
 6160:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 6161:                     } else {
 6162:                         if ($author_ok eq 'ok') {
 6163:                             my ($result,$logourl) = 
 6164:                                 &publishlogo($r,'upload',$role.'_'.$img,
 6165:                                            $dom,$confname,$img,$width,$height);
 6166:                             if ($result eq 'ok') {
 6167:                                 $confhash->{$role}{$img} = $logourl;
 6168:                                 $changes{$role}{'images'}{$img} = 1;
 6169:                             } else {
 6170:                                 $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);
 6171:                             }
 6172:                         } else {
 6173:                             $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);
 6174:                         }
 6175:                     }
 6176:                 } else {
 6177:                     $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);
 6178:                 }
 6179:                 if ($error) {
 6180:                     &Apache::lonnet::logthis($error);
 6181:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6182:                 }
 6183:             } elsif ($domconfig->{$role}{$img} ne '') {
 6184:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 6185:                     my $error;
 6186:                     if ($configuserok eq 'ok') {
 6187: # is confname an author?
 6188:                         if ($switchserver eq '') {
 6189:                             if ($author_ok eq 'ok') {
 6190:                                 my ($result,$logourl) = 
 6191:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 6192:                                             $dom,$confname,$img,$width,$height);
 6193:                                 if ($result eq 'ok') {
 6194:                                     $confhash->{$role}{$img} = $logourl;
 6195: 				    $changes{$role}{'images'}{$img} = 1;
 6196:                                 }
 6197:                             }
 6198:                         }
 6199:                     }
 6200:                 }
 6201:             }
 6202:         }
 6203:         if (ref($domconfig) eq 'HASH') {
 6204:             if (ref($domconfig->{$role}) eq 'HASH') {
 6205:                 foreach my $img (@images) {
 6206:                     if ($domconfig->{$role}{$img} ne '') {
 6207:                         if ($env{'form.'.$role.'_del_'.$img}) {
 6208:                             $confhash->{$role}{$img} = '';
 6209:                             $changes{$role}{'images'}{$img} = 1;
 6210:                         } else {
 6211:                             if ($confhash->{$role}{$img} eq '') {
 6212:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 6213:                             }
 6214:                         }
 6215:                     } else {
 6216:                         if ($env{'form.'.$role.'_del_'.$img}) {
 6217:                             $confhash->{$role}{$img} = '';
 6218:                             $changes{$role}{'images'}{$img} = 1;
 6219:                         } 
 6220:                     }
 6221:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 6222:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 6223:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 6224:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 6225:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 6226:                             }
 6227:                         } else {
 6228:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 6229:                                 $changes{$role}{'showlogo'}{$img} = 1;
 6230:                             }
 6231:                         }
 6232:                     }
 6233:                 }
 6234:                 if ($domconfig->{$role}{'font'} ne '') {
 6235:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 6236:                         $changes{$role}{'font'} = 1;
 6237:                     }
 6238:                 } else {
 6239:                     if ($confhash->{$role}{'font'}) {
 6240:                         $changes{$role}{'font'} = 1;
 6241:                     }
 6242:                 }
 6243:                 if ($role ne 'login') {
 6244:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 6245:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 6246:                             $changes{$role}{'fontmenu'} = 1;
 6247:                         }
 6248:                     } else {
 6249:                         if ($confhash->{$role}{'fontmenu'}) {
 6250:                             $changes{$role}{'fontmenu'} = 1;
 6251:                         }
 6252:                     }
 6253:                 }
 6254:                 foreach my $item (@bgs) {
 6255:                     if ($domconfig->{$role}{$item} ne '') {
 6256:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6257:                             $changes{$role}{'bgs'}{$item} = 1;
 6258:                         } 
 6259:                     } else {
 6260:                         if ($confhash->{$role}{$item}) {
 6261:                             $changes{$role}{'bgs'}{$item} = 1;
 6262:                         }
 6263:                     }
 6264:                 }
 6265:                 foreach my $item (@links) {
 6266:                     if ($domconfig->{$role}{$item} ne '') {
 6267:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6268:                             $changes{$role}{'links'}{$item} = 1;
 6269:                         }
 6270:                     } else {
 6271:                         if ($confhash->{$role}{$item}) {
 6272:                             $changes{$role}{'links'}{$item} = 1;
 6273:                         }
 6274:                     }
 6275:                 }
 6276:                 foreach my $item (@logintext) {
 6277:                     if ($domconfig->{$role}{$item} ne '') {
 6278:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6279:                             $changes{$role}{'logintext'}{$item} = 1;
 6280:                         }
 6281:                     } else {
 6282:                         if ($confhash->{$role}{$item}) {
 6283:                             $changes{$role}{'logintext'}{$item} = 1;
 6284:                         }
 6285:                     }
 6286:                 }
 6287:             } else {
 6288:                 &default_change_checker($role,\@images,\@links,\@bgs,
 6289:                                         \@logintext,$confhash,\%changes); 
 6290:             }
 6291:         } else {
 6292:             &default_change_checker($role,\@images,\@links,\@bgs,
 6293:                                     \@logintext,$confhash,\%changes); 
 6294:         }
 6295:     }
 6296:     return ($errors,%changes);
 6297: }
 6298: 
 6299: sub config_check {
 6300:     my ($dom,$confname,$servadm) = @_;
 6301:     my ($configuserok,$author_ok,$switchserver,%currroles);
 6302:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 6303:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 6304:                                                    $confname,$servadm);
 6305:     if ($configuserok eq 'ok') {
 6306:         $switchserver = &check_switchserver($dom,$confname);
 6307:         if ($switchserver eq '') {
 6308:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 6309:         }
 6310:     }
 6311:     return ($configuserok,$author_ok,$switchserver);
 6312: }
 6313: 
 6314: sub default_change_checker {
 6315:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 6316:     foreach my $item (@{$links}) {
 6317:         if ($confhash->{$role}{$item}) {
 6318:             $changes->{$role}{'links'}{$item} = 1;
 6319:         }
 6320:     }
 6321:     foreach my $item (@{$bgs}) {
 6322:         if ($confhash->{$role}{$item}) {
 6323:             $changes->{$role}{'bgs'}{$item} = 1;
 6324:         }
 6325:     }
 6326:     foreach my $item (@{$logintext}) {
 6327:         if ($confhash->{$role}{$item}) {
 6328:             $changes->{$role}{'logintext'}{$item} = 1;
 6329:         }
 6330:     }
 6331:     foreach my $img (@{$images}) {
 6332:         if ($env{'form.'.$role.'_del_'.$img}) {
 6333:             $confhash->{$role}{$img} = '';
 6334:             $changes->{$role}{'images'}{$img} = 1;
 6335:         }
 6336:         if ($role eq 'login') {
 6337:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 6338:                 $changes->{$role}{'showlogo'}{$img} = 1;
 6339:             }
 6340:         }
 6341:     }
 6342:     if ($confhash->{$role}{'font'}) {
 6343:         $changes->{$role}{'font'} = 1;
 6344:     }
 6345: }
 6346: 
 6347: sub display_colorchgs {
 6348:     my ($dom,$changes,$roles,$confhash) = @_;
 6349:     my (%choices,$resulttext);
 6350:     if (!grep(/^login$/,@{$roles})) {
 6351:         $resulttext = &mt('Changes made:').'<br />';
 6352:     }
 6353:     foreach my $role (@{$roles}) {
 6354:         if ($role eq 'login') {
 6355:             %choices = &login_choices();
 6356:         } else {
 6357:             %choices = &color_font_choices();
 6358:         }
 6359:         if (ref($changes->{$role}) eq 'HASH') {
 6360:             if ($role ne 'login') {
 6361:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 6362:             }
 6363:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 6364:                 if ($role ne 'login') {
 6365:                     $resulttext .= '<ul>';
 6366:                 }
 6367:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 6368:                     if ($role ne 'login') {
 6369:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 6370:                     }
 6371:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 6372:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 6373:                             if ($confhash->{$role}{$key}{$item}) {
 6374:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 6375:                             } else {
 6376:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 6377:                             }
 6378:                         } elsif ($confhash->{$role}{$item} eq '') {
 6379:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 6380:                         } else {
 6381:                             my $newitem = $confhash->{$role}{$item};
 6382:                             if ($key eq 'images') {
 6383:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 6384:                             }
 6385:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 6386:                         }
 6387:                     }
 6388:                     if ($role ne 'login') {
 6389:                         $resulttext .= '</ul></li>';
 6390:                     }
 6391:                 } else {
 6392:                     if ($confhash->{$role}{$key} eq '') {
 6393:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 6394:                     } else {
 6395:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 6396:                     }
 6397:                 }
 6398:                 if ($role ne 'login') {
 6399:                     $resulttext .= '</ul>';
 6400:                 }
 6401:             }
 6402:         }
 6403:     }
 6404:     return $resulttext;
 6405: }
 6406: 
 6407: sub thumb_dimensions {
 6408:     return ('200','50');
 6409: }
 6410: 
 6411: sub check_dimensions {
 6412:     my ($inputfile) = @_;
 6413:     my ($fullwidth,$fullheight);
 6414:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 6415:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 6416:             my $imageinfo = <PIPE>;
 6417:             if (!close(PIPE)) {
 6418:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 6419:             }
 6420:             chomp($imageinfo);
 6421:             my ($fullsize) = 
 6422:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 6423:             if ($fullsize) {
 6424:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 6425:             }
 6426:         }
 6427:     }
 6428:     return ($fullwidth,$fullheight);
 6429: }
 6430: 
 6431: sub check_configuser {
 6432:     my ($uhome,$dom,$confname,$servadm) = @_;
 6433:     my ($configuserok,%currroles);
 6434:     if ($uhome eq 'no_host') {
 6435:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 6436:         my $configpass = &LONCAPA::Enrollment::create_password();
 6437:         $configuserok = 
 6438:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 6439:                              $configpass,'','','','','',undef,$servadm);
 6440:     } else {
 6441:         $configuserok = 'ok';
 6442:         %currroles = 
 6443:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 6444:     }
 6445:     return ($configuserok,%currroles);
 6446: }
 6447: 
 6448: sub check_authorstatus {
 6449:     my ($dom,$confname,%currroles) = @_;
 6450:     my $author_ok;
 6451:     if (!$currroles{':'.$dom.':au'}) {
 6452:         my $start = time;
 6453:         my $end = 0;
 6454:         $author_ok = 
 6455:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 6456:                                         'au',$end,$start,'','','domconfig');
 6457:     } else {
 6458:         $author_ok = 'ok';
 6459:     }
 6460:     return $author_ok;
 6461: }
 6462: 
 6463: sub publishlogo {
 6464:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 6465:     my ($output,$fname,$logourl);
 6466:     if ($action eq 'upload') {
 6467:         $fname=$env{'form.'.$formname.'.filename'};
 6468:         chop($env{'form.'.$formname});
 6469:     } else {
 6470:         ($fname) = ($formname =~ /([^\/]+)$/);
 6471:     }
 6472:     if ($savefileas ne '') {
 6473:         $fname = $savefileas;
 6474:     }
 6475:     $fname=&Apache::lonnet::clean_filename($fname);
 6476: # See if there is anything left
 6477:     unless ($fname) { return ('error: no uploaded file'); }
 6478:     $fname="$subdir/$fname";
 6479:     my $docroot=$r->dir_config('lonDocRoot');
 6480:     my $filepath="$docroot/priv";
 6481:     my $relpath = "$dom/$confname";
 6482:     my ($fnamepath,$file,$fetchthumb);
 6483:     $file=$fname;
 6484:     if ($fname=~m|/|) {
 6485:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 6486:     }
 6487:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 6488:     my $count;
 6489:     for ($count=5;$count<=$#parts;$count++) {
 6490:         $filepath.="/$parts[$count]";
 6491:         if ((-e $filepath)!=1) {
 6492:             mkdir($filepath,02770);
 6493:         }
 6494:     }
 6495:     # Check for bad extension and disallow upload
 6496:     if ($file=~/\.(\w+)$/ &&
 6497:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 6498:         $output = 
 6499:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 6500:     } elsif ($file=~/\.(\w+)$/ &&
 6501:         !defined(&Apache::loncommon::fileembstyle($1))) {
 6502:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 6503:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 6504:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 6505:     } elsif (-d "$filepath/$file") {
 6506:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 6507:     } else {
 6508:         my $source = $filepath.'/'.$file;
 6509:         my $logfile;
 6510:         if (!open($logfile,">>$source".'.log')) {
 6511:             return (&mt('No write permission to Authoring Space'));
 6512:         }
 6513:         print $logfile
 6514: "\n================= Publish ".localtime()." ================\n".
 6515: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 6516: # Save the file
 6517:         if (!open(FH,'>'.$source)) {
 6518:             &Apache::lonnet::logthis('Failed to create '.$source);
 6519:             return (&mt('Failed to create file'));
 6520:         }
 6521:         if ($action eq 'upload') {
 6522:             if (!print FH ($env{'form.'.$formname})) {
 6523:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 6524:                 return (&mt('Failed to write file'));
 6525:             }
 6526:         } else {
 6527:             my $original = &Apache::lonnet::filelocation('',$formname);
 6528:             if(!copy($original,$source)) {
 6529:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 6530:                 return (&mt('Failed to write file'));
 6531:             }
 6532:         }
 6533:         close(FH);
 6534:         chmod(0660, $source); # Permissions to rw-rw---.
 6535: 
 6536:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 6537:         my $copyfile=$targetdir.'/'.$file;
 6538: 
 6539:         my @parts=split(/\//,$targetdir);
 6540:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 6541:         for (my $count=5;$count<=$#parts;$count++) {
 6542:             $path.="/$parts[$count]";
 6543:             if (!-e $path) {
 6544:                 print $logfile "\nCreating directory ".$path;
 6545:                 mkdir($path,02770);
 6546:             }
 6547:         }
 6548:         my $versionresult;
 6549:         if (-e $copyfile) {
 6550:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 6551:         } else {
 6552:             $versionresult = 'ok';
 6553:         }
 6554:         if ($versionresult eq 'ok') {
 6555:             if (copy($source,$copyfile)) {
 6556:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 6557:                 $output = 'ok';
 6558:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 6559:                 push(@{$modified_urls},[$copyfile,$source]);
 6560:                 my $metaoutput = 
 6561:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 6562:                 unless ($registered_cleanup) {
 6563:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 6564:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 6565:                     $registered_cleanup=1;
 6566:                 }
 6567:             } else {
 6568:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 6569:                 $output = &mt('Failed to copy file to RES space').", $!";
 6570:             }
 6571:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 6572:                 my $inputfile = $filepath.'/'.$file;
 6573:                 my $outfile = $filepath.'/'.'tn-'.$file;
 6574:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 6575:                 if ($fullwidth ne '' && $fullheight ne '') { 
 6576:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 6577:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 6578:                         system("convert -sample $thumbsize $inputfile $outfile");
 6579:                         chmod(0660, $filepath.'/tn-'.$file);
 6580:                         if (-e $outfile) {
 6581:                             my $copyfile=$targetdir.'/tn-'.$file;
 6582:                             if (copy($outfile,$copyfile)) {
 6583:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 6584:                                 my $thumb_metaoutput = 
 6585:                                     &write_metadata($dom,$confname,$formname,
 6586:                                                     $targetdir,'tn-'.$file,$logfile);
 6587:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 6588:                                 unless ($registered_cleanup) {
 6589:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 6590:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 6591:                                     $registered_cleanup=1;
 6592:                                 }
 6593:                             } else {
 6594:                                 print $logfile "\nUnable to write ".$copyfile.
 6595:                                                ':'.$!."\n";
 6596:                             }
 6597:                         }
 6598:                     }
 6599:                 }
 6600:             }
 6601:         } else {
 6602:             $output = $versionresult;
 6603:         }
 6604:     }
 6605:     return ($output,$logourl);
 6606: }
 6607: 
 6608: sub logo_versioning {
 6609:     my ($targetdir,$file,$logfile) = @_;
 6610:     my $target = $targetdir.'/'.$file;
 6611:     my ($maxversion,$fn,$extn,$output);
 6612:     $maxversion = 0;
 6613:     if ($file =~ /^(.+)\.(\w+)$/) {
 6614:         $fn=$1;
 6615:         $extn=$2;
 6616:     }
 6617:     opendir(DIR,$targetdir);
 6618:     while (my $filename=readdir(DIR)) {
 6619:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 6620:             $maxversion=($1>$maxversion)?$1:$maxversion;
 6621:         }
 6622:     }
 6623:     $maxversion++;
 6624:     print $logfile "\nCreating old version ".$maxversion."\n";
 6625:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 6626:     if (copy($target,$copyfile)) {
 6627:         print $logfile "Copied old target to ".$copyfile."\n";
 6628:         $copyfile=$copyfile.'.meta';
 6629:         if (copy($target.'.meta',$copyfile)) {
 6630:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 6631:             $output = 'ok';
 6632:         } else {
 6633:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 6634:             $output = &mt('Failed to copy old meta').", $!, ";
 6635:         }
 6636:     } else {
 6637:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 6638:         $output = &mt('Failed to copy old target').", $!, ";
 6639:     }
 6640:     return $output;
 6641: }
 6642: 
 6643: sub write_metadata {
 6644:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 6645:     my (%metadatafields,%metadatakeys,$output);
 6646:     $metadatafields{'title'}=$formname;
 6647:     $metadatafields{'creationdate'}=time;
 6648:     $metadatafields{'lastrevisiondate'}=time;
 6649:     $metadatafields{'copyright'}='public';
 6650:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 6651:                                          $env{'user.domain'};
 6652:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 6653:     $metadatafields{'domain'}=$dom;
 6654:     {
 6655:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 6656:         my $mfh;
 6657:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 6658:             foreach (sort(keys(%metadatafields))) {
 6659:                 unless ($_=~/\./) {
 6660:                     my $unikey=$_;
 6661:                     $unikey=~/^([A-Za-z]+)/;
 6662:                     my $tag=$1;
 6663:                     $tag=~tr/A-Z/a-z/;
 6664:                     print $mfh "\n\<$tag";
 6665:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 6666:                         my $value=$metadatafields{$unikey.'.'.$_};
 6667:                         $value=~s/\"/\'\'/g;
 6668:                         print $mfh ' '.$_.'="'.$value.'"';
 6669:                     }
 6670:                     print $mfh '>'.
 6671:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 6672:                             .'</'.$tag.'>';
 6673:                 }
 6674:             }
 6675:             $output = 'ok';
 6676:             print $logfile "\nWrote metadata";
 6677:             close($mfh);
 6678:         } else {
 6679:             print $logfile "\nFailed to open metadata file";
 6680:             $output = &mt('Could not write metadata');
 6681:         }
 6682:     }
 6683:     return $output;
 6684: }
 6685: 
 6686: sub notifysubscribed {
 6687:     foreach my $targetsource (@{$modified_urls}){
 6688:         next unless (ref($targetsource) eq 'ARRAY');
 6689:         my ($target,$source)=@{$targetsource};
 6690:         if ($source ne '') {
 6691:             if (open(my $logfh,'>>'.$source.'.log')) {
 6692:                 print $logfh "\nCleanup phase: Notifications\n";
 6693:                 my @subscribed=&subscribed_hosts($target);
 6694:                 foreach my $subhost (@subscribed) {
 6695:                     print $logfh "\nNotifying host ".$subhost.':';
 6696:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 6697:                     print $logfh $reply;
 6698:                 }
 6699:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 6700:                 foreach my $subhost (@subscribedmeta) {
 6701:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 6702:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 6703:                                                         $subhost);
 6704:                     print $logfh $reply;
 6705:                 }
 6706:                 print $logfh "\n============ Done ============\n";
 6707:                 close($logfh);
 6708:             }
 6709:         }
 6710:     }
 6711:     return OK;
 6712: }
 6713: 
 6714: sub subscribed_hosts {
 6715:     my ($target) = @_;
 6716:     my @subscribed;
 6717:     if (open(my $fh,"<$target.subscription")) {
 6718:         while (my $subline=<$fh>) {
 6719:             if ($subline =~ /^($match_lonid):/) {
 6720:                 my $host = $1;
 6721:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 6722:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 6723:                         push(@subscribed,$host);
 6724:                     }
 6725:                 }
 6726:             }
 6727:         }
 6728:     }
 6729:     return @subscribed;
 6730: }
 6731: 
 6732: sub check_switchserver {
 6733:     my ($dom,$confname) = @_;
 6734:     my ($allowed,$switchserver);
 6735:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 6736:     if ($home eq 'no_host') {
 6737:         $home = &Apache::lonnet::domain($dom,'primary');
 6738:     }
 6739:     my @ids=&Apache::lonnet::current_machine_ids();
 6740:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 6741:     if (!$allowed) {
 6742: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 6743:     }
 6744:     return $switchserver;
 6745: }
 6746: 
 6747: sub modify_quotas {
 6748:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 6749:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 6750:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 6751:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 6752:         $validationfieldsref);
 6753:     if ($action eq 'quotas') {
 6754:         $context = 'tools'; 
 6755:     } else {
 6756:         $context = $action;
 6757:     }
 6758:     if ($context eq 'requestcourses') {
 6759:         @usertools = ('official','unofficial','community','textbook');
 6760:         @options =('norequest','approval','validate','autolimit');
 6761:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 6762:         %titles = &courserequest_titles();
 6763:         $toolregexp = join('|',@usertools);
 6764:         %conditions = &courserequest_conditions();
 6765:         $confname = $dom.'-domainconfig';
 6766:         my $servadm = $r->dir_config('lonAdmEMail');
 6767:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 6768:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 6769:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 6770:     } elsif ($context eq 'requestauthor') {
 6771:         @usertools = ('author');
 6772:         %titles = &authorrequest_titles();
 6773:     } else {
 6774:         @usertools = ('aboutme','blog','webdav','portfolio');
 6775:         %titles = &tool_titles();
 6776:     }
 6777:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6778:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6779:     foreach my $key (keys(%env)) {
 6780:         if ($context eq 'requestcourses') {
 6781:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 6782:                 my $item = $1;
 6783:                 my $type = $2;
 6784:                 if ($type =~ /^limit_(.+)/) {
 6785:                     $limithash{$item}{$1} = $env{$key};
 6786:                 } else {
 6787:                     $confhash{$item}{$type} = $env{$key};
 6788:                 }
 6789:             }
 6790:         } elsif ($context eq 'requestauthor') {
 6791:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 6792:                 $confhash{$1} = $env{$key};
 6793:             }
 6794:         } else {
 6795:             if ($key =~ /^form\.quota_(.+)$/) {
 6796:                 $confhash{'defaultquota'}{$1} = $env{$key};
 6797:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 6798:                 $confhash{'authorquota'}{$1} = $env{$key};
 6799:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 6800:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 6801:             }
 6802:         }
 6803:     }
 6804:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 6805:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 6806:         @approvalnotify = sort(@approvalnotify);
 6807:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 6808:         my @crstypes = ('official','unofficial','community','textbook');
 6809:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 6810:         foreach my $type (@hasuniquecode) {
 6811:             if (grep(/^\Q$type\E$/,@crstypes)) {
 6812:                 $confhash{'uniquecode'}{$type} = 1;
 6813:             }
 6814:         }
 6815:         my ($newbook,@allpos);
 6816:         if ($context eq 'requestcourses') {
 6817:             if ($env{'form.addbook'}) {
 6818:                 if (($env{'form.addbook_cnum'} =~ /^$match_courseid$/) &&
 6819:                     ($env{'form.addbook_cdom'} =~ /^$match_domain$/)) {
 6820:                     if (&Apache::lonnet::homeserver($env{'form.addbook_cnum'},
 6821:                                                     $env{'form.addbook_cdom'}) eq 'no_host') {
 6822:                         $errors .= '<li><span class="LC_error">'.&mt('Invalid LON-CAPA course for textbook').
 6823:                                    '</span></li>';
 6824:                     } else {
 6825:                         $newbook = $env{'form.addbook_cdom'}.'_'.$env{'form.addbook_cnum'};
 6826:                         my $position = $env{'form.addbook_pos'};
 6827:                         $position =~ s/\D+//g;
 6828:                         if ($position ne '') {
 6829:                             $allpos[$position] = $newbook;
 6830:                         }
 6831:                     }
 6832:                 } else {
 6833:                     $errors .= '<li><span class="LC_error">'.&mt('Invalid LON-CAPA course for textbook').
 6834:                                '</span></li>';
 6835:                 }
 6836:             }
 6837: 
 6838:         }
 6839:         if (ref($domconfig{$action}) eq 'HASH') {
 6840:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 6841:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 6842:                     $changes{'notify'}{'approval'} = 1;
 6843:                 }
 6844:             } else {
 6845:                 if ($confhash{'notify'}{'approval'}) {
 6846:                     $changes{'notify'}{'approval'} = 1;
 6847:                 }
 6848:             }
 6849:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 6850:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 6851:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 6852:                         unless ($confhash{'uniquecode'}{$crstype}) {
 6853:                             $changes{'uniquecode'} = 1;
 6854:                         }
 6855:                     }
 6856:                     unless ($changes{'uniquecode'}) {
 6857:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 6858:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 6859:                                 $changes{'uniquecode'} = 1;
 6860:                             }
 6861:                         }
 6862:                     }
 6863:                } else {
 6864:                    $changes{'uniquecode'} = 1;
 6865:                }
 6866:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 6867:                 $changes{'uniquecode'} = 1;
 6868:             }
 6869:             if ($context eq 'requestcourses') {
 6870:                 if (ref($domconfig{$action}{'textbooks'}) eq 'HASH') {
 6871:                     my %deletions;
 6872:                     my @todelete = &Apache::loncommon::get_env_multiple('form.book_del');
 6873:                     if (@todelete) {
 6874:                         map { $deletions{$_} = 1; } @todelete;
 6875:                     }
 6876:                     my %imgdeletions;
 6877:                     my @todeleteimages = &Apache::loncommon::get_env_multiple('form.book_image_del');
 6878:                     if (@todeleteimages) {
 6879:                         map { $imgdeletions{$_} = 1; } @todeleteimages;
 6880:                     }
 6881:                     my $maxnum = $env{'form.book_maxnum'};
 6882:                     for (my $i=0; $i<=$maxnum; $i++) {
 6883:                         my $key = $env{'form.book_id_'.$i};
 6884:                         if (ref($domconfig{$action}{'textbooks'}{$key}) eq 'HASH') {
 6885:                             if ($deletions{$key}) {
 6886:                                 if ($domconfig{$action}{'textbooks'}{$key}{'image'}) {
 6887:                                     #FIXME need to obsolete item in RES space
 6888:                                 }
 6889:                                 next;
 6890:                             } else {
 6891:                                 my $newpos = $env{'form.'.$key};
 6892:                                 $newpos =~ s/\D+//g;
 6893:                                 foreach my $item ('subject','title','author') {
 6894:                                     $confhash{'textbooks'}{$key}{$item} = $env{'form.book_'.$item.'_'.$i};
 6895:                                     if ($domconfig{$action}{'textbooks'}{$key}{$item} ne $confhash{'textbooks'}{$key}{$item}) {
 6896:                                         $changes{'textbooks'}{$key} = 1;
 6897:                                     }
 6898:                                 }
 6899:                                 $allpos[$newpos] = $key;
 6900:                             }
 6901:                             if ($imgdeletions{$key}) {
 6902:                                 $changes{'textbooks'}{$key} = 1;
 6903:                                 #FIXME need to obsolete item in RES space
 6904:                             } elsif ($env{'form.book_image_'.$i.'.filename'}) {
 6905:                                 my ($cdom,$cnum) = split(/_/,$key);
 6906:                                 my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,'book_image_'.$i,
 6907:                                                                               $cdom,$cnum,$configuserok,
 6908:                                                                               $switchserver,$author_ok);
 6909:                                 if ($imgurl) {
 6910:                                     $confhash{'textbooks'}{$key}{'image'} = $imgurl;
 6911:                                     $changes{'textbooks'}{$key} = 1; 
 6912:                                 }
 6913:                                 if ($error) {
 6914:                                     &Apache::lonnet::logthis($error);
 6915:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6916:                                 } 
 6917:                             } elsif ($domconfig{$action}{'textbooks'}{$key}{'image'}) {
 6918:                                 $confhash{'textbooks'}{$key}{'image'} = 
 6919:                                     $domconfig{$action}{'textbooks'}{$key}{'image'};
 6920:                             }
 6921:                         }
 6922:                     }
 6923:                 }
 6924:             }
 6925:         } else {
 6926:             if ($confhash{'notify'}{'approval'}) {
 6927:                 $changes{'notify'}{'approval'} = 1;
 6928:             }
 6929:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 6930:                 $changes{'uniquecode'} = 1;
 6931:             }
 6932:         }
 6933:         if ($context eq 'requestcourses') {
 6934:             if ($newbook) {
 6935:                 $changes{'textbooks'}{$newbook} = 1;
 6936:                 foreach my $item ('subject','title','author') {
 6937:                     $env{'form.addbook_'.$item} =~ s/(`)/'/g;
 6938:                     if ($env{'form.addbook_'.$item}) {
 6939:                         $confhash{'textbooks'}{$newbook}{$item} = $env{'form.addbook_'.$item};
 6940:                     }
 6941:                 }
 6942:                 if ($env{'form.addbook_image.filename'} ne '') {
 6943:                     my ($cdom,$cnum) = split(/_/,$newbook);
 6944:                     my ($imageurl,$error) =
 6945:                         &process_textbook_image($r,$dom,$confname,'addbook_image',$cdom,$cnum,$configuserok,
 6946:                                                 $switchserver,$author_ok);
 6947:                     if ($imageurl) {
 6948:                         $confhash{'textbooks'}{$newbook}{'image'} = $imageurl;
 6949:                     }
 6950:                     if ($error) {
 6951:                         &Apache::lonnet::logthis($error);
 6952:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6953:                     }
 6954:                 }
 6955:             }
 6956:             if (@allpos > 0) {
 6957:                 my $idx = 0;
 6958:                 foreach my $item (@allpos) {
 6959:                     if ($item ne '') {
 6960:                         $confhash{'textbooks'}{$item}{'order'} = $idx;
 6961:                         if (ref($domconfig{$action}) eq 'HASH') {
 6962:                             if (ref($domconfig{$action}{'textbooks'}) eq 'HASH') {
 6963:                                 if (ref($domconfig{$action}{'textbooks'}{$item}) eq 'HASH') {
 6964:                                     if ($domconfig{$action}{'textbooks'}{$item}{'order'} ne $idx) {
 6965:                                         $changes{'textbooks'}{$item} = 1;
 6966:                                     }
 6967:                                 }
 6968:                             }
 6969:                         }
 6970:                         $idx ++;
 6971:                     }
 6972:                 }
 6973:             }
 6974:             if (ref($validationitemsref) eq 'ARRAY') {
 6975:                 foreach my $item (@{$validationitemsref}) {
 6976:                     if ($item eq 'fields') {
 6977:                         my @changed;
 6978:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 6979:                         if (@{$confhash{'validation'}{$item}} > 0) {
 6980:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 6981:                         }
 6982:                         if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 6983:                             if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 6984:                                 @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 6985:                                                                               $domconfig{'requestcourses'}{'validation'}{$item});
 6986:                             } else {
 6987:                                 @changed = @{$confhash{'validation'}{$item}};
 6988:                             }
 6989:                         } else {
 6990:                             @changed = @{$confhash{'validation'}{$item}};
 6991:                         }
 6992:                         if (@changed) {
 6993:                             if ($confhash{'validation'}{$item}) {
 6994:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 6995:                             } else {
 6996:                                 $changes{'validation'}{$item} = &mt('None');
 6997:                             }
 6998:                         }
 6999:                     } else {
 7000:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 7001:                         if ($item eq 'markup') {
 7002:                             if ($env{'form.requestcourses_validation_'.$item}) {
 7003:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 7004:                             }
 7005:                         }
 7006:                         if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7007:                             if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 7008:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 7009:                             }
 7010:                         } else {
 7011:                             if ($confhash{'validation'}{$item} ne '') {
 7012:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 7013:                             }
 7014:                         }
 7015:                     }
 7016:                 }
 7017:             }
 7018:             if ($env{'form.validationdc'}) {
 7019:                 my $newval = $env{'form.validationdc'};
 7020:                 my %domcoords = &get_active_dcs($dom);
 7021:                 if (exists($domcoords{$newval})) {
 7022:                     $confhash{'validation'}{'dc'} = $newval;
 7023:                 }
 7024:             }
 7025:             if (ref($confhash{'validation'}) eq 'HASH') {
 7026:                 if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7027:                     if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7028:                         unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7029:                             if ($confhash{'validation'}{'dc'} eq '') {
 7030:                                 $changes{'validation'}{'dc'} = &mt('None');
 7031:                             } else {
 7032:                                 $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7033:                             }
 7034:                         }
 7035:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 7036:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7037:                     }
 7038:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 7039:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7040:                 }
 7041:             } elsif (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7042:                 if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7043:                     $changes{'validation'}{'dc'} = &mt('None');
 7044:                 }
 7045:             }
 7046:         }
 7047:     } else {
 7048:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 7049:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 7050:     }
 7051:     foreach my $item (@usertools) {
 7052:         foreach my $type (@{$types},'default','_LC_adv') {
 7053:             my $unset; 
 7054:             if ($context eq 'requestcourses') {
 7055:                 $unset = '0';
 7056:                 if ($type eq '_LC_adv') {
 7057:                     $unset = '';
 7058:                 }
 7059:                 if ($confhash{$item}{$type} eq 'autolimit') {
 7060:                     $confhash{$item}{$type} .= '=';
 7061:                     unless ($limithash{$item}{$type} =~ /\D/) {
 7062:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 7063:                     }
 7064:                 }
 7065:             } elsif ($context eq 'requestauthor') {
 7066:                 $unset = '0';
 7067:                 if ($type eq '_LC_adv') {
 7068:                     $unset = '';
 7069:                 }
 7070:             } else {
 7071:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 7072:                     $confhash{$item}{$type} = 1;
 7073:                 } else {
 7074:                     $confhash{$item}{$type} = 0;
 7075:                 }
 7076:             }
 7077:             if (ref($domconfig{$action}) eq 'HASH') {
 7078:                 if ($action eq 'requestauthor') {
 7079:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 7080:                         $changes{$type} = 1;
 7081:                     }
 7082:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 7083:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 7084:                         $changes{$item}{$type} = 1;
 7085:                     }
 7086:                 } else {
 7087:                     if ($context eq 'requestcourses') {
 7088:                         if ($confhash{$item}{$type} ne $unset) {
 7089:                             $changes{$item}{$type} = 1;
 7090:                         }
 7091:                     } else {
 7092:                         if (!$confhash{$item}{$type}) {
 7093:                             $changes{$item}{$type} = 1;
 7094:                         }
 7095:                     }
 7096:                 }
 7097:             } else {
 7098:                 if ($context eq 'requestcourses') {
 7099:                     if ($confhash{$item}{$type} ne $unset) {
 7100:                         $changes{$item}{$type} = 1;
 7101:                     }
 7102:                 } elsif ($context eq 'requestauthor') {
 7103:                     if ($confhash{$type} ne $unset) {
 7104:                         $changes{$type} = 1;
 7105:                     }
 7106:                 } else {
 7107:                     if (!$confhash{$item}{$type}) {
 7108:                         $changes{$item}{$type} = 1;
 7109:                     }
 7110:                 }
 7111:             }
 7112:         }
 7113:     }
 7114:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 7115:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 7116:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 7117:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 7118:                     if (exists($confhash{'defaultquota'}{$key})) {
 7119:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 7120:                             $changes{'defaultquota'}{$key} = 1;
 7121:                         }
 7122:                     } else {
 7123:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 7124:                     }
 7125:                 }
 7126:             } else {
 7127:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 7128:                     if (exists($confhash{'defaultquota'}{$key})) {
 7129:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 7130:                             $changes{'defaultquota'}{$key} = 1;
 7131:                         }
 7132:                     } else {
 7133:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 7134:                     }
 7135:                 }
 7136:             }
 7137:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 7138:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 7139:                     if (exists($confhash{'authorquota'}{$key})) {
 7140:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 7141:                             $changes{'authorquota'}{$key} = 1;
 7142:                         }
 7143:                     } else {
 7144:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 7145:                     }
 7146:                 }
 7147:             }
 7148:         }
 7149:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 7150:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 7151:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 7152:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 7153:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 7154:                             $changes{'defaultquota'}{$key} = 1;
 7155:                         }
 7156:                     } else {
 7157:                         if (!exists($domconfig{'quotas'}{$key})) {
 7158:                             $changes{'defaultquota'}{$key} = 1;
 7159:                         }
 7160:                     }
 7161:                 } else {
 7162:                     $changes{'defaultquota'}{$key} = 1;
 7163:                 }
 7164:             }
 7165:         }
 7166:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 7167:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 7168:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 7169:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 7170:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 7171:                             $changes{'authorquota'}{$key} = 1;
 7172:                         }
 7173:                     } else {
 7174:                         $changes{'authorquota'}{$key} = 1;
 7175:                     }
 7176:                 } else {
 7177:                     $changes{'authorquota'}{$key} = 1;
 7178:                 }
 7179:             }
 7180:         }
 7181:     }
 7182: 
 7183:     if ($context eq 'requestauthor') {
 7184:         $domdefaults{'requestauthor'} = \%confhash;
 7185:     } else {
 7186:         foreach my $key (keys(%confhash)) {
 7187:             unless (($context eq 'requestcourses') && ($key eq 'textbooks')) {
 7188:                 $domdefaults{$key} = $confhash{$key};
 7189:             }
 7190:         }
 7191:     }
 7192: 
 7193:     my %quotahash = (
 7194:                       $action => { %confhash }
 7195:                     );
 7196:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 7197:                                              $dom);
 7198:     if ($putresult eq 'ok') {
 7199:         if (keys(%changes) > 0) {
 7200:             my $cachetime = 24*60*60;
 7201:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7202:             if (ref($lastactref) eq 'HASH') {
 7203:                 $lastactref->{'domdefaults'} = 1;
 7204:             }
 7205:             $resulttext = &mt('Changes made:').'<ul>';
 7206:             unless (($context eq 'requestcourses') ||
 7207:                     ($context eq 'requestauthor')) {
 7208:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 7209:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 7210:                     foreach my $type (@{$types},'default') {
 7211:                         if (defined($changes{'defaultquota'}{$type})) {
 7212:                             my $typetitle = $usertypes->{$type};
 7213:                             if ($type eq 'default') {
 7214:                                 $typetitle = $othertitle;
 7215:                             }
 7216:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 7217:                         }
 7218:                     }
 7219:                     $resulttext .= '</ul></li>';
 7220:                 }
 7221:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 7222:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 7223:                     foreach my $type (@{$types},'default') {
 7224:                         if (defined($changes{'authorquota'}{$type})) {
 7225:                             my $typetitle = $usertypes->{$type};
 7226:                             if ($type eq 'default') {
 7227:                                 $typetitle = $othertitle;
 7228:                             }
 7229:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 7230:                         }
 7231:                     }
 7232:                     $resulttext .= '</ul></li>';
 7233:                 }
 7234:             }
 7235:             my %newenv;
 7236:             foreach my $item (@usertools) {
 7237:                 my (%haschgs,%inconf);
 7238:                 if ($context eq 'requestauthor') {
 7239:                     %haschgs = %changes;
 7240:                     %inconf = %confhash;
 7241:                 } else {
 7242:                     if (ref($changes{$item}) eq 'HASH') {
 7243:                         %haschgs = %{$changes{$item}};
 7244:                     }
 7245:                     if (ref($confhash{$item}) eq 'HASH') {
 7246:                         %inconf = %{$confhash{$item}};
 7247:                     }
 7248:                 }
 7249:                 if (keys(%haschgs) > 0) {
 7250:                     my $newacc = 
 7251:                         &Apache::lonnet::usertools_access($env{'user.name'},
 7252:                                                           $env{'user.domain'},
 7253:                                                           $item,'reload',$context);
 7254:                     if (($context eq 'requestcourses') ||
 7255:                         ($context eq 'requestauthor')) {
 7256:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 7257:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 7258:                         }
 7259:                     } else {
 7260:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 7261:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 7262:                         }
 7263:                     }
 7264:                     unless ($context eq 'requestauthor') {
 7265:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 7266:                     }
 7267:                     foreach my $type (@{$types},'default','_LC_adv') {
 7268:                         if ($haschgs{$type}) {
 7269:                             my $typetitle = $usertypes->{$type};
 7270:                             if ($type eq 'default') {
 7271:                                 $typetitle = $othertitle;
 7272:                             } elsif ($type eq '_LC_adv') {
 7273:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 7274:                             }
 7275:                             if ($inconf{$type}) {
 7276:                                 if ($context eq 'requestcourses') {
 7277:                                     my $cond;
 7278:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 7279:                                         if ($1 eq '') {
 7280:                                             $cond = &mt('(Automatic processing of any request).');
 7281:                                         } else {
 7282:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 7283:                                         }
 7284:                                     } else { 
 7285:                                         $cond = $conditions{$inconf{$type}};
 7286:                                     }
 7287:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 7288:                                 } elsif ($context eq 'requestauthor') {
 7289:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 7290:                                                              $titles{$inconf{$type}},$typetitle);
 7291: 
 7292:                                 } else {
 7293:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 7294:                                 }
 7295:                             } else {
 7296:                                 if ($type eq '_LC_adv') {
 7297:                                     if ($inconf{$type} eq '0') {
 7298:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 7299:                                     } else { 
 7300:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 7301:                                     }
 7302:                                 } else {
 7303:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 7304:                                 }
 7305:                             }
 7306:                         }
 7307:                     }
 7308:                     unless ($context eq 'requestauthor') {
 7309:                         $resulttext .= '</ul></li>';
 7310:                     }
 7311:                 }
 7312:             }
 7313:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 7314:                 if (ref($changes{'notify'}) eq 'HASH') {
 7315:                     if ($changes{'notify'}{'approval'}) {
 7316:                         if (ref($confhash{'notify'}) eq 'HASH') {
 7317:                             if ($confhash{'notify'}{'approval'}) {
 7318:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 7319:                             } else {
 7320:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 7321:                             }
 7322:                         }
 7323:                     }
 7324:                 }
 7325:             }
 7326:             if ($action eq 'requestcourses') {
 7327:                 my @offon = ('off','on');
 7328:                 if ($changes{'uniquecode'}) {
 7329:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 7330:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 7331:                         $resulttext .= '<li>'.
 7332:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 7333:                                        '</li>';
 7334:                     } else {
 7335:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 7336:                                        '</li>';
 7337:                     }
 7338:                 }
 7339:                 if (ref($changes{'textbooks'}) eq 'HASH') {
 7340:                     $resulttext .= '<li>'.&mt('Available textbooks updated').'<ul>';
 7341:                     foreach my $key (sort(keys(%{$changes{'textbooks'}}))) {
 7342:                         my %coursehash = &Apache::lonnet::coursedescription($key);
 7343:                         my $coursetitle = $coursehash{'description'};
 7344:                         my $position = $confhash{'textbooks'}{$key}{'order'} + 1;
 7345:                         $resulttext .= '<li>';
 7346:                         foreach my $item ('subject','title','author') {
 7347:                             my $name = $item.':';
 7348:                             $name =~ s/^(\w)/\U$1/;
 7349:                             $resulttext .= &mt($name).' '.$confhash{'textbooks'}{$key}{$item}.'<br />';
 7350:                         }
 7351:                         $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 7352:                         if ($confhash{'textbooks'}{$key}{'image'}) {
 7353:                             $resulttext .= ' '.&mt('Image: [_1]',
 7354:                                                '<img src="'.$confhash{'textbooks'}{$key}{'image'}.'"'.
 7355:                                                ' alt="Textbook cover" />').'<br />';
 7356:                         }
 7357:                         $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 7358:                     }
 7359:                     $resulttext .= '</ul></li>';
 7360:                 }
 7361:                 if (ref($changes{'validation'}) eq 'HASH') {
 7362:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 7363:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 7364:                         foreach my $item (@{$validationitemsref}) {
 7365:                             if (exists($changes{'validation'}{$item})) {
 7366:                                 if ($item eq 'markup') {
 7367:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 7368:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 7369:                                 } else {
 7370:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 7371:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 7372:                                 }
 7373:                             }
 7374:                         }
 7375:                         if (exists($changes{'validation'}{'dc'})) {
 7376:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 7377:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 7378:                         }
 7379:                     }
 7380:                 }
 7381:             }
 7382:             $resulttext .= '</ul>';
 7383:             if (keys(%newenv)) {
 7384:                 &Apache::lonnet::appenv(\%newenv);
 7385:             }
 7386:         } else {
 7387:             if ($context eq 'requestcourses') {
 7388:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 7389:             } elsif ($context eq 'requestauthor') {
 7390:                 $resulttext = &mt('No changes made to rights to request author space.');
 7391:             } else {
 7392:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 7393:             }
 7394:         }
 7395:     } else {
 7396:         $resulttext = '<span class="LC_error">'.
 7397: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7398:     }
 7399:     if ($errors) {
 7400:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 7401:                        '<ul>'.$errors.'</ul></p>';
 7402:     }
 7403:     return $resulttext;
 7404: }
 7405: 
 7406: sub process_textbook_image {
 7407:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$configuserok,$switchserver,$author_ok) = @_;
 7408:     my $filename = $env{'form.'.$caller.'.filename'};
 7409:     my ($error,$url);
 7410:     my ($width,$height) = (50,50);
 7411:     if ($configuserok eq 'ok') {
 7412:         if ($switchserver) {
 7413:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 7414:                          $switchserver);
 7415:         } elsif ($author_ok eq 'ok') {
 7416:             my ($result,$imageurl) =
 7417:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 7418:                              "textbooks/$dom/$cnum/cover",$width,$height);
 7419:             if ($result eq 'ok') {
 7420:                 $url = $imageurl;
 7421:             } else {
 7422:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 7423:             }
 7424:         } else {
 7425:             $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);
 7426:         }
 7427:     } else {
 7428:         $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);
 7429:     }
 7430:     return ($url,$error);
 7431: }
 7432: 
 7433: sub modify_autoenroll {
 7434:     my ($dom,$lastactref,%domconfig) = @_;
 7435:     my ($resulttext,%changes);
 7436:     my %currautoenroll;
 7437:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 7438:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 7439:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 7440:         }
 7441:     }
 7442:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 7443:     my %title = ( run => 'Auto-enrollment active',
 7444:                   sender => 'Sender for notification messages',
 7445:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 7446:     my @offon = ('off','on');
 7447:     my $sender_uname = $env{'form.sender_uname'};
 7448:     my $sender_domain = $env{'form.sender_domain'};
 7449:     if ($sender_domain eq '') {
 7450:         $sender_uname = '';
 7451:     } elsif ($sender_uname eq '') {
 7452:         $sender_domain = '';
 7453:     }
 7454:     my $coowners = $env{'form.autoassign_coowners'};
 7455:     my %autoenrollhash =  (
 7456:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 7457:                                        'sender_uname' => $sender_uname,
 7458:                                        'sender_domain' => $sender_domain,
 7459:                                        'co-owners' => $coowners,
 7460:                                 }
 7461:                      );
 7462:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 7463:                                              $dom);
 7464:     if ($putresult eq 'ok') {
 7465:         if (exists($currautoenroll{'run'})) {
 7466:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 7467:                  $changes{'run'} = 1;
 7468:              }
 7469:         } elsif ($autorun) {
 7470:             if ($env{'form.autoenroll_run'} ne '1') {
 7471:                  $changes{'run'} = 1;
 7472:             }
 7473:         }
 7474:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 7475:             $changes{'sender'} = 1;
 7476:         }
 7477:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 7478:             $changes{'sender'} = 1;
 7479:         }
 7480:         if ($currautoenroll{'co-owners'} ne '') {
 7481:             if ($currautoenroll{'co-owners'} ne $coowners) {
 7482:                 $changes{'coowners'} = 1;
 7483:             }
 7484:         } elsif ($coowners) {
 7485:             $changes{'coowners'} = 1;
 7486:         }      
 7487:         if (keys(%changes) > 0) {
 7488:             $resulttext = &mt('Changes made:').'<ul>';
 7489:             if ($changes{'run'}) {
 7490:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 7491:             }
 7492:             if ($changes{'sender'}) {
 7493:                 if ($sender_uname eq '' || $sender_domain eq '') {
 7494:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 7495:                 } else {
 7496:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 7497:                 }
 7498:             }
 7499:             if ($changes{'coowners'}) {
 7500:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 7501:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 7502:                 if (ref($lastactref) eq 'HASH') {
 7503:                     $lastactref->{'domainconfig'} = 1;
 7504:                 }
 7505:             }
 7506:             $resulttext .= '</ul>';
 7507:         } else {
 7508:             $resulttext = &mt('No changes made to auto-enrollment settings');
 7509:         }
 7510:     } else {
 7511:         $resulttext = '<span class="LC_error">'.
 7512: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7513:     }
 7514:     return $resulttext;
 7515: }
 7516: 
 7517: sub modify_autoupdate {
 7518:     my ($dom,%domconfig) = @_;
 7519:     my ($resulttext,%currautoupdate,%fields,%changes);
 7520:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 7521:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 7522:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 7523:         }
 7524:     }
 7525:     my @offon = ('off','on');
 7526:     my %title = &Apache::lonlocal::texthash (
 7527:                    run => 'Auto-update:',
 7528:                    classlists => 'Updates to user information in classlists?'
 7529:                 );
 7530:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7531:     my %fieldtitles = &Apache::lonlocal::texthash (
 7532:                         id => 'Student/Employee ID',
 7533:                         permanentemail => 'E-mail address',
 7534:                         lastname => 'Last Name',
 7535:                         firstname => 'First Name',
 7536:                         middlename => 'Middle Name',
 7537:                         generation => 'Generation',
 7538:                       );
 7539:     $othertitle = &mt('All users');
 7540:     if (keys(%{$usertypes}) >  0) {
 7541:         $othertitle = &mt('Other users');
 7542:     }
 7543:     foreach my $key (keys(%env)) {
 7544:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 7545:             my ($usertype,$item) = ($1,$2);
 7546:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 7547:                 if ($usertype eq 'default') {   
 7548:                     push(@{$fields{$1}},$2);
 7549:                 } elsif (ref($types) eq 'ARRAY') {
 7550:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 7551:                         push(@{$fields{$1}},$2);
 7552:                     }
 7553:                 }
 7554:             }
 7555:         }
 7556:     }
 7557:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 7558:     @lockablenames = sort(@lockablenames);
 7559:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 7560:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 7561:         if (@changed) {
 7562:             $changes{'lockablenames'} = 1;
 7563:         }
 7564:     } else {
 7565:         if (@lockablenames) {
 7566:             $changes{'lockablenames'} = 1;
 7567:         }
 7568:     }
 7569:     my %updatehash = (
 7570:                       autoupdate => { run => $env{'form.autoupdate_run'},
 7571:                                       classlists => $env{'form.classlists'},
 7572:                                       fields => {%fields},
 7573:                                       lockablenames => \@lockablenames,
 7574:                                     }
 7575:                      );
 7576:     foreach my $key (keys(%currautoupdate)) {
 7577:         if (($key eq 'run') || ($key eq 'classlists')) {
 7578:             if (exists($updatehash{autoupdate}{$key})) {
 7579:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 7580:                     $changes{$key} = 1;
 7581:                 }
 7582:             }
 7583:         } elsif ($key eq 'fields') {
 7584:             if (ref($currautoupdate{$key}) eq 'HASH') {
 7585:                 foreach my $item (@{$types},'default') {
 7586:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 7587:                         my $change = 0;
 7588:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 7589:                             if (!exists($fields{$item})) {
 7590:                                 $change = 1;
 7591:                                 last;
 7592:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 7593:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 7594:                                     $change = 1;
 7595:                                     last;
 7596:                                 }
 7597:                             }
 7598:                         }
 7599:                         if ($change) {
 7600:                             push(@{$changes{$key}},$item);
 7601:                         }
 7602:                     } 
 7603:                 }
 7604:             }
 7605:         } elsif ($key eq 'lockablenames') {
 7606:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 7607:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 7608:                 if (@changed) {
 7609:                     $changes{'lockablenames'} = 1;
 7610:                 }
 7611:             } else {
 7612:                 if (@lockablenames) {
 7613:                     $changes{'lockablenames'} = 1;
 7614:                 }
 7615:             }
 7616:         }
 7617:     }
 7618:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 7619:         if (@lockablenames) {
 7620:             $changes{'lockablenames'} = 1;
 7621:         }
 7622:     }
 7623:     foreach my $item (@{$types},'default') {
 7624:         if (defined($fields{$item})) {
 7625:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 7626:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 7627:                     my $change = 0;
 7628:                     if (ref($fields{$item}) eq 'ARRAY') {
 7629:                         foreach my $type (@{$fields{$item}}) {
 7630:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 7631:                                 $change = 1;
 7632:                                 last;
 7633:                             }
 7634:                         }
 7635:                     }
 7636:                     if ($change) {
 7637:                         push(@{$changes{'fields'}},$item);
 7638:                     }
 7639:                 } else {
 7640:                     push(@{$changes{'fields'}},$item);
 7641:                 }
 7642:             } else {
 7643:                 push(@{$changes{'fields'}},$item);
 7644:             }
 7645:         }
 7646:     }
 7647:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 7648:                                              $dom);
 7649:     if ($putresult eq 'ok') {
 7650:         if (keys(%changes) > 0) {
 7651:             $resulttext = &mt('Changes made:').'<ul>';
 7652:             foreach my $key (sort(keys(%changes))) {
 7653:                 if ($key eq 'lockablenames') {
 7654:                     $resulttext .= '<li>';
 7655:                     if (@lockablenames) {
 7656:                         $usertypes->{'default'} = $othertitle;
 7657:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 7658:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 7659:                     } else {
 7660:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 7661:                     }
 7662:                     $resulttext .= '</li>';
 7663:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 7664:                     foreach my $item (@{$changes{$key}}) {
 7665:                         my @newvalues;
 7666:                         foreach my $type (@{$fields{$item}}) {
 7667:                             push(@newvalues,$fieldtitles{$type});
 7668:                         }
 7669:                         my $newvaluestr;
 7670:                         if (@newvalues > 0) {
 7671:                             $newvaluestr = join(', ',@newvalues);
 7672:                         } else {
 7673:                             $newvaluestr = &mt('none');
 7674:                         }
 7675:                         if ($item eq 'default') {
 7676:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 7677:                         } else {
 7678:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 7679:                         }
 7680:                     }
 7681:                 } else {
 7682:                     my $newvalue;
 7683:                     if ($key eq 'run') {
 7684:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 7685:                     } else {
 7686:                         $newvalue = $offon[$env{'form.'.$key}];
 7687:                     }
 7688:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 7689:                 }
 7690:             }
 7691:             $resulttext .= '</ul>';
 7692:         } else {
 7693:             $resulttext = &mt('No changes made to autoupdates');
 7694:         }
 7695:     } else {
 7696:         $resulttext = '<span class="LC_error">'.
 7697: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7698:     }
 7699:     return $resulttext;
 7700: }
 7701: 
 7702: sub modify_autocreate {
 7703:     my ($dom,%domconfig) = @_;
 7704:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 7705:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 7706:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 7707:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 7708:         }
 7709:     }
 7710:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 7711:                  req => 'Auto-creation of validated requests for official courses',
 7712:                  xmldc => 'Identity of course creator of courses from XML files',
 7713:                );
 7714:     my @types = ('xml','req');
 7715:     foreach my $item (@types) {
 7716:         $newvals{$item} = $env{'form.autocreate_'.$item};
 7717:         $newvals{$item} =~ s/\D//g;
 7718:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 7719:     }
 7720:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 7721:     my %domcoords = &get_active_dcs($dom);
 7722:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 7723:         $newvals{'xmldc'} = '';
 7724:     } 
 7725:     %autocreatehash =  (
 7726:                         autocreate => { xml => $newvals{'xml'},
 7727:                                         req => $newvals{'req'},
 7728:                                       }
 7729:                        );
 7730:     if ($newvals{'xmldc'} ne '') {
 7731:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 7732:     }
 7733:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 7734:                                              $dom);
 7735:     if ($putresult eq 'ok') {
 7736:         my @items = @types;
 7737:         if ($newvals{'xml'}) {
 7738:             push(@items,'xmldc');
 7739:         }
 7740:         foreach my $item (@items) {
 7741:             if (exists($currautocreate{$item})) {
 7742:                 if ($currautocreate{$item} ne $newvals{$item}) {
 7743:                     $changes{$item} = 1;
 7744:                 }
 7745:             } elsif ($newvals{$item}) {
 7746:                 $changes{$item} = 1;
 7747:             }
 7748:         }
 7749:         if (keys(%changes) > 0) {
 7750:             my @offon = ('off','on'); 
 7751:             $resulttext = &mt('Changes made:').'<ul>';
 7752:             foreach my $item (@types) {
 7753:                 if ($changes{$item}) {
 7754:                     my $newtxt = $offon[$newvals{$item}];
 7755:                     $resulttext .= '<li>'.
 7756:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
 7757:                                        '<b>','</b>').
 7758:                                    '</li>';
 7759:                 }
 7760:             }
 7761:             if ($changes{'xmldc'}) {
 7762:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 7763:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 7764:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
 7765:             }
 7766:             $resulttext .= '</ul>';
 7767:         } else {
 7768:             $resulttext = &mt('No changes made to auto-creation settings');
 7769:         }
 7770:     } else {
 7771:         $resulttext = '<span class="LC_error">'.
 7772:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7773:     }
 7774:     return $resulttext;
 7775: }
 7776: 
 7777: sub modify_directorysrch {
 7778:     my ($dom,%domconfig) = @_;
 7779:     my ($resulttext,%changes);
 7780:     my %currdirsrch;
 7781:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 7782:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 7783:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 7784:         }
 7785:     }
 7786:     my %title = ( available => 'Directory search available',
 7787:                   localonly => 'Other domains can search',
 7788:                   searchby => 'Search types',
 7789:                   searchtypes => 'Search latitude');
 7790:     my @offon = ('off','on');
 7791:     my @otherdoms = ('Yes','No');
 7792: 
 7793:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 7794:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 7795:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 7796: 
 7797:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7798:     if (keys(%{$usertypes}) == 0) {
 7799:         @cansearch = ('default');
 7800:     } else {
 7801:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 7802:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 7803:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 7804:                     push(@{$changes{'cansearch'}},$type);
 7805:                 }
 7806:             }
 7807:             foreach my $type (@cansearch) {
 7808:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 7809:                     push(@{$changes{'cansearch'}},$type);
 7810:                 }
 7811:             }
 7812:         } else {
 7813:             push(@{$changes{'cansearch'}},@cansearch);
 7814:         }
 7815:     }
 7816: 
 7817:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 7818:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 7819:             if (!grep(/^\Q$by\E$/,@searchby)) {
 7820:                 push(@{$changes{'searchby'}},$by);
 7821:             }
 7822:         }
 7823:         foreach my $by (@searchby) {
 7824:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 7825:                 push(@{$changes{'searchby'}},$by);
 7826:             }
 7827:         }
 7828:     } else {
 7829:         push(@{$changes{'searchby'}},@searchby);
 7830:     }
 7831: 
 7832:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 7833:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 7834:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 7835:                 push(@{$changes{'searchtypes'}},$type);
 7836:             }
 7837:         }
 7838:         foreach my $type (@searchtypes) {
 7839:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 7840:                 push(@{$changes{'searchtypes'}},$type);
 7841:             }
 7842:         }
 7843:     } else {
 7844:         if (exists($currdirsrch{'searchtypes'})) {
 7845:             foreach my $type (@searchtypes) {  
 7846:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 7847:                     push(@{$changes{'searchtypes'}},$type);
 7848:                 }
 7849:             }
 7850:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 7851:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 7852:             }   
 7853:         } else {
 7854:             push(@{$changes{'searchtypes'}},@searchtypes); 
 7855:         }
 7856:     }
 7857: 
 7858:     my %dirsrch_hash =  (
 7859:             directorysrch => { available => $env{'form.dirsrch_available'},
 7860:                                cansearch => \@cansearch,
 7861:                                localonly => $env{'form.dirsrch_localonly'},
 7862:                                searchby => \@searchby,
 7863:                                searchtypes => \@searchtypes,
 7864:                              }
 7865:             );
 7866:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 7867:                                              $dom);
 7868:     if ($putresult eq 'ok') {
 7869:         if (exists($currdirsrch{'available'})) {
 7870:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 7871:                  $changes{'available'} = 1;
 7872:              }
 7873:         } else {
 7874:             if ($env{'form.dirsrch_available'} eq '1') {
 7875:                 $changes{'available'} = 1;
 7876:             }
 7877:         }
 7878:         if (exists($currdirsrch{'localonly'})) {
 7879:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 7880:                  $changes{'localonly'} = 1;
 7881:              }
 7882:         } else {
 7883:             if ($env{'form.dirsrch_localonly'} eq '1') {
 7884:                 $changes{'localonly'} = 1;
 7885:             }
 7886:         }
 7887:         if (keys(%changes) > 0) {
 7888:             $resulttext = &mt('Changes made:').'<ul>';
 7889:             if ($changes{'available'}) {
 7890:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 7891:             }
 7892:             if ($changes{'localonly'}) {
 7893:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 7894:             }
 7895: 
 7896:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 7897:                 my $chgtext;
 7898:                 if (ref($usertypes) eq 'HASH') {
 7899:                     if (keys(%{$usertypes}) > 0) {
 7900:                         foreach my $type (@{$types}) {
 7901:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 7902:                                 $chgtext .= $usertypes->{$type}.'; ';
 7903:                             }
 7904:                         }
 7905:                         if (grep(/^default$/,@cansearch)) {
 7906:                             $chgtext .= $othertitle;
 7907:                         } else {
 7908:                             $chgtext =~ s/\; $//;
 7909:                         }
 7910:                         $resulttext .=
 7911:                             '<li>'.
 7912:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
 7913:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
 7914:                             '</li>';
 7915:                     }
 7916:                 }
 7917:             }
 7918:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 7919:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 7920:                 my $chgtext;
 7921:                 foreach my $type (@{$titleorder}) {
 7922:                     if (grep(/^\Q$type\E$/,@searchby)) {
 7923:                         if (defined($searchtitles->{$type})) {
 7924:                             $chgtext .= $searchtitles->{$type}.'; ';
 7925:                         }
 7926:                     }
 7927:                 }
 7928:                 $chgtext =~ s/\; $//;
 7929:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 7930:             }
 7931:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 7932:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 7933:                 my $chgtext;
 7934:                 foreach my $type (@{$srchtypeorder}) {
 7935:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 7936:                         if (defined($srchtypes_desc->{$type})) {
 7937:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 7938:                         }
 7939:                     }
 7940:                 }
 7941:                 $chgtext =~ s/\; $//;
 7942:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
 7943:             }
 7944:             $resulttext .= '</ul>';
 7945:         } else {
 7946:             $resulttext = &mt('No changes made to institution directory search settings');
 7947:         }
 7948:     } else {
 7949:         $resulttext = '<span class="LC_error">'.
 7950:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 7951:     }
 7952:     return $resulttext;
 7953: }
 7954: 
 7955: sub modify_contacts {
 7956:     my ($dom,$lastactref,%domconfig) = @_;
 7957:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 7958:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 7959:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 7960:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 7961:         }
 7962:     }
 7963:     my (%others,%to,%bcc);
 7964:     my @contacts = ('supportemail','adminemail');
 7965:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 7966:                     'requestsmail','updatesmail','idconflictsmail');
 7967:     my @toggles = ('reporterrors','reportupdates');
 7968:     foreach my $type (@mailings) {
 7969:         @{$newsetting{$type}} = 
 7970:             &Apache::loncommon::get_env_multiple('form.'.$type);
 7971:         foreach my $item (@contacts) {
 7972:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 7973:                 $contacts_hash{contacts}{$type}{$item} = 1;
 7974:             } else {
 7975:                 $contacts_hash{contacts}{$type}{$item} = 0;
 7976:             }
 7977:         }  
 7978:         $others{$type} = $env{'form.'.$type.'_others'};
 7979:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 7980:         if ($type eq 'helpdeskmail') {
 7981:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 7982:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 7983:         }
 7984:     }
 7985:     foreach my $item (@contacts) {
 7986:         $to{$item} = $env{'form.'.$item};
 7987:         $contacts_hash{'contacts'}{$item} = $to{$item};
 7988:     }
 7989:     foreach my $item (@toggles) {
 7990:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
 7991:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
 7992:         }
 7993:     }
 7994:     if (keys(%currsetting) > 0) {
 7995:         foreach my $item (@contacts) {
 7996:             if ($to{$item} ne $currsetting{$item}) {
 7997:                 $changes{$item} = 1;
 7998:             }
 7999:         }
 8000:         foreach my $type (@mailings) {
 8001:             foreach my $item (@contacts) {
 8002:                 if (ref($currsetting{$type}) eq 'HASH') {
 8003:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 8004:                         push(@{$changes{$type}},$item);
 8005:                     }
 8006:                 } else {
 8007:                     push(@{$changes{$type}},@{$newsetting{$type}});
 8008:                 }
 8009:             }
 8010:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 8011:                 push(@{$changes{$type}},'others');
 8012:             }
 8013:             if ($type eq 'helpdeskmail') {   
 8014:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 8015:                     push(@{$changes{$type}},'bcc'); 
 8016:                 }
 8017:             }
 8018:         }
 8019:     } else {
 8020:         my %default;
 8021:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 8022:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 8023:         $default{'errormail'} = 'adminemail';
 8024:         $default{'packagesmail'} = 'adminemail';
 8025:         $default{'helpdeskmail'} = 'supportemail';
 8026:         $default{'lonstatusmail'} = 'adminemail';
 8027:         $default{'requestsmail'} = 'adminemail';
 8028:         $default{'updatesmail'} = 'adminemail';
 8029:         foreach my $item (@contacts) {
 8030:            if ($to{$item} ne $default{$item}) {
 8031:               $changes{$item} = 1;
 8032:            }
 8033:         }
 8034:         foreach my $type (@mailings) {
 8035:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 8036:                
 8037:                 push(@{$changes{$type}},@{$newsetting{$type}});
 8038:             }
 8039:             if ($others{$type} ne '') {
 8040:                 push(@{$changes{$type}},'others');
 8041:             }
 8042:             if ($type eq 'helpdeskmail') {
 8043:                 if ($bcc{$type} ne '') {
 8044:                     push(@{$changes{$type}},'bcc');
 8045:                 }
 8046:             }
 8047:         }
 8048:     }
 8049:     foreach my $item (@toggles) {
 8050:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
 8051:             $changes{$item} = 1;
 8052:         } elsif ((!$env{'form.'.$item}) &&
 8053:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
 8054:             $changes{$item} = 1;
 8055:         }
 8056:     }
 8057:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 8058:                                              $dom);
 8059:     if ($putresult eq 'ok') {
 8060:         if (keys(%changes) > 0) {
 8061:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 8062:             if (ref($lastactref) eq 'HASH') {
 8063:                 $lastactref->{'domainconfig'} = 1;
 8064:             }
 8065:             my ($titles,$short_titles)  = &contact_titles();
 8066:             $resulttext = &mt('Changes made:').'<ul>';
 8067:             foreach my $item (@contacts) {
 8068:                 if ($changes{$item}) {
 8069:                     $resulttext .= '<li>'.$titles->{$item}.
 8070:                                     &mt(' set to: ').
 8071:                                     '<span class="LC_cusr_emph">'.
 8072:                                     $to{$item}.'</span></li>';
 8073:                 }
 8074:             }
 8075:             foreach my $type (@mailings) {
 8076:                 if (ref($changes{$type}) eq 'ARRAY') {
 8077:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 8078:                     my @text;
 8079:                     foreach my $item (@{$newsetting{$type}}) {
 8080:                         push(@text,$short_titles->{$item});
 8081:                     }
 8082:                     if ($others{$type} ne '') {
 8083:                         push(@text,$others{$type});
 8084:                     }
 8085:                     $resulttext .= '<span class="LC_cusr_emph">'.
 8086:                                    join(', ',@text).'</span>';
 8087:                     if ($type eq 'helpdeskmail') {
 8088:                         if ($bcc{$type} ne '') {
 8089:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 8090:                         }
 8091:                     }
 8092:                     $resulttext .= '</li>';
 8093:                 }
 8094:             }
 8095:             my @offon = ('off','on');
 8096:             if ($changes{'reporterrors'}) {
 8097:                 $resulttext .= '<li>'.
 8098:                                &mt('E-mail error reports to [_1] set to "'.
 8099:                                    $offon[$env{'form.reporterrors'}].'".',
 8100:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 8101:                                        &mt('LON-CAPA core group - MSU'),600,500)).
 8102:                                '</li>';
 8103:             }
 8104:             if ($changes{'reportupdates'}) {
 8105:                 $resulttext .= '<li>'.
 8106:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
 8107:                                     $offon[$env{'form.reportupdates'}].'".',
 8108:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 8109:                                         &mt('LON-CAPA core group - MSU'),600,500)).
 8110:                                 '</li>';
 8111:             }
 8112:             $resulttext .= '</ul>';
 8113:         } else {
 8114:             $resulttext = &mt('No changes made to contact information');
 8115:         }
 8116:     } else {
 8117:         $resulttext = '<span class="LC_error">'.
 8118:             &mt('An error occurred: [_1].',$putresult).'</span>';
 8119:     }
 8120:     return $resulttext;
 8121: }
 8122: 
 8123: sub modify_usercreation {
 8124:     my ($dom,%domconfig) = @_;
 8125:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
 8126:     my $warningmsg;
 8127:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 8128:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 8129:             if ($key eq 'cancreate') {
 8130:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 8131:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 8132:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 8133:                             ($item eq 'captcha') || ($item eq 'recaptchakeys')) {
 8134:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8135:                         } else {
 8136:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8137:                         }
 8138:                     }
 8139:                 }
 8140:             } elsif ($key eq 'email_rule') {
 8141:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 8142:             } else {
 8143:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 8144:             }
 8145:         }
 8146:     }
 8147:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 8148:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 8149:     my @contexts = ('author','course','requestcrs');
 8150:     foreach my $item(@contexts) {
 8151:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
 8152:     }
 8153:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8154:         foreach my $item (@contexts) {
 8155:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 8156:                 push(@{$changes{'cancreate'}},$item);
 8157:             }
 8158:         }
 8159:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 8160:         foreach my $item (@contexts) {
 8161:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 8162:                 if ($cancreate{$item} ne 'any') {
 8163:                     push(@{$changes{'cancreate'}},$item);
 8164:                 }
 8165:             } else {
 8166:                 if ($cancreate{$item} ne 'none') {
 8167:                     push(@{$changes{'cancreate'}},$item);
 8168:                 }
 8169:             }
 8170:         }
 8171:     } else {
 8172:         foreach my $item (@contexts)  {
 8173:             push(@{$changes{'cancreate'}},$item);
 8174:         }
 8175:     }
 8176: 
 8177:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 8178:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 8179:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 8180:                 push(@{$changes{'username_rule'}},$type);
 8181:             }
 8182:         }
 8183:         foreach my $type (@username_rule) {
 8184:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 8185:                 push(@{$changes{'username_rule'}},$type);
 8186:             }
 8187:         }
 8188:     } else {
 8189:         push(@{$changes{'username_rule'}},@username_rule);
 8190:     }
 8191: 
 8192:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 8193:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 8194:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 8195:                 push(@{$changes{'id_rule'}},$type);
 8196:             }
 8197:         }
 8198:         foreach my $type (@id_rule) {
 8199:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 8200:                 push(@{$changes{'id_rule'}},$type);
 8201:             }
 8202:         }
 8203:     } else {
 8204:         push(@{$changes{'id_rule'}},@id_rule);
 8205:     }
 8206: 
 8207:     my @authen_contexts = ('author','course','domain');
 8208:     my @authtypes = ('int','krb4','krb5','loc');
 8209:     my %authhash;
 8210:     foreach my $item (@authen_contexts) {
 8211:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 8212:         foreach my $auth (@authtypes) {
 8213:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 8214:                 $authhash{$item}{$auth} = 1;
 8215:             } else {
 8216:                 $authhash{$item}{$auth} = 0;
 8217:             }
 8218:         }
 8219:     }
 8220:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 8221:         foreach my $item (@authen_contexts) {
 8222:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 8223:                 foreach my $auth (@authtypes) {
 8224:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 8225:                         push(@{$changes{'authtypes'}},$item);
 8226:                         last;
 8227:                     }
 8228:                 }
 8229:             }
 8230:         }
 8231:     } else {
 8232:         foreach my $item (@authen_contexts) {
 8233:             push(@{$changes{'authtypes'}},$item);
 8234:         }
 8235:     }
 8236: 
 8237:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
 8238:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
 8239:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
 8240:     $save_usercreate{'id_rule'} = \@id_rule;
 8241:     $save_usercreate{'username_rule'} = \@username_rule,
 8242:     $save_usercreate{'authtypes'} = \%authhash;
 8243: 
 8244:     my %usercreation_hash =  (
 8245:         usercreation     => \%save_usercreate,
 8246:     );
 8247: 
 8248:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 8249:                                              $dom);
 8250: 
 8251:     if ($putresult eq 'ok') {
 8252:         if (keys(%changes) > 0) {
 8253:             $resulttext = &mt('Changes made:').'<ul>';
 8254:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8255:                 my %lt = &usercreation_types();
 8256:                 foreach my $type (@{$changes{'cancreate'}}) {
 8257:                     my $chgtext = $lt{$type}.', ';
 8258:                     if ($cancreate{$type} eq 'none') {
 8259:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 8260:                     } elsif ($cancreate{$type} eq 'any') {
 8261:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 8262:                     } elsif ($cancreate{$type} eq 'official') {
 8263:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 8264:                     } elsif ($cancreate{$type} eq 'unofficial') {
 8265:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 8266:                     }
 8267:                     $resulttext .= '<li>'.$chgtext.'</li>';
 8268:                 }
 8269:             }
 8270:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 8271:                 my ($rules,$ruleorder) = 
 8272:                     &Apache::lonnet::inst_userrules($dom,'username');
 8273:                 my $chgtext = '<ul>';
 8274:                 foreach my $type (@username_rule) {
 8275:                     if (ref($rules->{$type}) eq 'HASH') {
 8276:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 8277:                     }
 8278:                 }
 8279:                 $chgtext .= '</ul>';
 8280:                 if (@username_rule > 0) {
 8281:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 8282:                 } else {
 8283:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 8284:                 }
 8285:             }
 8286:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 8287:                 my ($idrules,$idruleorder) = 
 8288:                     &Apache::lonnet::inst_userrules($dom,'id');
 8289:                 my $chgtext = '<ul>';
 8290:                 foreach my $type (@id_rule) {
 8291:                     if (ref($idrules->{$type}) eq 'HASH') {
 8292:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 8293:                     }
 8294:                 }
 8295:                 $chgtext .= '</ul>';
 8296:                 if (@id_rule > 0) {
 8297:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 8298:                 } else {
 8299:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 8300:                 }
 8301:             }
 8302:             my %authname = &authtype_names();
 8303:             my %context_title = &context_names();
 8304:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 8305:                 my $chgtext = '<ul>';
 8306:                 foreach my $type (@{$changes{'authtypes'}}) {
 8307:                     my @allowed;
 8308:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 8309:                     foreach my $auth (@authtypes) {
 8310:                         if ($authhash{$type}{$auth}) {
 8311:                             push(@allowed,$authname{$auth});
 8312:                         }
 8313:                     }
 8314:                     if (@allowed > 0) {
 8315:                         $chgtext .= join(', ',@allowed).'</li>';
 8316:                     } else {
 8317:                         $chgtext .= &mt('none').'</li>';
 8318:                     }
 8319:                 }
 8320:                 $chgtext .= '</ul>';
 8321:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 8322:                 $resulttext .= '</li>';
 8323:             }
 8324:             $resulttext .= '</ul>';
 8325:         } else {
 8326:             $resulttext = &mt('No changes made to user creation settings');
 8327:         }
 8328:     } else {
 8329:         $resulttext = '<span class="LC_error">'.
 8330:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8331:     }
 8332:     if ($warningmsg ne '') {
 8333:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 8334:     }
 8335:     return $resulttext;
 8336: }
 8337: 
 8338: sub modify_selfcreation {
 8339:     my ($dom,%domconfig) = @_;
 8340:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%changes,%cancreate);
 8341:     my (%save_usercreate,%save_usermodify);
 8342:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8343:     if (ref($types) eq 'ARRAY') {
 8344:         $usertypes->{'default'} = $othertitle;
 8345:         push(@{$types},'default');
 8346:     }
 8347: #
 8348: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
 8349: #
 8350:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 8351:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 8352:             if ($key eq 'cancreate') {
 8353:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 8354:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 8355:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 8356:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') || 
 8357:                             ($item eq 'emailusername') || ($item eq 'notify') ||
 8358:                             ($item eq 'selfcreateprocessing') || ($item eq 'shibenv')) {
 8359:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8360:                         } else {
 8361:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8362:                         }
 8363:                     }
 8364:                 }
 8365:             } elsif ($key eq 'email_rule') {
 8366:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 8367:             } else {
 8368:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 8369:             }
 8370:         }
 8371:     }
 8372: #
 8373: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
 8374: #
 8375:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 8376:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 8377:             if ($key eq 'selfcreate') {
 8378:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
 8379:             } else {
 8380:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
 8381:             }
 8382:         }
 8383:     }
 8384: 
 8385:     my @contexts = ('selfcreate');
 8386:     @{$cancreate{'selfcreate'}} = ();
 8387:     %{$cancreate{'emailusername'}} = ();
 8388:     @{$cancreate{'statustocreate'}} = ();
 8389:     %{$cancreate{'selfcreateprocessing'}} = ();
 8390:     %{$cancreate{'shibenv'}} = ();
 8391:     my %selfcreatetypes = (
 8392:                              sso   => 'users authenticated by institutional single sign on',
 8393:                              login => 'users authenticated by institutional log-in',
 8394:                              email => 'users who provide a valid e-mail address for use as username',
 8395:                           );
 8396: #
 8397: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
 8398: # is permitted.
 8399: #
 8400: 
 8401:     my @statuses;
 8402:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 8403:         if (ref($domconfig{'inststatus'}{'inststatusguest'}) eq 'ARRAY') {
 8404:             @statuses = @{$domconfig{'inststatus'}{'inststatusguest'}};
 8405:         }
 8406:     }
 8407:     push(@statuses,'default');
 8408: 
 8409:     foreach my $item ('login','sso','email') {
 8410:         if ($item eq 'email') {
 8411:             if ($env{'form.cancreate_email'}) {
 8412:                 push(@{$cancreate{'selfcreate'}},'email');
 8413:                 push(@contexts,'selfcreateprocessing');
 8414:                 foreach my $type (@statuses) {
 8415:                     if ($type eq 'default') {
 8416:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess'};
 8417:                     } else { 
 8418:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
 8419:                     }
 8420:                 }
 8421:             }
 8422:         } else {
 8423:             if ($env{'form.cancreate_'.$item}) {
 8424:                 push(@{$cancreate{'selfcreate'}},$item);
 8425:             }
 8426:         }
 8427:     }
 8428:     my (@email_rule,%userinfo,%savecaptcha);
 8429:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 8430: #
 8431: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
 8432: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
 8433: #
 8434: 
 8435:     if ($env{'form.cancreate_email'} eq 'email') {
 8436:         push(@contexts,'emailusername');
 8437:         if (ref($types) eq 'ARRAY') {
 8438:             foreach my $type (@{$types}) {
 8439:                 if (ref($infofields) eq 'ARRAY') {
 8440:                     foreach my $field (@{$infofields}) {
 8441:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
 8442:                             $cancreate{'emailusername'}{$type}{$field} = $1;
 8443:                         }
 8444:                     }
 8445:                 }
 8446:             }
 8447:         }
 8448: #
 8449: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
 8450: # queued requests for self-creation of account using e-mail address as username
 8451: #
 8452: 
 8453:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
 8454:         @approvalnotify = sort(@approvalnotify);
 8455:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
 8456:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8457:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
 8458:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
 8459:                     push(@{$changes{'cancreate'}},'notify');
 8460:                 }
 8461:             } else {
 8462:                 if ($cancreate{'notify'}{'approval'}) {
 8463:                     push(@{$changes{'cancreate'}},'notify');
 8464:                 }
 8465:             }
 8466:         } elsif ($cancreate{'notify'}{'approval'}) {
 8467:             push(@{$changes{'cancreate'}},'notify');
 8468:         }
 8469: 
 8470: #
 8471: # Retrieve rules (if any) governing types of e-mail address which may be used as a username
 8472: #
 8473:         @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 8474:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
 8475:         if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 8476:             if (@{$curr_usercreation{'email_rule'}} > 0) {
 8477:                 foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 8478:                     if (!grep(/^\Q$type\E$/,@email_rule)) {
 8479:                         push(@{$changes{'email_rule'}},$type);
 8480:                     }
 8481:                 }
 8482:             }
 8483:             if (@email_rule > 0) {
 8484:                 foreach my $type (@email_rule) {
 8485:                     if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 8486:                         push(@{$changes{'email_rule'}},$type);
 8487:                     }
 8488:                 }
 8489:             }
 8490:         } elsif (@email_rule > 0) {
 8491:             push(@{$changes{'email_rule'}},@email_rule);
 8492:         }
 8493:     }
 8494: #  
 8495: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
 8496: # institutional log-in.
 8497: #
 8498:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
 8499:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8500:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
 8501:                ($domdefaults{'auth_def'} eq 'localauth'))) {
 8502:             $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.').' '.
 8503:                           &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.');
 8504:         }
 8505:     }
 8506:     my @fields = ('lastname','firstname','middlename','generation',
 8507:                   'permanentemail','id');
 8508:     my @shibfields = (@fields,'inststatus');
 8509:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8510: #
 8511: # Where usernames may created for institutional log-in and/or institutional single sign on:
 8512: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
 8513: # may self-create accounts 
 8514: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
 8515: # which the user may supply, if institutional data is unavailable.
 8516: #
 8517:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
 8518:         if (ref($types) eq 'ARRAY') {
 8519:             if (@{$types} > 1) {
 8520:                 @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
 8521:                 push(@contexts,'statustocreate');
 8522:             } else {
 8523:                 undef($cancreate{'statustocreate'});
 8524:             } 
 8525:             foreach my $type (@{$types}) {
 8526:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
 8527:                 foreach my $field (@fields) {
 8528:                     if (grep(/^\Q$field\E$/,@modifiable)) {
 8529:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
 8530:                     } else {
 8531:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
 8532:                     }
 8533:                 }
 8534:             }
 8535:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
 8536:                 foreach my $type (@{$types}) {
 8537:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
 8538:                         foreach my $field (@fields) {
 8539:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
 8540:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
 8541:                                 push(@{$changes{'selfcreate'}},$type);
 8542:                                 last;
 8543:                             }
 8544:                         }
 8545:                     }
 8546:                 }
 8547:             } else {
 8548:                 foreach my $type (@{$types}) {
 8549:                     push(@{$changes{'selfcreate'}},$type);
 8550:                 }
 8551:             }
 8552:         }
 8553:         foreach my $field (@shibfields) {
 8554:             if ($env{'form.shibenv_'.$field} ne '') {
 8555:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
 8556:             }
 8557:         }
 8558:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8559:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
 8560:                 foreach my $field (@shibfields) {
 8561:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
 8562:                         push(@{$changes{'cancreate'}},'shibenv');
 8563:                     }
 8564:                 }
 8565:             } else {
 8566:                 foreach my $field (@shibfields) {
 8567:                     if ($env{'form.shibenv_'.$field}) {
 8568:                         push(@{$changes{'cancreate'}},'shibenv');
 8569:                         last;
 8570:                     }
 8571:                 }
 8572:             }
 8573:         }
 8574:     }
 8575:     foreach my $item (@contexts) {
 8576:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 8577:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 8578:                 if (ref($cancreate{$item}) eq 'ARRAY') {
 8579:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 8580:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8581:                             push(@{$changes{'cancreate'}},$item);
 8582:                         }
 8583:                     }
 8584:                 }
 8585:             }
 8586:             if (ref($cancreate{$item}) eq 'ARRAY') {
 8587:                 foreach my $type (@{$cancreate{$item}}) {
 8588:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 8589:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8590:                             push(@{$changes{'cancreate'}},$item);
 8591:                         }
 8592:                     }
 8593:                 }
 8594:             }
 8595:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
 8596:             if (ref($cancreate{$item}) eq 'HASH') {
 8597:                 foreach my $curr (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
 8598:                     if (ref($curr_usercreation{'cancreate'}{$item}{$curr}) eq 'HASH') {
 8599:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$curr}})) {
 8600:                             unless ($curr_usercreation{'cancreate'}{$item}{$curr}{$field} eq $cancreate{$item}{$curr}{$field}) {
 8601:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8602:                                     push(@{$changes{'cancreate'}},$item);
 8603:                                 }
 8604:                             }
 8605:                         }
 8606:                     } elsif ($item eq 'selfcreateprocessing') {
 8607:                         if ($cancreate{$item}{$curr} ne $curr_usercreation{'cancreate'}{$item}{$curr}) {
 8608:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8609:                                 push(@{$changes{'cancreate'}},$item);
 8610:                             }
 8611:                         }
 8612:                     } else {
 8613:                         if (!$cancreate{$item}{$curr}) {
 8614:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8615:                                 push(@{$changes{'cancreate'}},$item);
 8616:                             }
 8617:                         }
 8618:                     }
 8619:                 }
 8620:                 foreach my $field (keys(%{$cancreate{$item}})) {
 8621:                     if (ref($cancreate{$item}{$field}) eq 'HASH') {
 8622:                         foreach my $inner (keys(%{$cancreate{$item}{$field}})) {
 8623:                             if (ref($curr_usercreation{'cancreate'}{$item}{$field}) eq 'HASH') {
 8624:                                 unless ($curr_usercreation{'cancreate'}{$item}{$field}{$inner} eq $cancreate{$item}{$field}{$inner}) {
 8625:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8626:                                         push(@{$changes{'cancreate'}},$item);
 8627:                                     }
 8628:                                 }
 8629:                             } else {
 8630:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8631:                                     push(@{$changes{'cancreate'}},$item);
 8632:                                 }
 8633:                             }
 8634:                         }
 8635:                     } elsif ($item eq 'selfcreateprocessing') {
 8636:                         if ($cancreate{$item}{$field} ne $curr_usercreation{'cancreate'}{$item}{$field}) {
 8637:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8638:                                 push(@{$changes{'cancreate'}},$item);
 8639:                             }
 8640:                         }
 8641:                     } else {
 8642:                         if (!$curr_usercreation{'cancreate'}{$item}{$field}) {
 8643:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8644:                                 push(@{$changes{'cancreate'}},$item);
 8645:                             }
 8646:                         }
 8647:                     }
 8648:                 }
 8649:             }
 8650:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
 8651:             if (ref($cancreate{$item}) eq 'ARRAY') {
 8652:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
 8653:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8654:                         push(@{$changes{'cancreate'}},$item);
 8655:                     }
 8656:                 }
 8657:             } elsif (ref($cancreate{$item}) eq 'HASH') {
 8658:                 if (!$cancreate{$item}{$curr_usercreation{'cancreate'}{$item}}) {
 8659:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8660:                         push(@{$changes{'cancreate'}},$item);
 8661:                     }
 8662:                 }
 8663:             }
 8664:         } elsif ($item eq 'emailusername') {
 8665:             if (ref($cancreate{$item}) eq 'HASH') {
 8666:                 foreach my $type (keys(%{$cancreate{$item}})) {
 8667:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
 8668:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
 8669:                             if ($cancreate{$item}{$type}{$field}) {
 8670:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8671:                                     push(@{$changes{'cancreate'}},$item);
 8672:                                 }
 8673:                                 last;
 8674:                             }
 8675:                         }
 8676:                     }
 8677:                 }
 8678:             }
 8679:         }
 8680:     }
 8681: #
 8682: # Populate %save_usercreate hash with updates to self-creation configuration.
 8683: #
 8684:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
 8685:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
 8686:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
 8687:     if (ref($cancreate{'notify'}) eq 'HASH') {
 8688:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
 8689:     }
 8690:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
 8691:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
 8692:     }
 8693:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 8694:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
 8695:     }
 8696:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
 8697:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
 8698:     }
 8699:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
 8700:     $save_usercreate{'emailrule'} = \@email_rule;
 8701: 
 8702:     my %userconfig_hash = (
 8703:             usercreation     => \%save_usercreate,
 8704:             usermodification => \%save_usermodify,
 8705:     );
 8706:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
 8707:                                              $dom);
 8708: #
 8709: # Accumulate details of changes to domain cofiguration for self-creation of usernames in $resulttext
 8710: #
 8711:     if ($putresult eq 'ok') {
 8712:         if (keys(%changes) > 0) {
 8713:             $resulttext = &mt('Changes made:').'<ul>';
 8714:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8715:                 my %lt = &selfcreation_types();
 8716:                 foreach my $type (@{$changes{'cancreate'}}) {
 8717:                     my $chgtext;
 8718:                     if ($type eq 'selfcreate') {
 8719:                         if (@{$cancreate{$type}} == 0) {
 8720:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
 8721:                         } else {
 8722:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
 8723:                                         '<ul>';
 8724:                             foreach my $case (@{$cancreate{$type}}) {
 8725:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 8726:                             }
 8727:                             $chgtext .= '</ul>';
 8728:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 8729:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 8730:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 8731:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 8732:                                             $chgtext .= '<br />'.
 8733:                                                         '<span class="LC_warning">'.
 8734:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 8735:                                                         '</span>';
 8736:                                         }
 8737:                                     }
 8738:                                 }
 8739:                             }
 8740:                         }
 8741:                     } elsif ($type eq 'shibenv') {
 8742:                         if (keys(%{$cancreate{$type}}) == 0) {
 8743:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information'); 
 8744:                         } else {
 8745:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
 8746:                                         '<ul>';
 8747:                             foreach my $field (@shibfields) {
 8748:                                 next if ($cancreate{$type}{$field} eq '');
 8749:                                 if ($field eq 'inststatus') {
 8750:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
 8751:                                 } else {
 8752:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
 8753:                                 }
 8754:                             }
 8755:                             $chgtext .= '</ul>';
 8756:                         }  
 8757:                     } elsif ($type eq 'statustocreate') {
 8758:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 8759:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 8760:                             if (@{$cancreate{'selfcreate'}} > 0) {
 8761:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 8762:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 8763:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 8764:                                         $chgtext .= '<br />'.
 8765:                                                     '<span class="LC_warning">'.
 8766:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 8767:                                                     '</span>';
 8768:                                     }
 8769:                                 } elsif (ref($usertypes) eq 'HASH') {
 8770:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 8771:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 8772:                                     } else {
 8773:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 8774:                                     }
 8775:                                     $chgtext .= '<ul>';
 8776:                                     foreach my $case (@{$cancreate{$type}}) {
 8777:                                         if ($case eq 'default') {
 8778:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 8779:                                         } else {
 8780:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 8781:                                         }
 8782:                                     }
 8783:                                     $chgtext .= '</ul>';
 8784:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 8785:                                         $chgtext .= '<br /><span class="LC_warning">'.
 8786:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
 8787:                                                     '</span>';
 8788:                                     }
 8789:                                 }
 8790:                             } else {
 8791:                                 if (@{$cancreate{$type}} == 0) {
 8792:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 8793:                                 } else {
 8794:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 8795:                                 }
 8796:                             }
 8797:                         }
 8798:                     } elsif ($type eq 'selfcreateprocessing') {
 8799:                         my %choices = &Apache::lonlocal::texthash (
 8800:                                                                     automatic => 'Automatic approval',
 8801:                                                                     approval  => 'Queued for approval',
 8802:                                                                   );
 8803:                         if (@statuses > 1) {
 8804:                             $chgtext .= &mt('Processing of requests to create account with e-mail address as username set as follows:'). 
 8805:                                         '<ul>';
 8806:                            foreach my $type (@statuses) {
 8807:                                if ($type eq 'default') {
 8808:                                    $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
 8809:                                } else {
 8810:                                    $chgtext .= '<li>'.$usertypes->{$type}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
 8811:                                }
 8812:                            }
 8813:                            $chgtext .= '</ul>';
 8814:                         } else {
 8815:                            $chgtext .= &mt('Processing of requests to create account with e-mail address as username set to: "[_1]"',
 8816:                                          $choices{$cancreate{'selfcreateprocessing'}{'default'}});
 8817:                         }
 8818:                     } elsif ($type eq 'captcha') {
 8819:                         if ($savecaptcha{$type} eq 'notused') {
 8820:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 8821:                         } else {
 8822:                             my %captchas = &captcha_phrases();
 8823:                             if ($captchas{$savecaptcha{$type}}) {
 8824:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
 8825:                             } else {
 8826:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
 8827:                             }
 8828:                         }
 8829:                     } elsif ($type eq 'recaptchakeys') {
 8830:                         my ($privkey,$pubkey);
 8831:                         if (ref($savecaptcha{$type}) eq 'HASH') {
 8832:                             $pubkey = $savecaptcha{$type}{'public'};
 8833:                             $privkey = $savecaptcha{$type}{'private'};
 8834:                         }
 8835:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 8836:                         if (!$pubkey) {
 8837:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 8838:                         } else {
 8839:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 8840:                         }
 8841:                         if (!$privkey) {
 8842:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 8843:                         } else {
 8844:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 8845:                         }
 8846:                         $chgtext .= '</ul>';
 8847:                     } elsif ($type eq 'emailusername') {
 8848:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
 8849:                             if (ref($types) eq 'ARRAY') {
 8850:                                 foreach my $type (@{$types}) {
 8851:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
 8852:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
 8853:                                             $chgtext .= &mt('When self-creating account with e-mail as username, the following information will be provided by [_1]:',$usertypes->{$type}).
 8854:                                                     '<ul>';
 8855:                                             foreach my $field (@{$infofields}) {
 8856:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
 8857:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
 8858:                                                 }
 8859:                                             }
 8860:                                         }
 8861:                                         $chgtext .= '</ul>';
 8862:                                     } else {
 8863:                                         $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 />';
 8864:                                     }
 8865:                                 }
 8866:                             }
 8867:                         }
 8868:                     } elsif ($type eq 'notify') {
 8869:                         $chgtext = &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
 8870:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8871:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
 8872:                                 if ($cancreate{'notify'}{'approval'}) {
 8873:                                     $chgtext = &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
 8874:                                 }
 8875:                             }
 8876:                         }
 8877:                     }
 8878:                     if ($chgtext) {
 8879:                         $resulttext .= '<li>'.$chgtext.'</li>';
 8880:                     }
 8881:                 }
 8882:             }
 8883:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 8884:                 my ($emailrules,$emailruleorder) =
 8885:                     &Apache::lonnet::inst_userrules($dom,'email');
 8886:                 my $chgtext = '<ul>';
 8887:                 foreach my $type (@email_rule) {
 8888:                     if (ref($emailrules->{$type}) eq 'HASH') {
 8889:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 8890:                     }
 8891:                 }
 8892:                 $chgtext .= '</ul>';
 8893:                 if (@email_rule > 0) {
 8894:                     $resulttext .= '<li>'.
 8895:                                    &mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').
 8896:                                        $chgtext.
 8897:                                    '</li>';
 8898:                 } else {
 8899:                     $resulttext .= '<li>'.
 8900:                                    &mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').
 8901:                                    '</li>';
 8902:                 }
 8903:             }
 8904:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
 8905:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
 8906:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8907:                 foreach my $type (@{$changes{'selfcreate'}}) {
 8908:                     my $typename = $type;
 8909:                     if (ref($usertypes) eq 'HASH') {
 8910:                         if ($usertypes->{$type} ne '') {
 8911:                             $typename = $usertypes->{$type};
 8912:                         }
 8913:                     }
 8914:                     my @modifiable;
 8915:                     $resulttext .= '<li>'.
 8916:                                     &mt('Self-creation of account by users with status: [_1]',
 8917:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
 8918:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
 8919:                     foreach my $field (@fields) {
 8920:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
 8921:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
 8922:                         }
 8923:                     }
 8924:                     if (@modifiable > 0) {
 8925:                         $resulttext .= join(', ',@modifiable);
 8926:                     } else {
 8927:                         $resulttext .= &mt('none');
 8928:                     }
 8929:                     $resulttext .= '</li>';
 8930:                 }
 8931:                 $resulttext .= '</ul></li>';
 8932:             }
 8933:             $resulttext .= '</ul>';
 8934:         } else {
 8935:             $resulttext = &mt('No changes made to self-creation settings');
 8936:         }
 8937:     } else {
 8938:         $resulttext = '<span class="LC_error">'.
 8939:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8940:     }
 8941:     if ($warningmsg ne '') {
 8942:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 8943:     }
 8944:     return $resulttext;
 8945: }
 8946: 
 8947: sub process_captcha {
 8948:     my ($container,$changes,$newsettings,$current) = @_;
 8949:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 8950:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 8951:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 8952:         $newsettings->{'captcha'} = 'original';
 8953:     }
 8954:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 8955:         if ($container eq 'cancreate') {
 8956:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 8957:                 push(@{$changes->{'cancreate'}},'captcha');
 8958:             } elsif (!defined($changes->{'cancreate'})) {
 8959:                 $changes->{'cancreate'} = ['captcha'];
 8960:             }
 8961:         } else {
 8962:             $changes->{'captcha'} = 1;
 8963:         }
 8964:     }
 8965:     my ($newpub,$newpriv,$currpub,$currpriv);
 8966:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 8967:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 8968:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 8969:         $newpub =~ s/\W//g;
 8970:         $newpriv =~ s/\W//g;
 8971:         $newsettings->{'recaptchakeys'} = {
 8972:                                              public  => $newpub,
 8973:                                              private => $newpriv,
 8974:                                           };
 8975:     }
 8976:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 8977:         $currpub = $current->{'recaptchakeys'}{'public'};
 8978:         $currpriv = $current->{'recaptchakeys'}{'private'};
 8979:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
 8980:             $newsettings->{'recaptchakeys'} = {
 8981:                                                  public  => '',
 8982:                                                  private => '',
 8983:                                               }
 8984:         }
 8985:     }
 8986:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 8987:         if ($container eq 'cancreate') {
 8988:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 8989:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
 8990:             } elsif (!defined($changes->{'cancreate'})) {
 8991:                 $changes->{'cancreate'} = ['recaptchakeys'];
 8992:             }
 8993:         } else {
 8994:             $changes->{'recaptchakeys'} = 1;
 8995:         }
 8996:     }
 8997:     return;
 8998: }
 8999: 
 9000: sub modify_usermodification {
 9001:     my ($dom,%domconfig) = @_;
 9002:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
 9003:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 9004:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 9005:             if ($key eq 'selfcreate') {
 9006:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
 9007:             } else {  
 9008:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 9009:             }
 9010:         }
 9011:     }
 9012:     my @contexts = ('author','course');
 9013:     my %context_title = (
 9014:                            author => 'In author context',
 9015:                            course => 'In course context',
 9016:                         );
 9017:     my @fields = ('lastname','firstname','middlename','generation',
 9018:                   'permanentemail','id');
 9019:     my %roles = (
 9020:                   author => ['ca','aa'],
 9021:                   course => ['st','ep','ta','in','cr'],
 9022:                 );
 9023:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9024:     foreach my $context (@contexts) {
 9025:         foreach my $role (@{$roles{$context}}) {
 9026:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 9027:             foreach my $item (@fields) {
 9028:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 9029:                     $modifyhash{$context}{$role}{$item} = 1;
 9030:                 } else {
 9031:                     $modifyhash{$context}{$role}{$item} = 0;
 9032:                 }
 9033:             }
 9034:         }
 9035:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 9036:             foreach my $role (@{$roles{$context}}) {
 9037:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 9038:                     foreach my $field (@fields) {
 9039:                         if ($modifyhash{$context}{$role}{$field} ne 
 9040:                                 $curr_usermodification{$context}{$role}{$field}) {
 9041:                             push(@{$changes{$context}},$role);
 9042:                             last;
 9043:                         }
 9044:                     }
 9045:                 }
 9046:             }
 9047:         } else {
 9048:             foreach my $context (@contexts) {
 9049:                 foreach my $role (@{$roles{$context}}) {
 9050:                     push(@{$changes{$context}},$role);
 9051:                 }
 9052:             }
 9053:         }
 9054:     }
 9055:     my %usermodification_hash =  (
 9056:                                    usermodification => \%modifyhash,
 9057:                                  );
 9058:     my $putresult = &Apache::lonnet::put_dom('configuration',
 9059:                                              \%usermodification_hash,$dom);
 9060:     if ($putresult eq 'ok') {
 9061:         if (keys(%changes) > 0) {
 9062:             $resulttext = &mt('Changes made: ').'<ul>';
 9063:             foreach my $context (@contexts) {
 9064:                 if (ref($changes{$context}) eq 'ARRAY') {
 9065:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 9066:                     if (ref($changes{$context}) eq 'ARRAY') {
 9067:                         foreach my $role (@{$changes{$context}}) {
 9068:                             my $rolename;
 9069:                             if ($role eq 'cr') {
 9070:                                 $rolename = &mt('Custom');
 9071:                             } else {
 9072:                                 $rolename = &Apache::lonnet::plaintext($role);
 9073:                             }
 9074:                             my @modifiable;
 9075:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 9076:                             foreach my $field (@fields) {
 9077:                                 if ($modifyhash{$context}{$role}{$field}) {
 9078:                                     push(@modifiable,$fieldtitles{$field});
 9079:                                 }
 9080:                             }
 9081:                             if (@modifiable > 0) {
 9082:                                 $resulttext .= join(', ',@modifiable);
 9083:                             } else {
 9084:                                 $resulttext .= &mt('none'); 
 9085:                             }
 9086:                             $resulttext .= '</li>';
 9087:                         }
 9088:                         $resulttext .= '</ul></li>';
 9089:                     }
 9090:                 }
 9091:             }
 9092:             $resulttext .= '</ul>';
 9093:         } else {
 9094:             $resulttext = &mt('No changes made to user modification settings');
 9095:         }
 9096:     } else {
 9097:         $resulttext = '<span class="LC_error">'.
 9098:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9099:     }
 9100:     return $resulttext;
 9101: }
 9102: 
 9103: sub modify_defaults {
 9104:     my ($dom,$lastactref,%domconfig) = @_;
 9105:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 9106:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9107:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 9108:     my @authtypes = ('internal','krb4','krb5','localauth');
 9109:     foreach my $item (@items) {
 9110:         $newvalues{$item} = $env{'form.'.$item};
 9111:         if ($item eq 'auth_def') {
 9112:             if ($newvalues{$item} ne '') {
 9113:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 9114:                     push(@errors,$item);
 9115:                 }
 9116:             }
 9117:         } elsif ($item eq 'lang_def') {
 9118:             if ($newvalues{$item} ne '') {
 9119:                 if ($newvalues{$item} =~ /^(\w+)/) {
 9120:                     my $langcode = $1;
 9121:                     if ($langcode ne 'x_chef') {
 9122:                         if (code2language($langcode) eq '') {
 9123:                             push(@errors,$item);
 9124:                         }
 9125:                     }
 9126:                 } else {
 9127:                     push(@errors,$item);
 9128:                 }
 9129:             }
 9130:         } elsif ($item eq 'timezone_def') {
 9131:             if ($newvalues{$item} ne '') {
 9132:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 9133:                     push(@errors,$item);   
 9134:                 }
 9135:             }
 9136:         } elsif ($item eq 'datelocale_def') {
 9137:             if ($newvalues{$item} ne '') {
 9138:                 my @datelocale_ids = DateTime::Locale->ids();
 9139:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 9140:                     push(@errors,$item);
 9141:                 }
 9142:             }
 9143:         } elsif ($item eq 'portal_def') {
 9144:             if ($newvalues{$item} ne '') {
 9145:                 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])\/?$/) {
 9146:                     push(@errors,$item);
 9147:                 }
 9148:             }
 9149:         }
 9150:         if (grep(/^\Q$item\E$/,@errors)) {
 9151:             $newvalues{$item} = $domdefaults{$item};
 9152:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 9153:             $changes{$item} = 1;
 9154:         }
 9155:         $domdefaults{$item} = $newvalues{$item};
 9156:     }
 9157:     my %defaults_hash = (
 9158:                          defaults => \%newvalues,
 9159:                         );
 9160:     my $title = &defaults_titles();
 9161: 
 9162:     my $currinststatus;
 9163:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 9164:         $currinststatus = $domconfig{'inststatus'};
 9165:     } else {
 9166:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9167:         $currinststatus = {
 9168:                              inststatustypes => $usertypes,
 9169:                              inststatusorder => $types,
 9170:                              inststatusguest => [],
 9171:                           };
 9172:     }
 9173:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
 9174:     my @allpos;
 9175:     my %guests;
 9176:     my %alltypes;
 9177:     my ($currtitles,$currguests,$currorder);
 9178:     if (ref($currinststatus) eq 'HASH') {
 9179:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
 9180:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
 9181:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
 9182:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
 9183:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
 9184:                     }
 9185:                 }
 9186:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
 9187:                     my $position = $env{'form.inststatus_pos_'.$type};
 9188:                     $position =~ s/\D+//g;
 9189:                     $allpos[$position] = $type;
 9190:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
 9191:                     $alltypes{$type} =~ s/`//g;
 9192:                     if ($env{'form.inststatus_guest_'.$type}) {
 9193:                         $guests{$type} = 1;
 9194:                     }
 9195:                 }
 9196:             }
 9197:             if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
 9198:                 $currguests = join(',',@{$currinststatus->{'inststatusguest'}});
 9199:             }
 9200:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
 9201:             $currtitles =~ s/,$//;
 9202:         }
 9203:     }
 9204:     if ($env{'form.addinststatus'}) {
 9205:         my $newtype = $env{'form.addinststatus'};
 9206:         $newtype =~ s/\W//g;
 9207:         unless (exists($alltypes{$newtype})) {
 9208:             if ($env{'form.addinststatus_guest'}) {
 9209:                 $guests{$newtype} = 1;
 9210:             }
 9211:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
 9212:             $alltypes{$newtype} =~ s/`//g; 
 9213:             my $position = $env{'form.addinststatus_pos'};
 9214:             $position =~ s/\D+//g;
 9215:             if ($position ne '') {
 9216:                 $allpos[$position] = $newtype;
 9217:             }
 9218:         }
 9219:     }
 9220:     my (@orderedstatus,@orderedguests);
 9221:     foreach my $type (@allpos) {
 9222:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
 9223:             push(@orderedstatus,$type);
 9224:             if ($guests{$type}) {
 9225:                 push(@orderedguests,$type);
 9226:             }
 9227:         }
 9228:     }
 9229:     foreach my $type (keys(%alltypes)) {
 9230:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
 9231:             delete($alltypes{$type});
 9232:         }
 9233:     }
 9234:     $defaults_hash{'inststatus'} = {
 9235:                                      inststatustypes => \%alltypes,
 9236:                                      inststatusorder => \@orderedstatus,
 9237:                                      inststatusguest => \@orderedguests,
 9238:                                    };
 9239:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
 9240:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
 9241:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
 9242:         }
 9243:     }
 9244:     if ($currorder ne join(',',@orderedstatus)) {
 9245:         $changes{'inststatus'}{'inststatusorder'} = 1;
 9246:     }
 9247:     if ($currguests ne join(',',@orderedguests)) {
 9248:         $changes{'inststatus'}{'inststatusguest'} = 1;
 9249:     }
 9250:     my $newtitles;
 9251:     foreach my $item (@orderedstatus) {
 9252:         $newtitles .= $alltypes{$item}.',';
 9253:     }
 9254:     $newtitles =~ s/,$//;
 9255:     if ($currtitles ne $newtitles) {
 9256:         $changes{'inststatus'}{'inststatustypes'} = 1;
 9257:     }
 9258:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 9259:                                              $dom);
 9260:     if ($putresult eq 'ok') {
 9261:         if (keys(%changes) > 0) {
 9262:             $resulttext = &mt('Changes made:').'<ul>';
 9263:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
 9264:             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";
 9265:             foreach my $item (sort(keys(%changes))) {
 9266:                 if ($item eq 'inststatus') {
 9267:                     if (ref($changes{'inststatus'}) eq 'HASH') {
 9268:                         if (($changes{'inststatus'}{'inststatustypes'}) || $changes{'inststatus'}{'inststatusorder'}) {
 9269:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
 9270:                             foreach my $type (@orderedstatus) { 
 9271:                                 $resulttext .= $alltypes{$type}.', ';
 9272:                             }
 9273:                             $resulttext =~ s/, $//;
 9274:                             $resulttext .= '</li>';
 9275:                         }
 9276:                         if ($changes{'inststatus'}{'inststatusguest'}) {
 9277:                             $resulttext .= '<li>'; 
 9278:                             if (@orderedguests) {
 9279:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to:').' ';
 9280:                                 foreach my $type (@orderedguests) {
 9281:                                     $resulttext .= $alltypes{$type}.', ';
 9282:                                 }
 9283:                                 $resulttext =~ s/, $//;
 9284:                             } else {
 9285:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to none.');
 9286:                             }
 9287:                             $resulttext .= '</li>';
 9288:                         }
 9289:                     }
 9290:                 } else {
 9291:                     my $value = $env{'form.'.$item};
 9292:                     if ($value eq '') {
 9293:                         $value = &mt('none');
 9294:                     } elsif ($item eq 'auth_def') {
 9295:                         my %authnames = &authtype_names();
 9296:                         my %shortauth = (
 9297:                                           internal   => 'int',
 9298:                                           krb4       => 'krb4',
 9299:                                           krb5       => 'krb5',
 9300:                                           localauth  => 'loc',
 9301:                         );
 9302:                         $value = $authnames{$shortauth{$value}};
 9303:                     }
 9304:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 9305:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
 9306:                 }
 9307:             }
 9308:             $resulttext .= '</ul>';
 9309:             $mailmsgtext .= "\n";
 9310:             my $cachetime = 24*60*60;
 9311:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9312:             if (ref($lastactref) eq 'HASH') {
 9313:                 $lastactref->{'domdefaults'} = 1;
 9314:             }
 9315:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 9316:                 my $notify = 1;
 9317:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
 9318:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
 9319:                         $notify = 0;
 9320:                     }
 9321:                 }
 9322:                 if ($notify) {
 9323:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
 9324:                                                "LON-CAPA Domain Settings Change - $dom",
 9325:                                                $mailmsgtext);
 9326:                 }
 9327:             }
 9328:         } else {
 9329:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 9330:         }
 9331:     } else {
 9332:         $resulttext = '<span class="LC_error">'.
 9333:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9334:     }
 9335:     if (@errors > 0) {
 9336:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 9337:         foreach my $item (@errors) {
 9338:             $resulttext .= ' "'.$title->{$item}.'",';
 9339:         }
 9340:         $resulttext =~ s/,$//;
 9341:     }
 9342:     return $resulttext;
 9343: }
 9344: 
 9345: sub modify_scantron {
 9346:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 9347:     my ($resulttext,%confhash,%changes,$errors);
 9348:     my $custom = 'custom.tab';
 9349:     my $default = 'default.tab';
 9350:     my $servadm = $r->dir_config('lonAdmEMail');
 9351:     my ($configuserok,$author_ok,$switchserver) = 
 9352:         &config_check($dom,$confname,$servadm);
 9353:     if ($env{'form.scantronformat.filename'} ne '') {
 9354:         my $error;
 9355:         if ($configuserok eq 'ok') {
 9356:             if ($switchserver) {
 9357:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 9358:             } else {
 9359:                 if ($author_ok eq 'ok') {
 9360:                     my ($result,$scantronurl) =
 9361:                         &publishlogo($r,'upload','scantronformat',$dom,
 9362:                                      $confname,'scantron','','',$custom);
 9363:                     if ($result eq 'ok') {
 9364:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 9365:                         $changes{'scantronformat'} = 1;
 9366:                     } else {
 9367:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 9368:                     }
 9369:                 } else {
 9370:                     $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);
 9371:                 }
 9372:             }
 9373:         } else {
 9374:             $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);
 9375:         }
 9376:         if ($error) {
 9377:             &Apache::lonnet::logthis($error);
 9378:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9379:         }
 9380:     }
 9381:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 9382:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 9383:             if ($env{'form.scantronformat_del'}) {
 9384:                 $confhash{'scantron'}{'scantronformat'} = '';
 9385:                 $changes{'scantronformat'} = 1;
 9386:             }
 9387:         }
 9388:     }
 9389:     if (keys(%confhash) > 0) {
 9390:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 9391:                                                  $dom);
 9392:         if ($putresult eq 'ok') {
 9393:             if (keys(%changes) > 0) {
 9394:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 9395:                     $resulttext = &mt('Changes made:').'<ul>';
 9396:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 9397:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 9398:                     } else {
 9399:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 9400:                     }
 9401:                     $resulttext .= '</ul>';
 9402:                 } else {
 9403:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 9404:                 }
 9405:                 $resulttext .= '</ul>';
 9406:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 9407:                 if (ref($lastactref) eq 'HASH') {
 9408:                     $lastactref->{'domainconfig'} = 1;
 9409:                 }
 9410:             } else {
 9411:                 $resulttext = &mt('No changes made to bubblesheet format file');
 9412:             }
 9413:         } else {
 9414:             $resulttext = '<span class="LC_error">'.
 9415:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 9416:         }
 9417:     } else {
 9418:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 9419:     }
 9420:     if ($errors) {
 9421:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 9422:                        $errors.'</ul>';
 9423:     }
 9424:     return $resulttext;
 9425: }
 9426: 
 9427: sub modify_coursecategories {
 9428:     my ($dom,$lastactref,%domconfig) = @_;
 9429:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 9430:         $cathash);
 9431:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 9432:     my @catitems = ('unauth','auth');
 9433:     my @cattypes = ('std','domonly','codesrch','none');
 9434:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 9435:         $cathash = $domconfig{'coursecategories'}{'cats'};
 9436:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 9437:             $changes{'togglecats'} = 1;
 9438:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 9439:         }
 9440:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 9441:             $changes{'categorize'} = 1;
 9442:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 9443:         }
 9444:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 9445:             $changes{'togglecatscomm'} = 1;
 9446:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 9447:         }
 9448:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 9449:             $changes{'categorizecomm'} = 1;
 9450:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 9451:         }
 9452:         foreach my $item (@catitems) {
 9453:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
 9454:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
 9455:                     $changes{$item} = 1;
 9456:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
 9457:                 }
 9458:             }
 9459:         }
 9460:     } else {
 9461:         $changes{'togglecats'} = 1;
 9462:         $changes{'categorize'} = 1;
 9463:         $changes{'togglecatscomm'} = 1;
 9464:         $changes{'categorizecomm'} = 1;
 9465:         $domconfig{'coursecategories'} = {
 9466:                                              togglecats => $env{'form.togglecats'},
 9467:                                              categorize => $env{'form.categorize'},
 9468:                                              togglecatscomm => $env{'form.togglecatscomm'},
 9469:                                              categorizecomm => $env{'form.categorizecomm'},
 9470:                                          };
 9471:         foreach my $item (@catitems) {
 9472:             if ($env{'form.coursecat_'.$item} ne 'std') {
 9473:                 $changes{$item} = 1;
 9474:             }
 9475:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
 9476:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
 9477:             }
 9478:         }
 9479:     }
 9480:     if (ref($cathash) eq 'HASH') {
 9481:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 9482:             push (@deletecategory,'instcode::0');
 9483:         }
 9484:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 9485:             push(@deletecategory,'communities::0');
 9486:         }
 9487:     }
 9488:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 9489:     if (ref($cathash) eq 'HASH') {
 9490:         if (@deletecategory > 0) {
 9491:             #FIXME Need to remove category from all courses using a deleted category 
 9492:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 9493:             foreach my $item (@deletecategory) {
 9494:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 9495:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 9496:                     $deletions{$item} = 1;
 9497:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 9498:                 }
 9499:             }
 9500:         }
 9501:         foreach my $item (keys(%{$cathash})) {
 9502:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 9503:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 9504:                 $reorderings{$item} = 1;
 9505:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 9506:             }
 9507:             if ($env{'form.addcategory_name_'.$item} ne '') {
 9508:                 my $newcat = $env{'form.addcategory_name_'.$item};
 9509:                 my $newdepth = $depth+1;
 9510:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 9511:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 9512:                 $adds{$newitem} = 1; 
 9513:             }
 9514:             if ($env{'form.subcat_'.$item} ne '') {
 9515:                 my $newcat = $env{'form.subcat_'.$item};
 9516:                 my $newdepth = $depth+1;
 9517:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 9518:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 9519:                 $adds{$newitem} = 1;
 9520:             }
 9521:         }
 9522:     }
 9523:     if ($env{'form.instcode'} eq '1') {
 9524:         if (ref($cathash) eq 'HASH') {
 9525:             my $newitem = 'instcode::0';
 9526:             if ($cathash->{$newitem} eq '') {  
 9527:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 9528:                 $adds{$newitem} = 1;
 9529:             }
 9530:         } else {
 9531:             my $newitem = 'instcode::0';
 9532:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 9533:             $adds{$newitem} = 1;
 9534:         }
 9535:     }
 9536:     if ($env{'form.communities'} eq '1') {
 9537:         if (ref($cathash) eq 'HASH') {
 9538:             my $newitem = 'communities::0';
 9539:             if ($cathash->{$newitem} eq '') {
 9540:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 9541:                 $adds{$newitem} = 1;
 9542:             }
 9543:         } else {
 9544:             my $newitem = 'communities::0';
 9545:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 9546:             $adds{$newitem} = 1;
 9547:         }
 9548:     }
 9549:     if ($env{'form.addcategory_name'} ne '') {
 9550:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 9551:             ($env{'form.addcategory_name'} ne 'communities')) {
 9552:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 9553:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 9554:             $adds{$newitem} = 1;
 9555:         }
 9556:     }
 9557:     my $putresult;
 9558:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 9559:         if (keys(%deletions) > 0) {
 9560:             foreach my $key (keys(%deletions)) {
 9561:                 if ($predelallitems{$key} ne '') {
 9562:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 9563:                 }
 9564:             }
 9565:         }
 9566:         my (@chkcats,@chktrails,%chkallitems);
 9567:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 9568:         if (ref($chkcats[0]) eq 'ARRAY') {
 9569:             my $depth = 0;
 9570:             my $chg = 0;
 9571:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 9572:                 my $name = $chkcats[0][$i];
 9573:                 my $item;
 9574:                 if ($name eq '') {
 9575:                     $chg ++;
 9576:                 } else {
 9577:                     $item = &escape($name).'::0';
 9578:                     if ($chg) {
 9579:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 9580:                     }
 9581:                     $depth ++; 
 9582:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 9583:                     $depth --;
 9584:                 }
 9585:             }
 9586:         }
 9587:     }
 9588:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 9589:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 9590:         if ($putresult eq 'ok') {
 9591:             my %title = (
 9592:                          togglecats     => 'Show/Hide a course in catalog',
 9593:                          categorize     => 'Assign a category to a course',
 9594:                          togglecatscomm => 'Show/Hide a community in catalog',
 9595:                          categorizecomm => 'Assign a category to a community',
 9596:                         );
 9597:             my %level = (
 9598:                          dom  => 'set in Domain ("Modify Course/Community")',
 9599:                          crs  => 'set in Course ("Course Configuration")',
 9600:                          comm => 'set in Community ("Community Configuration")',
 9601:                          none     => 'No catalog',
 9602:                          std      => 'Standard catalog',
 9603:                          domonly  => 'Domain-only catalog',
 9604:                          codesrch => 'Code search form',
 9605:                         );
 9606:             $resulttext = &mt('Changes made:').'<ul>';
 9607:             if ($changes{'togglecats'}) {
 9608:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 9609:             }
 9610:             if ($changes{'categorize'}) {
 9611:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 9612:             }
 9613:             if ($changes{'togglecatscomm'}) {
 9614:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 9615:             }
 9616:             if ($changes{'categorizecomm'}) {
 9617:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 9618:             }
 9619:             if ($changes{'unauth'}) {
 9620:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
 9621:             }
 9622:             if ($changes{'auth'}) {
 9623:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
 9624:             }
 9625:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 9626:                 my $cathash;
 9627:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 9628:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 9629:                 } else {
 9630:                     $cathash = {};
 9631:                 } 
 9632:                 my (@cats,@trails,%allitems);
 9633:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 9634:                 if (keys(%deletions) > 0) {
 9635:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 9636:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 9637:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 9638:                     }
 9639:                     $resulttext .= '</ul></li>';
 9640:                 }
 9641:                 if (keys(%reorderings) > 0) {
 9642:                     my %sort_by_trail;
 9643:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 9644:                     foreach my $key (keys(%reorderings)) {
 9645:                         if ($allitems{$key} ne '') {
 9646:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 9647:                         }
 9648:                     }
 9649:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 9650:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 9651:                     }
 9652:                     $resulttext .= '</ul></li>';
 9653:                 }
 9654:                 if (keys(%adds) > 0) {
 9655:                     my %sort_by_trail;
 9656:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 9657:                     foreach my $key (keys(%adds)) {
 9658:                         if ($allitems{$key} ne '') {
 9659:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 9660:                         }
 9661:                     }
 9662:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 9663:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 9664:                     }
 9665:                     $resulttext .= '</ul></li>';
 9666:                 }
 9667:             }
 9668:             $resulttext .= '</ul>';
 9669:             if ($changes{'unauth'} || $changes{'auth'}) {
 9670:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 9671:                 if (ref($lastactref) eq 'HASH') {
 9672:                     $lastactref->{'domainconfig'} = 1;
 9673:                 }
 9674:             }
 9675:         } else {
 9676:             $resulttext = '<span class="LC_error">'.
 9677:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 9678:         }
 9679:     } else {
 9680:         $resulttext = &mt('No changes made to course and community categories');
 9681:     }
 9682:     return $resulttext;
 9683: }
 9684: 
 9685: sub modify_serverstatuses {
 9686:     my ($dom,%domconfig) = @_;
 9687:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 9688:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 9689:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 9690:     }
 9691:     my @pages = &serverstatus_pages();
 9692:     foreach my $type (@pages) {
 9693:         $newserverstatus{$type}{'namedusers'} = '';
 9694:         $newserverstatus{$type}{'machines'} = '';
 9695:         if (defined($env{'form.'.$type.'_namedusers'})) {
 9696:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 9697:             my @okusers;
 9698:             foreach my $user (@users) {
 9699:                 my ($uname,$udom) = split(/:/,$user);
 9700:                 if (($udom =~ /^$match_domain$/) &&   
 9701:                     (&Apache::lonnet::domain($udom)) &&
 9702:                     ($uname =~ /^$match_username$/)) {
 9703:                     if (!grep(/^\Q$user\E/,@okusers)) {
 9704:                         push(@okusers,$user);
 9705:                     }
 9706:                 }
 9707:             }
 9708:             if (@okusers > 0) {
 9709:                  @okusers = sort(@okusers);
 9710:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 9711:             }
 9712:         }
 9713:         if (defined($env{'form.'.$type.'_machines'})) {
 9714:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 9715:             my @okmachines;
 9716:             foreach my $ip (@machines) {
 9717:                 my @parts = split(/\./,$ip);
 9718:                 next if (@parts < 4);
 9719:                 my $badip = 0;
 9720:                 for (my $i=0; $i<4; $i++) {
 9721:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 9722:                         $badip = 1;
 9723:                         last;
 9724:                     }
 9725:                 }
 9726:                 if (!$badip) {
 9727:                     push(@okmachines,$ip);     
 9728:                 }
 9729:             }
 9730:             @okmachines = sort(@okmachines);
 9731:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 9732:         }
 9733:     }
 9734:     my %serverstatushash =  (
 9735:                                 serverstatuses => \%newserverstatus,
 9736:                             );
 9737:     foreach my $type (@pages) {
 9738:         foreach my $setting ('namedusers','machines') {
 9739:             my (@current,@new);
 9740:             if (ref($currserverstatus{$type}) eq 'HASH') {
 9741:                 if ($currserverstatus{$type}{$setting} ne '') { 
 9742:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 9743:                 }
 9744:             }
 9745:             if ($newserverstatus{$type}{$setting} ne '') {
 9746:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 9747:             }
 9748:             if (@current > 0) {
 9749:                 if (@new > 0) {
 9750:                     foreach my $item (@current) {
 9751:                         if (!grep(/^\Q$item\E$/,@new)) {
 9752:                             $changes{$type}{$setting} = 1;
 9753:                             last;
 9754:                         }
 9755:                     }
 9756:                     foreach my $item (@new) {
 9757:                         if (!grep(/^\Q$item\E$/,@current)) {
 9758:                             $changes{$type}{$setting} = 1;
 9759:                             last;
 9760:                         }
 9761:                     }
 9762:                 } else {
 9763:                     $changes{$type}{$setting} = 1;
 9764:                 }
 9765:             } elsif (@new > 0) {
 9766:                 $changes{$type}{$setting} = 1;
 9767:             }
 9768:         }
 9769:     }
 9770:     if (keys(%changes) > 0) {
 9771:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 9772:         my $putresult = &Apache::lonnet::put_dom('configuration',
 9773:                                                  \%serverstatushash,$dom);
 9774:         if ($putresult eq 'ok') {
 9775:             $resulttext .= &mt('Changes made:').'<ul>';
 9776:             foreach my $type (@pages) {
 9777:                 if (ref($changes{$type}) eq 'HASH') {
 9778:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 9779:                     if ($changes{$type}{'namedusers'}) {
 9780:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 9781:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 9782:                         } else {
 9783:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 9784:                         }
 9785:                     }
 9786:                     if ($changes{$type}{'machines'}) {
 9787:                         if ($newserverstatus{$type}{'machines'} eq '') {
 9788:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 9789:                         } else {
 9790:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 9791:                         }
 9792: 
 9793:                     }
 9794:                     $resulttext .= '</ul></li>';
 9795:                 }
 9796:             }
 9797:             $resulttext .= '</ul>';
 9798:         } else {
 9799:             $resulttext = '<span class="LC_error">'.
 9800:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 9801: 
 9802:         }
 9803:     } else {
 9804:         $resulttext = &mt('No changes made to access to server status pages');
 9805:     }
 9806:     return $resulttext;
 9807: }
 9808: 
 9809: sub modify_helpsettings {
 9810:     my ($r,$dom,$confname,%domconfig) = @_;
 9811:     my ($resulttext,$errors,%changes,%helphash);
 9812:     my %defaultchecked = ('submitbugs' => 'on');
 9813:     my @offon = ('off','on');
 9814:     my @toggles = ('submitbugs');
 9815:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 9816:         foreach my $item (@toggles) {
 9817:             if ($defaultchecked{$item} eq 'on') { 
 9818:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 9819:                     if ($env{'form.'.$item} eq '0') {
 9820:                         $changes{$item} = 1;
 9821:                     }
 9822:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 9823:                     $changes{$item} = 1;
 9824:                 }
 9825:             } elsif ($defaultchecked{$item} eq 'off') {
 9826:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 9827:                     if ($env{'form.'.$item} eq '1') {
 9828:                         $changes{$item} = 1;
 9829:                     }
 9830:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 9831:                     $changes{$item} = 1;
 9832:                 }
 9833:             }
 9834:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
 9835:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
 9836:             }
 9837:         }
 9838:     }
 9839:     my $putresult;
 9840:     if (keys(%changes) > 0) {
 9841:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
 9842:         if ($putresult eq 'ok') {
 9843:             $resulttext = &mt('Changes made:').'<ul>';
 9844:             foreach my $item (sort(keys(%changes))) {
 9845:                 if ($item eq 'submitbugs') {
 9846:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
 9847:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 9848:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
 9849:                 }
 9850:             }
 9851:             $resulttext .= '</ul>';
 9852:         } else {
 9853:             $resulttext = &mt('No changes made to help settings');
 9854:             $errors .= '<li><span class="LC_error">'.
 9855:                        &mt('An error occurred storing the settings: [_1]',
 9856:                            $putresult).'</span></li>';
 9857:         }
 9858:     }
 9859:     if ($errors) {
 9860:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 9861:                        $errors.'</ul>';
 9862:     }
 9863:     return $resulttext;
 9864: }
 9865: 
 9866: sub modify_coursedefaults {
 9867:     my ($dom,$lastactref,%domconfig) = @_;
 9868:     my ($resulttext,$errors,%changes,%defaultshash);
 9869:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 9870:     my @toggles = ('canuse_pdfforms');
 9871:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
 9872:                    'uploadquota_community','uploadquota_textbook');
 9873:     my @types = ('official','unofficial','community','textbook');
 9874:     my %staticdefaults = (
 9875:                            anonsurvey_threshold => 10,
 9876:                            uploadquota          => 500,
 9877:                          );
 9878: 
 9879:     $defaultshash{'coursedefaults'} = {};
 9880: 
 9881:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
 9882:         if ($domconfig{'coursedefaults'} eq '') {
 9883:             $domconfig{'coursedefaults'} = {};
 9884:         }
 9885:     }
 9886: 
 9887:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 9888:         foreach my $item (@toggles) {
 9889:             if ($defaultchecked{$item} eq 'on') {
 9890:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 9891:                     ($env{'form.'.$item} eq '0')) {
 9892:                     $changes{$item} = 1;
 9893:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 9894:                     $changes{$item} = 1;
 9895:                 }
 9896:             } elsif ($defaultchecked{$item} eq 'off') {
 9897:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 9898:                     ($env{'form.'.$item} eq '1')) {
 9899:                     $changes{$item} = 1;
 9900:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 9901:                     $changes{$item} = 1;
 9902:                 }
 9903:             }
 9904:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
 9905:         }
 9906:         foreach my $item (@numbers) {
 9907:             my ($currdef,$newdef);
 9908:             $newdef = $env{'form.'.$item};
 9909:             if ($item eq 'anonsurvey_threshold') {
 9910:                 $currdef = $domconfig{'coursedefaults'}{$item};
 9911:                 $newdef =~ s/\D//g;
 9912:                 if ($newdef eq '' || $newdef < 1) {
 9913:                     $newdef = 1;
 9914:                 }
 9915:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
 9916:             } else {
 9917:                 my ($type) = ($item =~ /^\Quploadquota_\E(\w+)$/);
 9918:                 if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 9919:                     $currdef = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
 9920:                 }
 9921:                 $newdef =~ s/[^\w.\-]//g;
 9922:                 $defaultshash{'coursedefaults'}{'uploadquota'}{$type} = $newdef;
 9923:             }
 9924:             if ($currdef ne $newdef) {
 9925:                 my $staticdef;
 9926:                 if ($item eq 'anonsurvey_threshold') {
 9927:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
 9928:                         $changes{$item} = 1;
 9929:                     }
 9930:                 } else {
 9931:                     unless (($currdef eq '') && ($newdef == $staticdefaults{'uploadquota'})) {
 9932:                         $changes{'uploadquota'} = 1;
 9933:                     }
 9934:                 }
 9935:             }
 9936:         }
 9937: 
 9938:         my $officialcreds = $env{'form.official_credits'};
 9939:         $officialcreds =~ s/[^\d.]+//g;
 9940:         my $unofficialcreds = $env{'form.unofficial_credits'};
 9941:         $unofficialcreds =~ s/[^\d.]+//g;
 9942:         my $textbookcreds = $env{'form.textbook_credits'};
 9943:         $textbookcreds =~ s/[^\d.]+//g;
 9944:         if (ref($domconfig{'coursedefaults'}{'coursecredits'} ne 'HASH') &&
 9945:                 ($env{'form.coursecredits'} eq '1')) {
 9946:                 $changes{'coursecredits'} = 1;
 9947:         } else {
 9948:             if (($domconfig{'coursedefaults'}{'coursecredits'}{'official'} ne $officialcreds)  ||
 9949:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'unofficial'} ne $unofficialcreds) ||
 9950:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'textbook'} ne $textbookcreds)) {
 9951:                 $changes{'coursecredits'} = 1;
 9952:             }
 9953:         }
 9954:         $defaultshash{'coursedefaults'}{'coursecredits'} = {
 9955:             official   => $officialcreds,
 9956:             unofficial => $unofficialcreds,
 9957:             textbook   => $textbookcreds,
 9958:         }
 9959:     }
 9960:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 9961:                                              $dom);
 9962:     if ($putresult eq 'ok') {
 9963:         if (keys(%changes) > 0) {
 9964:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9965:             if (($changes{'canuse_pdfforms'}) || ($changes{'coursecredits'}) || 
 9966:                 ($changes{'uploadquota'})) { 
 9967:                 if ($changes{'canuse_pdfforms'}) {
 9968:                     $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
 9969:                 }
 9970:                 if ($changes{'coursecredits'}) {
 9971:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 9972:                         $domdefaults{'officialcredits'} =
 9973:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'official'};
 9974:                         $domdefaults{'unofficialcredits'} =
 9975:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'};
 9976:                         $domdefaults{'textbookcredits'} =
 9977:                             $domdefaults{'coursedefaults'}{'coursecredits'}{'textbook'};
 9978:                     }
 9979:                 }
 9980:                 if ($changes{'uploadquota'}) {
 9981:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 9982:                         foreach my $type (@types) {
 9983:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
 9984:                         }
 9985:                     }
 9986:                 }
 9987:                 my $cachetime = 24*60*60;
 9988:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9989:                 if (ref($lastactref) eq 'HASH') {
 9990:                     $lastactref->{'domdefaults'} = 1;
 9991:                 }
 9992:             }
 9993:             $resulttext = &mt('Changes made:').'<ul>';
 9994:             foreach my $item (sort(keys(%changes))) {
 9995:                 if ($item eq 'canuse_pdfforms') {
 9996:                     if ($env{'form.'.$item} eq '1') {
 9997:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
 9998:                     } else {
 9999:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
10000:                     }
10001:                 } elsif ($item eq 'anonsurvey_threshold') {
10002:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
10003:                 } elsif ($item eq 'uploadquota') {
10004:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
10005:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
10006:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
10007:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
10008:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
10009: 
10010:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
10011:                                        '</ul>'.
10012:                                        '</li>';
10013:                     } else {
10014:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
10015:                     }
10016:                 } elsif ($item eq 'coursecredits') {
10017:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
10018:                         if (($domdefaults{'officialcredits'} eq '') &&
10019:                             ($domdefaults{'unofficialcredits'} eq '') &&
10020:                             ($domdefaults{'textbookcredits'} eq '')) {
10021:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
10022:                         } else {
10023:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
10024:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
10025:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
10026:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
10027:                                            '</ul>'.
10028:                                            '</li>';
10029:                         }
10030:                     } else {
10031:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
10032:                     }
10033:                 }
10034:             }
10035:             $resulttext .= '</ul>';
10036:         } else {
10037:             $resulttext = &mt('No changes made to course defaults');
10038:         }
10039:     } else {
10040:         $resulttext = '<span class="LC_error">'.
10041:             &mt('An error occurred: [_1]',$putresult).'</span>';
10042:     }
10043:     return $resulttext;
10044: }
10045: 
10046: sub modify_selfenrollment {
10047:     my ($dom,$lastactref,%domconfig) = @_;
10048:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
10049:     my @types = ('official','unofficial','community','textbook');
10050:     my %titles = &tool_titles();
10051:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
10052:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
10053:     $ordered{'default'} = ['types','registered','approval','limit'];
10054: 
10055:     my (%roles,%shown,%toplevel);
10056:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
10057: 
10058:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
10059:         if ($domconfig{'selfenrollment'} eq '') {
10060:             $domconfig{'selfenrollment'} = {};
10061:         }
10062:     }
10063:     %toplevel = (
10064:                   admin      => 'Configuration Rights',
10065:                   default    => 'Default settings',
10066:                   validation => 'Validation of self-enrollment requests',
10067:                 );
10068:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
10069: 
10070:     if (ref($ordered{'admin'}) eq 'ARRAY') {
10071:         foreach my $item (@{$ordered{'admin'}}) {
10072:             foreach my $type (@types) {
10073:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
10074:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
10075:                 } else {
10076:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
10077:                 }
10078:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
10079:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
10080:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
10081:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
10082:                             push(@{$changes{'admin'}{$type}},$item);
10083:                         }
10084:                     } else {
10085:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
10086:                             push(@{$changes{'admin'}{$type}},$item);
10087:                         }
10088:                     }
10089:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
10090:                     push(@{$changes{'admin'}{$type}},$item);
10091:                 }
10092:             }
10093:         }
10094:     }
10095: 
10096:     foreach my $item (@{$ordered{'default'}}) {
10097:         foreach my $type (@types) {
10098:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
10099:             if ($item eq 'types') {
10100:                 unless (($value eq 'all') || ($value eq 'dom')) {
10101:                     $value = '';
10102:                 }
10103:             } elsif ($item eq 'registered') {
10104:                 unless ($value eq '1') {
10105:                     $value = 0;
10106:                 }
10107:             } elsif ($item eq 'approval') {
10108:                 unless ($value =~ /^[012]$/) {
10109:                     $value = 0;
10110:                 }
10111:             } else {
10112:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
10113:                     $value = 'none';
10114:                 }
10115:             }
10116:             $selfenrollhash{'default'}{$type}{$item} = $value;
10117:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
10118:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
10119:                     if ($selfenrollhash{'default'}{$type}{$item} ne
10120:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
10121:                          push(@{$changes{'default'}{$type}},$item);
10122:                     }
10123:                 } else {
10124:                     push(@{$changes{'default'}{$type}},$item);
10125:                 }
10126:             } else {
10127:                 push(@{$changes{'default'}{$type}},$item);
10128:             }
10129:             if ($item eq 'limit') {
10130:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
10131:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
10132:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
10133:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
10134:                     }
10135:                 } else {
10136:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
10137:                 }
10138:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
10139:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
10140:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
10141:                          push(@{$changes{'default'}{$type}},'cap');
10142:                     }
10143:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
10144:                     push(@{$changes{'default'}{$type}},'cap');
10145:                 }
10146:             }
10147:         }
10148:     }
10149: 
10150:     foreach my $item (@{$itemsref}) {
10151:         if ($item eq 'fields') {
10152:             my @changed;
10153:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
10154:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
10155:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
10156:             }
10157:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
10158:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
10159:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
10160:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
10161:                 } else {
10162:                     @changed = @{$selfenrollhash{'validation'}{$item}};
10163:                 }
10164:             } else {
10165:                 @changed = @{$selfenrollhash{'validation'}{$item}};
10166:             }
10167:             if (@changed) {
10168:                 if ($selfenrollhash{'validation'}{$item}) { 
10169:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
10170:                 } else {
10171:                     $changes{'validation'}{$item} = &mt('None');
10172:                 }
10173:             }
10174:         } else {
10175:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
10176:             if ($item eq 'markup') {
10177:                if ($env{'form.selfenroll_validation_'.$item}) {
10178:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
10179:                }
10180:             }
10181:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
10182:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
10183:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
10184:                 }
10185:             }
10186:         }
10187:     }
10188: 
10189:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
10190:                                              $dom);
10191:     if ($putresult eq 'ok') {
10192:         if (keys(%changes) > 0) {
10193:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10194:             $resulttext = &mt('Changes made:').'<ul>';
10195:             foreach my $key ('admin','default','validation') {
10196:                 if (ref($changes{$key}) eq 'HASH') {
10197:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
10198:                     if ($key eq 'validation') {
10199:                         foreach my $item (@{$itemsref}) {
10200:                             if (exists($changes{$key}{$item})) {
10201:                                 if ($item eq 'markup') {
10202:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
10203:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
10204:                                 } else {  
10205:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
10206:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
10207:                                 }
10208:                             }
10209:                         }
10210:                     } else {
10211:                         foreach my $type (@types) {
10212:                             if ($type eq 'community') {
10213:                                 $roles{'1'} = &mt('Community personnel');
10214:                             } else {
10215:                                 $roles{'1'} = &mt('Course personnel');
10216:                             }
10217:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
10218:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
10219:                                     if ($key eq 'admin') {
10220:                                         my @mgrdc = ();
10221:                                         if (ref($ordered{$key}) eq 'ARRAY') {
10222:                                             foreach my $item (@{$ordered{'admin'}}) {
10223:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
10224:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
10225:                                                         push(@mgrdc,$item);
10226:                                                     }
10227:                                                 }
10228:                                             }
10229:                                             if (@mgrdc) {
10230:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
10231:                                             } else {
10232:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
10233:                                             }
10234:                                         }
10235:                                     } else {
10236:                                         if (ref($ordered{$key}) eq 'ARRAY') {
10237:                                             foreach my $item (@{$ordered{$key}}) {
10238:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
10239:                                                     $domdefaults{$type.'selfenroll'.$item} =
10240:                                                         $selfenrollhash{$key}{$type}{$item};
10241:                                                 }
10242:                                             }
10243:                                         }
10244:                                     }
10245:                                 }
10246:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
10247:                                 foreach my $item (@{$ordered{$key}}) {
10248:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
10249:                                         $resulttext .= '<li>';
10250:                                         if ($key eq 'admin') {
10251:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
10252:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
10253:                                         } else {
10254:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
10255:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
10256:                                         }
10257:                                         $resulttext .= '</li>';
10258:                                     }
10259:                                 }
10260:                                 $resulttext .= '</ul></li>';
10261:                             }
10262:                         }
10263:                         $resulttext .= '</ul></li>'; 
10264:                     }
10265:                 }
10266:                 if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
10267:                     my $cachetime = 24*60*60;
10268:                     &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10269:                     if (ref($lastactref) eq 'HASH') {
10270:                         $lastactref->{'domdefaults'} = 1;
10271:                     }
10272:                 }
10273:             }
10274:             $resulttext .= '</ul>';
10275:         } else {
10276:             $resulttext = &mt('No changes made to self-enrollment settings');
10277:         }
10278:     } else {
10279:         $resulttext = '<span class="LC_error">'.
10280:             &mt('An error occurred: [_1]',$putresult).'</span>';
10281:     }
10282:     return $resulttext;
10283: }
10284: 
10285: sub modify_usersessions {
10286:     my ($dom,$lastactref,%domconfig) = @_;
10287:     my @hostingtypes = ('version','excludedomain','includedomain');
10288:     my @offloadtypes = ('primary','default');
10289:     my %types = (
10290:                   remote => \@hostingtypes,
10291:                   hosted => \@hostingtypes,
10292:                   spares => \@offloadtypes,
10293:                 );
10294:     my @prefixes = ('remote','hosted','spares');
10295:     my @lcversions = &Apache::lonnet::all_loncaparevs();
10296:     my (%by_ip,%by_location,@intdoms);
10297:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
10298:     my @locations = sort(keys(%by_location));
10299:     my (%defaultshash,%changes);
10300:     foreach my $prefix (@prefixes) {
10301:         $defaultshash{'usersessions'}{$prefix} = {};
10302:     }
10303:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10304:     my $resulttext;
10305:     my %iphost = &Apache::lonnet::get_iphost();
10306:     foreach my $prefix (@prefixes) {
10307:         next if ($prefix eq 'spares');
10308:         foreach my $type (@{$types{$prefix}}) {
10309:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
10310:             if ($type eq 'version') {
10311:                 my $value = $env{'form.'.$prefix.'_'.$type};
10312:                 my $okvalue;
10313:                 if ($value ne '') {
10314:                     if (grep(/^\Q$value\E$/,@lcversions)) {
10315:                         $okvalue = $value;
10316:                     }
10317:                 }
10318:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
10319:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
10320:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
10321:                             if ($inuse == 0) {
10322:                                 $changes{$prefix}{$type} = 1;
10323:                             } else {
10324:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
10325:                                     $changes{$prefix}{$type} = 1;
10326:                                 }
10327:                                 if ($okvalue ne '') {
10328:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10329:                                 } 
10330:                             }
10331:                         } else {
10332:                             if (($inuse == 1) && ($okvalue ne '')) {
10333:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10334:                                 $changes{$prefix}{$type} = 1;
10335:                             }
10336:                         }
10337:                     } else {
10338:                         if (($inuse == 1) && ($okvalue ne '')) {
10339:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10340:                             $changes{$prefix}{$type} = 1;
10341:                         }
10342:                     }
10343:                 } else {
10344:                     if (($inuse == 1) && ($okvalue ne '')) {
10345:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10346:                         $changes{$prefix}{$type} = 1;
10347:                     }
10348:                 }
10349:             } else {
10350:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
10351:                 my @okvals;
10352:                 foreach my $val (@vals) {
10353:                     if ($val =~ /:/) {
10354:                         my @items = split(/:/,$val);
10355:                         foreach my $item (@items) {
10356:                             if (ref($by_location{$item}) eq 'ARRAY') {
10357:                                 push(@okvals,$item);
10358:                             }
10359:                         }
10360:                     } else {
10361:                         if (ref($by_location{$val}) eq 'ARRAY') {
10362:                             push(@okvals,$val);
10363:                         }
10364:                     }
10365:                 }
10366:                 @okvals = sort(@okvals);
10367:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
10368:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
10369:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
10370:                             if ($inuse == 0) {
10371:                                 $changes{$prefix}{$type} = 1; 
10372:                             } else {
10373:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10374:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
10375:                                 if (@changed > 0) {
10376:                                     $changes{$prefix}{$type} = 1;
10377:                                 }
10378:                             }
10379:                         } else {
10380:                             if ($inuse == 1) {
10381:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10382:                                 $changes{$prefix}{$type} = 1;
10383:                             }
10384:                         } 
10385:                     } else {
10386:                         if ($inuse == 1) {
10387:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10388:                             $changes{$prefix}{$type} = 1;
10389:                         }
10390:                     }
10391:                 } else {
10392:                     if ($inuse == 1) {
10393:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10394:                         $changes{$prefix}{$type} = 1;
10395:                     }
10396:                 }
10397:             }
10398:         }
10399:     }
10400: 
10401:     my @alldoms = &Apache::lonnet::all_domains();
10402:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
10403:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
10404:     my $savespares;
10405: 
10406:     foreach my $lonhost (sort(keys(%servers))) {
10407:         my $serverhomeID =
10408:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
10409:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
10410:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
10411:         my %spareschg;
10412:         foreach my $type (@{$types{'spares'}}) {
10413:             my @okspares;
10414:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
10415:             foreach my $server (@checked) {
10416:                 if (&Apache::lonnet::hostname($server) ne '') {
10417:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
10418:                         unless (grep(/^\Q$server\E$/,@okspares)) {
10419:                             push(@okspares,$server);
10420:                         }
10421:                     }
10422:                 }
10423:             }
10424:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
10425:             my $newspare;
10426:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
10427:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
10428:                     $newspare = $new;
10429:                 }
10430:             }
10431:             my @spares;
10432:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
10433:                 @spares = sort(@okspares,$newspare);
10434:             } else {
10435:                 @spares = sort(@okspares);
10436:             }
10437:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
10438:             if (ref($spareid{$lonhost}) eq 'HASH') {
10439:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
10440:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
10441:                     if (@diffs > 0) {
10442:                         $spareschg{$type} = 1;
10443:                     }
10444:                 }
10445:             }
10446:         }
10447:         if (keys(%spareschg) > 0) {
10448:             $changes{'spares'}{$lonhost} = \%spareschg;
10449:         }
10450:     }
10451: 
10452:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
10453:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
10454:             if (ref($changes{'spares'}) eq 'HASH') {
10455:                 if (keys(%{$changes{'spares'}}) > 0) {
10456:                     $savespares = 1;
10457:                 }
10458:             }
10459:         } else {
10460:             $savespares = 1;
10461:         }
10462:     }
10463: 
10464:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
10465:     if ((keys(%changes) > 0) || ($savespares)) {
10466:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
10467:                                                  $dom);
10468:         if ($putresult eq 'ok') {
10469:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
10470:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
10471:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
10472:                 }
10473:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
10474:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
10475:                 }
10476:             }
10477:             my $cachetime = 24*60*60;
10478:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10479:             if (ref($lastactref) eq 'HASH') {
10480:                 $lastactref->{'domdefaults'} = 1;
10481:             }
10482:             if (keys(%changes) > 0) {
10483:                 my %lt = &usersession_titles();
10484:                 $resulttext = &mt('Changes made:').'<ul>';
10485:                 foreach my $prefix (@prefixes) {
10486:                     if (ref($changes{$prefix}) eq 'HASH') {
10487:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
10488:                         if ($prefix eq 'spares') {
10489:                             if (ref($changes{$prefix}) eq 'HASH') {
10490:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
10491:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
10492:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
10493:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
10494:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
10495:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
10496:                                         foreach my $type (@{$types{$prefix}}) {
10497:                                             if ($changes{$prefix}{$lonhost}{$type}) {
10498:                                                 my $offloadto = &mt('None');
10499:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
10500:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
10501:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
10502:                                                     }
10503:                                                 }
10504:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
10505:                                             }
10506:                                         }
10507:                                     }
10508:                                     $resulttext .= '</li>';
10509:                                 }
10510:                             }
10511:                         } else {
10512:                             foreach my $type (@{$types{$prefix}}) {
10513:                                 if (defined($changes{$prefix}{$type})) {
10514:                                     my $newvalue;
10515:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
10516:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
10517:                                             if ($type eq 'version') {
10518:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
10519:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
10520:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
10521:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
10522:                                                 }
10523:                                             }
10524:                                         }
10525:                                     }
10526:                                     if ($newvalue eq '') {
10527:                                         if ($type eq 'version') {
10528:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
10529:                                         } else {
10530:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
10531:                                         }
10532:                                     } else {
10533:                                         if ($type eq 'version') {
10534:                                             $newvalue .= ' '.&mt('(or later)'); 
10535:                                         }
10536:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
10537:                                     }
10538:                                 }
10539:                             }
10540:                         }
10541:                         $resulttext .= '</ul>';
10542:                     }
10543:                 }
10544:                 $resulttext .= '</ul>';
10545:             } else {
10546:                 $resulttext = $nochgmsg;
10547:             }
10548:         } else {
10549:             $resulttext = '<span class="LC_error">'.
10550:                           &mt('An error occurred: [_1]',$putresult).'</span>';
10551:         }
10552:     } else {
10553:         $resulttext = $nochgmsg;
10554:     }
10555:     return $resulttext;
10556: }
10557: 
10558: sub modify_loadbalancing {
10559:     my ($dom,%domconfig) = @_;
10560:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
10561:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
10562:     my ($othertitle,$usertypes,$types) =
10563:         &Apache::loncommon::sorted_inst_types($dom);
10564:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
10565:     my @sparestypes = ('primary','default');
10566:     my %typetitles = &sparestype_titles();
10567:     my $resulttext;
10568:     my (%currbalancer,%currtargets,%currrules,%existing);
10569:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
10570:         %existing = %{$domconfig{'loadbalancing'}};
10571:     }
10572:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
10573:                               \%currtargets,\%currrules);
10574:     my ($saveloadbalancing,%defaultshash,%changes);
10575:     my ($alltypes,$othertypes,$titles) =
10576:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
10577:     my %ruletitles = &offloadtype_text();
10578:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
10579:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
10580:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
10581:         if ($balancer eq '') {
10582:             next;
10583:         }
10584:         if (!exists($servers{$balancer})) {
10585:             if (exists($currbalancer{$balancer})) {
10586:                 push(@{$changes{'delete'}},$balancer);
10587:             }
10588:             next;
10589:         }
10590:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
10591:             push(@{$changes{'delete'}},$balancer);
10592:             next;
10593:         }
10594:         if (!exists($currbalancer{$balancer})) {
10595:             push(@{$changes{'add'}},$balancer);
10596:         }
10597:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
10598:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
10599:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
10600:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
10601:             $saveloadbalancing = 1;
10602:         }
10603:         foreach my $sparetype (@sparestypes) {
10604:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
10605:             my @offloadto;
10606:             foreach my $target (@targets) {
10607:                 if (($servers{$target}) && ($target ne $balancer)) {
10608:                     if ($sparetype eq 'default') {
10609:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
10610:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
10611:                         }
10612:                     }
10613:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
10614:                         push(@offloadto,$target);
10615:                     }
10616:                 }
10617:                 $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
10618:             }
10619:         }
10620:         if (ref($currtargets{$balancer}) eq 'HASH') {
10621:             foreach my $sparetype (@sparestypes) {
10622:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
10623:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
10624:                     if (@targetdiffs > 0) {
10625:                         $changes{'curr'}{$balancer}{'targets'} = 1;
10626:                     }
10627:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10628:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10629:                         $changes{'curr'}{$balancer}{'targets'} = 1;
10630:                     }
10631:                 }
10632:             }
10633:         } else {
10634:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
10635:                 foreach my $sparetype (@sparestypes) {
10636:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10637:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10638:                             $changes{'curr'}{$balancer}{'targets'} = 1;
10639:                         }
10640:                     }
10641:                 }
10642:             }
10643:         }
10644:         my $ishomedom;
10645:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
10646:             $ishomedom = 1;
10647:         }
10648:         if (ref($alltypes) eq 'ARRAY') {
10649:             foreach my $type (@{$alltypes}) {
10650:                 my $rule;
10651:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
10652:                          (!$ishomedom)) {
10653:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
10654:                 }
10655:                 if ($rule eq 'specific') {
10656:                     $rule = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
10657:                 }
10658:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
10659:                 if (ref($currrules{$balancer}) eq 'HASH') {
10660:                     if ($rule ne $currrules{$balancer}{$type}) {
10661:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
10662:                     }
10663:                 } elsif ($rule ne '') {
10664:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
10665:                 }
10666:             }
10667:         }
10668:     }
10669:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
10670:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
10671:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
10672:             $defaultshash{'loadbalancing'} = {};
10673:         }
10674:         my $putresult = &Apache::lonnet::put_dom('configuration',
10675:                                                  \%defaultshash,$dom);
10676:         if ($putresult eq 'ok') {
10677:             if (keys(%changes) > 0) {
10678:                 if (ref($changes{'delete'}) eq 'ARRAY') {
10679:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
10680:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
10681:                         my $cachekey = &escape('loadbalancing').':'.&escape($dom);
10682:                         &Apache::lonnet::remote_devalidate_cache($balancer,[$cachekey]);
10683:                     }
10684:                 }
10685:                 if (ref($changes{'add'}) eq 'ARRAY') {
10686:                     foreach my $balancer (sort(@{$changes{'add'}})) {
10687:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
10688:                     }
10689:                 }
10690:                 if (ref($changes{'curr'}) eq 'HASH') {
10691:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
10692:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
10693:                             if ($changes{'curr'}{$balancer}{'targets'}) {
10694:                                 my %offloadstr;
10695:                                 foreach my $sparetype (@sparestypes) {
10696:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10697:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10698:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
10699:                                         }
10700:                                     }
10701:                                 }
10702:                                 if (keys(%offloadstr) == 0) {
10703:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
10704:                                 } else {
10705:                                     my $showoffload;
10706:                                     foreach my $sparetype (@sparestypes) {
10707:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
10708:                                         if (defined($offloadstr{$sparetype})) {
10709:                                             $showoffload .= $offloadstr{$sparetype};
10710:                                         } else {
10711:                                             $showoffload .= &mt('None');
10712:                                         }
10713:                                         $showoffload .= ('&nbsp;'x3);
10714:                                     }
10715:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
10716:                                 }
10717:                             }
10718:                         }
10719:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
10720:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
10721:                                 foreach my $type (@{$alltypes}) {
10722:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
10723:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
10724:                                         my $balancetext;
10725:                                         if ($rule eq '') {
10726:                                             $balancetext =  $ruletitles{'default'};
10727:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
10728:                                                  ($rule eq 'balancer') || ($rule eq 'offloadedto')) {
10729:                                             $balancetext =  $ruletitles{$rule};
10730:                                         } else {
10731:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
10732:                                         }
10733:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
10734:                                     }
10735:                                 }
10736:                             }
10737:                         }
10738:                         my $cachekey = &escape('loadbalancing').':'.&escape($dom);
10739:                         &Apache::lonnet::remote_devalidate_cache($balancer,[$cachekey]);
10740:                     }
10741:                 }
10742:                 if ($resulttext ne '') {
10743:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
10744:                 } else {
10745:                     $resulttext = $nochgmsg;
10746:                 }
10747:             } else {
10748:                 $resulttext = $nochgmsg;
10749:             }
10750:         } else {
10751:             $resulttext = '<span class="LC_error">'.
10752:                           &mt('An error occurred: [_1]',$putresult).'</span>';
10753:         }
10754:     } else {
10755:         $resulttext = $nochgmsg;
10756:     }
10757:     return $resulttext;
10758: }
10759: 
10760: sub recurse_check {
10761:     my ($chkcats,$categories,$depth,$name) = @_;
10762:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
10763:         my $chg = 0;
10764:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
10765:             my $category = $chkcats->[$depth]{$name}[$j];
10766:             my $item;
10767:             if ($category eq '') {
10768:                 $chg ++;
10769:             } else {
10770:                 my $deeper = $depth + 1;
10771:                 $item = &escape($category).':'.&escape($name).':'.$depth;
10772:                 if ($chg) {
10773:                     $categories->{$item} -= $chg;
10774:                 }
10775:                 &recurse_check($chkcats,$categories,$deeper,$category);
10776:                 $deeper --;
10777:             }
10778:         }
10779:     }
10780:     return;
10781: }
10782: 
10783: sub recurse_cat_deletes {
10784:     my ($item,$coursecategories,$deletions) = @_;
10785:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
10786:     my $subdepth = $depth + 1;
10787:     if (ref($coursecategories) eq 'HASH') {
10788:         foreach my $subitem (keys(%{$coursecategories})) {
10789:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
10790:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
10791:                 delete($coursecategories->{$subitem});
10792:                 $deletions->{$subitem} = 1;
10793:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
10794:             }
10795:         }
10796:     }
10797:     return;
10798: }
10799: 
10800: sub get_active_dcs {
10801:     my ($dom) = @_;
10802:     my $now = time;
10803:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
10804:     my %domcoords;
10805:     my $numdcs = 0;
10806:     foreach my $server (keys(%dompersonnel)) {
10807:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
10808:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
10809:             $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
10810:         }
10811:     }
10812:     return %domcoords;
10813: }
10814: 
10815: sub active_dc_picker {
10816:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
10817:     my %domcoords = &get_active_dcs($dom);
10818:     my @domcoord = keys(%domcoords);
10819:     if (keys(%currhash)) {
10820:         foreach my $dc (keys(%currhash)) {
10821:             unless (exists($domcoords{$dc})) {
10822:                 push(@domcoord,$dc);
10823:             }
10824:         }
10825:     }
10826:     @domcoord = sort(@domcoord);
10827:     my $numdcs = scalar(@domcoord);
10828:     my $rows = 0;
10829:     my $table;
10830:     if ($numdcs > 1) {
10831:         $table = '<table>';
10832:         for (my $i=0; $i<@domcoord; $i++) {
10833:             my $rem = $i%($numinrow);
10834:             if ($rem == 0) {
10835:                 if ($i > 0) {
10836:                     $table .= '</tr>';
10837:                 }
10838:                 $table .= '<tr>';
10839:                 $rows ++;
10840:             }
10841:             my $check = '';
10842:             if ($inputtype eq 'radio') {
10843:                 if (keys(%currhash) == 0) {
10844:                     if (!$i) {
10845:                         $check = ' checked="checked"';
10846:                     }
10847:                 } elsif (exists($currhash{$domcoord[$i]})) {
10848:                     $check = ' checked="checked"';
10849:                 }
10850:             } else {
10851:                 if (exists($currhash{$domcoord[$i]})) {
10852:                     $check = ' checked="checked"';
10853:                 }
10854:             }
10855:             if ($i == @domcoord - 1) {
10856:                 my $colsleft = $numinrow - $rem;
10857:                 if ($colsleft > 1) {
10858:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
10859:                 } else {
10860:                     $table .= '<td class="LC_left_item">';
10861:                 }
10862:             } else {
10863:                 $table .= '<td class="LC_left_item">';
10864:             }
10865:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
10866:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
10867:             $table .= '<span class="LC_nobreak"><label>'.
10868:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
10869:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
10870:             if ($user ne $dcname.':'.$dcdom) {
10871:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
10872:             }
10873:             $table .= '</label></span></td>';
10874:         }
10875:         $table .= '</tr></table>';
10876:     } elsif ($numdcs == 1) {
10877:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
10878:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
10879:         if ($inputtype eq 'radio') {
10880:             $table .= '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
10881:             if ($user ne $dcname.':'.$dcdom) {
10882:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
10883:             }
10884:         } else {
10885:             my $check;
10886:             if (exists($currhash{$domcoord[0]})) {
10887:                 $check = ' checked="checked"';
10888:             }
10889:             $table .= '<span class="LC_nobreak"><label>'.
10890:                       '<input type="checkbox" name="'.$name.'" '.
10891:                       'value="'.$domcoord[0].'"'.$check.' />'.$user;
10892:             if ($user ne $dcname.':'.$dcdom) {
10893:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
10894:             }
10895:             $table .= '</label></span>';
10896:             $rows ++;
10897:         }
10898:     }
10899:     return ($numdcs,$table,$rows);
10900: }
10901: 
10902: sub usersession_titles {
10903:     return &Apache::lonlocal::texthash(
10904:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
10905:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
10906:                spares => 'Servers offloaded to, when busy',
10907:                version => 'LON-CAPA version requirement',
10908:                excludedomain => 'Allow all, but exclude specific domains',
10909:                includedomain => 'Deny all, but include specific domains',
10910:                primary => 'Primary (checked first)',
10911:                default => 'Default',
10912:            );
10913: }
10914: 
10915: sub id_for_thisdom {
10916:     my (%servers) = @_;
10917:     my %altids;
10918:     foreach my $server (keys(%servers)) {
10919:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
10920:         if ($serverhome ne $server) {
10921:             $altids{$serverhome} = $server;
10922:         }
10923:     }
10924:     return %altids;
10925: }
10926: 
10927: sub count_servers {
10928:     my ($currbalancer,%servers) = @_;
10929:     my (@spares,$numspares);
10930:     foreach my $lonhost (sort(keys(%servers))) {
10931:         next if ($currbalancer eq $lonhost);
10932:         push(@spares,$lonhost);
10933:     }
10934:     if ($currbalancer) {
10935:         $numspares = scalar(@spares);
10936:     } else {
10937:         $numspares = scalar(@spares) - 1;
10938:     }
10939:     return ($numspares,@spares);
10940: }
10941: 
10942: sub lonbalance_targets_js {
10943:     my ($dom,$types,$servers,$settings) = @_;
10944:     my $select = &mt('Select');
10945:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
10946:     if (ref($servers) eq 'HASH') {
10947:         $alltargets = join("','",sort(keys(%{$servers})));
10948:         my @homedoms;
10949:         foreach my $server (sort(keys(%{$servers}))) {
10950:             if (&Apache::lonnet::host_domain($server) eq $dom) {
10951:                 push(@homedoms,'1');
10952:             } else {
10953:                 push(@homedoms,'0');
10954:             }
10955:         }
10956:         $allishome = join("','",@homedoms);
10957:     }
10958:     if (ref($types) eq 'ARRAY') {
10959:         if (@{$types} > 0) {
10960:             @alltypes = @{$types};
10961:         }
10962:     }
10963:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
10964:     $allinsttypes = join("','",@alltypes);
10965:     my (%currbalancer,%currtargets,%currrules,%existing);
10966:     if (ref($settings) eq 'HASH') {
10967:         %existing = %{$settings};
10968:     }
10969:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
10970:                               \%currtargets,\%currrules);
10971:     my $balancers = join("','",sort(keys(%currbalancer)));
10972:     return <<"END";
10973: 
10974: <script type="text/javascript">
10975: // <![CDATA[
10976: 
10977: currBalancers = new Array('$balancers');
10978: 
10979: function toggleTargets(balnum) {
10980:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
10981:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
10982:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
10983:     var prevbalancer = prevhostitem.value;
10984:     var baltotal = document.getElementById('loadbalancing_total').value;
10985:     prevhostitem.value = balancer;
10986:     if (prevbalancer != '') {
10987:         var prevIdx = currBalancers.indexOf(prevbalancer);
10988:         if (prevIdx != -1) {
10989:             currBalancers.splice(prevIdx,1);
10990:         }
10991:     }
10992:     if (balancer == '') {
10993:         hideSpares(balnum);
10994:     } else {
10995:         var currIdx = currBalancers.indexOf(balancer);
10996:         if (currIdx == -1) {
10997:             currBalancers.push(balancer);
10998:         }
10999:         var homedoms = new Array('$allishome');
11000:         var ishomedom = homedoms[lonhostitem.selectedIndex];
11001:         showSpares(balancer,ishomedom,balnum);
11002:     }
11003:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
11004:     return;
11005: }
11006: 
11007: function showSpares(balancer,ishomedom,balnum) {
11008:     var alltargets = new Array('$alltargets');
11009:     var insttypes = new Array('$allinsttypes');
11010:     var offloadtypes = new Array('primary','default');
11011: 
11012:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
11013:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
11014:  
11015:     for (var i=0; i<offloadtypes.length; i++) {
11016:         var count = 0;
11017:         for (var j=0; j<alltargets.length; j++) {
11018:             if (alltargets[j] != balancer) {
11019:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
11020:                 item.value = alltargets[j];
11021:                 item.style.textAlign='left';
11022:                 item.style.textFace='normal';
11023:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
11024:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
11025:                     item.disabled = '';
11026:                 } else {
11027:                     item.disabled = 'disabled';
11028:                     item.checked = false;
11029:                 }
11030:                 count ++;
11031:             }
11032:         }
11033:     }
11034:     for (var k=0; k<insttypes.length; k++) {
11035:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
11036:             if (ishomedom == 1) {
11037:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
11038:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
11039:             } else {
11040:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
11041:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
11042:             }
11043:         } else {
11044:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
11045:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
11046:         }
11047:         if ((insttypes[k] != '_LC_external') && 
11048:             ((insttypes[k] != '_LC_internetdom') ||
11049:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
11050:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
11051:             item.options.length = 0;
11052:             item.options[0] = new Option("","",true,true);
11053:             var idx = 0;
11054:             for (var m=0; m<alltargets.length; m++) {
11055:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
11056:                     idx ++;
11057:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
11058:                 }
11059:             }
11060:         }
11061:     }
11062:     return;
11063: }
11064: 
11065: function hideSpares(balnum) {
11066:     var alltargets = new Array('$alltargets');
11067:     var insttypes = new Array('$allinsttypes');
11068:     var offloadtypes = new Array('primary','default');
11069: 
11070:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
11071:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
11072: 
11073:     var total = alltargets.length - 1;
11074:     for (var i=0; i<offloadtypes; i++) {
11075:         for (var j=0; j<total; j++) {
11076:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
11077:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
11078:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
11079:         }
11080:     }
11081:     for (var k=0; k<insttypes.length; k++) {
11082:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
11083:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
11084:         if (insttypes[k] != '_LC_external') {
11085:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
11086:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
11087:         }
11088:     }
11089:     return;
11090: }
11091: 
11092: function checkOffloads(item,balnum,type) {
11093:     var alltargets = new Array('$alltargets');
11094:     var offloadtypes = new Array('primary','default');
11095:     if (item.checked) {
11096:         var total = alltargets.length - 1;
11097:         var other;
11098:         if (type == offloadtypes[0]) {
11099:             other = offloadtypes[1];
11100:         } else {
11101:             other = offloadtypes[0];
11102:         }
11103:         for (var i=0; i<total; i++) {
11104:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
11105:             if (server == item.value) {
11106:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
11107:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
11108:                 }
11109:             }
11110:         }
11111:     }
11112:     return;
11113: }
11114: 
11115: function singleServerToggle(balnum,type) {
11116:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
11117:     if (offloadtoSelIdx == 0) {
11118:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
11119:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
11120: 
11121:     } else {
11122:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
11123:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
11124:     }
11125:     return;
11126: }
11127: 
11128: function balanceruleChange(formname,balnum,type) {
11129:     if (type == '_LC_external') {
11130:         return;
11131:     }
11132:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
11133:     for (var i=0; i<typesRules.length; i++) {
11134:         if (formname.elements[typesRules[i]].checked) {
11135:             if (formname.elements[typesRules[i]].value != 'specific') {
11136:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
11137:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
11138:             } else {
11139:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
11140:             }
11141:         }
11142:     }
11143:     return;
11144: }
11145: 
11146: function balancerDeleteChange(balnum) {
11147:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
11148:     var baltotal = document.getElementById('loadbalancing_total').value;
11149:     var addtarget;
11150:     var removetarget;
11151:     var action = 'delete';
11152:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
11153:         var lonhost = hostitem.value;
11154:         var currIdx = currBalancers.indexOf(lonhost);
11155:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
11156:             if (currIdx != -1) {
11157:                 currBalancers.splice(currIdx,1);
11158:             }
11159:             addtarget = lonhost;
11160:         } else {
11161:             if (currIdx == -1) {
11162:                 currBalancers.push(lonhost);
11163:             }
11164:             removetarget = lonhost;
11165:             action = 'undelete';
11166:         }
11167:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
11168:     }
11169:     return;
11170: }
11171: 
11172: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
11173:     if (baltotal > 1) {
11174:         var offloadtypes = new Array('primary','default');
11175:         var alltargets = new Array('$alltargets');
11176:         var insttypes = new Array('$allinsttypes');
11177:         for (var i=0; i<baltotal; i++) {
11178:             if (i != balnum) {
11179:                 for (var j=0; j<offloadtypes.length; j++) {
11180:                     var total = alltargets.length - 1;
11181:                     for (var k=0; k<total; k++) {
11182:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
11183:                         var server = serveritem.value;
11184:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
11185:                             if (server == addtarget) {
11186:                                 serveritem.disabled = '';
11187:                             }
11188:                         }
11189:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
11190:                             if (server == removetarget) {
11191:                                 serveritem.disabled = 'disabled';
11192:                                 serveritem.checked = false;
11193:                             }
11194:                         }
11195:                     }
11196:                 }
11197:                 for (var j=0; j<insttypes.length; j++) {
11198:                     if (insttypes[j] != '_LC_external') {
11199:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
11200:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
11201:                             var currSel = singleserver.selectedIndex;
11202:                             var currVal = singleserver.options[currSel].value;
11203:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
11204:                                 var numoptions = singleserver.options.length;
11205:                                 var needsnew = 1;
11206:                                 for (var k=0; k<numoptions; k++) {
11207:                                     if (singleserver.options[k] == addtarget) {
11208:                                         needsnew = 0;
11209:                                         break;
11210:                                     }
11211:                                 }
11212:                                 if (needsnew == 1) {
11213:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
11214:                                 }
11215:                             }
11216:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
11217:                                 singleserver.options.length = 0;
11218:                                 if ((currVal) && (currVal != removetarget)) {
11219:                                     singleserver.options[0] = new Option("","",false,false);
11220:                                 } else {
11221:                                     singleserver.options[0] = new Option("","",true,true);
11222:                                 }
11223:                                 var idx = 0;
11224:                                 for (var m=0; m<alltargets.length; m++) {
11225:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
11226:                                         idx ++;
11227:                                         if (currVal == alltargets[m]) {
11228:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
11229:                                         } else {
11230:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
11231:                                         }
11232:                                     }
11233:                                 }
11234:                             }
11235:                         }
11236:                     }
11237:                 }
11238:             }
11239:         }
11240:     }
11241:     return;
11242: }
11243: 
11244: // ]]>
11245: </script>
11246: 
11247: END
11248: }
11249: 
11250: sub new_spares_js {
11251:     my @sparestypes = ('primary','default');
11252:     my $types = join("','",@sparestypes);
11253:     my $select = &mt('Select');
11254:     return <<"END";
11255: 
11256: <script type="text/javascript">
11257: // <![CDATA[
11258: 
11259: function updateNewSpares(formname,lonhost) {
11260:     var types = new Array('$types');
11261:     var include = new Array();
11262:     var exclude = new Array();
11263:     for (var i=0; i<types.length; i++) {
11264:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
11265:         for (var j=0; j<spareboxes.length; j++) {
11266:             if (formname.elements[spareboxes[j]].checked) {
11267:                 exclude.push(formname.elements[spareboxes[j]].value);
11268:             } else {
11269:                 include.push(formname.elements[spareboxes[j]].value);
11270:             }
11271:         }
11272:     }
11273:     for (var i=0; i<types.length; i++) {
11274:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
11275:         var selIdx = newSpare.selectedIndex;
11276:         var currnew = newSpare.options[selIdx].value;
11277:         var okSpares = new Array();
11278:         for (var j=0; j<newSpare.options.length; j++) {
11279:             var possible = newSpare.options[j].value;
11280:             if (possible != '') {
11281:                 if (exclude.indexOf(possible) == -1) {
11282:                     okSpares.push(possible);
11283:                 } else {
11284:                     if (currnew == possible) {
11285:                         selIdx = 0;
11286:                     }
11287:                 }
11288:             }
11289:         }
11290:         for (var k=0; k<include.length; k++) {
11291:             if (okSpares.indexOf(include[k]) == -1) {
11292:                 okSpares.push(include[k]);
11293:             }
11294:         }
11295:         okSpares.sort();
11296:         newSpare.options.length = 0;
11297:         if (selIdx == 0) {
11298:             newSpare.options[0] = new Option("$select","",true,true);
11299:         } else {
11300:             newSpare.options[0] = new Option("$select","",false,false);
11301:         }
11302:         for (var m=0; m<okSpares.length; m++) {
11303:             var idx = m+1;
11304:             var selThis = 0;
11305:             if (selIdx != 0) {
11306:                 if (okSpares[m] == currnew) {
11307:                     selThis = 1;
11308:                 }
11309:             }
11310:             if (selThis == 1) {
11311:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
11312:             } else {
11313:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
11314:             }
11315:         }
11316:     }
11317:     return;
11318: }
11319: 
11320: function checkNewSpares(lonhost,type) {
11321:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
11322:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
11323:     if (chosen != '') { 
11324:         var othertype;
11325:         var othernewSpare;
11326:         if (type == 'primary') {
11327:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
11328:         }
11329:         if (type == 'default') {
11330:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
11331:         }
11332:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
11333:             othernewSpare.selectedIndex = 0;
11334:         }
11335:     }
11336:     return;
11337: }
11338: 
11339: // ]]>
11340: </script>
11341: 
11342: END
11343: 
11344: }
11345: 
11346: sub common_domprefs_js {
11347:     return <<"END";
11348: 
11349: <script type="text/javascript">
11350: // <![CDATA[
11351: 
11352: function getIndicesByName(formname,item) {
11353:     var group = new Array();
11354:     for (var i=0;i<formname.elements.length;i++) {
11355:         if (formname.elements[i].name == item) {
11356:             group.push(formname.elements[i].id);
11357:         }
11358:     }
11359:     return group;
11360: }
11361: 
11362: // ]]>
11363: </script>
11364: 
11365: END
11366: 
11367: }
11368: 
11369: sub recaptcha_js {
11370:     my %lt = &captcha_phrases();
11371:     return <<"END";
11372: 
11373: <script type="text/javascript">
11374: // <![CDATA[
11375: 
11376: function updateCaptcha(caller,context) {
11377:     var privitem;
11378:     var pubitem;
11379:     var privtext;
11380:     var pubtext;
11381:     if (document.getElementById(context+'_recaptchapub')) {
11382:         pubitem = document.getElementById(context+'_recaptchapub');
11383:     } else {
11384:         return;
11385:     }
11386:     if (document.getElementById(context+'_recaptchapriv')) {
11387:         privitem = document.getElementById(context+'_recaptchapriv');
11388:     } else {
11389:         return;
11390:     }
11391:     if (document.getElementById(context+'_recaptchapubtxt')) {
11392:         pubtext = document.getElementById(context+'_recaptchapubtxt');
11393:     } else {
11394:         return;
11395:     }
11396:     if (document.getElementById(context+'_recaptchaprivtxt')) {
11397:         privtext = document.getElementById(context+'_recaptchaprivtxt');
11398:     } else {
11399:         return;
11400:     }
11401:     if (caller.checked) {
11402:         if (caller.value == 'recaptcha') {
11403:             pubitem.type = 'text';
11404:             privitem.type = 'text';
11405:             pubitem.size = '40';
11406:             privitem.size = '40';
11407:             pubtext.innerHTML = "$lt{'pub'}";
11408:             privtext.innerHTML = "$lt{'priv'}";
11409:         } else {
11410:             pubitem.type = 'hidden';
11411:             privitem.type = 'hidden';
11412:             pubtext.innerHTML = '';
11413:             privtext.innerHTML = '';
11414:         }
11415:     }
11416:     return;
11417: }
11418: 
11419: // ]]>
11420: </script>
11421: 
11422: END
11423: 
11424: }
11425: 
11426: sub toggle_display_js {
11427:     return <<"END";
11428: 
11429: <script type="text/javascript">
11430: // <![CDATA[
11431: 
11432: function toggleDisplay(domForm,caller) {
11433:     if (document.getElementById(caller)) {
11434:         var divitem = document.getElementById(caller);
11435:         var optionsElement = domForm.coursecredits;
11436:         if (caller == 'emailoptions') {
11437:             optionsElement = domForm.cancreate_email; 
11438:         }
11439:         if (optionsElement.length) {
11440:             var currval;
11441:             for (var i=0; i<optionsElement.length; i++) {
11442:                 if (optionsElement[i].checked) {
11443:                    currval = optionsElement[i].value;
11444:                 }
11445:             }
11446:             if (currval == 1) {
11447:                 divitem.style.display = 'block';
11448:             } else {
11449:                 divitem.style.display = 'none';
11450:             }
11451:         }
11452:     }
11453:     return;
11454: }
11455: 
11456: // ]]>
11457: </script>
11458: 
11459: END
11460: 
11461: }
11462: 
11463: sub captcha_phrases {
11464:     return &Apache::lonlocal::texthash (
11465:                  priv => 'Private key',
11466:                  pub  => 'Public key',
11467:                  original  => 'original (CAPTCHA)',
11468:                  recaptcha => 'successor (ReCAPTCHA)',
11469:                  notused   => 'unused',
11470:     );
11471: }
11472: 
11473: sub devalidate_remote_domconfs {
11474:     my ($dom,$cachekeys) = @_;
11475:     return unless (ref($cachekeys) eq 'HASH');
11476:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
11477:     my %thismachine;
11478:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
11479:     my @posscached = ('domainconfig','domdefaults');
11480:     if (keys(%servers) > 1) {
11481:         foreach my $server (keys(%servers)) {
11482:             next if ($thismachine{$server});
11483:             my @cached;
11484:             foreach my $name (@posscached) {
11485:                 if ($cachekeys->{$name}) {
11486:                     push(@cached,&escape($name).':'.&escape($dom));
11487:                 }
11488:             }
11489:             if (@cached) {
11490:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
11491:             }
11492:         }
11493:     }
11494:     return;
11495: }
11496: 
11497: 1;

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