File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.94: download - view: text, annotated - select for diffs
Thu Aug 1 01:45:48 2019 UTC (4 years, 9 months ago) by raeburn
Branches: version_2_11_X
Diff to branchpoint 1.160: preferred, unified
- For 2.11
  Backport 1.342

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.160.6.94 2019/08/01 01:45:48 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: #
   29: ###############################################################
   30: ##############################################################
   31: 
   32: =pod
   33: 
   34: =head1 NAME
   35: 
   36: Apache::domainprefs.pm
   37: 
   38: =head1 SYNOPSIS
   39: 
   40: Handles configuration of a LON-CAPA domain.  
   41: 
   42: This is part of the LearningOnline Network with CAPA project
   43: described at http://www.lon-capa.org.
   44: 
   45: 
   46: =head1 OVERVIEW
   47: 
   48: Each institution using LON-CAPA will typically have a single domain designated 
   49: for use by individuals affiliated with the institution.  Accordingly, each domain
   50: may define a default set of logos and a color scheme which can be used to "brand"
   51: the LON-CAPA instance. In addition, an institution will typically have a language
   52: and timezone which are used for the majority of courses.
   53: 
   54: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   55: host of other domain-wide settings which determine the types of functionality
   56: available to users and courses in the domain.
   57: 
   58: There is also a mechanism to configure cataloging of courses in the domain, and
   59: controls on the operation of automated processes which govern such things as
   60: roster updates, user directory updates and processing of course requests.
   61: 
   62: The domain coordination manual which is built dynamically on install/update of 
   63: LON-CAPA from the relevant help items provides more information about domain 
   64: configuration.
   65: 
   66: Most of the domain settings are stored in the configuration.db GDBM file which is
   67: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   68: where $dom is the domain.  The configuration.db stores settings in a number of 
   69: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   70: the domain as files (e.g., image files for logos etc., or plain text files for
   71: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   72: session hosted on the primary library server in the domain, as these files are 
   73: stored in author space belonging to a special $dom-domainconfig user.   
   74: 
   75: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   76: the current settings, and provides an interface to make modifications.
   77: 
   78: =head1 SUBROUTINES
   79: 
   80: =over
   81: 
   82: =item print_quotas()
   83: 
   84: Inputs: 4 
   85: 
   86: $dom,$settings,$rowtotal,$action.
   87: 
   88: $dom is the domain, $settings is a reference to a hash of current settings for
   89: the current context, $rowtotal is a reference to the scalar used to record the 
   90: number of rows displayed on the page, and $action is the context (quotas, 
   91: requestcourses or requestauthor).
   92: 
   93: The print_quotas routine was orginally created to display/store information
   94: about default quota sizes for portfolio spaces for the different types of 
   95: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   96: but is now also used to manage availability of user tools: 
   97: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   98: used by course owners to request creation of a course, and to display/store
   99: default quota sizes for Authoring Spaces.
  100: 
  101: Outputs: 1
  102: 
  103: $datatable  - HTML containing form elements which allow settings to be changed. 
  104: 
  105: In the case of course requests, radio buttons are displayed for each institutional
  106: affiliate type (and also default, and _LC_adv) for each of the course types 
  107: (official, unofficial, community, and textbook).  In each case the radio buttons 
  108: allow the selection of one of four values:
  109: 
  110: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  111: which have the following effects:
  112: 
  113: 0
  114: 
  115: =over
  116: 
  117: - course requests are not allowed for this course types/affiliation
  118: 
  119: =back
  120: 
  121: approval 
  122: 
  123: =over 
  124: 
  125: - course requests must be approved by a Doman Coordinator in the 
  126: course's domain
  127: 
  128: =back
  129: 
  130: validate 
  131: 
  132: =over
  133: 
  134: - an institutional validation (e.g., check requestor is instructor
  135: of record) needs to be passed before the course will be created.  The required
  136: validation is in localenroll.pm on the primary library server for the course 
  137: domain.
  138: 
  139: =back
  140: 
  141: autolimit 
  142: 
  143: =over
  144:  
  145: - course requests will be processed automatically up to a limit of
  146: N requests for the course type for the particular requestor.
  147: If N is undefined, there is no limit to the number of course requests
  148: which a course owner may submit and have processed automatically. 
  149: 
  150: =back
  151: 
  152: =item modify_quotas() 
  153: 
  154: =back
  155: 
  156: =cut
  157: 
  158: package Apache::domainprefs;
  159: 
  160: use strict;
  161: use Apache::Constants qw(:common :http);
  162: use Apache::lonnet;
  163: use Apache::loncommon();
  164: use Apache::lonhtmlcommon();
  165: use Apache::lonlocal;
  166: use Apache::lonmsg();
  167: use Apache::lonconfigsettings;
  168: use Apache::lonuserutils();
  169: use Apache::loncoursequeueadmin();
  170: use LONCAPA qw(:DEFAULT :match);
  171: use LONCAPA::Enrollment;
  172: use LONCAPA::lonauthcgi();
  173: use File::Copy;
  174: use Locale::Language;
  175: use DateTime::TimeZone;
  176: use DateTime::Locale;
  177: 
  178: my $registered_cleanup;
  179: my $modified_urls;
  180: 
  181: sub handler {
  182:     my $r=shift;
  183:     if ($r->header_only) {
  184:         &Apache::loncommon::content_type($r,'text/html');
  185:         $r->send_http_header;
  186:         return OK;
  187:     }
  188: 
  189:     my $context = 'domain';
  190:     my $dom = $env{'request.role.domain'};
  191:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  192:     if (&Apache::lonnet::allowed('mau',$dom)) {
  193:         &Apache::loncommon::content_type($r,'text/html');
  194:         $r->send_http_header;
  195:     } else {
  196:         $env{'user.error.msg'}=
  197:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  198:         return HTTP_NOT_ACCEPTABLE;
  199:     }
  200: 
  201:     $registered_cleanup=0;
  202:     @{$modified_urls}=();
  203: 
  204:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  205:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  206:                                             ['phase','actions']);
  207:     my $phase = 'pickactions';
  208:     if ( exists($env{'form.phase'}) ) {
  209:         $phase = $env{'form.phase'};
  210:     }
  211:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  212:     my %domconfig =
  213:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  214:                 'quotas','autoenroll','autoupdate','autocreate',
  215:                 'directorysrch','usercreation','usermodification',
  216:                 'contacts','defaults','scantron','coursecategories',
  217:                 'serverstatuses','requestcourses','helpsettings',
  218:                 'coursedefaults','usersessions','loadbalancing',
  219:                 'requestauthor','selfenrollment','inststatus'],$dom);
  220:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  221:                        'autoupdate','autocreate','directorysrch','contacts',
  222:                        'usercreation','selfcreation','usermodification','scantron',
  223:                        'requestcourses','requestauthor','coursecategories',
  224:                        'serverstatuses','helpsettings','coursedefaults',
  225:                        'selfenrollment','usersessions');
  226:     my %existing;
  227:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  228:         %existing = %{$domconfig{'loadbalancing'}};
  229:     }
  230:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  231:         push(@prefs_order,'loadbalancing');
  232:     }
  233:     my %prefs = (
  234:         'rolecolors' =>
  235:                    { text => 'Default color schemes',
  236:                      help => 'Domain_Configuration_Color_Schemes',
  237:                      header => [{col1 => 'Student Settings',
  238:                                  col2 => '',},
  239:                                 {col1 => 'Coordinator Settings',
  240:                                  col2 => '',},
  241:                                 {col1 => 'Author Settings',
  242:                                  col2 => '',},
  243:                                 {col1 => 'Administrator Settings',
  244:                                  col2 => '',}],
  245:                       print => \&print_rolecolors,
  246:                       modify => \&modify_rolecolors,
  247:                     },
  248:         'login' =>
  249:                     { text => 'Log-in page options',
  250:                       help => 'Domain_Configuration_Login_Page',
  251:                       header => [{col1 => 'Log-in Page Items',
  252:                                   col2 => '',},
  253:                                  {col1 => 'Log-in Help',
  254:                                   col2 => 'Value'},
  255:                                  {col1 => 'Custom HTML in document head',
  256:                                   col2 => 'Value'}],
  257:                       print => \&print_login,
  258:                       modify => \&modify_login,
  259:                     },
  260:         'defaults' => 
  261:                     { text => 'Default authentication/language/timezone/portal/types',
  262:                       help => 'Domain_Configuration_LangTZAuth',
  263:                       header => [{col1 => 'Setting',
  264:                                   col2 => 'Value'},
  265:                                  {col1 => 'Internal Authentication',
  266:                                   col2 => 'Value'},
  267:                                  {col1 => 'Institutional user types',
  268:                                   col2 => 'Name displayed'}],
  269:                       print => \&print_defaults,
  270:                       modify => \&modify_defaults,
  271:                     },
  272:         'quotas' => 
  273:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  274:                       help => 'Domain_Configuration_Quotas',
  275:                       header => [{col1 => 'User affiliation',
  276:                                   col2 => 'Available tools',
  277:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  278:                       print => \&print_quotas,
  279:                       modify => \&modify_quotas,
  280:                     },
  281:         'autoenroll' =>
  282:                    { text => 'Auto-enrollment settings',
  283:                      help => 'Domain_Configuration_Auto_Enrollment',
  284:                      header => [{col1 => 'Configuration setting',
  285:                                  col2 => 'Value(s)'}],
  286:                      print => \&print_autoenroll,
  287:                      modify => \&modify_autoenroll,
  288:                    },
  289:         'autoupdate' => 
  290:                    { text => 'Auto-update settings',
  291:                      help => 'Domain_Configuration_Auto_Updates',
  292:                      header => [{col1 => 'Setting',
  293:                                  col2 => 'Value',},
  294:                                 {col1 => 'Setting',
  295:                                  col2 => 'Affiliation'},
  296:                                 {col1 => 'User population',
  297:                                  col2 => 'Updatable user data'}],
  298:                      print => \&print_autoupdate,
  299:                      modify => \&modify_autoupdate,
  300:                   },
  301:         'autocreate' => 
  302:                   { text => 'Auto-course creation settings',
  303:                      help => 'Domain_Configuration_Auto_Creation',
  304:                      header => [{col1 => 'Configuration Setting',
  305:                                  col2 => 'Value',}],
  306:                      print => \&print_autocreate,
  307:                      modify => \&modify_autocreate,
  308:                   },
  309:         'directorysrch' => 
  310:                   { text => 'Directory searches',
  311:                     help => 'Domain_Configuration_InstDirectory_Search',
  312:                     header => [{col1 => 'Institutional Directory Setting',
  313:                                 col2 => 'Value',},
  314:                                {col1 => 'LON-CAPA Directory Setting',
  315:                                 col2 => 'Value',}],
  316:                     print => \&print_directorysrch,
  317:                     modify => \&modify_directorysrch,
  318:                   },
  319:         'contacts' =>
  320:                   { text => 'E-mail addresses and helpform',
  321:                     help => 'Domain_Configuration_Contact_Info',
  322:                     header => [{col1 => 'Default e-mail addresses',
  323:                                 col2 => 'Value',},
  324:                                {col1 => 'Recipient(s) for notifications',
  325:                                 col2 => 'Value',},
  326:                                {col1 => 'Ask helpdesk form settings',
  327:                                 col2 => 'Value',},],
  328:                     print => \&print_contacts,
  329:                     modify => \&modify_contacts,
  330:                   },
  331:         'usercreation' => 
  332:                   { text => 'User creation',
  333:                     help => 'Domain_Configuration_User_Creation',
  334:                     header => [{col1 => 'Format rule type',
  335:                                 col2 => 'Format rules in force'},
  336:                                {col1 => 'User account creation',
  337:                                 col2 => 'Usernames which may be created',},
  338:                                {col1 => 'Context',
  339:                                 col2 => 'Assignable authentication types'}],
  340:                     print => \&print_usercreation,
  341:                     modify => \&modify_usercreation,
  342:                   },
  343:         'selfcreation' => 
  344:                   { text => 'Users self-creating accounts',
  345:                     help => 'Domain_Configuration_Self_Creation', 
  346:                     header => [{col1 => 'Self-creation with institutional username',
  347:                                 col2 => 'Enabled?'},
  348:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  349:                                 col2 => 'Information user can enter'},
  350:                                {col1 => 'Self-creation with e-mail verification',
  351:                                 col2 => 'Settings'}],
  352:                     print => \&print_selfcreation,
  353:                     modify => \&modify_selfcreation,
  354:                   },
  355:         'usermodification' =>
  356:                   { text => 'User modification',
  357:                     help => 'Domain_Configuration_User_Modification',
  358:                     header => [{col1 => 'Target user has role',
  359:                                 col2 => 'User information updatable in author context'},
  360:                                {col1 => 'Target user has role',
  361:                                 col2 => 'User information updatable in course context'}],
  362:                     print => \&print_usermodification,
  363:                     modify => \&modify_usermodification,
  364:                   },
  365:         'scantron' =>
  366:                   { text => 'Bubblesheet format file',
  367:                     help => 'Domain_Configuration_Scantron_Format',
  368:                     header => [ {col1 => 'Item',
  369:                                  col2 => '',
  370:                               }],
  371:                     print => \&print_scantron,
  372:                     modify => \&modify_scantron,
  373:                   },
  374:         'requestcourses' => 
  375:                  {text => 'Request creation of courses',
  376:                   help => 'Domain_Configuration_Request_Courses',
  377:                   header => [{col1 => 'User affiliation',
  378:                               col2 => 'Availability/Processing of requests',},
  379:                              {col1 => 'Setting',
  380:                               col2 => 'Value'},
  381:                              {col1 => 'Available textbooks',
  382:                               col2 => ''},
  383:                              {col1 => 'Available templates',
  384:                               col2 => ''},
  385:                              {col1 => 'Validation (not official courses)',
  386:                               col2 => 'Value'},],
  387:                   print => \&print_quotas,
  388:                   modify => \&modify_quotas,
  389:                  },
  390:         'requestauthor' =>
  391:                  {text => 'Request Authoring Space',
  392:                   help => 'Domain_Configuration_Request_Author',
  393:                   header => [{col1 => 'User affiliation',
  394:                               col2 => 'Availability/Processing of requests',},
  395:                              {col1 => 'Setting',
  396:                               col2 => 'Value'}],
  397:                   print => \&print_quotas,
  398:                   modify => \&modify_quotas,
  399:                  },
  400:         'coursecategories' =>
  401:                   { text => 'Cataloging of courses/communities',
  402:                     help => 'Domain_Configuration_Cataloging_Courses',
  403:                     header => [{col1 => 'Catalog type/availability',
  404:                                 col2 => '',},
  405:                                {col1 => 'Category settings for standard catalog',
  406:                                 col2 => '',},
  407:                                {col1 => 'Categories',
  408:                                 col2 => '',
  409:                                }],
  410:                     print => \&print_coursecategories,
  411:                     modify => \&modify_coursecategories,
  412:                   },
  413:         'serverstatuses' =>
  414:                  {text   => 'Access to server status pages',
  415:                   help   => 'Domain_Configuration_Server_Status',
  416:                   header => [{col1 => 'Status Page',
  417:                               col2 => 'Other named users',
  418:                               col3 => 'Specific IPs',
  419:                             }],
  420:                   print => \&print_serverstatuses,
  421:                   modify => \&modify_serverstatuses,
  422:                  },
  423:         'helpsettings' =>
  424:                  {text   => 'Support settings',
  425:                   help   => 'Domain_Configuration_Help_Settings',
  426:                   header => [{col1 => 'Help Page Settings (logged-in users)',
  427:                               col2 => 'Value'},
  428:                              {col1 => 'Helpdesk Roles',
  429:                               col2 => 'Settings'},],
  430:                   print  => \&print_helpsettings,
  431:                   modify => \&modify_helpsettings,
  432:                  },
  433:         'coursedefaults' => 
  434:                  {text => 'Course/Community defaults',
  435:                   help => 'Domain_Configuration_Course_Defaults',
  436:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  437:                               col2 => 'Value',},
  438:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  439:                               col2 => 'Value',},],
  440:                   print => \&print_coursedefaults,
  441:                   modify => \&modify_coursedefaults,
  442:                  },
  443:         'selfenrollment' => 
  444:                  {text   => 'Self-enrollment in Course/Community',
  445:                   help   => 'Domain_Configuration_Selfenrollment',
  446:                   header => [{col1 => 'Configuration Rights',
  447:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  448:                              {col1 => 'Defaults',
  449:                               col2 => 'Value'},
  450:                              {col1 => 'Self-enrollment validation (optional)',
  451:                               col2 => 'Value'},],
  452:                   print => \&print_selfenrollment,
  453:                   modify => \&modify_selfenrollment,
  454:                  },
  455:         'usersessions' =>
  456:                  {text  => 'User session hosting/offloading',
  457:                   help  => 'Domain_Configuration_User_Sessions',
  458:                   header => [{col1 => 'Domain server',
  459:                               col2 => 'Servers to offload sessions to when busy'},
  460:                              {col1 => 'Hosting of users from other domains',
  461:                               col2 => 'Rules'},
  462:                              {col1 => "Hosting domain's own users elsewhere",
  463:                               col2 => 'Rules'}],
  464:                   print => \&print_usersessions,
  465:                   modify => \&modify_usersessions,
  466:                  },
  467:         'loadbalancing' =>
  468:                  {text  => 'Dedicated Load Balancer(s)',
  469:                   help  => 'Domain_Configuration_Load_Balancing',
  470:                   header => [{col1 => 'Balancers',
  471:                               col2 => 'Default destinations',
  472:                               col3 => 'User affiliation',
  473:                               col4 => 'Overrides'},
  474:                             ],
  475:                   print => \&print_loadbalancing,
  476:                   modify => \&modify_loadbalancing,
  477:                  },
  478:     );
  479:     if (keys(%servers) > 1) {
  480:         $prefs{'login'}  = { text   => 'Log-in page options',
  481:                              help   => 'Domain_Configuration_Login_Page',
  482:                             header => [{col1 => 'Log-in Service',
  483:                                         col2 => 'Server Setting',},
  484:                                        {col1 => 'Log-in Page Items',
  485:                                         col2 => ''},
  486:                                        {col1 => 'Log-in Help',
  487:                                         col2 => 'Value'},
  488:                                        {col1 => 'Custom HTML in document head',
  489:                                         col2 => 'Value'}],
  490:                             print => \&print_login,
  491:                             modify => \&modify_login,
  492:                            };
  493:     }
  494: 
  495:     my @roles = ('student','coordinator','author','admin');
  496:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  497:     &Apache::lonhtmlcommon::add_breadcrumb
  498:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  499:       text=>"Settings to display/modify"});
  500:     my $confname = $dom.'-domainconfig';
  501: 
  502:     if ($phase eq 'process') {
  503:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  504:                                                               \%prefs,\%domconfig,$confname,\@roles);
  505:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  506:             $r->rflush();
  507:             &devalidate_remote_domconfs($dom,$result);
  508:         }
  509:     } elsif ($phase eq 'display') {
  510:         my $js = &recaptcha_js().
  511:                  &toggle_display_js();
  512:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  513:             my ($othertitle,$usertypes,$types) =
  514:                 &Apache::loncommon::sorted_inst_types($dom);
  515:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  516:                                           $domconfig{'loadbalancing'}).
  517:                    &new_spares_js().
  518:                    &common_domprefs_js().
  519:                    &Apache::loncommon::javascript_array_indexof();
  520:         }
  521:         if (grep(/^requestcourses$/,@actions)) {
  522:             my $javascript_validations;
  523:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  524:             $js .= <<END;
  525: <script type="text/javascript">
  526: $javascript_validations
  527: </script>
  528: $coursebrowserjs
  529: END
  530:         }
  531:         if (grep(/^selfcreation$/,@actions)) {
  532:             $js .= &selfcreate_javascript();
  533:         }
  534:         if (grep(/^contacts$/,@actions)) {
  535:             $js .= &contacts_javascript();
  536:         }
  537:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  538:     } else {
  539: # check if domconfig user exists for the domain.
  540:         my $servadm = $r->dir_config('lonAdmEMail');
  541:         my ($configuserok,$author_ok,$switchserver) =
  542:             &config_check($dom,$confname,$servadm);
  543:         unless ($configuserok eq 'ok') {
  544:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  545:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  546:                           $confname).
  547:                       '<br />'
  548:             );
  549:             if ($switchserver) {
  550:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  551:                           '<br />'.
  552:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  553:                           '<br />'.
  554:                           &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).
  555:                           '<br />'.
  556:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  557:                 );
  558:             } else {
  559:                 $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.').
  560:                           '<br />'.
  561:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  562:                 );
  563:             }
  564:             $r->print(&Apache::loncommon::end_page());
  565:             return OK;
  566:         }
  567:         if (keys(%domconfig) == 0) {
  568:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  569:             my @ids=&Apache::lonnet::current_machine_ids();
  570:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  571:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  572:                 my @loginimages = ('img','logo','domlogo','login');
  573:                 my $custom_img_count = 0;
  574:                 foreach my $img (@loginimages) {
  575:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  576:                         $custom_img_count ++;
  577:                     }
  578:                 }
  579:                 foreach my $role (@roles) {
  580:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  581:                         $custom_img_count ++;
  582:                     }
  583:                 }
  584:                 if ($custom_img_count > 0) {
  585:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  586:                     my $switch_server = &check_switchserver($dom,$confname);
  587:                     $r->print(
  588:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  589:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  590:     &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 />'.
  591:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  592:                     if ($switch_server) {
  593:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  594:                     }
  595:                     $r->print(&Apache::loncommon::end_page());
  596:                     return OK;
  597:                 }
  598:             }
  599:         }
  600:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  601:     }
  602:     return OK;
  603: }
  604: 
  605: sub process_changes {
  606:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  607:     my %domconfig;
  608:     if (ref($values) eq 'HASH') {
  609:         %domconfig = %{$values};
  610:     }
  611:     my $output;
  612:     if ($action eq 'login') {
  613:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  614:     } elsif ($action eq 'rolecolors') {
  615:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  616:                                      $lastactref,%domconfig);
  617:     } elsif ($action eq 'quotas') {
  618:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  619:     } elsif ($action eq 'autoenroll') {
  620:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  621:     } elsif ($action eq 'autoupdate') {
  622:         $output = &modify_autoupdate($dom,%domconfig);
  623:     } elsif ($action eq 'autocreate') {
  624:         $output = &modify_autocreate($dom,%domconfig);
  625:     } elsif ($action eq 'directorysrch') {
  626:         $output = &modify_directorysrch($dom,$lastactref,%domconfig);
  627:     } elsif ($action eq 'usercreation') {
  628:         $output = &modify_usercreation($dom,%domconfig);
  629:     } elsif ($action eq 'selfcreation') {
  630:         $output = &modify_selfcreation($dom,$lastactref,%domconfig);
  631:     } elsif ($action eq 'usermodification') {
  632:         $output = &modify_usermodification($dom,%domconfig);
  633:     } elsif ($action eq 'contacts') {
  634:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  635:     } elsif ($action eq 'defaults') {
  636:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  637:     } elsif ($action eq 'scantron') {
  638:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  639:     } elsif ($action eq 'coursecategories') {
  640:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  641:     } elsif ($action eq 'serverstatuses') {
  642:         $output = &modify_serverstatuses($dom,%domconfig);
  643:     } elsif ($action eq 'requestcourses') {
  644:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  645:     } elsif ($action eq 'requestauthor') {
  646:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  647:     } elsif ($action eq 'helpsettings') {
  648:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  649:     } elsif ($action eq 'coursedefaults') {
  650:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  651:     } elsif ($action eq 'selfenrollment') {
  652:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  653:     } elsif ($action eq 'usersessions') {
  654:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  655:     } elsif ($action eq 'loadbalancing') {
  656:         $output = &modify_loadbalancing($dom,%domconfig);
  657:     }
  658:     return $output;
  659: }
  660: 
  661: sub print_config_box {
  662:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  663:     my $rowtotal = 0;
  664:     my $output;
  665:     if ($action eq 'coursecategories') {
  666:         $output = &coursecategories_javascript($settings);
  667:     } elsif ($action eq 'defaults') {
  668:         $output = &defaults_javascript($settings); 
  669:     } elsif ($action eq 'helpsettings') {
  670:         my (%privs,%levelscurrent);
  671:         my %full=();
  672:         my %levels=(
  673:                      course => {},
  674:                      domain => {},
  675:                      system => {},
  676:                    );
  677:         my $context = 'domain';
  678:         my $crstype = 'Course';
  679:         my $formname = 'display';
  680:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  681:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  682:         $output =
  683:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full,
  684:                                                       \@templateroles);
  685:     }
  686:     $output .=
  687:          '<table class="LC_nested_outer">
  688:           <tr>
  689:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  690:            &mt($item->{text}).'&nbsp;'.
  691:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  692:           '</tr>';
  693:     $rowtotal ++;
  694:     my $numheaders = 1;
  695:     if (ref($item->{'header'}) eq 'ARRAY') {
  696:         $numheaders = scalar(@{$item->{'header'}});
  697:     }
  698:     if ($numheaders > 1) {
  699:         my $colspan = '';
  700:         my $rightcolspan = '';
  701:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  702:             ($action eq 'directorysrch') ||
  703:             (($action eq 'login') && ($numheaders < 4))) {
  704:             $colspan = ' colspan="2"';
  705:         }
  706:         if ($action eq 'usersessions') {
  707:             $rightcolspan = ' colspan="3"'; 
  708:         }
  709:         $output .= '
  710:           <tr>
  711:            <td>
  712:             <table class="LC_nested">
  713:              <tr class="LC_info_row">
  714:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  715:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  716:              </tr>';
  717:         $rowtotal ++;
  718:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  719:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  720:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'directorysrch') ||
  721:             ($action eq 'helpsettings') || ($action eq 'contacts')) {
  722:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  723:         } elsif ($action eq 'coursecategories') {
  724:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  725:         } elsif ($action eq 'login') {
  726:             if ($numheaders == 4) {
  727:                 $colspan = ' colspan="2"';
  728:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  729:             } else {
  730:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  731:             }
  732:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  733:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  734:         } elsif ($action eq 'rolecolors') {
  735:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  736:         }
  737:         $output .= '
  738:            </table>
  739:           </td>
  740:          </tr>
  741:          <tr>
  742:            <td>
  743:             <table class="LC_nested">
  744:              <tr class="LC_info_row">
  745:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  746:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  747:              </tr>';
  748:             $rowtotal ++;
  749:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  750:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  751:             ($action eq 'usersessions') || ($action eq 'coursecategories') ||
  752:             ($action eq 'contacts') || ($action eq 'defaults')) {
  753:             if ($action eq 'coursecategories') {
  754:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  755:                 $colspan = ' colspan="2"';
  756:             } else {
  757:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  758:             }
  759:             $output .= '
  760:            </table>
  761:           </td>
  762:          </tr>
  763:          <tr>
  764:            <td>
  765:             <table class="LC_nested">
  766:              <tr class="LC_info_row">
  767:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  768:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  769:              </tr>'."\n";
  770:             if ($action eq 'coursecategories') {
  771:                 $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  772:             } else {
  773:                 $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  774:             }
  775:             $rowtotal ++;
  776:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  777:                  ($action eq 'defaults') || ($action eq 'directorysrch') ||
  778:                  ($action eq 'helpsettings')) {
  779:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  780:         } elsif ($action eq 'login') {
  781:             if ($numheaders == 4) {
  782:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  783:            </table>
  784:           </td>
  785:          </tr>
  786:          <tr>
  787:            <td>
  788:             <table class="LC_nested">
  789:              <tr class="LC_info_row">
  790:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  791:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  792:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  793:                 $rowtotal ++;
  794:             } else {
  795:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  796:             }
  797:             $output .= '
  798:            </table>
  799:           </td>
  800:          </tr>
  801:          <tr>
  802:            <td>
  803:             <table class="LC_nested">
  804:              <tr class="LC_info_row">';
  805:             if ($numheaders == 4) {
  806:                 $output .= '
  807:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  808:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  809:              </tr>';
  810:             } else {
  811:                 $output .= '
  812:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  813:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  814:              </tr>';
  815:             }
  816:             $rowtotal ++;
  817:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal);
  818:         } elsif ($action eq 'requestcourses') {
  819:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  820:             $rowtotal ++;
  821:             $output .= &print_studentcode($settings,\$rowtotal).'
  822:            </table>
  823:           </td>
  824:          </tr>
  825:          <tr>
  826:            <td>
  827:             <table class="LC_nested">
  828:              <tr class="LC_info_row">
  829:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  830:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  831:                        &textbookcourses_javascript($settings).
  832:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
  833:             </table>
  834:            </td>
  835:           </tr>
  836:          <tr>
  837:            <td>
  838:             <table class="LC_nested">
  839:              <tr class="LC_info_row">
  840:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  841:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
  842:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
  843:             </table>
  844:            </td>
  845:           </tr>
  846:           <tr>
  847:            <td>
  848:             <table class="LC_nested">
  849:              <tr class="LC_info_row">
  850:               <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
  851:               <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
  852:              </tr>'.
  853:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
  854:         } elsif ($action eq 'requestauthor') {
  855:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  856:             $rowtotal ++;
  857:         } elsif ($action eq 'rolecolors') {
  858:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  859:            </table>
  860:           </td>
  861:          </tr>
  862:          <tr>
  863:            <td>
  864:             <table class="LC_nested">
  865:              <tr class="LC_info_row">
  866:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  867:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  868:               <td class="LC_right_item" valign="top">'.
  869:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  870:              </tr>'.
  871:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  872:            </table>
  873:           </td>
  874:          </tr>
  875:          <tr>
  876:            <td>
  877:             <table class="LC_nested">
  878:              <tr class="LC_info_row">
  879:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  880:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  881:              </tr>'.
  882:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  883:             $rowtotal += 2;
  884:         }
  885:     } else {
  886:         $output .= '
  887:           <tr>
  888:            <td>
  889:             <table class="LC_nested">
  890:              <tr class="LC_info_row">';
  891:         if ($action eq 'login') {
  892:             $output .= '  
  893:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  894:         } elsif ($action eq 'serverstatuses') {
  895:             $output .= '
  896:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  897:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  898: 
  899:         } else {
  900:             $output .= '
  901:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  902:         }
  903:         if (defined($item->{'header'}->[0]->{'col3'})) {
  904:             $output .= '<td class="LC_left_item" valign="top">'.
  905:                        &mt($item->{'header'}->[0]->{'col2'});
  906:             if ($action eq 'serverstatuses') {
  907:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  908:             } 
  909:         } else {
  910:             $output .= '<td class="LC_right_item" valign="top">'.
  911:                        &mt($item->{'header'}->[0]->{'col2'});
  912:         }
  913:         $output .= '</td>';
  914:         if ($item->{'header'}->[0]->{'col3'}) {
  915:             if (defined($item->{'header'}->[0]->{'col4'})) {
  916:                 $output .= '<td class="LC_left_item" valign="top">'.
  917:                             &mt($item->{'header'}->[0]->{'col3'});
  918:             } else {
  919:                 $output .= '<td class="LC_right_item" valign="top">'.
  920:                            &mt($item->{'header'}->[0]->{'col3'});
  921:             }
  922:             if ($action eq 'serverstatuses') {
  923:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  924:             }
  925:             $output .= '</td>';
  926:         }
  927:         if ($item->{'header'}->[0]->{'col4'}) {
  928:             $output .= '<td class="LC_right_item" valign="top">'.
  929:                        &mt($item->{'header'}->[0]->{'col4'});
  930:         }
  931:         $output .= '</tr>';
  932:         $rowtotal ++;
  933:         if ($action eq 'quotas') {
  934:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  935:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
  936:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing')) {
  937:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
  938:         } elsif ($action eq 'scantron') {
  939:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  940:         }
  941:     }
  942:     $output .= '
  943:    </table>
  944:   </td>
  945:  </tr>
  946: </table><br />';
  947:     return ($output,$rowtotal);
  948: }
  949: 
  950: sub print_login {
  951:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  952:     my ($css_class,$datatable);
  953:     my %choices = &login_choices();
  954: 
  955:     if ($caller eq 'service') {
  956:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  957:         my $choice = $choices{'disallowlogin'};
  958:         $css_class = ' class="LC_odd_row"';
  959:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  960:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  961:                       '<th>'.$choices{'server'}.'</th>'.
  962:                       '<th>'.$choices{'serverpath'}.'</th>'.
  963:                       '<th>'.$choices{'custompath'}.'</th>'.
  964:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  965:         my %disallowed;
  966:         if (ref($settings) eq 'HASH') {
  967:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  968:                %disallowed = %{$settings->{'loginvia'}};
  969:             }
  970:         }
  971:         foreach my $lonhost (sort(keys(%servers))) {
  972:             my $direct = 'selected="selected"';
  973:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  974:                 if ($disallowed{$lonhost}{'server'} ne '') {
  975:                     $direct = '';
  976:                 }
  977:             }
  978:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  979:                           '<td><select name="'.$lonhost.'_server">'.
  980:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  981:                           '</option>';
  982:             foreach my $hostid (sort(keys(%servers))) {
  983:                 next if ($servers{$hostid} eq $servers{$lonhost});
  984:                 my $selected = '';
  985:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  986:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  987:                         $selected = 'selected="selected"';
  988:                     }
  989:                 }
  990:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  991:                               $servers{$hostid}.'</option>';
  992:             }
  993:             $datatable .= '</select></td>'.
  994:                           '<td><select name="'.$lonhost.'_serverpath">';
  995:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  996:                 my $pathname = $path;
  997:                 if ($path eq 'custom') {
  998:                     $pathname = &mt('Custom Path').' ->';
  999:                 }
 1000:                 my $selected = '';
 1001:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1002:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1003:                         $selected = 'selected="selected"';
 1004:                     }
 1005:                 } elsif ($path eq '') {
 1006:                     $selected = 'selected="selected"';
 1007:                 }
 1008:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1009:             }
 1010:             $datatable .= '</select></td>';
 1011:             my ($custom,$exempt);
 1012:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1013:                 $custom = $disallowed{$lonhost}{'custompath'};
 1014:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1015:             }
 1016:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1017:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1018:                           '</tr>';
 1019:         }
 1020:         $datatable .= '</table></td></tr>';
 1021:         return $datatable;
 1022:     } elsif ($caller eq 'page') {
 1023:         my %defaultchecked = ( 
 1024:                                'coursecatalog' => 'on',
 1025:                                'helpdesk'      => 'on',
 1026:                                'adminmail'     => 'off',
 1027:                                'newuser'       => 'off',
 1028:                              );
 1029:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1030:         my (%checkedon,%checkedoff);
 1031:         foreach my $item (@toggles) {
 1032:             if ($defaultchecked{$item} eq 'on') { 
 1033:                 $checkedon{$item} = ' checked="checked" ';
 1034:                 $checkedoff{$item} = ' ';
 1035:             } elsif ($defaultchecked{$item} eq 'off') {
 1036:                 $checkedoff{$item} = ' checked="checked" ';
 1037:                 $checkedon{$item} = ' ';
 1038:             }
 1039:         }
 1040:         my @images = ('img','logo','domlogo','login');
 1041:         my @logintext = ('textcol','bgcol');
 1042:         my @bgs = ('pgbg','mainbg','sidebg');
 1043:         my @links = ('link','alink','vlink');
 1044:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1045:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1046:         my (%is_custom,%designs);
 1047:         my %defaults = (
 1048:                        font => $defaultdesign{'login.font'},
 1049:                        );
 1050:         foreach my $item (@images) {
 1051:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1052:             $defaults{'showlogo'}{$item} = 1;
 1053:         }
 1054:         foreach my $item (@bgs) {
 1055:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1056:         }
 1057:         foreach my $item (@logintext) {
 1058:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1059:         }
 1060:         foreach my $item (@links) {
 1061:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1062:         }
 1063:         if (ref($settings) eq 'HASH') {
 1064:             foreach my $item (@toggles) {
 1065:                 if ($settings->{$item} eq '1') {
 1066:                     $checkedon{$item} =  ' checked="checked" ';
 1067:                     $checkedoff{$item} = ' ';
 1068:                 } elsif ($settings->{$item} eq '0') {
 1069:                     $checkedoff{$item} =  ' checked="checked" ';
 1070:                     $checkedon{$item} = ' ';
 1071:                 }
 1072:             }
 1073:             foreach my $item (@images) {
 1074:                 if (defined($settings->{$item})) {
 1075:                     $designs{$item} = $settings->{$item};
 1076:                     $is_custom{$item} = 1;
 1077:                 }
 1078:                 if (defined($settings->{'showlogo'}{$item})) {
 1079:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1080:                 }
 1081:             }
 1082:             foreach my $item (@logintext) {
 1083:                 if ($settings->{$item} ne '') {
 1084:                     $designs{'logintext'}{$item} = $settings->{$item};
 1085:                     $is_custom{$item} = 1;
 1086:                 }
 1087:             }
 1088:             if ($settings->{'font'} ne '') {
 1089:                 $designs{'font'} = $settings->{'font'};
 1090:                 $is_custom{'font'} = 1;
 1091:             }
 1092:             foreach my $item (@bgs) {
 1093:                 if ($settings->{$item} ne '') {
 1094:                     $designs{'bgs'}{$item} = $settings->{$item};
 1095:                     $is_custom{$item} = 1;
 1096:                 }
 1097:             }
 1098:             foreach my $item (@links) {
 1099:                 if ($settings->{$item} ne '') {
 1100:                     $designs{'links'}{$item} = $settings->{$item};
 1101:                     $is_custom{$item} = 1;
 1102:                 }
 1103:             }
 1104:         } else {
 1105:             if ($designhash{$dom.'.login.font'} ne '') {
 1106:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1107:                 $is_custom{'font'} = 1;
 1108:             }
 1109:             foreach my $item (@images) {
 1110:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1111:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1112:                     $is_custom{$item} = 1;
 1113:                 }
 1114:             }
 1115:             foreach my $item (@bgs) {
 1116:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1117:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1118:                     $is_custom{$item} = 1;
 1119:                 }
 1120:             }
 1121:             foreach my $item (@links) {
 1122:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1123:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1124:                     $is_custom{$item} = 1;
 1125:                 }
 1126:             }
 1127:         }
 1128:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1129:                                                       logo => 'Institution Logo',
 1130:                                                       domlogo => 'Domain Logo',
 1131:                                                       login => 'Login box');
 1132:         my $itemcount = 1;
 1133:         foreach my $item (@toggles) {
 1134:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1135:             $datatable .=  
 1136:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1137:                 '</td><td>'.
 1138:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1139:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1140:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1141:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1142:                 '</tr>';
 1143:             $itemcount ++;
 1144:         }
 1145:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1146:         $datatable .= '</tr></table></td></tr>';
 1147:     } elsif ($caller eq 'help') {
 1148:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1149:         my $switchserver = &check_switchserver($dom,$confname);
 1150:         my $itemcount = 1;
 1151:         $defaulturl = '/adm/loginproblems.html';
 1152:         $defaulttype = 'default';
 1153:         %lt = &Apache::lonlocal::texthash (
 1154:                      del     => 'Delete?',
 1155:                      rep     => 'Replace:',
 1156:                      upl     => 'Upload:',
 1157:                      default => 'Default',
 1158:                      custom  => 'Custom',
 1159:                                              );
 1160:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1161:         my @currlangs;
 1162:         if (ref($settings) eq 'HASH') {
 1163:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1164:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1165:                     next if ($settings->{'helpurl'}{$key} eq '');
 1166:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1167:                     $type{$key} = 'custom';
 1168:                     unless ($key eq 'nolang') {
 1169:                         push(@currlangs,$key);
 1170:                     }
 1171:                 }
 1172:             } elsif ($settings->{'helpurl'} ne '') {
 1173:                 $type{'nolang'} = 'custom';
 1174:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1175:             }
 1176:         }
 1177:         foreach my $lang ('nolang',sort(@currlangs)) {
 1178:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1179:             $datatable .= '<tr'.$css_class.'>';
 1180:             if ($url{$lang} eq '') {
 1181:                 $url{$lang} = $defaulturl;
 1182:             }
 1183:             if ($type{$lang} eq '') {
 1184:                 $type{$lang} = $defaulttype;
 1185:             }
 1186:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1187:             if ($lang eq 'nolang') {
 1188:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1189:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1190:             } else {
 1191:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1192:                                   $langchoices{$lang},
 1193:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1194:             }
 1195:             $datatable .= '</span></td>'."\n".
 1196:                           '<td class="LC_left_item">';
 1197:             if ($type{$lang} eq 'custom') {
 1198:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1199:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1200:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1201:             } else {
 1202:                 $datatable .= $lt{'upl'};
 1203:             }
 1204:             $datatable .='<br />';
 1205:             if ($switchserver) {
 1206:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1207:             } else {
 1208:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1209:             }
 1210:             $datatable .= '</td></tr>';
 1211:             $itemcount ++;
 1212:         }
 1213:         my @addlangs;
 1214:         foreach my $lang (sort(keys(%langchoices))) {
 1215:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1216:             push(@addlangs,$lang);
 1217:         }
 1218:         if (@addlangs > 0) {
 1219:             my %toadd;
 1220:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1221:             $toadd{''} = &mt('Select');
 1222:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1223:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1224:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1225:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1226:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1227:             if ($switchserver) {
 1228:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1229:             } else {
 1230:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1231:             }
 1232:             $datatable .= '</td></tr>';
 1233:             $itemcount ++;
 1234:         }
 1235:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1236:     } elsif ($caller eq 'headtag') {
 1237:         my %domservers = &Apache::lonnet::get_servers($dom);
 1238:         my $choice = $choices{'headtag'};
 1239:         $css_class = ' class="LC_odd_row"';
 1240:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1241:                       '<td align="left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1242:                       '<th>'.$choices{'current'}.'</th>'.
 1243:                       '<th>'.$choices{'action'}.'</th>'.
 1244:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1245:         my (%currurls,%currexempt);
 1246:         if (ref($settings) eq 'HASH') {
 1247:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1248:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1249:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1250:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1251:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1252:                     }
 1253:                 }
 1254:             }
 1255:         }
 1256:         my %lt = &Apache::lonlocal::texthash(
 1257:                                                del  => 'Delete?',
 1258:                                                rep  => 'Replace:',
 1259:                                                upl  => 'Upload:',
 1260:                                                curr => 'View contents',
 1261:                                                none => 'None',
 1262:         );
 1263:         my $switchserver = &check_switchserver($dom,$confname);
 1264:         foreach my $lonhost (sort(keys(%domservers))) {
 1265:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1266:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1267:             if ($currurls{$lonhost}) {
 1268:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1269:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1270:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1271:                               '">'.$lt{'curr'}.'</a></td>'.
 1272:                               '<td><span class="LC_nobreak"><label>'.
 1273:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1274:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1275:             } else {
 1276:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1277:             }
 1278:             $datatable .='<br />';
 1279:             if ($switchserver) {
 1280:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1281:             } else {
 1282:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1283:             }
 1284:             $datatable .= '</td><td><input type="text" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1285:         }
 1286:         $datatable .= '</table></td></tr>';
 1287:     }
 1288:     return $datatable;
 1289: }
 1290: 
 1291: sub login_choices {
 1292:     my %choices =
 1293:         &Apache::lonlocal::texthash (
 1294:             coursecatalog => 'Display Course/Community Catalog link?',
 1295:             adminmail     => "Display Administrator's E-mail Address?",
 1296:             helpdesk      => 'Display "Contact Helpdesk" link',
 1297:             disallowlogin => "Login page requests redirected",
 1298:             hostid        => "Server",
 1299:             server        => "Redirect to:",
 1300:             serverpath    => "Path",
 1301:             custompath    => "Custom", 
 1302:             exempt        => "Exempt IP(s)",
 1303:             directlogin   => "No redirect",
 1304:             newuser       => "Link to create a user account",
 1305:             img           => "Header",
 1306:             logo          => "Main Logo",
 1307:             domlogo       => "Domain Logo",
 1308:             login         => "Log-in Header", 
 1309:             textcol       => "Text color",
 1310:             bgcol         => "Box color",
 1311:             bgs           => "Background colors",
 1312:             links         => "Link colors",
 1313:             font          => "Font color",
 1314:             pgbg          => "Header",
 1315:             mainbg        => "Page",
 1316:             sidebg        => "Login box",
 1317:             link          => "Link",
 1318:             alink         => "Active link",
 1319:             vlink         => "Visited link",
 1320:             headtag       => "Custom markup",
 1321:             action        => "Action",
 1322:             current       => "Current",
 1323:         );
 1324:     return %choices;
 1325: }
 1326: 
 1327: sub print_rolecolors {
 1328:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1329:     my %choices = &color_font_choices();
 1330:     my @bgs = ('pgbg','tabbg','sidebg');
 1331:     my @links = ('link','alink','vlink');
 1332:     my @images = ('img');
 1333:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1334:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1335:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1336:     my (%is_custom,%designs);
 1337:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1338:     if (ref($settings) eq 'HASH') {
 1339:         if (ref($settings->{$role}) eq 'HASH') {
 1340:             if ($settings->{$role}->{'img'} ne '') {
 1341:                 $designs{'img'} = $settings->{$role}->{'img'};
 1342:                 $is_custom{'img'} = 1;
 1343:             }
 1344:             if ($settings->{$role}->{'font'} ne '') {
 1345:                 $designs{'font'} = $settings->{$role}->{'font'};
 1346:                 $is_custom{'font'} = 1;
 1347:             }
 1348:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1349:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1350:                 $is_custom{'fontmenu'} = 1;
 1351:             }
 1352:             foreach my $item (@bgs) {
 1353:                 if ($settings->{$role}->{$item} ne '') {
 1354:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1355:                     $is_custom{$item} = 1;
 1356:                 }
 1357:             }
 1358:             foreach my $item (@links) {
 1359:                 if ($settings->{$role}->{$item} ne '') {
 1360:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1361:                     $is_custom{$item} = 1;
 1362:                 }
 1363:             }
 1364:         }
 1365:     } else {
 1366:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1367:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1368:             $is_custom{'img'} = 1;
 1369:         }
 1370:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1371:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1372:             $is_custom{'fontmenu'} = 1; 
 1373:         }
 1374:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1375:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1376:             $is_custom{'font'} = 1;
 1377:         }
 1378:         foreach my $item (@bgs) {
 1379:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1380:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1381:                 $is_custom{$item} = 1;
 1382:             
 1383:             }
 1384:         }
 1385:         foreach my $item (@links) {
 1386:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1387:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1388:                 $is_custom{$item} = 1;
 1389:             }
 1390:         }
 1391:     }
 1392:     my $itemcount = 1;
 1393:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1394:     $datatable .= '</tr></table></td></tr>';
 1395:     return $datatable;
 1396: }
 1397: 
 1398: sub role_defaults {
 1399:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1400:     my %defaults;
 1401:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1402:         return %defaults;
 1403:     }
 1404:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1405:     if ($role eq 'login') {
 1406:         %defaults = (
 1407:                        font => $defaultdesign{$role.'.font'},
 1408:                     );
 1409:         if (ref($logintext) eq 'ARRAY') {
 1410:             foreach my $item (@{$logintext}) {
 1411:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1412:             }
 1413:         }
 1414:         foreach my $item (@{$images}) {
 1415:             $defaults{'showlogo'}{$item} = 1;
 1416:         }
 1417:     } else {
 1418:         %defaults = (
 1419:                        img => $defaultdesign{$role.'.img'},
 1420:                        font => $defaultdesign{$role.'.font'},
 1421:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1422:                     );
 1423:     }
 1424:     foreach my $item (@{$bgs}) {
 1425:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1426:     }
 1427:     foreach my $item (@{$links}) {
 1428:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1429:     }
 1430:     foreach my $item (@{$images}) {
 1431:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1432:     }
 1433:     return %defaults;
 1434: }
 1435: 
 1436: sub display_color_options {
 1437:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1438:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1439:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1440:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1441:     my $datatable = '<tr'.$css_class.'>'.
 1442:         '<td>'.$choices->{'font'}.'</td>';
 1443:     if (!$is_custom->{'font'}) {
 1444:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1445:     } else {
 1446:         $datatable .= '<td>&nbsp;</td>';
 1447:     }
 1448:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1449: 
 1450:     $datatable .= '<td><span class="LC_nobreak">'.
 1451:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1452:                   ' value="'.$current_color.'" />&nbsp;'.
 1453:                   '&nbsp;</span></td></tr>';
 1454:     unless ($role eq 'login') { 
 1455:         $datatable .= '<tr'.$css_class.'>'.
 1456:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1457:         if (!$is_custom->{'fontmenu'}) {
 1458:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1459:         } else {
 1460:             $datatable .= '<td>&nbsp;</td>';
 1461:         }
 1462: 	$current_color = $designs->{'fontmenu'} ?
 1463: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1464:         $datatable .= '<td><span class="LC_nobreak">'.
 1465:                       '<input class="colorchooser" type="text" size="10" name="'
 1466: 		      .$role.'_fontmenu"'.
 1467:                       ' value="'.$current_color.'" />&nbsp;'.
 1468:                       '&nbsp;</span></td></tr>';
 1469:     }
 1470:     my $switchserver = &check_switchserver($dom,$confname);
 1471:     foreach my $img (@{$images}) {
 1472: 	$itemcount ++;
 1473:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1474:         $datatable .= '<tr'.$css_class.'>'.
 1475:                       '<td>'.$choices->{$img};
 1476:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1477:         if ($role eq 'login') {
 1478:             if ($img eq 'login') {
 1479:                 $login_hdr_pick =
 1480:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1481:                 $logincolors =
 1482:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1483:                                        $designs,$defaults);
 1484:             } elsif ($img ne 'domlogo') {
 1485:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1486:             }
 1487:         }
 1488:         $datatable .= '</td>';
 1489:         if ($designs->{$img} ne '') {
 1490:             $imgfile = $designs->{$img};
 1491: 	    $img_import = ($imgfile =~ m{^/adm/});
 1492:         } else {
 1493:             $imgfile = $defaults->{$img};
 1494:         }
 1495:         if ($imgfile) {
 1496:             my ($showfile,$fullsize);
 1497:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1498:                 my $urldir = $1;
 1499:                 my $filename = $2;
 1500:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1501:                 if (@info) {
 1502:                     my $thumbfile = 'tn-'.$filename;
 1503:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1504:                     if (@thumb) {
 1505:                         $showfile = $urldir.'/'.$thumbfile;
 1506:                     } else {
 1507:                         $showfile = $imgfile;
 1508:                     }
 1509:                 } else {
 1510:                     $showfile = '';
 1511:                 }
 1512:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1513:                 $showfile = $imgfile;
 1514:                 my $imgdir = $1;
 1515:                 my $filename = $2;
 1516:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1517:                     $showfile = "/$imgdir/tn-".$filename;
 1518:                 } else {
 1519:                     my $input = $londocroot.$imgfile;
 1520:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1521:                     if (!-e $output) {
 1522:                         my ($width,$height) = &thumb_dimensions();
 1523:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1524:                         if ($fullwidth ne '' && $fullheight ne '') {
 1525:                             if ($fullwidth > $width && $fullheight > $height) { 
 1526:                                 my $size = $width.'x'.$height;
 1527:                                 my @args = ('convert','-sample',$size,$input,$output);
 1528:                                 system({$args[0]} @args);
 1529:                                 $showfile = "/$imgdir/tn-".$filename;
 1530:                             }
 1531:                         }
 1532:                     }
 1533:                 }
 1534:             }
 1535:             if ($showfile) {
 1536:                 if ($showfile =~ m{^/(adm|res)/}) {
 1537:                     if ($showfile =~ m{^/res/}) {
 1538:                         my $local_showfile =
 1539:                             &Apache::lonnet::filelocation('',$showfile);
 1540:                         &Apache::lonnet::repcopy($local_showfile);
 1541:                     }
 1542:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1543:                 }
 1544:                 if ($imgfile) {
 1545:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1546:                         if ($imgfile =~ m{^/res/}) {
 1547:                             my $local_imgfile =
 1548:                                 &Apache::lonnet::filelocation('',$imgfile);
 1549:                             &Apache::lonnet::repcopy($local_imgfile);
 1550:                         }
 1551:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1552:                     } else {
 1553:                         $fullsize = $imgfile;
 1554:                     }
 1555:                 }
 1556:                 $datatable .= '<td>';
 1557:                 if ($img eq 'login') {
 1558:                     $datatable .= $login_hdr_pick;
 1559:                 } 
 1560:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1561:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1562:             } else {
 1563:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1564:                               &mt('Upload:').'<br />';
 1565:             }
 1566:         } else {
 1567:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1568:                           &mt('Upload:').'<br />';
 1569:         }
 1570:         if ($switchserver) {
 1571:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1572:         } else {
 1573:             if ($img ne 'login') { # suppress file selection for Log-in header
 1574:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1575:             }
 1576:         }
 1577:         $datatable .= '</td></tr>';
 1578:     }
 1579:     $itemcount ++;
 1580:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1581:     $datatable .= '<tr'.$css_class.'>'.
 1582:                   '<td>'.$choices->{'bgs'}.'</td>';
 1583:     my $bgs_def;
 1584:     foreach my $item (@{$bgs}) {
 1585:         if (!$is_custom->{$item}) {
 1586:             $bgs_def .= '<td><span class="LC_nobreak">'.$choices->{$item}.'</span>&nbsp;<span class="css_default_'.$role.'_'.$item.'" style="background-color: '.$defaults->{'bgs'}{$item}.';">&nbsp;&nbsp;&nbsp;</span><br />'.$defaults->{'bgs'}{$item}.'</td>';
 1587:         }
 1588:     }
 1589:     if ($bgs_def) {
 1590:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1591:     } else {
 1592:         $datatable .= '<td>&nbsp;</td>';
 1593:     }
 1594:     $datatable .= '<td class="LC_right_item">'.
 1595:                   '<table border="0"><tr>';
 1596: 
 1597:     foreach my $item (@{$bgs}) {
 1598:         $datatable .= '<td align="center">'.$choices->{$item};
 1599: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1600:         if ($designs->{'bgs'}{$item}) {
 1601:             $datatable .= '&nbsp;';
 1602:         }
 1603:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1604:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1605:     }
 1606:     $datatable .= '</tr></table></td></tr>';
 1607:     $itemcount ++;
 1608:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1609:     $datatable .= '<tr'.$css_class.'>'.
 1610:                   '<td>'.$choices->{'links'}.'</td>';
 1611:     my $links_def;
 1612:     foreach my $item (@{$links}) {
 1613:         if (!$is_custom->{$item}) {
 1614:             $links_def .= '<td>'.$choices->{$item}.'<br /><span class="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1615:         }
 1616:     }
 1617:     if ($links_def) {
 1618:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1619:     } else {
 1620:         $datatable .= '<td>&nbsp;</td>';
 1621:     }
 1622:     $datatable .= '<td class="LC_right_item">'.
 1623:                   '<table border="0"><tr>';
 1624:     foreach my $item (@{$links}) {
 1625: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1626:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1627:         if ($designs->{'links'}{$item}) {
 1628:             $datatable.='&nbsp;';
 1629:         }
 1630:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1631:                       '" /></td>';
 1632:     }
 1633:     $$rowtotal += $itemcount;
 1634:     return $datatable;
 1635: }
 1636: 
 1637: sub logo_display_options {
 1638:     my ($img,$defaults,$designs) = @_;
 1639:     my $checkedon;
 1640:     if (ref($defaults) eq 'HASH') {
 1641:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1642:             if ($defaults->{'showlogo'}{$img}) {
 1643:                 $checkedon = 'checked="checked" ';     
 1644:             }
 1645:         } 
 1646:     }
 1647:     if (ref($designs) eq 'HASH') {
 1648:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1649:             if (defined($designs->{'showlogo'}{$img})) {
 1650:                 if ($designs->{'showlogo'}{$img} == 0) {
 1651:                     $checkedon = '';
 1652:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1653:                     $checkedon = 'checked="checked" ';
 1654:                 }
 1655:             }
 1656:         }
 1657:     }
 1658:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1659:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1660:            &mt('show').'</label>'."\n";
 1661: }
 1662: 
 1663: sub login_header_options  {
 1664:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1665:     my $output = '';
 1666:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1667:         $output .= &mt('Text default(s):').'<br />';
 1668:         if (!$is_custom->{'textcol'}) {
 1669:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1670:                        '&nbsp;&nbsp;&nbsp;';
 1671:         }
 1672:         if (!$is_custom->{'bgcol'}) {
 1673:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1674:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1675:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1676:         }
 1677:         $output .= '<br />';
 1678:     }
 1679:     $output .='<br />';
 1680:     return $output;
 1681: }
 1682: 
 1683: sub login_text_colors {
 1684:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1685:     my $color_menu = '<table border="0"><tr>';
 1686:     foreach my $item (@{$logintext}) {
 1687:         $color_menu .= '<td align="center">'.$choices->{$item};
 1688:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1689:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1690:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1691:     }
 1692:     $color_menu .= '</tr></table><br />';
 1693:     return $color_menu;
 1694: }
 1695: 
 1696: sub image_changes {
 1697:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1698:     my $output;
 1699:     if ($img eq 'login') {
 1700:         $output = '</td><td>'.$logincolors; # suppress image for Log-in header
 1701:     } elsif (!$is_custom) {
 1702:         if ($img ne 'domlogo') {
 1703:             $output = &mt('Default image:').'<br />';
 1704:         } else {
 1705:             $output = &mt('Default in use:').'<br />';
 1706:         }
 1707:     }
 1708:     if ($img ne 'login') {
 1709:         if ($img_import) {
 1710:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1711:         }
 1712:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1713:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1714:         if ($is_custom) {
 1715:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1716:                        '<input type="checkbox" name="'.
 1717:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1718:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1719:         } else {
 1720:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1721:         }
 1722:     }
 1723:     return $output;
 1724: }
 1725: 
 1726: sub print_quotas {
 1727:     my ($dom,$settings,$rowtotal,$action) = @_;
 1728:     my $context;
 1729:     if ($action eq 'quotas') {
 1730:         $context = 'tools';
 1731:     } else {
 1732:         $context = $action;
 1733:     }
 1734:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1735:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1736:     my $typecount = 0;
 1737:     my ($css_class,%titles);
 1738:     if ($context eq 'requestcourses') {
 1739:         @usertools = ('official','unofficial','community','textbook');
 1740:         @options =('norequest','approval','validate','autolimit');
 1741:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1742:         %titles = &courserequest_titles();
 1743:     } elsif ($context eq 'requestauthor') {
 1744:         @usertools = ('author');
 1745:         @options = ('norequest','approval','automatic');
 1746:         %titles = &authorrequest_titles();
 1747:     } else {
 1748:         @usertools = ('aboutme','blog','webdav','portfolio');
 1749:         %titles = &tool_titles();
 1750:     }
 1751:     if (ref($types) eq 'ARRAY') {
 1752:         foreach my $type (@{$types}) {
 1753:             my ($currdefquota,$currauthorquota);
 1754:             unless (($context eq 'requestcourses') ||
 1755:                     ($context eq 'requestauthor')) {
 1756:                 if (ref($settings) eq 'HASH') {
 1757:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1758:                         $currdefquota = $settings->{defaultquota}->{$type};
 1759:                     } else {
 1760:                         $currdefquota = $settings->{$type};
 1761:                     }
 1762:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1763:                         $currauthorquota = $settings->{authorquota}->{$type};
 1764:                     }
 1765:                 }
 1766:             }
 1767:             if (defined($usertypes->{$type})) {
 1768:                 $typecount ++;
 1769:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1770:                 $datatable .= '<tr'.$css_class.'>'.
 1771:                               '<td>'.$usertypes->{$type}.'</td>'.
 1772:                               '<td class="LC_left_item">';
 1773:                 if ($context eq 'requestcourses') {
 1774:                     $datatable .= '<table><tr>';
 1775:                 }
 1776:                 my %cell;  
 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}->{$type};
 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}{$type}) {
 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:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1815:                                   '<input type="radio" name="crsreq_'.$item.
 1816:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1817:                                   $titles{$option}.'</label>';
 1818:                             if ($option eq 'autolimit') {
 1819:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1820:                                                 $item.'_limit_'.$type.'" size="1" '.
 1821:                                                 'value="'.$currlimit.'" />';
 1822:                             }
 1823:                             $cell{$item} .= '</span> ';
 1824:                             if ($option eq 'autolimit') {
 1825:                                 $cell{$item} .= $titles{'unlimited'};
 1826:                             }
 1827:                         }
 1828:                     } elsif ($context eq 'requestauthor') {
 1829:                         my $curroption;
 1830:                         if (ref($settings) eq 'HASH') {
 1831:                             $curroption = $settings->{$type};
 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_'.$type.
 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}->{$type} == 0) {
 1855:                                     $checked = '';
 1856:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1857:                                     $checked =  'checked="checked" ';
 1858:                                 }
 1859:                             }
 1860:                         }
 1861:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1862:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1863:                                       '" value="'.$type.'" '.$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">'.$cell{$item}.'</td>';  
 1871:                     }
 1872:                     $datatable .= '</tr></table>';
 1873:                 }
 1874:                 $datatable .= '</td>';
 1875:                 unless (($context eq 'requestcourses') ||
 1876:                         ($context eq 'requestauthor')) {
 1877:                     $datatable .= 
 1878:                               '<td class="LC_right_item">'.
 1879:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1880:                               '<input type="text" name="quota_'.$type.
 1881:                               '" value="'.$currdefquota.
 1882:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1883:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1884:                               '<input type="text" name="authorquota_'.$type.
 1885:                               '" value="'.$currauthorquota.
 1886:                               '" size="5" /></span></td>';
 1887:                 }
 1888:                 $datatable .= '</tr>';
 1889:             }
 1890:         }
 1891:     }
 1892:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1893:         $defaultquota = '20';
 1894:         $authorquota = '500';
 1895:         if (ref($settings) eq 'HASH') {
 1896:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1897:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1898:             } elsif (defined($settings->{'default'})) {
 1899:                 $defaultquota = $settings->{'default'};
 1900:             }
 1901:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1902:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1903:             }
 1904:         }
 1905:     }
 1906:     $typecount ++;
 1907:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1908:     $datatable .= '<tr'.$css_class.'>'.
 1909:                   '<td>'.$othertitle.'</td>'.
 1910:                   '<td class="LC_left_item">';
 1911:     if ($context eq 'requestcourses') {
 1912:         $datatable .= '<table><tr>';
 1913:     }
 1914:     my %defcell;
 1915:     foreach my $item (@usertools) {
 1916:         if ($context eq 'requestcourses') {
 1917:             my ($curroption,$currlimit);
 1918:             if (ref($settings) eq 'HASH') {
 1919:                 if (ref($settings->{$item}) eq 'HASH') {
 1920:                     $curroption = $settings->{$item}->{'default'};
 1921:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1922:                         $currlimit = $1;
 1923:                     }
 1924:                 }
 1925:             }
 1926:             if (!$curroption) {
 1927:                 $curroption = 'norequest';
 1928:             }
 1929:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1930:             foreach my $option (@options) {
 1931:                 my $val = $option;
 1932:                 if ($option eq 'norequest') {
 1933:                     $val = 0;
 1934:                 }
 1935:                 if ($option eq 'validate') {
 1936:                     my $canvalidate = 0;
 1937:                     if (ref($validations{$item}) eq 'HASH') {
 1938:                         if ($validations{$item}{'default'}) {
 1939:                             $canvalidate = 1;
 1940:                         }
 1941:                     }
 1942:                     next if (!$canvalidate);
 1943:                 }
 1944:                 my $checked = '';
 1945:                 if ($option eq $curroption) {
 1946:                     $checked = ' checked="checked"';
 1947:                 } elsif ($option eq 'autolimit') {
 1948:                     if ($curroption =~ /^autolimit/) {
 1949:                         $checked = ' checked="checked"';
 1950:                     }
 1951:                 }
 1952:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1953:                                   '<input type="radio" name="crsreq_'.$item.
 1954:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1955:                                   $titles{$option}.'</label>';
 1956:                 if ($option eq 'autolimit') {
 1957:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1958:                                        $item.'_limit_default" size="1" '.
 1959:                                        'value="'.$currlimit.'" />';
 1960:                 }
 1961:                 $defcell{$item} .= '</span> ';
 1962:                 if ($option eq 'autolimit') {
 1963:                     $defcell{$item} .= $titles{'unlimited'};
 1964:                 }
 1965:             }
 1966:         } elsif ($context eq 'requestauthor') {
 1967:             my $curroption;
 1968:             if (ref($settings) eq 'HASH') {
 1969:                 $curroption = $settings->{'default'};
 1970:             }
 1971:             if (!$curroption) {
 1972:                 $curroption = 'norequest';
 1973:             }
 1974:             foreach my $option (@options) {
 1975:                 my $val = $option;
 1976:                 if ($option eq 'norequest') {
 1977:                     $val = 0;
 1978:                 }
 1979:                 my $checked = '';
 1980:                 if ($option eq $curroption) {
 1981:                     $checked = ' checked="checked"';
 1982:                 }
 1983:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1984:                               '<input type="radio" name="authorreq_default"'.
 1985:                               ' value="'.$val.'"'.$checked.' />'.
 1986:                               $titles{$option}.'</label></span>&nbsp; ';
 1987:             }
 1988:         } else {
 1989:             my $checked = 'checked="checked" ';
 1990:             if (ref($settings) eq 'HASH') {
 1991:                 if (ref($settings->{$item}) eq 'HASH') {
 1992:                     if ($settings->{$item}->{'default'} == 0) {
 1993:                         $checked = '';
 1994:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1995:                         $checked = 'checked="checked" ';
 1996:                     }
 1997:                 }
 1998:             }
 1999:             $datatable .= '<span class="LC_nobreak"><label>'.
 2000:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2001:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2002:                           '</label></span>&nbsp; ';
 2003:         }
 2004:     }
 2005:     if ($context eq 'requestcourses') {
 2006:         $datatable .= '</tr><tr>';
 2007:         foreach my $item (@usertools) {
 2008:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2009:         }
 2010:         $datatable .= '</tr></table>';
 2011:     }
 2012:     $datatable .= '</td>';
 2013:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2014:         $datatable .= '<td class="LC_right_item">'.
 2015:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2016:                       '<input type="text" name="defaultquota" value="'.
 2017:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2018:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2019:                       '<input type="text" name="authorquota" value="'.
 2020:                       $authorquota.'" size="5" /></span></td>';
 2021:     }
 2022:     $datatable .= '</tr>';
 2023:     $typecount ++;
 2024:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2025:     $datatable .= '<tr'.$css_class.'>'.
 2026:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2027:     if ($context eq 'requestcourses') {
 2028:         $datatable .= &mt('(overrides affiliation, if set)').
 2029:                       '</td>'.
 2030:                       '<td class="LC_left_item">'.
 2031:                       '<table><tr>';
 2032:     } else {
 2033:         $datatable .= &mt('(overrides affiliation, if checked)').
 2034:                       '</td>'.
 2035:                       '<td class="LC_left_item" colspan="2">'.
 2036:                       '<br />';
 2037:     }
 2038:     my %advcell;
 2039:     foreach my $item (@usertools) {
 2040:         if ($context eq 'requestcourses') {
 2041:             my ($curroption,$currlimit);
 2042:             if (ref($settings) eq 'HASH') {
 2043:                 if (ref($settings->{$item}) eq 'HASH') {
 2044:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2045:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2046:                         $currlimit = $1;
 2047:                     }
 2048:                 }
 2049:             }
 2050:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2051:             my $checked = '';
 2052:             if ($curroption eq '') {
 2053:                 $checked = ' checked="checked"';
 2054:             }
 2055:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2056:                                '<input type="radio" name="crsreq_'.$item.
 2057:                                '__LC_adv" value=""'.$checked.' />'.
 2058:                                &mt('No override set').'</label></span>&nbsp; ';
 2059:             foreach my $option (@options) {
 2060:                 my $val = $option;
 2061:                 if ($option eq 'norequest') {
 2062:                     $val = 0;
 2063:                 }
 2064:                 if ($option eq 'validate') {
 2065:                     my $canvalidate = 0;
 2066:                     if (ref($validations{$item}) eq 'HASH') {
 2067:                         if ($validations{$item}{'_LC_adv'}) {
 2068:                             $canvalidate = 1;
 2069:                         }
 2070:                     }
 2071:                     next if (!$canvalidate);
 2072:                 }
 2073:                 my $checked = '';
 2074:                 if ($val eq $curroption) {
 2075:                     $checked = ' checked="checked"';
 2076:                 } elsif ($option eq 'autolimit') {
 2077:                     if ($curroption =~ /^autolimit/) {
 2078:                         $checked = ' checked="checked"';
 2079:                     }
 2080:                 }
 2081:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2082:                                   '<input type="radio" name="crsreq_'.$item.
 2083:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2084:                                   $titles{$option}.'</label>';
 2085:                 if ($option eq 'autolimit') {
 2086:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2087:                                        $item.'_limit__LC_adv" size="1" '.
 2088:                                        'value="'.$currlimit.'" />';
 2089:                 }
 2090:                 $advcell{$item} .= '</span> ';
 2091:                 if ($option eq 'autolimit') {
 2092:                     $advcell{$item} .= $titles{'unlimited'};
 2093:                 }
 2094:             }
 2095:         } elsif ($context eq 'requestauthor') {
 2096:             my $curroption;
 2097:             if (ref($settings) eq 'HASH') {
 2098:                 $curroption = $settings->{'_LC_adv'};
 2099:             }
 2100:             my $checked = '';
 2101:             if ($curroption eq '') {
 2102:                 $checked = ' checked="checked"';
 2103:             }
 2104:             $datatable .= '<span class="LC_nobreak"><label>'.
 2105:                           '<input type="radio" name="authorreq__LC_adv"'.
 2106:                           ' value=""'.$checked.' />'.
 2107:                           &mt('No override set').'</label></span>&nbsp; ';
 2108:             foreach my $option (@options) {
 2109:                 my $val = $option;
 2110:                 if ($option eq 'norequest') {
 2111:                     $val = 0;
 2112:                 }
 2113:                 my $checked = '';
 2114:                 if ($val eq $curroption) {
 2115:                     $checked = ' checked="checked"';
 2116:                 }
 2117:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2118:                               '<input type="radio" name="authorreq__LC_adv"'.
 2119:                               ' value="'.$val.'"'.$checked.' />'.
 2120:                               $titles{$option}.'</label></span>&nbsp; ';
 2121:             }
 2122:         } else {
 2123:             my $checked = 'checked="checked" ';
 2124:             if (ref($settings) eq 'HASH') {
 2125:                 if (ref($settings->{$item}) eq 'HASH') {
 2126:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2127:                         $checked = '';
 2128:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2129:                         $checked = 'checked="checked" ';
 2130:                     }
 2131:                 }
 2132:             }
 2133:             $datatable .= '<span class="LC_nobreak"><label>'.
 2134:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2135:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2136:                           '</label></span>&nbsp; ';
 2137:         }
 2138:     }
 2139:     if ($context eq 'requestcourses') {
 2140:         $datatable .= '</tr><tr>';
 2141:         foreach my $item (@usertools) {
 2142:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2143:         }
 2144:         $datatable .= '</tr></table>';
 2145:     }
 2146:     $datatable .= '</td></tr>';
 2147:     $$rowtotal += $typecount;
 2148:     return $datatable;
 2149: }
 2150: 
 2151: sub print_requestmail {
 2152:     my ($dom,$action,$settings,$rowtotal,$customcss,$rowstyle) = @_;
 2153:     my ($now,$datatable,%currapp);
 2154:     $now = time;
 2155:     if (ref($settings) eq 'HASH') {
 2156:         if (ref($settings->{'notify'}) eq 'HASH') {
 2157:             if ($settings->{'notify'}{'approval'} ne '') {
 2158:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2159:             }
 2160:         }
 2161:     }
 2162:     my $numinrow = 2;
 2163:     my $css_class;
 2164:     if ($$rowtotal%2) {
 2165:         $css_class = 'LC_odd_row';
 2166:     }
 2167:     if ($customcss) {
 2168:         $css_class .= " $customcss";
 2169:     }
 2170:     $css_class =~ s/^\s+//;
 2171:     if ($css_class) {
 2172:         $css_class = ' class="'.$css_class.'"';
 2173:     }
 2174:     if ($rowstyle) {
 2175:         $css_class .= ' style="'.$rowstyle.'"';
 2176:     }
 2177:     my $text;
 2178:     if ($action eq 'requestcourses') {
 2179:         $text = &mt('Receive notification of course requests requiring approval');
 2180:     } elsif ($action eq 'requestauthor') {
 2181:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2182:     } else {
 2183:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2184:     }
 2185:     $datatable = '<tr'.$css_class.'>'.
 2186:                  ' <td>'.$text.'</td>'.
 2187:                  ' <td class="LC_left_item">';
 2188:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2189:                                                  $action.'notifyapproval',%currapp);
 2190:     if ($numdc > 0) {
 2191:         $datatable .= $table;
 2192:     } else {
 2193:         $datatable .= &mt('There are no active Domain Coordinators');
 2194:     }
 2195:     $datatable .='</td></tr>';
 2196:     return $datatable;
 2197: }
 2198: 
 2199: sub print_studentcode {
 2200:     my ($settings,$rowtotal) = @_;
 2201:     my $rownum = 0; 
 2202:     my ($output,%current);
 2203:     my @crstypes = ('official','unofficial','community','textbook');
 2204:     if (ref($settings) eq 'HASH') {
 2205:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2206:             foreach my $type (@crstypes) {
 2207:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2208:             }
 2209:         }
 2210:     }
 2211:     $output .= '<tr>'.
 2212:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2213:                '<td class="LC_left_item">';
 2214:     foreach my $type (@crstypes) {
 2215:         my $check = ' ';
 2216:         if ($current{$type}) {
 2217:             $check = ' checked="checked" ';
 2218:         }
 2219:         $output .= '<span class="LC_nobreak"><label>'.
 2220:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2221:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2222:     }
 2223:     $output .= '</td></tr>';
 2224:     $$rowtotal ++;
 2225:     return $output;
 2226: }
 2227: 
 2228: sub print_textbookcourses {
 2229:     my ($dom,$type,$settings,$rowtotal) = @_;
 2230:     my $rownum = 0;
 2231:     my $css_class;
 2232:     my $itemcount = 1;
 2233:     my $maxnum = 0;
 2234:     my $bookshash;
 2235:     if (ref($settings) eq 'HASH') {
 2236:         $bookshash = $settings->{$type};
 2237:     }
 2238:     my %ordered;
 2239:     if (ref($bookshash) eq 'HASH') {
 2240:         foreach my $item (keys(%{$bookshash})) {
 2241:             if (ref($bookshash->{$item}) eq 'HASH') {
 2242:                 my $num = $bookshash->{$item}{'order'};
 2243:                 $ordered{$num} = $item;
 2244:             }
 2245:         }
 2246:     }
 2247:     my $confname = $dom.'-domainconfig';
 2248:     my $switchserver = &check_switchserver($dom,$confname);
 2249:     my $maxnum = scalar(keys(%ordered));
 2250:     my $datatable;
 2251:     if (keys(%ordered)) {
 2252:         my @items = sort { $a <=> $b } keys(%ordered);
 2253:         for (my $i=0; $i<@items; $i++) {
 2254:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2255:             my $key = $ordered{$items[$i]};
 2256:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2257:             my $coursetitle = $coursehash{'description'};
 2258:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2259:             if (ref($bookshash->{$key}) eq 'HASH') {
 2260:                 $subject = $bookshash->{$key}->{'subject'};
 2261:                 $title = $bookshash->{$key}->{'title'};
 2262:                 if ($type eq 'textbooks') {
 2263:                     $publisher = $bookshash->{$key}->{'publisher'};
 2264:                     $author = $bookshash->{$key}->{'author'};
 2265:                     $image = $bookshash->{$key}->{'image'};
 2266:                     if ($image ne '') {
 2267:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2268:                         my $imagethumb = "$path/tn-".$imagefile;
 2269:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2270:                     }
 2271:                 }
 2272:             }
 2273:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2274:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2275:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2276:             for (my $k=0; $k<=$maxnum; $k++) {
 2277:                 my $vpos = $k+1;
 2278:                 my $selstr;
 2279:                 if ($k == $i) {
 2280:                     $selstr = ' selected="selected" ';
 2281:                 }
 2282:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2283:             }
 2284:             $datatable .= '</select>'.('&nbsp;'x2).
 2285:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2286:                 &mt('Delete?').'</label></span></td>'.
 2287:                 '<td colspan="2">'.
 2288:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2289:                 ('&nbsp;'x2).
 2290:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2291:             if ($type eq 'textbooks') {
 2292:                 $datatable .= ('&nbsp;'x2).
 2293:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2294:                               ('&nbsp;'x2).
 2295:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2296:                               ('&nbsp;'x2).
 2297:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2298:                 if ($image) {
 2299:                     $datatable .= '<span class="LC_nobreak">'.
 2300:                                   $imgsrc.
 2301:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2302:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2303:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2304:                 }
 2305:                 if ($switchserver) {
 2306:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2307:                 } else {
 2308:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2309:                 }
 2310:             }
 2311:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2312:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2313:                           $coursetitle.'</span></td></tr>'."\n";
 2314:             $itemcount ++;
 2315:         }
 2316:     }
 2317:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2318:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2319:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2320:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2321:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2322:     for (my $k=0; $k<$maxnum+1; $k++) {
 2323:         my $vpos = $k+1;
 2324:         my $selstr;
 2325:         if ($k == $maxnum) {
 2326:             $selstr = ' selected="selected" ';
 2327:         }
 2328:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2329:     }
 2330:     $datatable .= '</select>&nbsp;'."\n".
 2331:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</span></td>'."\n".
 2332:                   '<td colspan="2">'.
 2333:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2334:                   ('&nbsp;'x2).
 2335:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2336:                   ('&nbsp;'x2);
 2337:     if ($type eq 'textbooks') {
 2338:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2339:                       ('&nbsp;'x2).
 2340:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2341:                       ('&nbsp;'x2).
 2342:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2343:         if ($switchserver) {
 2344:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2345:         } else {
 2346:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2347:         }
 2348:         $datatable .= '</span>'."\n";
 2349:     }
 2350:     $datatable .= '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2351:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2352:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2353:                   &Apache::loncommon::selectcourse_link
 2354:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course').
 2355:                   '</span></td>'."\n".
 2356:                   '</tr>'."\n";
 2357:     $itemcount ++;
 2358:     return $datatable;
 2359: }
 2360: 
 2361: sub textbookcourses_javascript {
 2362:     my ($settings) = @_;
 2363:     return unless(ref($settings) eq 'HASH');
 2364:     my (%ordered,%total,%jstext);
 2365:     foreach my $type ('textbooks','templates') {
 2366:         $total{$type} = 0;
 2367:         if (ref($settings->{$type}) eq 'HASH') {
 2368:             foreach my $item (keys(%{$settings->{$type}})) {
 2369:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2370:                     my $num = $settings->{$type}->{$item}{'order'};
 2371:                     $ordered{$type}{$num} = $item;
 2372:                 }
 2373:             }
 2374:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2375:         }
 2376:         my @jsarray = ();
 2377:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2378:             push(@jsarray,$ordered{$type}{$item});
 2379:         }
 2380:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2381:     }
 2382:     return <<"ENDSCRIPT";
 2383: <script type="text/javascript">
 2384: // <![CDATA[
 2385: function reorderBooks(form,item,caller) {
 2386:     var changedVal;
 2387: $jstext{'textbooks'};
 2388: $jstext{'templates'};
 2389:     var newpos;
 2390:     var maxh;
 2391:     if (caller == 'textbooks') {  
 2392:         newpos = 'textbooks_addbook_pos';
 2393:         maxh = 1 + $total{'textbooks'};
 2394:     } else {
 2395:         newpos = 'templates_addbook_pos';
 2396:         maxh = 1 + $total{'templates'};
 2397:     }
 2398:     var current = new Array;
 2399:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2400:     if (item == newpos) {
 2401:         changedVal = newitemVal;
 2402:     } else {
 2403:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2404:         current[newitemVal] = newpos;
 2405:     }
 2406:     if (caller == 'textbooks') {
 2407:         for (var i=0; i<textbooks.length; i++) {
 2408:             var elementName = 'textbooks_'+textbooks[i];
 2409:             if (elementName != item) {
 2410:                 if (form.elements[elementName]) {
 2411:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2412:                     current[currVal] = elementName;
 2413:                 }
 2414:             }
 2415:         }
 2416:     }
 2417:     if (caller == 'templates') {
 2418:         for (var i=0; i<templates.length; i++) {
 2419:             var elementName = 'templates_'+templates[i];
 2420:             if (elementName != item) {
 2421:                 if (form.elements[elementName]) {
 2422:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2423:                     current[currVal] = elementName;
 2424:                 }
 2425:             }
 2426:         }
 2427:     }
 2428:     var oldVal;
 2429:     for (var j=0; j<maxh; j++) {
 2430:         if (current[j] == undefined) {
 2431:             oldVal = j;
 2432:         }
 2433:     }
 2434:     if (oldVal < changedVal) {
 2435:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2436:            var elementName = current[k];
 2437:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2438:         }
 2439:     } else {
 2440:         for (var k=changedVal; k<oldVal; k++) {
 2441:             var elementName = current[k];
 2442:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2443:         }
 2444:     }
 2445:     return;
 2446: }
 2447: 
 2448: // ]]>
 2449: </script>
 2450: 
 2451: ENDSCRIPT
 2452: }
 2453: 
 2454: sub print_autoenroll {
 2455:     my ($dom,$settings,$rowtotal) = @_;
 2456:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2457:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,$failsafe);
 2458:     if (ref($settings) eq 'HASH') {
 2459:         if (exists($settings->{'run'})) {
 2460:             if ($settings->{'run'} eq '0') {
 2461:                 $runoff = ' checked="checked" ';
 2462:                 $runon = ' ';
 2463:             } else {
 2464:                 $runon = ' checked="checked" ';
 2465:                 $runoff = ' ';
 2466:             }
 2467:         } else {
 2468:             if ($autorun) {
 2469:                 $runon = ' checked="checked" ';
 2470:                 $runoff = ' ';
 2471:             } else {
 2472:                 $runoff = ' checked="checked" ';
 2473:                 $runon = ' ';
 2474:             }
 2475:         }
 2476:         if (exists($settings->{'co-owners'})) {
 2477:             if ($settings->{'co-owners'} eq '0') {
 2478:                 $coownersoff = ' checked="checked" ';
 2479:                 $coownerson = ' ';
 2480:             } else {
 2481:                 $coownerson = ' checked="checked" ';
 2482:                 $coownersoff = ' ';
 2483:             }
 2484:         } else {
 2485:             $coownersoff = ' checked="checked" ';
 2486:             $coownerson = ' ';
 2487:         }
 2488:         if (exists($settings->{'sender_domain'})) {
 2489:             $defdom = $settings->{'sender_domain'};
 2490:         }
 2491:         if (exists($settings->{'autofailsafe'})) {
 2492:             $failsafe = $settings->{'autofailsafe'};
 2493:         }
 2494:     } else {
 2495:         if ($autorun) {
 2496:             $runon = ' checked="checked" ';
 2497:             $runoff = ' ';
 2498:         } else {
 2499:             $runoff = ' checked="checked" ';
 2500:             $runon = ' ';
 2501:         }
 2502:     }
 2503:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2504:     my $notif_sender;
 2505:     if (ref($settings) eq 'HASH') {
 2506:         $notif_sender = $settings->{'sender_uname'};
 2507:     }
 2508:     my $datatable='<tr class="LC_odd_row">'.
 2509:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2510:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2511:                   '<input type="radio" name="autoenroll_run"'.
 2512:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2513:                   '<label><input type="radio" name="autoenroll_run"'.
 2514:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2515:                   '</tr><tr>'.
 2516:                   '<td>'.&mt('Notification messages - sender').
 2517:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2518:                   &mt('username').':&nbsp;'.
 2519:                   '<input type="text" name="sender_uname" value="'.
 2520:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2521:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2522:                   '<tr class="LC_odd_row">'.
 2523:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2524:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2525:                   '<input type="radio" name="autoassign_coowners"'.
 2526:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2527:                   '<label><input type="radio" name="autoassign_coowners"'.
 2528:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2529:                   '</tr><tr>'.
 2530:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 2531:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2532:                   '<input type="text" name="autoenroll_failsafe"'.
 2533:                   ' value="'.$failsafe.'" size="4" /></span></td></tr>';
 2534:     $$rowtotal += 4;
 2535:     return $datatable;
 2536: }
 2537: 
 2538: sub print_autoupdate {
 2539:     my ($position,$dom,$settings,$rowtotal) = @_;
 2540:     my $datatable;
 2541:     if ($position eq 'top') {
 2542:         my $updateon = ' ';
 2543:         my $updateoff = ' checked="checked" ';
 2544:         my $classlistson = ' ';
 2545:         my $classlistsoff = ' checked="checked" ';
 2546:         if (ref($settings) eq 'HASH') {
 2547:             if ($settings->{'run'} eq '1') {
 2548:                 $updateon = $updateoff;
 2549:                 $updateoff = ' ';
 2550:             }
 2551:             if ($settings->{'classlists'} eq '1') {
 2552:                 $classlistson = $classlistsoff;
 2553:                 $classlistsoff = ' ';
 2554:             }
 2555:         }
 2556:         my %title = (
 2557:                    run => 'Auto-update active?',
 2558:                    classlists => 'Update information in classlists?',
 2559:                     );
 2560:         $datatable = '<tr class="LC_odd_row">'. 
 2561:                   '<td>'.&mt($title{'run'}).'</td>'.
 2562:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2563:                   '<input type="radio" name="autoupdate_run"'.
 2564:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2565:                   '<label><input type="radio" name="autoupdate_run"'.
 2566:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2567:                   '</tr><tr>'.
 2568:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2569:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2570:                   '<label><input type="radio" name="classlists"'.
 2571:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2572:                   '<label><input type="radio" name="classlists"'.
 2573:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2574:                   '</tr>';
 2575:         $$rowtotal += 2;
 2576:     } elsif ($position eq 'middle') {
 2577:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2578:         my $numinrow = 3;
 2579:         my $locknamesettings;
 2580:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2581:                                      $dom,$numinrow,$othertitle,
 2582:                                     'lockablenames',$rowtotal);
 2583:         $$rowtotal ++;
 2584:     } else {
 2585:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2586:         my @fields = ('lastname','firstname','middlename','generation',
 2587:                       'permanentemail','id');
 2588:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2589:         my $numrows = 0;
 2590:         if (ref($types) eq 'ARRAY') {
 2591:             if (@{$types} > 0) {
 2592:                 $datatable = 
 2593:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2594:                                          \@fields,$types,\$numrows);
 2595:                     $$rowtotal += @{$types}; 
 2596:             }
 2597:         }
 2598:         $datatable .= 
 2599:             &usertype_update_row($settings,{'default' => $othertitle},
 2600:                                  \%fieldtitles,\@fields,['default'],
 2601:                                  \$numrows);
 2602:         $$rowtotal ++;     
 2603:     }
 2604:     return $datatable;
 2605: }
 2606: 
 2607: sub print_autocreate {
 2608:     my ($dom,$settings,$rowtotal) = @_;
 2609:     my (%createon,%createoff,%currhash);
 2610:     my @types = ('xml','req');
 2611:     if (ref($settings) eq 'HASH') {
 2612:         foreach my $item (@types) {
 2613:             $createoff{$item} = ' checked="checked" ';
 2614:             $createon{$item} = ' ';
 2615:             if (exists($settings->{$item})) {
 2616:                 if ($settings->{$item}) {
 2617:                     $createon{$item} = ' checked="checked" ';
 2618:                     $createoff{$item} = ' ';
 2619:                 }
 2620:             }
 2621:         }
 2622:         if ($settings->{'xmldc'} ne '') {
 2623:             $currhash{$settings->{'xmldc'}} = 1;
 2624:         }
 2625:     } else {
 2626:         foreach my $item (@types) {
 2627:             $createoff{$item} = ' checked="checked" ';
 2628:             $createon{$item} = ' ';
 2629:         }
 2630:     }
 2631:     $$rowtotal += 2;
 2632:     my $numinrow = 2;
 2633:     my $datatable='<tr class="LC_odd_row">'.
 2634:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2635:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2636:                   '<input type="radio" name="autocreate_xml"'.
 2637:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2638:                   '<label><input type="radio" name="autocreate_xml"'.
 2639:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2640:                   '</td></tr><tr>'.
 2641:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2642:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2643:                   '<input type="radio" name="autocreate_req"'.
 2644:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2645:                   '<label><input type="radio" name="autocreate_req"'.
 2646:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2647:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2648:                                                    'autocreate_xmldc',%currhash);
 2649:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 2650:     if ($numdc > 1) {
 2651:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 2652:                       '</td><td class="LC_left_item">';
 2653:     } else {
 2654:         $datatable .= &mt('Course creation processed as:').
 2655:                       '</td><td class="LC_right_item">';
 2656:     }
 2657:     $datatable .= $dctable.'</td></tr>';
 2658:     $$rowtotal += $rows;
 2659:     return $datatable;
 2660: }
 2661: 
 2662: sub print_directorysrch {
 2663:     my ($position,$dom,$settings,$rowtotal) = @_;
 2664:     my $datatable;
 2665:     if ($position eq 'top') {
 2666:         my $instsrchon = ' ';
 2667:         my $instsrchoff = ' checked="checked" ';
 2668:         my ($exacton,$containson,$beginson);
 2669:         my $instlocalon = ' ';
 2670:         my $instlocaloff = ' checked="checked" ';
 2671:         if (ref($settings) eq 'HASH') {
 2672:             if ($settings->{'available'} eq '1') {
 2673:                 $instsrchon = $instsrchoff;
 2674:                 $instsrchoff = ' ';
 2675:             }
 2676:             if ($settings->{'localonly'} eq '1') {
 2677:                 $instlocalon = $instlocaloff;
 2678:                 $instlocaloff = ' ';
 2679:             }
 2680:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2681:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 2682:                     if ($type eq 'exact') {
 2683:                         $exacton = ' checked="checked" ';
 2684:                     } elsif ($type eq 'contains') {
 2685:                         $containson = ' checked="checked" ';
 2686:                     } elsif ($type eq 'begins') {
 2687:                         $beginson = ' checked="checked" ';
 2688:                     }
 2689:                 }
 2690:             } else {
 2691:                 if ($settings->{'searchtypes'} eq 'exact') {
 2692:                     $exacton = ' checked="checked" ';
 2693:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 2694:                     $containson = ' checked="checked" ';
 2695:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 2696:                     $exacton = ' checked="checked" ';
 2697:                     $containson = ' checked="checked" ';
 2698:                 }
 2699:             }
 2700:         }
 2701:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2702:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2703: 
 2704:         my $numinrow = 4;
 2705:         my $cansrchrow = 0;
 2706:         $datatable='<tr class="LC_odd_row">'.
 2707:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 2708:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2709:                    '<input type="radio" name="dirsrch_available"'.
 2710:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2711:                    '<label><input type="radio" name="dirsrch_available"'.
 2712:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2713:                    '</tr><tr>'.
 2714:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 2715:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2716:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 2717:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2718:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 2719:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2720:                    '</tr>';
 2721:         $$rowtotal += 2;
 2722:         if (ref($usertypes) eq 'HASH') {
 2723:             if (keys(%{$usertypes}) > 0) {
 2724:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2725:                                              $numinrow,$othertitle,'cansearch',
 2726:                                              $rowtotal);
 2727:                 $cansrchrow = 1;
 2728:             }
 2729:         }
 2730:         if ($cansrchrow) {
 2731:             $$rowtotal ++;
 2732:             $datatable .= '<tr>';
 2733:         } else {
 2734:             $datatable .= '<tr class="LC_odd_row">';
 2735:         }
 2736:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2737:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2738:         foreach my $title (@{$titleorder}) {
 2739:             if (defined($searchtitles->{$title})) {
 2740:                 my $check = ' ';
 2741:                 if (ref($settings) eq 'HASH') {
 2742:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2743:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2744:                             $check = ' checked="checked" ';
 2745:                         }
 2746:                     }
 2747:                 }
 2748:                 $datatable .= '<td class="LC_left_item">'.
 2749:                               '<span class="LC_nobreak"><label>'.
 2750:                               '<input type="checkbox" name="searchby" '.
 2751:                               'value="'.$title.'"'.$check.'/>'.
 2752:                               $searchtitles->{$title}.'</label></span></td>';
 2753:             }
 2754:         }
 2755:         $datatable .= '</tr></table></td></tr>';
 2756:         $$rowtotal ++;
 2757:         if ($cansrchrow) {
 2758:             $datatable .= '<tr class="LC_odd_row">';
 2759:         } else {
 2760:             $datatable .= '<tr>';
 2761:         }
 2762:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2763:                       '<td class="LC_left_item" colspan="2">'.
 2764:                       '<span class="LC_nobreak"><label>'.
 2765:                       '<input type="checkbox" name="searchtypes" '.
 2766:                       $exacton.' value="exact" />'.&mt('Exact match').
 2767:                       '</label>&nbsp;'.
 2768:                       '<label><input type="checkbox" name="searchtypes" '.
 2769:                       $beginson.' value="begins" />'.&mt('Begins with').
 2770:                       '</label>&nbsp;'.
 2771:                       '<label><input type="checkbox" name="searchtypes" '.
 2772:                       $containson.' value="contains" />'.&mt('Contains').
 2773:                       '</label></span></td></tr>';
 2774:         $$rowtotal ++;
 2775:     } else {
 2776:         my $domsrchon = ' checked="checked" ';
 2777:         my $domsrchoff = ' ';
 2778:         my $domlocalon = ' ';
 2779:         my $domlocaloff = ' checked="checked" ';
 2780:         if (ref($settings) eq 'HASH') {
 2781:             if ($settings->{'lclocalonly'} eq '1') {
 2782:                 $domlocalon = $domlocaloff;
 2783:                 $domlocaloff = ' ';
 2784:             }
 2785:             if ($settings->{'lcavailable'} eq '0') {
 2786:                 $domsrchoff = $domsrchon;
 2787:                 $domsrchon = ' ';
 2788:             }
 2789:         }
 2790:         $datatable='<tr class="LC_odd_row">'.
 2791:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 2792:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2793:                       '<input type="radio" name="dirsrch_domavailable"'.
 2794:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2795:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 2796:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2797:                       '</tr><tr>'.
 2798:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 2799:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2800:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 2801:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2802:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 2803:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2804:                       '</tr>';
 2805:         $$rowtotal += 2;
 2806:     }
 2807:     return $datatable;
 2808: }
 2809: 
 2810: sub print_contacts {
 2811:     my ($position,$dom,$settings,$rowtotal) = @_;
 2812:     my $datatable;
 2813:     my @contacts = ('adminemail','supportemail');
 2814:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 2815:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings);
 2816:     if ($position eq 'top') {
 2817:         if (ref($settings) eq 'HASH') {
 2818:             foreach my $item (@contacts) {
 2819:                 if (exists($settings->{$item})) {
 2820:                     $to{$item} = $settings->{$item};
 2821:                 }
 2822:             }
 2823:         }
 2824:     } elsif ($position eq 'middle') {
 2825:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 2826:                      'updatesmail','idconflictsmail','hostipmail');
 2827:         foreach my $type (@mailings) {
 2828:             $otheremails{$type} = '';
 2829:         }
 2830:     } else {
 2831:         @mailings = ('helpdeskmail','otherdomsmail');
 2832:         foreach my $type (@mailings) {
 2833:             $otheremails{$type} = '';
 2834:         }
 2835:         $bccemails{'helpdeskmail'} = '';
 2836:         $bccemails{'otherdomsmail'} = '';
 2837:         $includestr{'helpdeskmail'} = '';
 2838:         $includestr{'otherdomsmail'} = '';
 2839:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 2840:     }
 2841:     if (ref($settings) eq 'HASH') {
 2842:         unless ($position eq 'top') {
 2843:             foreach my $type (@mailings) {
 2844:                 if (exists($settings->{$type})) {
 2845:                     if (ref($settings->{$type}) eq 'HASH') {
 2846:                         foreach my $item (@contacts) {
 2847:                             if ($settings->{$type}{$item}) {
 2848:                                 $checked{$type}{$item} = ' checked="checked" ';
 2849:                             }
 2850:                         }
 2851:                         $otheremails{$type} = $settings->{$type}{'others'};
 2852:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 2853:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 2854:                             if ($settings->{$type}{'include'} ne '') {
 2855:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 2856:                                 $includestr{$type} = &unescape($includestr{$type});
 2857:                             }
 2858:                         }
 2859:                     }
 2860:                 } elsif ($type eq 'lonstatusmail') {
 2861:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2862:                 }
 2863:             }
 2864:         }
 2865:         if ($position eq 'bottom') {
 2866:             foreach my $type (@mailings) {
 2867:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 2868:                 if ($settings->{$type}{'include'} ne '') {
 2869:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 2870:                     $includestr{$type} = &unescape($includestr{$type});
 2871:                 }
 2872:             }
 2873:             if (ref($settings->{'helpform'}) eq 'HASH') {
 2874:                 if (ref($fields) eq 'ARRAY') {
 2875:                     foreach my $field (@{$fields}) {
 2876:                         $currfield{$field} = $settings->{'helpform'}{$field};
 2877:                     }
 2878:                 }
 2879:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 2880:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 2881:                 } else {
 2882:                     $maxsize = '1.0';
 2883:                 }
 2884:             } else {
 2885:                 if (ref($fields) eq 'ARRAY') {
 2886:                     foreach my $field (@{$fields}) {
 2887:                         $currfield{$field} = 'yes';
 2888:                     }
 2889:                 }
 2890:                 $maxsize = '1.0';
 2891:             }
 2892:         }
 2893:     } else {
 2894:         if ($position eq 'top') {
 2895:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2896:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2897:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2898:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2899:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2900:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2901:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2902:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 2903:             $checked{'hostipmail'}{'adminemail'} = ' checked="checked" ';
 2904:         } elsif ($position eq 'bottom') {
 2905:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2906:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 2907:             if (ref($fields) eq 'ARRAY') {
 2908:                 foreach my $field (@{$fields}) {
 2909:                     $currfield{$field} = 'yes';
 2910:                 }
 2911:             }
 2912:             $maxsize = '1.0';
 2913:         }
 2914:     }
 2915:     my ($titles,$short_titles) = &contact_titles();
 2916:     my $rownum = 0;
 2917:     my $css_class;
 2918:     if ($position eq 'top') {
 2919:         foreach my $item (@contacts) {
 2920:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 2921:             $datatable .= '<tr'.$css_class.'>'. 
 2922:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 2923:                           '</span></td><td class="LC_right_item">'.
 2924:                           '<input type="text" name="'.$item.'" value="'.
 2925:                           $to{$item}.'" /></td></tr>';
 2926:             $rownum ++;
 2927:         }
 2928:     } else {
 2929:         foreach my $type (@mailings) {
 2930:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 2931:             $datatable .= '<tr'.$css_class.'>'.
 2932:                           '<td><span class="LC_nobreak">'.
 2933:                           $titles->{$type}.': </span></td>'.
 2934:                           '<td class="LC_left_item">';
 2935:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 2936:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 2937:             }
 2938:             $datatable .= '<span class="LC_nobreak">';
 2939:             foreach my $item (@contacts) {
 2940:                 $datatable .= '<label>'.
 2941:                               '<input type="checkbox" name="'.$type.'"'.
 2942:                               $checked{$type}{$item}.
 2943:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 2944:                               '</label>&nbsp;';
 2945:             }
 2946:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2947:                           '<input type="text" name="'.$type.'_others" '.
 2948:                           'value="'.$otheremails{$type}.'"  />';
 2949:             my %locchecked;
 2950:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 2951:                 foreach my $loc ('s','b') {
 2952:                     if ($includeloc{$type} eq $loc) {
 2953:                         $locchecked{$loc} = ' checked="checked"';
 2954:                         last;
 2955:                     }
 2956:                 }
 2957:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2958:                               '<input type="text" name="'.$type.'_bcc" '.
 2959:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 2960:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 2961:                               &mt('Text automatically added to e-mail:').' '.
 2962:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br />'.
 2963:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 2964:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 2965:                               ('&nbsp;'x2).
 2966:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 2967:                               '</span></fieldset>';
 2968:             }
 2969:             $datatable .= '</td></tr>'."\n";
 2970:             $rownum ++;
 2971:         }
 2972:     }
 2973:     if ($position eq 'middle') {
 2974:         my %choices;
 2975:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 2976:                                        &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2977:                                        &mt('LON-CAPA core group - MSU'),600,500));
 2978:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 2979:                                         &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2980:                                         &mt('LON-CAPA core group - MSU'),600,500));
 2981:         my @toggles = ('reporterrors','reportupdates');
 2982:         my %defaultchecked = ('reporterrors'  => 'on',
 2983:                               'reportupdates' => 'on');
 2984:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2985:                                                    \%choices,$rownum);
 2986:         $datatable .= $reports;
 2987:     } elsif ($position eq 'bottom') {
 2988:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2989:         $datatable .= '<tr'.$css_class.'>'.
 2990:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 2991:                       &mt('(e-mail, subject, and description always shown)').
 2992:                       '</td><td class="LC_left_item">';
 2993:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 2994:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 2995:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 2996:             foreach my $field (@{$fields}) {
 2997:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 2998:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 2999:                     $datatable .= ' '.&mt('(logged-in users)');
 3000:                 }
 3001:                 $datatable .='</td><td>';
 3002:                 my $clickaction;
 3003:                 if ($field eq 'screenshot') {
 3004:                     $clickaction = ' onclick="screenshotSize(this);"';
 3005:                 }
 3006:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 3007:                     foreach my $option (@{$possoptions->{$field}}) {
 3008:                         my $checked;
 3009:                         if ($currfield{$field} eq $option) {
 3010:                             $checked = ' checked="checked"';
 3011:                         }
 3012:                         $datatable .= '<span class="LC_nobreak"><label>'.
 3013:                                       '<input type="radio" name="helpform_'.$field.'" '.
 3014:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 3015:                                       '</label></span>'.('&nbsp;'x2);
 3016:                     }
 3017:                 }
 3018:                 if ($field eq 'screenshot') {
 3019:                     my $display;
 3020:                     if ($currfield{$field} eq 'no') {
 3021:                         $display = ' style="display:none"';
 3022:                     }
 3023:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.'>'.
 3024:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 3025:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 3026:                 }
 3027:                 $datatable .= '</td></tr>';
 3028:             }
 3029:             $datatable .= '</table>';
 3030:         }
 3031:         $datatable .= '</td></tr>'."\n";
 3032:         $rownum ++;
 3033:     }
 3034:     $$rowtotal += $rownum;
 3035:     return $datatable;
 3036: }
 3037: 
 3038: sub contacts_javascript {
 3039:     return <<"ENDSCRIPT";
 3040: 
 3041: <script type="text/javascript">
 3042: // <![CDATA[
 3043: 
 3044: function screenshotSize(field) {
 3045:     if (document.getElementById('help_screenshotsize')) {
 3046:         if (field.value == 'no') {
 3047:             document.getElementById('help_screenshotsize').style.display="none";
 3048:         } else {
 3049:             document.getElementById('help_screenshotsize').style.display="";
 3050:         }
 3051:     }
 3052:     return;
 3053: }
 3054: 
 3055: // ]]>
 3056: </script>
 3057: 
 3058: ENDSCRIPT
 3059: }
 3060: 
 3061: sub print_helpsettings {
 3062:     my ($position,$dom,$settings,$rowtotal) = @_;
 3063:     my $confname = $dom.'-domainconfig';
 3064:     my $formname = 'display';
 3065:     my ($datatable,$itemcount);
 3066:     if ($position eq 'top') {
 3067:         $itemcount = 1;
 3068:         my (%choices,%defaultchecked,@toggles);
 3069:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 3070:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 3071:                                      &mt('LON-CAPA bug tracker'),600,500));
 3072:         %defaultchecked = ('submitbugs' => 'on');
 3073:         @toggles = ('submitbugs');
 3074:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3075:                                                      \%choices,$itemcount);
 3076:         $$rowtotal ++;
 3077:     } else {
 3078:         my $css_class;
 3079:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 3080:         my (%customroles,%ordered,%current);
 3081:         if (ref($settings) eq 'HASH') {
 3082:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 3083:                 %current = %{$settings->{'adhoc'}};
 3084:             }
 3085:         }
 3086:         my $count = 0;
 3087:         foreach my $key (sort(keys(%existing))) {
 3088:             if ($key=~/^rolesdef\_(\w+)$/) {
 3089:                 my $rolename = $1;
 3090:                 my (%privs,$order);
 3091:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 3092:                 $customroles{$rolename} = \%privs;
 3093:                 if (ref($current{$rolename}) eq 'HASH') {
 3094:                     $order = $current{$rolename}{'order'};
 3095:                 }
 3096:                 if ($order eq '') {
 3097:                     $order = $count;
 3098:                 }
 3099:                 $ordered{$order} = $rolename;
 3100:                 $count++;
 3101:             }
 3102:         }
 3103:         my $maxnum = scalar(keys(%ordered));
 3104:         my @roles_by_num = ();
 3105:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3106:             push(@roles_by_num,$item);
 3107:         }
 3108:         my $context = 'domprefs';
 3109:         my $crstype = 'Course';
 3110:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3111:         my @accesstypes = ('all','dh','da','none');
 3112:         my ($numstatustypes,@jsarray);
 3113:         if (ref($types) eq 'ARRAY') {
 3114:             if (@{$types} > 0) {
 3115:                 $numstatustypes = scalar(@{$types});
 3116:                 push(@accesstypes,'status');
 3117:                 @jsarray = ('bystatus');
 3118:             }
 3119:         }
 3120:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 3121:         if (keys(%domhelpdesk)) {
 3122:             push(@accesstypes,('inc','exc'));
 3123:             push(@jsarray,('notinc','notexc'));
 3124:         }
 3125:         my $hiddenstr = join("','",@jsarray);
 3126:         $datatable .= &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname);
 3127:         my $context = 'domprefs';
 3128:         my $crstype = 'Course';
 3129:         my $prefix = 'helproles_';
 3130:         my $add_class = 'LC_hidden';
 3131:         foreach my $num (@roles_by_num) {
 3132:             my $role = $ordered{$num};
 3133:             my ($desc,$access,@statuses);
 3134:             if (ref($current{$role}) eq 'HASH') {
 3135:                 $desc = $current{$role}{'desc'};
 3136:                 $access = $current{$role}{'access'};
 3137:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 3138:                     @statuses = @{$current{$role}{'insttypes'}};
 3139:                 }
 3140:             }
 3141:             if ($desc eq '') {
 3142:                 $desc = $role;
 3143:             }
 3144:             my $identifier = 'custhelp'.$num;
 3145:             my %full=();
 3146:             my %levels= (
 3147:                          course => {},
 3148:                          domain => {},
 3149:                          system => {},
 3150:                         );
 3151:             my %levelscurrent=(
 3152:                                course => {},
 3153:                                domain => {},
 3154:                                system => {},
 3155:                               );
 3156:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 3157:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3158:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3159:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 3160:             $datatable .= '<tr '.$css_class.'><td valign="top"><b>'.$role.'</b><br />'.
 3161:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 3162:             for (my $k=0; $k<=$maxnum; $k++) {
 3163:                 my $vpos = $k+1;
 3164:                 my $selstr;
 3165:                 if ($k == $num) {
 3166:                     $selstr = ' selected="selected" ';
 3167:                 }
 3168:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3169:             }
 3170:             $datatable .= '</select>'.('&nbsp;'x2).
 3171:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 3172:                           '</td>'.
 3173:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3174:                           &mt('Name shown to users:').
 3175:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 3176:                           '</fieldset>'.
 3177:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 3178:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 3179:                           '<fieldset>'.
 3180:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 3181:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 3182:                                                                    \%levelscurrent,$identifier,
 3183:                                                                    'LC_hidden',$prefix.$num.'_privs').
 3184:                           '</fieldset></td>';
 3185:             $itemcount ++;
 3186:         }
 3187:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3188:         my $newcust = 'custhelp'.$count;
 3189:         my (%privs,%levelscurrent);
 3190:         my %full=();
 3191:         my %levels= (
 3192:                      course => {},
 3193:                      domain => {},
 3194:                      system => {},
 3195:                     );
 3196:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 3197:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3198:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 3199:         $datatable .= '<tr '.$css_class.'><td valign="top"><span class="LC_nobreak"><label>'.
 3200:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 3201:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 3202:         for (my $k=0; $k<$maxnum+1; $k++) {
 3203:             my $vpos = $k+1;
 3204:             my $selstr;
 3205:             if ($k == $maxnum) {
 3206:                 $selstr = ' selected="selected" ';
 3207:             }
 3208:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3209:         }
 3210:         $datatable .= '</select>&nbsp;'."\n".
 3211:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 3212:                       '</label></span></td>'.
 3213:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3214:                       '<span class="LC_nobreak">'.
 3215:                       &mt('Internal name:').
 3216:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 3217:                       '</span>'.('&nbsp;'x4).
 3218:                       '<span class="LC_nobreak">'.
 3219:                       &mt('Name shown to users:').
 3220:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 3221:                       '</span></fieldset>'.
 3222:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 3223:                                              $usertypes,$types,\%domhelpdesk).
 3224:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 3225:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 3226:                                                                 \@templateroles,$newcust).
 3227:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 3228:                                                                \%levelscurrent,$newcust).
 3229:                       '</fieldset>'.
 3230:                       &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname).
 3231:                       '</td></tr>';
 3232:         $count ++;
 3233:         $$rowtotal += $count;
 3234:     }
 3235:     return $datatable;
 3236: }
 3237: 
 3238: sub adhocbutton {
 3239:     my ($prefix,$num,$field,$visibility) = @_;
 3240:     my %lt = &Apache::lonlocal::texthash(
 3241:                                           show => 'Show details',
 3242:                                           hide => 'Hide details',
 3243:                                         );
 3244:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 3245:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 3246:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 3247:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 3248: }
 3249: 
 3250: sub helpsettings_javascript {
 3251:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 3252:     return unless(ref($roles_by_num) eq 'ARRAY');
 3253:     my %html_js_lt = &Apache::lonlocal::texthash(
 3254:                                           show => 'Show details',
 3255:                                           hide => 'Hide details',
 3256:                                         );
 3257:     &html_escape(\%html_js_lt);
 3258:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 3259:     return <<"ENDSCRIPT";
 3260: <script type="text/javascript">
 3261: // <![CDATA[
 3262: 
 3263: function reorderHelpRoles(form,item) {
 3264:     var changedVal;
 3265: $jstext
 3266:     var newpos = 'helproles_${total}_pos';
 3267:     var maxh = 1 + $total;
 3268:     var current = new Array();
 3269:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3270:     if (item == newpos) {
 3271:         changedVal = newitemVal;
 3272:     } else {
 3273:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3274:         current[newitemVal] = newpos;
 3275:     }
 3276:     for (var i=0; i<helproles.length; i++) {
 3277:         var elementName = 'helproles_'+helproles[i]+'_pos';
 3278:         if (elementName != item) {
 3279:             if (form.elements[elementName]) {
 3280:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3281:                 current[currVal] = elementName;
 3282:             }
 3283:         }
 3284:     }
 3285:     var oldVal;
 3286:     for (var j=0; j<maxh; j++) {
 3287:         if (current[j] == undefined) {
 3288:             oldVal = j;
 3289:         }
 3290:     }
 3291:     if (oldVal < changedVal) {
 3292:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3293:            var elementName = current[k];
 3294:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3295:         }
 3296:     } else {
 3297:         for (var k=changedVal; k<oldVal; k++) {
 3298:             var elementName = current[k];
 3299:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3300:         }
 3301:     }
 3302:     return;
 3303: }
 3304: 
 3305: function helpdeskAccess(num) {
 3306:     var curraccess = null;
 3307:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 3308:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 3309:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 3310:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 3311:             }
 3312:         }
 3313:     }
 3314:     var shown = Array();
 3315:     var hidden = Array();
 3316:     if (curraccess == 'none') {
 3317:         hidden = Array('$hiddenstr');
 3318:     } else {
 3319:         if (curraccess == 'status') {
 3320:             shown = Array('bystatus');
 3321:             hidden = Array('notinc','notexc');
 3322:         } else {
 3323:             if (curraccess == 'exc') {
 3324:                 shown = Array('notexc');
 3325:                 hidden = Array('notinc','bystatus');
 3326:             }
 3327:             if (curraccess == 'inc') {
 3328:                 shown = Array('notinc');
 3329:                 hidden = Array('notexc','bystatus');
 3330:             }
 3331:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 3332:                 hidden = Array('notinc','notexc','bystatus');
 3333:             }
 3334:         }
 3335:     }
 3336:     if (hidden.length > 0) {
 3337:         for (var i=0; i<hidden.length; i++) {
 3338:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 3339:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 3340:             }
 3341:         }
 3342:     }
 3343:     if (shown.length > 0) {
 3344:         for (var i=0; i<shown.length; i++) {
 3345:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 3346:                 if (shown[i] == 'privs') {
 3347:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 3348:                 } else {
 3349:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 3350:                 }
 3351:             }
 3352:         }
 3353:     }
 3354:     return;
 3355: }
 3356: 
 3357: function toggleHelpdeskItem(num,field) {
 3358:     if (document.getElementById('helproles_'+num+'_'+field)) {
 3359:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 3360:             document.getElementById('helproles_'+num+'_'+field).className =
 3361:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 3362:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3363:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 3364:             }
 3365:         } else {
 3366:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 3367:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3368:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 3369:             }
 3370:         }
 3371:     }
 3372:     return;
 3373: }
 3374: 
 3375: // ]]>
 3376: </script>
 3377: 
 3378: ENDSCRIPT
 3379: }
 3380: 
 3381: sub helpdeskroles_access {
 3382:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 3383:         $usertypes,$types,$domhelpdesk) = @_;
 3384:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 3385:     my %lt = &Apache::lonlocal::texthash(
 3386:                     'rou'    => 'Role usage',
 3387:                     'whi'    => 'Which helpdesk personnel may use this role?',
 3388:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 3389:                     'dh'     => 'All with domain helpdesk role',
 3390:                     'da'     => 'All with domain helpdesk assistant role',
 3391:                     'none'   => 'None',
 3392:                     'status' => 'Determined based on institutional status',
 3393:                     'inc'    => 'Include all, but exclude specific personnel',
 3394:                     'exc'    => 'Exclude all, but include specific personnel',
 3395:                   );
 3396:     my %usecheck = (
 3397:                      all => ' checked="checked"',
 3398:                    );
 3399:     my %displaydiv = (
 3400:                       status => 'none',
 3401:                       inc    => 'none',
 3402:                       exc    => 'none',
 3403:                       priv   => 'block',
 3404:                      );
 3405:     my $output;
 3406:     if (ref($current) eq 'HASH') {
 3407:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 3408:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 3409:                 $usecheck{$current->{access}} = $usecheck{'all'};
 3410:                 delete($usecheck{'all'});
 3411:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 3412:                     my $access = $1;
 3413:                     $displaydiv{$access} = 'inline';
 3414:                 } elsif ($current->{access} eq 'none') {
 3415:                     $displaydiv{'priv'} = 'none';
 3416:                 }
 3417:             }
 3418:         }
 3419:     }
 3420:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 3421:               '<p>'.$lt{'whi'}.'</p>';
 3422:     foreach my $access (@{$accesstypes}) {
 3423:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 3424:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 3425:                    $lt{$access}.'</label>';
 3426:         if ($access eq 'status') {
 3427:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 3428:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 3429:                                                                  $othertitle,$usertypes,$types).
 3430:                        '</div>';
 3431:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 3432:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 3433:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3434:                        '</div>';
 3435:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 3436:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 3437:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3438:                        '</div>';
 3439:         }
 3440:         $output .= '</p>';
 3441:     }
 3442:     $output .= '</fieldset>';
 3443:     return $output;
 3444: }
 3445: 
 3446: sub radiobutton_prefs {
 3447:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 3448:         $additional,$align) = @_;
 3449:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 3450:                    (ref($choices) eq 'HASH'));
 3451: 
 3452:     my (%checkedon,%checkedoff,$datatable,$css_class);
 3453: 
 3454:     foreach my $item (@{$toggles}) {
 3455:         if ($defaultchecked->{$item} eq 'on') {
 3456:             $checkedon{$item} = ' checked="checked" ';
 3457:             $checkedoff{$item} = ' ';
 3458:         } elsif ($defaultchecked->{$item} eq 'off') {
 3459:             $checkedoff{$item} = ' checked="checked" ';
 3460:             $checkedon{$item} = ' ';
 3461:         }
 3462:     }
 3463:     if (ref($settings) eq 'HASH') {
 3464:         foreach my $item (@{$toggles}) {
 3465:             if ($settings->{$item} eq '1') {
 3466:                 $checkedon{$item} =  ' checked="checked" ';
 3467:                 $checkedoff{$item} = ' ';
 3468:             } elsif ($settings->{$item} eq '0') {
 3469:                 $checkedoff{$item} =  ' checked="checked" ';
 3470:                 $checkedon{$item} = ' ';
 3471:             }
 3472:         }
 3473:     }
 3474:     if ($onclick) {
 3475:         $onclick = ' onclick="'.$onclick.'"';
 3476:     }
 3477:     foreach my $item (@{$toggles}) {
 3478:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3479:         $datatable .=
 3480:             '<tr'.$css_class.'><td valign="top">'.
 3481:             '<span class="LC_nobreak">'.$choices->{$item}.
 3482:             '</span></td>';
 3483:         if ($align eq 'left') {
 3484:             $datatable .= '<td class="LC_left_item">';
 3485:         } else {
 3486:             $datatable .= '<td class="LC_right_item">';
 3487:         }
 3488:         $datatable .=
 3489:             '<span class="LC_nobreak">'.
 3490:             '<label><input type="radio" name="'.
 3491:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 3492:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 3493:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 3494:             '</span>'.$additional.
 3495:             '</td>'.
 3496:             '</tr>';
 3497:         $itemcount ++;
 3498:     }
 3499:     return ($datatable,$itemcount);
 3500: }
 3501: 
 3502: sub print_coursedefaults {
 3503:     my ($position,$dom,$settings,$rowtotal) = @_;
 3504:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 3505:     my $itemcount = 1;
 3506:     my %choices =  &Apache::lonlocal::texthash (
 3507:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 3508:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 3509:         coursecredits        => 'Credits can be specified for courses',
 3510:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 3511:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 3512:         texengine            => 'Default method to display mathematics',
 3513:         postsubmit           => 'Disable submit button/keypress following student submission',
 3514:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 3515:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 3516:     );
 3517:     my %staticdefaults = (
 3518:                            anonsurvey_threshold => 10,
 3519:                            uploadquota          => 500,
 3520:                            postsubmit           => 60,
 3521:                            mysqltables          => 172800,
 3522:                          );
 3523:     if ($position eq 'top') {
 3524:         %defaultchecked = (
 3525:                             'uselcmath'       => 'on',
 3526:                             'usejsme'         => 'on',
 3527:                             'canclone'        => 'none',
 3528:                           );
 3529:         @toggles = ('uselcmath','usejsme');
 3530:         my $deftex = $Apache::lonnet::deftex;
 3531:         if (ref($settings) eq 'HASH') {
 3532:             if ($settings->{'texengine'}) {
 3533:                 if ($settings->{'texengine'} =~ /^(MathJax|mimetex|tth)$/) {
 3534:                     $deftex = $settings->{'texengine'};
 3535:                 }
 3536:             }
 3537:         }
 3538:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3539:         my $mathdisp = '<tr'.$css_class.'><td style="vertical-align: top">'.
 3540:                        '<span class="LC_nobreak">'.$choices{'texengine'}.
 3541:                        '</span></td><td class="LC_right_item">'.
 3542:                        '<select name="texengine">'."\n";
 3543:         my %texoptions = (
 3544:                             MathJax  => 'MathJax',
 3545:                             mimetex  => &mt('Convert to Images'),
 3546:                             tth      => &mt('TeX to HTML'),
 3547:                          );
 3548:         foreach my $renderer ('MathJax','mimetex','tth') {
 3549:             my $selected = '';
 3550:             if ($renderer eq $deftex) {
 3551:                 $selected = ' selected="selected"';
 3552:             }
 3553:             $mathdisp .= '<option value="'.$renderer.'"'.$selected.'>'.$texoptions{$renderer}.'</option>'."\n";
 3554:         }
 3555:         $mathdisp .= '</select></td></tr>'."\n";
 3556:         $itemcount ++;
 3557:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3558:                                                      \%choices,$itemcount);
 3559:         $datatable = $mathdisp.$datatable;
 3560:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3561:         $datatable .=
 3562:             '<tr'.$css_class.'><td valign="top">'.
 3563:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 3564:             '</span></td><td class="LC_left_item">';
 3565:         my $currcanclone = 'none';
 3566:         my $onclick;
 3567:         my @cloneoptions = ('none','domain');
 3568:         my %clonetitles = (
 3569:                              none     => 'No additional course requesters',
 3570:                              domain   => "Any course requester in course's domain",
 3571:                              instcode => 'Course requests for official courses ...',
 3572:                           );
 3573:         my (%codedefaults,@code_order,@posscodes);
 3574:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 3575:                                                     \@code_order) eq 'ok') {
 3576:             if (@code_order > 0) {
 3577:                 push(@cloneoptions,'instcode');
 3578:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 3579:             }
 3580:         }
 3581:         if (ref($settings) eq 'HASH') {
 3582:             if ($settings->{'canclone'}) {
 3583:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 3584:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 3585:                         if (@code_order > 0) {
 3586:                             $currcanclone = 'instcode';
 3587:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 3588:                         }
 3589:                     }
 3590:                 } elsif ($settings->{'canclone'} eq 'domain') {
 3591:                     $currcanclone = $settings->{'canclone'};
 3592:                 }
 3593:             }
 3594:         }
 3595:         foreach my $option (@cloneoptions) {
 3596:             my ($checked,$additional);
 3597:             if ($currcanclone eq $option) {
 3598:                 $checked = ' checked="checked"';
 3599:             }
 3600:             if ($option eq 'instcode') {
 3601:                 if (@code_order) {
 3602:                     my $show = 'none';
 3603:                     if ($checked) {
 3604:                         $show = 'block';
 3605:                     }
 3606:                     $additional = '<div id="cloneinstcode" style="display:'.$show.'" />'.
 3607:                                   &mt('Institutional codes for new and cloned course have identical:').
 3608:                                   '<br />';
 3609:                     foreach my $item (@code_order) {
 3610:                         my $codechk;
 3611:                         if ($checked) {
 3612:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 3613:                                 $codechk = ' checked="checked"';
 3614:                             }
 3615:                         }
 3616:                         $additional .= '<label>'.
 3617:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 3618:                                        $item.'</label>';
 3619:                     }
 3620:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 3621:                 }
 3622:             }
 3623:             $datatable .=
 3624:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 3625:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 3626:                 '</label>&nbsp;'.$additional.'</span><br />';
 3627:         }
 3628:         $datatable .= '</td>'.
 3629:                       '</tr>';
 3630:         $itemcount ++;
 3631:     } else {
 3632:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3633:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 3634:         my $currusecredits = 0;
 3635:         my $postsubmitclient = 1;
 3636:         my @types = ('official','unofficial','community','textbook');
 3637:         if (ref($settings) eq 'HASH') {
 3638:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 3639:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 3640:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 3641:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 3642:                 }
 3643:             }
 3644:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 3645:                 foreach my $type (@types) {
 3646:                     next if ($type eq 'community');
 3647:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 3648:                     if ($defcredits{$type} ne '') {
 3649:                         $currusecredits = 1;
 3650:                     }
 3651:                 }
 3652:             }
 3653:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 3654:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 3655:                     $postsubmitclient = 0;
 3656:                     foreach my $type (@types) {
 3657:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3658:                     }
 3659:                 } else {
 3660:                     foreach my $type (@types) {
 3661:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 3662:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 3663:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 3664:                             } else {
 3665:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3666:                             }
 3667:                         } else {
 3668:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3669:                         }
 3670:                     }
 3671:                 }
 3672:             } else {
 3673:                 foreach my $type (@types) {
 3674:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3675:                 }
 3676:             }
 3677:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 3678:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 3679:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 3680:                 }
 3681:             } else {
 3682:                 foreach my $type (@types) {
 3683:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 3684:                 }
 3685:             }
 3686:         } else {
 3687:             foreach my $type (@types) {
 3688:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3689:             }
 3690:         }
 3691:         if (!$currdefresponder) {
 3692:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 3693:         } elsif ($currdefresponder < 1) {
 3694:             $currdefresponder = 1;
 3695:         }
 3696:         foreach my $type (@types) {
 3697:             if ($curruploadquota{$type} eq '') {
 3698:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 3699:             }
 3700:         }
 3701:         $datatable .=
 3702:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3703:                 $choices{'anonsurvey_threshold'}.
 3704:                 '</span></td>'.
 3705:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 3706:                 '<input type="text" name="anonsurvey_threshold"'.
 3707:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 3708:                 '</td></tr>'."\n";
 3709:         $itemcount ++;
 3710:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3711:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3712:                       $choices{'uploadquota'}.
 3713:                       '</span></td>'.
 3714:                       '<td align="right" class="LC_right_item">'.
 3715:                       '<table><tr>';
 3716:         foreach my $type (@types) {
 3717:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 3718:                            '<input type="text" name="uploadquota_'.$type.'"'.
 3719:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 3720:         }
 3721:         $datatable .= '</tr></table></td></tr>'."\n";
 3722:         $itemcount ++;
 3723:         my $onclick = "toggleDisplay(this.form,'credits');";
 3724:         my $display = 'none';
 3725:         if ($currusecredits) {
 3726:             $display = 'block';
 3727:         }
 3728:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 3729:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 3730:         foreach my $type (@types) {
 3731:             next if ($type eq 'community');
 3732:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 3733:                            '<input type="text" name="'.$type.'_credits"'.
 3734:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 3735:         }
 3736:         $additional .= '</tr></table></div>'."\n";
 3737:         %defaultchecked = ('coursecredits' => 'off');
 3738:         @toggles = ('coursecredits');
 3739:         my $current = {
 3740:                         'coursecredits' => $currusecredits,
 3741:                       };
 3742:         (my $table,$itemcount) =
 3743:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 3744:                                \%choices,$itemcount,$onclick,$additional,'left');
 3745:         $datatable .= $table;
 3746:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 3747:         my $display = 'none';
 3748:         if ($postsubmitclient) {
 3749:             $display = 'block';
 3750:         }
 3751:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 3752:                       &mt('Number of seconds submit is disabled').'<br />'.
 3753:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 3754:                       '<table><tr>';
 3755:         foreach my $type (@types) {
 3756:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 3757:                            '<input type="text" name="'.$type.'_timeout" value="'.
 3758:                            $deftimeout{$type}.'" size="5" /></td>';
 3759:         }
 3760:         $additional .= '</tr></table></div>'."\n";
 3761:         %defaultchecked = ('postsubmit' => 'on');
 3762:         @toggles = ('postsubmit');
 3763:         $current = {
 3764:                        'postsubmit' => $postsubmitclient,
 3765:                    };
 3766:         ($table,$itemcount) =
 3767:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 3768:                                \%choices,$itemcount,$onclick,$additional,'left');
 3769:         $datatable .= $table;
 3770:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3771:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3772:                       $choices{'mysqltables'}.
 3773:                       '</span></td>'.
 3774:                       '<td align="right" class="LC_right_item">'.
 3775:                       '<table><tr>';
 3776:         foreach my $type (@types) {
 3777:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 3778:                            '<input type="text" name="mysqltables_'.$type.'"'.
 3779:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 3780:         }
 3781:         $datatable .= '</tr></table></td></tr>'."\n";
 3782:         $itemcount ++;
 3783: 
 3784:     }
 3785:     $$rowtotal += $itemcount;
 3786:     return $datatable;
 3787: }
 3788: 
 3789: sub print_selfenrollment {
 3790:     my ($position,$dom,$settings,$rowtotal) = @_;
 3791:     my ($css_class,$datatable);
 3792:     my $itemcount = 1;
 3793:     my @types = ('official','unofficial','community','textbook');
 3794:     if (($position eq 'top') || ($position eq 'middle')) {
 3795:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 3796:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 3797:         my @rows;
 3798:         my $key;
 3799:         if ($position eq 'top') {
 3800:             $key = 'admin'; 
 3801:             if (ref($rowsref) eq 'ARRAY') {
 3802:                 @rows = @{$rowsref};
 3803:             }
 3804:         } elsif ($position eq 'middle') {
 3805:             $key = 'default';
 3806:             @rows = ('types','registered','approval','limit');
 3807:         }
 3808:         foreach my $row (@rows) {
 3809:             if (defined($titlesref->{$row})) {
 3810:                 $itemcount ++;
 3811:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3812:                 $datatable .= '<tr'.$css_class.'>'.
 3813:                               '<td>'.$titlesref->{$row}.'</td>'.
 3814:                               '<td class="LC_left_item">'.
 3815:                               '<table><tr>';
 3816:                 my (%current,%currentcap);
 3817:                 if (ref($settings) eq 'HASH') {
 3818:                     if (ref($settings->{$key}) eq 'HASH') {
 3819:                         foreach my $type (@types) {
 3820:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 3821:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 3822:                             }
 3823:                             if (($row eq 'limit') && ($key eq 'default')) {
 3824:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 3825:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 3826:                                 }
 3827:                             }
 3828:                         }
 3829:                     }
 3830:                 }
 3831:                 my %roles = (
 3832:                              '0' => &Apache::lonnet::plaintext('dc'),
 3833:                             ); 
 3834:             
 3835:                 foreach my $type (@types) {
 3836:                     unless (($row eq 'registered') && ($key eq 'default')) {
 3837:                         $datatable .= '<th>'.&mt($type).'</th>';
 3838:                     }
 3839:                 }
 3840:                 unless (($row eq 'registered') && ($key eq 'default')) {
 3841:                     $datatable .= '</tr><tr>';
 3842:                 }
 3843:                 foreach my $type (@types) {
 3844:                     if ($type eq 'community') {
 3845:                         $roles{'1'} = &mt('Community personnel');
 3846:                     } else {
 3847:                         $roles{'1'} = &mt('Course personnel');
 3848:                     }
 3849:                     $datatable .= '<td style="vertical-align: top">';
 3850:                     if ($position eq 'top') {
 3851:                         my %checked;
 3852:                         if ($current{$type} eq '0') {
 3853:                             $checked{'0'} = ' checked="checked"';
 3854:                         } else {
 3855:                             $checked{'1'} = ' checked="checked"';
 3856:                         }
 3857:                         foreach my $role ('1','0') {
 3858:                             $datatable .= '<span class="LC_nobreak"><label>'.
 3859:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 3860:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 3861:                                           $roles{$role}.'</label></span> ';
 3862:                         }
 3863:                     } else {
 3864:                         if ($row eq 'types') {
 3865:                             my %checked;
 3866:                             if ($current{$type} =~ /^(all|dom)$/) {
 3867:                                 $checked{$1} = ' checked="checked"';
 3868:                             } else {
 3869:                                 $checked{''} = ' checked="checked"';
 3870:                             }
 3871:                             foreach my $val ('','dom','all') {
 3872:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3873:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3874:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3875:                             }
 3876:                         } elsif ($row eq 'registered') {
 3877:                             my %checked;
 3878:                             if ($current{$type} eq '1') {
 3879:                                 $checked{'1'} = ' checked="checked"';
 3880:                             } else {
 3881:                                 $checked{'0'} = ' checked="checked"';
 3882:                             }
 3883:                             foreach my $val ('0','1') {
 3884:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3885:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3886:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3887:                             }
 3888:                         } elsif ($row eq 'approval') {
 3889:                             my %checked;
 3890:                             if ($current{$type} =~ /^([12])$/) {
 3891:                                 $checked{$1} = ' checked="checked"';
 3892:                             } else {
 3893:                                 $checked{'0'} = ' checked="checked"';
 3894:                             }
 3895:                             for my $val (0..2) {
 3896:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3897:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3898:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3899:                             }
 3900:                         } elsif ($row eq 'limit') {
 3901:                             my %checked;
 3902:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 3903:                                 $checked{$1} = ' checked="checked"';
 3904:                             } else {
 3905:                                 $checked{'none'} = ' checked="checked"';
 3906:                             }
 3907:                             my $cap;
 3908:                             if ($currentcap{$type} =~ /^\d+$/) {
 3909:                                 $cap = $currentcap{$type};
 3910:                             }
 3911:                             foreach my $val ('none','allstudents','selfenrolled') {
 3912:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3913:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3914:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3915:                             }
 3916:                             $datatable .= '<br />'.
 3917:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 3918:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 3919:                                           '</span>'; 
 3920:                         }
 3921:                     }
 3922:                     $datatable .= '</td>';
 3923:                 }
 3924:                 $datatable .= '</tr>';
 3925:             }
 3926:             $datatable .= '</table></td></tr>';
 3927:         }
 3928:     } elsif ($position eq 'bottom') {
 3929:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 3930:     }
 3931:     $$rowtotal += $itemcount;
 3932:     return $datatable;
 3933: }
 3934: 
 3935: sub print_validation_rows {
 3936:     my ($caller,$dom,$settings,$rowtotal) = @_;
 3937:     my ($itemsref,$namesref,$fieldsref);
 3938:     if ($caller eq 'selfenroll') { 
 3939:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 3940:     } elsif ($caller eq 'requestcourses') {
 3941:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 3942:     }
 3943:     my %currvalidation;
 3944:     if (ref($settings) eq 'HASH') {
 3945:         if (ref($settings->{'validation'}) eq 'HASH') {
 3946:             %currvalidation = %{$settings->{'validation'}};
 3947:         }
 3948:     }
 3949:     my $datatable;
 3950:     my $itemcount = 0;
 3951:     foreach my $item (@{$itemsref}) {
 3952:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3953:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3954:                       $namesref->{$item}.
 3955:                       '</span></td>'.
 3956:                       '<td class="LC_left_item">';
 3957:         if (($item eq 'url') || ($item eq 'button')) {
 3958:             $datatable .= '<span class="LC_nobreak">'.
 3959:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 3960:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 3961:         } elsif ($item eq 'fields') {
 3962:             my @currfields;
 3963:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 3964:                 @currfields = @{$currvalidation{$item}};
 3965:             }
 3966:             foreach my $field (@{$fieldsref}) {
 3967:                 my $check = '';
 3968:                 if (grep(/^\Q$field\E$/,@currfields)) {
 3969:                     $check = ' checked="checked"';
 3970:                 }
 3971:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3972:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 3973:                               ' value="'.$field.'"'.$check.' />'.$field.
 3974:                               '</label></span> ';
 3975:             }
 3976:         } elsif ($item eq 'markup') {
 3977:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5">'.
 3978:                            $currvalidation{$item}.
 3979:                               '</textarea>';
 3980:         }
 3981:         $datatable .= '</td></tr>'."\n";
 3982:         if (ref($rowtotal)) {
 3983:             $itemcount ++;
 3984:         }
 3985:     }
 3986:     if ($caller eq 'requestcourses') {
 3987:         my %currhash;
 3988:         if (ref($settings) eq 'HASH') {
 3989:             if (ref($settings->{'validation'}) eq 'HASH') {
 3990:                 if ($settings->{'validation'}{'dc'} ne '') {
 3991:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 3992:                 }
 3993:             }
 3994:         }
 3995:         my $numinrow = 2;
 3996:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 3997:                                                        'validationdc',%currhash);
 3998:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3999:         $datatable .= '<tr'.$css_class.'><td>';
 4000:         if ($numdc > 1) {
 4001:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 4002:         } else {
 4003:             $datatable .=  &mt('Course creation processed as: ');
 4004:         }
 4005:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 4006:         $itemcount ++;
 4007:     }
 4008:     if (ref($rowtotal)) {
 4009:         $$rowtotal += $itemcount;
 4010:     }
 4011:     return $datatable;
 4012: }
 4013: 
 4014: sub print_usersessions {
 4015:     my ($position,$dom,$settings,$rowtotal) = @_;
 4016:     my ($css_class,$datatable,%checked,%choices);
 4017:     my (%by_ip,%by_location,@intdoms);
 4018:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 4019: 
 4020:     my @alldoms = &Apache::lonnet::all_domains();
 4021:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 4022:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4023:     my %altids = &id_for_thisdom(%servers);
 4024:     my $itemcount = 1;
 4025:     if ($position eq 'top') {
 4026:         if (keys(%serverhomes) > 1) {
 4027:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 4028:             my $curroffloadnow;
 4029:             if (ref($settings) eq 'HASH') {
 4030:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 4031:                     $curroffloadnow = $settings->{'offloadnow'};
 4032:                 }
 4033:             }
 4034:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$curroffloadnow,$rowtotal);
 4035:         } else {
 4036:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 4037:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 4038:         }
 4039:     } else {
 4040:         if (keys(%by_location) == 0) {
 4041:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 4042:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 4043:         } else {
 4044:             my %lt = &usersession_titles();
 4045:             my $numinrow = 5;
 4046:             my $prefix;
 4047:             my @types;
 4048:             if ($position eq 'bottom') {
 4049:                 $prefix = 'remote';
 4050:                 @types = ('version','excludedomain','includedomain');
 4051:             } else {
 4052:                 $prefix = 'hosted';
 4053:                 @types = ('excludedomain','includedomain');
 4054:             }
 4055:             my (%current,%checkedon,%checkedoff);
 4056:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 4057:             my @locations = sort(keys(%by_location));
 4058:             foreach my $type (@types) {
 4059:                 $checkedon{$type} = '';
 4060:                 $checkedoff{$type} = ' checked="checked"';
 4061:             }
 4062:             if (ref($settings) eq 'HASH') {
 4063:                 if (ref($settings->{$prefix}) eq 'HASH') {
 4064:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 4065:                         $current{$key} = $settings->{$prefix}{$key};
 4066:                         if ($key eq 'version') {
 4067:                             if ($current{$key} ne '') {
 4068:                                 $checkedon{$key} = ' checked="checked"';
 4069:                                 $checkedoff{$key} = '';
 4070:                             }
 4071:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 4072:                             $checkedon{$key} = ' checked="checked"';
 4073:                             $checkedoff{$key} = '';
 4074:                         }
 4075:                     }
 4076:                 }
 4077:             }
 4078:             foreach my $type (@types) {
 4079:                 next if ($type ne 'version' && !@locations);
 4080:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4081:                 $datatable .= '<tr'.$css_class.'>
 4082:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 4083:                                <span class="LC_nobreak">&nbsp;
 4084:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 4085:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 4086:                 if ($type eq 'version') {
 4087:                     my $selector = '<select name="'.$prefix.'_version">';
 4088:                     foreach my $version (@lcversions) {
 4089:                         my $selected = '';
 4090:                         if ($current{'version'} eq $version) {
 4091:                             $selected = ' selected="selected"';
 4092:                         }
 4093:                         $selector .= ' <option value="'.$version.'"'.
 4094:                                      $selected.'>'.$version.'</option>';
 4095:                     }
 4096:                     $selector .= '</select> ';
 4097:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 4098:                 } else {
 4099:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 4100:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 4101:                                  ' />'.('&nbsp;'x2).
 4102:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 4103:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 4104:                                  "\n".
 4105:                                  '</div><div><table>';
 4106:                     my $rem;
 4107:                     for (my $i=0; $i<@locations; $i++) {
 4108:                         my ($showloc,$value,$checkedtype);
 4109:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 4110:                             my $ip = $by_location{$locations[$i]}->[0];
 4111:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 4112:                                  $value = join(':',@{$by_ip{$ip}});
 4113:                                 $showloc = join(', ',@{$by_ip{$ip}});
 4114:                                 if (ref($current{$type}) eq 'ARRAY') {
 4115:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 4116:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 4117:                                             $checkedtype = ' checked="checked"';
 4118:                                             last;
 4119:                                         }
 4120:                                     }
 4121:                                 }
 4122:                             }
 4123:                         }
 4124:                         $rem = $i%($numinrow);
 4125:                         if ($rem == 0) {
 4126:                             if ($i > 0) {
 4127:                                 $datatable .= '</tr>';
 4128:                             }
 4129:                             $datatable .= '<tr>';
 4130:                         }
 4131:                         $datatable .= '<td class="LC_left_item">'.
 4132:                                       '<span class="LC_nobreak"><label>'.
 4133:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 4134:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 4135:                                       '</label></span></td>';
 4136:                     }
 4137:                     $rem = @locations%($numinrow);
 4138:                     my $colsleft = $numinrow - $rem;
 4139:                     if ($colsleft > 1 ) {
 4140:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4141:                                       '&nbsp;</td>';
 4142:                     } elsif ($colsleft == 1) {
 4143:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4144:                     }
 4145:                     $datatable .= '</tr></table>';
 4146:                 }
 4147:                 $datatable .= '</td></tr>';
 4148:                 $itemcount ++;
 4149:             }
 4150:         }
 4151:     }
 4152:     $$rowtotal += $itemcount;
 4153:     return $datatable;
 4154: }
 4155: 
 4156: sub build_location_hashes {
 4157:     my ($intdoms,$by_ip,$by_location) = @_;
 4158:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 4159:                   (ref($by_location) eq 'HASH')); 
 4160:     my %iphost = &Apache::lonnet::get_iphost();
 4161:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 4162:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 4163:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 4164:         foreach my $id (@{$iphost{$primary_ip}}) {
 4165:             my $intdom = &Apache::lonnet::internet_dom($id);
 4166:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 4167:                 push(@{$intdoms},$intdom);
 4168:             }
 4169:         }
 4170:     }
 4171:     foreach my $ip (keys(%iphost)) {
 4172:         if (ref($iphost{$ip}) eq 'ARRAY') {
 4173:             foreach my $id (@{$iphost{$ip}}) {
 4174:                 my $location = &Apache::lonnet::internet_dom($id);
 4175:                 if ($location) {
 4176:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 4177:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4178:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 4179:                             push(@{$by_ip->{$ip}},$location);
 4180:                         }
 4181:                     } else {
 4182:                         $by_ip->{$ip} = [$location];
 4183:                     }
 4184:                 }
 4185:             }
 4186:         }
 4187:     }
 4188:     foreach my $ip (sort(keys(%{$by_ip}))) {
 4189:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4190:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 4191:             my $first = $by_ip->{$ip}->[0];
 4192:             if (ref($by_location->{$first}) eq 'ARRAY') {
 4193:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 4194:                     push(@{$by_location->{$first}},$ip);
 4195:                 }
 4196:             } else {
 4197:                 $by_location->{$first} = [$ip];
 4198:             }
 4199:         }
 4200:     }
 4201:     return;
 4202: }
 4203: 
 4204: sub current_offloads_to {
 4205:     my ($dom,$settings,$servers) = @_;
 4206:     my (%spareid,%otherdomconfigs);
 4207:     if (ref($servers) eq 'HASH') {
 4208:         foreach my $lonhost (sort(keys(%{$servers}))) {
 4209:             my $gotspares;
 4210:             if (ref($settings) eq 'HASH') {
 4211:                 if (ref($settings->{'spares'}) eq 'HASH') {
 4212:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 4213:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 4214:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 4215:                         $gotspares = 1;
 4216:                     }
 4217:                 }
 4218:             }
 4219:             unless ($gotspares) {
 4220:                 my $gotspares;
 4221:                 my $serverhomeID =
 4222:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 4223:                 my $serverhomedom =
 4224:                     &Apache::lonnet::host_domain($serverhomeID);
 4225:                 if ($serverhomedom ne $dom) {
 4226:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 4227:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4228:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4229:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4230:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4231:                                 $gotspares = 1;
 4232:                             }
 4233:                         }
 4234:                     } else {
 4235:                         $otherdomconfigs{$serverhomedom} =
 4236:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 4237:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 4238:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4239:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4240:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 4241:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4242:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4243:                                         $gotspares = 1;
 4244:                                     }
 4245:                                 }
 4246:                             }
 4247:                         }
 4248:                     }
 4249:                 }
 4250:             }
 4251:             unless ($gotspares) {
 4252:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4253:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4254:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4255:                } else {
 4256:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 4257:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 4258:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4259:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4260:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4261:                     } else {
 4262:                         my %what = (
 4263:                              spareid => 1,
 4264:                         );
 4265:                         my ($result,$returnhash) = 
 4266:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 4267:                         if ($result eq 'ok') { 
 4268:                             if (ref($returnhash) eq 'HASH') {
 4269:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 4270:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 4271:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 4272:                                 }
 4273:                             }
 4274:                         }
 4275:                     }
 4276:                 }
 4277:             }
 4278:         }
 4279:     }
 4280:     return %spareid;
 4281: }
 4282: 
 4283: sub spares_row {
 4284:     my ($dom,$servers,$spareid,$serverhomes,$altids,$curroffloadnow,$rowtotal) = @_;
 4285:     my $css_class;
 4286:     my $numinrow = 4;
 4287:     my $itemcount = 1;
 4288:     my $datatable;
 4289:     my %typetitles = &sparestype_titles();
 4290:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 4291:         foreach my $server (sort(keys(%{$servers}))) {
 4292:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 4293:             my ($othercontrol,$serverdom);
 4294:             if ($serverhome ne $server) {
 4295:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 4296:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 4297:             } else {
 4298:                 $serverdom = &Apache::lonnet::host_domain($server);
 4299:                 if ($serverdom ne $dom) {
 4300:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 4301:                 }
 4302:             }
 4303:             next unless (ref($spareid->{$server}) eq 'HASH');
 4304:             my $checkednow;
 4305:             if (ref($curroffloadnow) eq 'HASH') {
 4306:                 if ($curroffloadnow->{$server}) {
 4307:                     $checkednow = ' checked="checked"';
 4308:                 }
 4309:             }
 4310:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4311:             $datatable .= '<tr'.$css_class.'>
 4312:                            <td rowspan="2">
 4313:                             <span class="LC_nobreak">'.
 4314:                           &mt('[_1] when busy, offloads to:'
 4315:                               ,'<b>'.$server.'</b>').'</span><br />'.
 4316:                           '<span class="LC_nobreak">'."\n".
 4317:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 4318:                           '&nbsp;'.&mt('Switch active users on next access').'</label></span>'.
 4319:                           "\n";
 4320:             my (%current,%canselect);
 4321:             my @choices = 
 4322:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 4323:             foreach my $type ('primary','default') {
 4324:                 if (ref($spareid->{$server}) eq 'HASH') {
 4325:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 4326:                         my @spares = @{$spareid->{$server}{$type}};
 4327:                         if (@spares > 0) {
 4328:                             if ($othercontrol) {
 4329:                                 $current{$type} = join(', ',@spares);
 4330:                             } else {
 4331:                                 $current{$type} .= '<table>';
 4332:                                 my $numspares = scalar(@spares);
 4333:                                 for (my $i=0;  $i<@spares; $i++) {
 4334:                                     my $rem = $i%($numinrow);
 4335:                                     if ($rem == 0) {
 4336:                                         if ($i > 0) {
 4337:                                             $current{$type} .= '</tr>';
 4338:                                         }
 4339:                                         $current{$type} .= '<tr>';
 4340:                                     }
 4341:                                     $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;'.
 4342:                                                        $spareid->{$server}{$type}[$i].
 4343:                                                        '</label></td>'."\n";
 4344:                                 }
 4345:                                 my $rem = @spares%($numinrow);
 4346:                                 my $colsleft = $numinrow - $rem;
 4347:                                 if ($colsleft > 1 ) {
 4348:                                     $current{$type} .= '<td colspan="'.$colsleft.
 4349:                                                        '" class="LC_left_item">'.
 4350:                                                        '&nbsp;</td>';
 4351:                                 } elsif ($colsleft == 1) {
 4352:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 4353:                                 }
 4354:                                 $current{$type} .= '</tr></table>';
 4355:                             }
 4356:                         }
 4357:                     }
 4358:                     if ($current{$type} eq '') {
 4359:                         $current{$type} = &mt('None specified');
 4360:                     }
 4361:                     if ($othercontrol) {
 4362:                         if ($type eq 'primary') {
 4363:                             $canselect{$type} = $othercontrol;
 4364:                         }
 4365:                     } else {
 4366:                         $canselect{$type} = 
 4367:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 4368:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 4369:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 4370:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 4371:                         if (@choices > 0) {
 4372:                             foreach my $lonhost (@choices) {
 4373:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 4374:                             }
 4375:                         }
 4376:                         $canselect{$type} .= '</select>'."\n";
 4377:                     }
 4378:                 } else {
 4379:                     $current{$type} = &mt('Could not be determined');
 4380:                     if ($type eq 'primary') {
 4381:                         $canselect{$type} =  $othercontrol;
 4382:                     }
 4383:                 }
 4384:                 if ($type eq 'default') {
 4385:                     $datatable .= '<tr'.$css_class.'>';
 4386:                 }
 4387:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 4388:                               '<td>'.$current{$type}.'</td>'."\n".
 4389:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 4390:             }
 4391:             $itemcount ++;
 4392:         }
 4393:     }
 4394:     $$rowtotal += $itemcount;
 4395:     return $datatable;
 4396: }
 4397: 
 4398: sub possible_newspares {
 4399:     my ($server,$currspares,$serverhomes,$altids) = @_;
 4400:     my $serverhostname = &Apache::lonnet::hostname($server);
 4401:     my %excluded;
 4402:     if ($serverhostname ne '') {
 4403:         %excluded = (
 4404:                        $serverhostname => 1,
 4405:                     );
 4406:     }
 4407:     if (ref($currspares) eq 'HASH') {
 4408:         foreach my $type (keys(%{$currspares})) {
 4409:             if (ref($currspares->{$type}) eq 'ARRAY') {
 4410:                 if (@{$currspares->{$type}} > 0) {
 4411:                     foreach my $curr (@{$currspares->{$type}}) {
 4412:                         my $hostname = &Apache::lonnet::hostname($curr);
 4413:                         $excluded{$hostname} = 1;
 4414:                     }
 4415:                 }
 4416:             }
 4417:         }
 4418:     }
 4419:     my @choices;
 4420:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 4421:         if (keys(%{$serverhomes}) > 1) {
 4422:             foreach my $name (sort(keys(%{$serverhomes}))) {
 4423:                 unless ($excluded{$name}) {
 4424:                     if (exists($altids->{$serverhomes->{$name}})) {
 4425:                         push(@choices,$altids->{$serverhomes->{$name}});
 4426:                     } else {
 4427:                         push(@choices,$serverhomes->{$name});
 4428:                     }
 4429:                 }
 4430:             }
 4431:         }
 4432:     }
 4433:     return sort(@choices);
 4434: }
 4435: 
 4436: sub print_loadbalancing {
 4437:     my ($dom,$settings,$rowtotal) = @_;
 4438:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 4439:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 4440:     my $numinrow = 1;
 4441:     my $datatable;
 4442:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4443:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
 4444:     if (ref($settings) eq 'HASH') {
 4445:         %existing = %{$settings};
 4446:     }
 4447:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 4448:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 4449:                                   \%currtargets,\%currrules,\%currcookies);
 4450:     } else {
 4451:         return;
 4452:     }
 4453:     my ($othertitle,$usertypes,$types) =
 4454:         &Apache::loncommon::sorted_inst_types($dom);
 4455:     my $rownum = 8;
 4456:     if (ref($types) eq 'ARRAY') {
 4457:         $rownum += scalar(@{$types});
 4458:     }
 4459:     my @css_class = ('LC_odd_row','LC_even_row');
 4460:     my $balnum = 0;
 4461:     my $islast;
 4462:     my (@toshow,$disabledtext);
 4463:     if (keys(%currbalancer) > 0) {
 4464:         @toshow = sort(keys(%currbalancer));
 4465:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 4466:             push(@toshow,'');
 4467:         }
 4468:     } else {
 4469:         @toshow = ('');
 4470:         $disabledtext = &mt('No existing load balancer');
 4471:     }
 4472:     foreach my $lonhost (@toshow) {
 4473:         if ($balnum == scalar(@toshow)-1) {
 4474:             $islast = 1;
 4475:         } else {
 4476:             $islast = 0;
 4477:         }
 4478:         my $cssidx = $balnum%2;
 4479:         my $targets_div_style = 'display: none';
 4480:         my $disabled_div_style = 'display: block';
 4481:         my $homedom_div_style = 'display: none';
 4482:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 4483:                       '<td rowspan="'.$rownum.'" valign="top">'.
 4484:                       '<p>';
 4485:         if ($lonhost eq '') {
 4486:             $datatable .= '<span class="LC_nobreak">';
 4487:             if (keys(%currbalancer) > 0) {
 4488:                 $datatable .= &mt('Add balancer:');
 4489:             } else {
 4490:                 $datatable .= &mt('Enable balancer:');
 4491:             }
 4492:             $datatable .= '&nbsp;'.
 4493:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 4494:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 4495:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 4496:                           '<option value="" selected="selected">'.&mt('None').
 4497:                           '</option>'."\n";
 4498:             foreach my $server (sort(keys(%servers))) {
 4499:                 next if ($currbalancer{$server});
 4500:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 4501:             }
 4502:             $datatable .=
 4503:                 '</select>'."\n".
 4504:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 4505:         } else {
 4506:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 4507:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 4508:                            &mt('Stop balancing').'</label>'.
 4509:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 4510:             $targets_div_style = 'display: block';
 4511:             $disabled_div_style = 'display: none';
 4512:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 4513:                 $homedom_div_style = 'display: block';
 4514:             }
 4515:         }
 4516:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 4517:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 4518:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 4519:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 4520:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 4521:         my @sparestypes = ('primary','default');
 4522:         my %typetitles = &sparestype_titles();
 4523:         my %hostherechecked = (
 4524:                                   no => ' checked="checked"',
 4525:                               );
 4526:         my %balcookiechecked = (
 4527:                                   no => ' checked="checked"',
 4528:                                );
 4529:         foreach my $sparetype (@sparestypes) {
 4530:             my $targettable;
 4531:             for (my $i=0; $i<$numspares; $i++) {
 4532:                 my $checked;
 4533:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 4534:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 4535:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 4536:                             $checked = ' checked="checked"';
 4537:                         }
 4538:                     }
 4539:                 }
 4540:                 my ($chkboxval,$disabled);
 4541:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 4542:                     $chkboxval = $spares[$i];
 4543:                 }
 4544:                 if (exists($currbalancer{$spares[$i]})) {
 4545:                     $disabled = ' disabled="disabled"';
 4546:                 }
 4547:                 $targettable .=
 4548:                     '<td><span class="LC_nobreak"><label>'.
 4549:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 4550:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 4551:                     '</span></label></span></td>';
 4552:                 my $rem = $i%($numinrow);
 4553:                 if ($rem == 0) {
 4554:                     if (($i > 0) && ($i < $numspares-1)) {
 4555:                         $targettable .= '</tr>';
 4556:                     }
 4557:                     if ($i < $numspares-1) {
 4558:                         $targettable .= '<tr>';
 4559:                     }
 4560:                 }
 4561:             }
 4562:             if ($targettable ne '') {
 4563:                 my $rem = $numspares%($numinrow);
 4564:                 my $colsleft = $numinrow - $rem;
 4565:                 if ($colsleft > 1 ) {
 4566:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4567:                                     '&nbsp;</td>';
 4568:                 } elsif ($colsleft == 1) {
 4569:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 4570:                 }
 4571:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 4572:                                '<table><tr>'.$targettable.'</tr></table><br />';
 4573:             }
 4574:             $hostherechecked{$sparetype} = '';
 4575:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 4576:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 4577:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 4578:                         $hostherechecked{$sparetype} = ' checked="checked"';
 4579:                         $hostherechecked{'no'} = '';
 4580:                     }
 4581:                 }
 4582:             }
 4583:         }
 4584:         if ($currcookies{$lonhost}) {
 4585:             %balcookiechecked = (
 4586:                                     yes => ' checked="checked"',
 4587:                                 );
 4588:         }
 4589:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 4590:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 4591:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 4592:         foreach my $sparetype (@sparestypes) {
 4593:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 4594:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 4595:                           '</i></label><br />';
 4596:         }
 4597:         $datatable .= &mt('Use balancer cookie').'<br />'.
 4598:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="1"'.
 4599:                       $balcookiechecked{'yes'}.' />'.&mt('Yes').'</label><br />'.
 4600:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="0"'.
 4601:                       $balcookiechecked{'no'}.' />'.&mt('No').'</label><br />'.
 4602:                       '</div></td></tr>'.
 4603:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 4604:                                            $othertitle,$usertypes,$types,\%servers,
 4605:                                            \%currbalancer,$lonhost,
 4606:                                            $targets_div_style,$homedom_div_style,
 4607:                                            $css_class[$cssidx],$balnum,$islast);
 4608:         $$rowtotal += $rownum;
 4609:         $balnum ++;
 4610:     }
 4611:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 4612:     return $datatable;
 4613: }
 4614: 
 4615: sub get_loadbalancers_config {
 4616:     my ($servers,$existing,$currbalancer,$currtargets,$currrules,$currcookies) = @_;
 4617:     return unless ((ref($servers) eq 'HASH') &&
 4618:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 4619:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH') &&
 4620:                    (ref($currcookies) eq 'HASH'));
 4621:     if (keys(%{$existing}) > 0) {
 4622:         my $oldlonhost;
 4623:         foreach my $key (sort(keys(%{$existing}))) {
 4624:             if ($key eq 'lonhost') {
 4625:                 $oldlonhost = $existing->{'lonhost'};
 4626:                 $currbalancer->{$oldlonhost} = 1;
 4627:             } elsif ($key eq 'targets') {
 4628:                 if ($oldlonhost) {
 4629:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 4630:                 }
 4631:             } elsif ($key eq 'rules') {
 4632:                 if ($oldlonhost) {
 4633:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 4634:                 }
 4635:             } elsif (ref($existing->{$key}) eq 'HASH') {
 4636:                 $currbalancer->{$key} = 1;
 4637:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 4638:                 $currrules->{$key} = $existing->{$key}{'rules'};
 4639:                 if ($existing->{$key}{'cookie'}) {
 4640:                     $currcookies->{$key} = 1;
 4641:                 }
 4642:             }
 4643:         }
 4644:     } else {
 4645:         my ($balancerref,$targetsref) =
 4646:                 &Apache::lonnet::get_lonbalancer_config($servers);
 4647:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 4648:             foreach my $server (sort(keys(%{$balancerref}))) {
 4649:                 $currbalancer->{$server} = 1;
 4650:                 $currtargets->{$server} = $targetsref->{$server};
 4651:             }
 4652:         }
 4653:     }
 4654:     return;
 4655: }
 4656: 
 4657: sub loadbalancing_rules {
 4658:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 4659:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 4660:         $css_class,$balnum,$islast) = @_;
 4661:     my $output;
 4662:     my $num = 0;
 4663:     my ($alltypes,$othertypes,$titles) =
 4664:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 4665:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 4666:         foreach my $type (@{$alltypes}) {
 4667:             $num ++;
 4668:             my $current;
 4669:             if (ref($currrules) eq 'HASH') {
 4670:                 $current = $currrules->{$type};
 4671:             }
 4672:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 4673:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 4674:                     $current = '';
 4675:                 }
 4676:             }
 4677:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 4678:                                              $servers,$currbalancer,$lonhost,$dom,
 4679:                                              $targets_div_style,$homedom_div_style,
 4680:                                              $css_class,$balnum,$num,$islast);
 4681:         }
 4682:     }
 4683:     return $output;
 4684: }
 4685: 
 4686: sub loadbalancing_titles {
 4687:     my ($dom,$intdom,$usertypes,$types) = @_;
 4688:     my %othertypes = (
 4689:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 4690:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 4691:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 4692:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 4693:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 4694:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 4695:                      );
 4696:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 4697:     my @available;
 4698:     if (ref($types) eq 'ARRAY') {
 4699:         @available = @{$types};
 4700:     }
 4701:     unless (grep(/^default$/,@available)) {
 4702:         push(@available,'default');
 4703:     }
 4704:     unshift(@alltypes,@available);
 4705:     my %titles;
 4706:     foreach my $type (@alltypes) {
 4707:         if ($type =~ /^_LC_/) {
 4708:             $titles{$type} = $othertypes{$type};
 4709:         } elsif ($type eq 'default') {
 4710:             $titles{$type} = &mt('All users from [_1]',$dom);
 4711:             if (ref($types) eq 'ARRAY') {
 4712:                 if (@{$types} > 0) {
 4713:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 4714:                 }
 4715:             }
 4716:         } elsif (ref($usertypes) eq 'HASH') {
 4717:             $titles{$type} = $usertypes->{$type};
 4718:         }
 4719:     }
 4720:     return (\@alltypes,\%othertypes,\%titles);
 4721: }
 4722: 
 4723: sub loadbalance_rule_row {
 4724:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 4725:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 4726:     my @rulenames;
 4727:     my %ruletitles = &offloadtype_text();
 4728:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 4729:         @rulenames = ('balancer','offloadedto','specific');
 4730:     } else {
 4731:         @rulenames = ('default','homeserver');
 4732:         if ($type eq '_LC_external') {
 4733:             push(@rulenames,'externalbalancer');
 4734:         } else {
 4735:             push(@rulenames,'specific');
 4736:         }
 4737:         push(@rulenames,'none');
 4738:     }
 4739:     my $style = $targets_div_style;
 4740:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 4741:         $style = $homedom_div_style;
 4742:     }
 4743:     my $space;
 4744:     if ($islast && $num == 1) {
 4745:         $space = '<div display="inline-block">&nbsp;</div>';
 4746:     }
 4747:     my $output =
 4748:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 4749:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 4750:         '<td valaign="top">'.$space.
 4751:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 4752:     for (my $i=0; $i<@rulenames; $i++) {
 4753:         my $rule = $rulenames[$i];
 4754:         my ($checked,$extra);
 4755:         if ($rulenames[$i] eq 'default') {
 4756:             $rule = '';
 4757:         }
 4758:         if ($rulenames[$i] eq 'specific') {
 4759:             if (ref($servers) eq 'HASH') {
 4760:                 my $default;
 4761:                 if (($current ne '') && (exists($servers->{$current}))) {
 4762:                     $checked = ' checked="checked"';
 4763:                 }
 4764:                 unless ($checked) {
 4765:                     $default = ' selected="selected"';
 4766:                 }
 4767:                 $extra =
 4768:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 4769:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 4770:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 4771:                     '<option value=""'.$default.'></option>'."\n";
 4772:                 foreach my $server (sort(keys(%{$servers}))) {
 4773:                     if (ref($currbalancer) eq 'HASH') {
 4774:                         next if (exists($currbalancer->{$server}));
 4775:                     }
 4776:                     my $selected;
 4777:                     if ($server eq $current) {
 4778:                         $selected = ' selected="selected"';
 4779:                     }
 4780:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 4781:                 }
 4782:                 $extra .= '</select>';
 4783:             }
 4784:         } elsif ($rule eq $current) {
 4785:             $checked = ' checked="checked"';
 4786:         }
 4787:         $output .= '<span class="LC_nobreak"><label>'.
 4788:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 4789:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 4790:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 4791:                    ')"'.$checked.' />&nbsp;';
 4792:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 4793:             $output .= $ruletitles{'particular'};
 4794:         } else {
 4795:             $output .= $ruletitles{$rulenames[$i]};
 4796:         }
 4797:         $output .= '</label>'.$extra.'</span><br />'."\n";
 4798:     }
 4799:     $output .= '</div></td></tr>'."\n";
 4800:     return $output;
 4801: }
 4802: 
 4803: sub offloadtype_text {
 4804:     my %ruletitles = &Apache::lonlocal::texthash (
 4805:            'default'          => 'Offloads to default destinations',
 4806:            'homeserver'       => "Offloads to user's home server",
 4807:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 4808:            'specific'         => 'Offloads to specific server',
 4809:            'none'             => 'No offload',
 4810:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 4811:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 4812:            'particular'       => 'Session hosted (after re-auth) on server:',
 4813:     );
 4814:     return %ruletitles;
 4815: }
 4816: 
 4817: sub sparestype_titles {
 4818:     my %typestitles = &Apache::lonlocal::texthash (
 4819:                           'primary' => 'primary',
 4820:                           'default' => 'default',
 4821:                       );
 4822:     return %typestitles;
 4823: }
 4824: 
 4825: sub contact_titles {
 4826:     my %titles = &Apache::lonlocal::texthash (
 4827:                    'supportemail'    => 'Support E-mail address',
 4828:                    'adminemail'      => 'Default Server Admin E-mail address',
 4829:                    'errormail'       => 'Error reports to be e-mailed to',
 4830:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 4831:                    'helpdeskmail'    => "Helpdesk requests for this domain's users",
 4832:                    'otherdomsmail'   => 'Helpdesk requests for other (unconfigured) domains',
 4833:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 4834:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 4835:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 4836:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 4837:                    'hostipmail'      => 'E-mail from nightly check of hostname/IP network changes',
 4838:                  );
 4839:     my %short_titles = &Apache::lonlocal::texthash (
 4840:                            adminemail   => 'Admin E-mail address',
 4841:                            supportemail => 'Support E-mail',
 4842:                        );   
 4843:     return (\%titles,\%short_titles);
 4844: }
 4845: 
 4846: sub helpform_fields {
 4847:     my %titles =  &Apache::lonlocal::texthash (
 4848:                        'username'   => 'Name',
 4849:                        'user'       => 'Username/domain',
 4850:                        'phone'      => 'Phone',
 4851:                        'cc'         => 'Cc e-mail',
 4852:                        'course'     => 'Course Details',
 4853:                        'section'    => 'Sections',
 4854:                        'screenshot' => 'File upload',
 4855:     );
 4856:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 4857:     my %possoptions = (
 4858:                         username     => ['yes','no','req'],
 4859:                         phone        => ['yes','no','req'],
 4860:                         user         => ['yes','no'],
 4861:                         cc           => ['yes','no'],
 4862:                         course       => ['yes','no'],
 4863:                         section      => ['yes','no'],
 4864:                         screenshot   => ['yes','no'],
 4865:                       );
 4866:     my %fieldoptions = &Apache::lonlocal::texthash (
 4867:                          'yes'  => 'Optional',
 4868:                          'req'  => 'Required',
 4869:                          'no'   => "Not shown",
 4870:     );
 4871:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 4872: }
 4873: 
 4874: sub tool_titles {
 4875:     my %titles = &Apache::lonlocal::texthash (
 4876:                      aboutme    => 'Personal web page',
 4877:                      blog       => 'Blog',
 4878:                      webdav     => 'WebDAV',
 4879:                      portfolio  => 'Portfolio',
 4880:                      official   => 'Official courses (with institutional codes)',
 4881:                      unofficial => 'Unofficial courses',
 4882:                      community  => 'Communities',
 4883:                      textbook   => 'Textbook courses',
 4884:                  );
 4885:     return %titles;
 4886: }
 4887: 
 4888: sub courserequest_titles {
 4889:     my %titles = &Apache::lonlocal::texthash (
 4890:                                    official   => 'Official',
 4891:                                    unofficial => 'Unofficial',
 4892:                                    community  => 'Communities',
 4893:                                    textbook   => 'Textbook',
 4894:                                    norequest  => 'Not allowed',
 4895:                                    approval   => 'Approval by Dom. Coord.',
 4896:                                    validate   => 'With validation',
 4897:                                    autolimit  => 'Numerical limit',
 4898:                                    unlimited  => '(blank for unlimited)',
 4899:                  );
 4900:     return %titles;
 4901: }
 4902: 
 4903: sub authorrequest_titles {
 4904:     my %titles = &Apache::lonlocal::texthash (
 4905:                                    norequest  => 'Not allowed',
 4906:                                    approval   => 'Approval by Dom. Coord.',
 4907:                                    automatic  => 'Automatic approval',
 4908:                  );
 4909:     return %titles;
 4910: }
 4911: 
 4912: sub courserequest_conditions {
 4913:     my %conditions = &Apache::lonlocal::texthash (
 4914:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 4915:        validate   => '(Processing of request subject to institutional validation).',
 4916:                  );
 4917:     return %conditions;
 4918: }
 4919: 
 4920: 
 4921: sub print_usercreation {
 4922:     my ($position,$dom,$settings,$rowtotal) = @_;
 4923:     my $numinrow = 4;
 4924:     my $datatable;
 4925:     if ($position eq 'top') {
 4926:         $$rowtotal ++;
 4927:         my $rowcount = 0;
 4928:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 4929:         if (ref($rules) eq 'HASH') {
 4930:             if (keys(%{$rules}) > 0) {
 4931:                 $datatable .= &user_formats_row('username',$settings,$rules,
 4932:                                                 $ruleorder,$numinrow,$rowcount);
 4933:                 $$rowtotal ++;
 4934:                 $rowcount ++;
 4935:             }
 4936:         }
 4937:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 4938:         if (ref($idrules) eq 'HASH') {
 4939:             if (keys(%{$idrules}) > 0) {
 4940:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 4941:                                                 $idruleorder,$numinrow,$rowcount);
 4942:                 $$rowtotal ++;
 4943:                 $rowcount ++;
 4944:             }
 4945:         }
 4946:         if ($rowcount == 0) {
 4947:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 4948:             $$rowtotal ++;
 4949:             $rowcount ++;
 4950:         }
 4951:     } elsif ($position eq 'middle') {
 4952:         my @creators = ('author','course','requestcrs');
 4953:         my ($rules,$ruleorder) =
 4954:             &Apache::lonnet::inst_userrules($dom,'username');
 4955:         my %lt = &usercreation_types();
 4956:         my %checked;
 4957:         if (ref($settings) eq 'HASH') {
 4958:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 4959:                 foreach my $item (@creators) {
 4960:                     $checked{$item} = $settings->{'cancreate'}{$item};
 4961:                 }
 4962:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 4963:                 foreach my $item (@creators) {
 4964:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 4965:                         $checked{$item} = 'none';
 4966:                     }
 4967:                 }
 4968:             }
 4969:         }
 4970:         my $rownum = 0;
 4971:         foreach my $item (@creators) {
 4972:             $rownum ++;
 4973:             if ($checked{$item} eq '') {
 4974:                 $checked{$item} = 'any';
 4975:             }
 4976:             my $css_class;
 4977:             if ($rownum%2) {
 4978:                 $css_class = '';
 4979:             } else {
 4980:                 $css_class = ' class="LC_odd_row" ';
 4981:             }
 4982:             $datatable .= '<tr'.$css_class.'>'.
 4983:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 4984:                          '</span></td><td align="right">';
 4985:             my @options = ('any');
 4986:             if (ref($rules) eq 'HASH') {
 4987:                 if (keys(%{$rules}) > 0) {
 4988:                     push(@options,('official','unofficial'));
 4989:                 }
 4990:             }
 4991:             push(@options,'none');
 4992:             foreach my $option (@options) {
 4993:                 my $type = 'radio';
 4994:                 my $check = ' ';
 4995:                 if ($checked{$item} eq $option) {
 4996:                     $check = ' checked="checked" ';
 4997:                 } 
 4998:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4999:                               '<input type="'.$type.'" name="can_createuser_'.
 5000:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 5001:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 5002:             }
 5003:             $datatable .= '</td></tr>';
 5004:         }
 5005:     } else {
 5006:         my @contexts = ('author','course','domain');
 5007:         my @authtypes = ('int','krb4','krb5','loc');
 5008:         my %checked;
 5009:         if (ref($settings) eq 'HASH') {
 5010:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 5011:                 foreach my $item (@contexts) {
 5012:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 5013:                         foreach my $auth (@authtypes) {
 5014:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 5015:                                 $checked{$item}{$auth} = ' checked="checked" ';
 5016:                             }
 5017:                         }
 5018:                     }
 5019:                 }
 5020:             }
 5021:         } else {
 5022:             foreach my $item (@contexts) {
 5023:                 foreach my $auth (@authtypes) {
 5024:                     $checked{$item}{$auth} = ' checked="checked" ';
 5025:                 }
 5026:             }
 5027:         }
 5028:         my %title = &context_names();
 5029:         my %authname = &authtype_names();
 5030:         my $rownum = 0;
 5031:         my $css_class; 
 5032:         foreach my $item (@contexts) {
 5033:             if ($rownum%2) {
 5034:                 $css_class = '';
 5035:             } else {
 5036:                 $css_class = ' class="LC_odd_row" ';
 5037:             }
 5038:             $datatable .=   '<tr'.$css_class.'>'.
 5039:                             '<td>'.$title{$item}.
 5040:                             '</td><td class="LC_left_item">'.
 5041:                             '<span class="LC_nobreak">';
 5042:             foreach my $auth (@authtypes) {
 5043:                 $datatable .= '<label>'. 
 5044:                               '<input type="checkbox" name="'.$item.'_auth" '.
 5045:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 5046:                               $authname{$auth}.'</label>&nbsp;';
 5047:             }
 5048:             $datatable .= '</span></td></tr>';
 5049:             $rownum ++;
 5050:         }
 5051:         $$rowtotal += $rownum;
 5052:     }
 5053:     return $datatable;
 5054: }
 5055: 
 5056: sub print_selfcreation {
 5057:     my ($position,$dom,$settings,$rowtotal) = @_;
 5058:     my (@selfcreate,$createsettings,$processing,$emailoptions,$emailverified,
 5059:         $emaildomain,$datatable);
 5060:     if (ref($settings) eq 'HASH') {
 5061:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 5062:             $createsettings = $settings->{'cancreate'};
 5063:             if (ref($createsettings) eq 'HASH') {
 5064:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 5065:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 5066:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 5067:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 5068:                         @selfcreate = ('email','login','sso');
 5069:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 5070:                         @selfcreate = ($createsettings->{'selfcreate'});
 5071:                     }
 5072:                 }
 5073:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 5074:                     $processing = $createsettings->{'selfcreateprocessing'};
 5075:                 }
 5076:                 if (ref($createsettings->{'emailoptions'}) eq 'HASH') {
 5077:                     $emailoptions = $createsettings->{'emailoptions'};
 5078:                 }
 5079:                 if (ref($createsettings->{'emailverified'}) eq 'HASH') {
 5080:                     $emailverified = $createsettings->{'emailverified'};
 5081:                 }
 5082:                 if (ref($createsettings->{'emaildomain'}) eq 'HASH') {
 5083:                     $emaildomain = $createsettings->{'emaildomain'};
 5084:                 }
 5085:             }
 5086:         }
 5087:     }
 5088:     my %radiohash;
 5089:     my $numinrow = 4;
 5090:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 5091:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5092:     if ($position eq 'top') {
 5093:         my %choices = &Apache::lonlocal::texthash (
 5094:                                                       cancreate_login      => 'Institutional Login',
 5095:                                                       cancreate_sso        => 'Institutional Single Sign On',
 5096:                                                   );
 5097:         my @toggles = sort(keys(%choices));
 5098:         my %defaultchecked = (
 5099:                                'cancreate_login' => 'off',
 5100:                                'cancreate_sso'   => 'off',
 5101:                              );
 5102:         my ($onclick,$itemcount);
 5103:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5104:                                                      \%choices,$itemcount,$onclick);
 5105:         $$rowtotal += $itemcount;
 5106: 
 5107:         if (ref($usertypes) eq 'HASH') {
 5108:             if (keys(%{$usertypes}) > 0) {
 5109:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 5110:                                              $dom,$numinrow,$othertitle,
 5111:                                              'statustocreate',$rowtotal);
 5112:                 $$rowtotal ++;
 5113:             }
 5114:         }
 5115:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 5116:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5117:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 5118:         my $rem;
 5119:         my $numperrow = 2;
 5120:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 5121:         $datatable .= '<tr'.$css_class.'>'.
 5122:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 5123:                      '<td class="LC_left_item">'."\n".
 5124:                      '<table>'."\n";
 5125:         for (my $i=0; $i<@fields; $i++) {
 5126:             $rem = $i%($numperrow);
 5127:             if ($rem == 0) {
 5128:                 if ($i > 0) {
 5129:                     $datatable .= '</tr>';
 5130:                 }
 5131:                 $datatable .= '<tr>';
 5132:             }
 5133:             my $currval;
 5134:             if (ref($createsettings) eq 'HASH') {
 5135:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 5136:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 5137:                 }
 5138:             }
 5139:             $datatable .= '<td class="LC_left_item">'.
 5140:                           '<span class="LC_nobreak">'.
 5141:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 5142:                           'value="'.$currval.'" size="10" />&nbsp;'.
 5143:                           $fieldtitles{$fields[$i]}.'</span></td>';
 5144:         }
 5145:         my $colsleft = $numperrow - $rem;
 5146:         if ($colsleft > 1 ) {
 5147:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5148:                          '&nbsp;</td>';
 5149:         } elsif ($colsleft == 1) {
 5150:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 5151:         }
 5152:         $datatable .= '</tr></table></td></tr>';
 5153:         $$rowtotal ++;
 5154:     } elsif ($position eq 'middle') {
 5155:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 5156:         my @posstypes;
 5157:         if (ref($types) eq 'ARRAY') {
 5158:             @posstypes = @{$types};
 5159:         }
 5160:         unless (grep(/^default$/,@posstypes)) {
 5161:             push(@posstypes,'default');
 5162:         }
 5163:         my %usertypeshash;
 5164:         if (ref($usertypes) eq 'HASH') {
 5165:             %usertypeshash = %{$usertypes};
 5166:         }
 5167:         $usertypeshash{'default'} = $othertitle;
 5168:         foreach my $status (@posstypes) {
 5169:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 5170:                                                    $numinrow,$$rowtotal,\%usertypeshash);
 5171:             $$rowtotal ++;
 5172:         }
 5173:     } else {
 5174:         my %choices = &Apache::lonlocal::texthash (
 5175:                           'cancreate_email' => 'Non-institutional username (via e-mail verification)',
 5176:                                                   );
 5177:         my @toggles = sort(keys(%choices));
 5178:         my %defaultchecked = (
 5179:                                'cancreate_email' => 'off',
 5180:                              );
 5181:         my $customclass = 'LC_selfcreate_email';
 5182:         my $classprefix = 'LC_canmodify_emailusername_';
 5183:         my $optionsprefix = 'LC_options_emailusername_';
 5184:         my $display = 'none';
 5185:         my $rowstyle = 'display:none';
 5186:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 5187:             $display = 'block';
 5188:             $rowstyle = 'display:table-row';
 5189:         }
 5190:         my $onclick = "toggleRows(this.form,'cancreate_email','selfassign','$customclass','$classprefix','$optionsprefix');";
 5191:         ($datatable,$$rowtotal) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5192:                                                      \%choices,$$rowtotal,$onclick);
 5193:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal,$customclass,
 5194:                                          $rowstyle);
 5195:         $$rowtotal ++;
 5196:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal,$customclass,
 5197:                                       $rowstyle);
 5198:         $$rowtotal ++;
 5199:         my (@ordered,@posstypes,%usertypeshash);
 5200:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5201:         my ($emailrules,$emailruleorder) =
 5202:             &Apache::lonnet::inst_userrules($dom,'email');
 5203:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 5204:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 5205:         if (ref($types) eq 'ARRAY') {
 5206:             @posstypes = @{$types};
 5207:         }
 5208:         if (@posstypes) {
 5209:             unless (grep(/^default$/,@posstypes)) {
 5210:                 push(@posstypes,'default');
 5211:             }
 5212:             if (ref($usertypes) eq 'HASH') {
 5213:                 %usertypeshash = %{$usertypes};
 5214:             }
 5215:             my $currassign;
 5216:             if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
 5217:                 $currassign = {
 5218:                                   selfassign => $domdefaults{'inststatusguest'},
 5219:                               };
 5220:                 @ordered = @{$domdefaults{'inststatusguest'}};
 5221:             } else {
 5222:                 $currassign = { selfassign => [] };
 5223:             }
 5224:             my $onclicktypes = "toggleDataRow(this.form,'selfassign','$customclass','$optionsprefix',);".
 5225:                                "toggleDataRow(this.form,'selfassign','$customclass','$classprefix',1);";
 5226:             $datatable .= &insttypes_row($currassign,$types,$usertypes,$dom,
 5227:                                          $numinrow,$othertitle,'selfassign',
 5228:                                          $rowtotal,$onclicktypes,$customclass,
 5229:                                          $rowstyle);
 5230:             $$rowtotal ++;
 5231:             $usertypeshash{'default'} = $othertitle;
 5232:             foreach my $status (@posstypes) {
 5233:                 my $css_class;
 5234:                 if ($$rowtotal%2) {
 5235:                     $css_class = 'LC_odd_row ';
 5236:                 }
 5237:                 $css_class .= $customclass;
 5238:                 my $rowid = $optionsprefix.$status;
 5239:                 my $hidden = 1;
 5240:                 my $currstyle = 'display:none';
 5241:                 if (grep(/^\Q$status\E$/,@ordered)) {
 5242:                     $currstyle = $rowstyle;
 5243:                     $hidden = 0;
 5244:                 }
 5245:                 $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 5246:                                              $emailrules,$emailruleorder,$settings,$status,$rowid,
 5247:                                              $usertypeshash{$status},$css_class,$currstyle,$intdom);
 5248:                 unless ($hidden) {
 5249:                     $$rowtotal ++;
 5250:                 }
 5251:             }
 5252:         } else {
 5253:             my $css_class;
 5254:             if ($$rowtotal%2) {
 5255:                 $css_class = 'LC_odd_row ';
 5256:             }
 5257:             $css_class .= $customclass;
 5258:             $usertypeshash{'default'} = $othertitle;
 5259:             $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 5260:                                          $emailrules,$emailruleorder,$settings,'default','',
 5261:                                          $othertitle,$css_class,$rowstyle,$intdom);
 5262:             $$rowtotal ++;
 5263:         }
 5264:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 5265:         $numinrow = 1;
 5266:         if (@posstypes) {
 5267:             foreach my $status (@posstypes) {
 5268:                 my $rowid = $classprefix.$status;
 5269:                 my $datarowstyle = 'display:none';
 5270:                 if (grep(/^\Q$status\E$/,@ordered)) {
 5271:                     $datarowstyle = $rowstyle;
 5272:                 }
 5273:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 5274:                                                        $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 5275:                                                        $infotitles,$rowid,$customclass,$datarowstyle);
 5276:                 unless ($datarowstyle eq 'display:none') {
 5277:                     $$rowtotal ++;
 5278:                 }
 5279:             }
 5280:         } else {
 5281:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_default',$settings,
 5282:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 5283:                                                    $infotitles,'',$customclass,$rowstyle);
 5284:         }
 5285:     }
 5286:     return $datatable;
 5287: }
 5288: 
 5289: sub selfcreate_javascript {
 5290:     return <<"ENDSCRIPT";
 5291: 
 5292: <script type="text/javascript">
 5293: // <![CDATA[
 5294: 
 5295: function toggleRows(form,radio,checkbox,target,prefix,altprefix) {
 5296:     var x = document.getElementsByClassName(target);
 5297:     var insttypes = 0;
 5298:     var insttypeRegExp = new RegExp(prefix);
 5299:     if ((x.length != undefined) && (x.length > 0)) {
 5300:         if (form.elements[radio].length != undefined) {
 5301:             for (var i=0; i<form.elements[radio].length; i++) {
 5302:                 if (form.elements[radio][i].checked) {
 5303:                     if (form.elements[radio][i].value == 1) {
 5304:                         for (var j=0; j<x.length; j++) {
 5305:                             if (x[j].id == 'undefined') {
 5306:                                 x[j].style.display = 'table-row';
 5307:                             } else if (insttypeRegExp.test(x[j].id)) {
 5308:                                 insttypes ++;
 5309:                             } else {
 5310:                                 x[j].style.display = 'table-row';
 5311:                             }
 5312:                         }
 5313:                     } else {
 5314:                         for (var j=0; j<x.length; j++) {
 5315:                             x[j].style.display = 'none';
 5316:                         }
 5317:                     }
 5318:                     break;
 5319:                 }
 5320:             }
 5321:             if (insttypes > 0) {
 5322:                 toggleDataRow(form,checkbox,target,altprefix);
 5323:                 toggleDataRow(form,checkbox,target,prefix,1);
 5324:             }
 5325:         }
 5326:     }
 5327:     return;
 5328: }
 5329: 
 5330: function toggleDataRow(form,checkbox,target,prefix,docount) {
 5331:     if (form.elements[checkbox].length != undefined) {
 5332:         var count = 0;
 5333:         if (docount) {
 5334:             for (var i=0; i<form.elements[checkbox].length; i++) {
 5335:                 if (form.elements[checkbox][i].checked) {
 5336:                     count ++;
 5337:                 }
 5338:             }
 5339:         }
 5340:         for (var i=0; i<form.elements[checkbox].length; i++) {
 5341:             var type = form.elements[checkbox][i].value;
 5342:             if (document.getElementById(prefix+type)) {
 5343:                 if (form.elements[checkbox][i].checked) {
 5344:                     document.getElementById(prefix+type).style.display = 'table-row';
 5345:                     if (count % 2 == 1) {
 5346:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 5347:                     } else {
 5348:                         document.getElementById(prefix+type).className = target;
 5349:                     }
 5350:                     count ++;
 5351:                 } else {
 5352:                     document.getElementById(prefix+type).style.display = 'none';
 5353:                 }
 5354:             }
 5355:         }
 5356:     }
 5357:     return;
 5358: }
 5359: 
 5360: function toggleEmailOptions(form,radio,prefix,altprefix,status) {
 5361:     var caller = radio+'_'+status;
 5362:     if (form.elements[caller].length != undefined) {
 5363:         for (var i=0; i<form.elements[caller].length; i++) {
 5364:             if (form.elements[caller][i].checked) {
 5365:                 if (document.getElementById(altprefix+'_inst_'+status)) {
 5366:                     var curr = form.elements[caller][i].value;
 5367:                     if (prefix) {
 5368:                         document.getElementById(prefix+'_'+status).style.display = 'none';
 5369:                     }
 5370:                     document.getElementById(altprefix+'_inst_'+status).style.display = 'none';
 5371:                     document.getElementById(altprefix+'_noninst_'+status).style.display = 'none';
 5372:                     if (curr == 'custom') {
 5373:                         if (prefix) {
 5374:                             document.getElementById(prefix+'_'+status).style.display = 'inline';
 5375:                         }
 5376:                     } else if (curr == 'inst') {
 5377:                         document.getElementById(altprefix+'_inst_'+status).style.display = 'inline';
 5378:                     } else if (curr == 'noninst') {
 5379:                         document.getElementById(altprefix+'_noninst_'+status).style.display = 'inline';
 5380:                     }
 5381:                     break;
 5382:                 }
 5383:             }
 5384:         }
 5385:     }
 5386: }
 5387: 
 5388: // ]]>
 5389: </script>
 5390: 
 5391: ENDSCRIPT
 5392: }
 5393: 
 5394: sub noninst_users {
 5395:     my ($processing,$emailverified,$emailoptions,$emaildomain,$emailrules,
 5396:         $emailruleorder,$settings,$type,$rowid,$typetitle,$css_class,$rowstyle,$intdom) = @_;
 5397:     my $class = 'LC_left_item';
 5398:     if ($css_class) {
 5399:         $css_class = ' class="'.$css_class.'"';
 5400:     }
 5401:     if ($rowid) {
 5402:         $rowid = ' id="'.$rowid.'"';
 5403:     }
 5404:     if ($rowstyle) {
 5405:         $rowstyle = ' style="'.$rowstyle.'"';
 5406:     }
 5407:     my ($output,$description);
 5408:     if ($type eq 'default') {
 5409:         $description = &mt('Requests for: [_1]',$typetitle);
 5410:     } else {
 5411:         $description = &mt('Requests for: [_1] (status self-reported)',$typetitle);
 5412:     }
 5413:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 5414:               "<td>$description</td>\n".
 5415:               '<td class="'.$class.'" colspan="2">'.
 5416:               '<table><tr>';
 5417:     my %headers = &Apache::lonlocal::texthash(
 5418:               approve  => 'Processing',
 5419:               email    => 'E-mail',
 5420:               username => 'Username',
 5421:     );
 5422:     foreach my $item ('approve','email','username') {
 5423:         $output .= '<th>'.$headers{$item}.'</th>';
 5424:     }
 5425:     $output .= '</tr><tr>';
 5426:     foreach my $item ('approve','email','username') {
 5427:         $output .= '<td valign="top">';
 5428:         my (%choices,@options,$hashref,$defoption,$name,$onclick,$hascustom);
 5429:         if ($item eq 'approve') {
 5430:             %choices = &Apache::lonlocal::texthash (
 5431:                                                      automatic => 'Automatically approved',
 5432:                                                      approval  => 'Queued for approval',
 5433:                                                    );
 5434:             @options = ('automatic','approval');
 5435:             $hashref = $processing;
 5436:             $defoption = 'automatic';
 5437:             $name = 'cancreate_emailprocess_'.$type;
 5438:         } elsif ($item eq 'email') {
 5439:             %choices = &Apache::lonlocal::texthash (
 5440:                                                      any     => 'Any e-mail',
 5441:                                                      inst    => 'Institutional only',
 5442:                                                      noninst => 'Non-institutional only',
 5443:                                                      custom  => 'Custom restrictions',
 5444:                                                    );
 5445:             @options = ('any','inst','noninst');
 5446:             my $showcustom;
 5447:             if (ref($emailrules) eq 'HASH') {
 5448:                 if (keys(%{$emailrules}) > 0) {
 5449:                     push(@options,'custom');
 5450:                     $showcustom = 'cancreate_emailrule';
 5451:                     if (ref($settings) eq 'HASH') {
 5452:                         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
 5453:                             foreach my $rule (@{$settings->{'email_rule'}}) {
 5454:                                 if (exists($emailrules->{$rule})) {
 5455:                                     $hascustom ++;
 5456:                                 }
 5457:                             }
 5458:                         } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
 5459:                             if (ref($settings->{'email_rule'}{$type}) eq 'ARRAY') {
 5460:                                 foreach my $rule (@{$settings->{'email_rule'}{$type}}) {
 5461:                                     if (exists($emailrules->{$rule})) {
 5462:                                         $hascustom ++;
 5463:                                     }
 5464:                                 }
 5465:                             }
 5466:                         }
 5467:                     }
 5468:                 }
 5469:             }
 5470:             $onclick = ' onclick="toggleEmailOptions(this.form,'."'cancreate_emailoptions','$showcustom',".
 5471:                                                      "'cancreate_emaildomain','$type'".');"';
 5472:             $hashref = $emailoptions;
 5473:             $defoption = 'any';
 5474:             $name = 'cancreate_emailoptions_'.$type;
 5475:         } elsif ($item eq 'username') {
 5476:             %choices = &Apache::lonlocal::texthash (
 5477:                                                      all    => 'Same as e-mail',
 5478:                                                      first  => 'Omit @domain',
 5479:                                                      free   => 'Free to choose',
 5480:                                                    );
 5481:             @options = ('all','first','free');
 5482:             $hashref = $emailverified;
 5483:             $defoption = 'all';
 5484:             $name = 'cancreate_usernameoptions_'.$type;
 5485:         }
 5486:         foreach my $option (@options) {
 5487:             my $checked;
 5488:             if (ref($hashref) eq 'HASH') {
 5489:                 if ($type eq '') {
 5490:                     if (!exists($hashref->{'default'})) {
 5491:                         if ($option eq $defoption) {
 5492:                             $checked = ' checked="checked"';
 5493:                         }
 5494:                     } else {
 5495:                         if ($hashref->{'default'} eq $option) {
 5496:                             $checked = ' checked="checked"';
 5497:                         }
 5498:                     }
 5499:                 } else {
 5500:                     if (!exists($hashref->{$type})) {
 5501:                         if ($option eq $defoption) {
 5502:                             $checked = ' checked="checked"';
 5503:                         }
 5504:                     } else {
 5505:                         if ($hashref->{$type} eq $option) {
 5506:                             $checked = ' checked="checked"';
 5507:                         }
 5508:                     }
 5509:                 }
 5510:             } elsif (($item eq 'email') && ($hascustom)) {
 5511:                 if ($option eq 'custom') {
 5512:                     $checked = ' checked="checked"';
 5513:                 }
 5514:             } elsif ($option eq $defoption) {
 5515:                 $checked = ' checked="checked"';
 5516:             }
 5517:             $output .= '<span class="LC_nobreak"><label>'.
 5518:                        '<input type="radio" name="'.$name.'"'.
 5519:                        $checked.' value="'.$option.'"'.$onclick.' />'.
 5520:                        $choices{$option}.'</label></span><br />';
 5521:             if ($item eq 'email') {
 5522:                 if ($option eq 'custom') {
 5523:                     my $id = 'cancreate_emailrule_'.$type;
 5524:                     my $display = 'none';
 5525:                     if ($checked) {
 5526:                         $display = 'inline';
 5527:                     }
 5528:                     my $numinrow = 2;
 5529:                     $output .= '<fieldset id="'.$id.'" style="display:'.$display.';">'.
 5530:                                '<legend>'.&mt('Disallow').'</legend><table>'.
 5531:                                &user_formats_row('email',$settings,$emailrules,
 5532:                                                  $emailruleorder,$numinrow,'',$type);
 5533:                               '</table></fieldset>';
 5534:                 } elsif (($option eq 'inst') || ($option eq 'noninst')) {
 5535:                     my %text = &Apache::lonlocal::texthash (
 5536:                                                              inst    => 'must end:',
 5537:                                                              noninst => 'cannot end:',
 5538:                                                            );
 5539:                     my $value;
 5540:                     if (ref($emaildomain) eq 'HASH') {
 5541:                         if (ref($emaildomain->{$type}) eq 'HASH') {
 5542:                             $value = $emaildomain->{$type}->{$option};
 5543:                         }
 5544:                     }
 5545:                     if ($value eq '') {
 5546:                         $value = '@'.$intdom;
 5547:                     }
 5548:                     my $condition = 'cancreate_emaildomain_'.$option.'_'.$type;
 5549:                     my $display = 'none';
 5550:                     if ($checked) {
 5551:                         $display = 'inline';
 5552:                     }
 5553:                     $output .= '<div id="'.$condition.'" style="display:'.$display.';">'.
 5554:                                '<span class="LC_domprefs_email">'.$text{$option}.'</span> '.
 5555:                                '<input type="text" name="'.$condition.'" value="'.$value.'" size="10" />'.
 5556:                                '</div>';
 5557:                 }
 5558:             }
 5559:         }
 5560:         $output .= '</td>'."\n";
 5561:     }
 5562:     }
 5563:     $output .= "</tr></table></td></tr>\n";
 5564:     return $output;
 5565: }
 5566: 
 5567: sub captcha_choice {
 5568:     my ($context,$settings,$itemcount,$customcss,$rowstyle) = @_;
 5569:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 5570:         $vertext,$currver); 
 5571:     my %lt = &captcha_phrases();
 5572:     $keyentry = 'hidden';
 5573:     if ($context eq 'cancreate') {
 5574:         $rowname = &mt('CAPTCHA validation');
 5575:     } elsif ($context eq 'login') {
 5576:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 5577:     }
 5578:     if (ref($settings) eq 'HASH') {
 5579:         if ($settings->{'captcha'}) {
 5580:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 5581:         } else {
 5582:             $checked{'original'} = ' checked="checked"';
 5583:         }
 5584:         if ($settings->{'captcha'} eq 'recaptcha') {
 5585:             $pubtext = $lt{'pub'};
 5586:             $privtext = $lt{'priv'};
 5587:             $keyentry = 'text';
 5588:             $vertext = $lt{'ver'};
 5589:             $currver = $settings->{'recaptchaversion'};
 5590:             if ($currver ne '2') {
 5591:                 $currver = 1;
 5592:             }
 5593:         }
 5594:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 5595:             $currpub = $settings->{'recaptchakeys'}{'public'};
 5596:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 5597:         }
 5598:     } else {
 5599:         $checked{'original'} = ' checked="checked"';
 5600:     }
 5601:     my $css_class;
 5602:     if ($itemcount%2) {
 5603:         $css_class = 'LC_odd_row';
 5604:     }
 5605:     if ($customcss) {
 5606:         $css_class .= " $customcss";
 5607:     }
 5608:     $css_class =~ s/^\s+//;
 5609:     if ($css_class) {
 5610:         $css_class = ' class="'.$css_class.'"';
 5611:     }
 5612:     if ($rowstyle) {
 5613:         $css_class .= ' style="'.$rowstyle.'"';
 5614:     }
 5615:     my $output = '<tr'.$css_class.'>'.
 5616:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 5617:                  '<table><tr><td>'."\n";
 5618:     foreach my $option ('original','recaptcha','notused') {
 5619:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 5620:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 5621:                    $lt{$option}.'</label></span>';
 5622:         unless ($option eq 'notused') {
 5623:             $output .= ('&nbsp;'x2)."\n";
 5624:         }
 5625:     }
 5626: #
 5627: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 5628: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 5629: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 5630: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 5631: #
 5632:     $output .= '</td></tr>'."\n".
 5633:                '<tr><td class="LC_zero_height">'."\n".
 5634:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 5635:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 5636:                $currpub.'" size="40" /></span><br />'."\n".
 5637:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 5638:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 5639:                $currpriv.'" size="40" /></span><br />'.
 5640:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 5641:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 5642:                $currver.'" size="3" /></span><br />'.
 5643:                '</td></tr></table>'."\n".
 5644:                '</td></tr>';
 5645:     return $output;
 5646: }
 5647: 
 5648: sub user_formats_row {
 5649:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount,$status) = @_;
 5650:     my $output;
 5651:     my %text = (
 5652:                    'username' => 'new usernames',
 5653:                    'id'       => 'IDs',
 5654:                );
 5655:     unless ($type eq 'email') {
 5656:         my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 5657:         $output = '<tr '.$css_class.'>'.
 5658:                   '<td><span class="LC_nobreak">'.
 5659:                   &mt("Format rules to check for $text{$type}: ").
 5660:                   '</td><td class="LC_left_item" colspan="2"><table>';
 5661:     }
 5662:     my $rem;
 5663:     if (ref($ruleorder) eq 'ARRAY') {
 5664:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 5665:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 5666:                 my $rem = $i%($numinrow);
 5667:                 if ($rem == 0) {
 5668:                     if ($i > 0) {
 5669:                         $output .= '</tr>';
 5670:                     }
 5671:                     $output .= '<tr>';
 5672:                 }
 5673:                 my $check = ' ';
 5674:                 if (ref($settings) eq 'HASH') {
 5675:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 5676:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 5677:                             $check = ' checked="checked" ';
 5678:                         }
 5679:                     } elsif ((ref($settings->{$type.'_rule'}) eq 'HASH') && ($status ne '')) {
 5680:                         if (ref($settings->{$type.'_rule'}->{$status}) eq 'ARRAY') {
 5681:                             if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}->{$status}})) {
 5682:                                 $check = ' checked="checked" ';
 5683:                             }
 5684:                         }
 5685:                     }
 5686:                 }
 5687:                 my $name = $type.'_rule';
 5688:                 if ($type eq 'email') {
 5689:                     $name .= '_'.$status;
 5690:                 }
 5691:                 $output .= '<td class="LC_left_item">'.
 5692:                            '<span class="LC_nobreak"><label>'.
 5693:                            '<input type="checkbox" name="'.$name.'" '.
 5694:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 5695:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 5696:             }
 5697:         }
 5698:         $rem = @{$ruleorder}%($numinrow);
 5699:     }
 5700:     my $colsleft;
 5701:     if ($rem) {
 5702:         $colsleft = $numinrow - $rem;
 5703:     }
 5704:     if ($colsleft > 1 ) {
 5705:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5706:                    '&nbsp;</td>';
 5707:     } elsif ($colsleft == 1) {
 5708:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 5709:     }
 5710:     $output .= '</tr></table>';
 5711:     unless ($type eq 'email') {
 5712:         $output .= '</td></tr>';
 5713:     }
 5714:     return $output;
 5715: }
 5716: 
 5717: sub usercreation_types {
 5718:     my %lt = &Apache::lonlocal::texthash (
 5719:                     author     => 'When adding a co-author',
 5720:                     course     => 'When adding a user to a course',
 5721:                     requestcrs => 'When requesting a course',
 5722:                     any        => 'Any',
 5723:                     official   => 'Institutional only ',
 5724:                     unofficial => 'Non-institutional only',
 5725:                     none       => 'None',
 5726:     );
 5727:     return %lt;
 5728: }
 5729: 
 5730: sub selfcreation_types {
 5731:     my %lt = &Apache::lonlocal::texthash (
 5732:                     selfcreate => 'User creates own account',
 5733:                     any        => 'Any',
 5734:                     official   => 'Institutional only ',
 5735:                     unofficial => 'Non-institutional only',
 5736:                     email      => 'E-mail address',
 5737:                     login      => 'Institutional Login',
 5738:                     sso        => 'SSO',
 5739:              );
 5740: }
 5741: 
 5742: sub authtype_names {
 5743:     my %lt = &Apache::lonlocal::texthash(
 5744:                       int    => 'Internal',
 5745:                       krb4   => 'Kerberos 4',
 5746:                       krb5   => 'Kerberos 5',
 5747:                       loc    => 'Local',
 5748:                   );
 5749:     return %lt;
 5750: }
 5751: 
 5752: sub context_names {
 5753:     my %context_title = &Apache::lonlocal::texthash(
 5754:        author => 'Creating users when an Author',
 5755:        course => 'Creating users when in a course',
 5756:        domain => 'Creating users when a Domain Coordinator',
 5757:     );
 5758:     return %context_title;
 5759: }
 5760: 
 5761: sub print_usermodification {
 5762:     my ($position,$dom,$settings,$rowtotal) = @_;
 5763:     my $numinrow = 4;
 5764:     my ($context,$datatable,$rowcount);
 5765:     if ($position eq 'top') {
 5766:         $rowcount = 0;
 5767:         $context = 'author'; 
 5768:         foreach my $role ('ca','aa') {
 5769:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 5770:                                                    $numinrow,$rowcount);
 5771:             $$rowtotal ++;
 5772:             $rowcount ++;
 5773:         }
 5774:     } elsif ($position eq 'bottom') {
 5775:         $context = 'course';
 5776:         $rowcount = 0;
 5777:         foreach my $role ('st','ep','ta','in','cr') {
 5778:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 5779:                                                    $numinrow,$rowcount);
 5780:             $$rowtotal ++;
 5781:             $rowcount ++;
 5782:         }
 5783:     }
 5784:     return $datatable;
 5785: }
 5786: 
 5787: sub print_defaults {
 5788:     my ($position,$dom,$settings,$rowtotal) = @_;
 5789:     my $rownum = 0;
 5790:     my ($datatable,$css_class,$titles);
 5791:     unless ($position eq 'bottom') {
 5792:         $titles = &defaults_titles($dom);
 5793:     }
 5794:     if ($position eq 'top') {
 5795:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 5796:                      'datelocale_def','portal_def');
 5797:         my %defaults;
 5798:         if (ref($settings) eq 'HASH') {
 5799:             %defaults = %{$settings};
 5800:         } else {
 5801:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 5802:             foreach my $item (@items) {
 5803:                 $defaults{$item} = $domdefaults{$item};
 5804:             }
 5805:         }
 5806:         foreach my $item (@items) {
 5807:             if ($rownum%2) {
 5808:                 $css_class = '';
 5809:             } else {
 5810:                 $css_class = ' class="LC_odd_row" ';
 5811:             }
 5812:             $datatable .= '<tr'.$css_class.'>'.
 5813:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 5814:                           '</span></td><td class="LC_right_item" colspan="3">';
 5815:             if ($item eq 'auth_def') {
 5816:                 my @authtypes = ('internal','krb4','krb5','localauth');
 5817:                 my %shortauth = (
 5818:                                  internal => 'int',
 5819:                                  krb4 => 'krb4',
 5820:                                  krb5 => 'krb5',
 5821:                                  localauth  => 'loc'
 5822:                                 );
 5823:                 my %authnames = &authtype_names();
 5824:                 foreach my $auth (@authtypes) {
 5825:                     my $checked = ' ';
 5826:                     if ($defaults{$item} eq $auth) {
 5827:                         $checked = ' checked="checked" ';
 5828:                     }
 5829:                     $datatable .= '<label><input type="radio" name="'.$item.
 5830:                                   '" value="'.$auth.'"'.$checked.'/>'.
 5831:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 5832:                 }
 5833:             } elsif ($item eq 'timezone_def') {
 5834:                 my $includeempty = 1;
 5835:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 5836:             } elsif ($item eq 'datelocale_def') {
 5837:                 my $includeempty = 1;
 5838:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 5839:             } elsif ($item eq 'lang_def') {
 5840:                 my $includeempty = 1;
 5841:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 5842:             } else {
 5843:                 my $size;
 5844:                 if ($item eq 'portal_def') {
 5845:                     $size = ' size="25"';
 5846:                 }
 5847:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 5848:                               $defaults{$item}.'"'.$size.' />';
 5849:             }
 5850:             $datatable .= '</td></tr>';
 5851:             $rownum ++;
 5852:         }
 5853:     } elsif ($position eq 'middle') {
 5854:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 5855:         my %defaults;
 5856:         if (ref($settings) eq 'HASH') {
 5857:             %defaults = %{$settings};
 5858:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 5859:                 $defaults{'intauth_cost'} = 10;
 5860:             }
 5861:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 5862:                 $defaults{'intauth_check'} = 0;
 5863:             }
 5864:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 5865:                 $defaults{'intauth_switch'} = 0;
 5866:             }
 5867:         } else {
 5868:             %defaults = (
 5869:                           'intauth_cost'   => 10,
 5870:                           'intauth_check'  => 0,
 5871:                           'intauth_switch' => 0,
 5872:                         );
 5873:         }
 5874:         foreach my $item (@items) {
 5875:             if ($rownum%2) {
 5876:                 $css_class = '';
 5877:             } else {
 5878:                 $css_class = ' class="LC_odd_row" ';
 5879:             }
 5880:             $datatable .= '<tr'.$css_class.'>'.
 5881:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 5882:                           '</span></td><td class="LC_left_item" colspan="3">';
 5883:             if ($item eq 'intauth_switch') {
 5884:                 my @options = (0,1,2);
 5885:                 my %optiondesc = &Apache::lonlocal::texthash (
 5886:                                    0 => 'No',
 5887:                                    1 => 'Yes',
 5888:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 5889:                                  );
 5890:                 $datatable .= '<table width="100%">';
 5891:                 foreach my $option (@options) {
 5892:                     my $checked = ' ';
 5893:                     if ($defaults{$item} eq $option) {
 5894:                         $checked = ' checked="checked"';
 5895:                     }
 5896:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 5897:                                   '<label><input type="radio" name="'.$item.
 5898:                                   '" value="'.$option.'"'.$checked.' />'.
 5899:                                   $optiondesc{$option}.'</label></span></td></tr>';
 5900:                 }
 5901:                 $datatable .= '</table>';
 5902:             } elsif ($item eq 'intauth_check') {
 5903:                 my @options = (0,1,2);
 5904:                 my %optiondesc = &Apache::lonlocal::texthash (
 5905:                                    0 => 'No',
 5906:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 5907:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 5908:                                  );
 5909:                 $datatable .= '<table width="100%">';
 5910:                 foreach my $option (@options) {
 5911:                     my $checked = ' ';
 5912:                     my $onclick;
 5913:                     if ($defaults{$item} eq $option) {
 5914:                         $checked = ' checked="checked"';
 5915:                     }
 5916:                     if ($option == 2) {
 5917:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 5918:                     }
 5919:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 5920:                                   '<label><input type="radio" name="'.$item.
 5921:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 5922:                                   $optiondesc{$option}.'</label></span></td></tr>';
 5923:                 }
 5924:                 $datatable .= '</table>';
 5925:             } else {
 5926:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 5927:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />';
 5928:             }
 5929:             $datatable .= '</td></tr>';
 5930:             $rownum ++;
 5931:         }
 5932:     } else {
 5933:         my %defaults;
 5934:         if (ref($settings) eq 'HASH') {
 5935:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 5936:                 my $maxnum = @{$settings->{'inststatusorder'}};
 5937:                 for (my $i=0; $i<$maxnum; $i++) {
 5938:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 5939:                     my $item = $settings->{'inststatusorder'}->[$i];
 5940:                     my $title = $settings->{'inststatustypes'}->{$item};
 5941:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 5942:                     $datatable .= '<tr'.$css_class.'>'.
 5943:                                   '<td><span class="LC_nobreak">'.
 5944:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 5945:                     for (my $k=0; $k<=$maxnum; $k++) {
 5946:                         my $vpos = $k+1;
 5947:                         my $selstr;
 5948:                         if ($k == $i) {
 5949:                             $selstr = ' selected="selected" ';
 5950:                         }
 5951:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5952:                     }
 5953:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 5954:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 5955:                                   &mt('delete').'</span></td>'.
 5956:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed:').
 5957:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 5958:                                   '</span></td></tr>';
 5959:                 }
 5960:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 5961:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 5962:                 $datatable .= '<tr '.$css_class.'>'.
 5963:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 5964:                 for (my $k=0; $k<=$maxnum; $k++) {
 5965:                     my $vpos = $k+1;
 5966:                     my $selstr;
 5967:                     if ($k == $maxnum) {
 5968:                         $selstr = ' selected="selected" ';
 5969:                     }
 5970:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5971:                 }
 5972:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 5973:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 5974:                               '&nbsp;'.&mt('(new)').
 5975:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 5976:                               &mt('Name displayed:').
 5977:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 5978:                               '</tr>'."\n";
 5979:                 $rownum ++;
 5980:             }
 5981:         }
 5982:     }
 5983:     $$rowtotal += $rownum;
 5984:     return $datatable;
 5985: }
 5986: 
 5987: sub get_languages_hash {
 5988:     my %langchoices;
 5989:     foreach my $id (&Apache::loncommon::languageids()) {
 5990:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 5991:         if ($code ne '') {
 5992:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 5993:         }
 5994:     }
 5995:     return %langchoices;
 5996: }
 5997: 
 5998: sub defaults_titles {
 5999:     my ($dom) = @_;
 6000:     my %titles = &Apache::lonlocal::texthash (
 6001:                    'auth_def'      => 'Default authentication type',
 6002:                    'auth_arg_def'  => 'Default authentication argument',
 6003:                    'lang_def'      => 'Default language',
 6004:                    'timezone_def'  => 'Default timezone',
 6005:                    'datelocale_def' => 'Default locale for dates',
 6006:                    'portal_def'     => 'Portal/Default URL',
 6007:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
 6008:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
 6009:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
 6010:                  );
 6011:     if ($dom) {
 6012:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 6013:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 6014:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 6015:         $protocol = 'http' if ($protocol ne 'https');
 6016:         if ($uint_dom) {
 6017:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 6018:                                          $uint_dom);
 6019:         }
 6020:     }
 6021:     return (\%titles);
 6022: }
 6023: 
 6024: sub print_scantronformat {
 6025:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 6026:     my $itemcount = 1;
 6027:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 6028:         %confhash);
 6029:     my $switchserver = &check_switchserver($dom,$confname);
 6030:     my %lt = &Apache::lonlocal::texthash (
 6031:                 default => 'Default bubblesheet format file error',
 6032:                 custom  => 'Custom bubblesheet format file error',
 6033:              );
 6034:     my %scantronfiles = (
 6035:         default => 'default.tab',
 6036:         custom => 'custom.tab',
 6037:     );
 6038:     foreach my $key (keys(%scantronfiles)) {
 6039:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 6040:                               .$scantronfiles{$key};
 6041:     }
 6042:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 6043:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 6044:         if (!$switchserver) {
 6045:             my $servadm = $r->dir_config('lonAdmEMail');
 6046:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 6047:             if ($configuserok eq 'ok') {
 6048:                 if ($author_ok eq 'ok') {
 6049:                     my %legacyfile = (
 6050:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 6051:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 6052:                     );
 6053:                     my %md5chk;
 6054:                     foreach my $type (keys(%legacyfile)) {
 6055:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 6056:                         chomp($md5chk{$type});
 6057:                     }
 6058:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 6059:                         foreach my $type (keys(%legacyfile)) {
 6060:                             ($scantronurls{$type},my $error) = 
 6061:                                 &legacy_scantronformat($r,$dom,$confname,
 6062:                                                  $type,$legacyfile{$type},
 6063:                                                  $scantronurls{$type},
 6064:                                                  $scantronfiles{$type});
 6065:                             if ($error ne '') {
 6066:                                 $error{$type} = $error;
 6067:                             }
 6068:                         }
 6069:                         if (keys(%error) == 0) {
 6070:                             $is_custom = 1;
 6071:                             $confhash{'scantron'}{'scantronformat'} = 
 6072:                                 $scantronurls{'custom'};
 6073:                             my $putresult = 
 6074:                                 &Apache::lonnet::put_dom('configuration',
 6075:                                                          \%confhash,$dom);
 6076:                             if ($putresult ne 'ok') {
 6077:                                 $error{'custom'} = 
 6078:                                     '<span class="LC_error">'.
 6079:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 6080:                             }
 6081:                         }
 6082:                     } else {
 6083:                         ($scantronurls{'default'},my $error) =
 6084:                             &legacy_scantronformat($r,$dom,$confname,
 6085:                                           'default',$legacyfile{'default'},
 6086:                                           $scantronurls{'default'},
 6087:                                           $scantronfiles{'default'});
 6088:                         if ($error eq '') {
 6089:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 6090:                             my $putresult =
 6091:                                 &Apache::lonnet::put_dom('configuration',
 6092:                                                          \%confhash,$dom);
 6093:                             if ($putresult ne 'ok') {
 6094:                                 $error{'default'} =
 6095:                                     '<span class="LC_error">'.
 6096:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 6097:                             }
 6098:                         } else {
 6099:                             $error{'default'} = $error;
 6100:                         }
 6101:                     }
 6102:                 }
 6103:             }
 6104:         } else {
 6105:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 6106:         }
 6107:     }
 6108:     if (ref($settings) eq 'HASH') {
 6109:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 6110:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 6111:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 6112:                 $scantronurl = '';
 6113:             } else {
 6114:                 $scantronurl = $settings->{'scantronformat'};
 6115:             }
 6116:             $is_custom = 1;
 6117:         } else {
 6118:             $scantronurl = $scantronurls{'default'};
 6119:         }
 6120:     } else {
 6121:         if ($is_custom) {
 6122:             $scantronurl = $scantronurls{'custom'};
 6123:         } else {
 6124:             $scantronurl = $scantronurls{'default'};
 6125:         }
 6126:     }
 6127:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6128:     $datatable .= '<tr'.$css_class.'>';
 6129:     if (!$is_custom) {
 6130:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 6131:                       '<span class="LC_nobreak">';
 6132:         if ($scantronurl) {
 6133:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 6134:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 6135:         } else {
 6136:             $datatable = &mt('File unavailable for display');
 6137:         }
 6138:         $datatable .= '</span></td>';
 6139:         if (keys(%error) == 0) { 
 6140:             $datatable .= '<td valign="bottom">';
 6141:             if (!$switchserver) {
 6142:                 $datatable .= &mt('Upload:').'<br />';
 6143:             }
 6144:         } else {
 6145:             my $errorstr;
 6146:             foreach my $key (sort(keys(%error))) {
 6147:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 6148:             }
 6149:             $datatable .= '<td>'.$errorstr;
 6150:         }
 6151:     } else {
 6152:         if (keys(%error) > 0) {
 6153:             my $errorstr;
 6154:             foreach my $key (sort(keys(%error))) {
 6155:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 6156:             } 
 6157:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 6158:         } elsif ($scantronurl) {
 6159:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 6160:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 6161:             $datatable .= '<td><span class="LC_nobreak">'.
 6162:                           $link.
 6163:                           '<label><input type="checkbox" name="scantronformat_del"'.
 6164:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 6165:                           '<td><span class="LC_nobreak">&nbsp;'.
 6166:                           &mt('Replace:').'</span><br />';
 6167:         }
 6168:     }
 6169:     if (keys(%error) == 0) {
 6170:         if ($switchserver) {
 6171:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 6172:         } else {
 6173:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 6174:                          '<input type="file" name="scantronformat" /></span>';
 6175:         }
 6176:     }
 6177:     $datatable .= '</td></tr>';
 6178:     $$rowtotal ++;
 6179:     return $datatable;
 6180: }
 6181: 
 6182: sub legacy_scantronformat {
 6183:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 6184:     my ($url,$error);
 6185:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 6186:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 6187:         (my $result,$url) =
 6188:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 6189:                          '','',$newfile);
 6190:         if ($result ne 'ok') {
 6191:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 6192:         }
 6193:     }
 6194:     return ($url,$error);
 6195: }
 6196: 
 6197: sub print_coursecategories {
 6198:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 6199:     my $datatable;
 6200:     if ($position eq 'top') {
 6201:         my (%checked);
 6202:         my @catitems = ('unauth','auth');
 6203:         my @cattypes = ('std','domonly','codesrch','none');
 6204:         $checked{'unauth'} = 'std';
 6205:         $checked{'auth'} = 'std';
 6206:         if (ref($settings) eq 'HASH') {
 6207:             foreach my $type (@cattypes) {
 6208:                 if ($type eq $settings->{'unauth'}) {
 6209:                     $checked{'unauth'} = $type;
 6210:                 }
 6211:                 if ($type eq $settings->{'auth'}) {
 6212:                     $checked{'auth'} = $type;
 6213:                 }
 6214:             }
 6215:         }
 6216:         my %lt = &Apache::lonlocal::texthash (
 6217:                                                unauth   => 'Catalog type for unauthenticated users',
 6218:                                                auth     => 'Catalog type for authenticated users',
 6219:                                                none     => 'No catalog',
 6220:                                                std      => 'Standard catalog',
 6221:                                                domonly  => 'Domain-only catalog',
 6222:                                                codesrch => "Code search form",
 6223:                                              );
 6224:        my $itemcount = 0;
 6225:        foreach my $item (@catitems) {
 6226:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 6227:            $datatable .= '<tr '.$css_class.'>'.
 6228:                          '<td>'.$lt{$item}.'</td>'.
 6229:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 6230:            foreach my $type (@cattypes) {
 6231:                my $ischecked;
 6232:                if ($checked{$item} eq $type) {
 6233:                    $ischecked=' checked="checked"';
 6234:                }
 6235:                $datatable .= '<label>'.
 6236:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 6237:                              ' />'.$lt{$type}.'</label>&nbsp;';
 6238:            }
 6239:            $datatable .= '</span></td></tr>';
 6240:            $itemcount ++;
 6241:         }
 6242:         $$rowtotal += $itemcount;
 6243:     } elsif ($position eq 'middle') {
 6244:         my $toggle_cats_crs = ' ';
 6245:         my $toggle_cats_dom = ' checked="checked" ';
 6246:         my $can_cat_crs = ' ';
 6247:         my $can_cat_dom = ' checked="checked" ';
 6248:         my $toggle_catscomm_comm = ' ';
 6249:         my $toggle_catscomm_dom = ' checked="checked" ';
 6250:         my $can_catcomm_comm = ' ';
 6251:         my $can_catcomm_dom = ' checked="checked" ';
 6252: 
 6253:         if (ref($settings) eq 'HASH') {
 6254:             if ($settings->{'togglecats'} eq 'crs') {
 6255:                 $toggle_cats_crs = $toggle_cats_dom;
 6256:                 $toggle_cats_dom = ' ';
 6257:             }
 6258:             if ($settings->{'categorize'} eq 'crs') {
 6259:                 $can_cat_crs = $can_cat_dom;
 6260:                 $can_cat_dom = ' ';
 6261:             }
 6262:             if ($settings->{'togglecatscomm'} eq 'comm') {
 6263:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 6264:                 $toggle_catscomm_dom = ' ';
 6265:             }
 6266:             if ($settings->{'categorizecomm'} eq 'comm') {
 6267:                 $can_catcomm_comm = $can_catcomm_dom;
 6268:                 $can_catcomm_dom = ' ';
 6269:             }
 6270:         }
 6271:         my %title = &Apache::lonlocal::texthash (
 6272:                      togglecats     => 'Show/Hide a course in catalog',
 6273:                      togglecatscomm => 'Show/Hide a community in catalog',
 6274:                      categorize     => 'Assign a category to a course',
 6275:                      categorizecomm => 'Assign a category to a community',
 6276:                     );
 6277:         my %level = &Apache::lonlocal::texthash (
 6278:                      dom  => 'Set in Domain',
 6279:                      crs  => 'Set in Course',
 6280:                      comm => 'Set in Community',
 6281:                     );
 6282:         $datatable = '<tr class="LC_odd_row">'.
 6283:                   '<td>'.$title{'togglecats'}.'</td>'.
 6284:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 6285:                   '<input type="radio" name="togglecats"'.
 6286:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6287:                   '<label><input type="radio" name="togglecats"'.
 6288:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 6289:                   '</tr><tr>'.
 6290:                   '<td>'.$title{'categorize'}.'</td>'.
 6291:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6292:                   '<label><input type="radio" name="categorize"'.
 6293:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6294:                   '<label><input type="radio" name="categorize"'.
 6295:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 6296:                   '</tr><tr class="LC_odd_row">'.
 6297:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 6298:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 6299:                   '<input type="radio" name="togglecatscomm"'.
 6300:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6301:                   '<label><input type="radio" name="togglecatscomm"'.
 6302:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 6303:                   '</tr><tr>'.
 6304:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 6305:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6306:                   '<label><input type="radio" name="categorizecomm"'.
 6307:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6308:                   '<label><input type="radio" name="categorizecomm"'.
 6309:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 6310:                   '</tr>';
 6311:         $$rowtotal += 4;
 6312:     } else {
 6313:         my $css_class;
 6314:         my $itemcount = 1;
 6315:         my $cathash; 
 6316:         if (ref($settings) eq 'HASH') {
 6317:             $cathash = $settings->{'cats'};
 6318:         }
 6319:         if (ref($cathash) eq 'HASH') {
 6320:             my (@cats,@trails,%allitems,%idx,@jsarray);
 6321:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 6322:                                                    \%allitems,\%idx,\@jsarray);
 6323:             my $maxdepth = scalar(@cats);
 6324:             my $colattrib = '';
 6325:             if ($maxdepth > 2) {
 6326:                 $colattrib = ' colspan="2" ';
 6327:             }
 6328:             my @path;
 6329:             if (@cats > 0) {
 6330:                 if (ref($cats[0]) eq 'ARRAY') {
 6331:                     my $numtop = @{$cats[0]};
 6332:                     my $maxnum = $numtop;
 6333:                     my %default_names = (
 6334:                           instcode    => &mt('Official courses'),
 6335:                           communities => &mt('Communities'),
 6336:                     );
 6337: 
 6338:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 6339:                         ($cathash->{'instcode::0'} eq '') ||
 6340:                         (!grep(/^communities$/,@{$cats[0]})) || 
 6341:                         ($cathash->{'communities::0'} eq '')) {
 6342:                         $maxnum ++;
 6343:                     }
 6344:                     my $lastidx;
 6345:                     for (my $i=0; $i<$numtop; $i++) {
 6346:                         my $parent = $cats[0][$i];
 6347:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6348:                         my $item = &escape($parent).'::0';
 6349:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 6350:                         $lastidx = $idx{$item};
 6351:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6352:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 6353:                         for (my $k=0; $k<=$maxnum; $k++) {
 6354:                             my $vpos = $k+1;
 6355:                             my $selstr;
 6356:                             if ($k == $i) {
 6357:                                 $selstr = ' selected="selected" ';
 6358:                             }
 6359:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6360:                         }
 6361:                         $datatable .= '</select></span></td><td>';
 6362:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 6363:                             $datatable .=  '<span class="LC_nobreak">'
 6364:                                            .$default_names{$parent}.'</span>';
 6365:                             if ($parent eq 'instcode') {
 6366:                                 $datatable .= '<br /><span class="LC_nobreak">('
 6367:                                               .&mt('with institutional codes')
 6368:                                               .')</span></td><td'.$colattrib.'>';
 6369:                             } else {
 6370:                                 $datatable .= '<table><tr><td>';
 6371:                             }
 6372:                             $datatable .= '<span class="LC_nobreak">'
 6373:                                           .'<label><input type="radio" name="'
 6374:                                           .$parent.'" value="1" checked="checked" />'
 6375:                                           .&mt('Display').'</label>';
 6376:                             if ($parent eq 'instcode') {
 6377:                                 $datatable .= '&nbsp;';
 6378:                             } else {
 6379:                                 $datatable .= '</span></td></tr><tr><td>'
 6380:                                               .'<span class="LC_nobreak">';
 6381:                             }
 6382:                             $datatable .= '<label><input type="radio" name="'
 6383:                                           .$parent.'" value="0" />'
 6384:                                           .&mt('Do not display').'</label></span>';
 6385:                             if ($parent eq 'communities') {
 6386:                                 $datatable .= '</td></tr></table>';
 6387:                             }
 6388:                             $datatable .= '</td>';
 6389:                         } else {
 6390:                             $datatable .= $parent
 6391:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 6392:                                           .'<input type="checkbox" name="deletecategory" '
 6393:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 6394:                         }
 6395:                         my $depth = 1;
 6396:                         push(@path,$parent);
 6397:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 6398:                         pop(@path);
 6399:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 6400:                         $itemcount ++;
 6401:                     }
 6402:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6403:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 6404:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 6405:                     for (my $k=0; $k<=$maxnum; $k++) {
 6406:                         my $vpos = $k+1;
 6407:                         my $selstr;
 6408:                         if ($k == $numtop) {
 6409:                             $selstr = ' selected="selected" ';
 6410:                         }
 6411:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6412:                     }
 6413:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 6414:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 6415:                                   .'</tr>'."\n";
 6416:                     $itemcount ++;
 6417:                     foreach my $default ('instcode','communities') {
 6418:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 6419:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6420:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 6421:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 6422:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 6423:                             for (my $k=0; $k<=$maxnum; $k++) {
 6424:                                 my $vpos = $k+1;
 6425:                                 my $selstr;
 6426:                                 if ($k == $maxnum) {
 6427:                                     $selstr = ' selected="selected" ';
 6428:                                 }
 6429:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6430:                             }
 6431:                             $datatable .= '</select></span></td>'.
 6432:                                           '<td><span class="LC_nobreak">'.
 6433:                                           $default_names{$default}.'</span>';
 6434:                             if ($default eq 'instcode') {
 6435:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 6436:                                               .&mt('with institutional codes').')</span>';
 6437:                             }
 6438:                             $datatable .= '</td>'
 6439:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 6440:                                           .&mt('Display').'</label>&nbsp;'
 6441:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 6442:                                           .&mt('Do not display').'</label></span></td></tr>';
 6443:                         }
 6444:                     }
 6445:                 }
 6446:             } else {
 6447:                 $datatable .= &initialize_categories($itemcount);
 6448:             }
 6449:         } else {
 6450:             $datatable .= '<tr><td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td></tr>'
 6451:                           .&initialize_categories($itemcount);
 6452:         }
 6453:         $$rowtotal += $itemcount;
 6454:     }
 6455:     return $datatable;
 6456: }
 6457: 
 6458: sub print_serverstatuses {
 6459:     my ($dom,$settings,$rowtotal) = @_;
 6460:     my $datatable;
 6461:     my @pages = &serverstatus_pages();
 6462:     my (%namedaccess,%machineaccess);
 6463:     foreach my $type (@pages) {
 6464:         $namedaccess{$type} = '';
 6465:         $machineaccess{$type}= '';
 6466:     }
 6467:     if (ref($settings) eq 'HASH') {
 6468:         foreach my $type (@pages) {
 6469:             if (exists($settings->{$type})) {
 6470:                 if (ref($settings->{$type}) eq 'HASH') {
 6471:                     foreach my $key (keys(%{$settings->{$type}})) {
 6472:                         if ($key eq 'namedusers') {
 6473:                             $namedaccess{$type} = $settings->{$type}->{$key};
 6474:                         } elsif ($key eq 'machines') {
 6475:                             $machineaccess{$type} = $settings->{$type}->{$key};
 6476:                         }
 6477:                     }
 6478:                 }
 6479:             }
 6480:         }
 6481:     }
 6482:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 6483:     my $rownum = 0;
 6484:     my $css_class;
 6485:     foreach my $type (@pages) {
 6486:         $rownum ++;
 6487:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 6488:         $datatable .= '<tr'.$css_class.'>'.
 6489:                       '<td><span class="LC_nobreak">'.
 6490:                       $titles->{$type}.'</span></td>'.
 6491:                       '<td class="LC_left_item">'.
 6492:                       '<input type="text" name="'.$type.'_namedusers" '.
 6493:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 6494:                       '<td class="LC_right_item">'.
 6495:                       '<span class="LC_nobreak">'.
 6496:                       '<input type="text" name="'.$type.'_machines" '.
 6497:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 6498:                       '</span></td></tr>'."\n";
 6499:     }
 6500:     $$rowtotal += $rownum;
 6501:     return $datatable;
 6502: }
 6503: 
 6504: sub serverstatus_pages {
 6505:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 6506:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 6507:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 6508:             'uniquecodes','diskusage','coursecatalog');
 6509: }
 6510: 
 6511: sub defaults_javascript {
 6512:     my ($settings) = @_;
 6513:     my $intauthcheck = &mt('Warning: disallowing login for an authenticated user if the stored cost is less than the default will require a password reset by/for the user.');
 6514:     my $intauthcost = &mt('Warning: bcrypt encryption cost for internal authentication must be an integer.');
 6515:     &js_escape(\$intauthcheck);
 6516:     &js_escape(\$intauthcost);
 6517:     my $intauthjs = <<"ENDSCRIPT";
 6518: 
 6519: function warnIntAuth(field) {
 6520:     if (field.name == 'intauth_check') {
 6521:         if (field.value == '2') {
 6522:             alert('$intauthcheck');
 6523:         }
 6524:     }
 6525:     if (field.name == 'intauth_cost') {
 6526:         field.value.replace(/\s/g,'');
 6527:         if (field.value != '') {
 6528:             var regexdigit=/^\\d+\$/;
 6529:             if (!regexdigit.test(field.value)) {
 6530:                 alert('$intauthcost');
 6531:             }
 6532:         }
 6533:     }
 6534:     return;
 6535: }
 6536: 
 6537: ENDSCRIPT
 6538: 
 6539:     if (ref($settings) ne 'HASH') {
 6540:         return &Apache::lonhtmlcommon::scripttag($intauthjs);
 6541:     }
 6542:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 6543:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 6544:         if ($maxnum eq '') {
 6545:             $maxnum = 0;
 6546:         }
 6547:         $maxnum ++;
 6548:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 6549:         return <<"ENDSCRIPT";
 6550: <script type="text/javascript">
 6551: // <![CDATA[
 6552: function reorderTypes(form,caller) {
 6553:     var changedVal;
 6554: $jstext 
 6555:     var newpos = 'addinststatus_pos';
 6556:     var current = new Array;
 6557:     var maxh = $maxnum;
 6558:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 6559:     var oldVal;
 6560:     if (caller == newpos) {
 6561:         changedVal = newitemVal;
 6562:     } else {
 6563:         var curritem = 'inststatus_pos_'+caller;
 6564:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 6565:         current[newitemVal] = newpos;
 6566:     }
 6567:     for (var i=0; i<inststatuses.length; i++) {
 6568:         if (inststatuses[i] != caller) {
 6569:             var elementName = 'inststatus_pos_'+inststatuses[i];
 6570:             if (form.elements[elementName]) {
 6571:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 6572:                 current[currVal] = elementName;
 6573:             }
 6574:         }
 6575:     }
 6576:     for (var j=0; j<maxh; j++) {
 6577:         if (current[j] == undefined) {
 6578:             oldVal = j;
 6579:         }
 6580:     }
 6581:     if (oldVal < changedVal) {
 6582:         for (var k=oldVal+1; k<=changedVal ; k++) {
 6583:            var elementName = current[k];
 6584:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 6585:         }
 6586:     } else {
 6587:         for (var k=changedVal; k<oldVal; k++) {
 6588:             var elementName = current[k];
 6589:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 6590:         }
 6591:     }
 6592:     return;
 6593: }
 6594: 
 6595: $intauthjs
 6596: 
 6597: // ]]>
 6598: </script>
 6599: 
 6600: ENDSCRIPT
 6601:     } else {
 6602:         return &Apache::lonhtmlcommon::scripttag($intauthjs);
 6603:     }
 6604: }
 6605: 
 6606: sub coursecategories_javascript {
 6607:     my ($settings) = @_;
 6608:     my ($output,$jstext,$cathash);
 6609:     if (ref($settings) eq 'HASH') {
 6610:         $cathash = $settings->{'cats'};
 6611:     }
 6612:     if (ref($cathash) eq 'HASH') {
 6613:         my (@cats,@jsarray,%idx);
 6614:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 6615:         if (@jsarray > 0) {
 6616:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 6617:             for (my $i=0; $i<@jsarray; $i++) {
 6618:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 6619:                     my $catstr = join('","',@{$jsarray[$i]});
 6620:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 6621:                 }
 6622:             }
 6623:         }
 6624:     } else {
 6625:         $jstext  = '    var categories = Array(1);'."\n".
 6626:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 6627:     }
 6628:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 6629:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 6630:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
 6631:     &js_escape(\$instcode_reserved);
 6632:     &js_escape(\$communities_reserved);
 6633:     &js_escape(\$choose_again);
 6634:     $output = <<"ENDSCRIPT";
 6635: <script type="text/javascript">
 6636: // <![CDATA[
 6637: function reorderCats(form,parent,item,idx) {
 6638:     var changedVal;
 6639: $jstext
 6640:     var newpos = 'addcategory_pos';
 6641:     if (parent == '') {
 6642:         var has_instcode = 0;
 6643:         var maxtop = categories[idx].length;
 6644:         for (var j=0; j<maxtop; j++) {
 6645:             if (categories[idx][j] == 'instcode::0') {
 6646:                 has_instcode == 1;
 6647:             }
 6648:         }
 6649:         if (has_instcode == 0) {
 6650:             categories[idx][maxtop] = 'instcode_pos';
 6651:         }
 6652:     } else {
 6653:         newpos += '_'+parent;
 6654:     }
 6655:     var maxh = 1 + categories[idx].length;
 6656:     var current = new Array;
 6657:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 6658:     if (item == newpos) {
 6659:         changedVal = newitemVal;
 6660:     } else {
 6661:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 6662:         current[newitemVal] = newpos;
 6663:     }
 6664:     for (var i=0; i<categories[idx].length; i++) {
 6665:         var elementName = categories[idx][i];
 6666:         if (elementName != item) {
 6667:             if (form.elements[elementName]) {
 6668:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 6669:                 current[currVal] = elementName;
 6670:             }
 6671:         }
 6672:     }
 6673:     var oldVal;
 6674:     for (var j=0; j<maxh; j++) {
 6675:         if (current[j] == undefined) {
 6676:             oldVal = j;
 6677:         }
 6678:     }
 6679:     if (oldVal < changedVal) {
 6680:         for (var k=oldVal+1; k<=changedVal ; k++) {
 6681:            var elementName = current[k];
 6682:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 6683:         }
 6684:     } else {
 6685:         for (var k=changedVal; k<oldVal; k++) {
 6686:             var elementName = current[k];
 6687:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 6688:         }
 6689:     }
 6690:     return;
 6691: }
 6692: 
 6693: function categoryCheck(form) {
 6694:     if (form.elements['addcategory_name'].value == 'instcode') {
 6695:         alert('$instcode_reserved\\n$choose_again');
 6696:         return false;
 6697:     }
 6698:     if (form.elements['addcategory_name'].value == 'communities') {
 6699:         alert('$communities_reserved\\n$choose_again');
 6700:         return false;
 6701:     }
 6702:     return true;
 6703: }
 6704: 
 6705: // ]]>
 6706: </script>
 6707: 
 6708: ENDSCRIPT
 6709:     return $output;
 6710: }
 6711: 
 6712: sub initialize_categories {
 6713:     my ($itemcount) = @_;
 6714:     my ($datatable,$css_class,$chgstr);
 6715:     my %default_names = (
 6716:                       instcode    => 'Official courses (with institutional codes)',
 6717:                       communities => 'Communities',
 6718:                         );
 6719:     my $select0 = ' selected="selected"';
 6720:     my $select1 = '';
 6721:     foreach my $default ('instcode','communities') {
 6722:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6723:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','0'".');"';
 6724:         if ($default eq 'communities') {
 6725:             $select1 = $select0;
 6726:             $select0 = '';
 6727:         }
 6728:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6729:                      .'<select name="'.$default.'_pos">'
 6730:                      .'<option value="0"'.$select0.'>1</option>'
 6731:                      .'<option value="1"'.$select1.'>2</option>'
 6732:                      .'<option value="2">3</option></select>&nbsp;'
 6733:                      .$default_names{$default}
 6734:                      .'</span></td><td><span class="LC_nobreak">'
 6735:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 6736:                      .&mt('Display').'</label>&nbsp;<label>'
 6737:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 6738:                  .'</label></span></td></tr>';
 6739:         $itemcount ++;
 6740:     }
 6741:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6742:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 6743:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6744:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 6745:                   .'<option value="0">1</option>'
 6746:                   .'<option value="1">2</option>'
 6747:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 6748:                   .&mt('Add category').'</span></td><tda<span class="LC_nobreak">>'.&mt('Name:')
 6749:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></span>'
 6750:                   .'</td></tr>';
 6751:     return $datatable;
 6752: }
 6753: 
 6754: sub build_category_rows {
 6755:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 6756:     my ($text,$name,$item,$chgstr);
 6757:     if (ref($cats) eq 'ARRAY') {
 6758:         my $maxdepth = scalar(@{$cats});
 6759:         if (ref($cats->[$depth]) eq 'HASH') {
 6760:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 6761:                 my $numchildren = @{$cats->[$depth]{$parent}};
 6762:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6763:                 $text .= '<td><table class="LC_data_table">';
 6764:                 my ($idxnum,$parent_name,$parent_item);
 6765:                 my $higher = $depth - 1;
 6766:                 if ($higher == 0) {
 6767:                     $parent_name = &escape($parent).'::'.$higher;
 6768:                 } else {
 6769:                     if (ref($path) eq 'ARRAY') {
 6770:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 6771:                     }
 6772:                 }
 6773:                 $parent_item = 'addcategory_pos_'.$parent_name;
 6774:                 for (my $j=0; $j<=$numchildren; $j++) {
 6775:                     if ($j < $numchildren) {
 6776:                         $name = $cats->[$depth]{$parent}[$j];
 6777:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 6778:                         $idxnum = $idx->{$item};
 6779:                     } else {
 6780:                         $name = $parent_name;
 6781:                         $item = $parent_item;
 6782:                     }
 6783:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 6784:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 6785:                     for (my $i=0; $i<=$numchildren; $i++) {
 6786:                         my $vpos = $i+1;
 6787:                         my $selstr;
 6788:                         if ($j == $i) {
 6789:                             $selstr = ' selected="selected" ';
 6790:                         }
 6791:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 6792:                     }
 6793:                     $text .= '</select>&nbsp;';
 6794:                     if ($j < $numchildren) {
 6795:                         my $deeper = $depth+1;
 6796:                         $text .= $name.'&nbsp;'
 6797:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 6798:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 6799:                         if(ref($path) eq 'ARRAY') {
 6800:                             push(@{$path},$name);
 6801:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 6802:                             pop(@{$path});
 6803:                         }
 6804:                     } else {
 6805:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="text" size="20" name="addcategory_name_';
 6806:                         if ($j == $numchildren) {
 6807:                             $text .= $name;
 6808:                         } else {
 6809:                             $text .= $item;
 6810:                         }
 6811:                         $text .= '" value="" />';
 6812:                     }
 6813:                     $text .= '</td></tr>';
 6814:                 }
 6815:                 $text .= '</table></td>';
 6816:             } else {
 6817:                 my $higher = $depth-1;
 6818:                 if ($higher == 0) {
 6819:                     $name = &escape($parent).'::'.$higher;
 6820:                 } else {
 6821:                     if (ref($path) eq 'ARRAY') {
 6822:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 6823:                     }
 6824:                 }
 6825:                 my $colspan;
 6826:                 if ($parent ne 'instcode') {
 6827:                     $colspan = $maxdepth - $depth - 1;
 6828:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="text" size="20" name="subcat_'.$name.'" value="" /></td>';
 6829:                 }
 6830:             }
 6831:         }
 6832:     }
 6833:     return $text;
 6834: }
 6835: 
 6836: sub modifiable_userdata_row {
 6837:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref,
 6838:         $rowid,$customcss,$rowstyle) = @_;
 6839:     my ($role,$rolename,$statustype);
 6840:     $role = $item;
 6841:     if ($context eq 'cancreate') {
 6842:         if ($item =~ /^(emailusername)_(.+)$/) {
 6843:             $role = $1;
 6844:             $statustype = $2;
 6845:             if (ref($usertypes) eq 'HASH') {
 6846:                 if ($usertypes->{$statustype}) {
 6847:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 6848:                 } else {
 6849:                     $rolename = &mt('Data provided by user');
 6850:                 }
 6851:             }
 6852:         }
 6853:     } elsif ($context eq 'selfcreate') {
 6854:         if (ref($usertypes) eq 'HASH') {
 6855:             $rolename = $usertypes->{$role};
 6856:         } else {
 6857:             $rolename = $role;
 6858:         }
 6859:     } else {
 6860:         if ($role eq 'cr') {
 6861:             $rolename = &mt('Custom role');
 6862:         } else {
 6863:             $rolename = &Apache::lonnet::plaintext($role);
 6864:         }
 6865:     }
 6866:     my (@fields,%fieldtitles);
 6867:     if (ref($fieldsref) eq 'ARRAY') {
 6868:         @fields = @{$fieldsref};
 6869:     } else {
 6870:         @fields = ('lastname','firstname','middlename','generation',
 6871:                    'permanentemail','id');
 6872:     }
 6873:     if ((ref($titlesref) eq 'HASH')) {
 6874:         %fieldtitles = %{$titlesref};
 6875:     } else {
 6876:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 6877:     }
 6878:     my $output;
 6879:     my $css_class;
 6880:     if ($rowcount%2) {
 6881:         $css_class = 'LC_odd_row';
 6882:     }
 6883:     if ($customcss) {
 6884:         $css_class .= " $customcss";
 6885:     }
 6886:     $css_class =~ s/^\s+//;
 6887:     if ($css_class) {
 6888:         $css_class = ' class="'.$css_class.'"';
 6889:     }
 6890:     if ($rowstyle) {
 6891:         $css_class .= ' style="'.$rowstyle.'"';
 6892:     }
 6893:     if ($rowid) {
 6894:         $rowid = ' id="'.$rowid.'"';
 6895:     }
 6896: 
 6897:     $output = '<tr '.$css_class.$rowid.'>'.
 6898:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 6899:               '<td class="LC_left_item" colspan="2"><table>';
 6900:     my $rem;
 6901:     my %checks;
 6902:     if (ref($settings) eq 'HASH') {
 6903:         if (ref($settings->{$context}) eq 'HASH') {
 6904:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 6905:                 my $hashref = $settings->{$context}->{$role};
 6906:                 if ($role eq 'emailusername') {
 6907:                     if ($statustype) {
 6908:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 6909:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 6910:                             if (ref($hashref) eq 'HASH') { 
 6911:                                 foreach my $field (@fields) {
 6912:                                     if ($hashref->{$field}) {
 6913:                                         $checks{$field} = $hashref->{$field};
 6914:                                     }
 6915:                                 }
 6916:                             }
 6917:                         }
 6918:                     }
 6919:                 } else {
 6920:                     if (ref($hashref) eq 'HASH') {
 6921:                         foreach my $field (@fields) {
 6922:                             if ($hashref->{$field}) {
 6923:                                 $checks{$field} = ' checked="checked" ';
 6924:                             }
 6925:                         }
 6926:                     }
 6927:                 }
 6928:             }
 6929:         }
 6930:     }
 6931: 
 6932:     my $total = scalar(@fields);
 6933:     for (my $i=0; $i<$total; $i++) {
 6934:         $rem = $i%($numinrow);
 6935:         if ($rem == 0) {
 6936:             if ($i > 0) {
 6937:                 $output .= '</tr>';
 6938:             }
 6939:             $output .= '<tr>';
 6940:         }
 6941:         my $check = ' ';
 6942:         unless ($role eq 'emailusername') {
 6943:             if (exists($checks{$fields[$i]})) {
 6944:                 $check = $checks{$fields[$i]}
 6945:             } else {
 6946:                 if ($role eq 'st') {
 6947:                     if (ref($settings) ne 'HASH') {
 6948:                         $check = ' checked="checked" '; 
 6949:                     }
 6950:                 }
 6951:             }
 6952:         }
 6953:         $output .= '<td class="LC_left_item">'.
 6954:                    '<span class="LC_nobreak">';
 6955:         if ($role eq 'emailusername') {
 6956:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 6957:                 $checks{$fields[$i]} = 'omit';
 6958:             }
 6959:             foreach my $option ('required','optional','omit') {
 6960:                 my $checked='';
 6961:                 if ($checks{$fields[$i]} eq $option) {
 6962:                     $checked='checked="checked" ';
 6963:                 }
 6964:                 $output .= '<label>'.
 6965:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 6966:                            &mt($option).'</label>'.('&nbsp;' x2);
 6967:             }
 6968:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 6969:         } else {
 6970:             $output .= '<label>'.
 6971:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 6972:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 6973:                        '</label>';
 6974:         }
 6975:         $output .= '</span></td>';
 6976:     }
 6977:     $rem = $total%$numinrow;
 6978:     my $colsleft;
 6979:     if ($rem) {
 6980:         $colsleft = $numinrow - $rem;
 6981:     }
 6982:     if ($colsleft > 1) {
 6983:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 6984:                    '&nbsp;</td>';
 6985:     } elsif ($colsleft == 1) {
 6986:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 6987:     }
 6988:     $output .= '</tr></table></td></tr>';
 6989:     return $output;
 6990: }
 6991: 
 6992: sub insttypes_row {
 6993:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
 6994:         $customcss,$rowstyle) = @_;
 6995:     my %lt = &Apache::lonlocal::texthash (
 6996:                       cansearch => 'Users allowed to search',
 6997:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 6998:                       lockablenames => 'User preference to lock name',
 6999:                       selfassign    => 'Self-reportable affiliations',
 7000:              );
 7001:     my $showdom;
 7002:     if ($context eq 'cansearch') {
 7003:         $showdom = ' ('.$dom.')';
 7004:     }
 7005:     my $class = 'LC_left_item';
 7006:     if ($context eq 'statustocreate') {
 7007:         $class = 'LC_right_item';
 7008:     }
 7009:     my $css_class;
 7010:     if ($$rowtotal%2) {
 7011:         $css_class = 'LC_odd_row';
 7012:     }
 7013:     if ($customcss) {
 7014:         $css_class .= ' '.$customcss;
 7015:     }
 7016:     $css_class =~ s/^\s+//;
 7017:     if ($css_class) {
 7018:         $css_class = ' class="'.$css_class.'"';
 7019:     }
 7020:     if ($rowstyle) {
 7021:         $css_class .= ' style="'.$rowstyle.'"';
 7022:     }
 7023:     if ($onclick) {
 7024:         $onclick = 'onclick="'.$onclick.'" ';
 7025:     }
 7026:     my $output = '<tr'.$css_class.'>'.
 7027:                  '<td>'.$lt{$context}.$showdom.
 7028:                  '</td><td class="'.$class.'" colspan="2"><table>';
 7029:     my $rem;
 7030:     if (ref($types) eq 'ARRAY') {
 7031:         for (my $i=0; $i<@{$types}; $i++) {
 7032:             if (defined($usertypes->{$types->[$i]})) {
 7033:                 my $rem = $i%($numinrow);
 7034:                 if ($rem == 0) {
 7035:                     if ($i > 0) {
 7036:                         $output .= '</tr>';
 7037:                     }
 7038:                     $output .= '<tr>';
 7039:                 }
 7040:                 my $check = ' ';
 7041:                 if (ref($settings) eq 'HASH') {
 7042:                     if (ref($settings->{$context}) eq 'ARRAY') {
 7043:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 7044:                             $check = ' checked="checked" ';
 7045:                         }
 7046:                     } elsif ($context eq 'statustocreate') {
 7047:                         $check = ' checked="checked" ';
 7048:                     }
 7049:                 }
 7050:                 $output .= '<td class="LC_left_item">'.
 7051:                            '<span class="LC_nobreak"><label>'.
 7052:                            '<input type="checkbox" name="'.$context.'" '.
 7053:                            'value="'.$types->[$i].'"'.$check.$onclick.'/>'.
 7054:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 7055:             }
 7056:         }
 7057:         $rem = @{$types}%($numinrow);
 7058:     }
 7059:     my $colsleft = $numinrow - $rem;
 7060:     if ($rem == 0) {
 7061:         $output .= '<tr>';
 7062:     }
 7063:     if ($colsleft > 1) {
 7064:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 7065:     } else {
 7066:         $output .= '<td class="LC_left_item">';
 7067:     }
 7068:     my $defcheck = ' ';
 7069:     if (ref($settings) eq 'HASH') {  
 7070:         if (ref($settings->{$context}) eq 'ARRAY') {
 7071:             if (grep(/^default$/,@{$settings->{$context}})) {
 7072:                 $defcheck = ' checked="checked" ';
 7073:             }
 7074:         } elsif ($context eq 'statustocreate') {
 7075:             $defcheck = ' checked="checked" ';
 7076:         }
 7077:     }
 7078:     $output .= '<span class="LC_nobreak"><label>'.
 7079:                '<input type="checkbox" name="'.$context.'" '.
 7080:                'value="default"'.$defcheck.$onclick.'/>'.
 7081:                $othertitle.'</label></span></td>'.
 7082:                '</tr></table></td></tr>';
 7083:     return $output;
 7084: }
 7085: 
 7086: sub sorted_searchtitles {
 7087:     my %searchtitles = &Apache::lonlocal::texthash(
 7088:                          'uname' => 'username',
 7089:                          'lastname' => 'last name',
 7090:                          'lastfirst' => 'last name, first name',
 7091:                      );
 7092:     my @titleorder = ('uname','lastname','lastfirst');
 7093:     return (\%searchtitles,\@titleorder);
 7094: }
 7095: 
 7096: sub sorted_searchtypes {
 7097:     my %srchtypes_desc = (
 7098:                            exact    => 'is exact match',
 7099:                            contains => 'contains ..',
 7100:                            begins   => 'begins with ..',
 7101:                          );
 7102:     my @srchtypeorder = ('exact','begins','contains');
 7103:     return (\%srchtypes_desc,\@srchtypeorder);
 7104: }
 7105: 
 7106: sub usertype_update_row {
 7107:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 7108:     my $datatable;
 7109:     my $numinrow = 4;
 7110:     foreach my $type (@{$types}) {
 7111:         if (defined($usertypes->{$type})) {
 7112:             $$rownums ++;
 7113:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 7114:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 7115:                           '</td><td class="LC_left_item"><table>';
 7116:             for (my $i=0; $i<@{$fields}; $i++) {
 7117:                 my $rem = $i%($numinrow);
 7118:                 if ($rem == 0) {
 7119:                     if ($i > 0) {
 7120:                         $datatable .= '</tr>';
 7121:                     }
 7122:                     $datatable .= '<tr>';
 7123:                 }
 7124:                 my $check = ' ';
 7125:                 if (ref($settings) eq 'HASH') {
 7126:                     if (ref($settings->{'fields'}) eq 'HASH') {
 7127:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 7128:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 7129:                                 $check = ' checked="checked" ';
 7130:                             }
 7131:                         }
 7132:                     }
 7133:                 }
 7134: 
 7135:                 if ($i == @{$fields}-1) {
 7136:                     my $colsleft = $numinrow - $rem;
 7137:                     if ($colsleft > 1) {
 7138:                         $datatable .= '<td colspan="'.$colsleft.'">';
 7139:                     } else {
 7140:                         $datatable .= '<td>';
 7141:                     }
 7142:                 } else {
 7143:                     $datatable .= '<td>';
 7144:                 }
 7145:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7146:                               '<input type="checkbox" name="updateable_'.$type.
 7147:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 7148:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 7149:             }
 7150:             $datatable .= '</tr></table></td></tr>';
 7151:         }
 7152:     }
 7153:     return $datatable;
 7154: }
 7155: 
 7156: sub modify_login {
 7157:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 7158:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 7159:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 7160:     %title = ( coursecatalog => 'Display course catalog',
 7161:                adminmail => 'Display administrator E-mail address',
 7162:                helpdesk  => 'Display "Contact Helpdesk" link',
 7163:                newuser => 'Link for visitors to create a user account',
 7164:                loginheader => 'Log-in box header');
 7165:     @offon = ('off','on');
 7166:     if (ref($domconfig{login}) eq 'HASH') {
 7167:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 7168:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 7169:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 7170:             }
 7171:         }
 7172:     }
 7173:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 7174:                                            \%domconfig,\%loginhash);
 7175:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 7176:     foreach my $item (@toggles) {
 7177:         $loginhash{login}{$item} = $env{'form.'.$item};
 7178:     }
 7179:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 7180:     if (ref($colchanges{'login'}) eq 'HASH') {  
 7181:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 7182:                                          \%loginhash);
 7183:     }
 7184: 
 7185:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7186:     my %domservers = &Apache::lonnet::get_servers($dom);
 7187:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 7188:     if (keys(%servers) > 1) {
 7189:         foreach my $lonhost (keys(%servers)) {
 7190:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 7191:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 7192:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 7193:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 7194:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 7195:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7196:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7197:                         $changes{'loginvia'}{$lonhost} = 1;
 7198:                     } else {
 7199:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 7200:                         $changes{'loginvia'}{$lonhost} = 1;
 7201:                     }
 7202:                 } else {
 7203:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7204:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7205:                         $changes{'loginvia'}{$lonhost} = 1;
 7206:                     }
 7207:                 }
 7208:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 7209:                     foreach my $item (@loginvia_attribs) {
 7210:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 7211:                     }
 7212:                 } else {
 7213:                     foreach my $item (@loginvia_attribs) {
 7214:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 7215:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 7216:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 7217:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 7218:                                 $new = '/';
 7219:                             }
 7220:                         }
 7221:                         if (($item eq 'custompath') && 
 7222:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 7223:                             $new = '';
 7224:                         }
 7225:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 7226:                             $changes{'loginvia'}{$lonhost} = 1;
 7227:                         }
 7228:                         if ($item eq 'exempt') {
 7229:                             $new = &check_exempt_addresses($new);
 7230:                         }
 7231:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 7232:                     }
 7233:                 }
 7234:             } else {
 7235:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7236:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7237:                     $changes{'loginvia'}{$lonhost} = 1;
 7238:                     foreach my $item (@loginvia_attribs) {
 7239:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 7240:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 7241:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 7242:                                 $new = '/';
 7243:                             }
 7244:                         }
 7245:                         if (($item eq 'custompath') && 
 7246:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 7247:                             $new = '';
 7248:                         }
 7249:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 7250:                     }
 7251:                 }
 7252:             }
 7253:         }
 7254:     }
 7255: 
 7256:     my $servadm = $r->dir_config('lonAdmEMail');
 7257:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 7258:     if (ref($domconfig{'login'}) eq 'HASH') {
 7259:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 7260:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 7261:                 if ($lang eq 'nolang') {
 7262:                     push(@currlangs,$lang);
 7263:                 } elsif (defined($langchoices{$lang})) {
 7264:                     push(@currlangs,$lang);
 7265:                 } else {
 7266:                     next;
 7267:                 }
 7268:             }
 7269:         }
 7270:     }
 7271:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 7272:     if (@currlangs > 0) {
 7273:         foreach my $lang (@currlangs) {
 7274:             if (grep(/^\Q$lang\E$/,@delurls)) {
 7275:                 $changes{'helpurl'}{$lang} = 1;
 7276:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 7277:                 $changes{'helpurl'}{$lang} = 1;
 7278:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 7279:                 push(@newlangs,$lang);
 7280:             } else {
 7281:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 7282:             }
 7283:         }
 7284:     }
 7285:     unless (grep(/^nolang$/,@currlangs)) {
 7286:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 7287:             $changes{'helpurl'}{'nolang'} = 1;
 7288:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 7289:             push(@newlangs,'nolang');
 7290:         }
 7291:     }
 7292:     if ($env{'form.loginhelpurl_add_lang'}) {
 7293:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 7294:             ($env{'form.loginhelpurl_add_file.filename'})) {
 7295:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 7296:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 7297:         }
 7298:     }
 7299:     if ((@newlangs > 0) || ($addedfile)) {
 7300:         my $error;
 7301:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 7302:         if ($configuserok eq 'ok') {
 7303:             if ($switchserver) {
 7304:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 7305:             } elsif ($author_ok eq 'ok') {
 7306:                 my @allnew = @newlangs;
 7307:                 if ($addedfile ne '') {
 7308:                     push(@allnew,$addedfile);
 7309:                 }
 7310:                 foreach my $lang (@allnew) {
 7311:                     my $formelem = 'loginhelpurl_'.$lang;
 7312:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 7313:                         $formelem = 'loginhelpurl_add_file';
 7314:                     }
 7315:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 7316:                                                                "help/$lang",'','',$newfile{$lang});
 7317:                     if ($result eq 'ok') {
 7318:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 7319:                         $changes{'helpurl'}{$lang} = 1;
 7320:                     } else {
 7321:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 7322:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 7323:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 7324:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 7325:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 7326:                         }
 7327:                     }
 7328:                 }
 7329:             } else {
 7330:                 $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);
 7331:             }
 7332:         } else {
 7333:             $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);
 7334:         }
 7335:         if ($error) {
 7336:             &Apache::lonnet::logthis($error);
 7337:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7338:         }
 7339:     }
 7340: 
 7341:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
 7342:     if (ref($domconfig{'login'}) eq 'HASH') {
 7343:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
 7344:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
 7345:                 if ($domservers{$lonhost}) {
 7346:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 7347:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
 7348:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
 7349:                     }
 7350:                 }
 7351:             }
 7352:         }
 7353:     }
 7354:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
 7355:     foreach my $lonhost (sort(keys(%domservers))) {
 7356:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 7357:             $changes{'headtag'}{$lonhost} = 1;
 7358:         } else {
 7359:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
 7360:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
 7361:             }
 7362:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
 7363:                 push(@newhosts,$lonhost);
 7364:             } elsif ($currheadtagurls{$lonhost}) {
 7365:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
 7366:                 if ($currexempt{$lonhost}) {
 7367:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
 7368:                         $changes{'headtag'}{$lonhost} = 1;
 7369:                     }
 7370:                 } elsif ($possexempt{$lonhost}) {
 7371:                     $changes{'headtag'}{$lonhost} = 1;
 7372:                 }
 7373:                 if ($possexempt{$lonhost}) {
 7374:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 7375:                 }
 7376:             }
 7377:         }
 7378:     }
 7379:     if (@newhosts) {
 7380:         my $error;
 7381:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 7382:         if ($configuserok eq 'ok') {
 7383:             if ($switchserver) {
 7384:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
 7385:             } elsif ($author_ok eq 'ok') {
 7386:                 foreach my $lonhost (@newhosts) {
 7387:                     my $formelem = 'loginheadtag_'.$lonhost;
 7388:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 7389:                                                                           "login/headtag/$lonhost",'','',
 7390:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
 7391:                     if ($result eq 'ok') {
 7392:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
 7393:                         $changes{'headtag'}{$lonhost} = 1;
 7394:                         if ($possexempt{$lonhost}) {
 7395:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 7396:                         }
 7397:                     } else {
 7398:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
 7399:                                            $newheadtagurls{$lonhost},$result);
 7400:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 7401:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
 7402:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
 7403:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
 7404:                         }
 7405:                     }
 7406:                 }
 7407:             } else {
 7408:                 $error = &mt("Upload of custom markup file(s) failed because an author role could not be assigned to a Domain Configuration user ([_1]) in domain: [_2].  Error was: [_3].",$confname,$dom,$author_ok);
 7409:             }
 7410:         } else {
 7411:             $error = &mt("Upload of custom markup file(s) failed because a Domain Configuration user ([_1]) could not be created in domain: [_2].  Error was: [_3].",$confname,$dom,$configuserok);
 7412:         }
 7413:         if ($error) {
 7414:             &Apache::lonnet::logthis($error);
 7415:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7416:         }
 7417:     }
 7418:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 7419: 
 7420:     my $defaulthelpfile = '/adm/loginproblems.html';
 7421:     my $defaulttext = &mt('Default in use');
 7422: 
 7423:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 7424:                                              $dom);
 7425:     if ($putresult eq 'ok') {
 7426:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 7427:         my %defaultchecked = (
 7428:                     'coursecatalog' => 'on',
 7429:                     'helpdesk'      => 'on',
 7430:                     'adminmail'     => 'off',
 7431:                     'newuser'       => 'off',
 7432:         );
 7433:         if (ref($domconfig{'login'}) eq 'HASH') {
 7434:             foreach my $item (@toggles) {
 7435:                 if ($defaultchecked{$item} eq 'on') { 
 7436:                     if (($domconfig{'login'}{$item} eq '0') &&
 7437:                         ($env{'form.'.$item} eq '1')) {
 7438:                         $changes{$item} = 1;
 7439:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 7440:                               $domconfig{'login'}{$item} eq '1') &&
 7441:                              ($env{'form.'.$item} eq '0')) {
 7442:                         $changes{$item} = 1;
 7443:                     }
 7444:                 } elsif ($defaultchecked{$item} eq 'off') {
 7445:                     if (($domconfig{'login'}{$item} eq '1') &&
 7446:                         ($env{'form.'.$item} eq '0')) {
 7447:                         $changes{$item} = 1;
 7448:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 7449:                               $domconfig{'login'}{$item} eq '0') &&
 7450:                              ($env{'form.'.$item} eq '1')) {
 7451:                         $changes{$item} = 1;
 7452:                     }
 7453:                 }
 7454:             }
 7455:         }
 7456:         if (keys(%changes) > 0 || $colchgtext) {
 7457:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 7458:             if (ref($lastactref) eq 'HASH') {
 7459:                 $lastactref->{'domainconfig'} = 1;
 7460:             }
 7461:             $resulttext = &mt('Changes made:').'<ul>';
 7462:             foreach my $item (sort(keys(%changes))) {
 7463:                 if ($item eq 'loginvia') {
 7464:                     if (ref($changes{$item}) eq 'HASH') {
 7465:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 7466:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 7467:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 7468:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 7469:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 7470:                                     $protocol = 'http' if ($protocol ne 'https');
 7471:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 7472: 
 7473:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 7474:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 7475:                                     } else {
 7476:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 7477:                                     }
 7478:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 7479:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 7480:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 7481:                                     }
 7482:                                     $resulttext .= '</li>';
 7483:                                 } else {
 7484:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 7485:                                 }
 7486:                             } else {
 7487:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 7488:                             }
 7489:                         }
 7490:                         $resulttext .= '</ul></li>';
 7491:                     }
 7492:                 } elsif ($item eq 'helpurl') {
 7493:                     if (ref($changes{$item}) eq 'HASH') {
 7494:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 7495:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 7496:                                 my ($chg,$link);
 7497:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 7498:                                 if ($lang eq 'nolang') {
 7499:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 7500:                                 } else {
 7501:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 7502:                                 }
 7503:                                 $resulttext .= '<li>'.$chg.'</li>';
 7504:                             } else {
 7505:                                 my $chg;
 7506:                                 if ($lang eq 'nolang') {
 7507:                                     $chg = &mt('custom log-in help file for no preferred language');
 7508:                                 } else {
 7509:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 7510:                                 }
 7511:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 7512:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 7513:                                                       '?inhibitmenu=yes',$chg,600,500).
 7514:                                                '</li>';
 7515:                             }
 7516:                         }
 7517:                     }
 7518:                 } elsif ($item eq 'headtag') {
 7519:                     if (ref($changes{$item}) eq 'HASH') {
 7520:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 7521:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 7522:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
 7523:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 7524:                                 $resulttext .= '<li><a href="'.
 7525:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
 7526:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 7527:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
 7528:                                 if ($possexempt{$lonhost}) {
 7529:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
 7530:                                 } else {
 7531:                                     $resulttext .= &mt('included for any client IP');
 7532:                                 }
 7533:                                 $resulttext .= '</li>';
 7534:                             }
 7535:                         }
 7536:                     }
 7537:                 } elsif ($item eq 'captcha') {
 7538:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7539:                         my $chgtxt;
 7540:                         if ($loginhash{'login'}{$item} eq 'notused') {
 7541:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 7542:                         } else {
 7543:                             my %captchas = &captcha_phrases();
 7544:                             if ($captchas{$loginhash{'login'}{$item}}) {
 7545:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 7546:                             } else {
 7547:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 7548:                             }
 7549:                         }
 7550:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 7551:                     }
 7552:                 } elsif ($item eq 'recaptchakeys') {
 7553:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7554:                         my ($privkey,$pubkey);
 7555:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 7556:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 7557:                             $privkey = $loginhash{'login'}{$item}{'private'};
 7558:                         }
 7559:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 7560:                         if (!$pubkey) {
 7561:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 7562:                         } else {
 7563:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 7564:                         }
 7565:                         if (!$privkey) {
 7566:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 7567:                         } else {
 7568:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 7569:                         }
 7570:                         $chgtxt .= '</ul>';
 7571:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 7572:                     }
 7573:                 } elsif ($item eq 'recaptchaversion') {
 7574:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7575:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
 7576:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
 7577:                                            '</li>';
 7578:                         }
 7579:                     }
 7580:                 } else {
 7581:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 7582:                 }
 7583:             }
 7584:             $resulttext .= $colchgtext.'</ul>';
 7585:         } else {
 7586:             $resulttext = &mt('No changes made to log-in page settings');
 7587:         }
 7588:     } else {
 7589:         $resulttext = '<span class="LC_error">'.
 7590: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7591:     }
 7592:     if ($errors) {
 7593:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 7594:                        $errors.'</ul>';
 7595:     }
 7596:     return $resulttext;
 7597: }
 7598: 
 7599: sub check_exempt_addresses {
 7600:     my ($iplist) = @_;
 7601:     $iplist =~ s/^\s+//;
 7602:     $iplist =~ s/\s+$//;
 7603:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
 7604:     my (@okips,$new);
 7605:     foreach my $ip (@poss_ips) {
 7606:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 7607:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 7608:                 push(@okips,$ip);
 7609:             }
 7610:         }
 7611:     }
 7612:     if (@okips > 0) {
 7613:         $new = join(',',@okips);
 7614:     } else {
 7615:         $new = '';
 7616:     }
 7617:     return $new;
 7618: }
 7619: 
 7620: sub color_font_choices {
 7621:     my %choices =
 7622:         &Apache::lonlocal::texthash (
 7623:             img => "Header",
 7624:             bgs => "Background colors",
 7625:             links => "Link colors",
 7626:             images => "Images",
 7627:             font => "Font color",
 7628:             fontmenu => "Font menu",
 7629:             pgbg => "Page",
 7630:             tabbg => "Header",
 7631:             sidebg => "Border",
 7632:             link => "Link",
 7633:             alink => "Active link",
 7634:             vlink => "Visited link",
 7635:         );
 7636:     return %choices;
 7637: }
 7638: 
 7639: sub modify_rolecolors {
 7640:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 7641:     my ($resulttext,%rolehash);
 7642:     $rolehash{'rolecolors'} = {};
 7643:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 7644:         if ($domconfig{'rolecolors'} eq '') {
 7645:             $domconfig{'rolecolors'} = {};
 7646:         }
 7647:     }
 7648:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 7649:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 7650:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 7651:                                              $dom);
 7652:     if ($putresult eq 'ok') {
 7653:         if (keys(%changes) > 0) {
 7654:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 7655:             if (ref($lastactref) eq 'HASH') {
 7656:                 $lastactref->{'domainconfig'} = 1;
 7657:             }
 7658:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 7659:                                              $rolehash{'rolecolors'});
 7660:         } else {
 7661:             $resulttext = &mt('No changes made to default color schemes');
 7662:         }
 7663:     } else {
 7664:         $resulttext = '<span class="LC_error">'.
 7665: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7666:     }
 7667:     if ($errors) {
 7668:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 7669:                        $errors.'</ul>';
 7670:     }
 7671:     return $resulttext;
 7672: }
 7673: 
 7674: sub modify_colors {
 7675:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 7676:     my (%changes,%choices);
 7677:     my @bgs;
 7678:     my @links = ('link','alink','vlink');
 7679:     my @logintext;
 7680:     my @images;
 7681:     my $servadm = $r->dir_config('lonAdmEMail');
 7682:     my $errors;
 7683:     my %defaults;
 7684:     foreach my $role (@{$roles}) {
 7685:         if ($role eq 'login') {
 7686:             %choices = &login_choices();
 7687:             @logintext = ('textcol','bgcol');
 7688:         } else {
 7689:             %choices = &color_font_choices();
 7690:         }
 7691:         if ($role eq 'login') {
 7692:             @images = ('img','logo','domlogo','login');
 7693:             @bgs = ('pgbg','mainbg','sidebg');
 7694:         } else {
 7695:             @images = ('img');
 7696:             @bgs = ('pgbg','tabbg','sidebg');
 7697:         }
 7698:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 7699:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 7700:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 7701:         }
 7702:         if ($role eq 'login') {
 7703:             foreach my $item (@logintext) {
 7704:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 7705:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 7706:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 7707:                 }
 7708:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 7709:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 7710:                 }
 7711:             }
 7712:         } else {
 7713:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 7714:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 7715:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 7716:             }
 7717:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 7718:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 7719:             }
 7720:         }
 7721:         foreach my $item (@bgs) {
 7722:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 7723:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 7724:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 7725:             }
 7726:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 7727:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 7728:             }
 7729:         }
 7730:         foreach my $item (@links) {
 7731:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 7732:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 7733:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 7734:             }
 7735:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 7736:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 7737:             }
 7738:         }
 7739:         my ($configuserok,$author_ok,$switchserver) = 
 7740:             &config_check($dom,$confname,$servadm);
 7741:         my ($width,$height) = &thumb_dimensions();
 7742:         if (ref($domconfig->{$role}) ne 'HASH') {
 7743:             $domconfig->{$role} = {};
 7744:         }
 7745:         foreach my $img (@images) {
 7746:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 7747:                 if (defined($env{'form.login_showlogo_'.$img})) {
 7748:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 7749:                 } else { 
 7750:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 7751:                 }
 7752:             } 
 7753: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 7754: 		 && !defined($domconfig->{$role}{$img})
 7755: 		 && !$env{'form.'.$role.'_del_'.$img}
 7756: 		 && $env{'form.'.$role.'_import_'.$img}) {
 7757: 		# import the old configured image from the .tab setting
 7758: 		# if they haven't provided a new one 
 7759: 		$domconfig->{$role}{$img} = 
 7760: 		    $env{'form.'.$role.'_import_'.$img};
 7761: 	    }
 7762:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 7763:                 my $error;
 7764:                 if ($configuserok eq 'ok') {
 7765:                     if ($switchserver) {
 7766:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 7767:                     } else {
 7768:                         if ($author_ok eq 'ok') {
 7769:                             my ($result,$logourl) = 
 7770:                                 &publishlogo($r,'upload',$role.'_'.$img,
 7771:                                            $dom,$confname,$img,$width,$height);
 7772:                             if ($result eq 'ok') {
 7773:                                 $confhash->{$role}{$img} = $logourl;
 7774:                                 $changes{$role}{'images'}{$img} = 1;
 7775:                             } else {
 7776:                                 $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);
 7777:                             }
 7778:                         } else {
 7779:                             $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);
 7780:                         }
 7781:                     }
 7782:                 } else {
 7783:                     $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);
 7784:                 }
 7785:                 if ($error) {
 7786:                     &Apache::lonnet::logthis($error);
 7787:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7788:                 }
 7789:             } elsif ($domconfig->{$role}{$img} ne '') {
 7790:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 7791:                     my $error;
 7792:                     if ($configuserok eq 'ok') {
 7793: # is confname an author?
 7794:                         if ($switchserver eq '') {
 7795:                             if ($author_ok eq 'ok') {
 7796:                                 my ($result,$logourl) = 
 7797:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 7798:                                             $dom,$confname,$img,$width,$height);
 7799:                                 if ($result eq 'ok') {
 7800:                                     $confhash->{$role}{$img} = $logourl;
 7801: 				    $changes{$role}{'images'}{$img} = 1;
 7802:                                 }
 7803:                             }
 7804:                         }
 7805:                     }
 7806:                 }
 7807:             }
 7808:         }
 7809:         if (ref($domconfig) eq 'HASH') {
 7810:             if (ref($domconfig->{$role}) eq 'HASH') {
 7811:                 foreach my $img (@images) {
 7812:                     if ($domconfig->{$role}{$img} ne '') {
 7813:                         if ($env{'form.'.$role.'_del_'.$img}) {
 7814:                             $confhash->{$role}{$img} = '';
 7815:                             $changes{$role}{'images'}{$img} = 1;
 7816:                         } else {
 7817:                             if ($confhash->{$role}{$img} eq '') {
 7818:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 7819:                             }
 7820:                         }
 7821:                     } else {
 7822:                         if ($env{'form.'.$role.'_del_'.$img}) {
 7823:                             $confhash->{$role}{$img} = '';
 7824:                             $changes{$role}{'images'}{$img} = 1;
 7825:                         } 
 7826:                     }
 7827:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 7828:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 7829:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 7830:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 7831:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 7832:                             }
 7833:                         } else {
 7834:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 7835:                                 $changes{$role}{'showlogo'}{$img} = 1;
 7836:                             }
 7837:                         }
 7838:                     }
 7839:                 }
 7840:                 if ($domconfig->{$role}{'font'} ne '') {
 7841:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 7842:                         $changes{$role}{'font'} = 1;
 7843:                     }
 7844:                 } else {
 7845:                     if ($confhash->{$role}{'font'}) {
 7846:                         $changes{$role}{'font'} = 1;
 7847:                     }
 7848:                 }
 7849:                 if ($role ne 'login') {
 7850:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 7851:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 7852:                             $changes{$role}{'fontmenu'} = 1;
 7853:                         }
 7854:                     } else {
 7855:                         if ($confhash->{$role}{'fontmenu'}) {
 7856:                             $changes{$role}{'fontmenu'} = 1;
 7857:                         }
 7858:                     }
 7859:                 }
 7860:                 foreach my $item (@bgs) {
 7861:                     if ($domconfig->{$role}{$item} ne '') {
 7862:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 7863:                             $changes{$role}{'bgs'}{$item} = 1;
 7864:                         } 
 7865:                     } else {
 7866:                         if ($confhash->{$role}{$item}) {
 7867:                             $changes{$role}{'bgs'}{$item} = 1;
 7868:                         }
 7869:                     }
 7870:                 }
 7871:                 foreach my $item (@links) {
 7872:                     if ($domconfig->{$role}{$item} ne '') {
 7873:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 7874:                             $changes{$role}{'links'}{$item} = 1;
 7875:                         }
 7876:                     } else {
 7877:                         if ($confhash->{$role}{$item}) {
 7878:                             $changes{$role}{'links'}{$item} = 1;
 7879:                         }
 7880:                     }
 7881:                 }
 7882:                 foreach my $item (@logintext) {
 7883:                     if ($domconfig->{$role}{$item} ne '') {
 7884:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 7885:                             $changes{$role}{'logintext'}{$item} = 1;
 7886:                         }
 7887:                     } else {
 7888:                         if ($confhash->{$role}{$item}) {
 7889:                             $changes{$role}{'logintext'}{$item} = 1;
 7890:                         }
 7891:                     }
 7892:                 }
 7893:             } else {
 7894:                 &default_change_checker($role,\@images,\@links,\@bgs,
 7895:                                         \@logintext,$confhash,\%changes); 
 7896:             }
 7897:         } else {
 7898:             &default_change_checker($role,\@images,\@links,\@bgs,
 7899:                                     \@logintext,$confhash,\%changes); 
 7900:         }
 7901:     }
 7902:     return ($errors,%changes);
 7903: }
 7904: 
 7905: sub config_check {
 7906:     my ($dom,$confname,$servadm) = @_;
 7907:     my ($configuserok,$author_ok,$switchserver,%currroles);
 7908:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 7909:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 7910:                                                    $confname,$servadm);
 7911:     if ($configuserok eq 'ok') {
 7912:         $switchserver = &check_switchserver($dom,$confname);
 7913:         if ($switchserver eq '') {
 7914:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 7915:         }
 7916:     }
 7917:     return ($configuserok,$author_ok,$switchserver);
 7918: }
 7919: 
 7920: sub default_change_checker {
 7921:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 7922:     foreach my $item (@{$links}) {
 7923:         if ($confhash->{$role}{$item}) {
 7924:             $changes->{$role}{'links'}{$item} = 1;
 7925:         }
 7926:     }
 7927:     foreach my $item (@{$bgs}) {
 7928:         if ($confhash->{$role}{$item}) {
 7929:             $changes->{$role}{'bgs'}{$item} = 1;
 7930:         }
 7931:     }
 7932:     foreach my $item (@{$logintext}) {
 7933:         if ($confhash->{$role}{$item}) {
 7934:             $changes->{$role}{'logintext'}{$item} = 1;
 7935:         }
 7936:     }
 7937:     foreach my $img (@{$images}) {
 7938:         if ($env{'form.'.$role.'_del_'.$img}) {
 7939:             $confhash->{$role}{$img} = '';
 7940:             $changes->{$role}{'images'}{$img} = 1;
 7941:         }
 7942:         if ($role eq 'login') {
 7943:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 7944:                 $changes->{$role}{'showlogo'}{$img} = 1;
 7945:             }
 7946:         }
 7947:     }
 7948:     if ($confhash->{$role}{'font'}) {
 7949:         $changes->{$role}{'font'} = 1;
 7950:     }
 7951: }
 7952: 
 7953: sub display_colorchgs {
 7954:     my ($dom,$changes,$roles,$confhash) = @_;
 7955:     my (%choices,$resulttext);
 7956:     if (!grep(/^login$/,@{$roles})) {
 7957:         $resulttext = &mt('Changes made:').'<br />';
 7958:     }
 7959:     foreach my $role (@{$roles}) {
 7960:         if ($role eq 'login') {
 7961:             %choices = &login_choices();
 7962:         } else {
 7963:             %choices = &color_font_choices();
 7964:         }
 7965:         if (ref($changes->{$role}) eq 'HASH') {
 7966:             if ($role ne 'login') {
 7967:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 7968:             }
 7969:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 7970:                 if ($role ne 'login') {
 7971:                     $resulttext .= '<ul>';
 7972:                 }
 7973:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 7974:                     if ($role ne 'login') {
 7975:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 7976:                     }
 7977:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 7978:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 7979:                             if ($confhash->{$role}{$key}{$item}) {
 7980:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 7981:                             } else {
 7982:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 7983:                             }
 7984:                         } elsif ($confhash->{$role}{$item} eq '') {
 7985:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 7986:                         } else {
 7987:                             my $newitem = $confhash->{$role}{$item};
 7988:                             if ($key eq 'images') {
 7989:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 7990:                             }
 7991:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 7992:                         }
 7993:                     }
 7994:                     if ($role ne 'login') {
 7995:                         $resulttext .= '</ul></li>';
 7996:                     }
 7997:                 } else {
 7998:                     if ($confhash->{$role}{$key} eq '') {
 7999:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 8000:                     } else {
 8001:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 8002:                     }
 8003:                 }
 8004:                 if ($role ne 'login') {
 8005:                     $resulttext .= '</ul>';
 8006:                 }
 8007:             }
 8008:         }
 8009:     }
 8010:     return $resulttext;
 8011: }
 8012: 
 8013: sub thumb_dimensions {
 8014:     return ('200','50');
 8015: }
 8016: 
 8017: sub check_dimensions {
 8018:     my ($inputfile) = @_;
 8019:     my ($fullwidth,$fullheight);
 8020:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 8021:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 8022:             my $imageinfo = <PIPE>;
 8023:             if (!close(PIPE)) {
 8024:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 8025:             }
 8026:             chomp($imageinfo);
 8027:             my ($fullsize) = 
 8028:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 8029:             if ($fullsize) {
 8030:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 8031:             }
 8032:         }
 8033:     }
 8034:     return ($fullwidth,$fullheight);
 8035: }
 8036: 
 8037: sub check_configuser {
 8038:     my ($uhome,$dom,$confname,$servadm) = @_;
 8039:     my ($configuserok,%currroles);
 8040:     if ($uhome eq 'no_host') {
 8041:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 8042:         my $configpass = &LONCAPA::Enrollment::create_password();
 8043:         $configuserok = 
 8044:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 8045:                              $configpass,'','','','','',undef,$servadm);
 8046:     } else {
 8047:         $configuserok = 'ok';
 8048:         %currroles = 
 8049:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 8050:     }
 8051:     return ($configuserok,%currroles);
 8052: }
 8053: 
 8054: sub check_authorstatus {
 8055:     my ($dom,$confname,%currroles) = @_;
 8056:     my $author_ok;
 8057:     if (!$currroles{':'.$dom.':au'}) {
 8058:         my $start = time;
 8059:         my $end = 0;
 8060:         $author_ok = 
 8061:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 8062:                                         'au',$end,$start,'','','domconfig');
 8063:     } else {
 8064:         $author_ok = 'ok';
 8065:     }
 8066:     return $author_ok;
 8067: }
 8068: 
 8069: sub publishlogo {
 8070:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 8071:     my ($output,$fname,$logourl);
 8072:     if ($action eq 'upload') {
 8073:         $fname=$env{'form.'.$formname.'.filename'};
 8074:         chop($env{'form.'.$formname});
 8075:     } else {
 8076:         ($fname) = ($formname =~ /([^\/]+)$/);
 8077:     }
 8078:     if ($savefileas ne '') {
 8079:         $fname = $savefileas;
 8080:     }
 8081:     $fname=&Apache::lonnet::clean_filename($fname);
 8082: # See if there is anything left
 8083:     unless ($fname) { return ('error: no uploaded file'); }
 8084:     $fname="$subdir/$fname";
 8085:     my $docroot=$r->dir_config('lonDocRoot');
 8086:     my $filepath="$docroot/priv";
 8087:     my $relpath = "$dom/$confname";
 8088:     my ($fnamepath,$file,$fetchthumb);
 8089:     $file=$fname;
 8090:     if ($fname=~m|/|) {
 8091:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 8092:     }
 8093:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 8094:     my $count;
 8095:     for ($count=5;$count<=$#parts;$count++) {
 8096:         $filepath.="/$parts[$count]";
 8097:         if ((-e $filepath)!=1) {
 8098:             mkdir($filepath,02770);
 8099:         }
 8100:     }
 8101:     # Check for bad extension and disallow upload
 8102:     if ($file=~/\.(\w+)$/ &&
 8103:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 8104:         $output = 
 8105:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 8106:     } elsif ($file=~/\.(\w+)$/ &&
 8107:         !defined(&Apache::loncommon::fileembstyle($1))) {
 8108:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 8109:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 8110:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 8111:     } elsif (-d "$filepath/$file") {
 8112:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 8113:     } else {
 8114:         my $source = $filepath.'/'.$file;
 8115:         my $logfile;
 8116:         if (!open($logfile,">>",$source.'.log')) {
 8117:             return (&mt('No write permission to Authoring Space'));
 8118:         }
 8119:         print $logfile
 8120: "\n================= Publish ".localtime()." ================\n".
 8121: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 8122: # Save the file
 8123:         if (!open(FH,">",$source)) {
 8124:             &Apache::lonnet::logthis('Failed to create '.$source);
 8125:             return (&mt('Failed to create file'));
 8126:         }
 8127:         if ($action eq 'upload') {
 8128:             if (!print FH ($env{'form.'.$formname})) {
 8129:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 8130:                 return (&mt('Failed to write file'));
 8131:             }
 8132:         } else {
 8133:             my $original = &Apache::lonnet::filelocation('',$formname);
 8134:             if(!copy($original,$source)) {
 8135:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 8136:                 return (&mt('Failed to write file'));
 8137:             }
 8138:         }
 8139:         close(FH);
 8140:         chmod(0660, $source); # Permissions to rw-rw---.
 8141: 
 8142:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 8143:         my $copyfile=$targetdir.'/'.$file;
 8144: 
 8145:         my @parts=split(/\//,$targetdir);
 8146:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 8147:         for (my $count=5;$count<=$#parts;$count++) {
 8148:             $path.="/$parts[$count]";
 8149:             if (!-e $path) {
 8150:                 print $logfile "\nCreating directory ".$path;
 8151:                 mkdir($path,02770);
 8152:             }
 8153:         }
 8154:         my $versionresult;
 8155:         if (-e $copyfile) {
 8156:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 8157:         } else {
 8158:             $versionresult = 'ok';
 8159:         }
 8160:         if ($versionresult eq 'ok') {
 8161:             if (copy($source,$copyfile)) {
 8162:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 8163:                 $output = 'ok';
 8164:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 8165:                 push(@{$modified_urls},[$copyfile,$source]);
 8166:                 my $metaoutput = 
 8167:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 8168:                 unless ($registered_cleanup) {
 8169:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 8170:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 8171:                     $registered_cleanup=1;
 8172:                 }
 8173:             } else {
 8174:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 8175:                 $output = &mt('Failed to copy file to RES space').", $!";
 8176:             }
 8177:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 8178:                 my $inputfile = $filepath.'/'.$file;
 8179:                 my $outfile = $filepath.'/'.'tn-'.$file;
 8180:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 8181:                 if ($fullwidth ne '' && $fullheight ne '') { 
 8182:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 8183:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 8184:                         my @args = ('convert','-sample',$thumbsize,$inputfile,$outfile);
 8185:                         system({$args[0]} @args);
 8186:                         chmod(0660, $filepath.'/tn-'.$file);
 8187:                         if (-e $outfile) {
 8188:                             my $copyfile=$targetdir.'/tn-'.$file;
 8189:                             if (copy($outfile,$copyfile)) {
 8190:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 8191:                                 my $thumb_metaoutput = 
 8192:                                     &write_metadata($dom,$confname,$formname,
 8193:                                                     $targetdir,'tn-'.$file,$logfile);
 8194:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 8195:                                 unless ($registered_cleanup) {
 8196:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 8197:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 8198:                                     $registered_cleanup=1;
 8199:                                 }
 8200:                             } else {
 8201:                                 print $logfile "\nUnable to write ".$copyfile.
 8202:                                                ':'.$!."\n";
 8203:                             }
 8204:                         }
 8205:                     }
 8206:                 }
 8207:             }
 8208:         } else {
 8209:             $output = $versionresult;
 8210:         }
 8211:     }
 8212:     return ($output,$logourl);
 8213: }
 8214: 
 8215: sub logo_versioning {
 8216:     my ($targetdir,$file,$logfile) = @_;
 8217:     my $target = $targetdir.'/'.$file;
 8218:     my ($maxversion,$fn,$extn,$output);
 8219:     $maxversion = 0;
 8220:     if ($file =~ /^(.+)\.(\w+)$/) {
 8221:         $fn=$1;
 8222:         $extn=$2;
 8223:     }
 8224:     opendir(DIR,$targetdir);
 8225:     while (my $filename=readdir(DIR)) {
 8226:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 8227:             $maxversion=($1>$maxversion)?$1:$maxversion;
 8228:         }
 8229:     }
 8230:     $maxversion++;
 8231:     print $logfile "\nCreating old version ".$maxversion."\n";
 8232:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 8233:     if (copy($target,$copyfile)) {
 8234:         print $logfile "Copied old target to ".$copyfile."\n";
 8235:         $copyfile=$copyfile.'.meta';
 8236:         if (copy($target.'.meta',$copyfile)) {
 8237:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 8238:             $output = 'ok';
 8239:         } else {
 8240:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 8241:             $output = &mt('Failed to copy old meta').", $!, ";
 8242:         }
 8243:     } else {
 8244:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 8245:         $output = &mt('Failed to copy old target').", $!, ";
 8246:     }
 8247:     return $output;
 8248: }
 8249: 
 8250: sub write_metadata {
 8251:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 8252:     my (%metadatafields,%metadatakeys,$output);
 8253:     $metadatafields{'title'}=$formname;
 8254:     $metadatafields{'creationdate'}=time;
 8255:     $metadatafields{'lastrevisiondate'}=time;
 8256:     $metadatafields{'copyright'}='public';
 8257:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 8258:                                          $env{'user.domain'};
 8259:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 8260:     $metadatafields{'domain'}=$dom;
 8261:     {
 8262:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 8263:         my $mfh;
 8264:         if (open($mfh,">",$targetdir.'/'.$file.'.meta')) {
 8265:             foreach (sort(keys(%metadatafields))) {
 8266:                 unless ($_=~/\./) {
 8267:                     my $unikey=$_;
 8268:                     $unikey=~/^([A-Za-z]+)/;
 8269:                     my $tag=$1;
 8270:                     $tag=~tr/A-Z/a-z/;
 8271:                     print $mfh "\n\<$tag";
 8272:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 8273:                         my $value=$metadatafields{$unikey.'.'.$_};
 8274:                         $value=~s/\"/\'\'/g;
 8275:                         print $mfh ' '.$_.'="'.$value.'"';
 8276:                     }
 8277:                     print $mfh '>'.
 8278:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 8279:                             .'</'.$tag.'>';
 8280:                 }
 8281:             }
 8282:             $output = 'ok';
 8283:             print $logfile "\nWrote metadata";
 8284:             close($mfh);
 8285:         } else {
 8286:             print $logfile "\nFailed to open metadata file";
 8287:             $output = &mt('Could not write metadata');
 8288:         }
 8289:     }
 8290:     return $output;
 8291: }
 8292: 
 8293: sub notifysubscribed {
 8294:     foreach my $targetsource (@{$modified_urls}){
 8295:         next unless (ref($targetsource) eq 'ARRAY');
 8296:         my ($target,$source)=@{$targetsource};
 8297:         if ($source ne '') {
 8298:             if (open(my $logfh,">>",$source.'.log')) {
 8299:                 print $logfh "\nCleanup phase: Notifications\n";
 8300:                 my @subscribed=&subscribed_hosts($target);
 8301:                 foreach my $subhost (@subscribed) {
 8302:                     print $logfh "\nNotifying host ".$subhost.':';
 8303:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 8304:                     print $logfh $reply;
 8305:                 }
 8306:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 8307:                 foreach my $subhost (@subscribedmeta) {
 8308:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 8309:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 8310:                                                         $subhost);
 8311:                     print $logfh $reply;
 8312:                 }
 8313:                 print $logfh "\n============ Done ============\n";
 8314:                 close($logfh);
 8315:             }
 8316:         }
 8317:     }
 8318:     return OK;
 8319: }
 8320: 
 8321: sub subscribed_hosts {
 8322:     my ($target) = @_;
 8323:     my @subscribed;
 8324:     if (open(my $fh,"<","$target.subscription")) {
 8325:         while (my $subline=<$fh>) {
 8326:             if ($subline =~ /^($match_lonid):/) {
 8327:                 my $host = $1;
 8328:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 8329:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 8330:                         push(@subscribed,$host);
 8331:                     }
 8332:                 }
 8333:             }
 8334:         }
 8335:     }
 8336:     return @subscribed;
 8337: }
 8338: 
 8339: sub check_switchserver {
 8340:     my ($dom,$confname) = @_;
 8341:     my ($allowed,$switchserver);
 8342:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 8343:     if ($home eq 'no_host') {
 8344:         $home = &Apache::lonnet::domain($dom,'primary');
 8345:     }
 8346:     my @ids=&Apache::lonnet::current_machine_ids();
 8347:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 8348:     if (!$allowed) {
 8349: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 8350:     }
 8351:     return $switchserver;
 8352: }
 8353: 
 8354: sub modify_quotas {
 8355:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 8356:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 8357:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 8358:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 8359:         $validationfieldsref);
 8360:     if ($action eq 'quotas') {
 8361:         $context = 'tools'; 
 8362:     } else {
 8363:         $context = $action;
 8364:     }
 8365:     if ($context eq 'requestcourses') {
 8366:         @usertools = ('official','unofficial','community','textbook');
 8367:         @options =('norequest','approval','validate','autolimit');
 8368:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 8369:         %titles = &courserequest_titles();
 8370:         $toolregexp = join('|',@usertools);
 8371:         %conditions = &courserequest_conditions();
 8372:         $confname = $dom.'-domainconfig';
 8373:         my $servadm = $r->dir_config('lonAdmEMail');
 8374:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 8375:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 8376:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 8377:     } elsif ($context eq 'requestauthor') {
 8378:         @usertools = ('author');
 8379:         %titles = &authorrequest_titles();
 8380:     } else {
 8381:         @usertools = ('aboutme','blog','webdav','portfolio');
 8382:         %titles = &tool_titles();
 8383:     }
 8384:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8385:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8386:     foreach my $key (keys(%env)) {
 8387:         if ($context eq 'requestcourses') {
 8388:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 8389:                 my $item = $1;
 8390:                 my $type = $2;
 8391:                 if ($type =~ /^limit_(.+)/) {
 8392:                     $limithash{$item}{$1} = $env{$key};
 8393:                 } else {
 8394:                     $confhash{$item}{$type} = $env{$key};
 8395:                 }
 8396:             }
 8397:         } elsif ($context eq 'requestauthor') {
 8398:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 8399:                 $confhash{$1} = $env{$key};
 8400:             }
 8401:         } else {
 8402:             if ($key =~ /^form\.quota_(.+)$/) {
 8403:                 $confhash{'defaultquota'}{$1} = $env{$key};
 8404:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 8405:                 $confhash{'authorquota'}{$1} = $env{$key};
 8406:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 8407:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 8408:             }
 8409:         }
 8410:     }
 8411:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 8412:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 8413:         @approvalnotify = sort(@approvalnotify);
 8414:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 8415:         my @crstypes = ('official','unofficial','community','textbook');
 8416:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 8417:         foreach my $type (@hasuniquecode) {
 8418:             if (grep(/^\Q$type\E$/,@crstypes)) {
 8419:                 $confhash{'uniquecode'}{$type} = 1;
 8420:             }
 8421:         }
 8422:         my (%newbook,%allpos);
 8423:         if ($context eq 'requestcourses') {
 8424:             foreach my $type ('textbooks','templates') {
 8425:                 @{$allpos{$type}} = (); 
 8426:                 my $invalid;
 8427:                 if ($type eq 'textbooks') {
 8428:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
 8429:                 } else {
 8430:                     $invalid = &mt('Invalid LON-CAPA course for template');
 8431:                 }
 8432:                 if ($env{'form.'.$type.'_addbook'}) {
 8433:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
 8434:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
 8435:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
 8436:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
 8437:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 8438:                         } else {
 8439:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
 8440:                             my $position = $env{'form.'.$type.'_addbook_pos'};
 8441:                             $position =~ s/\D+//g;
 8442:                             if ($position ne '') {
 8443:                                 $allpos{$type}[$position] = $newbook{$type};
 8444:                             }
 8445:                         }
 8446:                     } else {
 8447:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 8448:                     }
 8449:                 }
 8450:             } 
 8451:         }
 8452:         if (ref($domconfig{$action}) eq 'HASH') {
 8453:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 8454:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 8455:                     $changes{'notify'}{'approval'} = 1;
 8456:                 }
 8457:             } else {
 8458:                 if ($confhash{'notify'}{'approval'}) {
 8459:                     $changes{'notify'}{'approval'} = 1;
 8460:                 }
 8461:             }
 8462:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 8463:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 8464:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 8465:                         unless ($confhash{'uniquecode'}{$crstype}) {
 8466:                             $changes{'uniquecode'} = 1;
 8467:                         }
 8468:                     }
 8469:                     unless ($changes{'uniquecode'}) {
 8470:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 8471:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 8472:                                 $changes{'uniquecode'} = 1;
 8473:                             }
 8474:                         }
 8475:                     }
 8476:                } else {
 8477:                    $changes{'uniquecode'} = 1;
 8478:                }
 8479:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 8480:                 $changes{'uniquecode'} = 1;
 8481:             }
 8482:             if ($context eq 'requestcourses') {
 8483:                 foreach my $type ('textbooks','templates') {
 8484:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
 8485:                         my %deletions;
 8486:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
 8487:                         if (@todelete) {
 8488:                             map { $deletions{$_} = 1; } @todelete;
 8489:                         }
 8490:                         my %imgdeletions;
 8491:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
 8492:                         if (@todeleteimages) {
 8493:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
 8494:                         }
 8495:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
 8496:                         for (my $i=0; $i<=$maxnum; $i++) {
 8497:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
 8498:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
 8499:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
 8500:                                 if ($deletions{$key}) {
 8501:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
 8502:                                         #FIXME need to obsolete item in RES space
 8503:                                     }
 8504:                                     next;
 8505:                                 } else {
 8506:                                     my $newpos = $env{'form.'.$itemid};
 8507:                                     $newpos =~ s/\D+//g;
 8508:                                     foreach my $item ('subject','title','publisher','author') {
 8509:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 8510:                                                  ($type eq 'templates'));
 8511:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
 8512:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
 8513:                                             $changes{$type}{$key} = 1;
 8514:                                         }
 8515:                                     }
 8516:                                     $allpos{$type}[$newpos] = $key;
 8517:                                 }
 8518:                                 if ($imgdeletions{$key}) {
 8519:                                     $changes{$type}{$key} = 1;
 8520:                                     #FIXME need to obsolete item in RES space
 8521:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
 8522:                                     my ($cdom,$cnum) = split(/_/,$key);
 8523:                                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
 8524:                                         $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
 8525:                                     } else {
 8526:                                         my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
 8527:                                                                                       $cdom,$cnum,$type,$configuserok,
 8528:                                                                                       $switchserver,$author_ok);
 8529:                                         if ($imgurl) {
 8530:                                             $confhash{$type}{$key}{'image'} = $imgurl;
 8531:                                             $changes{$type}{$key} = 1; 
 8532:                                         }
 8533:                                         if ($error) {
 8534:                                             &Apache::lonnet::logthis($error);
 8535:                                             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8536:                                         }
 8537:                                     } 
 8538:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
 8539:                                     $confhash{$type}{$key}{'image'} = 
 8540:                                         $domconfig{$action}{$type}{$key}{'image'};
 8541:                                 }
 8542:                             }
 8543:                         }
 8544:                     }
 8545:                 }
 8546:             }
 8547:         } else {
 8548:             if ($confhash{'notify'}{'approval'}) {
 8549:                 $changes{'notify'}{'approval'} = 1;
 8550:             }
 8551:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 8552:                 $changes{'uniquecode'} = 1;
 8553:             }
 8554:         }
 8555:         if ($context eq 'requestcourses') {
 8556:             foreach my $type ('textbooks','templates') {
 8557:                 if ($newbook{$type}) {
 8558:                     $changes{$type}{$newbook{$type}} = 1;
 8559:                     foreach my $item ('subject','title','publisher','author') {
 8560:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 8561:                                  ($type eq 'template'));
 8562:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
 8563:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
 8564:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
 8565:                         }
 8566:                     }
 8567:                     if ($type eq 'textbooks') {
 8568:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
 8569:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
 8570:                             if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
 8571:                                 $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
 8572:                             } else {
 8573:                                 my ($imageurl,$error) =
 8574:                                     &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
 8575:                                                             $configuserok,$switchserver,$author_ok);
 8576:                                 if ($imageurl) {
 8577:                                     $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
 8578:                                 }
 8579:                                 if ($error) {
 8580:                                     &Apache::lonnet::logthis($error);
 8581:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8582:                                 }
 8583:                             }
 8584:                         }
 8585:                     }
 8586:                 }
 8587:                 if (@{$allpos{$type}} > 0) {
 8588:                     my $idx = 0;
 8589:                     foreach my $item (@{$allpos{$type}}) {
 8590:                         if ($item ne '') {
 8591:                             $confhash{$type}{$item}{'order'} = $idx;
 8592:                             if (ref($domconfig{$action}) eq 'HASH') {
 8593:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
 8594:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
 8595:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
 8596:                                             $changes{$type}{$item} = 1;
 8597:                                         }
 8598:                                     }
 8599:                                 }
 8600:                             }
 8601:                             $idx ++;
 8602:                         }
 8603:                     }
 8604:                 }
 8605:             }
 8606:             if (ref($validationitemsref) eq 'ARRAY') {
 8607:                 foreach my $item (@{$validationitemsref}) {
 8608:                     if ($item eq 'fields') {
 8609:                         my @changed;
 8610:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 8611:                         if (@{$confhash{'validation'}{$item}} > 0) {
 8612:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 8613:                         }
 8614:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8615:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8616:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 8617:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 8618:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
 8619:                                 } else {
 8620:                                     @changed = @{$confhash{'validation'}{$item}};
 8621:                                 }
 8622:                             } else {
 8623:                                 @changed = @{$confhash{'validation'}{$item}};
 8624:                             }
 8625:                         } else {
 8626:                             @changed = @{$confhash{'validation'}{$item}};
 8627:                         }
 8628:                         if (@changed) {
 8629:                             if ($confhash{'validation'}{$item}) {
 8630:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 8631:                             } else {
 8632:                                 $changes{'validation'}{$item} = &mt('None');
 8633:                             }
 8634:                         }
 8635:                     } else {
 8636:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 8637:                         if ($item eq 'markup') {
 8638:                             if ($env{'form.requestcourses_validation_'.$item}) {
 8639:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 8640:                             }
 8641:                         }
 8642:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8643:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8644:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 8645:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 8646:                                 }
 8647:                             } else {
 8648:                                 if ($confhash{'validation'}{$item} ne '') {
 8649:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 8650:                                 }
 8651:                             }
 8652:                         } else {
 8653:                             if ($confhash{'validation'}{$item} ne '') {
 8654:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 8655:                             }
 8656:                         }
 8657:                     }
 8658:                 }
 8659:             }
 8660:             if ($env{'form.validationdc'}) {
 8661:                 my $newval = $env{'form.validationdc'};
 8662:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
 8663:                 if (exists($domcoords{$newval})) {
 8664:                     $confhash{'validation'}{'dc'} = $newval;
 8665:                 }
 8666:             }
 8667:             if (ref($confhash{'validation'}) eq 'HASH') {
 8668:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8669:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8670:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 8671:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 8672:                                 if ($confhash{'validation'}{'dc'} eq '') {
 8673:                                     $changes{'validation'}{'dc'} = &mt('None');
 8674:                                 } else {
 8675:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8676:                                 }
 8677:                             }
 8678:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
 8679:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8680:                         }
 8681:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 8682:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8683:                     }
 8684:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 8685:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8686:                 }
 8687:             } else {
 8688:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8689:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8690:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 8691:                             $changes{'validation'}{'dc'} = &mt('None');
 8692:                         }
 8693:                     }
 8694:                 }
 8695:             }
 8696:         }
 8697:     } else {
 8698:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 8699:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 8700:     }
 8701:     foreach my $item (@usertools) {
 8702:         foreach my $type (@{$types},'default','_LC_adv') {
 8703:             my $unset; 
 8704:             if ($context eq 'requestcourses') {
 8705:                 $unset = '0';
 8706:                 if ($type eq '_LC_adv') {
 8707:                     $unset = '';
 8708:                 }
 8709:                 if ($confhash{$item}{$type} eq 'autolimit') {
 8710:                     $confhash{$item}{$type} .= '=';
 8711:                     unless ($limithash{$item}{$type} =~ /\D/) {
 8712:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 8713:                     }
 8714:                 }
 8715:             } elsif ($context eq 'requestauthor') {
 8716:                 $unset = '0';
 8717:                 if ($type eq '_LC_adv') {
 8718:                     $unset = '';
 8719:                 }
 8720:             } else {
 8721:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 8722:                     $confhash{$item}{$type} = 1;
 8723:                 } else {
 8724:                     $confhash{$item}{$type} = 0;
 8725:                 }
 8726:             }
 8727:             if (ref($domconfig{$action}) eq 'HASH') {
 8728:                 if ($action eq 'requestauthor') {
 8729:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 8730:                         $changes{$type} = 1;
 8731:                     }
 8732:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 8733:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 8734:                         $changes{$item}{$type} = 1;
 8735:                     }
 8736:                 } else {
 8737:                     if ($context eq 'requestcourses') {
 8738:                         if ($confhash{$item}{$type} ne $unset) {
 8739:                             $changes{$item}{$type} = 1;
 8740:                         }
 8741:                     } else {
 8742:                         if (!$confhash{$item}{$type}) {
 8743:                             $changes{$item}{$type} = 1;
 8744:                         }
 8745:                     }
 8746:                 }
 8747:             } else {
 8748:                 if ($context eq 'requestcourses') {
 8749:                     if ($confhash{$item}{$type} ne $unset) {
 8750:                         $changes{$item}{$type} = 1;
 8751:                     }
 8752:                 } elsif ($context eq 'requestauthor') {
 8753:                     if ($confhash{$type} ne $unset) {
 8754:                         $changes{$type} = 1;
 8755:                     }
 8756:                 } else {
 8757:                     if (!$confhash{$item}{$type}) {
 8758:                         $changes{$item}{$type} = 1;
 8759:                     }
 8760:                 }
 8761:             }
 8762:         }
 8763:     }
 8764:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 8765:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 8766:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 8767:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 8768:                     if (exists($confhash{'defaultquota'}{$key})) {
 8769:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 8770:                             $changes{'defaultquota'}{$key} = 1;
 8771:                         }
 8772:                     } else {
 8773:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 8774:                     }
 8775:                 }
 8776:             } else {
 8777:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 8778:                     if (exists($confhash{'defaultquota'}{$key})) {
 8779:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 8780:                             $changes{'defaultquota'}{$key} = 1;
 8781:                         }
 8782:                     } else {
 8783:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 8784:                     }
 8785:                 }
 8786:             }
 8787:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 8788:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 8789:                     if (exists($confhash{'authorquota'}{$key})) {
 8790:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 8791:                             $changes{'authorquota'}{$key} = 1;
 8792:                         }
 8793:                     } else {
 8794:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 8795:                     }
 8796:                 }
 8797:             }
 8798:         }
 8799:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 8800:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 8801:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 8802:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 8803:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 8804:                             $changes{'defaultquota'}{$key} = 1;
 8805:                         }
 8806:                     } else {
 8807:                         if (!exists($domconfig{'quotas'}{$key})) {
 8808:                             $changes{'defaultquota'}{$key} = 1;
 8809:                         }
 8810:                     }
 8811:                 } else {
 8812:                     $changes{'defaultquota'}{$key} = 1;
 8813:                 }
 8814:             }
 8815:         }
 8816:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 8817:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 8818:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 8819:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 8820:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 8821:                             $changes{'authorquota'}{$key} = 1;
 8822:                         }
 8823:                     } else {
 8824:                         $changes{'authorquota'}{$key} = 1;
 8825:                     }
 8826:                 } else {
 8827:                     $changes{'authorquota'}{$key} = 1;
 8828:                 }
 8829:             }
 8830:         }
 8831:     }
 8832: 
 8833:     if ($context eq 'requestauthor') {
 8834:         $domdefaults{'requestauthor'} = \%confhash;
 8835:     } else {
 8836:         foreach my $key (keys(%confhash)) {
 8837:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
 8838:                 $domdefaults{$key} = $confhash{$key};
 8839:             }
 8840:         }
 8841:     }
 8842: 
 8843:     my %quotahash = (
 8844:                       $action => { %confhash }
 8845:                     );
 8846:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 8847:                                              $dom);
 8848:     if ($putresult eq 'ok') {
 8849:         if (keys(%changes) > 0) {
 8850:             my $cachetime = 24*60*60;
 8851:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 8852:             if (ref($lastactref) eq 'HASH') {
 8853:                 $lastactref->{'domdefaults'} = 1;
 8854:             }
 8855:             $resulttext = &mt('Changes made:').'<ul>';
 8856:             unless (($context eq 'requestcourses') ||
 8857:                     ($context eq 'requestauthor')) {
 8858:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 8859:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 8860:                     foreach my $type (@{$types},'default') {
 8861:                         if (defined($changes{'defaultquota'}{$type})) {
 8862:                             my $typetitle = $usertypes->{$type};
 8863:                             if ($type eq 'default') {
 8864:                                 $typetitle = $othertitle;
 8865:                             }
 8866:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 8867:                         }
 8868:                     }
 8869:                     $resulttext .= '</ul></li>';
 8870:                 }
 8871:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 8872:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 8873:                     foreach my $type (@{$types},'default') {
 8874:                         if (defined($changes{'authorquota'}{$type})) {
 8875:                             my $typetitle = $usertypes->{$type};
 8876:                             if ($type eq 'default') {
 8877:                                 $typetitle = $othertitle;
 8878:                             }
 8879:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 8880:                         }
 8881:                     }
 8882:                     $resulttext .= '</ul></li>';
 8883:                 }
 8884:             }
 8885:             my %newenv;
 8886:             foreach my $item (@usertools) {
 8887:                 my (%haschgs,%inconf);
 8888:                 if ($context eq 'requestauthor') {
 8889:                     %haschgs = %changes;
 8890:                     %inconf = %confhash;
 8891:                 } else {
 8892:                     if (ref($changes{$item}) eq 'HASH') {
 8893:                         %haschgs = %{$changes{$item}};
 8894:                     }
 8895:                     if (ref($confhash{$item}) eq 'HASH') {
 8896:                         %inconf = %{$confhash{$item}};
 8897:                     }
 8898:                 }
 8899:                 if (keys(%haschgs) > 0) {
 8900:                     my $newacc = 
 8901:                         &Apache::lonnet::usertools_access($env{'user.name'},
 8902:                                                           $env{'user.domain'},
 8903:                                                           $item,'reload',$context);
 8904:                     if (($context eq 'requestcourses') ||
 8905:                         ($context eq 'requestauthor')) {
 8906:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 8907:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 8908:                         }
 8909:                     } else {
 8910:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 8911:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 8912:                         }
 8913:                     }
 8914:                     unless ($context eq 'requestauthor') {
 8915:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 8916:                     }
 8917:                     foreach my $type (@{$types},'default','_LC_adv') {
 8918:                         if ($haschgs{$type}) {
 8919:                             my $typetitle = $usertypes->{$type};
 8920:                             if ($type eq 'default') {
 8921:                                 $typetitle = $othertitle;
 8922:                             } elsif ($type eq '_LC_adv') {
 8923:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 8924:                             }
 8925:                             if ($inconf{$type}) {
 8926:                                 if ($context eq 'requestcourses') {
 8927:                                     my $cond;
 8928:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 8929:                                         if ($1 eq '') {
 8930:                                             $cond = &mt('(Automatic processing of any request).');
 8931:                                         } else {
 8932:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 8933:                                         }
 8934:                                     } else { 
 8935:                                         $cond = $conditions{$inconf{$type}};
 8936:                                     }
 8937:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 8938:                                 } elsif ($context eq 'requestauthor') {
 8939:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 8940:                                                              $titles{$inconf{$type}},$typetitle);
 8941: 
 8942:                                 } else {
 8943:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 8944:                                 }
 8945:                             } else {
 8946:                                 if ($type eq '_LC_adv') {
 8947:                                     if ($inconf{$type} eq '0') {
 8948:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 8949:                                     } else { 
 8950:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 8951:                                     }
 8952:                                 } else {
 8953:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 8954:                                 }
 8955:                             }
 8956:                         }
 8957:                     }
 8958:                     unless ($context eq 'requestauthor') {
 8959:                         $resulttext .= '</ul></li>';
 8960:                     }
 8961:                 }
 8962:             }
 8963:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 8964:                 if (ref($changes{'notify'}) eq 'HASH') {
 8965:                     if ($changes{'notify'}{'approval'}) {
 8966:                         if (ref($confhash{'notify'}) eq 'HASH') {
 8967:                             if ($confhash{'notify'}{'approval'}) {
 8968:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 8969:                             } else {
 8970:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 8971:                             }
 8972:                         }
 8973:                     }
 8974:                 }
 8975:             }
 8976:             if ($action eq 'requestcourses') {
 8977:                 my @offon = ('off','on');
 8978:                 if ($changes{'uniquecode'}) {
 8979:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 8980:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 8981:                         $resulttext .= '<li>'.
 8982:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 8983:                                        '</li>';
 8984:                     } else {
 8985:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 8986:                                        '</li>';
 8987:                     }
 8988:                 }
 8989:                 foreach my $type ('textbooks','templates') {
 8990:                     if (ref($changes{$type}) eq 'HASH') {
 8991:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
 8992:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
 8993:                             my %coursehash = &Apache::lonnet::coursedescription($key);
 8994:                             my $coursetitle = $coursehash{'description'};
 8995:                             my $position = $confhash{$type}{$key}{'order'} + 1;
 8996:                             $resulttext .= '<li>';
 8997:                             foreach my $item ('subject','title','publisher','author') {
 8998:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
 8999:                                          ($type eq 'templates'));
 9000:                                 my $name = $item.':';
 9001:                                 $name =~ s/^(\w)/\U$1/;
 9002:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
 9003:                             }
 9004:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 9005:                             if ($type eq 'textbooks') {
 9006:                                 if ($confhash{$type}{$key}{'image'}) {
 9007:                                     $resulttext .= ' '.&mt('Image: [_1]',
 9008:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
 9009:                                                    ' alt="Textbook cover" />').'<br />';
 9010:                                 }
 9011:                             }
 9012:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 9013:                         }
 9014:                         $resulttext .= '</ul></li>';
 9015:                     }
 9016:                 }
 9017:                 if (ref($changes{'validation'}) eq 'HASH') {
 9018:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 9019:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 9020:                         foreach my $item (@{$validationitemsref}) {
 9021:                             if (exists($changes{'validation'}{$item})) {
 9022:                                 if ($item eq 'markup') {
 9023:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9024:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 9025:                                 } else {
 9026:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9027:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 9028:                                 }
 9029:                             }
 9030:                         }
 9031:                         if (exists($changes{'validation'}{'dc'})) {
 9032:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 9033:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 9034:                         }
 9035:                     }
 9036:                 }
 9037:             }
 9038:             $resulttext .= '</ul>';
 9039:             if (keys(%newenv)) {
 9040:                 &Apache::lonnet::appenv(\%newenv);
 9041:             }
 9042:         } else {
 9043:             if ($context eq 'requestcourses') {
 9044:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 9045:             } elsif ($context eq 'requestauthor') {
 9046:                 $resulttext = &mt('No changes made to rights to request author space.');
 9047:             } else {
 9048:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 9049:             }
 9050:         }
 9051:     } else {
 9052:         $resulttext = '<span class="LC_error">'.
 9053: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9054:     }
 9055:     if ($errors) {
 9056:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 9057:                        '<ul>'.$errors.'</ul></p>';
 9058:     }
 9059:     return $resulttext;
 9060: }
 9061: 
 9062: sub process_textbook_image {
 9063:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
 9064:     my $filename = $env{'form.'.$caller.'.filename'};
 9065:     my ($error,$url);
 9066:     my ($width,$height) = (50,50);
 9067:     if ($configuserok eq 'ok') {
 9068:         if ($switchserver) {
 9069:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 9070:                          $switchserver);
 9071:         } elsif ($author_ok eq 'ok') {
 9072:             my ($result,$imageurl) =
 9073:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 9074:                              "$type/$cdom/$cnum/cover",$width,$height);
 9075:             if ($result eq 'ok') {
 9076:                 $url = $imageurl;
 9077:             } else {
 9078:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 9079:             }
 9080:         } else {
 9081:             $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);
 9082:         }
 9083:     } else {
 9084:         $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);
 9085:     }
 9086:     return ($url,$error);
 9087: }
 9088: 
 9089: sub modify_autoenroll {
 9090:     my ($dom,$lastactref,%domconfig) = @_;
 9091:     my ($resulttext,%changes);
 9092:     my %currautoenroll;
 9093:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 9094:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 9095:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 9096:         }
 9097:     }
 9098:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 9099:     my %title = ( run => 'Auto-enrollment active',
 9100:                   sender => 'Sender for notification messages',
 9101:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
 9102:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
 9103:     my @offon = ('off','on');
 9104:     my $sender_uname = $env{'form.sender_uname'};
 9105:     my $sender_domain = $env{'form.sender_domain'};
 9106:     if ($sender_domain eq '') {
 9107:         $sender_uname = '';
 9108:     } elsif ($sender_uname eq '') {
 9109:         $sender_domain = '';
 9110:     }
 9111:     my $coowners = $env{'form.autoassign_coowners'};
 9112:     my $failsafe = $env{'form.autoenroll_failsafe'};
 9113:     $failsafe =~ s{^\s+|\s+$}{}g;
 9114:     if ($failsafe =~ /\D/) {
 9115:         undef($failsafe);
 9116:     }
 9117:     my %autoenrollhash =  (
 9118:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 9119:                                        'sender_uname' => $sender_uname,
 9120:                                        'sender_domain' => $sender_domain,
 9121:                                        'co-owners' => $coowners,
 9122:                                        'autofailsafe' => $failsafe,
 9123:                                 }
 9124:                      );
 9125:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 9126:                                              $dom);
 9127:     if ($putresult eq 'ok') {
 9128:         if (exists($currautoenroll{'run'})) {
 9129:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 9130:                  $changes{'run'} = 1;
 9131:              }
 9132:         } elsif ($autorun) {
 9133:             if ($env{'form.autoenroll_run'} ne '1') {
 9134:                  $changes{'run'} = 1;
 9135:             }
 9136:         }
 9137:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 9138:             $changes{'sender'} = 1;
 9139:         }
 9140:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 9141:             $changes{'sender'} = 1;
 9142:         }
 9143:         if ($currautoenroll{'co-owners'} ne '') {
 9144:             if ($currautoenroll{'co-owners'} ne $coowners) {
 9145:                 $changes{'coowners'} = 1;
 9146:             }
 9147:         } elsif ($coowners) {
 9148:             $changes{'coowners'} = 1;
 9149:         }
 9150:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
 9151:             $changes{'autofailsafe'} = 1;
 9152:         }
 9153:         if (keys(%changes) > 0) {
 9154:             $resulttext = &mt('Changes made:').'<ul>';
 9155:             if ($changes{'run'}) {
 9156:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 9157:             }
 9158:             if ($changes{'sender'}) {
 9159:                 if ($sender_uname eq '' || $sender_domain eq '') {
 9160:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 9161:                 } else {
 9162:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 9163:                 }
 9164:             }
 9165:             if ($changes{'coowners'}) {
 9166:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 9167:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 9168:                 if (ref($lastactref) eq 'HASH') {
 9169:                     $lastactref->{'domainconfig'} = 1;
 9170:                 }
 9171:             }
 9172:             if ($changes{'autofailsafe'}) {
 9173:                 if ($failsafe ne '') {
 9174:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$failsafe).'</li>';
 9175:                 } else {
 9176:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section: deleted');
 9177:                 }
 9178:                 &Apache::lonnet::get_domain_defaults($dom,1);
 9179:                 if (ref($lastactref) eq 'HASH') {
 9180:                     $lastactref->{'domdefaults'} = 1;
 9181:                 }
 9182:             }
 9183:             $resulttext .= '</ul>';
 9184:         } else {
 9185:             $resulttext = &mt('No changes made to auto-enrollment settings');
 9186:         }
 9187:     } else {
 9188:         $resulttext = '<span class="LC_error">'.
 9189: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9190:     }
 9191:     return $resulttext;
 9192: }
 9193: 
 9194: sub modify_autoupdate {
 9195:     my ($dom,%domconfig) = @_;
 9196:     my ($resulttext,%currautoupdate,%fields,%changes);
 9197:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 9198:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 9199:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 9200:         }
 9201:     }
 9202:     my @offon = ('off','on');
 9203:     my %title = &Apache::lonlocal::texthash (
 9204:                    run => 'Auto-update:',
 9205:                    classlists => 'Updates to user information in classlists?'
 9206:                 );
 9207:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9208:     my %fieldtitles = &Apache::lonlocal::texthash (
 9209:                         id => 'Student/Employee ID',
 9210:                         permanentemail => 'E-mail address',
 9211:                         lastname => 'Last Name',
 9212:                         firstname => 'First Name',
 9213:                         middlename => 'Middle Name',
 9214:                         generation => 'Generation',
 9215:                       );
 9216:     $othertitle = &mt('All users');
 9217:     if (keys(%{$usertypes}) >  0) {
 9218:         $othertitle = &mt('Other users');
 9219:     }
 9220:     foreach my $key (keys(%env)) {
 9221:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 9222:             my ($usertype,$item) = ($1,$2);
 9223:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 9224:                 if ($usertype eq 'default') {   
 9225:                     push(@{$fields{$1}},$2);
 9226:                 } elsif (ref($types) eq 'ARRAY') {
 9227:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 9228:                         push(@{$fields{$1}},$2);
 9229:                     }
 9230:                 }
 9231:             }
 9232:         }
 9233:     }
 9234:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 9235:     @lockablenames = sort(@lockablenames);
 9236:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 9237:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 9238:         if (@changed) {
 9239:             $changes{'lockablenames'} = 1;
 9240:         }
 9241:     } else {
 9242:         if (@lockablenames) {
 9243:             $changes{'lockablenames'} = 1;
 9244:         }
 9245:     }
 9246:     my %updatehash = (
 9247:                       autoupdate => { run => $env{'form.autoupdate_run'},
 9248:                                       classlists => $env{'form.classlists'},
 9249:                                       fields => {%fields},
 9250:                                       lockablenames => \@lockablenames,
 9251:                                     }
 9252:                      );
 9253:     foreach my $key (keys(%currautoupdate)) {
 9254:         if (($key eq 'run') || ($key eq 'classlists')) {
 9255:             if (exists($updatehash{autoupdate}{$key})) {
 9256:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 9257:                     $changes{$key} = 1;
 9258:                 }
 9259:             }
 9260:         } elsif ($key eq 'fields') {
 9261:             if (ref($currautoupdate{$key}) eq 'HASH') {
 9262:                 foreach my $item (@{$types},'default') {
 9263:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 9264:                         my $change = 0;
 9265:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 9266:                             if (!exists($fields{$item})) {
 9267:                                 $change = 1;
 9268:                                 last;
 9269:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 9270:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 9271:                                     $change = 1;
 9272:                                     last;
 9273:                                 }
 9274:                             }
 9275:                         }
 9276:                         if ($change) {
 9277:                             push(@{$changes{$key}},$item);
 9278:                         }
 9279:                     } 
 9280:                 }
 9281:             }
 9282:         } elsif ($key eq 'lockablenames') {
 9283:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 9284:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 9285:                 if (@changed) {
 9286:                     $changes{'lockablenames'} = 1;
 9287:                 }
 9288:             } else {
 9289:                 if (@lockablenames) {
 9290:                     $changes{'lockablenames'} = 1;
 9291:                 }
 9292:             }
 9293:         }
 9294:     }
 9295:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 9296:         if (@lockablenames) {
 9297:             $changes{'lockablenames'} = 1;
 9298:         }
 9299:     }
 9300:     foreach my $item (@{$types},'default') {
 9301:         if (defined($fields{$item})) {
 9302:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 9303:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 9304:                     my $change = 0;
 9305:                     if (ref($fields{$item}) eq 'ARRAY') {
 9306:                         foreach my $type (@{$fields{$item}}) {
 9307:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 9308:                                 $change = 1;
 9309:                                 last;
 9310:                             }
 9311:                         }
 9312:                     }
 9313:                     if ($change) {
 9314:                         push(@{$changes{'fields'}},$item);
 9315:                     }
 9316:                 } else {
 9317:                     push(@{$changes{'fields'}},$item);
 9318:                 }
 9319:             } else {
 9320:                 push(@{$changes{'fields'}},$item);
 9321:             }
 9322:         }
 9323:     }
 9324:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 9325:                                              $dom);
 9326:     if ($putresult eq 'ok') {
 9327:         if (keys(%changes) > 0) {
 9328:             $resulttext = &mt('Changes made:').'<ul>';
 9329:             foreach my $key (sort(keys(%changes))) {
 9330:                 if ($key eq 'lockablenames') {
 9331:                     $resulttext .= '<li>';
 9332:                     if (@lockablenames) {
 9333:                         $usertypes->{'default'} = $othertitle;
 9334:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 9335:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 9336:                     } else {
 9337:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 9338:                     }
 9339:                     $resulttext .= '</li>';
 9340:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 9341:                     foreach my $item (@{$changes{$key}}) {
 9342:                         my @newvalues;
 9343:                         foreach my $type (@{$fields{$item}}) {
 9344:                             push(@newvalues,$fieldtitles{$type});
 9345:                         }
 9346:                         my $newvaluestr;
 9347:                         if (@newvalues > 0) {
 9348:                             $newvaluestr = join(', ',@newvalues);
 9349:                         } else {
 9350:                             $newvaluestr = &mt('none');
 9351:                         }
 9352:                         if ($item eq 'default') {
 9353:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 9354:                         } else {
 9355:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 9356:                         }
 9357:                     }
 9358:                 } else {
 9359:                     my $newvalue;
 9360:                     if ($key eq 'run') {
 9361:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 9362:                     } else {
 9363:                         $newvalue = $offon[$env{'form.'.$key}];
 9364:                     }
 9365:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 9366:                 }
 9367:             }
 9368:             $resulttext .= '</ul>';
 9369:         } else {
 9370:             $resulttext = &mt('No changes made to autoupdates');
 9371:         }
 9372:     } else {
 9373:         $resulttext = '<span class="LC_error">'.
 9374: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9375:     }
 9376:     return $resulttext;
 9377: }
 9378: 
 9379: sub modify_autocreate {
 9380:     my ($dom,%domconfig) = @_;
 9381:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 9382:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 9383:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 9384:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 9385:         }
 9386:     }
 9387:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 9388:                  req => 'Auto-creation of validated requests for official courses',
 9389:                  xmldc => 'Identity of course creator of courses from XML files',
 9390:                );
 9391:     my @types = ('xml','req');
 9392:     foreach my $item (@types) {
 9393:         $newvals{$item} = $env{'form.autocreate_'.$item};
 9394:         $newvals{$item} =~ s/\D//g;
 9395:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 9396:     }
 9397:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 9398:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
 9399:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 9400:         $newvals{'xmldc'} = '';
 9401:     } 
 9402:     %autocreatehash =  (
 9403:                         autocreate => { xml => $newvals{'xml'},
 9404:                                         req => $newvals{'req'},
 9405:                                       }
 9406:                        );
 9407:     if ($newvals{'xmldc'} ne '') {
 9408:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 9409:     }
 9410:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 9411:                                              $dom);
 9412:     if ($putresult eq 'ok') {
 9413:         my @items = @types;
 9414:         if ($newvals{'xml'}) {
 9415:             push(@items,'xmldc');
 9416:         }
 9417:         foreach my $item (@items) {
 9418:             if (exists($currautocreate{$item})) {
 9419:                 if ($currautocreate{$item} ne $newvals{$item}) {
 9420:                     $changes{$item} = 1;
 9421:                 }
 9422:             } elsif ($newvals{$item}) {
 9423:                 $changes{$item} = 1;
 9424:             }
 9425:         }
 9426:         if (keys(%changes) > 0) {
 9427:             my @offon = ('off','on'); 
 9428:             $resulttext = &mt('Changes made:').'<ul>';
 9429:             foreach my $item (@types) {
 9430:                 if ($changes{$item}) {
 9431:                     my $newtxt = $offon[$newvals{$item}];
 9432:                     $resulttext .= '<li>'.
 9433:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
 9434:                                        '<b>','</b>').
 9435:                                    '</li>';
 9436:                 }
 9437:             }
 9438:             if ($changes{'xmldc'}) {
 9439:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 9440:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 9441:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
 9442:             }
 9443:             $resulttext .= '</ul>';
 9444:         } else {
 9445:             $resulttext = &mt('No changes made to auto-creation settings');
 9446:         }
 9447:     } else {
 9448:         $resulttext = '<span class="LC_error">'.
 9449:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9450:     }
 9451:     return $resulttext;
 9452: }
 9453: 
 9454: sub modify_directorysrch {
 9455:     my ($dom,$lastactref,%domconfig) = @_;
 9456:     my ($resulttext,%changes);
 9457:     my %currdirsrch;
 9458:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 9459:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 9460:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 9461:         }
 9462:     }
 9463:     my %title = ( available => 'Institutional directory search available',
 9464:                   localonly => 'Other domains can search institution',
 9465:                   lcavailable => 'LON-CAPA directory search available',
 9466:                   lclocalonly => 'Other domains can search LON-CAPA domain',
 9467:                   searchby => 'Search types',
 9468:                   searchtypes => 'Search latitude');
 9469:     my @offon = ('off','on');
 9470:     my @otherdoms = ('Yes','No');
 9471: 
 9472:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 9473:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 9474:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 9475: 
 9476:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9477:     if (keys(%{$usertypes}) == 0) {
 9478:         @cansearch = ('default');
 9479:     } else {
 9480:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 9481:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 9482:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 9483:                     push(@{$changes{'cansearch'}},$type);
 9484:                 }
 9485:             }
 9486:             foreach my $type (@cansearch) {
 9487:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 9488:                     push(@{$changes{'cansearch'}},$type);
 9489:                 }
 9490:             }
 9491:         } else {
 9492:             push(@{$changes{'cansearch'}},@cansearch);
 9493:         }
 9494:     }
 9495: 
 9496:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 9497:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 9498:             if (!grep(/^\Q$by\E$/,@searchby)) {
 9499:                 push(@{$changes{'searchby'}},$by);
 9500:             }
 9501:         }
 9502:         foreach my $by (@searchby) {
 9503:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 9504:                 push(@{$changes{'searchby'}},$by);
 9505:             }
 9506:         }
 9507:     } else {
 9508:         push(@{$changes{'searchby'}},@searchby);
 9509:     }
 9510: 
 9511:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 9512:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 9513:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 9514:                 push(@{$changes{'searchtypes'}},$type);
 9515:             }
 9516:         }
 9517:         foreach my $type (@searchtypes) {
 9518:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 9519:                 push(@{$changes{'searchtypes'}},$type);
 9520:             }
 9521:         }
 9522:     } else {
 9523:         if (exists($currdirsrch{'searchtypes'})) {
 9524:             foreach my $type (@searchtypes) {  
 9525:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 9526:                     push(@{$changes{'searchtypes'}},$type);
 9527:                 }
 9528:             }
 9529:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 9530:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 9531:             }   
 9532:         } else {
 9533:             push(@{$changes{'searchtypes'}},@searchtypes); 
 9534:         }
 9535:     }
 9536: 
 9537:     my %dirsrch_hash =  (
 9538:             directorysrch => { available => $env{'form.dirsrch_available'},
 9539:                                cansearch => \@cansearch,
 9540:                                localonly => $env{'form.dirsrch_instlocalonly'},
 9541:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
 9542:                                lcavailable => $env{'form.dirsrch_domavailable'},
 9543:                                searchby => \@searchby,
 9544:                                searchtypes => \@searchtypes,
 9545:                              }
 9546:             );
 9547:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 9548:                                              $dom);
 9549:     if ($putresult eq 'ok') {
 9550:         if (exists($currdirsrch{'available'})) {
 9551:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 9552:                  $changes{'available'} = 1;
 9553:              }
 9554:         } else {
 9555:             if ($env{'form.dirsrch_available'} eq '1') {
 9556:                 $changes{'available'} = 1;
 9557:             }
 9558:         }
 9559:         if (exists($currdirsrch{'lcavailable'})) {
 9560:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
 9561:                 $changes{'lcavailable'} = 1;
 9562:             }
 9563:         } else {
 9564:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
 9565:                 $changes{'lcavailable'} = 1;
 9566:             }
 9567:         }
 9568:         if (exists($currdirsrch{'localonly'})) {
 9569:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
 9570:                 $changes{'localonly'} = 1;
 9571:             }
 9572:         } else {
 9573:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
 9574:                 $changes{'localonly'} = 1;
 9575:             }
 9576:         }
 9577:         if (exists($currdirsrch{'lclocalonly'})) {
 9578:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
 9579:                 $changes{'lclocalonly'} = 1;
 9580:             }
 9581:         } else {
 9582:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
 9583:                 $changes{'lclocalonly'} = 1;
 9584:             }
 9585:         }
 9586:         if (keys(%changes) > 0) {
 9587:             $resulttext = &mt('Changes made:').'<ul>';
 9588:             if ($changes{'available'}) {
 9589:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 9590:             }
 9591:             if ($changes{'lcavailable'}) {
 9592:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
 9593:             }
 9594:             if ($changes{'localonly'}) {
 9595:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
 9596:             }
 9597:             if ($changes{'lclocalonly'}) {
 9598:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
 9599:             }
 9600:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 9601:                 my $chgtext;
 9602:                 if (ref($usertypes) eq 'HASH') {
 9603:                     if (keys(%{$usertypes}) > 0) {
 9604:                         foreach my $type (@{$types}) {
 9605:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 9606:                                 $chgtext .= $usertypes->{$type}.'; ';
 9607:                             }
 9608:                         }
 9609:                         if (grep(/^default$/,@cansearch)) {
 9610:                             $chgtext .= $othertitle;
 9611:                         } else {
 9612:                             $chgtext =~ s/\; $//;
 9613:                         }
 9614:                         $resulttext .=
 9615:                             '<li>'.
 9616:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
 9617:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
 9618:                             '</li>';
 9619:                     }
 9620:                 }
 9621:             }
 9622:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 9623:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 9624:                 my $chgtext;
 9625:                 foreach my $type (@{$titleorder}) {
 9626:                     if (grep(/^\Q$type\E$/,@searchby)) {
 9627:                         if (defined($searchtitles->{$type})) {
 9628:                             $chgtext .= $searchtitles->{$type}.'; ';
 9629:                         }
 9630:                     }
 9631:                 }
 9632:                 $chgtext =~ s/\; $//;
 9633:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 9634:             }
 9635:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 9636:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 9637:                 my $chgtext;
 9638:                 foreach my $type (@{$srchtypeorder}) {
 9639:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 9640:                         if (defined($srchtypes_desc->{$type})) {
 9641:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 9642:                         }
 9643:                     }
 9644:                 }
 9645:                 $chgtext =~ s/\; $//;
 9646:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
 9647:             }
 9648:             $resulttext .= '</ul>';
 9649:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
 9650:             if (ref($lastactref) eq 'HASH') {
 9651:                 $lastactref->{'directorysrch'} = 1;
 9652:             }
 9653:         } else {
 9654:             $resulttext = &mt('No changes made to directory search settings');
 9655:         }
 9656:     } else {
 9657:         $resulttext = '<span class="LC_error">'.
 9658:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 9659:     }
 9660:     return $resulttext;
 9661: }
 9662: 
 9663: sub modify_contacts {
 9664:     my ($dom,$lastactref,%domconfig) = @_;
 9665:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 9666:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 9667:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 9668:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 9669:         }
 9670:     }
 9671:     my (%others,%to,%bcc,%includestr,%includeloc);
 9672:     my @contacts = ('supportemail','adminemail');
 9673:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
 9674:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail',hostipmail');
 9675:     my @toggles = ('reporterrors','reportupdates');
 9676:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 9677:     foreach my $type (@mailings) {
 9678:         @{$newsetting{$type}} = 
 9679:             &Apache::loncommon::get_env_multiple('form.'.$type);
 9680:         foreach my $item (@contacts) {
 9681:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 9682:                 $contacts_hash{contacts}{$type}{$item} = 1;
 9683:             } else {
 9684:                 $contacts_hash{contacts}{$type}{$item} = 0;
 9685:             }
 9686:         }
 9687:         $others{$type} = $env{'form.'.$type.'_others'};
 9688:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 9689:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 9690:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 9691:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 9692:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
 9693:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
 9694:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
 9695:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
 9696:             }
 9697:         }
 9698:     }
 9699:     foreach my $item (@contacts) {
 9700:         $to{$item} = $env{'form.'.$item};
 9701:         $contacts_hash{'contacts'}{$item} = $to{$item};
 9702:     }
 9703:     foreach my $item (@toggles) {
 9704:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
 9705:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
 9706:         }
 9707:     }
 9708:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
 9709:         foreach my $field (@{$fields}) {
 9710:             if (ref($possoptions->{$field}) eq 'ARRAY') {
 9711:                 my $value = $env{'form.helpform_'.$field};
 9712:                 $value =~ s/^\s+|\s+$//g;
 9713:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
 9714:                     $contacts_hash{contacts}{'helpform'}{$field} = $value;
 9715:                     if ($field eq 'screenshot') {
 9716:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
 9717:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
 9718:                             $contacts_hash{contacts}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
 9719:                         }
 9720:                     }
 9721:                 }
 9722:             }
 9723:         }
 9724:     }
 9725:     if (keys(%currsetting) > 0) {
 9726:         foreach my $item (@contacts) {
 9727:             if ($to{$item} ne $currsetting{$item}) {
 9728:                 $changes{$item} = 1;
 9729:             }
 9730:         }
 9731:         foreach my $type (@mailings) {
 9732:             foreach my $item (@contacts) {
 9733:                 if (ref($currsetting{$type}) eq 'HASH') {
 9734:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 9735:                         push(@{$changes{$type}},$item);
 9736:                     }
 9737:                 } else {
 9738:                     push(@{$changes{$type}},@{$newsetting{$type}});
 9739:                 }
 9740:             }
 9741:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 9742:                 push(@{$changes{$type}},'others');
 9743:             }
 9744:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 9745:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 9746:                     push(@{$changes{$type}},'bcc'); 
 9747:                 }
 9748:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
 9749:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
 9750:                     push(@{$changes{$type}},'include');
 9751:                 }
 9752:             }
 9753:         }
 9754:         if (ref($fields) eq 'ARRAY') {
 9755:             if (ref($currsetting{'helpform'}) eq 'HASH') {
 9756:                 foreach my $field (@{$fields}) {
 9757:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
 9758:                         push(@{$changes{'helpform'}},$field);
 9759:                     }
 9760:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
 9761:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
 9762:                             push(@{$changes{'helpform'}},'maxsize');
 9763:                         }
 9764:                     }
 9765:                 }
 9766:             } else {
 9767:                 foreach my $field (@{$fields}) {
 9768:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
 9769:                         push(@{$changes{'helpform'}},$field);
 9770:                     }
 9771:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
 9772:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
 9773:                             push(@{$changes{'helpform'}},'maxsize');
 9774:                         }
 9775:                     }
 9776:                 }
 9777:             }
 9778:         }
 9779:     } else {
 9780:         my %default;
 9781:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 9782:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 9783:         $default{'errormail'} = 'adminemail';
 9784:         $default{'packagesmail'} = 'adminemail';
 9785:         $default{'helpdeskmail'} = 'supportemail';
 9786:         $default{'otherdomsmail'} = 'supportemail';
 9787:         $default{'lonstatusmail'} = 'adminemail';
 9788:         $default{'requestsmail'} = 'adminemail';
 9789:         $default{'updatesmail'} = 'adminemail';
 9790:         $default{'hostipmail'} = 'adminemail';
 9791:         foreach my $item (@contacts) {
 9792:            if ($to{$item} ne $default{$item}) {
 9793:                $changes{$item} = 1;
 9794:            }
 9795:         }
 9796:         foreach my $type (@mailings) {
 9797:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 9798:                 push(@{$changes{$type}},@{$newsetting{$type}});
 9799:             }
 9800:             if ($others{$type} ne '') {
 9801:                 push(@{$changes{$type}},'others');
 9802:             }
 9803:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 9804:                 if ($bcc{$type} ne '') {
 9805:                     push(@{$changes{$type}},'bcc');
 9806:                 }
 9807:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
 9808:                     push(@{$changes{$type}},'include');
 9809:                 }
 9810:             }
 9811:         }
 9812:         if (ref($fields) eq 'ARRAY') {
 9813:             foreach my $field (@{$fields}) {
 9814:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
 9815:                     push(@{$changes{'helpform'}},$field);
 9816:                 }
 9817:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
 9818:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
 9819:                         push(@{$changes{'helpform'}},'maxsize');
 9820:                     }
 9821:                 }
 9822:             }
 9823:         }
 9824:     }
 9825:     foreach my $item (@toggles) {
 9826:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
 9827:             $changes{$item} = 1;
 9828:         } elsif ((!$env{'form.'.$item}) &&
 9829:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
 9830:             $changes{$item} = 1;
 9831:         }
 9832:     }
 9833:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 9834:                                              $dom);
 9835:     if ($putresult eq 'ok') {
 9836:         if (keys(%changes) > 0) {
 9837:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 9838:             if (ref($lastactref) eq 'HASH') {
 9839:                 $lastactref->{'domainconfig'} = 1;
 9840:             }
 9841:             my ($titles,$short_titles)  = &contact_titles();
 9842:             $resulttext = &mt('Changes made:').'<ul>';
 9843:             foreach my $item (@contacts) {
 9844:                 if ($changes{$item}) {
 9845:                     $resulttext .= '<li>'.$titles->{$item}.
 9846:                                     &mt(' set to: ').
 9847:                                     '<span class="LC_cusr_emph">'.
 9848:                                     $to{$item}.'</span></li>';
 9849:                 }
 9850:             }
 9851:             foreach my $type (@mailings) {
 9852:                 if (ref($changes{$type}) eq 'ARRAY') {
 9853:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 9854:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
 9855:                     } else {
 9856:                         $resulttext .= '<li>'.$titles->{$type}.': ';
 9857:                     }
 9858:                     my @text;
 9859:                     foreach my $item (@{$newsetting{$type}}) {
 9860:                         push(@text,$short_titles->{$item});
 9861:                     }
 9862:                     if ($others{$type} ne '') {
 9863:                         push(@text,$others{$type});
 9864:                     }
 9865:                     if (@text) {
 9866:                         $resulttext .= '<span class="LC_cusr_emph">'.
 9867:                                        join(', ',@text).'</span>';
 9868:                     }
 9869:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 9870:                         if ($bcc{$type} ne '') {
 9871:                             my $bcctext;
 9872:                             if (@text) {
 9873:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
 9874:                             } else {
 9875:                                 $bcctext = '(Bcc)';
 9876:                             }
 9877:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 9878:                         } elsif (!@text) {
 9879:                             $resulttext .= &mt('No one');
 9880:                         }
 9881:                         if ($includestr{$type} ne '') {
 9882:                             if ($includeloc{$type} eq 'b') {
 9883:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
 9884:                             } elsif ($includeloc{$type} eq 's') {
 9885:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
 9886:                             }
 9887:                         }
 9888:                     } elsif (!@text) {
 9889:                         $resulttext .= &mt('No recipients');
 9890:                     }
 9891:                     $resulttext .= '</li>';
 9892:                 }
 9893:             }
 9894:             my @offon = ('off','on');
 9895:             if ($changes{'reporterrors'}) {
 9896:                 $resulttext .= '<li>'.
 9897:                                &mt('E-mail error reports to [_1] set to "'.
 9898:                                    $offon[$env{'form.reporterrors'}].'".',
 9899:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 9900:                                        &mt('LON-CAPA core group - MSU'),600,500)).
 9901:                                '</li>';
 9902:             }
 9903:             if ($changes{'reportupdates'}) {
 9904:                 $resulttext .= '<li>'.
 9905:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
 9906:                                     $offon[$env{'form.reportupdates'}].'".',
 9907:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 9908:                                         &mt('LON-CAPA core group - MSU'),600,500)).
 9909:                                 '</li>';
 9910:             }
 9911:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
 9912:                 my (@optional,@required,@unused,$maxsizechg);
 9913:                 foreach my $field (@{$changes{'helpform'}}) {
 9914:                     if ($field eq 'maxsize') {
 9915:                         $maxsizechg = 1;
 9916:                         next;
 9917:                     }
 9918:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
 9919:                         push(@optional,$field);
 9920:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
 9921:                         push(@unused,$field);
 9922:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
 9923:                         push(@required,$field);
 9924:                     }
 9925:                 }
 9926:                 if (@optional) {
 9927:                     $resulttext .= '<li>'.
 9928:                                    &mt('Help form fields changed to "Optional": [_1].',
 9929:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
 9930:                                    '</li>';
 9931:                 }
 9932:                 if (@required) {
 9933:                     $resulttext .= '<li>'.
 9934:                                    &mt('Help form fields changed to "Required": [_1].',
 9935:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
 9936:                                    '</li>';
 9937:                 }
 9938:                 if (@unused) {
 9939:                     $resulttext .= '<li>'.
 9940:                                    &mt('Help form fields changed to "Not shown": [_1].',
 9941:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
 9942:                                    '</li>';
 9943:                 }
 9944:                 if ($maxsizechg) {
 9945:                     $resulttext .= '<li>'.
 9946:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
 9947:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
 9948:                                    '</li>';
 9949: 
 9950:                 }
 9951:             }
 9952:             $resulttext .= '</ul>';
 9953:         } else {
 9954:             $resulttext = &mt('No changes made to contacts and form settings');
 9955:         }
 9956:     } else {
 9957:         $resulttext = '<span class="LC_error">'.
 9958:             &mt('An error occurred: [_1].',$putresult).'</span>';
 9959:     }
 9960:     return $resulttext;
 9961: }
 9962: 
 9963: sub modify_usercreation {
 9964:     my ($dom,%domconfig) = @_;
 9965:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
 9966:     my $warningmsg;
 9967:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 9968:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 9969:             if ($key eq 'cancreate') {
 9970:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 9971:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 9972:                         if (($item eq 'requestcrs') || ($item eq 'course') || ($item eq 'author')) {
 9973:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 9974:                         } else {
 9975:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 9976:                         }
 9977:                     }
 9978:                 }
 9979:             } elsif ($key eq 'email_rule') {
 9980:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 9981:             } else {
 9982:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 9983:             }
 9984:         }
 9985:     }
 9986:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 9987:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 9988:     my @contexts = ('author','course','requestcrs');
 9989:     foreach my $item(@contexts) {
 9990:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
 9991:     }
 9992:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 9993:         foreach my $item (@contexts) {
 9994:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 9995:                 push(@{$changes{'cancreate'}},$item);
 9996:             }
 9997:         }
 9998:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 9999:         foreach my $item (@contexts) {
10000:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
10001:                 if ($cancreate{$item} ne 'any') {
10002:                     push(@{$changes{'cancreate'}},$item);
10003:                 }
10004:             } else {
10005:                 if ($cancreate{$item} ne 'none') {
10006:                     push(@{$changes{'cancreate'}},$item);
10007:                 }
10008:             }
10009:         }
10010:     } else {
10011:         foreach my $item (@contexts)  {
10012:             push(@{$changes{'cancreate'}},$item);
10013:         }
10014:     }
10015: 
10016:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
10017:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
10018:             if (!grep(/^\Q$type\E$/,@username_rule)) {
10019:                 push(@{$changes{'username_rule'}},$type);
10020:             }
10021:         }
10022:         foreach my $type (@username_rule) {
10023:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
10024:                 push(@{$changes{'username_rule'}},$type);
10025:             }
10026:         }
10027:     } else {
10028:         push(@{$changes{'username_rule'}},@username_rule);
10029:     }
10030: 
10031:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
10032:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
10033:             if (!grep(/^\Q$type\E$/,@id_rule)) {
10034:                 push(@{$changes{'id_rule'}},$type);
10035:             }
10036:         }
10037:         foreach my $type (@id_rule) {
10038:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
10039:                 push(@{$changes{'id_rule'}},$type);
10040:             }
10041:         }
10042:     } else {
10043:         push(@{$changes{'id_rule'}},@id_rule);
10044:     }
10045: 
10046:     my @authen_contexts = ('author','course','domain');
10047:     my @authtypes = ('int','krb4','krb5','loc');
10048:     my %authhash;
10049:     foreach my $item (@authen_contexts) {
10050:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
10051:         foreach my $auth (@authtypes) {
10052:             if (grep(/^\Q$auth\E$/,@authallowed)) {
10053:                 $authhash{$item}{$auth} = 1;
10054:             } else {
10055:                 $authhash{$item}{$auth} = 0;
10056:             }
10057:         }
10058:     }
10059:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
10060:         foreach my $item (@authen_contexts) {
10061:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
10062:                 foreach my $auth (@authtypes) {
10063:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
10064:                         push(@{$changes{'authtypes'}},$item);
10065:                         last;
10066:                     }
10067:                 }
10068:             }
10069:         }
10070:     } else {
10071:         foreach my $item (@authen_contexts) {
10072:             push(@{$changes{'authtypes'}},$item);
10073:         }
10074:     }
10075: 
10076:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
10077:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
10078:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
10079:     $save_usercreate{'id_rule'} = \@id_rule;
10080:     $save_usercreate{'username_rule'} = \@username_rule,
10081:     $save_usercreate{'authtypes'} = \%authhash;
10082: 
10083:     my %usercreation_hash =  (
10084:         usercreation     => \%save_usercreate,
10085:     );
10086: 
10087:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
10088:                                              $dom);
10089: 
10090:     if ($putresult eq 'ok') {
10091:         if (keys(%changes) > 0) {
10092:             $resulttext = &mt('Changes made:').'<ul>';
10093:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
10094:                 my %lt = &usercreation_types();
10095:                 foreach my $type (@{$changes{'cancreate'}}) {
10096:                     my $chgtext = $lt{$type}.', ';
10097:                     if ($cancreate{$type} eq 'none') {
10098:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
10099:                     } elsif ($cancreate{$type} eq 'any') {
10100:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
10101:                     } elsif ($cancreate{$type} eq 'official') {
10102:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
10103:                     } elsif ($cancreate{$type} eq 'unofficial') {
10104:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
10105:                     }
10106:                     $resulttext .= '<li>'.$chgtext.'</li>';
10107:                 }
10108:             }
10109:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
10110:                 my ($rules,$ruleorder) = 
10111:                     &Apache::lonnet::inst_userrules($dom,'username');
10112:                 my $chgtext = '<ul>';
10113:                 foreach my $type (@username_rule) {
10114:                     if (ref($rules->{$type}) eq 'HASH') {
10115:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
10116:                     }
10117:                 }
10118:                 $chgtext .= '</ul>';
10119:                 if (@username_rule > 0) {
10120:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
10121:                 } else {
10122:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
10123:                 }
10124:             }
10125:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
10126:                 my ($idrules,$idruleorder) = 
10127:                     &Apache::lonnet::inst_userrules($dom,'id');
10128:                 my $chgtext = '<ul>';
10129:                 foreach my $type (@id_rule) {
10130:                     if (ref($idrules->{$type}) eq 'HASH') {
10131:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
10132:                     }
10133:                 }
10134:                 $chgtext .= '</ul>';
10135:                 if (@id_rule > 0) {
10136:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
10137:                 } else {
10138:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
10139:                 }
10140:             }
10141:             my %authname = &authtype_names();
10142:             my %context_title = &context_names();
10143:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
10144:                 my $chgtext = '<ul>';
10145:                 foreach my $type (@{$changes{'authtypes'}}) {
10146:                     my @allowed;
10147:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
10148:                     foreach my $auth (@authtypes) {
10149:                         if ($authhash{$type}{$auth}) {
10150:                             push(@allowed,$authname{$auth});
10151:                         }
10152:                     }
10153:                     if (@allowed > 0) {
10154:                         $chgtext .= join(', ',@allowed).'</li>';
10155:                     } else {
10156:                         $chgtext .= &mt('none').'</li>';
10157:                     }
10158:                 }
10159:                 $chgtext .= '</ul>';
10160:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
10161:                 $resulttext .= '</li>';
10162:             }
10163:             $resulttext .= '</ul>';
10164:         } else {
10165:             $resulttext = &mt('No changes made to user creation settings');
10166:         }
10167:     } else {
10168:         $resulttext = '<span class="LC_error">'.
10169:             &mt('An error occurred: [_1]',$putresult).'</span>';
10170:     }
10171:     if ($warningmsg ne '') {
10172:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
10173:     }
10174:     return $resulttext;
10175: }
10176: 
10177: sub modify_selfcreation {
10178:     my ($dom,$lastactref,%domconfig) = @_;
10179:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%curr_inststatus,%changes,%cancreate);
10180:     my (%save_usercreate,%save_usermodify,%save_inststatus,@types,%usertypes);
10181:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10182:     my ($othertitle,$usertypesref,$typesref) = &Apache::loncommon::sorted_inst_types($dom);
10183:     if (ref($typesref) eq 'ARRAY') {
10184:         @types = @{$typesref};
10185:     }
10186:     if (ref($usertypesref) eq 'HASH') {
10187:         %usertypes = %{$usertypesref};
10188:     }
10189:     $usertypes{'default'} = $othertitle;
10190: #
10191: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
10192: #
10193:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
10194:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
10195:             if ($key eq 'cancreate') {
10196:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
10197:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
10198:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
10199:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
10200:                             ($item eq 'recaptchaversion') || ($item eq 'notify') ||
10201:                             ($item eq 'emailusername') || ($item eq 'shibenv') ||
10202:                             ($item eq 'selfcreateprocessing') || ($item eq 'emailverified') ||
10203:                             ($item eq 'emailoptions') || ($item eq 'emaildomain')) {
10204:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
10205:                         } else {
10206:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
10207:                         }
10208:                     }
10209:                 }
10210:             } elsif ($key eq 'email_rule') {
10211:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
10212:             } else {
10213:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
10214:             }
10215:         }
10216:     }
10217: #
10218: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
10219: #
10220:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
10221:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
10222:             if ($key eq 'selfcreate') {
10223:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
10224:             } else {
10225:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
10226:             }
10227:         }
10228:     }
10229: #
10230: # Retrieve current domain configuration for institutional status types from $domconfig{'inststatus'}.
10231: #
10232:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
10233:         foreach my $key (keys(%{$domconfig{'inststatus'}})) {
10234:             if ($key eq 'inststatusguest') {
10235:                 $curr_inststatus{$key} = $domconfig{'inststatus'}{$key};
10236:             } else {
10237:                 $save_inststatus{$key} = $domconfig{'inststatus'}{$key};
10238:             }
10239:         }
10240:     }
10241: 
10242:     my @contexts = ('selfcreate');
10243:     @{$cancreate{'selfcreate'}} = ();
10244:     %{$cancreate{'emailusername'}} = ();
10245:     if (@types) {
10246:         @{$cancreate{'statustocreate'}} = ();
10247:     }
10248:     %{$cancreate{'selfcreateprocessing'}} = ();
10249:     %{$cancreate{'shibenv'}} = ();
10250:     %{$cancreate{'emailverified'}} = ();
10251:     %{$cancreate{'emailoptions'}} = ();
10252:     %{$cancreate{'emaildomain'}} = ();
10253:     my %selfcreatetypes = (
10254:                              sso   => 'users authenticated by institutional single sign on',
10255:                              login => 'users authenticated by institutional log-in',
10256:                              email => 'users verified by e-mail',
10257:                           );
10258: #
10259: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
10260: # is permitted.
10261: #
10262: 
10263:     my ($emailrules,$emailruleorder) = &Apache::lonnet::inst_userrules($dom,'email');
10264: 
10265:     my (@statuses,%email_rule);
10266:     foreach my $item ('login','sso','email') {
10267:         if ($item eq 'email') {
10268:             if ($env{'form.cancreate_email'}) {
10269:                 if (@types) {
10270:                     my @poss_statuses = &Apache::loncommon::get_env_multiple('form.selfassign');
10271:                     foreach my $status (@poss_statuses) {
10272:                         if (grep(/^\Q$status\E$/,(@types,'default'))) {
10273:                             push(@statuses,$status);
10274:                         }
10275:                     }
10276:                     $save_inststatus{'inststatusguest'} = \@statuses;
10277:                 } else {
10278:                     push(@statuses,'default');
10279:                 }
10280:                 if (@statuses) {
10281:                     my %curr_rule;
10282:                     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
10283:                         foreach my $type (@statuses) {
10284:                             $curr_rule{$type} = $curr_usercreation{'email_rule'};
10285:                         }
10286:                     } elsif (ref($curr_usercreation{'email_rule'}) eq 'HASH') {
10287:                         foreach my $type (@statuses) {
10288:                             $curr_rule{$type} = $curr_usercreation{'email_rule'}{$type};
10289:                         }
10290:                     }
10291:                     push(@{$cancreate{'selfcreate'}},'email');
10292:                     push(@contexts,('selfcreateprocessing','emailverified','emailoptions'));
10293:                     my %curremaildom;
10294:                     if (ref($curr_usercreation{'cancreate'}{'emaildomain'}) eq 'HASH') {
10295:                         %curremaildom = %{$curr_usercreation{'cancreate'}{'emaildomain'}};
10296:                     }
10297:                     foreach my $type (@statuses) {
10298:                         if ($env{'form.cancreate_emailprocess_'.$type} =~ /^(?:approval|automatic)$/) {
10299:                             $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
10300:                         }
10301:                         if ($env{'form.cancreate_usernameoptions_'.$type} =~ /^(?:all|first|free)$/) {
10302:                             $cancreate{'emailverified'}{$type} = $env{'form.cancreate_usernameoptions_'.$type};
10303:                         }
10304:                         if ($env{'form.cancreate_emailoptions_'.$type} =~ /^(any|inst|noninst|custom)$/) {
10305: #
10306: # Retrieve rules (if any) governing types of e-mail address which may be used to verify a username.
10307: #
10308:                             my $chosen = $1;
10309:                             if (($chosen eq 'inst') || ($chosen eq 'noninst')) {
10310:                                 my $emaildom;
10311:                                 if ($env{'form.cancreate_emaildomain_'.$chosen.'_'.$type} =~ /^\@[^\@]+$/) {
10312:                                     $emaildom = $env{'form.cancreate_emaildomain_'.$chosen.'_'.$type};
10313:                                     $cancreate{'emaildomain'}{$type}{$chosen} = $emaildom;
10314:                                     if (ref($curremaildom{$type}) eq 'HASH') {
10315:                                         if (exists($curremaildom{$type}{$chosen})) {
10316:                                             if ($curremaildom{$type}{$chosen} ne $emaildom) {
10317:                                                 push(@{$changes{'cancreate'}},'emaildomain');
10318:                                             }
10319:                                         } elsif ($emaildom ne '') {
10320:                                             push(@{$changes{'cancreate'}},'emaildomain');
10321:                                         }
10322:                                     } elsif ($emaildom ne '') {
10323:                                         push(@{$changes{'cancreate'}},'emaildomain');
10324:                                     }
10325:                                 }
10326:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
10327:                             } elsif ($chosen eq 'custom') {
10328:                                 my @possemail_rules = &Apache::loncommon::get_env_multiple('form.email_rule_'.$type);
10329:                                 $email_rule{$type} = [];
10330:                                 if (ref($emailrules) eq 'HASH') {
10331:                                     foreach my $rule (@possemail_rules) {
10332:                                         if (exists($emailrules->{$rule})) {
10333:                                             push(@{$email_rule{$type}},$rule);
10334:                                         }
10335:                                     }
10336:                                 }
10337:                                 if (@{$email_rule{$type}}) {
10338:                                     $cancreate{'emailoptions'}{$type} = 'custom';
10339:                                     if (ref($curr_rule{$type}) eq 'ARRAY') {
10340:                                         if (@{$curr_rule{$type}} > 0) {
10341:                                             foreach my $rule (@{$curr_rule{$type}}) {
10342:                                                 if (!grep(/^\Q$rule\E$/,@{$email_rule{$type}})) {
10343:                                                     push(@{$changes{'email_rule'}},$type);
10344:                                                 }
10345:                                             }
10346:                                         }
10347:                                         foreach my $type (@{$email_rule{$type}}) {
10348:                                             if (!grep(/^\Q$type\E$/,@{$curr_rule{$type}})) {
10349:                                                 push(@{$changes{'email_rule'}},$type);
10350:                                             }
10351:                                         }
10352:                                     } else {
10353:                                         push(@{$changes{'email_rule'}},$type);
10354:                                     }
10355:                                 }
10356:                             } else {
10357:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
10358:                             }
10359:                         }
10360:                     }
10361:                     if (@types) {
10362:                         if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
10363:                             my @changed = &Apache::loncommon::compare_arrays(\@statuses,$curr_inststatus{'inststatusguest'});
10364:                             if (@changed) {
10365:                                 push(@{$changes{'inststatus'}},'inststatusguest');
10366:                             }
10367:                         } else {
10368:                             push(@{$changes{'inststatus'}},'inststatusguest');
10369:                         }
10370:                     }
10371:                 } else {
10372:                     delete($env{'form.cancreate_email'});
10373:                     if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
10374:                         if (@{$curr_inststatus{'inststatusguest'}} > 0) {
10375:                             push(@{$changes{'inststatus'}},'inststatusguest');
10376:                         }
10377:                     }
10378:                 }
10379:             } else {
10380:                 $save_inststatus{'inststatusguest'} = [];
10381:                 if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
10382:                     if (@{$curr_inststatus{'inststatusguest'}} > 0) {
10383:                         push(@{$changes{'inststatus'}},'inststatusguest');
10384:                     }
10385:                 }
10386:             }
10387:         } else {
10388:             if ($env{'form.cancreate_'.$item}) {
10389:                 push(@{$cancreate{'selfcreate'}},$item);
10390:             }
10391:         }
10392:     }
10393:     my (%userinfo,%savecaptcha);
10394:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
10395: #
10396: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
10397: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
10398: #
10399: 
10400:     if ($env{'form.cancreate_email'}) {
10401:         push(@contexts,'emailusername');
10402:         if (@statuses) {
10403:             foreach my $type (@statuses) {
10404:                 if (ref($infofields) eq 'ARRAY') {
10405:                     foreach my $field (@{$infofields}) {
10406:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
10407:                             $cancreate{'emailusername'}{$type}{$field} = $1;
10408:                         }
10409:                     }
10410:                 }
10411:             }
10412:         }
10413: #
10414: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
10415: # queued requests for self-creation of account verified by e-mail.
10416: #
10417: 
10418:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
10419:         @approvalnotify = sort(@approvalnotify);
10420:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
10421:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
10422:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
10423:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
10424:                     push(@{$changes{'cancreate'}},'notify');
10425:                 }
10426:             } else {
10427:                 if ($cancreate{'notify'}{'approval'}) {
10428:                     push(@{$changes{'cancreate'}},'notify');
10429:                 }
10430:             }
10431:         } elsif ($cancreate{'notify'}{'approval'}) {
10432:             push(@{$changes{'cancreate'}},'notify');
10433:         }
10434: 
10435:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
10436:     }
10437: #  
10438: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
10439: # institutional log-in.
10440: #
10441:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
10442:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
10443:                ($domdefaults{'auth_def'} eq 'localauth'))) {
10444:             $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.').' '.
10445:                           &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.');
10446:         }
10447:     }
10448:     my @fields = ('lastname','firstname','middlename','generation',
10449:                   'permanentemail','id');
10450:     my @shibfields = (@fields,'inststatus');
10451:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
10452: #
10453: # Where usernames may created for institutional log-in and/or institutional single sign on:
10454: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
10455: # may self-create accounts 
10456: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
10457: # which the user may supply, if institutional data is unavailable.
10458: #
10459:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
10460:         if (@types) {
10461:             @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
10462:             push(@contexts,'statustocreate');
10463:             foreach my $type (@types) {
10464:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
10465:                 foreach my $field (@fields) {
10466:                     if (grep(/^\Q$field\E$/,@modifiable)) {
10467:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
10468:                     } else {
10469:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
10470:                     }
10471:                 }
10472:             }
10473:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
10474:                 foreach my $type (@types) {
10475:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
10476:                         foreach my $field (@fields) {
10477:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
10478:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
10479:                                 push(@{$changes{'selfcreate'}},$type);
10480:                                 last;
10481:                             }
10482:                         }
10483:                     }
10484:                 }
10485:             } else {
10486:                 foreach my $type (@types) {
10487:                     push(@{$changes{'selfcreate'}},$type);
10488:                 }
10489:             }
10490:         }
10491:         foreach my $field (@shibfields) {
10492:             if ($env{'form.shibenv_'.$field} ne '') {
10493:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
10494:             }
10495:         }
10496:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
10497:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
10498:                 foreach my $field (@shibfields) {
10499:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
10500:                         push(@{$changes{'cancreate'}},'shibenv');
10501:                     }
10502:                 }
10503:             } else {
10504:                 foreach my $field (@shibfields) {
10505:                     if ($env{'form.shibenv_'.$field}) {
10506:                         push(@{$changes{'cancreate'}},'shibenv');
10507:                         last;
10508:                     }
10509:                 }
10510:             }
10511:         }
10512:     }
10513:     foreach my $item (@contexts) {
10514:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
10515:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
10516:                 if (ref($cancreate{$item}) eq 'ARRAY') {
10517:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
10518:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10519:                             push(@{$changes{'cancreate'}},$item);
10520:                         }
10521:                     }
10522:                 }
10523:             }
10524:             if (ref($cancreate{$item}) eq 'ARRAY') {
10525:                 foreach my $type (@{$cancreate{$item}}) {
10526:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
10527:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10528:                             push(@{$changes{'cancreate'}},$item);
10529:                         }
10530:                     }
10531:                 }
10532:             }
10533:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
10534:             if (ref($cancreate{$item}) eq 'HASH') {
10535:                 foreach my $type (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
10536:                     if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
10537:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$type}})) {
10538:                             unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
10539:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10540:                                     push(@{$changes{'cancreate'}},$item);
10541:                                 }
10542:                             }
10543:                         }
10544:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
10545:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
10546:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10547:                                 push(@{$changes{'cancreate'}},$item);
10548:                             }
10549:                         }
10550:                     }
10551:                 }
10552:                 foreach my $type (keys(%{$cancreate{$item}})) {
10553:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
10554:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
10555:                             if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
10556:                                 unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
10557:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10558:                                         push(@{$changes{'cancreate'}},$item);
10559:                                     }
10560:                                 }
10561:                             } else {
10562:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10563:                                     push(@{$changes{'cancreate'}},$item);
10564:                                 }
10565:                             }
10566:                         }
10567:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
10568:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
10569:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10570:                                 push(@{$changes{'cancreate'}},$item);
10571:                             }
10572:                         }
10573:                     }
10574:                 }
10575:             }
10576:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
10577:             if (ref($cancreate{$item}) eq 'ARRAY') {
10578:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
10579:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10580:                         push(@{$changes{'cancreate'}},$item);
10581:                     }
10582:                 }
10583:             }
10584:         } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
10585:             if (ref($cancreate{$item}) eq 'HASH') {
10586:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10587:                     push(@{$changes{'cancreate'}},$item);
10588:                 }
10589:             }
10590:         } elsif ($item eq 'emailusername') {
10591:             if (ref($cancreate{$item}) eq 'HASH') {
10592:                 foreach my $type (keys(%{$cancreate{$item}})) {
10593:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
10594:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
10595:                             if ($cancreate{$item}{$type}{$field}) {
10596:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10597:                                     push(@{$changes{'cancreate'}},$item);
10598:                                 }
10599:                                 last;
10600:                             }
10601:                         }
10602:                     }
10603:                 }
10604:             }
10605:         }
10606:     }
10607: #
10608: # Populate %save_usercreate hash with updates to self-creation configuration.
10609: #
10610:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
10611:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
10612:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
10613:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
10614:     if (ref($cancreate{'notify'}) eq 'HASH') {
10615:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
10616:     }
10617:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
10618:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
10619:     }
10620:     if (ref($cancreate{'emailverified'}) eq 'HASH') {
10621:         $save_usercreate{'cancreate'}{'emailverified'} = $cancreate{'emailverified'};
10622:     }
10623:     if (ref($cancreate{'emailoptions'}) eq 'HASH') {
10624:         $save_usercreate{'cancreate'}{'emailoptions'} = $cancreate{'emailoptions'};
10625:     }
10626:     if (ref($cancreate{'emaildomain'}) eq 'HASH') {
10627:         $save_usercreate{'cancreate'}{'emaildomain'} = $cancreate{'emaildomain'};
10628:     }
10629:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
10630:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
10631:     }
10632:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
10633:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
10634:     }
10635:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
10636:     $save_usercreate{'email_rule'} = \%email_rule;
10637: 
10638:     my %userconfig_hash = (
10639:             usercreation     => \%save_usercreate,
10640:             usermodification => \%save_usermodify,
10641:             inststatus       => \%save_inststatus,
10642:     );
10643: 
10644:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
10645:                                              $dom);
10646: #
10647: # Accumulate details of changes to domain configuration for self-creation of usernames in $resulttext
10648: #
10649:     if ($putresult eq 'ok') {
10650:         if (keys(%changes) > 0) {
10651:             $resulttext = &mt('Changes made:').'<ul>';
10652:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
10653:                 my %lt = &selfcreation_types();
10654:                 foreach my $type (@{$changes{'cancreate'}}) {
10655:                     my $chgtext = '';
10656:                     if ($type eq 'selfcreate') {
10657:                         if (@{$cancreate{$type}} == 0) {
10658:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
10659:                         } else {
10660:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
10661:                                         '<ul>';
10662:                             foreach my $case (@{$cancreate{$type}}) {
10663:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
10664:                             }
10665:                             $chgtext .= '</ul>';
10666:                             if (ref($cancreate{$type}) eq 'ARRAY') {
10667:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
10668:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
10669:                                         if (@{$cancreate{'statustocreate'}} == 0) {
10670:                                             $chgtext .= '<span class="LC_warning">'.
10671:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts via log-in or single sign-on.").
10672:                                                         '</span><br />';
10673:                                         }
10674:                                     }
10675:                                 }
10676:                                 if (grep(/^email$/,@{$cancreate{$type}})) {
10677:                                     if (!@statuses) {
10678:                                         $chgtext .= '<span class="LC_warning">'.
10679:                                                     &mt("However, e-mail verification is currently set to 'unavailable' for all user types (including 'other'), so self-creation of accounts is not possible for non-institutional log-in.").
10680:                                                     '</span><br />';
10681: 
10682:                                     }
10683:                                 }
10684:                             }
10685:                         }
10686:                     } elsif ($type eq 'shibenv') {
10687:                         if (keys(%{$cancreate{$type}}) == 0) {
10688:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information').'<br />'; 
10689:                         } else {
10690:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
10691:                                         '<ul>';
10692:                             foreach my $field (@shibfields) {
10693:                                 next if ($cancreate{$type}{$field} eq '');
10694:                                 if ($field eq 'inststatus') {
10695:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
10696:                                 } else {
10697:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
10698:                                 }
10699:                             }
10700:                             $chgtext .= '</ul>';
10701:                         }
10702:                     } elsif ($type eq 'statustocreate') {
10703:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
10704:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
10705:                             if (@{$cancreate{'selfcreate'}} > 0) {
10706:                                 if (@{$cancreate{'statustocreate'}} == 0) {
10707:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
10708:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
10709:                                         $chgtext .= '<br />'.
10710:                                                     '<span class="LC_warning">'.
10711:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
10712:                                                     '</span>';
10713:                                     }
10714:                                 } elsif (keys(%usertypes) > 0) {
10715:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
10716:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
10717:                                     } else {
10718:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
10719:                                     }
10720:                                     $chgtext .= '<ul>';
10721:                                     foreach my $case (@{$cancreate{$type}}) {
10722:                                         if ($case eq 'default') {
10723:                                             $chgtext .= '<li>'.$othertitle.'</li>';
10724:                                         } else {
10725:                                             $chgtext .= '<li>'.$usertypes{$case}.'</li>';
10726:                                         }
10727:                                     }
10728:                                     $chgtext .= '</ul>';
10729:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
10730:                                         $chgtext .= '<span class="LC_warning">'.
10731:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
10732:                                                     '</span>';
10733:                                     }
10734:                                 }
10735:                             } else {
10736:                                 if (@{$cancreate{$type}} == 0) {
10737:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
10738:                                 } else {
10739:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
10740:                                 }
10741:                             }
10742:                             $chgtext .= '<br />';
10743:                         }
10744:                     } elsif ($type eq 'selfcreateprocessing') {
10745:                         my %choices = &Apache::lonlocal::texthash (
10746:                                                                     automatic => 'Automatic approval',
10747:                                                                     approval  => 'Queued for approval',
10748:                                                                   );
10749:                         if (@types) {
10750:                             if (@statuses) {
10751:                                 $chgtext .= &mt('Processing of requests to create account with e-mail verification set as follows:').
10752:                                             '<ul>';
10753:                                 foreach my $status (@statuses) {
10754:                                     if ($status eq 'default') {
10755:                                         $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
10756:                                     } else {
10757:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
10758:                                     }
10759:                                 }
10760:                                 $chgtext .= '</ul>';
10761:                             }
10762:                         } else {
10763:                             $chgtext .= &mt('Processing of requests to create account with e-mail verification set to: "[_1]"',
10764:                                             $choices{$cancreate{'selfcreateprocessing'}{'default'}});
10765:                         }
10766:                     } elsif ($type eq 'emailverified') {
10767:                         my %options = &Apache::lonlocal::texthash (
10768:                                                                     all   => 'Same as e-mail',
10769:                                                                     first => 'Omit @domain',
10770:                                                                     free  => 'Free to choose',
10771:                                                                   );
10772:                         if (@types) {
10773:                             if (@statuses) {
10774:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, username is set as follows:').
10775:                                             '<ul>';
10776:                                 foreach my $status (@statuses) {
10777:                                     if ($status eq 'default') {
10778:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
10779:                                     } else {
10780:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
10781:                                     }
10782:                                 }
10783:                                 $chgtext .= '</ul>';
10784:                             }
10785:                         } else {
10786:                             $chgtext .= &mt("For self-created accounts verified by e-mail address, user's username is: '[_1]'",
10787:                                             $options{$cancreate{'emailverified'}{'default'}});
10788:                         }
10789:                     } elsif ($type eq 'emailoptions') {
10790:                         my %options = &Apache::lonlocal::texthash (
10791:                                                                     any     => 'Any e-mail',
10792:                                                                     inst    => 'Institutional only',
10793:                                                                     noninst => 'Non-institutional only',
10794:                                                                     custom  => 'Custom restrictions',
10795:                                                                   );
10796:                         if (@types) {
10797:                             if (@statuses) {
10798:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, requirements for e-mail address are as follows:').
10799:                                             '<ul>';
10800:                                 foreach my $status (@statuses) {
10801:                                     if ($type eq 'default') {
10802:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
10803:                                     } else {
10804:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
10805:                                     }
10806:                                 }
10807:                                 $chgtext .= '</ul>';
10808:                             }
10809:                         } else {
10810:                             if ($cancreate{'emailoptions'}{'default'} eq 'any') {
10811:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, any e-mail may be used');
10812:                             } else {
10813:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, e-mail restricted to: "[_1]"',
10814:                                                 $options{$cancreate{'emailoptions'}{'default'}});
10815:                             }
10816:                         }
10817:                     } elsif ($type eq 'emaildomain') {
10818:                         my $output;
10819:                         if (@statuses) {
10820:                             foreach my $type (@statuses) {
10821:                                 if (ref($cancreate{'emaildomain'}{$type}) eq 'HASH') {
10822:                                     if ($cancreate{'emailoptions'}{$type} eq 'inst') {
10823:                                         if ($type eq 'default') {
10824:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
10825:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
10826:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
10827:                                             } else {
10828:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address needs to end: [_1]",
10829:                                                                                         $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
10830:                                             }
10831:                                         } else {
10832:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
10833:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
10834:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
10835:                                             } else {
10836:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address needs to end: [_1]",
10837:                                                                                               $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
10838:                                             }
10839:                                         }
10840:                                     } elsif ($cancreate{'emailoptions'}{$type} eq 'noninst') {
10841:                                         if ($type eq 'default') {
10842:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
10843:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
10844:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
10845:                                             } else {
10846:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address must not end: [_1]",
10847:                                                                                         $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
10848:                                             }
10849:                                         } else {
10850:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
10851:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
10852:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
10853:                                             } else {
10854:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address must not end: [_1]",
10855:                                                                                                 $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
10856:                                             }
10857:                                         }
10858:                                     }
10859:                                 }
10860:                             }
10861:                         }
10862:                         if ($output ne '') {
10863:                             $chgtext .= &mt('For self-created accounts verified by e-mail address:').
10864:                                         '<ul>'.$output.'</ul>';
10865:                         }
10866:                     } elsif ($type eq 'captcha') {
10867:                         if ($savecaptcha{$type} eq 'notused') {
10868:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
10869:                         } else {
10870:                             my %captchas = &captcha_phrases();
10871:                             if ($captchas{$savecaptcha{$type}}) {
10872:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
10873:                             } else {
10874:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
10875:                             }
10876:                         }
10877:                     } elsif ($type eq 'recaptchakeys') {
10878:                         my ($privkey,$pubkey);
10879:                         if (ref($savecaptcha{$type}) eq 'HASH') {
10880:                             $pubkey = $savecaptcha{$type}{'public'};
10881:                             $privkey = $savecaptcha{$type}{'private'};
10882:                         }
10883:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
10884:                         if (!$pubkey) {
10885:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
10886:                         } else {
10887:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
10888:                         }
10889:                         if (!$privkey) {
10890:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
10891:                         } else {
10892:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
10893:                         }
10894:                         $chgtext .= '</ul>';
10895:                     } elsif ($type eq 'recaptchaversion') {
10896:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
10897:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
10898:                         }
10899:                     } elsif ($type eq 'emailusername') {
10900:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
10901:                             if (@statuses) {
10902:                                 foreach my $type (@statuses) {
10903:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
10904:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
10905:                                             $chgtext .= &mt('When self-creating account with e-mail verification, the following information will be provided by [_1]:',"'$usertypes{$type}'").
10906:                                                     '<ul>';
10907:                                             foreach my $field (@{$infofields}) {
10908:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
10909:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
10910:                                                 }
10911:                                             }
10912:                                             $chgtext .= '</ul>';
10913:                                         } else {
10914:                                             $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
10915:                                         }
10916:                                     } else {
10917:                                         $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
10918:                                     }
10919:                                 }
10920:                             }
10921:                         }
10922:                     } elsif ($type eq 'notify') {
10923:                         my $numapprove = 0;
10924:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
10925:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
10926:                                 if ($cancreate{'notify'}{'approval'}) {
10927:                                     $chgtext .= &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
10928:                                     $numapprove ++;
10929:                                 }
10930:                             }
10931:                         }
10932:                         unless ($numapprove) {
10933:                             $chgtext .= &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
10934:                         }
10935:                     }
10936:                     if ($chgtext) {
10937:                         $resulttext .= '<li>'.$chgtext.'</li>';
10938:                     }
10939:                 }
10940:             }
10941:             if ((ref($changes{'email_rule'}) eq 'ARRAY') && (@{$changes{'email_rule'}} > 0)) {
10942:                 my ($emailrules,$emailruleorder) =
10943:                     &Apache::lonnet::inst_userrules($dom,'email');
10944:                 foreach my $type (@{$changes{'email_rule'}}) {
10945:                     if (ref($email_rule{$type}) eq 'ARRAY') {
10946:                         my $chgtext = '<ul>';
10947:                         foreach my $rule (@{$email_rule{$type}}) {
10948:                             if (ref($emailrules->{$rule}) eq 'HASH') {
10949:                                 $chgtext .= '<li>'.$emailrules->{$rule}{'name'}.'</li>';
10950:                             }
10951:                         }
10952:                         $chgtext .= '</ul>';
10953:                         my $typename;
10954:                         if (@types) {
10955:                             if ($type eq 'default') {
10956:                                 $typename = $othertitle;
10957:                             } else {
10958:                                 $typename = $usertypes{$type};
10959:                             }
10960:                             $chgtext .= &mt('(Affiliation: [_1])',$typename);
10961:                         }
10962:                         if (@{$email_rule{$type}} > 0) {
10963:                             $resulttext .= '<li>'.
10964:                                            &mt('Accounts may not be created by users verified by e-mail, for e-mail addresses of the following types: ',
10965:                                                $usertypes{$type}).
10966:                                            $chgtext.
10967:                                            '</li>';
10968:                         } else {
10969:                             $resulttext .= '<li>'.
10970:                                            &mt('There are now no restrictions on e-mail addresses which may be used for verification when a user requests an account.').
10971:                                            '</li>'.
10972:                                            &mt('(Affiliation: [_1])',$typename);
10973:                         }
10974:                     }
10975:                 }
10976:             }
10977:             if (ref($changes{'inststatus'}) eq 'ARRAY') {
10978:                 if (ref($save_inststatus{'inststatusguest'}) eq 'ARRAY') {
10979:                     if (@{$save_inststatus{'inststatusguest'}} > 0) {
10980:                         my $chgtext = '<ul>';
10981:                         foreach my $type (@{$save_inststatus{'inststatusguest'}}) {
10982:                             $chgtext .= '<li>'.$usertypes{$type}.'</li>';
10983:                         }
10984:                         $chgtext .= '</ul>';
10985:                         $resulttext .= '<li>'.
10986:                                        &mt('A user will self-report one of the following affiliations when requesting an account verified by e-mail: ').
10987:                                           $chgtext.
10988:                                        '</li>';
10989:                     } else {
10990:                         $resulttext .= '<li>'.
10991:                                        &mt('No affiliations available for self-reporting when requesting an account verified by e-mail.').
10992:                                        '</li>';
10993:                     }
10994:                 }
10995:             }
10996:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
10997:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
10998:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
10999:                 foreach my $type (@{$changes{'selfcreate'}}) {
11000:                     my $typename = $type;
11001:                     if (keys(%usertypes) > 0) {
11002:                         if ($usertypes{$type} ne '') {
11003:                             $typename = $usertypes{$type};
11004:                         }
11005:                     }
11006:                     my @modifiable;
11007:                     $resulttext .= '<li>'.
11008:                                     &mt('Self-creation of account by users with status: [_1]',
11009:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
11010:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
11011:                     foreach my $field (@fields) {
11012:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
11013:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
11014:                         }
11015:                     }
11016:                     if (@modifiable > 0) {
11017:                         $resulttext .= join(', ',@modifiable);
11018:                     } else {
11019:                         $resulttext .= &mt('none');
11020:                     }
11021:                     $resulttext .= '</li>';
11022:                 }
11023:                 $resulttext .= '</ul></li>';
11024:             }
11025:             $resulttext .= '</ul>';
11026:             my $cachetime = 24*60*60;
11027:             $domdefaults{'inststatusguest'} = $save_inststatus{'inststatusguest'};
11028:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
11029:             if (ref($lastactref) eq 'HASH') {
11030:                 $lastactref->{'domdefaults'} = 1;
11031:             }
11032:         } else {
11033:             $resulttext = &mt('No changes made to self-creation settings');
11034:         }
11035:     } else {
11036:         $resulttext = '<span class="LC_error">'.
11037:             &mt('An error occurred: [_1]',$putresult).'</span>';
11038:     }
11039:     if ($warningmsg ne '') {
11040:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
11041:     }
11042:     return $resulttext;
11043: }
11044: 
11045: sub process_captcha {
11046:     my ($container,$changes,$newsettings,$current) = @_;
11047:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
11048:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
11049:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
11050:         $newsettings->{'captcha'} = 'original';
11051:     }
11052:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
11053:         if ($container eq 'cancreate') {
11054:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
11055:                 push(@{$changes->{'cancreate'}},'captcha');
11056:             } elsif (!defined($changes->{'cancreate'})) {
11057:                 $changes->{'cancreate'} = ['captcha'];
11058:             }
11059:         } else {
11060:             $changes->{'captcha'} = 1;
11061:         }
11062:     }
11063:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
11064:     if ($newsettings->{'captcha'} eq 'recaptcha') {
11065:         $newpub = $env{'form.'.$container.'_recaptchapub'};
11066:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
11067:         $newpub =~ s/[^\w\-]//g;
11068:         $newpriv =~ s/[^\w\-]//g;
11069:         $newsettings->{'recaptchakeys'} = {
11070:                                              public  => $newpub,
11071:                                              private => $newpriv,
11072:                                           };
11073:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
11074:         $newversion =~ s/\D//g;
11075:         if ($newversion ne '2') {
11076:             $newversion = 1;
11077:         }
11078:         $newsettings->{'recaptchaversion'} = $newversion;
11079:     }
11080:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
11081:         $currpub = $current->{'recaptchakeys'}{'public'};
11082:         $currpriv = $current->{'recaptchakeys'}{'private'};
11083:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
11084:             $newsettings->{'recaptchakeys'} = {
11085:                                                  public  => '',
11086:                                                  private => '',
11087:                                               }
11088:         }
11089:     }
11090:     if ($current->{'captcha'} eq 'recaptcha') {
11091:         $currversion = $current->{'recaptchaversion'};
11092:         if ($currversion ne '2') {
11093:             $currversion = 1;
11094:         }
11095:     }
11096:     if ($currversion ne $newversion) {
11097:         if ($container eq 'cancreate') {
11098:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
11099:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
11100:             } elsif (!defined($changes->{'cancreate'})) {
11101:                 $changes->{'cancreate'} = ['recaptchaversion'];
11102:             }
11103:         } else {
11104:             $changes->{'recaptchaversion'} = 1;
11105:         }
11106:     }
11107:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
11108:         if ($container eq 'cancreate') {
11109:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
11110:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
11111:             } elsif (!defined($changes->{'cancreate'})) {
11112:                 $changes->{'cancreate'} = ['recaptchakeys'];
11113:             }
11114:         } else {
11115:             $changes->{'recaptchakeys'} = 1;
11116:         }
11117:     }
11118:     return;
11119: }
11120: 
11121: sub modify_usermodification {
11122:     my ($dom,%domconfig) = @_;
11123:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
11124:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
11125:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
11126:             if ($key eq 'selfcreate') {
11127:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
11128:             } else {  
11129:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
11130:             }
11131:         }
11132:     }
11133:     my @contexts = ('author','course');
11134:     my %context_title = (
11135:                            author => 'In author context',
11136:                            course => 'In course context',
11137:                         );
11138:     my @fields = ('lastname','firstname','middlename','generation',
11139:                   'permanentemail','id');
11140:     my %roles = (
11141:                   author => ['ca','aa'],
11142:                   course => ['st','ep','ta','in','cr'],
11143:                 );
11144:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11145:     foreach my $context (@contexts) {
11146:         foreach my $role (@{$roles{$context}}) {
11147:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
11148:             foreach my $item (@fields) {
11149:                 if (grep(/^\Q$item\E$/,@modifiable)) {
11150:                     $modifyhash{$context}{$role}{$item} = 1;
11151:                 } else {
11152:                     $modifyhash{$context}{$role}{$item} = 0;
11153:                 }
11154:             }
11155:         }
11156:         if (ref($curr_usermodification{$context}) eq 'HASH') {
11157:             foreach my $role (@{$roles{$context}}) {
11158:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
11159:                     foreach my $field (@fields) {
11160:                         if ($modifyhash{$context}{$role}{$field} ne 
11161:                                 $curr_usermodification{$context}{$role}{$field}) {
11162:                             push(@{$changes{$context}},$role);
11163:                             last;
11164:                         }
11165:                     }
11166:                 }
11167:             }
11168:         } else {
11169:             foreach my $context (@contexts) {
11170:                 foreach my $role (@{$roles{$context}}) {
11171:                     push(@{$changes{$context}},$role);
11172:                 }
11173:             }
11174:         }
11175:     }
11176:     my %usermodification_hash =  (
11177:                                    usermodification => \%modifyhash,
11178:                                  );
11179:     my $putresult = &Apache::lonnet::put_dom('configuration',
11180:                                              \%usermodification_hash,$dom);
11181:     if ($putresult eq 'ok') {
11182:         if (keys(%changes) > 0) {
11183:             $resulttext = &mt('Changes made: ').'<ul>';
11184:             foreach my $context (@contexts) {
11185:                 if (ref($changes{$context}) eq 'ARRAY') {
11186:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
11187:                     if (ref($changes{$context}) eq 'ARRAY') {
11188:                         foreach my $role (@{$changes{$context}}) {
11189:                             my $rolename;
11190:                             if ($role eq 'cr') {
11191:                                 $rolename = &mt('Custom');
11192:                             } else {
11193:                                 $rolename = &Apache::lonnet::plaintext($role);
11194:                             }
11195:                             my @modifiable;
11196:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
11197:                             foreach my $field (@fields) {
11198:                                 if ($modifyhash{$context}{$role}{$field}) {
11199:                                     push(@modifiable,$fieldtitles{$field});
11200:                                 }
11201:                             }
11202:                             if (@modifiable > 0) {
11203:                                 $resulttext .= join(', ',@modifiable);
11204:                             } else {
11205:                                 $resulttext .= &mt('none'); 
11206:                             }
11207:                             $resulttext .= '</li>';
11208:                         }
11209:                         $resulttext .= '</ul></li>';
11210:                     }
11211:                 }
11212:             }
11213:             $resulttext .= '</ul>';
11214:         } else {
11215:             $resulttext = &mt('No changes made to user modification settings');
11216:         }
11217:     } else {
11218:         $resulttext = '<span class="LC_error">'.
11219:             &mt('An error occurred: [_1]',$putresult).'</span>';
11220:     }
11221:     return $resulttext;
11222: }
11223: 
11224: sub modify_defaults {
11225:     my ($dom,$lastactref,%domconfig) = @_;
11226:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
11227:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11228:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
11229:                  'portal_def','intauth_cost','intauth_check','intauth_switch');
11230:     my @authtypes = ('internal','krb4','krb5','localauth');
11231:     foreach my $item (@items) {
11232:         $newvalues{$item} = $env{'form.'.$item};
11233:         if ($item eq 'auth_def') {
11234:             if ($newvalues{$item} ne '') {
11235:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
11236:                     push(@errors,$item);
11237:                 }
11238:             }
11239:         } elsif ($item eq 'lang_def') {
11240:             if ($newvalues{$item} ne '') {
11241:                 if ($newvalues{$item} =~ /^(\w+)/) {
11242:                     my $langcode = $1;
11243:                     if ($langcode ne 'x_chef') {
11244:                         if (code2language($langcode) eq '') {
11245:                             push(@errors,$item);
11246:                         }
11247:                     }
11248:                 } else {
11249:                     push(@errors,$item);
11250:                 }
11251:             }
11252:         } elsif ($item eq 'timezone_def') {
11253:             if ($newvalues{$item} ne '') {
11254:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
11255:                     push(@errors,$item);   
11256:                 }
11257:             }
11258:         } elsif ($item eq 'datelocale_def') {
11259:             if ($newvalues{$item} ne '') {
11260:                 my @datelocale_ids = DateTime::Locale->ids();
11261:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
11262:                     push(@errors,$item);
11263:                 }
11264:             }
11265:         } elsif ($item eq 'portal_def') {
11266:             if ($newvalues{$item} ne '') {
11267:                 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])\/?$/) {
11268:                     push(@errors,$item);
11269:                 }
11270:             }
11271:         } elsif ($item eq 'intauth_cost') {
11272:             if ($newvalues{$item} ne '') {
11273:                 if ($newvalues{$item} =~ /\D/) {
11274:                     push(@errors,$item);
11275:                 }
11276:             }
11277:         } elsif ($item eq 'intauth_check') {
11278:             if ($newvalues{$item} ne '') {
11279:                 unless ($newvalues{$item} =~ /^(0|1|2)$/) {
11280:                     push(@errors,$item);
11281:                 }
11282:             }
11283:         } elsif ($item eq 'intauth_switch') {
11284:             if ($newvalues{$item} ne '') {
11285:                 unless ($newvalues{$item} =~ /^(0|1|2)$/) {
11286:                     push(@errors,$item);
11287:                 }
11288:             }
11289:         }
11290:         if (grep(/^\Q$item\E$/,@errors)) {
11291:             $newvalues{$item} = $domdefaults{$item};
11292:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
11293:             $changes{$item} = 1;
11294:         }
11295:         $domdefaults{$item} = $newvalues{$item};
11296:     }
11297:     my %defaults_hash = (
11298:                          defaults => \%newvalues,
11299:                         );
11300:     my $title = &defaults_titles();
11301: 
11302:     my $currinststatus;
11303:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
11304:         $currinststatus = $domconfig{'inststatus'};
11305:     } else {
11306:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
11307:         $currinststatus = {
11308:                              inststatustypes => $usertypes,
11309:                              inststatusorder => $types,
11310:                              inststatusguest => [],
11311:                           };
11312:     }
11313:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
11314:     my @allpos;
11315:     my %alltypes;
11316:     my @inststatusguest;
11317:     if (ref($currinststatus) eq 'HASH') {
11318:         if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
11319:             foreach my $type (@{$currinststatus->{'inststatusguest'}}) {
11320:                 unless (grep(/^\Q$type\E$/,@todelete)) {
11321:                     push(@inststatusguest,$type);
11322:                 }
11323:             }
11324:         }
11325:     }
11326:     my ($currtitles,$currorder);
11327:     if (ref($currinststatus) eq 'HASH') {
11328:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
11329:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
11330:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
11331:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
11332:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
11333:                     }
11334:                 }
11335:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
11336:                     my $position = $env{'form.inststatus_pos_'.$type};
11337:                     $position =~ s/\D+//g;
11338:                     $allpos[$position] = $type;
11339:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
11340:                     $alltypes{$type} =~ s/`//g;
11341:                 }
11342:             }
11343:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
11344:             $currtitles =~ s/,$//;
11345:         }
11346:     }
11347:     if ($env{'form.addinststatus'}) {
11348:         my $newtype = $env{'form.addinststatus'};
11349:         $newtype =~ s/\W//g;
11350:         unless (exists($alltypes{$newtype})) {
11351:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
11352:             $alltypes{$newtype} =~ s/`//g; 
11353:             my $position = $env{'form.addinststatus_pos'};
11354:             $position =~ s/\D+//g;
11355:             if ($position ne '') {
11356:                 $allpos[$position] = $newtype;
11357:             }
11358:         }
11359:     }
11360:     my @orderedstatus;
11361:     foreach my $type (@allpos) {
11362:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
11363:             push(@orderedstatus,$type);
11364:         }
11365:     }
11366:     foreach my $type (keys(%alltypes)) {
11367:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
11368:             delete($alltypes{$type});
11369:         }
11370:     }
11371:     $defaults_hash{'inststatus'} = {
11372:                                      inststatustypes => \%alltypes,
11373:                                      inststatusorder => \@orderedstatus,
11374:                                      inststatusguest => \@inststatusguest,
11375:                                    };
11376:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
11377:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
11378:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
11379:         }
11380:     }
11381:     if ($currorder ne join(',',@orderedstatus)) {
11382:         $changes{'inststatus'}{'inststatusorder'} = 1;
11383:     }
11384:     my $newtitles;
11385:     foreach my $item (@orderedstatus) {
11386:         $newtitles .= $alltypes{$item}.',';
11387:     }
11388:     $newtitles =~ s/,$//;
11389:     if ($currtitles ne $newtitles) {
11390:         $changes{'inststatus'}{'inststatustypes'} = 1;
11391:     }
11392:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
11393:                                              $dom);
11394:     if ($putresult eq 'ok') {
11395:         if (keys(%changes) > 0) {
11396:             $resulttext = &mt('Changes made:').'<ul>';
11397:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
11398:             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";
11399:             foreach my $item (sort(keys(%changes))) {
11400:                 if ($item eq 'inststatus') {
11401:                     if (ref($changes{'inststatus'}) eq 'HASH') {
11402:                         if (@orderedstatus) {
11403:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
11404:                             foreach my $type (@orderedstatus) { 
11405:                                 $resulttext .= $alltypes{$type}.', ';
11406:                             }
11407:                             $resulttext =~ s/, $//;
11408:                             $resulttext .= '</li>';
11409:                         } else {
11410:                             $resulttext .= '<li>'.&mt('Institutional user status types deleted').'</li>';
11411:                         }
11412:                     }
11413:                 } else {
11414:                     my $value = $env{'form.'.$item};
11415:                     if ($value eq '') {
11416:                         $value = &mt('none');
11417:                     } elsif ($item eq 'auth_def') {
11418:                         my %authnames = &authtype_names();
11419:                         my %shortauth = (
11420:                                           internal   => 'int',
11421:                                           krb4       => 'krb4',
11422:                                           krb5       => 'krb5',
11423:                                           localauth  => 'loc',
11424:                         );
11425:                         $value = $authnames{$shortauth{$value}};
11426:                     } elsif ($item eq 'intauth_switch') {
11427:                         my %optiondesc = &Apache::lonlocal::texthash (
11428:                                             0 => 'No',
11429:                                             1 => 'Yes',
11430:                                             2 => 'Yes, and copy existing passwd file to passwd.bak file',
11431:                                          );
11432:                         if ($value =~ /^(0|1|2)$/) {
11433:                             $value = $optiondesc{$value};
11434:                         } else {
11435:                             $value = &mt('none -- defaults to No');
11436:                         }
11437:                     } elsif ($item eq 'intauth_check') {
11438:                         my %optiondesc = &Apache::lonlocal::texthash (
11439:                                              0 => 'No',
11440:                                              1 => 'Yes, allow login then update passwd file using default cost (if higher)',
11441:                                              2 => 'Yes, disallow login if stored cost is less than domain default',
11442:                                          );
11443:                         if ($value =~ /^(0|1|2)$/) {
11444:                             $value = $optiondesc{$value};
11445:                         } else {
11446:                             $value = &mt('none -- defaults to No');
11447:                         }
11448:                     }
11449:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
11450:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
11451:                 }
11452:             }
11453:             $resulttext .= '</ul>';
11454:             $mailmsgtext .= "\n";
11455:             my $cachetime = 24*60*60;
11456:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
11457:             if (ref($lastactref) eq 'HASH') {
11458:                 $lastactref->{'domdefaults'} = 1;
11459:             }
11460:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
11461:                 my $notify = 1;
11462:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
11463:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
11464:                         $notify = 0;
11465:                     }
11466:                 }
11467:                 if ($notify) {
11468:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
11469:                                                "LON-CAPA Domain Settings Change - $dom",
11470:                                                $mailmsgtext);
11471:                 }
11472:             }
11473:         } else {
11474:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
11475:         }
11476:     } else {
11477:         $resulttext = '<span class="LC_error">'.
11478:             &mt('An error occurred: [_1]',$putresult).'</span>';
11479:     }
11480:     if (@errors > 0) {
11481:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
11482:         foreach my $item (@errors) {
11483:             $resulttext .= ' "'.$title->{$item}.'",';
11484:         }
11485:         $resulttext =~ s/,$//;
11486:     }
11487:     return $resulttext;
11488: }
11489: 
11490: sub modify_scantron {
11491:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
11492:     my ($resulttext,%confhash,%changes,$errors);
11493:     my $custom = 'custom.tab';
11494:     my $default = 'default.tab';
11495:     my $servadm = $r->dir_config('lonAdmEMail');
11496:     my ($configuserok,$author_ok,$switchserver) = 
11497:         &config_check($dom,$confname,$servadm);
11498:     if ($env{'form.scantronformat.filename'} ne '') {
11499:         my $error;
11500:         if ($configuserok eq 'ok') {
11501:             if ($switchserver) {
11502:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
11503:             } else {
11504:                 if ($author_ok eq 'ok') {
11505:                     my ($result,$scantronurl) =
11506:                         &publishlogo($r,'upload','scantronformat',$dom,
11507:                                      $confname,'scantron','','',$custom);
11508:                     if ($result eq 'ok') {
11509:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
11510:                         $changes{'scantronformat'} = 1;
11511:                     } else {
11512:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
11513:                     }
11514:                 } else {
11515:                     $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);
11516:                 }
11517:             }
11518:         } else {
11519:             $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);
11520:         }
11521:         if ($error) {
11522:             &Apache::lonnet::logthis($error);
11523:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11524:         }
11525:     }
11526:     if (ref($domconfig{'scantron'}) eq 'HASH') {
11527:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
11528:             if ($env{'form.scantronformat_del'}) {
11529:                 $confhash{'scantron'}{'scantronformat'} = '';
11530:                 $changes{'scantronformat'} = 1;
11531:             }
11532:         }
11533:     }
11534:     if (keys(%confhash) > 0) {
11535:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
11536:                                                  $dom);
11537:         if ($putresult eq 'ok') {
11538:             if (keys(%changes) > 0) {
11539:                 if (ref($confhash{'scantron'}) eq 'HASH') {
11540:                     $resulttext = &mt('Changes made:').'<ul>';
11541:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
11542:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
11543:                     } else {
11544:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
11545:                     }
11546:                     $resulttext .= '</ul>';
11547:                 } else {
11548:                     $resulttext = &mt('Changes made to bubblesheet format file.');
11549:                 }
11550:                 $resulttext .= '</ul>';
11551:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
11552:                 if (ref($lastactref) eq 'HASH') {
11553:                     $lastactref->{'domainconfig'} = 1;
11554:                 }
11555:             } else {
11556:                 $resulttext = &mt('No changes made to bubblesheet format file');
11557:             }
11558:         } else {
11559:             $resulttext = '<span class="LC_error">'.
11560:                 &mt('An error occurred: [_1]',$putresult).'</span>';
11561:         }
11562:     } else {
11563:         $resulttext = &mt('No changes made to bubblesheet format file'); 
11564:     }
11565:     if ($errors) {
11566:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
11567:                        $errors.'</ul>';
11568:     }
11569:     return $resulttext;
11570: }
11571: 
11572: sub modify_coursecategories {
11573:     my ($dom,$lastactref,%domconfig) = @_;
11574:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
11575:         $cathash);
11576:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
11577:     my @catitems = ('unauth','auth');
11578:     my @cattypes = ('std','domonly','codesrch','none');
11579:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
11580:         $cathash = $domconfig{'coursecategories'}{'cats'};
11581:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
11582:             $changes{'togglecats'} = 1;
11583:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
11584:         }
11585:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
11586:             $changes{'categorize'} = 1;
11587:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
11588:         }
11589:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
11590:             $changes{'togglecatscomm'} = 1;
11591:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
11592:         }
11593:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
11594:             $changes{'categorizecomm'} = 1;
11595:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
11596:         }
11597:         foreach my $item (@catitems) {
11598:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
11599:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
11600:                     $changes{$item} = 1;
11601:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
11602:                 }
11603:             }
11604:         }
11605:     } else {
11606:         $changes{'togglecats'} = 1;
11607:         $changes{'categorize'} = 1;
11608:         $changes{'togglecatscomm'} = 1;
11609:         $changes{'categorizecomm'} = 1;
11610:         $domconfig{'coursecategories'} = {
11611:                                              togglecats => $env{'form.togglecats'},
11612:                                              categorize => $env{'form.categorize'},
11613:                                              togglecatscomm => $env{'form.togglecatscomm'},
11614:                                              categorizecomm => $env{'form.categorizecomm'},
11615:                                          };
11616:         foreach my $item (@catitems) {
11617:             if ($env{'form.coursecat_'.$item} ne 'std') {
11618:                 $changes{$item} = 1;
11619:             }
11620:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
11621:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
11622:             }
11623:         }
11624:     }
11625:     if (ref($cathash) eq 'HASH') {
11626:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
11627:             push (@deletecategory,'instcode::0');
11628:         }
11629:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
11630:             push(@deletecategory,'communities::0');
11631:         }
11632:     }
11633:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
11634:     if (ref($cathash) eq 'HASH') {
11635:         if (@deletecategory > 0) {
11636:             #FIXME Need to remove category from all courses using a deleted category 
11637:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
11638:             foreach my $item (@deletecategory) {
11639:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
11640:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
11641:                     $deletions{$item} = 1;
11642:                     &recurse_cat_deletes($item,$cathash,\%deletions);
11643:                 }
11644:             }
11645:         }
11646:         foreach my $item (keys(%{$cathash})) {
11647:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
11648:             if ($cathash->{$item} ne $env{'form.'.$item}) {
11649:                 $reorderings{$item} = 1;
11650:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
11651:             }
11652:             if ($env{'form.addcategory_name_'.$item} ne '') {
11653:                 my $newcat = $env{'form.addcategory_name_'.$item};
11654:                 my $newdepth = $depth+1;
11655:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
11656:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
11657:                 $adds{$newitem} = 1; 
11658:             }
11659:             if ($env{'form.subcat_'.$item} ne '') {
11660:                 my $newcat = $env{'form.subcat_'.$item};
11661:                 my $newdepth = $depth+1;
11662:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
11663:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
11664:                 $adds{$newitem} = 1;
11665:             }
11666:         }
11667:     }
11668:     if ($env{'form.instcode'} eq '1') {
11669:         if (ref($cathash) eq 'HASH') {
11670:             my $newitem = 'instcode::0';
11671:             if ($cathash->{$newitem} eq '') {  
11672:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
11673:                 $adds{$newitem} = 1;
11674:             }
11675:         } else {
11676:             my $newitem = 'instcode::0';
11677:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
11678:             $adds{$newitem} = 1;
11679:         }
11680:     }
11681:     if ($env{'form.communities'} eq '1') {
11682:         if (ref($cathash) eq 'HASH') {
11683:             my $newitem = 'communities::0';
11684:             if ($cathash->{$newitem} eq '') {
11685:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
11686:                 $adds{$newitem} = 1;
11687:             }
11688:         } else {
11689:             my $newitem = 'communities::0';
11690:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
11691:             $adds{$newitem} = 1;
11692:         }
11693:     }
11694:     if ($env{'form.addcategory_name'} ne '') {
11695:         if (($env{'form.addcategory_name'} ne 'instcode') &&
11696:             ($env{'form.addcategory_name'} ne 'communities')) {
11697:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
11698:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
11699:             $adds{$newitem} = 1;
11700:         }
11701:     }
11702:     my $putresult;
11703:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
11704:         if (keys(%deletions) > 0) {
11705:             foreach my $key (keys(%deletions)) {
11706:                 if ($predelallitems{$key} ne '') {
11707:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
11708:                 }
11709:             }
11710:         }
11711:         my (@chkcats,@chktrails,%chkallitems);
11712:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
11713:         if (ref($chkcats[0]) eq 'ARRAY') {
11714:             my $depth = 0;
11715:             my $chg = 0;
11716:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
11717:                 my $name = $chkcats[0][$i];
11718:                 my $item;
11719:                 if ($name eq '') {
11720:                     $chg ++;
11721:                 } else {
11722:                     $item = &escape($name).'::0';
11723:                     if ($chg) {
11724:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
11725:                     }
11726:                     $depth ++; 
11727:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
11728:                     $depth --;
11729:                 }
11730:             }
11731:         }
11732:     }
11733:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
11734:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
11735:         if ($putresult eq 'ok') {
11736:             my %title = (
11737:                          togglecats     => 'Show/Hide a course in catalog',
11738:                          categorize     => 'Assign a category to a course',
11739:                          togglecatscomm => 'Show/Hide a community in catalog',
11740:                          categorizecomm => 'Assign a category to a community',
11741:                         );
11742:             my %level = (
11743:                          dom  => 'set in Domain ("Modify Course/Community")',
11744:                          crs  => 'set in Course ("Course Configuration")',
11745:                          comm => 'set in Community ("Community Configuration")',
11746:                          none     => 'No catalog',
11747:                          std      => 'Standard catalog',
11748:                          domonly  => 'Domain-only catalog',
11749:                          codesrch => 'Code search form',
11750:                         );
11751:             $resulttext = &mt('Changes made:').'<ul>';
11752:             if ($changes{'togglecats'}) {
11753:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
11754:             }
11755:             if ($changes{'categorize'}) {
11756:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
11757:             }
11758:             if ($changes{'togglecatscomm'}) {
11759:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
11760:             }
11761:             if ($changes{'categorizecomm'}) {
11762:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
11763:             }
11764:             if ($changes{'unauth'}) {
11765:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
11766:             }
11767:             if ($changes{'auth'}) {
11768:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
11769:             }
11770:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
11771:                 my $cathash;
11772:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
11773:                     $cathash = $domconfig{'coursecategories'}{'cats'};
11774:                 } else {
11775:                     $cathash = {};
11776:                 } 
11777:                 my (@cats,@trails,%allitems);
11778:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
11779:                 if (keys(%deletions) > 0) {
11780:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
11781:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
11782:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
11783:                     }
11784:                     $resulttext .= '</ul></li>';
11785:                 }
11786:                 if (keys(%reorderings) > 0) {
11787:                     my %sort_by_trail;
11788:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
11789:                     foreach my $key (keys(%reorderings)) {
11790:                         if ($allitems{$key} ne '') {
11791:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
11792:                         }
11793:                     }
11794:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
11795:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
11796:                     }
11797:                     $resulttext .= '</ul></li>';
11798:                 }
11799:                 if (keys(%adds) > 0) {
11800:                     my %sort_by_trail;
11801:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
11802:                     foreach my $key (keys(%adds)) {
11803:                         if ($allitems{$key} ne '') {
11804:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
11805:                         }
11806:                     }
11807:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
11808:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
11809:                     }
11810:                     $resulttext .= '</ul></li>';
11811:                 }
11812:                 &Apache::lonnet::do_cache_new('cats',$dom,$cathash,3600);
11813:                 if (ref($lastactref) eq 'HASH') {
11814:                     $lastactref->{'cats'} = 1;
11815:                 }
11816:             }
11817:             $resulttext .= '</ul>';
11818:             if ($changes{'unauth'} || $changes{'auth'}) {
11819:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
11820:                 if ($changes{'auth'}) {
11821:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
11822:                 }
11823:                 if ($changes{'unauth'}) {
11824:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
11825:                 }
11826:                 my $cachetime = 24*60*60;
11827:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
11828:                 if (ref($lastactref) eq 'HASH') {
11829:                     $lastactref->{'domdefaults'} = 1;
11830:                 }
11831:             }
11832:         } else {
11833:             $resulttext = '<span class="LC_error">'.
11834:                           &mt('An error occurred: [_1]',$putresult).'</span>';
11835:         }
11836:     } else {
11837:         $resulttext = &mt('No changes made to course and community categories');
11838:     }
11839:     return $resulttext;
11840: }
11841: 
11842: sub modify_serverstatuses {
11843:     my ($dom,%domconfig) = @_;
11844:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
11845:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
11846:         %currserverstatus = %{$domconfig{'serverstatuses'}};
11847:     }
11848:     my @pages = &serverstatus_pages();
11849:     foreach my $type (@pages) {
11850:         $newserverstatus{$type}{'namedusers'} = '';
11851:         $newserverstatus{$type}{'machines'} = '';
11852:         if (defined($env{'form.'.$type.'_namedusers'})) {
11853:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
11854:             my @okusers;
11855:             foreach my $user (@users) {
11856:                 my ($uname,$udom) = split(/:/,$user);
11857:                 if (($udom =~ /^$match_domain$/) &&   
11858:                     (&Apache::lonnet::domain($udom)) &&
11859:                     ($uname =~ /^$match_username$/)) {
11860:                     if (!grep(/^\Q$user\E/,@okusers)) {
11861:                         push(@okusers,$user);
11862:                     }
11863:                 }
11864:             }
11865:             if (@okusers > 0) {
11866:                  @okusers = sort(@okusers);
11867:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
11868:             }
11869:         }
11870:         if (defined($env{'form.'.$type.'_machines'})) {
11871:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
11872:             my @okmachines;
11873:             foreach my $ip (@machines) {
11874:                 my @parts = split(/\./,$ip);
11875:                 next if (@parts < 4);
11876:                 my $badip = 0;
11877:                 for (my $i=0; $i<4; $i++) {
11878:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
11879:                         $badip = 1;
11880:                         last;
11881:                     }
11882:                 }
11883:                 if (!$badip) {
11884:                     push(@okmachines,$ip);     
11885:                 }
11886:             }
11887:             @okmachines = sort(@okmachines);
11888:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
11889:         }
11890:     }
11891:     my %serverstatushash =  (
11892:                                 serverstatuses => \%newserverstatus,
11893:                             );
11894:     foreach my $type (@pages) {
11895:         foreach my $setting ('namedusers','machines') {
11896:             my (@current,@new);
11897:             if (ref($currserverstatus{$type}) eq 'HASH') {
11898:                 if ($currserverstatus{$type}{$setting} ne '') { 
11899:                     @current = split(/,/,$currserverstatus{$type}{$setting});
11900:                 }
11901:             }
11902:             if ($newserverstatus{$type}{$setting} ne '') {
11903:                 @new = split(/,/,$newserverstatus{$type}{$setting});
11904:             }
11905:             if (@current > 0) {
11906:                 if (@new > 0) {
11907:                     foreach my $item (@current) {
11908:                         if (!grep(/^\Q$item\E$/,@new)) {
11909:                             $changes{$type}{$setting} = 1;
11910:                             last;
11911:                         }
11912:                     }
11913:                     foreach my $item (@new) {
11914:                         if (!grep(/^\Q$item\E$/,@current)) {
11915:                             $changes{$type}{$setting} = 1;
11916:                             last;
11917:                         }
11918:                     }
11919:                 } else {
11920:                     $changes{$type}{$setting} = 1;
11921:                 }
11922:             } elsif (@new > 0) {
11923:                 $changes{$type}{$setting} = 1;
11924:             }
11925:         }
11926:     }
11927:     if (keys(%changes) > 0) {
11928:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
11929:         my $putresult = &Apache::lonnet::put_dom('configuration',
11930:                                                  \%serverstatushash,$dom);
11931:         if ($putresult eq 'ok') {
11932:             $resulttext .= &mt('Changes made:').'<ul>';
11933:             foreach my $type (@pages) {
11934:                 if (ref($changes{$type}) eq 'HASH') {
11935:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
11936:                     if ($changes{$type}{'namedusers'}) {
11937:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
11938:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
11939:                         } else {
11940:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
11941:                         }
11942:                     }
11943:                     if ($changes{$type}{'machines'}) {
11944:                         if ($newserverstatus{$type}{'machines'} eq '') {
11945:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
11946:                         } else {
11947:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
11948:                         }
11949: 
11950:                     }
11951:                     $resulttext .= '</ul></li>';
11952:                 }
11953:             }
11954:             $resulttext .= '</ul>';
11955:         } else {
11956:             $resulttext = '<span class="LC_error">'.
11957:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
11958: 
11959:         }
11960:     } else {
11961:         $resulttext = &mt('No changes made to access to server status pages');
11962:     }
11963:     return $resulttext;
11964: }
11965: 
11966: sub modify_helpsettings {
11967:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
11968:     my ($resulttext,$errors,%changes,%helphash);
11969:     my %defaultchecked = ('submitbugs' => 'on');
11970:     my @offon = ('off','on');
11971:     my @toggles = ('submitbugs');
11972:     my %current = ('submitbugs' => '',
11973:                    'adhoc'      => {},
11974:                   );
11975:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
11976:         %current = %{$domconfig{'helpsettings'}};
11977:     }
11978:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11979:     foreach my $item (@toggles) {
11980:         if ($defaultchecked{$item} eq 'on') { 
11981:             if ($current{$item} eq '') {
11982:                 if ($env{'form.'.$item} eq '0') {
11983:                     $changes{$item} = 1;
11984:                 }
11985:             } elsif ($current{$item} ne $env{'form.'.$item}) {
11986:                 $changes{$item} = 1;
11987:             }
11988:         } elsif ($defaultchecked{$item} eq 'off') {
11989:             if ($current{$item} eq '') {
11990:                 if ($env{'form.'.$item} eq '1') {
11991:                     $changes{$item} = 1;
11992:                 }
11993:             } elsif ($current{$item} ne $env{'form.'.$item}) {
11994:                 $changes{$item} = 1;
11995:             }
11996:         }
11997:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
11998:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
11999:         }
12000:     }
12001:     my $maxnum = $env{'form.helproles_maxnum'};
12002:     my $confname = $dom.'-domainconfig';
12003:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
12004:     my (@allpos,%newsettings,%changedprivs,$newrole);
12005:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12006:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
12007:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
12008:     my %lt = &Apache::lonlocal::texthash(
12009:                     s      => 'system',
12010:                     d      => 'domain',
12011:                     order  => 'Display order',
12012:                     access => 'Role usage',
12013:                     all    => 'All with domain helpdesk or helpdesk assistant role',
12014:                     dh     => 'All with domain helpdesk role',
12015:                     da     => 'All with domain helpdesk assistant role',
12016:                     none   => 'None',
12017:                     status => 'Determined based on institutional status',
12018:                     inc    => 'Include all, but exclude specific personnel',
12019:                     exc    => 'Exclude all, but include specific personnel',
12020:     );
12021:     for (my $num=0; $num<=$maxnum; $num++) {
12022:         my ($prefix,$identifier,$rolename,%curr);
12023:         if ($num == $maxnum) {
12024:             next unless ($env{'form.newcusthelp'} == $maxnum);
12025:             $identifier = 'custhelp'.$num;
12026:             $prefix = 'helproles_'.$num;
12027:             $rolename = $env{'form.custhelpname'.$num};
12028:             $rolename=~s/[^A-Za-z0-9]//gs;
12029:             next if ($rolename eq '');
12030:             next if (exists($existing{'rolesdef_'.$rolename}));
12031:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
12032:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
12033:                                                      $newprivs{'c'},$confname,$dom);
12034:             if ($result ne 'ok') {
12035:                 $errors .= '<li><span class="LC_error">'.
12036:                            &mt('An error occurred storing the new custom role: [_1]',
12037:                            $result).'</span></li>';
12038:                 next;
12039:             } else {
12040:                 $changedprivs{$rolename} = \%newprivs;
12041:                 $newrole = $rolename;
12042:             }
12043:         } else {
12044:             $prefix = 'helproles_'.$num;
12045:             $rolename = $env{'form.'.$prefix};
12046:             next if ($rolename eq '');
12047:             next unless (exists($existing{'rolesdef_'.$rolename}));
12048:             $identifier = 'custhelp'.$num;
12049:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
12050:             my %currprivs;
12051:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
12052:                 split(/\_/,$existing{'rolesdef_'.$rolename});
12053:             foreach my $level ('c','d','s') {
12054:                 if ($newprivs{$level} ne $currprivs{$level}) {
12055:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
12056:                                                              $newprivs{'c'},$confname,$dom);
12057:                     if ($result ne 'ok') {
12058:                         $errors .= '<li><span class="LC_error">'.
12059:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
12060:                                        $rolename,$result).'</span></li>';
12061:                     } else {
12062:                         $changedprivs{$rolename} = \%newprivs;
12063:                     }
12064:                     last;
12065:                 }
12066:             }
12067:             if (ref($current{'adhoc'}) eq 'HASH') {
12068:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
12069:                     %curr = %{$current{'adhoc'}{$rolename}};
12070:                 }
12071:             }
12072:         }
12073:         my $newpos = $env{'form.'.$prefix.'_pos'};
12074:         $newpos =~ s/\D+//g;
12075:         $allpos[$newpos] = $rolename;
12076:         my $newdesc = $env{'form.'.$prefix.'_desc'};
12077:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
12078:         if ($curr{'desc'}) {
12079:             if ($curr{'desc'} ne $newdesc) {
12080:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
12081:                 $newsettings{$rolename}{'desc'} = $newdesc;
12082:             }
12083:         } elsif ($newdesc ne '') {
12084:             $changes{'customrole'}{$rolename}{'desc'} = 1;
12085:             $newsettings{$rolename}{'desc'} = $newdesc;
12086:         }
12087:         my $access = $env{'form.'.$prefix.'_access'};
12088:         if (grep(/^\Q$access\E$/,@accesstypes)) {
12089:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
12090:             if ($access eq 'status') {
12091:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
12092:                 if (scalar(@statuses) == 0) {
12093:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
12094:                 } else {
12095:                     my (@shownstatus,$numtypes);
12096:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
12097:                     if (ref($types) eq 'ARRAY') {
12098:                         $numtypes = scalar(@{$types});
12099:                         foreach my $type (sort(@statuses)) {
12100:                             if ($type eq 'default') {
12101:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
12102:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
12103:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
12104:                                 push(@shownstatus,$usertypes->{$type});
12105:                             }
12106:                         }
12107:                     }
12108:                     if (grep(/^default$/,@statuses)) {
12109:                         push(@shownstatus,$othertitle);
12110:                     }
12111:                     if (scalar(@shownstatus) == 1+$numtypes) {
12112:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
12113:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
12114:                     } else {
12115:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
12116:                         if (ref($curr{'status'}) eq 'ARRAY') {
12117:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
12118:                             if (@diffs) {
12119:                                 $changes{'customrole'}{$rolename}{$access} = 1;
12120:                             }
12121:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
12122:                             $changes{'customrole'}{$rolename}{$access} = 1;
12123:                         }
12124:                     }
12125:                 }
12126:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
12127:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
12128:                 my @newspecstaff;
12129:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
12130:                 foreach my $person (sort(@personnel)) {
12131:                     if ($domhelpdesk{$person}) {
12132:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
12133:                     }
12134:                 }
12135:                 if (ref($curr{$access}) eq 'ARRAY') {
12136:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
12137:                     if (@diffs) {
12138:                         $changes{'customrole'}{$rolename}{$access} = 1;
12139:                     }
12140:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
12141:                     $changes{'customrole'}{$rolename}{$access} = 1;
12142:                 }
12143:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
12144:                     my ($uname,$udom) = split(/:/,$person);
12145:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
12146:                 }
12147:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
12148:             }
12149:         } else {
12150:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
12151:         }
12152:         unless ($curr{'access'} eq $access) {
12153:             $changes{'customrole'}{$rolename}{'access'} = 1;
12154:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
12155:         }
12156:     }
12157:     if (@allpos > 0) {
12158:         my $idx = 0;
12159:         foreach my $rolename (@allpos) {
12160:             if ($rolename ne '') {
12161:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
12162:                 if (ref($current{'adhoc'}) eq 'HASH') {
12163:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
12164:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
12165:                             $changes{'customrole'}{$rolename}{'order'} = 1;
12166:                             $newsettings{$rolename}{'order'} = $idx+1;
12167:                         }
12168:                     }
12169:                 }
12170:                 $idx ++;
12171:             }
12172:         }
12173:     }
12174:     my $putresult;
12175:     if (keys(%changes) > 0) {
12176:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
12177:         if ($putresult eq 'ok') {
12178:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
12179:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
12180:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
12181:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
12182:                 }
12183:             }
12184:             my $cachetime = 24*60*60;
12185:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12186:             if (ref($lastactref) eq 'HASH') {
12187:                 $lastactref->{'domdefaults'} = 1;
12188:             }
12189:         } else {
12190:             $errors .= '<li><span class="LC_error">'.
12191:                        &mt('An error occurred storing the settings: [_1]',
12192:                            $putresult).'</span></li>';
12193:         }
12194:     }
12195:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
12196:         $resulttext = &mt('Changes made:').'<ul>';
12197:         my (%shownprivs,@levelorder);
12198:         @levelorder = ('c','d','s');
12199:         if ((keys(%changes)) && ($putresult eq 'ok')) {
12200:             foreach my $item (sort(keys(%changes))) {
12201:                 if ($item eq 'submitbugs') {
12202:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
12203:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
12204:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
12205:                 } elsif ($item eq 'customrole') {
12206:                     if (ref($changes{'customrole'}) eq 'HASH') {
12207:                         my @keyorder = ('order','desc','access','status','exc','inc');
12208:                         my %keytext = &Apache::lonlocal::texthash(
12209:                                                                    order  => 'Order',
12210:                                                                    desc   => 'Role description',
12211:                                                                    access => 'Role usage',
12212:                                                                    status => 'Allowed institutional types',
12213:                                                                    exc    => 'Allowed personnel',
12214:                                                                    inc    => 'Disallowed personnel',
12215:                         );
12216:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
12217:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
12218:                                 if ($role eq $newrole) {
12219:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
12220:                                                               $role).'<ul>';
12221:                                 } else {
12222:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
12223:                                                               $role).'<ul>';
12224:                                 }
12225:                                 foreach my $key (@keyorder) {
12226:                                     if ($changes{'customrole'}{$role}{$key}) {
12227:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
12228:                                                                   $keytext{$key},$newsettings{$role}{$key}).
12229:                                                        '</li>';
12230:                                     }
12231:                                 }
12232:                                 if (ref($changedprivs{$role}) eq 'HASH') {
12233:                                     $shownprivs{$role} = 1;
12234:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
12235:                                     foreach my $level (@levelorder) {
12236:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
12237:                                             next if ($item eq '');
12238:                                             my ($priv) = split(/\&/,$item,2);
12239:                                             if (&Apache::lonnet::plaintext($priv)) {
12240:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
12241:                                                 unless ($level eq 'c') {
12242:                                                     $resulttext .= ' ('.$lt{$level}.')';
12243:                                                 }
12244:                                                 $resulttext .= '</li>';
12245:                                             }
12246:                                         }
12247:                                     }
12248:                                     $resulttext .= '</ul>';
12249:                                 }
12250:                                 $resulttext .= '</ul></li>';
12251:                             }
12252:                         }
12253:                     }
12254:                 }
12255:             }
12256:         }
12257:         if (keys(%changedprivs)) {
12258:             foreach my $role (sort(keys(%changedprivs))) {
12259:                 unless ($shownprivs{$role}) {
12260:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
12261:                                               $role).'<ul>'.
12262:                                    '<li>'.&mt('Privileges set to :').'<ul>';
12263:                     foreach my $level (@levelorder) {
12264:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
12265:                             next if ($item eq '');
12266:                             my ($priv) = split(/\&/,$item,2);
12267:                             if (&Apache::lonnet::plaintext($priv)) {
12268:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
12269:                                 unless ($level eq 'c') {
12270:                                     $resulttext .= ' ('.$lt{$level}.')';
12271:                                 }
12272:                                 $resulttext .= '</li>';
12273:                             }
12274:                         }
12275:                     }
12276:                     $resulttext .= '</ul></li></ul></li>';
12277:                 }
12278:             }
12279:         }
12280:         $resulttext .= '</ul>';
12281:     } else {
12282:         $resulttext = &mt('No changes made to help settings');
12283:     }
12284:     if ($errors) {
12285:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
12286:                                     $errors.'</ul>';
12287:     }
12288:     return $resulttext;
12289: }
12290: 
12291: sub modify_coursedefaults {
12292:     my ($dom,$lastactref,%domconfig) = @_;
12293:     my ($resulttext,$errors,%changes,%defaultshash);
12294:     my %defaultchecked = (
12295:                            'uselcmath'       => 'on',
12296:                            'usejsme'         => 'on'
12297:                          );
12298:     my @toggles = ('uselcmath','usejsme');
12299:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
12300:                    'uploadquota_community','uploadquota_textbook','mysqltables_official',
12301:                    'mysqltables_unofficial','mysqltables_community','mysqltables_textbook');
12302:     my @types = ('official','unofficial','community','textbook');
12303:     my %staticdefaults = (
12304:                            anonsurvey_threshold => 10,
12305:                            uploadquota          => 500,
12306:                            postsubmit           => 60,
12307:                            mysqltables          => 172800,
12308:                          );
12309:     my %texoptions = (
12310:                         MathJax  => 'MathJax',
12311:                         mimetex  => &mt('Convert to Images'),
12312:                         tth      => &mt('TeX to HTML'),
12313:                      );
12314:     $defaultshash{'coursedefaults'} = {};
12315: 
12316:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
12317:         if ($domconfig{'coursedefaults'} eq '') {
12318:             $domconfig{'coursedefaults'} = {};
12319:         }
12320:     }
12321: 
12322:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
12323:         foreach my $item (@toggles) {
12324:             if ($defaultchecked{$item} eq 'on') {
12325:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
12326:                     ($env{'form.'.$item} eq '0')) {
12327:                     $changes{$item} = 1;
12328:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
12329:                     $changes{$item} = 1;
12330:                 }
12331:             } elsif ($defaultchecked{$item} eq 'off') {
12332:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
12333:                     ($env{'form.'.$item} eq '1')) {
12334:                     $changes{$item} = 1;
12335:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
12336:                     $changes{$item} = 1;
12337:                 }
12338:             }
12339:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
12340:         }
12341:         foreach my $item (@numbers) {
12342:             my ($currdef,$newdef);
12343:             $newdef = $env{'form.'.$item};
12344:             if ($item eq 'anonsurvey_threshold') {
12345:                 $currdef = $domconfig{'coursedefaults'}{$item};
12346:                 $newdef =~ s/\D//g;
12347:                 if ($newdef eq '' || $newdef < 1) {
12348:                     $newdef = 1;
12349:                 }
12350:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
12351:             } else {
12352:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
12353:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
12354:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
12355:                 }
12356:                 $newdef =~ s/[^\w.\-]//g;
12357:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
12358:             }
12359:             if ($currdef ne $newdef) {
12360:                 if ($item eq 'anonsurvey_threshold') {
12361:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
12362:                         $changes{$item} = 1;
12363:                     }
12364:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
12365:                     my $setting = $1;
12366:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
12367:                         $changes{$setting} = 1;
12368:                     }
12369:                 }
12370:             }
12371:         }
12372:         my $texengine;
12373:         if ($env{'form.texengine'} =~ /^(MathJax|mimetex|tth)$/) {
12374:             $texengine = $env{'form.texengine'};
12375:             my $currdef = $domconfig{'coursedefaults'}{'texengine'};
12376:             if ($currdef eq '') {
12377:                 unless ($texengine eq $Apache::lonnet::deftex) {
12378:                     $changes{'texengine'} = 1;
12379:                 }
12380:             } elsif ($currdef ne $texengine) {
12381:                 $changes{'texengine'} = 1;
12382:             }
12383:         }
12384:         if ($texengine ne '') {
12385:             $defaultshash{'coursedefaults'}{'texengine'} = $texengine;
12386:         }
12387:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
12388:         my @currclonecode;
12389:         if (ref($currclone) eq 'HASH') {
12390:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
12391:                 @currclonecode = @{$currclone->{'instcode'}};
12392:             }
12393:         }
12394:         my $newclone;
12395:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
12396:             $newclone = $env{'form.canclone'};
12397:         }
12398:         if ($newclone eq 'instcode') {
12399:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
12400:             my (%codedefaults,@code_order,@clonecode);
12401:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
12402:                                                     \@code_order);
12403:             foreach my $item (@code_order) {
12404:                 if (grep(/^\Q$item\E$/,@newcodes)) {
12405:                     push(@clonecode,$item);
12406:                 }
12407:             }
12408:             if (@clonecode) {
12409:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
12410:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
12411:                 if (@diffs) {
12412:                     $changes{'canclone'} = 1;
12413:                 }
12414:             } else {
12415:                 $newclone eq '';
12416:             }
12417:         } elsif ($newclone ne '') {
12418:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
12419:         }
12420:         if ($newclone ne $currclone) {
12421:             $changes{'canclone'} = 1;
12422:         }
12423:         my %credits;
12424:         foreach my $type (@types) {
12425:             unless ($type eq 'community') {
12426:                 $credits{$type} = $env{'form.'.$type.'_credits'};
12427:                 $credits{$type} =~ s/[^\d.]+//g;
12428:             }
12429:         }
12430:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
12431:             ($env{'form.coursecredits'} eq '1')) {
12432:             $changes{'coursecredits'} = 1;
12433:             foreach my $type (keys(%credits)) {
12434:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
12435:             }
12436:         } else {
12437:             if ($env{'form.coursecredits'} eq '1') {
12438:                 foreach my $type (@types) {
12439:                     unless ($type eq 'community') {
12440:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
12441:                             $changes{'coursecredits'} = 1;
12442:                         }
12443:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
12444:                     }
12445:                 }
12446:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
12447:                 foreach my $type (@types) {
12448:                     unless ($type eq 'community') {
12449:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
12450:                             $changes{'coursecredits'} = 1;
12451:                             last;
12452:                         }
12453:                     }
12454:                 }
12455:             }
12456:         }
12457:         if ($env{'form.postsubmit'} eq '1') {
12458:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
12459:             my %currtimeout;
12460:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
12461:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
12462:                     $changes{'postsubmit'} = 1;
12463:                 }
12464:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
12465:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
12466:                 }
12467:             } else {
12468:                 $changes{'postsubmit'} = 1;
12469:             }
12470:             foreach my $type (@types) {
12471:                 my $timeout = $env{'form.'.$type.'_timeout'};
12472:                 $timeout =~ s/\D//g;
12473:                 if ($timeout == $staticdefaults{'postsubmit'}) {
12474:                     $timeout = '';
12475:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
12476:                     $timeout = '0';
12477:                 }
12478:                 unless ($timeout eq '') {
12479:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
12480:                 }
12481:                 if (exists($currtimeout{$type})) {
12482:                     if ($timeout ne $currtimeout{$type}) {
12483:                         $changes{'postsubmit'} = 1;
12484:                     }
12485:                 } elsif ($timeout ne '') {
12486:                     $changes{'postsubmit'} = 1;
12487:                 }
12488:             }
12489:         } else {
12490:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
12491:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
12492:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
12493:                     $changes{'postsubmit'} = 1;
12494:                 }
12495:             } else {
12496:                 $changes{'postsubmit'} = 1;
12497:             }
12498:         }
12499:     }
12500:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
12501:                                              $dom);
12502:     if ($putresult eq 'ok') {
12503:         if (keys(%changes) > 0) {
12504:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12505:             if (($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
12506:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
12507:                 ($changes{'canclone'}) || ($changes{'mysqltables'}) || ($changes{'texengine'})) {
12508:                 foreach my $item ('uselcmath','usejsme','texengine') {
12509:                     if ($changes{$item}) {
12510:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
12511:                     }
12512:                 }
12513:                 if ($changes{'coursecredits'}) {
12514:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
12515:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
12516:                             $domdefaults{$type.'credits'} =
12517:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
12518:                         }
12519:                     }
12520:                 }
12521:                 if ($changes{'postsubmit'}) {
12522:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
12523:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
12524:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
12525:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
12526:                                 $domdefaults{$type.'postsubtimeout'} =
12527:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
12528:                             }
12529:                         }
12530:                     }
12531:                 }
12532:                 if ($changes{'uploadquota'}) {
12533:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
12534:                         foreach my $type (@types) {
12535:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
12536:                         }
12537:                     }
12538:                 }
12539:                 if ($changes{'canclone'}) {
12540:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
12541:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
12542:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
12543:                             if (@clonecodes) {
12544:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
12545:                             }
12546:                         }
12547:                     } else {
12548:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
12549:                     }
12550:                 }
12551:                 my $cachetime = 24*60*60;
12552:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12553:                 if (ref($lastactref) eq 'HASH') {
12554:                     $lastactref->{'domdefaults'} = 1;
12555:                 }
12556:             }
12557:             $resulttext = &mt('Changes made:').'<ul>';
12558:             foreach my $item (sort(keys(%changes))) {
12559:                 if ($item eq 'uselcmath') {
12560:                     if ($env{'form.'.$item} eq '1') {
12561:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
12562:                     } else {
12563:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
12564:                     }
12565:                 } elsif ($item eq 'usejsme') {
12566:                     if ($env{'form.'.$item} eq '1') {
12567:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
12568:                     } else {
12569:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
12570:                     }
12571:                 } elsif ($item eq 'texengine') {
12572:                     if ($defaultshash{'coursedefaults'}{'texengine'} ne '') {
12573:                         $resulttext .= '<li>'.&mt('Default method to display mathematics set to: "[_1]"',
12574:                                                   $texoptions{$defaultshash{'coursedefaults'}{'texengine'}}).'</li>';
12575:                     }
12576:                 } elsif ($item eq 'anonsurvey_threshold') {
12577:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
12578:                 } elsif ($item eq 'uploadquota') {
12579:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
12580:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
12581:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
12582:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
12583:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
12584: 
12585:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
12586:                                        '</ul>'.
12587:                                        '</li>';
12588:                     } else {
12589:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
12590:                     }
12591:                 } elsif ($item eq 'mysqltables') {
12592:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
12593:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
12594:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
12595:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
12596:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
12597:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
12598:                                        '</ul>'.
12599:                                        '</li>';
12600:                     } else {
12601:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
12602:                     }
12603:                 } elsif ($item eq 'postsubmit') {
12604:                     if ($domdefaults{'postsubmit'} eq 'off') {
12605:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
12606:                     } else {
12607:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
12608:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
12609:                             $resulttext .= &mt('durations:').'<ul>';
12610:                             foreach my $type (@types) {
12611:                                 $resulttext .= '<li>';
12612:                                 my $timeout;
12613:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
12614:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
12615:                                 }
12616:                                 my $display;
12617:                                 if ($timeout eq '0') {
12618:                                     $display = &mt('unlimited');
12619:                                 } elsif ($timeout eq '') {
12620:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
12621:                                 } else {
12622:                                     $display = &mt('[quant,_1,second]',$timeout);
12623:                                 }
12624:                                 if ($type eq 'community') {
12625:                                     $resulttext .= &mt('Communities');
12626:                                 } elsif ($type eq 'official') {
12627:                                     $resulttext .= &mt('Official courses');
12628:                                 } elsif ($type eq 'unofficial') {
12629:                                     $resulttext .= &mt('Unofficial courses');
12630:                                 } elsif ($type eq 'textbook') {
12631:                                     $resulttext .= &mt('Textbook courses');
12632:                                 }
12633:                                 $resulttext .= ' -- '.$display.'</li>';
12634:                             }
12635:                             $resulttext .= '</ul>';
12636:                         }
12637:                         $resulttext .= '</li>';
12638:                     }
12639:                 } elsif ($item eq 'coursecredits') {
12640:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
12641:                         if (($domdefaults{'officialcredits'} eq '') &&
12642:                             ($domdefaults{'unofficialcredits'} eq '') &&
12643:                             ($domdefaults{'textbookcredits'} eq '')) {
12644:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
12645:                         } else {
12646:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
12647:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
12648:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
12649:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
12650:                                            '</ul>'.
12651:                                            '</li>';
12652:                         }
12653:                     } else {
12654:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
12655:                     }
12656:                 } elsif ($item eq 'canclone') {
12657:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
12658:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
12659:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
12660:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
12661:                         }
12662:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
12663:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
12664:                     } else {
12665:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
12666:                     }
12667:                 }
12668:             }
12669:             $resulttext .= '</ul>';
12670:         } else {
12671:             $resulttext = &mt('No changes made to course defaults');
12672:         }
12673:     } else {
12674:         $resulttext = '<span class="LC_error">'.
12675:             &mt('An error occurred: [_1]',$putresult).'</span>';
12676:     }
12677:     return $resulttext;
12678: }
12679: 
12680: sub modify_selfenrollment {
12681:     my ($dom,$lastactref,%domconfig) = @_;
12682:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
12683:     my @types = ('official','unofficial','community','textbook');
12684:     my %titles = &tool_titles();
12685:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
12686:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
12687:     $ordered{'default'} = ['types','registered','approval','limit'];
12688: 
12689:     my (%roles,%shown,%toplevel);
12690:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
12691: 
12692:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
12693:         if ($domconfig{'selfenrollment'} eq '') {
12694:             $domconfig{'selfenrollment'} = {};
12695:         }
12696:     }
12697:     %toplevel = (
12698:                   admin      => 'Configuration Rights',
12699:                   default    => 'Default settings',
12700:                   validation => 'Validation of self-enrollment requests',
12701:                 );
12702:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
12703: 
12704:     if (ref($ordered{'admin'}) eq 'ARRAY') {
12705:         foreach my $item (@{$ordered{'admin'}}) {
12706:             foreach my $type (@types) {
12707:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
12708:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
12709:                 } else {
12710:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
12711:                 }
12712:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
12713:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
12714:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
12715:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
12716:                             push(@{$changes{'admin'}{$type}},$item);
12717:                         }
12718:                     } else {
12719:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
12720:                             push(@{$changes{'admin'}{$type}},$item);
12721:                         }
12722:                     }
12723:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
12724:                     push(@{$changes{'admin'}{$type}},$item);
12725:                 }
12726:             }
12727:         }
12728:     }
12729: 
12730:     foreach my $item (@{$ordered{'default'}}) {
12731:         foreach my $type (@types) {
12732:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
12733:             if ($item eq 'types') {
12734:                 unless (($value eq 'all') || ($value eq 'dom')) {
12735:                     $value = '';
12736:                 }
12737:             } elsif ($item eq 'registered') {
12738:                 unless ($value eq '1') {
12739:                     $value = 0;
12740:                 }
12741:             } elsif ($item eq 'approval') {
12742:                 unless ($value =~ /^[012]$/) {
12743:                     $value = 0;
12744:                 }
12745:             } else {
12746:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
12747:                     $value = 'none';
12748:                 }
12749:             }
12750:             $selfenrollhash{'default'}{$type}{$item} = $value;
12751:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
12752:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
12753:                     if ($selfenrollhash{'default'}{$type}{$item} ne
12754:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
12755:                          push(@{$changes{'default'}{$type}},$item);
12756:                     }
12757:                 } else {
12758:                     push(@{$changes{'default'}{$type}},$item);
12759:                 }
12760:             } else {
12761:                 push(@{$changes{'default'}{$type}},$item);
12762:             }
12763:             if ($item eq 'limit') {
12764:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
12765:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
12766:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
12767:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
12768:                     }
12769:                 } else {
12770:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
12771:                 }
12772:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
12773:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
12774:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
12775:                          push(@{$changes{'default'}{$type}},'cap');
12776:                     }
12777:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
12778:                     push(@{$changes{'default'}{$type}},'cap');
12779:                 }
12780:             }
12781:         }
12782:     }
12783: 
12784:     foreach my $item (@{$itemsref}) {
12785:         if ($item eq 'fields') {
12786:             my @changed;
12787:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
12788:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
12789:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
12790:             }
12791:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
12792:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
12793:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
12794:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
12795:                 } else {
12796:                     @changed = @{$selfenrollhash{'validation'}{$item}};
12797:                 }
12798:             } else {
12799:                 @changed = @{$selfenrollhash{'validation'}{$item}};
12800:             }
12801:             if (@changed) {
12802:                 if ($selfenrollhash{'validation'}{$item}) { 
12803:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
12804:                 } else {
12805:                     $changes{'validation'}{$item} = &mt('None');
12806:                 }
12807:             }
12808:         } else {
12809:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
12810:             if ($item eq 'markup') {
12811:                if ($env{'form.selfenroll_validation_'.$item}) {
12812:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
12813:                }
12814:             }
12815:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
12816:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
12817:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
12818:                 }
12819:             }
12820:         }
12821:     }
12822: 
12823:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
12824:                                              $dom);
12825:     if ($putresult eq 'ok') {
12826:         if (keys(%changes) > 0) {
12827:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12828:             $resulttext = &mt('Changes made:').'<ul>';
12829:             foreach my $key ('admin','default','validation') {
12830:                 if (ref($changes{$key}) eq 'HASH') {
12831:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
12832:                     if ($key eq 'validation') {
12833:                         foreach my $item (@{$itemsref}) {
12834:                             if (exists($changes{$key}{$item})) {
12835:                                 if ($item eq 'markup') {
12836:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
12837:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
12838:                                 } else {  
12839:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
12840:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
12841:                                 }
12842:                             }
12843:                         }
12844:                     } else {
12845:                         foreach my $type (@types) {
12846:                             if ($type eq 'community') {
12847:                                 $roles{'1'} = &mt('Community personnel');
12848:                             } else {
12849:                                 $roles{'1'} = &mt('Course personnel');
12850:                             }
12851:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
12852:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
12853:                                     if ($key eq 'admin') {
12854:                                         my @mgrdc = ();
12855:                                         if (ref($ordered{$key}) eq 'ARRAY') {
12856:                                             foreach my $item (@{$ordered{'admin'}}) {
12857:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
12858:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
12859:                                                         push(@mgrdc,$item);
12860:                                                     }
12861:                                                 }
12862:                                             }
12863:                                             if (@mgrdc) {
12864:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
12865:                                             } else {
12866:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
12867:                                             }
12868:                                         }
12869:                                     } else {
12870:                                         if (ref($ordered{$key}) eq 'ARRAY') {
12871:                                             foreach my $item (@{$ordered{$key}}) {
12872:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
12873:                                                     $domdefaults{$type.'selfenroll'.$item} =
12874:                                                         $selfenrollhash{$key}{$type}{$item};
12875:                                                 }
12876:                                             }
12877:                                         }
12878:                                     }
12879:                                 }
12880:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
12881:                                 foreach my $item (@{$ordered{$key}}) {
12882:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
12883:                                         $resulttext .= '<li>';
12884:                                         if ($key eq 'admin') {
12885:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
12886:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
12887:                                         } else {
12888:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
12889:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
12890:                                         }
12891:                                         $resulttext .= '</li>';
12892:                                     }
12893:                                 }
12894:                                 $resulttext .= '</ul></li>';
12895:                             }
12896:                         }
12897:                         $resulttext .= '</ul></li>'; 
12898:                     }
12899:                 }
12900:             }
12901:             if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
12902:                 my $cachetime = 24*60*60;
12903:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12904:                 if (ref($lastactref) eq 'HASH') {
12905:                     $lastactref->{'domdefaults'} = 1;
12906:                 }
12907:             }
12908:             $resulttext .= '</ul>';
12909:         } else {
12910:             $resulttext = &mt('No changes made to self-enrollment settings');
12911:         }
12912:     } else {
12913:         $resulttext = '<span class="LC_error">'.
12914:             &mt('An error occurred: [_1]',$putresult).'</span>';
12915:     }
12916:     return $resulttext;
12917: }
12918: 
12919: sub modify_usersessions {
12920:     my ($dom,$lastactref,%domconfig) = @_;
12921:     my @hostingtypes = ('version','excludedomain','includedomain');
12922:     my @offloadtypes = ('primary','default');
12923:     my %types = (
12924:                   remote => \@hostingtypes,
12925:                   hosted => \@hostingtypes,
12926:                   spares => \@offloadtypes,
12927:                 );
12928:     my @prefixes = ('remote','hosted','spares');
12929:     my @lcversions = &Apache::lonnet::all_loncaparevs();
12930:     my (%by_ip,%by_location,@intdoms);
12931:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
12932:     my @locations = sort(keys(%by_location));
12933:     my (%defaultshash,%changes);
12934:     foreach my $prefix (@prefixes) {
12935:         $defaultshash{'usersessions'}{$prefix} = {};
12936:     }
12937:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12938:     my $resulttext;
12939:     my %iphost = &Apache::lonnet::get_iphost();
12940:     foreach my $prefix (@prefixes) {
12941:         next if ($prefix eq 'spares');
12942:         foreach my $type (@{$types{$prefix}}) {
12943:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
12944:             if ($type eq 'version') {
12945:                 my $value = $env{'form.'.$prefix.'_'.$type};
12946:                 my $okvalue;
12947:                 if ($value ne '') {
12948:                     if (grep(/^\Q$value\E$/,@lcversions)) {
12949:                         $okvalue = $value;
12950:                     }
12951:                 }
12952:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
12953:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
12954:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
12955:                             if ($inuse == 0) {
12956:                                 $changes{$prefix}{$type} = 1;
12957:                             } else {
12958:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
12959:                                     $changes{$prefix}{$type} = 1;
12960:                                 }
12961:                                 if ($okvalue ne '') {
12962:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
12963:                                 } 
12964:                             }
12965:                         } else {
12966:                             if (($inuse == 1) && ($okvalue ne '')) {
12967:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
12968:                                 $changes{$prefix}{$type} = 1;
12969:                             }
12970:                         }
12971:                     } else {
12972:                         if (($inuse == 1) && ($okvalue ne '')) {
12973:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
12974:                             $changes{$prefix}{$type} = 1;
12975:                         }
12976:                     }
12977:                 } else {
12978:                     if (($inuse == 1) && ($okvalue ne '')) {
12979:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
12980:                         $changes{$prefix}{$type} = 1;
12981:                     }
12982:                 }
12983:             } else {
12984:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
12985:                 my @okvals;
12986:                 foreach my $val (@vals) {
12987:                     if ($val =~ /:/) {
12988:                         my @items = split(/:/,$val);
12989:                         foreach my $item (@items) {
12990:                             if (ref($by_location{$item}) eq 'ARRAY') {
12991:                                 push(@okvals,$item);
12992:                             }
12993:                         }
12994:                     } else {
12995:                         if (ref($by_location{$val}) eq 'ARRAY') {
12996:                             push(@okvals,$val);
12997:                         }
12998:                     }
12999:                 }
13000:                 @okvals = sort(@okvals);
13001:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
13002:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
13003:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
13004:                             if ($inuse == 0) {
13005:                                 $changes{$prefix}{$type} = 1; 
13006:                             } else {
13007:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13008:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
13009:                                 if (@changed > 0) {
13010:                                     $changes{$prefix}{$type} = 1;
13011:                                 }
13012:                             }
13013:                         } else {
13014:                             if ($inuse == 1) {
13015:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13016:                                 $changes{$prefix}{$type} = 1;
13017:                             }
13018:                         } 
13019:                     } else {
13020:                         if ($inuse == 1) {
13021:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13022:                             $changes{$prefix}{$type} = 1;
13023:                         }
13024:                     }
13025:                 } else {
13026:                     if ($inuse == 1) {
13027:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13028:                         $changes{$prefix}{$type} = 1;
13029:                     }
13030:                 }
13031:             }
13032:         }
13033:     }
13034: 
13035:     my @alldoms = &Apache::lonnet::all_domains();
13036:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
13037:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
13038:     my $savespares;
13039: 
13040:     foreach my $lonhost (sort(keys(%servers))) {
13041:         my $serverhomeID =
13042:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
13043:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
13044:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
13045:         my %spareschg;
13046:         foreach my $type (@{$types{'spares'}}) {
13047:             my @okspares;
13048:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
13049:             foreach my $server (@checked) {
13050:                 if (&Apache::lonnet::hostname($server) ne '') {
13051:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
13052:                         unless (grep(/^\Q$server\E$/,@okspares)) {
13053:                             push(@okspares,$server);
13054:                         }
13055:                     }
13056:                 }
13057:             }
13058:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
13059:             my $newspare;
13060:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
13061:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
13062:                     $newspare = $new;
13063:                 }
13064:             }
13065:             my @spares;
13066:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
13067:                 @spares = sort(@okspares,$newspare);
13068:             } else {
13069:                 @spares = sort(@okspares);
13070:             }
13071:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
13072:             if (ref($spareid{$lonhost}) eq 'HASH') {
13073:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
13074:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
13075:                     if (@diffs > 0) {
13076:                         $spareschg{$type} = 1;
13077:                     }
13078:                 }
13079:             }
13080:         }
13081:         if (keys(%spareschg) > 0) {
13082:             $changes{'spares'}{$lonhost} = \%spareschg;
13083:         }
13084:     }
13085:     $defaultshash{'usersessions'}{'offloadnow'} = {};
13086:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
13087:     my @okoffload;
13088:     if (@offloadnow) {
13089:         foreach my $server (@offloadnow) {
13090:             if (&Apache::lonnet::hostname($server) ne '') {
13091:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
13092:                     push(@okoffload,$server);
13093:                 }
13094:             }
13095:         }
13096:         if (@okoffload) {
13097:             foreach my $lonhost (@okoffload) {
13098:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
13099:             }
13100:         }
13101:     }
13102:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
13103:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
13104:             if (ref($changes{'spares'}) eq 'HASH') {
13105:                 if (keys(%{$changes{'spares'}}) > 0) {
13106:                     $savespares = 1;
13107:                 }
13108:             }
13109:         } else {
13110:             $savespares = 1;
13111:         }
13112:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
13113:             foreach my $lonhost (keys(%{$domconfig{'usersessions'}{'offloadnow'}})) {
13114:                 unless ($defaultshash{'usersessions'}{'offloadnow'}{$lonhost}) {
13115:                     $changes{'offloadnow'} = 1;
13116:                     last;
13117:                 }
13118:             }
13119:             unless ($changes{'offloadnow'}) {
13120:                 foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{'offloadnow'}})) {
13121:                     unless ($domconfig{'usersessions'}{'offloadnow'}{$lonhost}) {
13122:                         $changes{'offloadnow'} = 1;
13123:                         last;
13124:                     }
13125:                 }
13126:             }
13127:         } elsif (@okoffload) {
13128:             $changes{'offloadnow'} = 1;
13129:         }
13130:     } elsif (@okoffload) {
13131:         $changes{'offloadnow'} = 1;
13132:     }
13133:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
13134:     if ((keys(%changes) > 0) || ($savespares)) {
13135:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
13136:                                                  $dom);
13137:         if ($putresult eq 'ok') {
13138:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
13139:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
13140:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
13141:                 }
13142:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
13143:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
13144:                 }
13145:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
13146:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
13147:                 }
13148:             }
13149:             my $cachetime = 24*60*60;
13150:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13151:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
13152:             if (ref($lastactref) eq 'HASH') {
13153:                 $lastactref->{'domdefaults'} = 1;
13154:                 $lastactref->{'usersessions'} = 1;
13155:             }
13156:             if (keys(%changes) > 0) {
13157:                 my %lt = &usersession_titles();
13158:                 $resulttext = &mt('Changes made:').'<ul>';
13159:                 foreach my $prefix (@prefixes) {
13160:                     if (ref($changes{$prefix}) eq 'HASH') {
13161:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
13162:                         if ($prefix eq 'spares') {
13163:                             if (ref($changes{$prefix}) eq 'HASH') {
13164:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
13165:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
13166:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
13167:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
13168:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
13169:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
13170:                                         foreach my $type (@{$types{$prefix}}) {
13171:                                             if ($changes{$prefix}{$lonhost}{$type}) {
13172:                                                 my $offloadto = &mt('None');
13173:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
13174:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
13175:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
13176:                                                     }
13177:                                                 }
13178:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
13179:                                             }
13180:                                         }
13181:                                     }
13182:                                     $resulttext .= '</li>';
13183:                                 }
13184:                             }
13185:                         } else {
13186:                             foreach my $type (@{$types{$prefix}}) {
13187:                                 if (defined($changes{$prefix}{$type})) {
13188:                                     my $newvalue;
13189:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
13190:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
13191:                                             if ($type eq 'version') {
13192:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
13193:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
13194:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
13195:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
13196:                                                 }
13197:                                             }
13198:                                         }
13199:                                     }
13200:                                     if ($newvalue eq '') {
13201:                                         if ($type eq 'version') {
13202:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
13203:                                         } else {
13204:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
13205:                                         }
13206:                                     } else {
13207:                                         if ($type eq 'version') {
13208:                                             $newvalue .= ' '.&mt('(or later)'); 
13209:                                         }
13210:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
13211:                                     }
13212:                                 }
13213:                             }
13214:                         }
13215:                         $resulttext .= '</ul>';
13216:                     }
13217:                 }
13218:                 if ($changes{'offloadnow'}) {
13219:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
13220:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
13221:                             $resulttext .= '<li>'.&mt('Switch active users on next access, for server(s):').'<ul>';
13222:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
13223:                                 $resulttext .= '<li>'.$lonhost.'</li>';
13224:                             }
13225:                             $resulttext .= '</ul>';
13226:                         } else {
13227:                             $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.');
13228:                         }
13229:                     } else {
13230:                         $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.').'</li>';
13231:                     }
13232:                 }
13233:                 $resulttext .= '</ul>';
13234:             } else {
13235:                 $resulttext = $nochgmsg;
13236:             }
13237:         } else {
13238:             $resulttext = '<span class="LC_error">'.
13239:                           &mt('An error occurred: [_1]',$putresult).'</span>';
13240:         }
13241:     } else {
13242:         $resulttext = $nochgmsg;
13243:     }
13244:     return $resulttext;
13245: }
13246: 
13247: sub modify_loadbalancing {
13248:     my ($dom,%domconfig) = @_;
13249:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
13250:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
13251:     my ($othertitle,$usertypes,$types) =
13252:         &Apache::loncommon::sorted_inst_types($dom);
13253:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
13254:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
13255:     my @sparestypes = ('primary','default');
13256:     my %typetitles = &sparestype_titles();
13257:     my $resulttext;
13258:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
13259:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
13260:         %existing = %{$domconfig{'loadbalancing'}};
13261:     }
13262:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
13263:                               \%currtargets,\%currrules,\%currcookies);
13264:     my ($saveloadbalancing,%defaultshash,%changes);
13265:     my ($alltypes,$othertypes,$titles) =
13266:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
13267:     my %ruletitles = &offloadtype_text();
13268:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
13269:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
13270:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
13271:         if ($balancer eq '') {
13272:             next;
13273:         }
13274:         if (!exists($servers{$balancer})) {
13275:             if (exists($currbalancer{$balancer})) {
13276:                 push(@{$changes{'delete'}},$balancer);
13277:             }
13278:             next;
13279:         }
13280:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
13281:             push(@{$changes{'delete'}},$balancer);
13282:             next;
13283:         }
13284:         if (!exists($currbalancer{$balancer})) {
13285:             push(@{$changes{'add'}},$balancer);
13286:         }
13287:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
13288:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
13289:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
13290:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
13291:             $saveloadbalancing = 1;
13292:         }
13293:         foreach my $sparetype (@sparestypes) {
13294:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
13295:             my @offloadto;
13296:             foreach my $target (@targets) {
13297:                 if (($servers{$target}) && ($target ne $balancer)) {
13298:                     if ($sparetype eq 'default') {
13299:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
13300:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
13301:                         }
13302:                     }
13303:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
13304:                         push(@offloadto,$target);
13305:                     }
13306:                 }
13307:             }
13308:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
13309:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
13310:                     push(@offloadto,$balancer);
13311:                 }
13312:             }
13313:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
13314:         }
13315:         if ($env{'form.loadbalancing_cookie_'.$i}) {
13316:             $defaultshash{'loadbalancing'}{$balancer}{'cookie'} = 1;
13317:             if (exists($currbalancer{$balancer})) {
13318:                 unless ($currcookies{$balancer}) {
13319:                     $changes{'curr'}{$balancer}{'cookie'} = 1;
13320:                 }
13321:             }
13322:         } elsif (exists($currbalancer{$balancer})) {
13323:             if ($currcookies{$balancer}) {
13324:                 $changes{'curr'}{$balancer}{'cookie'} = 1;
13325:             }
13326:         }
13327:         if (ref($currtargets{$balancer}) eq 'HASH') {
13328:             foreach my $sparetype (@sparestypes) {
13329:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
13330:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
13331:                     if (@targetdiffs > 0) {
13332:                         $changes{'curr'}{$balancer}{'targets'} = 1;
13333:                     }
13334:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
13335:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
13336:                         $changes{'curr'}{$balancer}{'targets'} = 1;
13337:                     }
13338:                 }
13339:             }
13340:         } else {
13341:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
13342:                 foreach my $sparetype (@sparestypes) {
13343:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
13344:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
13345:                             $changes{'curr'}{$balancer}{'targets'} = 1;
13346:                         }
13347:                     }
13348:                 }
13349:             }
13350:         }
13351:         my $ishomedom;
13352:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
13353:             $ishomedom = 1;
13354:         }
13355:         if (ref($alltypes) eq 'ARRAY') {
13356:             foreach my $type (@{$alltypes}) {
13357:                 my $rule;
13358:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
13359:                          (!$ishomedom)) {
13360:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
13361:                 }
13362:                 if ($rule eq 'specific') {
13363:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
13364:                     if (exists($servers{$specifiedhost})) {
13365:                         $rule = $specifiedhost;
13366:                     }
13367:                 }
13368:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
13369:                 if (ref($currrules{$balancer}) eq 'HASH') {
13370:                     if ($rule ne $currrules{$balancer}{$type}) {
13371:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
13372:                     }
13373:                 } elsif ($rule ne '') {
13374:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
13375:                 }
13376:             }
13377:         }
13378:     }
13379:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
13380:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
13381:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
13382:             $defaultshash{'loadbalancing'} = {};
13383:         }
13384:         my $putresult = &Apache::lonnet::put_dom('configuration',
13385:                                                  \%defaultshash,$dom);
13386:         if ($putresult eq 'ok') {
13387:             if (keys(%changes) > 0) {
13388:                 my %toupdate;
13389:                 if (ref($changes{'delete'}) eq 'ARRAY') {
13390:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
13391:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
13392:                         $toupdate{$balancer} = 1;
13393:                     }
13394:                 }
13395:                 if (ref($changes{'add'}) eq 'ARRAY') {
13396:                     foreach my $balancer (sort(@{$changes{'add'}})) {
13397:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
13398:                         $toupdate{$balancer} = 1;
13399:                     }
13400:                 }
13401:                 if (ref($changes{'curr'}) eq 'HASH') {
13402:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
13403:                         $toupdate{$balancer} = 1;
13404:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
13405:                             if ($changes{'curr'}{$balancer}{'targets'}) {
13406:                                 my %offloadstr;
13407:                                 foreach my $sparetype (@sparestypes) {
13408:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
13409:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
13410:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
13411:                                         }
13412:                                     }
13413:                                 }
13414:                                 if (keys(%offloadstr) == 0) {
13415:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
13416:                                 } else {
13417:                                     my $showoffload;
13418:                                     foreach my $sparetype (@sparestypes) {
13419:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
13420:                                         if (defined($offloadstr{$sparetype})) {
13421:                                             $showoffload .= $offloadstr{$sparetype};
13422:                                         } else {
13423:                                             $showoffload .= &mt('None');
13424:                                         }
13425:                                         $showoffload .= ('&nbsp;'x3);
13426:                                     }
13427:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
13428:                                 }
13429:                             }
13430:                         }
13431:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
13432:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
13433:                                 foreach my $type (@{$alltypes}) {
13434:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
13435:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
13436:                                         my $balancetext;
13437:                                         if ($rule eq '') {
13438:                                             $balancetext =  $ruletitles{'default'};
13439:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
13440:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
13441:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
13442:                                                 foreach my $sparetype (@sparestypes) {
13443:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
13444:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
13445:                                                     }
13446:                                                 }
13447:                                                 foreach my $item (@{$alltypes}) {
13448:                                                     next if ($item =~  /^_LC_ipchange/);
13449:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
13450:                                                     if ($hasrule eq 'homeserver') {
13451:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
13452:                                                     } else {
13453:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
13454:                                                             if ($servers{$hasrule}) {
13455:                                                                 $toupdate{$hasrule} = 1;
13456:                                                             }
13457:                                                         }
13458:                                                     }
13459:                                                 }
13460:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
13461:                                                     $balancetext =  $ruletitles{$rule};
13462:                                                 } else {
13463:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
13464:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
13465:                                                     if ($receiver) {
13466:                                                         $toupdate{$receiver};
13467:                                                     }
13468:                                                 }
13469:                                             } else {
13470:                                                 $balancetext =  $ruletitles{$rule};
13471:                                             }
13472:                                         } else {
13473:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
13474:                                         }
13475:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
13476:                                     }
13477:                                 }
13478:                             }
13479:                         }
13480:                         if ($changes{'curr'}{$balancer}{'cookie'}) {
13481:                             $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use enabled',
13482:                                                       $balancer).'</li>';
13483:                         }
13484:                         if (keys(%toupdate)) {
13485:                             my %thismachine;
13486:                             my $updatedhere;
13487:                             my $cachetime = 60*60*24;
13488:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
13489:                             foreach my $lonhost (keys(%toupdate)) {
13490:                                 if ($thismachine{$lonhost}) {
13491:                                     unless ($updatedhere) {
13492:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
13493:                                                                       $defaultshash{'loadbalancing'},
13494:                                                                       $cachetime);
13495:                                         $updatedhere = 1;
13496:                                     }
13497:                                 } else {
13498:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
13499:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
13500:                                 }
13501:                             }
13502:                         }
13503:                     }
13504:                 }
13505:                 if ($resulttext ne '') {
13506:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
13507:                 } else {
13508:                     $resulttext = $nochgmsg;
13509:                 }
13510:             } else {
13511:                 $resulttext = $nochgmsg;
13512:             }
13513:         } else {
13514:             $resulttext = '<span class="LC_error">'.
13515:                           &mt('An error occurred: [_1]',$putresult).'</span>';
13516:         }
13517:     } else {
13518:         $resulttext = $nochgmsg;
13519:     }
13520:     return $resulttext;
13521: }
13522: 
13523: sub recurse_check {
13524:     my ($chkcats,$categories,$depth,$name) = @_;
13525:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
13526:         my $chg = 0;
13527:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
13528:             my $category = $chkcats->[$depth]{$name}[$j];
13529:             my $item;
13530:             if ($category eq '') {
13531:                 $chg ++;
13532:             } else {
13533:                 my $deeper = $depth + 1;
13534:                 $item = &escape($category).':'.&escape($name).':'.$depth;
13535:                 if ($chg) {
13536:                     $categories->{$item} -= $chg;
13537:                 }
13538:                 &recurse_check($chkcats,$categories,$deeper,$category);
13539:                 $deeper --;
13540:             }
13541:         }
13542:     }
13543:     return;
13544: }
13545: 
13546: sub recurse_cat_deletes {
13547:     my ($item,$coursecategories,$deletions) = @_;
13548:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
13549:     my $subdepth = $depth + 1;
13550:     if (ref($coursecategories) eq 'HASH') {
13551:         foreach my $subitem (keys(%{$coursecategories})) {
13552:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
13553:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
13554:                 delete($coursecategories->{$subitem});
13555:                 $deletions->{$subitem} = 1;
13556:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
13557:             }
13558:         }
13559:     }
13560:     return;
13561: }
13562: 
13563: sub active_dc_picker {
13564:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
13565:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
13566:     my @domcoord = keys(%domcoords);
13567:     if (keys(%currhash)) {
13568:         foreach my $dc (keys(%currhash)) {
13569:             unless (exists($domcoords{$dc})) {
13570:                 push(@domcoord,$dc);
13571:             }
13572:         }
13573:     }
13574:     @domcoord = sort(@domcoord);
13575:     my $numdcs = scalar(@domcoord);
13576:     my $rows = 0;
13577:     my $table;
13578:     if ($numdcs > 1) {
13579:         $table = '<table>';
13580:         for (my $i=0; $i<@domcoord; $i++) {
13581:             my $rem = $i%($numinrow);
13582:             if ($rem == 0) {
13583:                 if ($i > 0) {
13584:                     $table .= '</tr>';
13585:                 }
13586:                 $table .= '<tr>';
13587:                 $rows ++;
13588:             }
13589:             my $check = '';
13590:             if ($inputtype eq 'radio') {
13591:                 if (keys(%currhash) == 0) {
13592:                     if (!$i) {
13593:                         $check = ' checked="checked"';
13594:                     }
13595:                 } elsif (exists($currhash{$domcoord[$i]})) {
13596:                     $check = ' checked="checked"';
13597:                 }
13598:             } else {
13599:                 if (exists($currhash{$domcoord[$i]})) {
13600:                     $check = ' checked="checked"';
13601:                 }
13602:             }
13603:             if ($i == @domcoord - 1) {
13604:                 my $colsleft = $numinrow - $rem;
13605:                 if ($colsleft > 1) {
13606:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
13607:                 } else {
13608:                     $table .= '<td class="LC_left_item">';
13609:                 }
13610:             } else {
13611:                 $table .= '<td class="LC_left_item">';
13612:             }
13613:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
13614:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
13615:             $table .= '<span class="LC_nobreak"><label>'.
13616:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
13617:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
13618:             if ($user ne $dcname.':'.$dcdom) {
13619:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
13620:             }
13621:             $table .= '</label></span></td>';
13622:         }
13623:         $table .= '</tr></table>';
13624:     } elsif ($numdcs == 1) {
13625:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
13626:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
13627:         if ($inputtype eq 'radio') {
13628:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
13629:             if ($user ne $dcname.':'.$dcdom) {
13630:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
13631:             }
13632:         } else {
13633:             my $check;
13634:             if (exists($currhash{$domcoord[0]})) {
13635:                 $check = ' checked="checked"';
13636:             }
13637:             $table = '<span class="LC_nobreak"><label>'.
13638:                      '<input type="checkbox" name="'.$name.'" '.
13639:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
13640:             if ($user ne $dcname.':'.$dcdom) {
13641:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
13642:             }
13643:             $table .= '</label></span>';
13644:             $rows ++;
13645:         }
13646:     }
13647:     return ($numdcs,$table,$rows);
13648: }
13649: 
13650: sub usersession_titles {
13651:     return &Apache::lonlocal::texthash(
13652:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
13653:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
13654:                spares => 'Servers offloaded to, when busy',
13655:                version => 'LON-CAPA version requirement',
13656:                excludedomain => 'Allow all, but exclude specific domains',
13657:                includedomain => 'Deny all, but include specific domains',
13658:                primary => 'Primary (checked first)',
13659:                default => 'Default',
13660:            );
13661: }
13662: 
13663: sub id_for_thisdom {
13664:     my (%servers) = @_;
13665:     my %altids;
13666:     foreach my $server (keys(%servers)) {
13667:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
13668:         if ($serverhome ne $server) {
13669:             $altids{$serverhome} = $server;
13670:         }
13671:     }
13672:     return %altids;
13673: }
13674: 
13675: sub count_servers {
13676:     my ($currbalancer,%servers) = @_;
13677:     my (@spares,$numspares);
13678:     foreach my $lonhost (sort(keys(%servers))) {
13679:         next if ($currbalancer eq $lonhost);
13680:         push(@spares,$lonhost);
13681:     }
13682:     if ($currbalancer) {
13683:         $numspares = scalar(@spares);
13684:     } else {
13685:         $numspares = scalar(@spares) - 1;
13686:     }
13687:     return ($numspares,@spares);
13688: }
13689: 
13690: sub lonbalance_targets_js {
13691:     my ($dom,$types,$servers,$settings) = @_;
13692:     my $select = &mt('Select');
13693:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
13694:     if (ref($servers) eq 'HASH') {
13695:         $alltargets = join("','",sort(keys(%{$servers})));
13696:         my @homedoms;
13697:         foreach my $server (sort(keys(%{$servers}))) {
13698:             if (&Apache::lonnet::host_domain($server) eq $dom) {
13699:                 push(@homedoms,'1');
13700:             } else {
13701:                 push(@homedoms,'0');
13702:             }
13703:         }
13704:         $allishome = join("','",@homedoms);
13705:     }
13706:     if (ref($types) eq 'ARRAY') {
13707:         if (@{$types} > 0) {
13708:             @alltypes = @{$types};
13709:         }
13710:     }
13711:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
13712:     $allinsttypes = join("','",@alltypes);
13713:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
13714:     if (ref($settings) eq 'HASH') {
13715:         %existing = %{$settings};
13716:     }
13717:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
13718:                               \%currtargets,\%currrules,\%currcookies);
13719:     my $balancers = join("','",sort(keys(%currbalancer)));
13720:     return <<"END";
13721: 
13722: <script type="text/javascript">
13723: // <![CDATA[
13724: 
13725: currBalancers = new Array('$balancers');
13726: 
13727: function toggleTargets(balnum) {
13728:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
13729:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
13730:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
13731:     var prevbalancer = prevhostitem.value;
13732:     var baltotal = document.getElementById('loadbalancing_total').value;
13733:     prevhostitem.value = balancer;
13734:     if (prevbalancer != '') {
13735:         var prevIdx = currBalancers.indexOf(prevbalancer);
13736:         if (prevIdx != -1) {
13737:             currBalancers.splice(prevIdx,1);
13738:         }
13739:     }
13740:     if (balancer == '') {
13741:         hideSpares(balnum);
13742:     } else {
13743:         var currIdx = currBalancers.indexOf(balancer);
13744:         if (currIdx == -1) {
13745:             currBalancers.push(balancer);
13746:         }
13747:         var homedoms = new Array('$allishome');
13748:         var ishomedom = homedoms[lonhostitem.selectedIndex];
13749:         showSpares(balancer,ishomedom,balnum);
13750:     }
13751:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
13752:     return;
13753: }
13754: 
13755: function showSpares(balancer,ishomedom,balnum) {
13756:     var alltargets = new Array('$alltargets');
13757:     var insttypes = new Array('$allinsttypes');
13758:     var offloadtypes = new Array('primary','default');
13759: 
13760:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
13761:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
13762:  
13763:     for (var i=0; i<offloadtypes.length; i++) {
13764:         var count = 0;
13765:         for (var j=0; j<alltargets.length; j++) {
13766:             if (alltargets[j] != balancer) {
13767:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
13768:                 item.value = alltargets[j];
13769:                 item.style.textAlign='left';
13770:                 item.style.textFace='normal';
13771:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
13772:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
13773:                     item.disabled = '';
13774:                 } else {
13775:                     item.disabled = 'disabled';
13776:                     item.checked = false;
13777:                 }
13778:                 count ++;
13779:             }
13780:         }
13781:     }
13782:     for (var k=0; k<insttypes.length; k++) {
13783:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
13784:             if (ishomedom == 1) {
13785:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
13786:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
13787:             } else {
13788:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
13789:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
13790:             }
13791:         } else {
13792:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
13793:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
13794:         }
13795:         if ((insttypes[k] != '_LC_external') && 
13796:             ((insttypes[k] != '_LC_internetdom') ||
13797:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
13798:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
13799:             item.options.length = 0;
13800:             item.options[0] = new Option("","",true,true);
13801:             var idx = 0;
13802:             for (var m=0; m<alltargets.length; m++) {
13803:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
13804:                     idx ++;
13805:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
13806:                 }
13807:             }
13808:         }
13809:     }
13810:     return;
13811: }
13812: 
13813: function hideSpares(balnum) {
13814:     var alltargets = new Array('$alltargets');
13815:     var insttypes = new Array('$allinsttypes');
13816:     var offloadtypes = new Array('primary','default');
13817: 
13818:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
13819:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
13820: 
13821:     var total = alltargets.length - 1;
13822:     for (var i=0; i<offloadtypes; i++) {
13823:         for (var j=0; j<total; j++) {
13824:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
13825:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
13826:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
13827:         }
13828:     }
13829:     for (var k=0; k<insttypes.length; k++) {
13830:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
13831:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
13832:         if (insttypes[k] != '_LC_external') {
13833:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
13834:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
13835:         }
13836:     }
13837:     return;
13838: }
13839: 
13840: function checkOffloads(item,balnum,type) {
13841:     var alltargets = new Array('$alltargets');
13842:     var offloadtypes = new Array('primary','default');
13843:     if (item.checked) {
13844:         var total = alltargets.length - 1;
13845:         var other;
13846:         if (type == offloadtypes[0]) {
13847:             other = offloadtypes[1];
13848:         } else {
13849:             other = offloadtypes[0];
13850:         }
13851:         for (var i=0; i<total; i++) {
13852:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
13853:             if (server == item.value) {
13854:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
13855:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
13856:                 }
13857:             }
13858:         }
13859:     }
13860:     return;
13861: }
13862: 
13863: function singleServerToggle(balnum,type) {
13864:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
13865:     if (offloadtoSelIdx == 0) {
13866:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
13867:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
13868: 
13869:     } else {
13870:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
13871:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
13872:     }
13873:     return;
13874: }
13875: 
13876: function balanceruleChange(formname,balnum,type) {
13877:     if (type == '_LC_external') {
13878:         return;
13879:     }
13880:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
13881:     for (var i=0; i<typesRules.length; i++) {
13882:         if (formname.elements[typesRules[i]].checked) {
13883:             if (formname.elements[typesRules[i]].value != 'specific') {
13884:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
13885:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
13886:             } else {
13887:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
13888:             }
13889:         }
13890:     }
13891:     return;
13892: }
13893: 
13894: function balancerDeleteChange(balnum) {
13895:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
13896:     var baltotal = document.getElementById('loadbalancing_total').value;
13897:     var addtarget;
13898:     var removetarget;
13899:     var action = 'delete';
13900:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
13901:         var lonhost = hostitem.value;
13902:         var currIdx = currBalancers.indexOf(lonhost);
13903:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
13904:             if (currIdx != -1) {
13905:                 currBalancers.splice(currIdx,1);
13906:             }
13907:             addtarget = lonhost;
13908:         } else {
13909:             if (currIdx == -1) {
13910:                 currBalancers.push(lonhost);
13911:             }
13912:             removetarget = lonhost;
13913:             action = 'undelete';
13914:         }
13915:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
13916:     }
13917:     return;
13918: }
13919: 
13920: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
13921:     if (baltotal > 1) {
13922:         var offloadtypes = new Array('primary','default');
13923:         var alltargets = new Array('$alltargets');
13924:         var insttypes = new Array('$allinsttypes');
13925:         for (var i=0; i<baltotal; i++) {
13926:             if (i != balnum) {
13927:                 for (var j=0; j<offloadtypes.length; j++) {
13928:                     var total = alltargets.length - 1;
13929:                     for (var k=0; k<total; k++) {
13930:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
13931:                         var server = serveritem.value;
13932:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
13933:                             if (server == addtarget) {
13934:                                 serveritem.disabled = '';
13935:                             }
13936:                         }
13937:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
13938:                             if (server == removetarget) {
13939:                                 serveritem.disabled = 'disabled';
13940:                                 serveritem.checked = false;
13941:                             }
13942:                         }
13943:                     }
13944:                 }
13945:                 for (var j=0; j<insttypes.length; j++) {
13946:                     if (insttypes[j] != '_LC_external') {
13947:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
13948:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
13949:                             var currSel = singleserver.selectedIndex;
13950:                             var currVal = singleserver.options[currSel].value;
13951:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
13952:                                 var numoptions = singleserver.options.length;
13953:                                 var needsnew = 1;
13954:                                 for (var k=0; k<numoptions; k++) {
13955:                                     if (singleserver.options[k] == addtarget) {
13956:                                         needsnew = 0;
13957:                                         break;
13958:                                     }
13959:                                 }
13960:                                 if (needsnew == 1) {
13961:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
13962:                                 }
13963:                             }
13964:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
13965:                                 singleserver.options.length = 0;
13966:                                 if ((currVal) && (currVal != removetarget)) {
13967:                                     singleserver.options[0] = new Option("","",false,false);
13968:                                 } else {
13969:                                     singleserver.options[0] = new Option("","",true,true);
13970:                                 }
13971:                                 var idx = 0;
13972:                                 for (var m=0; m<alltargets.length; m++) {
13973:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
13974:                                         idx ++;
13975:                                         if (currVal == alltargets[m]) {
13976:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
13977:                                         } else {
13978:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
13979:                                         }
13980:                                     }
13981:                                 }
13982:                             }
13983:                         }
13984:                     }
13985:                 }
13986:             }
13987:         }
13988:     }
13989:     return;
13990: }
13991: 
13992: // ]]>
13993: </script>
13994: 
13995: END
13996: }
13997: 
13998: sub new_spares_js {
13999:     my @sparestypes = ('primary','default');
14000:     my $types = join("','",@sparestypes);
14001:     my $select = &mt('Select');
14002:     return <<"END";
14003: 
14004: <script type="text/javascript">
14005: // <![CDATA[
14006: 
14007: function updateNewSpares(formname,lonhost) {
14008:     var types = new Array('$types');
14009:     var include = new Array();
14010:     var exclude = new Array();
14011:     for (var i=0; i<types.length; i++) {
14012:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
14013:         for (var j=0; j<spareboxes.length; j++) {
14014:             if (formname.elements[spareboxes[j]].checked) {
14015:                 exclude.push(formname.elements[spareboxes[j]].value);
14016:             } else {
14017:                 include.push(formname.elements[spareboxes[j]].value);
14018:             }
14019:         }
14020:     }
14021:     for (var i=0; i<types.length; i++) {
14022:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
14023:         var selIdx = newSpare.selectedIndex;
14024:         var currnew = newSpare.options[selIdx].value;
14025:         var okSpares = new Array();
14026:         for (var j=0; j<newSpare.options.length; j++) {
14027:             var possible = newSpare.options[j].value;
14028:             if (possible != '') {
14029:                 if (exclude.indexOf(possible) == -1) {
14030:                     okSpares.push(possible);
14031:                 } else {
14032:                     if (currnew == possible) {
14033:                         selIdx = 0;
14034:                     }
14035:                 }
14036:             }
14037:         }
14038:         for (var k=0; k<include.length; k++) {
14039:             if (okSpares.indexOf(include[k]) == -1) {
14040:                 okSpares.push(include[k]);
14041:             }
14042:         }
14043:         okSpares.sort();
14044:         newSpare.options.length = 0;
14045:         if (selIdx == 0) {
14046:             newSpare.options[0] = new Option("$select","",true,true);
14047:         } else {
14048:             newSpare.options[0] = new Option("$select","",false,false);
14049:         }
14050:         for (var m=0; m<okSpares.length; m++) {
14051:             var idx = m+1;
14052:             var selThis = 0;
14053:             if (selIdx != 0) {
14054:                 if (okSpares[m] == currnew) {
14055:                     selThis = 1;
14056:                 }
14057:             }
14058:             if (selThis == 1) {
14059:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
14060:             } else {
14061:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
14062:             }
14063:         }
14064:     }
14065:     return;
14066: }
14067: 
14068: function checkNewSpares(lonhost,type) {
14069:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
14070:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
14071:     if (chosen != '') { 
14072:         var othertype;
14073:         var othernewSpare;
14074:         if (type == 'primary') {
14075:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
14076:         }
14077:         if (type == 'default') {
14078:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
14079:         }
14080:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
14081:             othernewSpare.selectedIndex = 0;
14082:         }
14083:     }
14084:     return;
14085: }
14086: 
14087: // ]]>
14088: </script>
14089: 
14090: END
14091: 
14092: }
14093: 
14094: sub common_domprefs_js {
14095:     return <<"END";
14096: 
14097: <script type="text/javascript">
14098: // <![CDATA[
14099: 
14100: function getIndicesByName(formname,item) {
14101:     var group = new Array();
14102:     for (var i=0;i<formname.elements.length;i++) {
14103:         if (formname.elements[i].name == item) {
14104:             group.push(formname.elements[i].id);
14105:         }
14106:     }
14107:     return group;
14108: }
14109: 
14110: // ]]>
14111: </script>
14112: 
14113: END
14114: 
14115: }
14116: 
14117: sub recaptcha_js {
14118:     my %lt = &captcha_phrases();
14119:     return <<"END";
14120: 
14121: <script type="text/javascript">
14122: // <![CDATA[
14123: 
14124: function updateCaptcha(caller,context) {
14125:     var privitem;
14126:     var pubitem;
14127:     var privtext;
14128:     var pubtext;
14129:     var versionitem;
14130:     var versiontext;
14131:     if (document.getElementById(context+'_recaptchapub')) {
14132:         pubitem = document.getElementById(context+'_recaptchapub');
14133:     } else {
14134:         return;
14135:     }
14136:     if (document.getElementById(context+'_recaptchapriv')) {
14137:         privitem = document.getElementById(context+'_recaptchapriv');
14138:     } else {
14139:         return;
14140:     }
14141:     if (document.getElementById(context+'_recaptchapubtxt')) {
14142:         pubtext = document.getElementById(context+'_recaptchapubtxt');
14143:     } else {
14144:         return;
14145:     }
14146:     if (document.getElementById(context+'_recaptchaprivtxt')) {
14147:         privtext = document.getElementById(context+'_recaptchaprivtxt');
14148:     } else {
14149:         return;
14150:     }
14151:     if (document.getElementById(context+'_recaptchaversion')) {
14152:         versionitem = document.getElementById(context+'_recaptchaversion');
14153:     } else {
14154:         return;
14155:     }
14156:     if (document.getElementById(context+'_recaptchavertxt')) {
14157:         versiontext = document.getElementById(context+'_recaptchavertxt');
14158:     } else {
14159:         return;
14160:     }
14161:     if (caller.checked) {
14162:         if (caller.value == 'recaptcha') {
14163:             pubitem.type = 'text';
14164:             privitem.type = 'text';
14165:             pubitem.size = '40';
14166:             privitem.size = '40';
14167:             pubtext.innerHTML = "$lt{'pub'}";
14168:             privtext.innerHTML = "$lt{'priv'}";
14169:             versionitem.type = 'text';
14170:             versionitem.size = '3';
14171:             versiontext.innerHTML = "$lt{'ver'}";
14172:         } else {
14173:             pubitem.type = 'hidden';
14174:             privitem.type = 'hidden';
14175:             versionitem.type = 'hidden';
14176:             pubtext.innerHTML = '';
14177:             privtext.innerHTML = '';
14178:             versiontext.innerHTML = '';
14179:         }
14180:     }
14181:     return;
14182: }
14183: 
14184: // ]]>
14185: </script>
14186: 
14187: END
14188: 
14189: }
14190: 
14191: sub toggle_display_js {
14192:     return <<"END";
14193: 
14194: <script type="text/javascript">
14195: // <![CDATA[
14196: 
14197: function toggleDisplay(domForm,caller) {
14198:     if (document.getElementById(caller)) {
14199:         var divitem = document.getElementById(caller);
14200:         var optionsElement = domForm.coursecredits;
14201:         var checkval = 1;
14202:         var dispval = 'block';
14203:         var selfcreateRegExp = /^cancreate_emailverified/;
14204:         if (caller == 'emailoptions') {
14205:             optionsElement = domForm.cancreate_email; 
14206:         }
14207:         if (caller == 'studentsubmission') {
14208:             optionsElement = domForm.postsubmit;
14209:         }
14210:         if (caller == 'cloneinstcode') {
14211:             optionsElement = domForm.canclone;
14212:             checkval = 'instcode';
14213:         }
14214:         if (selfcreateRegExp.test(caller)) {
14215:             optionsElement = domForm.elements[caller];
14216:             checkval = 'other';
14217:             dispval = 'inline'
14218:         }
14219:         if (optionsElement.length) {
14220:             var currval;
14221:             for (var i=0; i<optionsElement.length; i++) {
14222:                 if (optionsElement[i].checked) {
14223:                    currval = optionsElement[i].value;
14224:                 }
14225:             }
14226:             if (currval == checkval) {
14227:                 divitem.style.display = dispval;
14228:             } else {
14229:                 divitem.style.display = 'none';
14230:             }
14231:         }
14232:     }
14233:     return;
14234: }
14235: 
14236: // ]]>
14237: </script>
14238: 
14239: END
14240: 
14241: }
14242: 
14243: sub captcha_phrases {
14244:     return &Apache::lonlocal::texthash (
14245:                  priv => 'Private key',
14246:                  pub  => 'Public key',
14247:                  original  => 'original (CAPTCHA)',
14248:                  recaptcha => 'successor (ReCAPTCHA)',
14249:                  notused   => 'unused',
14250:                  ver => 'ReCAPTCHA version (1 or 2)',
14251:     );
14252: }
14253: 
14254: sub devalidate_remote_domconfs {
14255:     my ($dom,$cachekeys) = @_;
14256:     return unless (ref($cachekeys) eq 'HASH');
14257:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
14258:     my %thismachine;
14259:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
14260:     my @posscached = ('domainconfig','domdefaults','usersessions','directorysrch','cats');
14261:     if (keys(%servers)) {
14262:         foreach my $server (keys(%servers)) {
14263:             next if ($thismachine{$server});
14264:             my @cached;
14265:             foreach my $name (@posscached) {
14266:                 if ($cachekeys->{$name}) {
14267:                     push(@cached,&escape($name).':'.&escape($dom));
14268:                 }
14269:             }
14270:             if (@cached) {
14271:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
14272:             }
14273:         }
14274:     }
14275:     return;
14276: }
14277: 
14278: 1;

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