File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.79: download - view: text, annotated - select for diffs
Mon Jan 30 19:18:33 2017 UTC (7 years, 4 months ago) by raeburn
Branches: version_2_11_X
Diff to branchpoint 1.160: preferred, unified
- For 2.11
  - Backport 1.290, 1.291, 1.292.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.160.6.79 2017/01/30 19:18:33 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 => 'Institutional user types',
  266:                                   col2 => 'Assignable to e-mail usernames'}],
  267:                       print => \&print_defaults,
  268:                       modify => \&modify_defaults,
  269:                     },
  270:         'quotas' => 
  271:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  272:                       help => 'Domain_Configuration_Quotas',
  273:                       header => [{col1 => 'User affiliation',
  274:                                   col2 => 'Available tools',
  275:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  276:                       print => \&print_quotas,
  277:                       modify => \&modify_quotas,
  278:                     },
  279:         'autoenroll' =>
  280:                    { text => 'Auto-enrollment settings',
  281:                      help => 'Domain_Configuration_Auto_Enrollment',
  282:                      header => [{col1 => 'Configuration setting',
  283:                                  col2 => 'Value(s)'}],
  284:                      print => \&print_autoenroll,
  285:                      modify => \&modify_autoenroll,
  286:                    },
  287:         'autoupdate' => 
  288:                    { text => 'Auto-update settings',
  289:                      help => 'Domain_Configuration_Auto_Updates',
  290:                      header => [{col1 => 'Setting',
  291:                                  col2 => 'Value',},
  292:                                 {col1 => 'Setting',
  293:                                  col2 => 'Affiliation'},
  294:                                 {col1 => 'User population',
  295:                                  col2 => 'Updatable user data'}],
  296:                      print => \&print_autoupdate,
  297:                      modify => \&modify_autoupdate,
  298:                   },
  299:         'autocreate' => 
  300:                   { text => 'Auto-course creation settings',
  301:                      help => 'Domain_Configuration_Auto_Creation',
  302:                      header => [{col1 => 'Configuration Setting',
  303:                                  col2 => 'Value',}],
  304:                      print => \&print_autocreate,
  305:                      modify => \&modify_autocreate,
  306:                   },
  307:         'directorysrch' => 
  308:                   { text => 'Directory searches',
  309:                     help => 'Domain_Configuration_InstDirectory_Search',
  310:                     header => [{col1 => 'Institutional Directory Setting',
  311:                                 col2 => 'Value',},
  312:                                {col1 => 'LON-CAPA Directory Setting',
  313:                                 col2 => 'Value',}],
  314:                     print => \&print_directorysrch,
  315:                     modify => \&modify_directorysrch,
  316:                   },
  317:         'contacts' =>
  318:                   { text => 'E-mail addresses and helpform',
  319:                     help => 'Domain_Configuration_Contact_Info',
  320:                     header => [{col1 => 'Default e-mail addresses',
  321:                                 col2 => 'Value',},
  322:                                {col1 => 'Recipient(s) for notifications',
  323:                                 col2 => 'Value',},
  324:                                {col1 => 'Ask helpdesk form settings',
  325:                                 col2 => 'Value',},],
  326:                     print => \&print_contacts,
  327:                     modify => \&modify_contacts,
  328:                   },
  329:         'usercreation' => 
  330:                   { text => 'User creation',
  331:                     help => 'Domain_Configuration_User_Creation',
  332:                     header => [{col1 => 'Format rule type',
  333:                                 col2 => 'Format rules in force'},
  334:                                {col1 => 'User account creation',
  335:                                 col2 => 'Usernames which may be created',},
  336:                                {col1 => 'Context',
  337:                                 col2 => 'Assignable authentication types'}],
  338:                     print => \&print_usercreation,
  339:                     modify => \&modify_usercreation,
  340:                   },
  341:         'selfcreation' => 
  342:                   { text => 'Users self-creating accounts',
  343:                     help => 'Domain_Configuration_Self_Creation', 
  344:                     header => [{col1 => 'Self-creation with institutional username',
  345:                                 col2 => 'Enabled?'},
  346:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  347:                                 col2 => 'Information user can enter'},
  348:                                {col1 => 'Self-creation with e-mail as username',
  349:                                 col2 => 'Settings'}],
  350:                     print => \&print_selfcreation,
  351:                     modify => \&modify_selfcreation,
  352:                   },
  353:         'usermodification' =>
  354:                   { text => 'User modification',
  355:                     help => 'Domain_Configuration_User_Modification',
  356:                     header => [{col1 => 'Target user has role',
  357:                                 col2 => 'User information updatable in author context'},
  358:                                {col1 => 'Target user has role',
  359:                                 col2 => 'User information updatable in course context'}],
  360:                     print => \&print_usermodification,
  361:                     modify => \&modify_usermodification,
  362:                   },
  363:         'scantron' =>
  364:                   { text => 'Bubblesheet format file',
  365:                     help => 'Domain_Configuration_Scantron_Format',
  366:                     header => [ {col1 => 'Item',
  367:                                  col2 => '',
  368:                               }],
  369:                     print => \&print_scantron,
  370:                     modify => \&modify_scantron,
  371:                   },
  372:         'requestcourses' => 
  373:                  {text => 'Request creation of courses',
  374:                   help => 'Domain_Configuration_Request_Courses',
  375:                   header => [{col1 => 'User affiliation',
  376:                               col2 => 'Availability/Processing of requests',},
  377:                              {col1 => 'Setting',
  378:                               col2 => 'Value'},
  379:                              {col1 => 'Available textbooks',
  380:                               col2 => ''},
  381:                              {col1 => 'Available templates',
  382:                               col2 => ''},
  383:                              {col1 => 'Validation (not official courses)',
  384:                               col2 => 'Value'},],
  385:                   print => \&print_quotas,
  386:                   modify => \&modify_quotas,
  387:                  },
  388:         'requestauthor' =>
  389:                  {text => 'Request Authoring Space',
  390:                   help => 'Domain_Configuration_Request_Author',
  391:                   header => [{col1 => 'User affiliation',
  392:                               col2 => 'Availability/Processing of requests',},
  393:                              {col1 => 'Setting',
  394:                               col2 => 'Value'}],
  395:                   print => \&print_quotas,
  396:                   modify => \&modify_quotas,
  397:                  },
  398:         'coursecategories' =>
  399:                   { text => 'Cataloging of courses/communities',
  400:                     help => 'Domain_Configuration_Cataloging_Courses',
  401:                     header => [{col1 => 'Catalog type/availability',
  402:                                 col2 => '',},
  403:                                {col1 => 'Category settings for standard catalog',
  404:                                 col2 => '',},
  405:                                {col1 => 'Categories',
  406:                                 col2 => '',
  407:                                }],
  408:                     print => \&print_coursecategories,
  409:                     modify => \&modify_coursecategories,
  410:                   },
  411:         'serverstatuses' =>
  412:                  {text   => 'Access to server status pages',
  413:                   help   => 'Domain_Configuration_Server_Status',
  414:                   header => [{col1 => 'Status Page',
  415:                               col2 => 'Other named users',
  416:                               col3 => 'Specific IPs',
  417:                             }],
  418:                   print => \&print_serverstatuses,
  419:                   modify => \&modify_serverstatuses,
  420:                  },
  421:         'helpsettings' =>
  422:                  {text   => 'Support settings',
  423:                   help   => 'Domain_Configuration_Help_Settings',
  424:                   header => [{col1 => 'Help Page Settings (logged-in users)',
  425:                               col2 => 'Value'},
  426:                              {col1 => 'Helpdesk Roles',
  427:                               col2 => 'Settings'},],
  428:                   print  => \&print_helpsettings,
  429:                   modify => \&modify_helpsettings,
  430:                  },
  431:         'coursedefaults' => 
  432:                  {text => 'Course/Community defaults',
  433:                   help => 'Domain_Configuration_Course_Defaults',
  434:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  435:                               col2 => 'Value',},
  436:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  437:                               col2 => 'Value',},],
  438:                   print => \&print_coursedefaults,
  439:                   modify => \&modify_coursedefaults,
  440:                  },
  441:         'selfenrollment' => 
  442:                  {text   => 'Self-enrollment in Course/Community',
  443:                   help   => 'Domain_Configuration_Selfenrollment',
  444:                   header => [{col1 => 'Configuration Rights',
  445:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  446:                              {col1 => 'Defaults',
  447:                               col2 => 'Value'},
  448:                              {col1 => 'Self-enrollment validation (optional)',
  449:                               col2 => 'Value'},],
  450:                   print => \&print_selfenrollment,
  451:                   modify => \&modify_selfenrollment,
  452:                  },
  453:         'usersessions' =>
  454:                  {text  => 'User session hosting/offloading',
  455:                   help  => 'Domain_Configuration_User_Sessions',
  456:                   header => [{col1 => 'Domain server',
  457:                               col2 => 'Servers to offload sessions to when busy'},
  458:                              {col1 => 'Hosting of users from other domains',
  459:                               col2 => 'Rules'},
  460:                              {col1 => "Hosting domain's own users elsewhere",
  461:                               col2 => 'Rules'}],
  462:                   print => \&print_usersessions,
  463:                   modify => \&modify_usersessions,
  464:                  },
  465:         'loadbalancing' =>
  466:                  {text  => 'Dedicated Load Balancer(s)',
  467:                   help  => 'Domain_Configuration_Load_Balancing',
  468:                   header => [{col1 => 'Balancers',
  469:                               col2 => 'Default destinations',
  470:                               col3 => 'User affiliation',
  471:                               col4 => 'Overrides'},
  472:                             ],
  473:                   print => \&print_loadbalancing,
  474:                   modify => \&modify_loadbalancing,
  475:                  },
  476:     );
  477:     if (keys(%servers) > 1) {
  478:         $prefs{'login'}  = { text   => 'Log-in page options',
  479:                              help   => 'Domain_Configuration_Login_Page',
  480:                             header => [{col1 => 'Log-in Service',
  481:                                         col2 => 'Server Setting',},
  482:                                        {col1 => 'Log-in Page Items',
  483:                                         col2 => ''},
  484:                                        {col1 => 'Log-in Help',
  485:                                         col2 => 'Value'},
  486:                                        {col1 => 'Custom HTML in document head',
  487:                                         col2 => 'Value'}],
  488:                             print => \&print_login,
  489:                             modify => \&modify_login,
  490:                            };
  491:     }
  492: 
  493:     my @roles = ('student','coordinator','author','admin');
  494:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  495:     &Apache::lonhtmlcommon::add_breadcrumb
  496:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  497:       text=>"Settings to display/modify"});
  498:     my $confname = $dom.'-domainconfig';
  499: 
  500:     if ($phase eq 'process') {
  501:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  502:                                                               \%prefs,\%domconfig,$confname,\@roles);
  503:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  504:             $r->rflush();
  505:             &devalidate_remote_domconfs($dom,$result);
  506:         }
  507:     } elsif ($phase eq 'display') {
  508:         my $js = &recaptcha_js().
  509:                  &toggle_display_js();
  510:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  511:             my ($othertitle,$usertypes,$types) =
  512:                 &Apache::loncommon::sorted_inst_types($dom);
  513:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  514:                                           $domconfig{'loadbalancing'}).
  515:                    &new_spares_js().
  516:                    &common_domprefs_js().
  517:                    &Apache::loncommon::javascript_array_indexof();
  518:         }
  519:         if (grep(/^requestcourses$/,@actions)) {
  520:             my $javascript_validations;
  521:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  522:             $js .= <<END;
  523: <script type="text/javascript">
  524: $javascript_validations
  525: </script>
  526: $coursebrowserjs
  527: END
  528:         }
  529:         if (grep(/^contacts$/,@actions)) {
  530:             $js .= &contacts_javascript();
  531:         }
  532:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  533:     } else {
  534: # check if domconfig user exists for the domain.
  535:         my $servadm = $r->dir_config('lonAdmEMail');
  536:         my ($configuserok,$author_ok,$switchserver) =
  537:             &config_check($dom,$confname,$servadm);
  538:         unless ($configuserok eq 'ok') {
  539:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  540:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  541:                           $confname).
  542:                       '<br />'
  543:             );
  544:             if ($switchserver) {
  545:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  546:                           '<br />'.
  547:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  548:                           '<br />'.
  549:                           &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).
  550:                           '<br />'.
  551:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  552:                 );
  553:             } else {
  554:                 $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.').
  555:                           '<br />'.
  556:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  557:                 );
  558:             }
  559:             $r->print(&Apache::loncommon::end_page());
  560:             return OK;
  561:         }
  562:         if (keys(%domconfig) == 0) {
  563:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  564:             my @ids=&Apache::lonnet::current_machine_ids();
  565:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  566:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  567:                 my @loginimages = ('img','logo','domlogo','login');
  568:                 my $custom_img_count = 0;
  569:                 foreach my $img (@loginimages) {
  570:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  571:                         $custom_img_count ++;
  572:                     }
  573:                 }
  574:                 foreach my $role (@roles) {
  575:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  576:                         $custom_img_count ++;
  577:                     }
  578:                 }
  579:                 if ($custom_img_count > 0) {
  580:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  581:                     my $switch_server = &check_switchserver($dom,$confname);
  582:                     $r->print(
  583:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  584:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  585:     &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 />'.
  586:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  587:                     if ($switch_server) {
  588:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  589:                     }
  590:                     $r->print(&Apache::loncommon::end_page());
  591:                     return OK;
  592:                 }
  593:             }
  594:         }
  595:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  596:     }
  597:     return OK;
  598: }
  599: 
  600: sub process_changes {
  601:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  602:     my %domconfig;
  603:     if (ref($values) eq 'HASH') {
  604:         %domconfig = %{$values};
  605:     }
  606:     my $output;
  607:     if ($action eq 'login') {
  608:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  609:     } elsif ($action eq 'rolecolors') {
  610:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  611:                                      $lastactref,%domconfig);
  612:     } elsif ($action eq 'quotas') {
  613:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  614:     } elsif ($action eq 'autoenroll') {
  615:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  616:     } elsif ($action eq 'autoupdate') {
  617:         $output = &modify_autoupdate($dom,%domconfig);
  618:     } elsif ($action eq 'autocreate') {
  619:         $output = &modify_autocreate($dom,%domconfig);
  620:     } elsif ($action eq 'directorysrch') {
  621:         $output = &modify_directorysrch($dom,%domconfig);
  622:     } elsif ($action eq 'usercreation') {
  623:         $output = &modify_usercreation($dom,%domconfig);
  624:     } elsif ($action eq 'selfcreation') {
  625:         $output = &modify_selfcreation($dom,%domconfig);
  626:     } elsif ($action eq 'usermodification') {
  627:         $output = &modify_usermodification($dom,%domconfig);
  628:     } elsif ($action eq 'contacts') {
  629:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  630:     } elsif ($action eq 'defaults') {
  631:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  632:     } elsif ($action eq 'scantron') {
  633:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  634:     } elsif ($action eq 'coursecategories') {
  635:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  636:     } elsif ($action eq 'serverstatuses') {
  637:         $output = &modify_serverstatuses($dom,%domconfig);
  638:     } elsif ($action eq 'requestcourses') {
  639:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  640:     } elsif ($action eq 'requestauthor') {
  641:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  642:     } elsif ($action eq 'helpsettings') {
  643:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  644:     } elsif ($action eq 'coursedefaults') {
  645:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  646:     } elsif ($action eq 'selfenrollment') {
  647:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  648:     } elsif ($action eq 'usersessions') {
  649:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  650:     } elsif ($action eq 'loadbalancing') {
  651:         $output = &modify_loadbalancing($dom,%domconfig);
  652:     }
  653:     return $output;
  654: }
  655: 
  656: sub print_config_box {
  657:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  658:     my $rowtotal = 0;
  659:     my $output;
  660:     if ($action eq 'coursecategories') {
  661:         $output = &coursecategories_javascript($settings);
  662:     } elsif ($action eq 'defaults') {
  663:         $output = &defaults_javascript($settings); 
  664:     } elsif ($action eq 'helpsettings') {
  665:         my (%privs,%levelscurrent);
  666:         my %full=();
  667:         my %levels=(
  668:                      course => {},
  669:                      domain => {},
  670:                      system => {},
  671:                    );
  672:         my $context = 'domain';
  673:         my $crstype = 'Course';
  674:         my $formname = 'display';
  675:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  676:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  677:         $output =
  678:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full,
  679:                                                       \@templateroles);
  680:     }
  681:     $output .=
  682:          '<table class="LC_nested_outer">
  683:           <tr>
  684:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  685:            &mt($item->{text}).'&nbsp;'.
  686:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  687:           '</tr>';
  688:     $rowtotal ++;
  689:     my $numheaders = 1;
  690:     if (ref($item->{'header'}) eq 'ARRAY') {
  691:         $numheaders = scalar(@{$item->{'header'}});
  692:     }
  693:     if ($numheaders > 1) {
  694:         my $colspan = '';
  695:         my $rightcolspan = '';
  696:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  697:             ($action eq 'directorysrch') ||
  698:             (($action eq 'login') && ($numheaders < 4))) {
  699:             $colspan = ' colspan="2"';
  700:         }
  701:         if ($action eq 'usersessions') {
  702:             $rightcolspan = ' colspan="3"'; 
  703:         }
  704:         $output .= '
  705:           <tr>
  706:            <td>
  707:             <table class="LC_nested">
  708:              <tr class="LC_info_row">
  709:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  710:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  711:              </tr>';
  712:         $rowtotal ++;
  713:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  714:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  715:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'directorysrch') ||
  716:             ($action eq 'helpsettings') || ($action eq 'contacts')) {
  717:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  718:         } elsif ($action eq 'coursecategories') {
  719:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  720:         } elsif ($action eq 'login') {
  721:             if ($numheaders == 4) {
  722:                 $colspan = ' colspan="2"';
  723:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  724:             } else {
  725:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  726:             }
  727:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  728:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  729:         } elsif ($action eq 'rolecolors') {
  730:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  731:         }
  732:         $output .= '
  733:            </table>
  734:           </td>
  735:          </tr>
  736:          <tr>
  737:            <td>
  738:             <table class="LC_nested">
  739:              <tr class="LC_info_row">
  740:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  741:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  742:              </tr>';
  743:             $rowtotal ++;
  744:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  745:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  746:             ($action eq 'usersessions') || ($action eq 'coursecategories') ||
  747:             ($action eq 'contacts')) {
  748:             if ($action eq 'coursecategories') {
  749:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  750:                 $colspan = ' colspan="2"';
  751:             } else {
  752:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  753:             }
  754:             $output .= '
  755:            </table>
  756:           </td>
  757:          </tr>
  758:          <tr>
  759:            <td>
  760:             <table class="LC_nested">
  761:              <tr class="LC_info_row">
  762:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  763:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  764:              </tr>'."\n";
  765:             if ($action eq 'coursecategories') {
  766:                 $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  767:             } else {
  768:                 $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  769:             }
  770:             $rowtotal ++;
  771:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  772:                  ($action eq 'defaults') || ($action eq 'directorysrch') ||
  773:                  ($action eq 'helpsettings')) {
  774:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  775:         } elsif ($action eq 'login') {
  776:             if ($numheaders == 4) {
  777:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  778:            </table>
  779:           </td>
  780:          </tr>
  781:          <tr>
  782:            <td>
  783:             <table class="LC_nested">
  784:              <tr class="LC_info_row">
  785:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  786:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  787:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  788:                 $rowtotal ++;
  789:             } else {
  790:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  791:             }
  792:             $output .= '
  793:            </table>
  794:           </td>
  795:          </tr>
  796:          <tr>
  797:            <td>
  798:             <table class="LC_nested">
  799:              <tr class="LC_info_row">';
  800:             if ($numheaders == 4) {
  801:                 $output .= '
  802:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  803:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  804:              </tr>';
  805:             } else {
  806:                 $output .= '
  807:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  808:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  809:              </tr>';
  810:             }
  811:             $rowtotal ++;
  812:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal);
  813:         } elsif ($action eq 'requestcourses') {
  814:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  815:             $rowtotal ++;
  816:             $output .= &print_studentcode($settings,\$rowtotal).'
  817:            </table>
  818:           </td>
  819:          </tr>
  820:          <tr>
  821:            <td>
  822:             <table class="LC_nested">
  823:              <tr class="LC_info_row">
  824:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  825:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  826:                        &textbookcourses_javascript($settings).
  827:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
  828:             </table>
  829:            </td>
  830:           </tr>
  831:          <tr>
  832:            <td>
  833:             <table class="LC_nested">
  834:              <tr class="LC_info_row">
  835:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  836:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
  837:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
  838:             </table>
  839:            </td>
  840:           </tr>
  841:           <tr>
  842:            <td>
  843:             <table class="LC_nested">
  844:              <tr class="LC_info_row">
  845:               <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
  846:               <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
  847:              </tr>'.
  848:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
  849:         } elsif ($action eq 'requestauthor') {
  850:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  851:             $rowtotal ++;
  852:         } elsif ($action eq 'rolecolors') {
  853:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  854:            </table>
  855:           </td>
  856:          </tr>
  857:          <tr>
  858:            <td>
  859:             <table class="LC_nested">
  860:              <tr class="LC_info_row">
  861:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  862:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  863:               <td class="LC_right_item" valign="top">'.
  864:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  865:              </tr>'.
  866:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  867:            </table>
  868:           </td>
  869:          </tr>
  870:          <tr>
  871:            <td>
  872:             <table class="LC_nested">
  873:              <tr class="LC_info_row">
  874:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  875:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  876:              </tr>'.
  877:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  878:             $rowtotal += 2;
  879:         }
  880:     } else {
  881:         $output .= '
  882:           <tr>
  883:            <td>
  884:             <table class="LC_nested">
  885:              <tr class="LC_info_row">';
  886:         if ($action eq 'login') {
  887:             $output .= '  
  888:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  889:         } elsif ($action eq 'serverstatuses') {
  890:             $output .= '
  891:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  892:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  893: 
  894:         } else {
  895:             $output .= '
  896:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  897:         }
  898:         if (defined($item->{'header'}->[0]->{'col3'})) {
  899:             $output .= '<td class="LC_left_item" valign="top">'.
  900:                        &mt($item->{'header'}->[0]->{'col2'});
  901:             if ($action eq 'serverstatuses') {
  902:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  903:             } 
  904:         } else {
  905:             $output .= '<td class="LC_right_item" valign="top">'.
  906:                        &mt($item->{'header'}->[0]->{'col2'});
  907:         }
  908:         $output .= '</td>';
  909:         if ($item->{'header'}->[0]->{'col3'}) {
  910:             if (defined($item->{'header'}->[0]->{'col4'})) {
  911:                 $output .= '<td class="LC_left_item" valign="top">'.
  912:                             &mt($item->{'header'}->[0]->{'col3'});
  913:             } else {
  914:                 $output .= '<td class="LC_right_item" valign="top">'.
  915:                            &mt($item->{'header'}->[0]->{'col3'});
  916:             }
  917:             if ($action eq 'serverstatuses') {
  918:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  919:             }
  920:             $output .= '</td>';
  921:         }
  922:         if ($item->{'header'}->[0]->{'col4'}) {
  923:             $output .= '<td class="LC_right_item" valign="top">'.
  924:                        &mt($item->{'header'}->[0]->{'col4'});
  925:         }
  926:         $output .= '</tr>';
  927:         $rowtotal ++;
  928:         if ($action eq 'quotas') {
  929:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  930:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
  931:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing')) {
  932:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
  933:         } elsif ($action eq 'scantron') {
  934:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  935:         }
  936:     }
  937:     $output .= '
  938:    </table>
  939:   </td>
  940:  </tr>
  941: </table><br />';
  942:     return ($output,$rowtotal);
  943: }
  944: 
  945: sub print_login {
  946:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  947:     my ($css_class,$datatable);
  948:     my %choices = &login_choices();
  949: 
  950:     if ($caller eq 'service') {
  951:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  952:         my $choice = $choices{'disallowlogin'};
  953:         $css_class = ' class="LC_odd_row"';
  954:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  955:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  956:                       '<th>'.$choices{'server'}.'</th>'.
  957:                       '<th>'.$choices{'serverpath'}.'</th>'.
  958:                       '<th>'.$choices{'custompath'}.'</th>'.
  959:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  960:         my %disallowed;
  961:         if (ref($settings) eq 'HASH') {
  962:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  963:                %disallowed = %{$settings->{'loginvia'}};
  964:             }
  965:         }
  966:         foreach my $lonhost (sort(keys(%servers))) {
  967:             my $direct = 'selected="selected"';
  968:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  969:                 if ($disallowed{$lonhost}{'server'} ne '') {
  970:                     $direct = '';
  971:                 }
  972:             }
  973:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  974:                           '<td><select name="'.$lonhost.'_server">'.
  975:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  976:                           '</option>';
  977:             foreach my $hostid (sort(keys(%servers))) {
  978:                 next if ($servers{$hostid} eq $servers{$lonhost});
  979:                 my $selected = '';
  980:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  981:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  982:                         $selected = 'selected="selected"';
  983:                     }
  984:                 }
  985:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  986:                               $servers{$hostid}.'</option>';
  987:             }
  988:             $datatable .= '</select></td>'.
  989:                           '<td><select name="'.$lonhost.'_serverpath">';
  990:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  991:                 my $pathname = $path;
  992:                 if ($path eq 'custom') {
  993:                     $pathname = &mt('Custom Path').' ->';
  994:                 }
  995:                 my $selected = '';
  996:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  997:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  998:                         $selected = 'selected="selected"';
  999:                     }
 1000:                 } elsif ($path eq '') {
 1001:                     $selected = 'selected="selected"';
 1002:                 }
 1003:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1004:             }
 1005:             $datatable .= '</select></td>';
 1006:             my ($custom,$exempt);
 1007:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1008:                 $custom = $disallowed{$lonhost}{'custompath'};
 1009:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1010:             }
 1011:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1012:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1013:                           '</tr>';
 1014:         }
 1015:         $datatable .= '</table></td></tr>';
 1016:         return $datatable;
 1017:     } elsif ($caller eq 'page') {
 1018:         my %defaultchecked = ( 
 1019:                                'coursecatalog' => 'on',
 1020:                                'helpdesk'      => 'on',
 1021:                                'adminmail'     => 'off',
 1022:                                'newuser'       => 'off',
 1023:                              );
 1024:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1025:         my (%checkedon,%checkedoff);
 1026:         foreach my $item (@toggles) {
 1027:             if ($defaultchecked{$item} eq 'on') { 
 1028:                 $checkedon{$item} = ' checked="checked" ';
 1029:                 $checkedoff{$item} = ' ';
 1030:             } elsif ($defaultchecked{$item} eq 'off') {
 1031:                 $checkedoff{$item} = ' checked="checked" ';
 1032:                 $checkedon{$item} = ' ';
 1033:             }
 1034:         }
 1035:         my @images = ('img','logo','domlogo','login');
 1036:         my @logintext = ('textcol','bgcol');
 1037:         my @bgs = ('pgbg','mainbg','sidebg');
 1038:         my @links = ('link','alink','vlink');
 1039:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1040:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1041:         my (%is_custom,%designs);
 1042:         my %defaults = (
 1043:                        font => $defaultdesign{'login.font'},
 1044:                        );
 1045:         foreach my $item (@images) {
 1046:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1047:             $defaults{'showlogo'}{$item} = 1;
 1048:         }
 1049:         foreach my $item (@bgs) {
 1050:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1051:         }
 1052:         foreach my $item (@logintext) {
 1053:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1054:         }
 1055:         foreach my $item (@links) {
 1056:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1057:         }
 1058:         if (ref($settings) eq 'HASH') {
 1059:             foreach my $item (@toggles) {
 1060:                 if ($settings->{$item} eq '1') {
 1061:                     $checkedon{$item} =  ' checked="checked" ';
 1062:                     $checkedoff{$item} = ' ';
 1063:                 } elsif ($settings->{$item} eq '0') {
 1064:                     $checkedoff{$item} =  ' checked="checked" ';
 1065:                     $checkedon{$item} = ' ';
 1066:                 }
 1067:             }
 1068:             foreach my $item (@images) {
 1069:                 if (defined($settings->{$item})) {
 1070:                     $designs{$item} = $settings->{$item};
 1071:                     $is_custom{$item} = 1;
 1072:                 }
 1073:                 if (defined($settings->{'showlogo'}{$item})) {
 1074:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1075:                 }
 1076:             }
 1077:             foreach my $item (@logintext) {
 1078:                 if ($settings->{$item} ne '') {
 1079:                     $designs{'logintext'}{$item} = $settings->{$item};
 1080:                     $is_custom{$item} = 1;
 1081:                 }
 1082:             }
 1083:             if ($settings->{'font'} ne '') {
 1084:                 $designs{'font'} = $settings->{'font'};
 1085:                 $is_custom{'font'} = 1;
 1086:             }
 1087:             foreach my $item (@bgs) {
 1088:                 if ($settings->{$item} ne '') {
 1089:                     $designs{'bgs'}{$item} = $settings->{$item};
 1090:                     $is_custom{$item} = 1;
 1091:                 }
 1092:             }
 1093:             foreach my $item (@links) {
 1094:                 if ($settings->{$item} ne '') {
 1095:                     $designs{'links'}{$item} = $settings->{$item};
 1096:                     $is_custom{$item} = 1;
 1097:                 }
 1098:             }
 1099:         } else {
 1100:             if ($designhash{$dom.'.login.font'} ne '') {
 1101:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1102:                 $is_custom{'font'} = 1;
 1103:             }
 1104:             foreach my $item (@images) {
 1105:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1106:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1107:                     $is_custom{$item} = 1;
 1108:                 }
 1109:             }
 1110:             foreach my $item (@bgs) {
 1111:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1112:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1113:                     $is_custom{$item} = 1;
 1114:                 }
 1115:             }
 1116:             foreach my $item (@links) {
 1117:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1118:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1119:                     $is_custom{$item} = 1;
 1120:                 }
 1121:             }
 1122:         }
 1123:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1124:                                                       logo => 'Institution Logo',
 1125:                                                       domlogo => 'Domain Logo',
 1126:                                                       login => 'Login box');
 1127:         my $itemcount = 1;
 1128:         foreach my $item (@toggles) {
 1129:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1130:             $datatable .=  
 1131:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1132:                 '</td><td>'.
 1133:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1134:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1135:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1136:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1137:                 '</tr>';
 1138:             $itemcount ++;
 1139:         }
 1140:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1141:         $datatable .= '</tr></table></td></tr>';
 1142:     } elsif ($caller eq 'help') {
 1143:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1144:         my $switchserver = &check_switchserver($dom,$confname);
 1145:         my $itemcount = 1;
 1146:         $defaulturl = '/adm/loginproblems.html';
 1147:         $defaulttype = 'default';
 1148:         %lt = &Apache::lonlocal::texthash (
 1149:                      del     => 'Delete?',
 1150:                      rep     => 'Replace:',
 1151:                      upl     => 'Upload:',
 1152:                      default => 'Default',
 1153:                      custom  => 'Custom',
 1154:                                              );
 1155:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1156:         my @currlangs;
 1157:         if (ref($settings) eq 'HASH') {
 1158:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1159:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1160:                     next if ($settings->{'helpurl'}{$key} eq '');
 1161:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1162:                     $type{$key} = 'custom';
 1163:                     unless ($key eq 'nolang') {
 1164:                         push(@currlangs,$key);
 1165:                     }
 1166:                 }
 1167:             } elsif ($settings->{'helpurl'} ne '') {
 1168:                 $type{'nolang'} = 'custom';
 1169:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1170:             }
 1171:         }
 1172:         foreach my $lang ('nolang',sort(@currlangs)) {
 1173:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1174:             $datatable .= '<tr'.$css_class.'>';
 1175:             if ($url{$lang} eq '') {
 1176:                 $url{$lang} = $defaulturl;
 1177:             }
 1178:             if ($type{$lang} eq '') {
 1179:                 $type{$lang} = $defaulttype;
 1180:             }
 1181:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1182:             if ($lang eq 'nolang') {
 1183:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1184:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1185:             } else {
 1186:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1187:                                   $langchoices{$lang},
 1188:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1189:             }
 1190:             $datatable .= '</span></td>'."\n".
 1191:                           '<td class="LC_left_item">';
 1192:             if ($type{$lang} eq 'custom') {
 1193:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1194:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1195:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1196:             } else {
 1197:                 $datatable .= $lt{'upl'};
 1198:             }
 1199:             $datatable .='<br />';
 1200:             if ($switchserver) {
 1201:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1202:             } else {
 1203:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1204:             }
 1205:             $datatable .= '</td></tr>';
 1206:             $itemcount ++;
 1207:         }
 1208:         my @addlangs;
 1209:         foreach my $lang (sort(keys(%langchoices))) {
 1210:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1211:             push(@addlangs,$lang);
 1212:         }
 1213:         if (@addlangs > 0) {
 1214:             my %toadd;
 1215:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1216:             $toadd{''} = &mt('Select');
 1217:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1218:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1219:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1220:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1221:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1222:             if ($switchserver) {
 1223:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1224:             } else {
 1225:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1226:             }
 1227:             $datatable .= '</td></tr>';
 1228:             $itemcount ++;
 1229:         }
 1230:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1231:     } elsif ($caller eq 'headtag') {
 1232:         my %domservers = &Apache::lonnet::get_servers($dom);
 1233:         my $choice = $choices{'headtag'};
 1234:         $css_class = ' class="LC_odd_row"';
 1235:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1236:                       '<td align="left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1237:                       '<th>'.$choices{'current'}.'</th>'.
 1238:                       '<th>'.$choices{'action'}.'</th>'.
 1239:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1240:         my (%currurls,%currexempt);
 1241:         if (ref($settings) eq 'HASH') {
 1242:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1243:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1244:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1245:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1246:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1247:                     }
 1248:                 }
 1249:             }
 1250:         }
 1251:         my %lt = &Apache::lonlocal::texthash(
 1252:                                                del  => 'Delete?',
 1253:                                                rep  => 'Replace:',
 1254:                                                upl  => 'Upload:',
 1255:                                                curr => 'View contents',
 1256:                                                none => 'None',
 1257:         );
 1258:         my $switchserver = &check_switchserver($dom,$confname);
 1259:         foreach my $lonhost (sort(keys(%domservers))) {
 1260:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1261:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1262:             if ($currurls{$lonhost}) {
 1263:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1264:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1265:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1266:                               '">'.$lt{'curr'}.'</a></td>'.
 1267:                               '<td><span class="LC_nobreak"><label>'.
 1268:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1269:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1270:             } else {
 1271:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1272:             }
 1273:             $datatable .='<br />';
 1274:             if ($switchserver) {
 1275:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1276:             } else {
 1277:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1278:             }
 1279:             $datatable .= '</td><td><input type="textbox" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1280:         }
 1281:         $datatable .= '</table></td></tr>';
 1282:     }
 1283:     return $datatable;
 1284: }
 1285: 
 1286: sub login_choices {
 1287:     my %choices =
 1288:         &Apache::lonlocal::texthash (
 1289:             coursecatalog => 'Display Course/Community Catalog link?',
 1290:             adminmail     => "Display Administrator's E-mail Address?",
 1291:             helpdesk      => 'Display "Contact Helpdesk" link',
 1292:             disallowlogin => "Login page requests redirected",
 1293:             hostid        => "Server",
 1294:             server        => "Redirect to:",
 1295:             serverpath    => "Path",
 1296:             custompath    => "Custom", 
 1297:             exempt        => "Exempt IP(s)",
 1298:             directlogin   => "No redirect",
 1299:             newuser       => "Link to create a user account",
 1300:             img           => "Header",
 1301:             logo          => "Main Logo",
 1302:             domlogo       => "Domain Logo",
 1303:             login         => "Log-in Header", 
 1304:             textcol       => "Text color",
 1305:             bgcol         => "Box color",
 1306:             bgs           => "Background colors",
 1307:             links         => "Link colors",
 1308:             font          => "Font color",
 1309:             pgbg          => "Header",
 1310:             mainbg        => "Page",
 1311:             sidebg        => "Login box",
 1312:             link          => "Link",
 1313:             alink         => "Active link",
 1314:             vlink         => "Visited link",
 1315:             headtag       => "Custom markup",
 1316:             action        => "Action",
 1317:             current       => "Current",
 1318:         );
 1319:     return %choices;
 1320: }
 1321: 
 1322: sub print_rolecolors {
 1323:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1324:     my %choices = &color_font_choices();
 1325:     my @bgs = ('pgbg','tabbg','sidebg');
 1326:     my @links = ('link','alink','vlink');
 1327:     my @images = ('img');
 1328:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1329:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1330:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1331:     my (%is_custom,%designs);
 1332:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1333:     if (ref($settings) eq 'HASH') {
 1334:         if (ref($settings->{$role}) eq 'HASH') {
 1335:             if ($settings->{$role}->{'img'} ne '') {
 1336:                 $designs{'img'} = $settings->{$role}->{'img'};
 1337:                 $is_custom{'img'} = 1;
 1338:             }
 1339:             if ($settings->{$role}->{'font'} ne '') {
 1340:                 $designs{'font'} = $settings->{$role}->{'font'};
 1341:                 $is_custom{'font'} = 1;
 1342:             }
 1343:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1344:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1345:                 $is_custom{'fontmenu'} = 1;
 1346:             }
 1347:             foreach my $item (@bgs) {
 1348:                 if ($settings->{$role}->{$item} ne '') {
 1349:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1350:                     $is_custom{$item} = 1;
 1351:                 }
 1352:             }
 1353:             foreach my $item (@links) {
 1354:                 if ($settings->{$role}->{$item} ne '') {
 1355:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1356:                     $is_custom{$item} = 1;
 1357:                 }
 1358:             }
 1359:         }
 1360:     } else {
 1361:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1362:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1363:             $is_custom{'img'} = 1;
 1364:         }
 1365:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1366:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1367:             $is_custom{'fontmenu'} = 1; 
 1368:         }
 1369:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1370:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1371:             $is_custom{'font'} = 1;
 1372:         }
 1373:         foreach my $item (@bgs) {
 1374:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1375:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1376:                 $is_custom{$item} = 1;
 1377:             
 1378:             }
 1379:         }
 1380:         foreach my $item (@links) {
 1381:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1382:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1383:                 $is_custom{$item} = 1;
 1384:             }
 1385:         }
 1386:     }
 1387:     my $itemcount = 1;
 1388:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1389:     $datatable .= '</tr></table></td></tr>';
 1390:     return $datatable;
 1391: }
 1392: 
 1393: sub role_defaults {
 1394:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1395:     my %defaults;
 1396:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1397:         return %defaults;
 1398:     }
 1399:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1400:     if ($role eq 'login') {
 1401:         %defaults = (
 1402:                        font => $defaultdesign{$role.'.font'},
 1403:                     );
 1404:         if (ref($logintext) eq 'ARRAY') {
 1405:             foreach my $item (@{$logintext}) {
 1406:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1407:             }
 1408:         }
 1409:         foreach my $item (@{$images}) {
 1410:             $defaults{'showlogo'}{$item} = 1;
 1411:         }
 1412:     } else {
 1413:         %defaults = (
 1414:                        img => $defaultdesign{$role.'.img'},
 1415:                        font => $defaultdesign{$role.'.font'},
 1416:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1417:                     );
 1418:     }
 1419:     foreach my $item (@{$bgs}) {
 1420:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1421:     }
 1422:     foreach my $item (@{$links}) {
 1423:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1424:     }
 1425:     foreach my $item (@{$images}) {
 1426:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1427:     }
 1428:     return %defaults;
 1429: }
 1430: 
 1431: sub display_color_options {
 1432:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1433:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1434:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1435:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1436:     my $datatable = '<tr'.$css_class.'>'.
 1437:         '<td>'.$choices->{'font'}.'</td>';
 1438:     if (!$is_custom->{'font'}) {
 1439:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1440:     } else {
 1441:         $datatable .= '<td>&nbsp;</td>';
 1442:     }
 1443:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1444: 
 1445:     $datatable .= '<td><span class="LC_nobreak">'.
 1446:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1447:                   ' value="'.$current_color.'" />&nbsp;'.
 1448:                   '&nbsp;</td></tr>';
 1449:     unless ($role eq 'login') { 
 1450:         $datatable .= '<tr'.$css_class.'>'.
 1451:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1452:         if (!$is_custom->{'fontmenu'}) {
 1453:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1454:         } else {
 1455:             $datatable .= '<td>&nbsp;</td>';
 1456:         }
 1457: 	$current_color = $designs->{'fontmenu'} ?
 1458: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1459:         $datatable .= '<td><span class="LC_nobreak">'.
 1460:                       '<input class="colorchooser" type="text" size="10" name="'
 1461: 		      .$role.'_fontmenu"'.
 1462:                       ' value="'.$current_color.'" />&nbsp;'.
 1463:                       '&nbsp;</td></tr>';
 1464:     }
 1465:     my $switchserver = &check_switchserver($dom,$confname);
 1466:     foreach my $img (@{$images}) {
 1467: 	$itemcount ++;
 1468:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1469:         $datatable .= '<tr'.$css_class.'>'.
 1470:                       '<td>'.$choices->{$img};
 1471:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1472:         if ($role eq 'login') {
 1473:             if ($img eq 'login') {
 1474:                 $login_hdr_pick =
 1475:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1476:                 $logincolors =
 1477:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1478:                                        $designs,$defaults);
 1479:             } elsif ($img ne 'domlogo') {
 1480:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1481:             }
 1482:         }
 1483:         $datatable .= '</td>';
 1484:         if ($designs->{$img} ne '') {
 1485:             $imgfile = $designs->{$img};
 1486: 	    $img_import = ($imgfile =~ m{^/adm/});
 1487:         } else {
 1488:             $imgfile = $defaults->{$img};
 1489:         }
 1490:         if ($imgfile) {
 1491:             my ($showfile,$fullsize);
 1492:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1493:                 my $urldir = $1;
 1494:                 my $filename = $2;
 1495:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1496:                 if (@info) {
 1497:                     my $thumbfile = 'tn-'.$filename;
 1498:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1499:                     if (@thumb) {
 1500:                         $showfile = $urldir.'/'.$thumbfile;
 1501:                     } else {
 1502:                         $showfile = $imgfile;
 1503:                     }
 1504:                 } else {
 1505:                     $showfile = '';
 1506:                 }
 1507:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1508:                 $showfile = $imgfile;
 1509:                 my $imgdir = $1;
 1510:                 my $filename = $2;
 1511:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1512:                     $showfile = "/$imgdir/tn-".$filename;
 1513:                 } else {
 1514:                     my $input = $londocroot.$imgfile;
 1515:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1516:                     if (!-e $output) {
 1517:                         my ($width,$height) = &thumb_dimensions();
 1518:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1519:                         if ($fullwidth ne '' && $fullheight ne '') {
 1520:                             if ($fullwidth > $width && $fullheight > $height) { 
 1521:                                 my $size = $width.'x'.$height;
 1522:                                 system("convert -sample $size $input $output");
 1523:                                 $showfile = "/$imgdir/tn-".$filename;
 1524:                             }
 1525:                         }
 1526:                     }
 1527:                 }
 1528:             }
 1529:             if ($showfile) {
 1530:                 if ($showfile =~ m{^/(adm|res)/}) {
 1531:                     if ($showfile =~ m{^/res/}) {
 1532:                         my $local_showfile =
 1533:                             &Apache::lonnet::filelocation('',$showfile);
 1534:                         &Apache::lonnet::repcopy($local_showfile);
 1535:                     }
 1536:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1537:                 }
 1538:                 if ($imgfile) {
 1539:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1540:                         if ($imgfile =~ m{^/res/}) {
 1541:                             my $local_imgfile =
 1542:                                 &Apache::lonnet::filelocation('',$imgfile);
 1543:                             &Apache::lonnet::repcopy($local_imgfile);
 1544:                         }
 1545:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1546:                     } else {
 1547:                         $fullsize = $imgfile;
 1548:                     }
 1549:                 }
 1550:                 $datatable .= '<td>';
 1551:                 if ($img eq 'login') {
 1552:                     $datatable .= $login_hdr_pick;
 1553:                 } 
 1554:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1555:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1556:             } else {
 1557:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1558:                               &mt('Upload:').'<br />';
 1559:             }
 1560:         } else {
 1561:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1562:                           &mt('Upload:').'<br />';
 1563:         }
 1564:         if ($switchserver) {
 1565:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1566:         } else {
 1567:             if ($img ne 'login') { # suppress file selection for Log-in header
 1568:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1569:             }
 1570:         }
 1571:         $datatable .= '</td></tr>';
 1572:     }
 1573:     $itemcount ++;
 1574:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1575:     $datatable .= '<tr'.$css_class.'>'.
 1576:                   '<td>'.$choices->{'bgs'}.'</td>';
 1577:     my $bgs_def;
 1578:     foreach my $item (@{$bgs}) {
 1579:         if (!$is_custom->{$item}) {
 1580:             $bgs_def .= '<td><span class="LC_nobreak">'.$choices->{$item}.'</span>&nbsp;<span id="css_default_'.$role.'_'.$item.'" style="background-color: '.$defaults->{'bgs'}{$item}.';">&nbsp;&nbsp;&nbsp;</span><br />'.$defaults->{'bgs'}{$item}.'</td>';
 1581:         }
 1582:     }
 1583:     if ($bgs_def) {
 1584:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1585:     } else {
 1586:         $datatable .= '<td>&nbsp;</td>';
 1587:     }
 1588:     $datatable .= '<td class="LC_right_item">'.
 1589:                   '<table border="0"><tr>';
 1590: 
 1591:     foreach my $item (@{$bgs}) {
 1592:         $datatable .= '<td align="center">'.$choices->{$item};
 1593: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1594:         if ($designs->{'bgs'}{$item}) {
 1595:             $datatable .= '&nbsp;';
 1596:         }
 1597:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1598:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1599:     }
 1600:     $datatable .= '</tr></table></td></tr>';
 1601:     $itemcount ++;
 1602:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1603:     $datatable .= '<tr'.$css_class.'>'.
 1604:                   '<td>'.$choices->{'links'}.'</td>';
 1605:     my $links_def;
 1606:     foreach my $item (@{$links}) {
 1607:         if (!$is_custom->{$item}) {
 1608:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1609:         }
 1610:     }
 1611:     if ($links_def) {
 1612:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1613:     } else {
 1614:         $datatable .= '<td>&nbsp;</td>';
 1615:     }
 1616:     $datatable .= '<td class="LC_right_item">'.
 1617:                   '<table border="0"><tr>';
 1618:     foreach my $item (@{$links}) {
 1619: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1620:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1621:         if ($designs->{'links'}{$item}) {
 1622:             $datatable.='&nbsp;';
 1623:         }
 1624:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1625:                       '" /></td>';
 1626:     }
 1627:     $$rowtotal += $itemcount;
 1628:     return $datatable;
 1629: }
 1630: 
 1631: sub logo_display_options {
 1632:     my ($img,$defaults,$designs) = @_;
 1633:     my $checkedon;
 1634:     if (ref($defaults) eq 'HASH') {
 1635:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1636:             if ($defaults->{'showlogo'}{$img}) {
 1637:                 $checkedon = 'checked="checked" ';     
 1638:             }
 1639:         } 
 1640:     }
 1641:     if (ref($designs) eq 'HASH') {
 1642:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1643:             if (defined($designs->{'showlogo'}{$img})) {
 1644:                 if ($designs->{'showlogo'}{$img} == 0) {
 1645:                     $checkedon = '';
 1646:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1647:                     $checkedon = 'checked="checked" ';
 1648:                 }
 1649:             }
 1650:         }
 1651:     }
 1652:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1653:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1654:            &mt('show').'</label>'."\n";
 1655: }
 1656: 
 1657: sub login_header_options  {
 1658:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1659:     my $output = '';
 1660:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1661:         $output .= &mt('Text default(s):').'<br />';
 1662:         if (!$is_custom->{'textcol'}) {
 1663:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1664:                        '&nbsp;&nbsp;&nbsp;';
 1665:         }
 1666:         if (!$is_custom->{'bgcol'}) {
 1667:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1668:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1669:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1670:         }
 1671:         $output .= '<br />';
 1672:     }
 1673:     $output .='<br />';
 1674:     return $output;
 1675: }
 1676: 
 1677: sub login_text_colors {
 1678:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1679:     my $color_menu = '<table border="0"><tr>';
 1680:     foreach my $item (@{$logintext}) {
 1681:         $color_menu .= '<td align="center">'.$choices->{$item};
 1682:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1683:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1684:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1685:     }
 1686:     $color_menu .= '</tr></table><br />';
 1687:     return $color_menu;
 1688: }
 1689: 
 1690: sub image_changes {
 1691:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1692:     my $output;
 1693:     if ($img eq 'login') {
 1694:             # suppress image for Log-in header
 1695:     } elsif (!$is_custom) {
 1696:         if ($img ne 'domlogo') {
 1697:             $output .= &mt('Default image:').'<br />';
 1698:         } else {
 1699:             $output .= &mt('Default in use:').'<br />';
 1700:         }
 1701:     }
 1702:     if ($img eq 'login') { # suppress image for Log-in header
 1703:         $output .= '<td>'.$logincolors;
 1704:     } else {
 1705:         if ($img_import) {
 1706:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1707:         }
 1708:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1709:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1710:         if ($is_custom) {
 1711:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1712:                        '<input type="checkbox" name="'.
 1713:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1714:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1715:         } else {
 1716:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1717:         }
 1718:     }
 1719:     return $output;
 1720: }
 1721: 
 1722: sub print_quotas {
 1723:     my ($dom,$settings,$rowtotal,$action) = @_;
 1724:     my $context;
 1725:     if ($action eq 'quotas') {
 1726:         $context = 'tools';
 1727:     } else {
 1728:         $context = $action;
 1729:     }
 1730:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1731:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1732:     my $typecount = 0;
 1733:     my ($css_class,%titles);
 1734:     if ($context eq 'requestcourses') {
 1735:         @usertools = ('official','unofficial','community','textbook');
 1736:         @options =('norequest','approval','validate','autolimit');
 1737:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1738:         %titles = &courserequest_titles();
 1739:     } elsif ($context eq 'requestauthor') {
 1740:         @usertools = ('author');
 1741:         @options = ('norequest','approval','automatic');
 1742:         %titles = &authorrequest_titles();
 1743:     } else {
 1744:         @usertools = ('aboutme','blog','webdav','portfolio');
 1745:         %titles = &tool_titles();
 1746:     }
 1747:     if (ref($types) eq 'ARRAY') {
 1748:         foreach my $type (@{$types}) {
 1749:             my ($currdefquota,$currauthorquota);
 1750:             unless (($context eq 'requestcourses') ||
 1751:                     ($context eq 'requestauthor')) {
 1752:                 if (ref($settings) eq 'HASH') {
 1753:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1754:                         $currdefquota = $settings->{defaultquota}->{$type};
 1755:                     } else {
 1756:                         $currdefquota = $settings->{$type};
 1757:                     }
 1758:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1759:                         $currauthorquota = $settings->{authorquota}->{$type};
 1760:                     }
 1761:                 }
 1762:             }
 1763:             if (defined($usertypes->{$type})) {
 1764:                 $typecount ++;
 1765:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1766:                 $datatable .= '<tr'.$css_class.'>'.
 1767:                               '<td>'.$usertypes->{$type}.'</td>'.
 1768:                               '<td class="LC_left_item">';
 1769:                 if ($context eq 'requestcourses') {
 1770:                     $datatable .= '<table><tr>';
 1771:                 }
 1772:                 my %cell;  
 1773:                 foreach my $item (@usertools) {
 1774:                     if ($context eq 'requestcourses') {
 1775:                         my ($curroption,$currlimit);
 1776:                         if (ref($settings) eq 'HASH') {
 1777:                             if (ref($settings->{$item}) eq 'HASH') {
 1778:                                 $curroption = $settings->{$item}->{$type};
 1779:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1780:                                     $currlimit = $1; 
 1781:                                 }
 1782:                             }
 1783:                         }
 1784:                         if (!$curroption) {
 1785:                             $curroption = 'norequest';
 1786:                         }
 1787:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1788:                         foreach my $option (@options) {
 1789:                             my $val = $option;
 1790:                             if ($option eq 'norequest') {
 1791:                                 $val = 0;  
 1792:                             }
 1793:                             if ($option eq 'validate') {
 1794:                                 my $canvalidate = 0;
 1795:                                 if (ref($validations{$item}) eq 'HASH') { 
 1796:                                     if ($validations{$item}{$type}) {
 1797:                                         $canvalidate = 1;
 1798:                                     }
 1799:                                 }
 1800:                                 next if (!$canvalidate);
 1801:                             }
 1802:                             my $checked = '';
 1803:                             if ($option eq $curroption) {
 1804:                                 $checked = ' checked="checked"';
 1805:                             } elsif ($option eq 'autolimit') {
 1806:                                 if ($curroption =~ /^autolimit/) {
 1807:                                     $checked = ' checked="checked"';
 1808:                                 }                       
 1809:                             } 
 1810:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1811:                                   '<input type="radio" name="crsreq_'.$item.
 1812:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1813:                                   $titles{$option}.'</label>';
 1814:                             if ($option eq 'autolimit') {
 1815:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1816:                                                 $item.'_limit_'.$type.'" size="1" '.
 1817:                                                 'value="'.$currlimit.'" />';
 1818:                             }
 1819:                             $cell{$item} .= '</span> ';
 1820:                             if ($option eq 'autolimit') {
 1821:                                 $cell{$item} .= $titles{'unlimited'};
 1822:                             }
 1823:                         }
 1824:                     } elsif ($context eq 'requestauthor') {
 1825:                         my $curroption;
 1826:                         if (ref($settings) eq 'HASH') {
 1827:                             $curroption = $settings->{$type};
 1828:                         }
 1829:                         if (!$curroption) {
 1830:                             $curroption = 'norequest';
 1831:                         }
 1832:                         foreach my $option (@options) {
 1833:                             my $val = $option;
 1834:                             if ($option eq 'norequest') {
 1835:                                 $val = 0;
 1836:                             }
 1837:                             my $checked = '';
 1838:                             if ($option eq $curroption) {
 1839:                                 $checked = ' checked="checked"';
 1840:                             }
 1841:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1842:                                   '<input type="radio" name="authorreq_'.$type.
 1843:                                   '" value="'.$val.'"'.$checked.' />'.
 1844:                                   $titles{$option}.'</label></span>&nbsp; ';
 1845:                         }
 1846:                     } else {
 1847:                         my $checked = 'checked="checked" ';
 1848:                         if (ref($settings) eq 'HASH') {
 1849:                             if (ref($settings->{$item}) eq 'HASH') {
 1850:                                 if ($settings->{$item}->{$type} == 0) {
 1851:                                     $checked = '';
 1852:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1853:                                     $checked =  'checked="checked" ';
 1854:                                 }
 1855:                             }
 1856:                         }
 1857:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1858:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1859:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1860:                                       '</label></span>&nbsp; ';
 1861:                     }
 1862:                 }
 1863:                 if ($context eq 'requestcourses') {
 1864:                     $datatable .= '</tr><tr>';
 1865:                     foreach my $item (@usertools) {
 1866:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1867:                     }
 1868:                     $datatable .= '</tr></table>';
 1869:                 }
 1870:                 $datatable .= '</td>';
 1871:                 unless (($context eq 'requestcourses') ||
 1872:                         ($context eq 'requestauthor')) {
 1873:                     $datatable .= 
 1874:                               '<td class="LC_right_item">'.
 1875:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1876:                               '<input type="text" name="quota_'.$type.
 1877:                               '" value="'.$currdefquota.
 1878:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1879:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1880:                               '<input type="text" name="authorquota_'.$type.
 1881:                               '" value="'.$currauthorquota.
 1882:                               '" size="5" /></span></td>';
 1883:                 }
 1884:                 $datatable .= '</tr>';
 1885:             }
 1886:         }
 1887:     }
 1888:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1889:         $defaultquota = '20';
 1890:         $authorquota = '500';
 1891:         if (ref($settings) eq 'HASH') {
 1892:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1893:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1894:             } elsif (defined($settings->{'default'})) {
 1895:                 $defaultquota = $settings->{'default'};
 1896:             }
 1897:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1898:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1899:             }
 1900:         }
 1901:     }
 1902:     $typecount ++;
 1903:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1904:     $datatable .= '<tr'.$css_class.'>'.
 1905:                   '<td>'.$othertitle.'</td>'.
 1906:                   '<td class="LC_left_item">';
 1907:     if ($context eq 'requestcourses') {
 1908:         $datatable .= '<table><tr>';
 1909:     }
 1910:     my %defcell;
 1911:     foreach my $item (@usertools) {
 1912:         if ($context eq 'requestcourses') {
 1913:             my ($curroption,$currlimit);
 1914:             if (ref($settings) eq 'HASH') {
 1915:                 if (ref($settings->{$item}) eq 'HASH') {
 1916:                     $curroption = $settings->{$item}->{'default'};
 1917:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1918:                         $currlimit = $1;
 1919:                     }
 1920:                 }
 1921:             }
 1922:             if (!$curroption) {
 1923:                 $curroption = 'norequest';
 1924:             }
 1925:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1926:             foreach my $option (@options) {
 1927:                 my $val = $option;
 1928:                 if ($option eq 'norequest') {
 1929:                     $val = 0;
 1930:                 }
 1931:                 if ($option eq 'validate') {
 1932:                     my $canvalidate = 0;
 1933:                     if (ref($validations{$item}) eq 'HASH') {
 1934:                         if ($validations{$item}{'default'}) {
 1935:                             $canvalidate = 1;
 1936:                         }
 1937:                     }
 1938:                     next if (!$canvalidate);
 1939:                 }
 1940:                 my $checked = '';
 1941:                 if ($option eq $curroption) {
 1942:                     $checked = ' checked="checked"';
 1943:                 } elsif ($option eq 'autolimit') {
 1944:                     if ($curroption =~ /^autolimit/) {
 1945:                         $checked = ' checked="checked"';
 1946:                     }
 1947:                 }
 1948:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1949:                                   '<input type="radio" name="crsreq_'.$item.
 1950:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1951:                                   $titles{$option}.'</label>';
 1952:                 if ($option eq 'autolimit') {
 1953:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1954:                                        $item.'_limit_default" size="1" '.
 1955:                                        'value="'.$currlimit.'" />';
 1956:                 }
 1957:                 $defcell{$item} .= '</span> ';
 1958:                 if ($option eq 'autolimit') {
 1959:                     $defcell{$item} .= $titles{'unlimited'};
 1960:                 }
 1961:             }
 1962:         } elsif ($context eq 'requestauthor') {
 1963:             my $curroption;
 1964:             if (ref($settings) eq 'HASH') {
 1965:                 $curroption = $settings->{'default'};
 1966:             }
 1967:             if (!$curroption) {
 1968:                 $curroption = 'norequest';
 1969:             }
 1970:             foreach my $option (@options) {
 1971:                 my $val = $option;
 1972:                 if ($option eq 'norequest') {
 1973:                     $val = 0;
 1974:                 }
 1975:                 my $checked = '';
 1976:                 if ($option eq $curroption) {
 1977:                     $checked = ' checked="checked"';
 1978:                 }
 1979:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1980:                               '<input type="radio" name="authorreq_default"'.
 1981:                               ' value="'.$val.'"'.$checked.' />'.
 1982:                               $titles{$option}.'</label></span>&nbsp; ';
 1983:             }
 1984:         } else {
 1985:             my $checked = 'checked="checked" ';
 1986:             if (ref($settings) eq 'HASH') {
 1987:                 if (ref($settings->{$item}) eq 'HASH') {
 1988:                     if ($settings->{$item}->{'default'} == 0) {
 1989:                         $checked = '';
 1990:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1991:                         $checked = 'checked="checked" ';
 1992:                     }
 1993:                 }
 1994:             }
 1995:             $datatable .= '<span class="LC_nobreak"><label>'.
 1996:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1997:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1998:                           '</label></span>&nbsp; ';
 1999:         }
 2000:     }
 2001:     if ($context eq 'requestcourses') {
 2002:         $datatable .= '</tr><tr>';
 2003:         foreach my $item (@usertools) {
 2004:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2005:         }
 2006:         $datatable .= '</tr></table>';
 2007:     }
 2008:     $datatable .= '</td>';
 2009:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2010:         $datatable .= '<td class="LC_right_item">'.
 2011:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2012:                       '<input type="text" name="defaultquota" value="'.
 2013:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2014:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2015:                       '<input type="text" name="authorquota" value="'.
 2016:                       $authorquota.'" size="5" /></span></td>';
 2017:     }
 2018:     $datatable .= '</tr>';
 2019:     $typecount ++;
 2020:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2021:     $datatable .= '<tr'.$css_class.'>'.
 2022:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2023:     if ($context eq 'requestcourses') {
 2024:         $datatable .= &mt('(overrides affiliation, if set)').
 2025:                       '</td>'.
 2026:                       '<td class="LC_left_item">'.
 2027:                       '<table><tr>';
 2028:     } else {
 2029:         $datatable .= &mt('(overrides affiliation, if checked)').
 2030:                       '</td>'.
 2031:                       '<td class="LC_left_item" colspan="2">'.
 2032:                       '<br />';
 2033:     }
 2034:     my %advcell;
 2035:     foreach my $item (@usertools) {
 2036:         if ($context eq 'requestcourses') {
 2037:             my ($curroption,$currlimit);
 2038:             if (ref($settings) eq 'HASH') {
 2039:                 if (ref($settings->{$item}) eq 'HASH') {
 2040:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2041:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2042:                         $currlimit = $1;
 2043:                     }
 2044:                 }
 2045:             }
 2046:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2047:             my $checked = '';
 2048:             if ($curroption eq '') {
 2049:                 $checked = ' checked="checked"';
 2050:             }
 2051:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2052:                                '<input type="radio" name="crsreq_'.$item.
 2053:                                '__LC_adv" value=""'.$checked.' />'.
 2054:                                &mt('No override set').'</label></span>&nbsp; ';
 2055:             foreach my $option (@options) {
 2056:                 my $val = $option;
 2057:                 if ($option eq 'norequest') {
 2058:                     $val = 0;
 2059:                 }
 2060:                 if ($option eq 'validate') {
 2061:                     my $canvalidate = 0;
 2062:                     if (ref($validations{$item}) eq 'HASH') {
 2063:                         if ($validations{$item}{'_LC_adv'}) {
 2064:                             $canvalidate = 1;
 2065:                         }
 2066:                     }
 2067:                     next if (!$canvalidate);
 2068:                 }
 2069:                 my $checked = '';
 2070:                 if ($val eq $curroption) {
 2071:                     $checked = ' checked="checked"';
 2072:                 } elsif ($option eq 'autolimit') {
 2073:                     if ($curroption =~ /^autolimit/) {
 2074:                         $checked = ' checked="checked"';
 2075:                     }
 2076:                 }
 2077:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2078:                                   '<input type="radio" name="crsreq_'.$item.
 2079:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2080:                                   $titles{$option}.'</label>';
 2081:                 if ($option eq 'autolimit') {
 2082:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2083:                                        $item.'_limit__LC_adv" size="1" '.
 2084:                                        'value="'.$currlimit.'" />';
 2085:                 }
 2086:                 $advcell{$item} .= '</span> ';
 2087:                 if ($option eq 'autolimit') {
 2088:                     $advcell{$item} .= $titles{'unlimited'};
 2089:                 }
 2090:             }
 2091:         } elsif ($context eq 'requestauthor') {
 2092:             my $curroption;
 2093:             if (ref($settings) eq 'HASH') {
 2094:                 $curroption = $settings->{'_LC_adv'};
 2095:             }
 2096:             my $checked = '';
 2097:             if ($curroption eq '') {
 2098:                 $checked = ' checked="checked"';
 2099:             }
 2100:             $datatable .= '<span class="LC_nobreak"><label>'.
 2101:                           '<input type="radio" name="authorreq__LC_adv"'.
 2102:                           ' value=""'.$checked.' />'.
 2103:                           &mt('No override set').'</label></span>&nbsp; ';
 2104:             foreach my $option (@options) {
 2105:                 my $val = $option;
 2106:                 if ($option eq 'norequest') {
 2107:                     $val = 0;
 2108:                 }
 2109:                 my $checked = '';
 2110:                 if ($val eq $curroption) {
 2111:                     $checked = ' checked="checked"';
 2112:                 }
 2113:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2114:                               '<input type="radio" name="authorreq__LC_adv"'.
 2115:                               ' value="'.$val.'"'.$checked.' />'.
 2116:                               $titles{$option}.'</label></span>&nbsp; ';
 2117:             }
 2118:         } else {
 2119:             my $checked = 'checked="checked" ';
 2120:             if (ref($settings) eq 'HASH') {
 2121:                 if (ref($settings->{$item}) eq 'HASH') {
 2122:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2123:                         $checked = '';
 2124:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2125:                         $checked = 'checked="checked" ';
 2126:                     }
 2127:                 }
 2128:             }
 2129:             $datatable .= '<span class="LC_nobreak"><label>'.
 2130:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2131:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2132:                           '</label></span>&nbsp; ';
 2133:         }
 2134:     }
 2135:     if ($context eq 'requestcourses') {
 2136:         $datatable .= '</tr><tr>';
 2137:         foreach my $item (@usertools) {
 2138:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2139:         }
 2140:         $datatable .= '</tr></table>';
 2141:     }
 2142:     $datatable .= '</td></tr>';
 2143:     $$rowtotal += $typecount;
 2144:     return $datatable;
 2145: }
 2146: 
 2147: sub print_requestmail {
 2148:     my ($dom,$action,$settings,$rowtotal) = @_;
 2149:     my ($now,$datatable,%currapp);
 2150:     $now = time;
 2151:     if (ref($settings) eq 'HASH') {
 2152:         if (ref($settings->{'notify'}) eq 'HASH') {
 2153:             if ($settings->{'notify'}{'approval'} ne '') {
 2154:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2155:             }
 2156:         }
 2157:     }
 2158:     my $numinrow = 2;
 2159:     my $css_class;
 2160:     $css_class = ($$rowtotal%2? ' class="LC_odd_row"':'');
 2161:     my $text;
 2162:     if ($action eq 'requestcourses') {
 2163:         $text = &mt('Receive notification of course requests requiring approval');
 2164:     } elsif ($action eq 'requestauthor') {
 2165:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2166:     } else {
 2167:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2168:     }
 2169:     $datatable = '<tr'.$css_class.'>'.
 2170:                  ' <td>'.$text.'</td>'.
 2171:                  ' <td class="LC_left_item">';
 2172:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2173:                                                  $action.'notifyapproval',%currapp);
 2174:     if ($numdc > 0) {
 2175:         $datatable .= $table;
 2176:     } else {
 2177:         $datatable .= &mt('There are no active Domain Coordinators');
 2178:     }
 2179:     $datatable .='</td></tr>';
 2180:     return $datatable;
 2181: }
 2182: 
 2183: sub print_studentcode {
 2184:     my ($settings,$rowtotal) = @_;
 2185:     my $rownum = 0; 
 2186:     my ($output,%current);
 2187:     my @crstypes = ('official','unofficial','community','textbook');
 2188:     if (ref($settings) eq 'HASH') {
 2189:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2190:             foreach my $type (@crstypes) {
 2191:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2192:             }
 2193:         }
 2194:     }
 2195:     $output .= '<tr>'.
 2196:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2197:                '<td class="LC_left_item">';
 2198:     foreach my $type (@crstypes) {
 2199:         my $check = ' ';
 2200:         if ($current{$type}) {
 2201:             $check = ' checked="checked" ';
 2202:         }
 2203:         $output .= '<span class="LC_nobreak"><label>'.
 2204:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2205:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2206:     }
 2207:     $output .= '</td></tr>';
 2208:     $$rowtotal ++;
 2209:     return $output;
 2210: }
 2211: 
 2212: sub print_textbookcourses {
 2213:     my ($dom,$type,$settings,$rowtotal) = @_;
 2214:     my $rownum = 0;
 2215:     my $css_class;
 2216:     my $itemcount = 1;
 2217:     my $maxnum = 0;
 2218:     my $bookshash;
 2219:     if (ref($settings) eq 'HASH') {
 2220:         $bookshash = $settings->{$type};
 2221:     }
 2222:     my %ordered;
 2223:     if (ref($bookshash) eq 'HASH') {
 2224:         foreach my $item (keys(%{$bookshash})) {
 2225:             if (ref($bookshash->{$item}) eq 'HASH') {
 2226:                 my $num = $bookshash->{$item}{'order'};
 2227:                 $ordered{$num} = $item;
 2228:             }
 2229:         }
 2230:     }
 2231:     my $confname = $dom.'-domainconfig';
 2232:     my $switchserver = &check_switchserver($dom,$confname);
 2233:     my $maxnum = scalar(keys(%ordered));
 2234:     my $datatable;
 2235:     if (keys(%ordered)) {
 2236:         my @items = sort { $a <=> $b } keys(%ordered);
 2237:         for (my $i=0; $i<@items; $i++) {
 2238:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2239:             my $key = $ordered{$items[$i]};
 2240:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2241:             my $coursetitle = $coursehash{'description'};
 2242:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2243:             if (ref($bookshash->{$key}) eq 'HASH') {
 2244:                 $subject = $bookshash->{$key}->{'subject'};
 2245:                 $title = $bookshash->{$key}->{'title'};
 2246:                 if ($type eq 'textbooks') {
 2247:                     $publisher = $bookshash->{$key}->{'publisher'};
 2248:                     $author = $bookshash->{$key}->{'author'};
 2249:                     $image = $bookshash->{$key}->{'image'};
 2250:                     if ($image ne '') {
 2251:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2252:                         my $imagethumb = "$path/tn-".$imagefile;
 2253:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2254:                     }
 2255:                 }
 2256:             }
 2257:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2258:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2259:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2260:             for (my $k=0; $k<=$maxnum; $k++) {
 2261:                 my $vpos = $k+1;
 2262:                 my $selstr;
 2263:                 if ($k == $i) {
 2264:                     $selstr = ' selected="selected" ';
 2265:                 }
 2266:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2267:             }
 2268:             $datatable .= '</select>'.('&nbsp;'x2).
 2269:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2270:                 &mt('Delete?').'</label></span></td>'.
 2271:                 '<td colspan="2">'.
 2272:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2273:                 ('&nbsp;'x2).
 2274:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2275:             if ($type eq 'textbooks') {
 2276:                 $datatable .= ('&nbsp;'x2).
 2277:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2278:                               ('&nbsp;'x2).
 2279:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2280:                               ('&nbsp;'x2).
 2281:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2282:                 if ($image) {
 2283:                     $datatable .= '<span class="LC_nobreak">'.
 2284:                                   $imgsrc.
 2285:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2286:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2287:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2288:                 }
 2289:                 if ($switchserver) {
 2290:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2291:                 } else {
 2292:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2293:                 }
 2294:             }
 2295:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2296:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2297:                           $coursetitle.'</span></td></tr>'."\n";
 2298:             $itemcount ++;
 2299:         }
 2300:     }
 2301:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2302:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2303:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2304:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2305:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2306:     for (my $k=0; $k<$maxnum+1; $k++) {
 2307:         my $vpos = $k+1;
 2308:         my $selstr;
 2309:         if ($k == $maxnum) {
 2310:             $selstr = ' selected="selected" ';
 2311:         }
 2312:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2313:     }
 2314:     $datatable .= '</select>&nbsp;'."\n".
 2315:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2316:                   '<td colspan="2">'.
 2317:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2318:                   ('&nbsp;'x2).
 2319:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2320:                   ('&nbsp;'x2);
 2321:     if ($type eq 'textbooks') {
 2322:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2323:                       ('&nbsp;'x2).
 2324:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2325:                       ('&nbsp;'x2).
 2326:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2327:         if ($switchserver) {
 2328:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2329:         } else {
 2330:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2331:         }
 2332:     }
 2333:     $datatable .= '</span>'."\n".
 2334:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2335:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2336:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2337:                   &Apache::loncommon::selectcourse_link
 2338:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course');
 2339:                   '</span></td>'."\n".
 2340:                   '</tr>'."\n";
 2341:     $itemcount ++;
 2342:     return $datatable;
 2343: }
 2344: 
 2345: sub textbookcourses_javascript {
 2346:     my ($settings) = @_;
 2347:     return unless(ref($settings) eq 'HASH');
 2348:     my (%ordered,%total,%jstext);
 2349:     foreach my $type ('textbooks','templates') {
 2350:         $total{$type} = 0;
 2351:         if (ref($settings->{$type}) eq 'HASH') {
 2352:             foreach my $item (keys(%{$settings->{$type}})) {
 2353:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2354:                     my $num = $settings->{$type}->{$item}{'order'};
 2355:                     $ordered{$type}{$num} = $item;
 2356:                 }
 2357:             }
 2358:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2359:         }
 2360:         my @jsarray = ();
 2361:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2362:             push(@jsarray,$ordered{$type}{$item});
 2363:         }
 2364:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2365:     }
 2366:     return <<"ENDSCRIPT";
 2367: <script type="text/javascript">
 2368: // <![CDATA[
 2369: function reorderBooks(form,item,caller) {
 2370:     var changedVal;
 2371: $jstext{'textbooks'};
 2372: $jstext{'templates'};
 2373:     var newpos;
 2374:     var maxh;
 2375:     if (caller == 'textbooks') {  
 2376:         newpos = 'textbooks_addbook_pos';
 2377:         maxh = 1 + $total{'textbooks'};
 2378:     } else {
 2379:         newpos = 'templates_addbook_pos';
 2380:         maxh = 1 + $total{'templates'};
 2381:     }
 2382:     var current = new Array;
 2383:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2384:     if (item == newpos) {
 2385:         changedVal = newitemVal;
 2386:     } else {
 2387:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2388:         current[newitemVal] = newpos;
 2389:     }
 2390:     if (caller == 'textbooks') {
 2391:         for (var i=0; i<textbooks.length; i++) {
 2392:             var elementName = 'textbooks_'+textbooks[i];
 2393:             if (elementName != item) {
 2394:                 if (form.elements[elementName]) {
 2395:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2396:                     current[currVal] = elementName;
 2397:                 }
 2398:             }
 2399:         }
 2400:     }
 2401:     if (caller == 'templates') {
 2402:         for (var i=0; i<templates.length; i++) {
 2403:             var elementName = 'templates_'+templates[i];
 2404:             if (elementName != item) {
 2405:                 if (form.elements[elementName]) {
 2406:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2407:                     current[currVal] = elementName;
 2408:                 }
 2409:             }
 2410:         }
 2411:     }
 2412:     var oldVal;
 2413:     for (var j=0; j<maxh; j++) {
 2414:         if (current[j] == undefined) {
 2415:             oldVal = j;
 2416:         }
 2417:     }
 2418:     if (oldVal < changedVal) {
 2419:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2420:            var elementName = current[k];
 2421:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2422:         }
 2423:     } else {
 2424:         for (var k=changedVal; k<oldVal; k++) {
 2425:             var elementName = current[k];
 2426:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2427:         }
 2428:     }
 2429:     return;
 2430: }
 2431: 
 2432: // ]]>
 2433: </script>
 2434: 
 2435: ENDSCRIPT
 2436: }
 2437: 
 2438: sub print_autoenroll {
 2439:     my ($dom,$settings,$rowtotal) = @_;
 2440:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2441:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,$failsafe);
 2442:     if (ref($settings) eq 'HASH') {
 2443:         if (exists($settings->{'run'})) {
 2444:             if ($settings->{'run'} eq '0') {
 2445:                 $runoff = ' checked="checked" ';
 2446:                 $runon = ' ';
 2447:             } else {
 2448:                 $runon = ' checked="checked" ';
 2449:                 $runoff = ' ';
 2450:             }
 2451:         } else {
 2452:             if ($autorun) {
 2453:                 $runon = ' checked="checked" ';
 2454:                 $runoff = ' ';
 2455:             } else {
 2456:                 $runoff = ' checked="checked" ';
 2457:                 $runon = ' ';
 2458:             }
 2459:         }
 2460:         if (exists($settings->{'co-owners'})) {
 2461:             if ($settings->{'co-owners'} eq '0') {
 2462:                 $coownersoff = ' checked="checked" ';
 2463:                 $coownerson = ' ';
 2464:             } else {
 2465:                 $coownerson = ' checked="checked" ';
 2466:                 $coownersoff = ' ';
 2467:             }
 2468:         } else {
 2469:             $coownersoff = ' checked="checked" ';
 2470:             $coownerson = ' ';
 2471:         }
 2472:         if (exists($settings->{'sender_domain'})) {
 2473:             $defdom = $settings->{'sender_domain'};
 2474:         }
 2475:         if (exists($settings->{'autofailsafe'})) {
 2476:             $failsafe = $settings->{'autofailsafe'};
 2477:         }
 2478:     } else {
 2479:         if ($autorun) {
 2480:             $runon = ' checked="checked" ';
 2481:             $runoff = ' ';
 2482:         } else {
 2483:             $runoff = ' checked="checked" ';
 2484:             $runon = ' ';
 2485:         }
 2486:     }
 2487:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2488:     my $notif_sender;
 2489:     if (ref($settings) eq 'HASH') {
 2490:         $notif_sender = $settings->{'sender_uname'};
 2491:     }
 2492:     my $datatable='<tr class="LC_odd_row">'.
 2493:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2494:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2495:                   '<input type="radio" name="autoenroll_run"'.
 2496:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2497:                   '<label><input type="radio" name="autoenroll_run"'.
 2498:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2499:                   '</tr><tr>'.
 2500:                   '<td>'.&mt('Notification messages - sender').
 2501:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2502:                   &mt('username').':&nbsp;'.
 2503:                   '<input type="text" name="sender_uname" value="'.
 2504:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2505:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2506:                   '<tr class="LC_odd_row">'.
 2507:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2508:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2509:                   '<input type="radio" name="autoassign_coowners"'.
 2510:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2511:                   '<label><input type="radio" name="autoassign_coowners"'.
 2512:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2513:                   '</tr><tr>'.
 2514:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 2515:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2516:                   '<input type="text" name="autoenroll_failsafe"'.
 2517:                   ' value="'.$failsafe.'" size="4" /></td></tr>';
 2518:     $$rowtotal += 4;
 2519:     return $datatable;
 2520: }
 2521: 
 2522: sub print_autoupdate {
 2523:     my ($position,$dom,$settings,$rowtotal) = @_;
 2524:     my $datatable;
 2525:     if ($position eq 'top') {
 2526:         my $updateon = ' ';
 2527:         my $updateoff = ' checked="checked" ';
 2528:         my $classlistson = ' ';
 2529:         my $classlistsoff = ' checked="checked" ';
 2530:         if (ref($settings) eq 'HASH') {
 2531:             if ($settings->{'run'} eq '1') {
 2532:                 $updateon = $updateoff;
 2533:                 $updateoff = ' ';
 2534:             }
 2535:             if ($settings->{'classlists'} eq '1') {
 2536:                 $classlistson = $classlistsoff;
 2537:                 $classlistsoff = ' ';
 2538:             }
 2539:         }
 2540:         my %title = (
 2541:                    run => 'Auto-update active?',
 2542:                    classlists => 'Update information in classlists?',
 2543:                     );
 2544:         $datatable = '<tr class="LC_odd_row">'. 
 2545:                   '<td>'.&mt($title{'run'}).'</td>'.
 2546:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2547:                   '<input type="radio" name="autoupdate_run"'.
 2548:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2549:                   '<label><input type="radio" name="autoupdate_run"'.
 2550:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2551:                   '</tr><tr>'.
 2552:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2553:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2554:                   '<label><input type="radio" name="classlists"'.
 2555:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2556:                   '<label><input type="radio" name="classlists"'.
 2557:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2558:                   '</tr>';
 2559:         $$rowtotal += 2;
 2560:     } elsif ($position eq 'middle') {
 2561:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2562:         my $numinrow = 3;
 2563:         my $locknamesettings;
 2564:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2565:                                      $dom,$numinrow,$othertitle,
 2566:                                     'lockablenames');
 2567:         $$rowtotal ++;
 2568:     } else {
 2569:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2570:         my @fields = ('lastname','firstname','middlename','generation',
 2571:                       'permanentemail','id');
 2572:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2573:         my $numrows = 0;
 2574:         if (ref($types) eq 'ARRAY') {
 2575:             if (@{$types} > 0) {
 2576:                 $datatable = 
 2577:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2578:                                          \@fields,$types,\$numrows);
 2579:                     $$rowtotal += @{$types}; 
 2580:             }
 2581:         }
 2582:         $datatable .= 
 2583:             &usertype_update_row($settings,{'default' => $othertitle},
 2584:                                  \%fieldtitles,\@fields,['default'],
 2585:                                  \$numrows);
 2586:         $$rowtotal ++;     
 2587:     }
 2588:     return $datatable;
 2589: }
 2590: 
 2591: sub print_autocreate {
 2592:     my ($dom,$settings,$rowtotal) = @_;
 2593:     my (%createon,%createoff,%currhash);
 2594:     my @types = ('xml','req');
 2595:     if (ref($settings) eq 'HASH') {
 2596:         foreach my $item (@types) {
 2597:             $createoff{$item} = ' checked="checked" ';
 2598:             $createon{$item} = ' ';
 2599:             if (exists($settings->{$item})) {
 2600:                 if ($settings->{$item}) {
 2601:                     $createon{$item} = ' checked="checked" ';
 2602:                     $createoff{$item} = ' ';
 2603:                 }
 2604:             }
 2605:         }
 2606:         if ($settings->{'xmldc'} ne '') {
 2607:             $currhash{$settings->{'xmldc'}} = 1;
 2608:         }
 2609:     } else {
 2610:         foreach my $item (@types) {
 2611:             $createoff{$item} = ' checked="checked" ';
 2612:             $createon{$item} = ' ';
 2613:         }
 2614:     }
 2615:     $$rowtotal += 2;
 2616:     my $numinrow = 2;
 2617:     my $datatable='<tr class="LC_odd_row">'.
 2618:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2619:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2620:                   '<input type="radio" name="autocreate_xml"'.
 2621:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2622:                   '<label><input type="radio" name="autocreate_xml"'.
 2623:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2624:                   '</td></tr><tr>'.
 2625:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2626:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2627:                   '<input type="radio" name="autocreate_req"'.
 2628:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2629:                   '<label><input type="radio" name="autocreate_req"'.
 2630:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2631:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2632:                                                    'autocreate_xmldc',%currhash);
 2633:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 2634:     if ($numdc > 1) {
 2635:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 2636:                       '</td><td class="LC_left_item">';
 2637:     } else {
 2638:         $datatable .= &mt('Course creation processed as:').
 2639:                       '</td><td class="LC_right_item">';
 2640:     }
 2641:     $datatable .= $dctable.'</td></tr>';
 2642:     $$rowtotal += $rows;
 2643:     return $datatable;
 2644: }
 2645: 
 2646: sub print_directorysrch {
 2647:     my ($position,$dom,$settings,$rowtotal) = @_;
 2648:     my $datatable;
 2649:     if ($position eq 'top') {
 2650:         my $instsrchon = ' ';
 2651:         my $instsrchoff = ' checked="checked" ';
 2652:         my ($exacton,$containson,$beginson);
 2653:         my $instlocalon = ' ';
 2654:         my $instlocaloff = ' checked="checked" ';
 2655:         if (ref($settings) eq 'HASH') {
 2656:             if ($settings->{'available'} eq '1') {
 2657:                 $instsrchon = $instsrchoff;
 2658:                 $instsrchoff = ' ';
 2659:             }
 2660:             if ($settings->{'localonly'} eq '1') {
 2661:                 $instlocalon = $instlocaloff;
 2662:                 $instlocaloff = ' ';
 2663:             }
 2664:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2665:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 2666:                     if ($type eq 'exact') {
 2667:                         $exacton = ' checked="checked" ';
 2668:                     } elsif ($type eq 'contains') {
 2669:                         $containson = ' checked="checked" ';
 2670:                     } elsif ($type eq 'begins') {
 2671:                         $beginson = ' checked="checked" ';
 2672:                     }
 2673:                 }
 2674:             } else {
 2675:                 if ($settings->{'searchtypes'} eq 'exact') {
 2676:                     $exacton = ' checked="checked" ';
 2677:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 2678:                     $containson = ' checked="checked" ';
 2679:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 2680:                     $exacton = ' checked="checked" ';
 2681:                     $containson = ' checked="checked" ';
 2682:                 }
 2683:             }
 2684:         }
 2685:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2686:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2687: 
 2688:         my $numinrow = 4;
 2689:         my $cansrchrow = 0;
 2690:         $datatable='<tr class="LC_odd_row">'.
 2691:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 2692:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2693:                    '<input type="radio" name="dirsrch_available"'.
 2694:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2695:                    '<label><input type="radio" name="dirsrch_available"'.
 2696:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2697:                    '</tr><tr>'.
 2698:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 2699:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2700:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 2701:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2702:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 2703:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2704:                    '</tr>';
 2705:         $$rowtotal += 2;
 2706:         if (ref($usertypes) eq 'HASH') {
 2707:             if (keys(%{$usertypes}) > 0) {
 2708:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2709:                                              $numinrow,$othertitle,'cansearch');
 2710:                 $cansrchrow = 1;
 2711:             }
 2712:         }
 2713:         if ($cansrchrow) {
 2714:             $$rowtotal ++;
 2715:             $datatable .= '<tr>';
 2716:         } else {
 2717:             $datatable .= '<tr class="LC_odd_row">';
 2718:         }
 2719:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2720:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2721:         foreach my $title (@{$titleorder}) {
 2722:             if (defined($searchtitles->{$title})) {
 2723:                 my $check = ' ';
 2724:                 if (ref($settings) eq 'HASH') {
 2725:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2726:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2727:                             $check = ' checked="checked" ';
 2728:                         }
 2729:                     }
 2730:                 }
 2731:                 $datatable .= '<td class="LC_left_item">'.
 2732:                               '<span class="LC_nobreak"><label>'.
 2733:                               '<input type="checkbox" name="searchby" '.
 2734:                               'value="'.$title.'"'.$check.'/>'.
 2735:                               $searchtitles->{$title}.'</label></span></td>';
 2736:             }
 2737:         }
 2738:         $datatable .= '</tr></table></td></tr>';
 2739:         $$rowtotal ++;
 2740:         if ($cansrchrow) {
 2741:             $datatable .= '<tr class="LC_odd_row">';
 2742:         } else {
 2743:             $datatable .= '<tr>';
 2744:         }
 2745:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2746:                       '<td class="LC_left_item" colspan="2">'.
 2747:                       '<span class="LC_nobreak"><label>'.
 2748:                       '<input type="checkbox" name="searchtypes" '.
 2749:                       $exacton.' value="exact" />'.&mt('Exact match').
 2750:                       '</label>&nbsp;'.
 2751:                       '<label><input type="checkbox" name="searchtypes" '.
 2752:                       $beginson.' value="begins" />'.&mt('Begins with').
 2753:                       '</label>&nbsp;'.
 2754:                       '<label><input type="checkbox" name="searchtypes" '.
 2755:                       $containson.' value="contains" />'.&mt('Contains').
 2756:                       '</label></span></td></tr>';
 2757:         $$rowtotal ++;
 2758:     } else {
 2759:         my $domsrchon = ' checked="checked" ';
 2760:         my $domsrchoff = ' ';
 2761:         my $domlocalon = ' ';
 2762:         my $domlocaloff = ' checked="checked" ';
 2763:         if (ref($settings) eq 'HASH') {
 2764:             if ($settings->{'lclocalonly'} eq '1') {
 2765:                 $domlocalon = $domlocaloff;
 2766:                 $domlocaloff = ' ';
 2767:             }
 2768:             if ($settings->{'lcavailable'} eq '0') {
 2769:                 $domsrchoff = $domsrchon;
 2770:                 $domsrchon = ' ';
 2771:             }
 2772:         }
 2773:         $datatable='<tr class="LC_odd_row">'.
 2774:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 2775:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2776:                       '<input type="radio" name="dirsrch_domavailable"'.
 2777:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2778:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 2779:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2780:                       '</tr><tr>'.
 2781:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 2782:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2783:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 2784:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2785:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 2786:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2787:                       '</tr>';
 2788:         $$rowtotal += 2;
 2789:     }
 2790:     return $datatable;
 2791: }
 2792: 
 2793: sub print_contacts {
 2794:     my ($position,$dom,$settings,$rowtotal) = @_;
 2795:     my $datatable;
 2796:     my @contacts = ('adminemail','supportemail');
 2797:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 2798:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings);
 2799:     if ($position eq 'top') {
 2800:         if (ref($settings) eq 'HASH') {
 2801:             foreach my $item (@contacts) {
 2802:                 if (exists($settings->{$item})) {
 2803:                     $to{$item} = $settings->{$item};
 2804:                 }
 2805:             }
 2806:         }
 2807:     } elsif ($position eq 'middle') {
 2808:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 2809:                      'updatesmail','idconflictsmail');
 2810:         foreach my $type (@mailings) {
 2811:             $otheremails{$type} = '';
 2812:         }
 2813:     } else {
 2814:         @mailings = ('helpdeskmail','otherdomsmail');
 2815:         foreach my $type (@mailings) {
 2816:             $otheremails{$type} = '';
 2817:         }
 2818:         $bccemails{'helpdeskmail'} = '';
 2819:         $bccemails{'otherdomsmail'} = '';
 2820:         $includestr{'helpdeskmail'} = '';
 2821:         $includestr{'otherdomsmail'} = '';
 2822:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 2823:     }
 2824:     if (ref($settings) eq 'HASH') {
 2825:         unless ($position eq 'top') {
 2826:             foreach my $type (@mailings) {
 2827:                 if (exists($settings->{$type})) {
 2828:                     if (ref($settings->{$type}) eq 'HASH') {
 2829:                         foreach my $item (@contacts) {
 2830:                             if ($settings->{$type}{$item}) {
 2831:                                 $checked{$type}{$item} = ' checked="checked" ';
 2832:                             }
 2833:                         }
 2834:                         $otheremails{$type} = $settings->{$type}{'others'};
 2835:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 2836:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 2837:                             if ($settings->{$type}{'include'} ne '') {
 2838:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 2839:                                 $includestr{$type} = &unescape($includestr{$type});
 2840:                             }
 2841:                         }
 2842:                     }
 2843:                 } elsif ($type eq 'lonstatusmail') {
 2844:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2845:                 }
 2846:             }
 2847:         }
 2848:         if ($position eq 'bottom') {
 2849:             foreach my $type (@mailings) {
 2850:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 2851:                 if ($settings->{$type}{'include'} ne '') {
 2852:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 2853:                     $includestr{$type} = &unescape($includestr{$type});
 2854:                 }
 2855:             }
 2856:             if (ref($settings->{'helpform'}) eq 'HASH') {
 2857:                 if (ref($fields) eq 'ARRAY') {
 2858:                     foreach my $field (@{$fields}) {
 2859:                         $currfield{$field} = $settings->{'helpform'}{$field};
 2860:                     }
 2861:                 }
 2862:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 2863:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 2864:                 } else {
 2865:                     $maxsize = '1.0';
 2866:                 }
 2867:             } else {
 2868:                 if (ref($fields) eq 'ARRAY') {
 2869:                     foreach my $field (@{$fields}) {
 2870:                         $currfield{$field} = 'yes';
 2871:                     }
 2872:                 }
 2873:                 $maxsize = '1.0';
 2874:             }
 2875:         }
 2876:     } else {
 2877:         if ($position eq 'top') {
 2878:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2879:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2880:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2881:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2882:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2883:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2884:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2885:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 2886:         } elsif ($position eq 'bottom') {
 2887:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2888:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 2889:             if (ref($fields) eq 'ARRAY') {
 2890:                 foreach my $field (@{$fields}) {
 2891:                     $currfield{$field} = 'yes';
 2892:                 }
 2893:             }
 2894:             $maxsize = '1.0';
 2895:         }
 2896:     }
 2897:     my ($titles,$short_titles) = &contact_titles();
 2898:     my $rownum = 0;
 2899:     my $css_class;
 2900:     if ($position eq 'top') {
 2901:         foreach my $item (@contacts) {
 2902:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 2903:             $datatable .= '<tr'.$css_class.'>'. 
 2904:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 2905:                           '</span></td><td class="LC_right_item">'.
 2906:                           '<input type="text" name="'.$item.'" value="'.
 2907:                           $to{$item}.'" /></td></tr>';
 2908:             $rownum ++;
 2909:         }
 2910:     } else {
 2911:         foreach my $type (@mailings) {
 2912:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 2913:             $datatable .= '<tr'.$css_class.'>'.
 2914:                           '<td><span class="LC_nobreak">'.
 2915:                           $titles->{$type}.': </span></td>'.
 2916:                           '<td class="LC_left_item">';
 2917:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 2918:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 2919:             }
 2920:             $datatable .= '<span class="LC_nobreak">';
 2921:             foreach my $item (@contacts) {
 2922:                 $datatable .= '<label>'.
 2923:                               '<input type="checkbox" name="'.$type.'"'.
 2924:                               $checked{$type}{$item}.
 2925:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 2926:                               '</label>&nbsp;';
 2927:             }
 2928:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2929:                           '<input type="text" name="'.$type.'_others" '.
 2930:                           'value="'.$otheremails{$type}.'"  />';
 2931:             my %locchecked;
 2932:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 2933:                 foreach my $loc ('s','b') {
 2934:                     if ($includeloc{$type} eq $loc) {
 2935:                         $locchecked{$loc} = ' checked="checked"';
 2936:                         last;
 2937:                     }
 2938:                 }
 2939:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2940:                               '<input type="text" name="'.$type.'_bcc" '.
 2941:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 2942:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 2943:                               &mt('Text automatically added to e-mail:').' '.
 2944:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br >'.
 2945:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 2946:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 2947:                               ('&nbsp;'x2).
 2948:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 2949:                               '</span></fieldset>';
 2950:             }
 2951:             $datatable .= '</td></tr>'."\n";
 2952:             $rownum ++;
 2953:         }
 2954:     }
 2955:     if ($position eq 'middle') {
 2956:         my %choices;
 2957:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 2958:                                        &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2959:                                        &mt('LON-CAPA core group - MSU'),600,500));
 2960:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 2961:                                         &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2962:                                         &mt('LON-CAPA core group - MSU'),600,500));
 2963:         my @toggles = ('reporterrors','reportupdates');
 2964:         my %defaultchecked = ('reporterrors'  => 'on',
 2965:                               'reportupdates' => 'on');
 2966:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2967:                                                    \%choices,$rownum);
 2968:         $datatable .= $reports;
 2969:     } elsif ($position eq 'bottom') {
 2970:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2971:         $datatable .= '<tr'.$css_class.'>'.
 2972:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 2973:                       &mt('(e-mail, subject, and description always shown)').
 2974:                       '</td><td class="LC_left_item">';
 2975:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 2976:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 2977:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 2978:             foreach my $field (@{$fields}) {
 2979:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 2980:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 2981:                     $datatable .= ' '.&mt('(logged-in users)');
 2982:                 }
 2983:                 $datatable .='</td><td>';
 2984:                 my $clickaction;
 2985:                 if ($field eq 'screenshot') {
 2986:                     $clickaction = ' onclick="screenshotSize(this);"';
 2987:                 }
 2988:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 2989:                     foreach my $option (@{$possoptions->{$field}}) {
 2990:                         my $checked;
 2991:                         if ($currfield{$field} eq $option) {
 2992:                             $checked = ' checked="checked"';
 2993:                         }
 2994:                         $datatable .= '<span class="LC_nobreak"><label>'.
 2995:                                       '<input type="radio" name="helpform_'.$field.'" '.
 2996:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 2997:                                       '</label></span>'.('&nbsp;'x2);
 2998:                     }
 2999:                 }
 3000:                 if ($field eq 'screenshot') {
 3001:                     my $display;
 3002:                     if ($currfield{$field} eq 'no') {
 3003:                         $display = ' style="display:none"';
 3004:                     }
 3005:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.' />'.
 3006:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 3007:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 3008:                 }
 3009:                 $datatable .= '</td></tr>';
 3010:             }
 3011:             $datatable .= '</table>';
 3012:         }
 3013:         $datatable .= '</td></tr>'."\n";
 3014:         $rownum ++;
 3015:     }
 3016:     $$rowtotal += $rownum;
 3017:     return $datatable;
 3018: }
 3019: 
 3020: sub contacts_javascript {
 3021:     return <<"ENDSCRIPT";
 3022: 
 3023: <script type="text/javascript">
 3024: // <![CDATA[
 3025: 
 3026: function screenshotSize(field) {
 3027:     if (document.getElementById('help_screenshotsize')) {
 3028:         if (field.value == 'no') {
 3029:             document.getElementById('help_screenshotsize').style.display="none";
 3030:         } else {
 3031:             document.getElementById('help_screenshotsize').style.display="";
 3032:         }
 3033:     }
 3034:     return;
 3035: }
 3036: 
 3037: // ]]>
 3038: </script>
 3039: 
 3040: ENDSCRIPT
 3041: }
 3042: 
 3043: sub print_helpsettings {
 3044:     my ($position,$dom,$settings,$rowtotal) = @_;
 3045:     my $confname = $dom.'-domainconfig';
 3046:     my $formname = 'display';
 3047:     my ($datatable,$itemcount);
 3048:     if ($position eq 'top') {
 3049:         $itemcount = 1;
 3050:         my (%choices,%defaultchecked,@toggles);
 3051:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 3052:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 3053:                                      &mt('LON-CAPA bug tracker'),600,500));
 3054:         %defaultchecked = ('submitbugs' => 'on');
 3055:         @toggles = ('submitbugs');
 3056:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3057:                                                      \%choices,$itemcount);
 3058:         $$rowtotal ++;
 3059:     } else {
 3060:         my $css_class;
 3061:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 3062:         my (%customroles,%ordered,%current);
 3063:         if (ref($settings->{'adhoc'}) eq 'HASH') {
 3064:             %current = %{$settings->{'adhoc'}};
 3065:         }
 3066:         my $count = 0;
 3067:         foreach my $key (sort(keys(%existing))) {
 3068:             if ($key=~/^rolesdef\_(\w+)$/) {
 3069:                 my $rolename = $1;
 3070:                 my (%privs,$order);
 3071:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 3072:                 $customroles{$rolename} = \%privs;
 3073:                 if (ref($current{$rolename}) eq 'HASH') {
 3074:                     $order = $current{$rolename}{'order'};
 3075:                 }
 3076:                 if ($order eq '') {
 3077:                     $order = $count;
 3078:                 }
 3079:                 $ordered{$order} = $rolename;
 3080:                 $count++;
 3081:             }
 3082:         }
 3083:         my $maxnum = scalar(keys(%ordered));
 3084:         my @roles_by_num = ();
 3085:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3086:             push(@roles_by_num,$item);
 3087:         }
 3088:         my $context = 'domprefs';
 3089:         my $crstype = 'Course';
 3090:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3091:         my @accesstypes = ('all','dh','da','none');
 3092:         my ($numstatustypes,@jsarray);
 3093:         if (ref($types) eq 'ARRAY') {
 3094:             if (@{$types} > 0) {
 3095:                 $numstatustypes = scalar(@{$types});
 3096:                 push(@accesstypes,'status');
 3097:                 @jsarray = ('bystatus');
 3098:             }
 3099:         }
 3100:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh'.'da']);
 3101:         if (keys(%domhelpdesk)) {
 3102:             push(@accesstypes,('inc','exc'));
 3103:             push(@jsarray,('notinc','notexc'));
 3104:         }
 3105:         my $hiddenstr = join("','",@jsarray);
 3106:         $datatable .= &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname);
 3107:         my $context = 'domprefs';
 3108:         my $crstype = 'Course';
 3109:         my $prefix = 'helproles_';
 3110:         my $add_class = 'LC_hidden';
 3111:         foreach my $num (@roles_by_num) {
 3112:             my $role = $ordered{$num};
 3113:             my ($desc,$access,@statuses);
 3114:             if (ref($current{$role}) eq 'HASH') {
 3115:                 $desc = $current{$role}{'desc'};
 3116:                 $access = $current{$role}{'access'};
 3117:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 3118:                     @statuses = @{$current{$role}{'insttypes'}};
 3119:                 }
 3120:             }
 3121:             if ($desc eq '') {
 3122:                 $desc = $role;
 3123:             }
 3124:             my $identifier = 'custhelp'.$num;
 3125:             my %full=();
 3126:             my %levels= (
 3127:                          course => {},
 3128:                          domain => {},
 3129:                          system => {},
 3130:                         );
 3131:             my %levelscurrent=(
 3132:                                course => {},
 3133:                                domain => {},
 3134:                                system => {},
 3135:                               );
 3136:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 3137:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3138:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3139:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 3140:             $datatable .= '<tr '.$css_class.'><td valign="top"><b>'.$role.'</b><br />'.
 3141:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 3142:             for (my $k=0; $k<=$maxnum; $k++) {
 3143:                 my $vpos = $k+1;
 3144:                 my $selstr;
 3145:                 if ($k == $num) {
 3146:                     $selstr = ' selected="selected" ';
 3147:                 }
 3148:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3149:             }
 3150:             $datatable .= '</select>'.('&nbsp;'x2).
 3151:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 3152:                           '</td>'.
 3153:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3154:                           &mt('Name shown to users:').
 3155:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 3156:                           '</fieldset>'.
 3157:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 3158:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 3159:                           '<fieldset>'.
 3160:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 3161:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 3162:                                                                    \%levelscurrent,$identifier,
 3163:                                                                    'LC_hidden',$prefix.$num.'_privs').
 3164:                           '</fieldset></td>';
 3165:             $itemcount ++;
 3166:         }
 3167:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3168:         my $newcust = 'custhelp'.$count;
 3169:         my (%privs,%levelscurrent);
 3170:         my %full=();
 3171:         my %levels= (
 3172:                      course => {},
 3173:                      domain => {},
 3174:                      system => {},
 3175:                     );
 3176:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 3177:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3178:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 3179:         $datatable .= '<tr '.$css_class.'><td valign="top"><span class="LC_nobreak"><label>'.
 3180:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 3181:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 3182:         for (my $k=0; $k<$maxnum+1; $k++) {
 3183:             my $vpos = $k+1;
 3184:             my $selstr;
 3185:             if ($k == $maxnum) {
 3186:                 $selstr = ' selected="selected" ';
 3187:             }
 3188:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3189:         }
 3190:         $datatable .= '</select>&nbsp;'."\n".
 3191:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 3192:                       '</label></span></td>'.
 3193:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3194:                       '<span class="LC_nobreak">'.
 3195:                       &mt('Internal name:').
 3196:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 3197:                       '</span>'.('&nbsp;'x4).
 3198:                       '<span class="LC_nobreak">'.
 3199:                       &mt('Name shown to users:').
 3200:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 3201:                       '</span></fieldset>'.
 3202:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 3203:                                              $usertypes,$types,\%domhelpdesk).
 3204:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 3205:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 3206:                                                                 \@templateroles,$newcust).
 3207:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 3208:                                                                \%levelscurrent,$newcust).
 3209:                       '</fieldset></td></tr>';
 3210:         $count ++;
 3211:         $$rowtotal += $count;
 3212:     }
 3213:     return $datatable;
 3214: }
 3215: 
 3216: sub adhocbutton {
 3217:     my ($prefix,$num,$field,$visibility) = @_;
 3218:     my %lt = &Apache::lonlocal::texthash(
 3219:                                           show => 'Show details',
 3220:                                           hide => 'Hide details',
 3221:                                         );
 3222:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 3223:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 3224:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 3225:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 3226: }
 3227: 
 3228: sub helpsettings_javascript {
 3229:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 3230:     return unless(ref($roles_by_num) eq 'ARRAY');
 3231:     my %html_js_lt = &Apache::lonlocal::texthash(
 3232:                                           show => 'Show details',
 3233:                                           hide => 'Hide details',
 3234:                                         );
 3235:     &html_escape(\%html_js_lt);
 3236:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 3237:     return <<"ENDSCRIPT";
 3238: <script type="text/javascript">
 3239: // <![CDATA[
 3240: 
 3241: function reorderHelpRoles(form,item) {
 3242:     var changedVal;
 3243: $jstext
 3244:     var newpos = 'helproles_${total}_pos';
 3245:     var maxh = 1 + $total;
 3246:     var current = new Array();
 3247:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3248:     if (item == newpos) {
 3249:         changedVal = newitemVal;
 3250:     } else {
 3251:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3252:         current[newitemVal] = newpos;
 3253:     }
 3254:     for (var i=0; i<helproles.length; i++) {
 3255:         var elementName = 'helproles_'+helproles[i]+'_pos';
 3256:         if (elementName != item) {
 3257:             if (form.elements[elementName]) {
 3258:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3259:                 current[currVal] = elementName;
 3260:             }
 3261:         }
 3262:     }
 3263:     var oldVal;
 3264:     for (var j=0; j<maxh; j++) {
 3265:         if (current[j] == undefined) {
 3266:             oldVal = j;
 3267:         }
 3268:     }
 3269:     if (oldVal < changedVal) {
 3270:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3271:            var elementName = current[k];
 3272:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3273:         }
 3274:     } else {
 3275:         for (var k=changedVal; k<oldVal; k++) {
 3276:             var elementName = current[k];
 3277:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3278:         }
 3279:     }
 3280:     return;
 3281: }
 3282: 
 3283: function helpdeskAccess(num) {
 3284:     var curraccess = null;
 3285:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 3286:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 3287:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 3288:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 3289:             }
 3290:         }
 3291:     }
 3292:     var shown = Array();
 3293:     var hidden = Array();
 3294:     if (curraccess == 'none') {
 3295:         hidden = Array('$hiddenstr');
 3296:     } else {
 3297:         if (curraccess == 'status') {
 3298:             shown = Array('bystatus');
 3299:             hidden = Array('notinc','notexc');
 3300:         } else {
 3301:             if (curraccess == 'exc') {
 3302:                 shown = Array('notexc');
 3303:                 hidden = Array('notinc','bystatus');
 3304:             }
 3305:             if (curraccess == 'inc') {
 3306:                 shown = Array('notinc');
 3307:                 hidden = Array('notexc','bystatus');
 3308:             }
 3309:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 3310:                 hidden = Array('notinc','notexc','bystatus');
 3311:             }
 3312:         }
 3313:     }
 3314:     if (hidden.length > 0) {
 3315:         for (var i=0; i<hidden.length; i++) {
 3316:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 3317:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 3318:             }
 3319:         }
 3320:     }
 3321:     if (shown.length > 0) {
 3322:         for (var i=0; i<shown.length; i++) {
 3323:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 3324:                 if (shown[i] == 'privs') {
 3325:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 3326:                 } else {
 3327:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 3328:                 }
 3329:             }
 3330:         }
 3331:     }
 3332:     return;
 3333: }
 3334: 
 3335: function toggleHelpdeskItem(num,field) {
 3336:     if (document.getElementById('helproles_'+num+'_'+field)) {
 3337:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 3338:             document.getElementById('helproles_'+num+'_'+field).className =
 3339:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 3340:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3341:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 3342:             }
 3343:         } else {
 3344:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 3345:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3346:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 3347:             }
 3348:         }
 3349:     }
 3350:     return;
 3351: }
 3352: 
 3353: // ]]>
 3354: </script>
 3355: 
 3356: ENDSCRIPT
 3357: }
 3358: 
 3359: sub helpdeskroles_access {
 3360:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 3361:         $usertypes,$types,$domhelpdesk) = @_;
 3362:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 3363:     my %lt = &Apache::lonlocal::texthash(
 3364:                     'rou'    => 'Role usage',
 3365:                     'whi'    => 'Which helpdesk personnel may use this role?',
 3366:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 3367:                     'dh'     => 'All with domain helpdesk role',
 3368:                     'da'     => 'All with domain helpdesk assistant role',
 3369:                     'none'   => 'None',
 3370:                     'status' => 'Determined based on institutional status',
 3371:                     'inc'    => 'Include all, but exclude specific personnel',
 3372:                     'exc'    => 'Exclude all, but include specific personnel',
 3373:                   );
 3374:     my %usecheck = (
 3375:                      all => ' checked="checked"',
 3376:                    );
 3377:     my %displaydiv = (
 3378:                       status => 'none',
 3379:                       inc    => 'none',
 3380:                       exc    => 'none',
 3381:                       priv   => 'block',
 3382:                      );
 3383:     my $output;
 3384:     if (ref($current) eq 'HASH') {
 3385:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 3386:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 3387:                 $usecheck{$current->{access}} = $usecheck{'all'};
 3388:                 delete($usecheck{'all'});
 3389:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 3390:                     my $access = $1;
 3391:                     $displaydiv{$access} = 'inline';
 3392:                 } elsif ($current->{access} eq 'none') {
 3393:                     $displaydiv{'priv'} = 'none';
 3394:                 }
 3395:             }
 3396:         }
 3397:     }
 3398:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 3399:               '<p>'.$lt{'whi'}.'</p>';
 3400:     foreach my $access (@{$accesstypes}) {
 3401:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 3402:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 3403:                    $lt{$access}.'</label>';
 3404:         if ($access eq 'status') {
 3405:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 3406:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 3407:                                                                  $othertitle,$usertypes,$types).
 3408:                        '</div>';
 3409:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 3410:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 3411:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3412:                        '</div>';
 3413:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 3414:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 3415:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3416:                        '</div>';
 3417:         }
 3418:         $output .= '</p>';
 3419:     }
 3420:     $output .= '</fieldset>';
 3421:     return $output;
 3422: }
 3423: 
 3424: sub radiobutton_prefs {
 3425:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 3426:         $additional,$align) = @_;
 3427:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 3428:                    (ref($choices) eq 'HASH'));
 3429: 
 3430:     my (%checkedon,%checkedoff,$datatable,$css_class);
 3431: 
 3432:     foreach my $item (@{$toggles}) {
 3433:         if ($defaultchecked->{$item} eq 'on') {
 3434:             $checkedon{$item} = ' checked="checked" ';
 3435:             $checkedoff{$item} = ' ';
 3436:         } elsif ($defaultchecked->{$item} eq 'off') {
 3437:             $checkedoff{$item} = ' checked="checked" ';
 3438:             $checkedon{$item} = ' ';
 3439:         }
 3440:     }
 3441:     if (ref($settings) eq 'HASH') {
 3442:         foreach my $item (@{$toggles}) {
 3443:             if ($settings->{$item} eq '1') {
 3444:                 $checkedon{$item} =  ' checked="checked" ';
 3445:                 $checkedoff{$item} = ' ';
 3446:             } elsif ($settings->{$item} eq '0') {
 3447:                 $checkedoff{$item} =  ' checked="checked" ';
 3448:                 $checkedon{$item} = ' ';
 3449:             }
 3450:         }
 3451:     }
 3452:     if ($onclick) {
 3453:         $onclick = ' onclick="'.$onclick.'"';
 3454:     }
 3455:     foreach my $item (@{$toggles}) {
 3456:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3457:         $datatable .=
 3458:             '<tr'.$css_class.'><td valign="top">'.
 3459:             '<span class="LC_nobreak">'.$choices->{$item}.
 3460:             '</span></td>';
 3461:         if ($align eq 'left') {
 3462:             $datatable .= '<td class="LC_left_item">';
 3463:         } else {
 3464:             $datatable .= '<td class="LC_right_item">';
 3465:         }
 3466:         $datatable .=
 3467:             '<span class="LC_nobreak">'.
 3468:             '<label><input type="radio" name="'.
 3469:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 3470:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 3471:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 3472:             '</span>'.$additional.
 3473:             '</td>'.
 3474:             '</tr>';
 3475:         $itemcount ++;
 3476:     }
 3477:     return ($datatable,$itemcount);
 3478: }
 3479: 
 3480: sub print_coursedefaults {
 3481:     my ($position,$dom,$settings,$rowtotal) = @_;
 3482:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 3483:     my $itemcount = 1;
 3484:     my %choices =  &Apache::lonlocal::texthash (
 3485:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 3486:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 3487:         coursecredits        => 'Credits can be specified for courses',
 3488:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 3489:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 3490:         postsubmit           => 'Disable submit button/keypress following student submission',
 3491:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 3492:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 3493:     );
 3494:     my %staticdefaults = (
 3495:                            anonsurvey_threshold => 10,
 3496:                            uploadquota          => 500,
 3497:                            postsubmit           => 60,
 3498:                            mysqltables          => 172800,
 3499:                          );
 3500:     if ($position eq 'top') {
 3501:         %defaultchecked = (
 3502:                             'uselcmath'       => 'on',
 3503:                             'usejsme'         => 'on',
 3504:                             'canclone'        => 'none',
 3505:                           );
 3506:         @toggles = ('uselcmath','usejsme');
 3507:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3508:                                                      \%choices,$itemcount);
 3509:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3510:         $datatable .=
 3511:             '<tr'.$css_class.'><td valign="top">'.
 3512:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 3513:             '</span></td><td class="LC_left_item">';
 3514:         my $currcanclone = 'none';
 3515:         my $onclick;
 3516:         my @cloneoptions = ('none','domain');
 3517:         my %clonetitles = (
 3518:                              none     => 'No additional course requesters',
 3519:                              domain   => "Any course requester in course's domain",
 3520:                              instcode => 'Course requests for official courses ...',
 3521:                           );
 3522:         my (%codedefaults,@code_order,@posscodes);
 3523:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 3524:                                                     \@code_order) eq 'ok') {
 3525:             if (@code_order > 0) {
 3526:                 push(@cloneoptions,'instcode');
 3527:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 3528:             }
 3529:         }
 3530:         if (ref($settings) eq 'HASH') {
 3531:             if ($settings->{'canclone'}) {
 3532:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 3533:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 3534:                         if (@code_order > 0) {
 3535:                             $currcanclone = 'instcode';
 3536:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 3537:                         }
 3538:                     }
 3539:                 } elsif ($settings->{'canclone'} eq 'domain') {
 3540:                     $currcanclone = $settings->{'canclone'};
 3541:                 }
 3542:             }
 3543:         }
 3544:         foreach my $option (@cloneoptions) {
 3545:             my ($checked,$additional);
 3546:             if ($currcanclone eq $option) {
 3547:                 $checked = ' checked="checked"';
 3548:             }
 3549:             if ($option eq 'instcode') {
 3550:                 if (@code_order) {
 3551:                     my $show = 'none';
 3552:                     if ($checked) {
 3553:                         $show = 'block';
 3554:                     }
 3555:                     $additional = '<div id="cloneinstcode" style="display:'.$show.'" />'.
 3556:                                   &mt('Institutional codes for new and cloned course have identical:').
 3557:                                   '<br />';
 3558:                     foreach my $item (@code_order) {
 3559:                         my $codechk;
 3560:                         if ($checked) {
 3561:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 3562:                                 $codechk = ' checked="checked"';
 3563:                             }
 3564:                         }
 3565:                         $additional .= '<label>'.
 3566:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 3567:                                        $item.'</label>';
 3568:                     }
 3569:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 3570:                 }
 3571:             }
 3572:             $datatable .=
 3573:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 3574:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 3575:                 '</label>&nbsp;'.$additional.'</span><br />';
 3576:         }
 3577:         $datatable .= '</td>'.
 3578:                       '</tr>';
 3579:         $itemcount ++;
 3580:     } else {
 3581:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3582:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 3583:         my $currusecredits = 0;
 3584:         my $postsubmitclient = 1;
 3585:         my @types = ('official','unofficial','community','textbook');
 3586:         if (ref($settings) eq 'HASH') {
 3587:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 3588:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 3589:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 3590:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 3591:                 }
 3592:             }
 3593:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 3594:                 foreach my $type (@types) {
 3595:                     next if ($type eq 'community');
 3596:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 3597:                     if ($defcredits{$type} ne '') {
 3598:                         $currusecredits = 1;
 3599:                     }
 3600:                 }
 3601:             }
 3602:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 3603:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 3604:                     $postsubmitclient = 0;
 3605:                     foreach my $type (@types) {
 3606:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3607:                     }
 3608:                 } else {
 3609:                     foreach my $type (@types) {
 3610:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 3611:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 3612:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 3613:                             } else {
 3614:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3615:                             }
 3616:                         } else {
 3617:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3618:                         }
 3619:                     }
 3620:                 }
 3621:             } else {
 3622:                 foreach my $type (@types) {
 3623:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3624:                 }
 3625:             }
 3626:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 3627:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 3628:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 3629:                 }
 3630:             } else {
 3631:                 foreach my $type (@types) {
 3632:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 3633:                 }
 3634:             }
 3635:         } else {
 3636:             foreach my $type (@types) {
 3637:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3638:             }
 3639:         }
 3640:         if (!$currdefresponder) {
 3641:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 3642:         } elsif ($currdefresponder < 1) {
 3643:             $currdefresponder = 1;
 3644:         }
 3645:         foreach my $type (@types) {
 3646:             if ($curruploadquota{$type} eq '') {
 3647:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 3648:             }
 3649:         }
 3650:         $datatable .=
 3651:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3652:                 $choices{'anonsurvey_threshold'}.
 3653:                 '</span></td>'.
 3654:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 3655:                 '<input type="text" name="anonsurvey_threshold"'.
 3656:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 3657:                 '</td></tr>'."\n";
 3658:         $itemcount ++;
 3659:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3660:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3661:                       $choices{'uploadquota'}.
 3662:                       '</span></td>'.
 3663:                       '<td align="right" class="LC_right_item">'.
 3664:                       '<table><tr>';
 3665:         foreach my $type (@types) {
 3666:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 3667:                            '<input type="text" name="uploadquota_'.$type.'"'.
 3668:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 3669:         }
 3670:         $datatable .= '</tr></table></td></tr>'."\n";
 3671:         $itemcount ++;
 3672:         my $onclick = "toggleDisplay(this.form,'credits');";
 3673:         my $display = 'none';
 3674:         if ($currusecredits) {
 3675:             $display = 'block';
 3676:         }
 3677:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 3678:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 3679:         foreach my $type (@types) {
 3680:             next if ($type eq 'community');
 3681:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 3682:                            '<input type="text" name="'.$type.'_credits"'.
 3683:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 3684:         }
 3685:         $additional .= '</tr></table></div>'."\n";
 3686:         %defaultchecked = ('coursecredits' => 'off');
 3687:         @toggles = ('coursecredits');
 3688:         my $current = {
 3689:                         'coursecredits' => $currusecredits,
 3690:                       };
 3691:         (my $table,$itemcount) =
 3692:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 3693:                                \%choices,$itemcount,$onclick,$additional,'left');
 3694:         $datatable .= $table;
 3695:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 3696:         my $display = 'none';
 3697:         if ($postsubmitclient) {
 3698:             $display = 'block';
 3699:         }
 3700:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 3701:                       &mt('Number of seconds submit is disabled').'<br />'.
 3702:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 3703:                       '<table><tr>';
 3704:         foreach my $type (@types) {
 3705:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 3706:                            '<input type="text" name="'.$type.'_timeout" value="'.
 3707:                            $deftimeout{$type}.'" size="5" /></td>';
 3708:         }
 3709:         $additional .= '</tr></table></div>'."\n";
 3710:         %defaultchecked = ('postsubmit' => 'on');
 3711:         @toggles = ('postsubmit');
 3712:         $current = {
 3713:                        'postsubmit' => $postsubmitclient,
 3714:                    };
 3715:         ($table,$itemcount) =
 3716:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 3717:                                \%choices,$itemcount,$onclick,$additional,'left');
 3718:         $datatable .= $table;
 3719:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3720:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3721:                       $choices{'mysqltables'}.
 3722:                       '</span></td>'.
 3723:                       '<td align="right" class="LC_right_item">'.
 3724:                       '<table><tr>';
 3725:         foreach my $type (@types) {
 3726:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 3727:                            '<input type="text" name="mysqltables_'.$type.'"'.
 3728:                            ' value="'.$currmysql{$type}.'" size="5" /></td>';
 3729:         }
 3730:         $datatable .= '</tr></table></td></tr>'."\n";
 3731:         $itemcount ++;
 3732: 
 3733:     }
 3734:     $$rowtotal += $itemcount;
 3735:     return $datatable;
 3736: }
 3737: 
 3738: sub print_selfenrollment {
 3739:     my ($position,$dom,$settings,$rowtotal) = @_;
 3740:     my ($css_class,$datatable);
 3741:     my $itemcount = 1;
 3742:     my @types = ('official','unofficial','community','textbook');
 3743:     if (($position eq 'top') || ($position eq 'middle')) {
 3744:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 3745:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 3746:         my @rows;
 3747:         my $key;
 3748:         if ($position eq 'top') {
 3749:             $key = 'admin'; 
 3750:             if (ref($rowsref) eq 'ARRAY') {
 3751:                 @rows = @{$rowsref};
 3752:             }
 3753:         } elsif ($position eq 'middle') {
 3754:             $key = 'default';
 3755:             @rows = ('types','registered','approval','limit');
 3756:         }
 3757:         foreach my $row (@rows) {
 3758:             if (defined($titlesref->{$row})) {
 3759:                 $itemcount ++;
 3760:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3761:                 $datatable .= '<tr'.$css_class.'>'.
 3762:                               '<td>'.$titlesref->{$row}.'</td>'.
 3763:                               '<td class="LC_left_item">'.
 3764:                               '<table><tr>';
 3765:                 my (%current,%currentcap);
 3766:                 if (ref($settings) eq 'HASH') {
 3767:                     if (ref($settings->{$key}) eq 'HASH') {
 3768:                         foreach my $type (@types) {
 3769:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 3770:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 3771:                             }
 3772:                             if (($row eq 'limit') && ($key eq 'default')) {
 3773:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 3774:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 3775:                                 }
 3776:                             }
 3777:                         }
 3778:                     }
 3779:                 }
 3780:                 my %roles = (
 3781:                              '0' => &Apache::lonnet::plaintext('dc'),
 3782:                             ); 
 3783:             
 3784:                 foreach my $type (@types) {
 3785:                     unless (($row eq 'registered') && ($key eq 'default')) {
 3786:                         $datatable .= '<th>'.&mt($type).'</th>';
 3787:                     }
 3788:                 }
 3789:                 unless (($row eq 'registered') && ($key eq 'default')) {
 3790:                     $datatable .= '</tr><tr>';
 3791:                 }
 3792:                 foreach my $type (@types) {
 3793:                     if ($type eq 'community') {
 3794:                         $roles{'1'} = &mt('Community personnel');
 3795:                     } else {
 3796:                         $roles{'1'} = &mt('Course personnel');
 3797:                     }
 3798:                     $datatable .= '<td style="vertical-align: top">';
 3799:                     if ($position eq 'top') {
 3800:                         my %checked;
 3801:                         if ($current{$type} eq '0') {
 3802:                             $checked{'0'} = ' checked="checked"';
 3803:                         } else {
 3804:                             $checked{'1'} = ' checked="checked"';
 3805:                         }
 3806:                         foreach my $role ('1','0') {
 3807:                             $datatable .= '<span class="LC_nobreak"><label>'.
 3808:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 3809:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 3810:                                           $roles{$role}.'</label></span> ';
 3811:                         }
 3812:                     } else {
 3813:                         if ($row eq 'types') {
 3814:                             my %checked;
 3815:                             if ($current{$type} =~ /^(all|dom)$/) {
 3816:                                 $checked{$1} = ' checked="checked"';
 3817:                             } else {
 3818:                                 $checked{''} = ' checked="checked"';
 3819:                             }
 3820:                             foreach my $val ('','dom','all') {
 3821:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3822:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3823:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3824:                             }
 3825:                         } elsif ($row eq 'registered') {
 3826:                             my %checked;
 3827:                             if ($current{$type} eq '1') {
 3828:                                 $checked{'1'} = ' checked="checked"';
 3829:                             } else {
 3830:                                 $checked{'0'} = ' checked="checked"';
 3831:                             }
 3832:                             foreach my $val ('0','1') {
 3833:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3834:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3835:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3836:                             }
 3837:                         } elsif ($row eq 'approval') {
 3838:                             my %checked;
 3839:                             if ($current{$type} =~ /^([12])$/) {
 3840:                                 $checked{$1} = ' checked="checked"';
 3841:                             } else {
 3842:                                 $checked{'0'} = ' checked="checked"';
 3843:                             }
 3844:                             for my $val (0..2) {
 3845:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3846:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3847:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3848:                             }
 3849:                         } elsif ($row eq 'limit') {
 3850:                             my %checked;
 3851:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 3852:                                 $checked{$1} = ' checked="checked"';
 3853:                             } else {
 3854:                                 $checked{'none'} = ' checked="checked"';
 3855:                             }
 3856:                             my $cap;
 3857:                             if ($currentcap{$type} =~ /^\d+$/) {
 3858:                                 $cap = $currentcap{$type};
 3859:                             }
 3860:                             foreach my $val ('none','allstudents','selfenrolled') {
 3861:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3862:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3863:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3864:                             }
 3865:                             $datatable .= '<br />'.
 3866:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 3867:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 3868:                                           '</span>'; 
 3869:                         }
 3870:                     }
 3871:                     $datatable .= '</td>';
 3872:                 }
 3873:                 $datatable .= '</tr>';
 3874:             }
 3875:             $datatable .= '</table></td></tr>';
 3876:         }
 3877:     } elsif ($position eq 'bottom') {
 3878:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 3879:     }
 3880:     $$rowtotal += $itemcount;
 3881:     return $datatable;
 3882: }
 3883: 
 3884: sub print_validation_rows {
 3885:     my ($caller,$dom,$settings,$rowtotal) = @_;
 3886:     my ($itemsref,$namesref,$fieldsref);
 3887:     if ($caller eq 'selfenroll') { 
 3888:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 3889:     } elsif ($caller eq 'requestcourses') {
 3890:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 3891:     }
 3892:     my %currvalidation;
 3893:     if (ref($settings) eq 'HASH') {
 3894:         if (ref($settings->{'validation'}) eq 'HASH') {
 3895:             %currvalidation = %{$settings->{'validation'}};
 3896:         }
 3897:     }
 3898:     my $datatable;
 3899:     my $itemcount = 0;
 3900:     foreach my $item (@{$itemsref}) {
 3901:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3902:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3903:                       $namesref->{$item}.
 3904:                       '</span></td>'.
 3905:                       '<td class="LC_left_item">';
 3906:         if (($item eq 'url') || ($item eq 'button')) {
 3907:             $datatable .= '<span class="LC_nobreak">'.
 3908:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 3909:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 3910:         } elsif ($item eq 'fields') {
 3911:             my @currfields;
 3912:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 3913:                 @currfields = @{$currvalidation{$item}};
 3914:             }
 3915:             foreach my $field (@{$fieldsref}) {
 3916:                 my $check = '';
 3917:                 if (grep(/^\Q$field\E$/,@currfields)) {
 3918:                     $check = ' checked="checked"';
 3919:                 }
 3920:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3921:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 3922:                               ' value="'.$field.'"'.$check.' />'.$field.
 3923:                               '</label></span> ';
 3924:             }
 3925:         } elsif ($item eq 'markup') {
 3926:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5" wrap="soft">'.
 3927:                            $currvalidation{$item}.
 3928:                               '</textarea>';
 3929:         }
 3930:         $datatable .= '</td></tr>'."\n";
 3931:         if (ref($rowtotal)) {
 3932:             $itemcount ++;
 3933:         }
 3934:     }
 3935:     if ($caller eq 'requestcourses') {
 3936:         my %currhash;
 3937:         if (ref($settings) eq 'HASH') {
 3938:             if (ref($settings->{'validation'}) eq 'HASH') {
 3939:                 if ($settings->{'validation'}{'dc'} ne '') {
 3940:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 3941:                 }
 3942:             }
 3943:         }
 3944:         my $numinrow = 2;
 3945:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 3946:                                                        'validationdc',%currhash);
 3947:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3948:         $datatable .= '</td></tr><tr'.$css_class.'><td>';
 3949:         if ($numdc > 1) {
 3950:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 3951:         } else {
 3952:             $datatable .=  &mt('Course creation processed as: ');
 3953:         }
 3954:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 3955:         $itemcount ++;
 3956:     }
 3957:     if (ref($rowtotal)) {
 3958:         $$rowtotal += $itemcount;
 3959:     }
 3960:     return $datatable;
 3961: }
 3962: 
 3963: sub print_usersessions {
 3964:     my ($position,$dom,$settings,$rowtotal) = @_;
 3965:     my ($css_class,$datatable,%checked,%choices);
 3966:     my (%by_ip,%by_location,@intdoms);
 3967:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 3968: 
 3969:     my @alldoms = &Apache::lonnet::all_domains();
 3970:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 3971:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3972:     my %altids = &id_for_thisdom(%servers);
 3973:     my $itemcount = 1;
 3974:     if ($position eq 'top') {
 3975:         if (keys(%serverhomes) > 1) {
 3976:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 3977:             my $curroffloadnow;
 3978:             if (ref($settings) eq 'HASH') {
 3979:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 3980:                     $curroffloadnow = $settings->{'offloadnow'};
 3981:                 }
 3982:             }
 3983:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$curroffloadnow,$rowtotal);
 3984:         } else {
 3985:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3986:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 3987:         }
 3988:     } else {
 3989:         if (keys(%by_location) == 0) {
 3990:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3991:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 3992:         } else {
 3993:             my %lt = &usersession_titles();
 3994:             my $numinrow = 5;
 3995:             my $prefix;
 3996:             my @types;
 3997:             if ($position eq 'bottom') {
 3998:                 $prefix = 'remote';
 3999:                 @types = ('version','excludedomain','includedomain');
 4000:             } else {
 4001:                 $prefix = 'hosted';
 4002:                 @types = ('excludedomain','includedomain');
 4003:             }
 4004:             my (%current,%checkedon,%checkedoff);
 4005:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 4006:             my @locations = sort(keys(%by_location));
 4007:             foreach my $type (@types) {
 4008:                 $checkedon{$type} = '';
 4009:                 $checkedoff{$type} = ' checked="checked"';
 4010:             }
 4011:             if (ref($settings) eq 'HASH') {
 4012:                 if (ref($settings->{$prefix}) eq 'HASH') {
 4013:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 4014:                         $current{$key} = $settings->{$prefix}{$key};
 4015:                         if ($key eq 'version') {
 4016:                             if ($current{$key} ne '') {
 4017:                                 $checkedon{$key} = ' checked="checked"';
 4018:                                 $checkedoff{$key} = '';
 4019:                             }
 4020:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 4021:                             $checkedon{$key} = ' checked="checked"';
 4022:                             $checkedoff{$key} = '';
 4023:                         }
 4024:                     }
 4025:                 }
 4026:             }
 4027:             foreach my $type (@types) {
 4028:                 next if ($type ne 'version' && !@locations);
 4029:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4030:                 $datatable .= '<tr'.$css_class.'>
 4031:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 4032:                                <span class="LC_nobreak">&nbsp;
 4033:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 4034:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 4035:                 if ($type eq 'version') {
 4036:                     my $selector = '<select name="'.$prefix.'_version">';
 4037:                     foreach my $version (@lcversions) {
 4038:                         my $selected = '';
 4039:                         if ($current{'version'} eq $version) {
 4040:                             $selected = ' selected="selected"';
 4041:                         }
 4042:                         $selector .= ' <option value="'.$version.'"'.
 4043:                                      $selected.'>'.$version.'</option>';
 4044:                     }
 4045:                     $selector .= '</select> ';
 4046:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 4047:                 } else {
 4048:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 4049:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 4050:                                  ' />'.('&nbsp;'x2).
 4051:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 4052:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 4053:                                  "\n".
 4054:                                  '</div><div><table>';
 4055:                     my $rem;
 4056:                     for (my $i=0; $i<@locations; $i++) {
 4057:                         my ($showloc,$value,$checkedtype);
 4058:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 4059:                             my $ip = $by_location{$locations[$i]}->[0];
 4060:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 4061:                                  $value = join(':',@{$by_ip{$ip}});
 4062:                                 $showloc = join(', ',@{$by_ip{$ip}});
 4063:                                 if (ref($current{$type}) eq 'ARRAY') {
 4064:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 4065:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 4066:                                             $checkedtype = ' checked="checked"';
 4067:                                             last;
 4068:                                         }
 4069:                                     }
 4070:                                 }
 4071:                             }
 4072:                         }
 4073:                         $rem = $i%($numinrow);
 4074:                         if ($rem == 0) {
 4075:                             if ($i > 0) {
 4076:                                 $datatable .= '</tr>';
 4077:                             }
 4078:                             $datatable .= '<tr>';
 4079:                         }
 4080:                         $datatable .= '<td class="LC_left_item">'.
 4081:                                       '<span class="LC_nobreak"><label>'.
 4082:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 4083:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 4084:                                       '</label></span></td>';
 4085:                     }
 4086:                     $rem = @locations%($numinrow);
 4087:                     my $colsleft = $numinrow - $rem;
 4088:                     if ($colsleft > 1 ) {
 4089:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4090:                                       '&nbsp;</td>';
 4091:                     } elsif ($colsleft == 1) {
 4092:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4093:                     }
 4094:                     $datatable .= '</tr></table>';
 4095:                 }
 4096:                 $datatable .= '</td></tr>';
 4097:                 $itemcount ++;
 4098:             }
 4099:         }
 4100:     }
 4101:     $$rowtotal += $itemcount;
 4102:     return $datatable;
 4103: }
 4104: 
 4105: sub build_location_hashes {
 4106:     my ($intdoms,$by_ip,$by_location) = @_;
 4107:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 4108:                   (ref($by_location) eq 'HASH')); 
 4109:     my %iphost = &Apache::lonnet::get_iphost();
 4110:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 4111:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 4112:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 4113:         foreach my $id (@{$iphost{$primary_ip}}) {
 4114:             my $intdom = &Apache::lonnet::internet_dom($id);
 4115:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 4116:                 push(@{$intdoms},$intdom);
 4117:             }
 4118:         }
 4119:     }
 4120:     foreach my $ip (keys(%iphost)) {
 4121:         if (ref($iphost{$ip}) eq 'ARRAY') {
 4122:             foreach my $id (@{$iphost{$ip}}) {
 4123:                 my $location = &Apache::lonnet::internet_dom($id);
 4124:                 if ($location) {
 4125:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 4126:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4127:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 4128:                             push(@{$by_ip->{$ip}},$location);
 4129:                         }
 4130:                     } else {
 4131:                         $by_ip->{$ip} = [$location];
 4132:                     }
 4133:                 }
 4134:             }
 4135:         }
 4136:     }
 4137:     foreach my $ip (sort(keys(%{$by_ip}))) {
 4138:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4139:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 4140:             my $first = $by_ip->{$ip}->[0];
 4141:             if (ref($by_location->{$first}) eq 'ARRAY') {
 4142:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 4143:                     push(@{$by_location->{$first}},$ip);
 4144:                 }
 4145:             } else {
 4146:                 $by_location->{$first} = [$ip];
 4147:             }
 4148:         }
 4149:     }
 4150:     return;
 4151: }
 4152: 
 4153: sub current_offloads_to {
 4154:     my ($dom,$settings,$servers) = @_;
 4155:     my (%spareid,%otherdomconfigs);
 4156:     if (ref($servers) eq 'HASH') {
 4157:         foreach my $lonhost (sort(keys(%{$servers}))) {
 4158:             my $gotspares;
 4159:             if (ref($settings) eq 'HASH') {
 4160:                 if (ref($settings->{'spares'}) eq 'HASH') {
 4161:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 4162:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 4163:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 4164:                         $gotspares = 1;
 4165:                     }
 4166:                 }
 4167:             }
 4168:             unless ($gotspares) {
 4169:                 my $gotspares;
 4170:                 my $serverhomeID =
 4171:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 4172:                 my $serverhomedom =
 4173:                     &Apache::lonnet::host_domain($serverhomeID);
 4174:                 if ($serverhomedom ne $dom) {
 4175:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 4176:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4177:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4178:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4179:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4180:                                 $gotspares = 1;
 4181:                             }
 4182:                         }
 4183:                     } else {
 4184:                         $otherdomconfigs{$serverhomedom} =
 4185:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 4186:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 4187:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4188:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4189:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 4190:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4191:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4192:                                         $gotspares = 1;
 4193:                                     }
 4194:                                 }
 4195:                             }
 4196:                         }
 4197:                     }
 4198:                 }
 4199:             }
 4200:             unless ($gotspares) {
 4201:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4202:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4203:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4204:                } else {
 4205:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 4206:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 4207:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4208:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4209:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4210:                     } else {
 4211:                         my %what = (
 4212:                              spareid => 1,
 4213:                         );
 4214:                         my ($result,$returnhash) = 
 4215:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 4216:                         if ($result eq 'ok') { 
 4217:                             if (ref($returnhash) eq 'HASH') {
 4218:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 4219:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 4220:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 4221:                                 }
 4222:                             }
 4223:                         }
 4224:                     }
 4225:                 }
 4226:             }
 4227:         }
 4228:     }
 4229:     return %spareid;
 4230: }
 4231: 
 4232: sub spares_row {
 4233:     my ($dom,$servers,$spareid,$serverhomes,$altids,$curroffloadnow,$rowtotal) = @_;
 4234:     my $css_class;
 4235:     my $numinrow = 4;
 4236:     my $itemcount = 1;
 4237:     my $datatable;
 4238:     my %typetitles = &sparestype_titles();
 4239:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 4240:         foreach my $server (sort(keys(%{$servers}))) {
 4241:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 4242:             my ($othercontrol,$serverdom);
 4243:             if ($serverhome ne $server) {
 4244:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 4245:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 4246:             } else {
 4247:                 $serverdom = &Apache::lonnet::host_domain($server);
 4248:                 if ($serverdom ne $dom) {
 4249:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 4250:                 }
 4251:             }
 4252:             next unless (ref($spareid->{$server}) eq 'HASH');
 4253:             my $checkednow;
 4254:             if (ref($curroffloadnow) eq 'HASH') {
 4255:                 if ($curroffloadnow->{$server}) {
 4256:                     $checkednow = ' checked="checked"';
 4257:                 }
 4258:             }
 4259:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4260:             $datatable .= '<tr'.$css_class.'>
 4261:                            <td rowspan="2">
 4262:                             <span class="LC_nobreak">'.
 4263:                           &mt('[_1] when busy, offloads to:'
 4264:                               ,'<b>'.$server.'</b>').'</span><br />'.
 4265:                           '<span class="LC_nobreak">'."\n".
 4266:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 4267:                           '&nbsp;'.&mt('Switch active users on next access').'</label></span>'.
 4268:                           "\n";
 4269:             my (%current,%canselect);
 4270:             my @choices = 
 4271:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 4272:             foreach my $type ('primary','default') {
 4273:                 if (ref($spareid->{$server}) eq 'HASH') {
 4274:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 4275:                         my @spares = @{$spareid->{$server}{$type}};
 4276:                         if (@spares > 0) {
 4277:                             if ($othercontrol) {
 4278:                                 $current{$type} = join(', ',@spares);
 4279:                             } else {
 4280:                                 $current{$type} .= '<table>';
 4281:                                 my $numspares = scalar(@spares);
 4282:                                 for (my $i=0;  $i<@spares; $i++) {
 4283:                                     my $rem = $i%($numinrow);
 4284:                                     if ($rem == 0) {
 4285:                                         if ($i > 0) {
 4286:                                             $current{$type} .= '</tr>';
 4287:                                         }
 4288:                                         $current{$type} .= '<tr>';
 4289:                                     }
 4290:                                     $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;'.
 4291:                                                        $spareid->{$server}{$type}[$i].
 4292:                                                        '</label></td>'."\n";
 4293:                                 }
 4294:                                 my $rem = @spares%($numinrow);
 4295:                                 my $colsleft = $numinrow - $rem;
 4296:                                 if ($colsleft > 1 ) {
 4297:                                     $current{$type} .= '<td colspan="'.$colsleft.
 4298:                                                        '" class="LC_left_item">'.
 4299:                                                        '&nbsp;</td>';
 4300:                                 } elsif ($colsleft == 1) {
 4301:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 4302:                                 }
 4303:                                 $current{$type} .= '</tr></table>';
 4304:                             }
 4305:                         }
 4306:                     }
 4307:                     if ($current{$type} eq '') {
 4308:                         $current{$type} = &mt('None specified');
 4309:                     }
 4310:                     if ($othercontrol) {
 4311:                         if ($type eq 'primary') {
 4312:                             $canselect{$type} = $othercontrol;
 4313:                         }
 4314:                     } else {
 4315:                         $canselect{$type} = 
 4316:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 4317:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 4318:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 4319:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 4320:                         if (@choices > 0) {
 4321:                             foreach my $lonhost (@choices) {
 4322:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 4323:                             }
 4324:                         }
 4325:                         $canselect{$type} .= '</select>'."\n";
 4326:                     }
 4327:                 } else {
 4328:                     $current{$type} = &mt('Could not be determined');
 4329:                     if ($type eq 'primary') {
 4330:                         $canselect{$type} =  $othercontrol;
 4331:                     }
 4332:                 }
 4333:                 if ($type eq 'default') {
 4334:                     $datatable .= '<tr'.$css_class.'>';
 4335:                 }
 4336:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 4337:                               '<td>'.$current{$type}.'</td>'."\n".
 4338:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 4339:             }
 4340:             $itemcount ++;
 4341:         }
 4342:     }
 4343:     $$rowtotal += $itemcount;
 4344:     return $datatable;
 4345: }
 4346: 
 4347: sub possible_newspares {
 4348:     my ($server,$currspares,$serverhomes,$altids) = @_;
 4349:     my $serverhostname = &Apache::lonnet::hostname($server);
 4350:     my %excluded;
 4351:     if ($serverhostname ne '') {
 4352:         %excluded = (
 4353:                        $serverhostname => 1,
 4354:                     );
 4355:     }
 4356:     if (ref($currspares) eq 'HASH') {
 4357:         foreach my $type (keys(%{$currspares})) {
 4358:             if (ref($currspares->{$type}) eq 'ARRAY') {
 4359:                 if (@{$currspares->{$type}} > 0) {
 4360:                     foreach my $curr (@{$currspares->{$type}}) {
 4361:                         my $hostname = &Apache::lonnet::hostname($curr);
 4362:                         $excluded{$hostname} = 1;
 4363:                     }
 4364:                 }
 4365:             }
 4366:         }
 4367:     }
 4368:     my @choices;
 4369:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 4370:         if (keys(%{$serverhomes}) > 1) {
 4371:             foreach my $name (sort(keys(%{$serverhomes}))) {
 4372:                 unless ($excluded{$name}) {
 4373:                     if (exists($altids->{$serverhomes->{$name}})) {
 4374:                         push(@choices,$altids->{$serverhomes->{$name}});
 4375:                     } else {
 4376:                         push(@choices,$serverhomes->{$name});
 4377:                     }
 4378:                 }
 4379:             }
 4380:         }
 4381:     }
 4382:     return sort(@choices);
 4383: }
 4384: 
 4385: sub print_loadbalancing {
 4386:     my ($dom,$settings,$rowtotal) = @_;
 4387:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 4388:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 4389:     my $numinrow = 1;
 4390:     my $datatable;
 4391:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4392:     my (%currbalancer,%currtargets,%currrules,%existing);
 4393:     if (ref($settings) eq 'HASH') {
 4394:         %existing = %{$settings};
 4395:     }
 4396:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 4397:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 4398:                                   \%currtargets,\%currrules);
 4399:     } else {
 4400:         return;
 4401:     }
 4402:     my ($othertitle,$usertypes,$types) =
 4403:         &Apache::loncommon::sorted_inst_types($dom);
 4404:     my $rownum = 8;
 4405:     if (ref($types) eq 'ARRAY') {
 4406:         $rownum += scalar(@{$types});
 4407:     }
 4408:     my @css_class = ('LC_odd_row','LC_even_row');
 4409:     my $balnum = 0;
 4410:     my $islast;
 4411:     my (@toshow,$disabledtext);
 4412:     if (keys(%currbalancer) > 0) {
 4413:         @toshow = sort(keys(%currbalancer));
 4414:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 4415:             push(@toshow,'');
 4416:         }
 4417:     } else {
 4418:         @toshow = ('');
 4419:         $disabledtext = &mt('No existing load balancer');
 4420:     }
 4421:     foreach my $lonhost (@toshow) {
 4422:         if ($balnum == scalar(@toshow)-1) {
 4423:             $islast = 1;
 4424:         } else {
 4425:             $islast = 0;
 4426:         }
 4427:         my $cssidx = $balnum%2;
 4428:         my $targets_div_style = 'display: none';
 4429:         my $disabled_div_style = 'display: block';
 4430:         my $homedom_div_style = 'display: none';
 4431:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 4432:                       '<td rowspan="'.$rownum.'" valign="top">'.
 4433:                       '<p>';
 4434:         if ($lonhost eq '') {
 4435:             $datatable .= '<span class="LC_nobreak">';
 4436:             if (keys(%currbalancer) > 0) {
 4437:                 $datatable .= &mt('Add balancer:');
 4438:             } else {
 4439:                 $datatable .= &mt('Enable balancer:');
 4440:             }
 4441:             $datatable .= '&nbsp;'.
 4442:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 4443:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 4444:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 4445:                           '<option value="" selected="selected">'.&mt('None').
 4446:                           '</option>'."\n";
 4447:             foreach my $server (sort(keys(%servers))) {
 4448:                 next if ($currbalancer{$server});
 4449:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 4450:             }
 4451:             $datatable .=
 4452:                 '</select>'."\n".
 4453:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 4454:         } else {
 4455:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 4456:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 4457:                            &mt('Stop balancing').'</label>'.
 4458:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 4459:             $targets_div_style = 'display: block';
 4460:             $disabled_div_style = 'display: none';
 4461:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 4462:                 $homedom_div_style = 'display: block';
 4463:             }
 4464:         }
 4465:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 4466:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 4467:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 4468:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 4469:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 4470:         my @sparestypes = ('primary','default');
 4471:         my %typetitles = &sparestype_titles();
 4472:         my %hostherechecked = (
 4473:                                   no => ' checked="checked"',
 4474:                               );
 4475:         foreach my $sparetype (@sparestypes) {
 4476:             my $targettable;
 4477:             for (my $i=0; $i<$numspares; $i++) {
 4478:                 my $checked;
 4479:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 4480:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 4481:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 4482:                             $checked = ' checked="checked"';
 4483:                         }
 4484:                     }
 4485:                 }
 4486:                 my ($chkboxval,$disabled);
 4487:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 4488:                     $chkboxval = $spares[$i];
 4489:                 }
 4490:                 if (exists($currbalancer{$spares[$i]})) {
 4491:                     $disabled = ' disabled="disabled"';
 4492:                 }
 4493:                 $targettable .=
 4494:                     '<td><span class="LC_nobreak"><label>'.
 4495:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 4496:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 4497:                     '</span></label></span></td>';
 4498:                 my $rem = $i%($numinrow);
 4499:                 if ($rem == 0) {
 4500:                     if (($i > 0) && ($i < $numspares-1)) {
 4501:                         $targettable .= '</tr>';
 4502:                     }
 4503:                     if ($i < $numspares-1) {
 4504:                         $targettable .= '<tr>';
 4505:                     }
 4506:                 }
 4507:             }
 4508:             if ($targettable ne '') {
 4509:                 my $rem = $numspares%($numinrow);
 4510:                 my $colsleft = $numinrow - $rem;
 4511:                 if ($colsleft > 1 ) {
 4512:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4513:                                     '&nbsp;</td>';
 4514:                 } elsif ($colsleft == 1) {
 4515:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 4516:                 }
 4517:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 4518:                                '<table><tr>'.$targettable.'</tr></table><br />';
 4519:             }
 4520:             $hostherechecked{$sparetype} = '';
 4521:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 4522:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 4523:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 4524:                         $hostherechecked{$sparetype} = ' checked="checked"';
 4525:                         $hostherechecked{'no'} = '';
 4526:                     }
 4527:                 }
 4528:             }
 4529:         }
 4530:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 4531:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 4532:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 4533:         foreach my $sparetype (@sparestypes) {
 4534:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 4535:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 4536:                           '</i></label><br />';
 4537:         }
 4538:         $datatable .= '</div></td></tr>'.
 4539:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 4540:                                            $othertitle,$usertypes,$types,\%servers,
 4541:                                            \%currbalancer,$lonhost,
 4542:                                            $targets_div_style,$homedom_div_style,
 4543:                                            $css_class[$cssidx],$balnum,$islast);
 4544:         $$rowtotal += $rownum;
 4545:         $balnum ++;
 4546:     }
 4547:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 4548:     return $datatable;
 4549: }
 4550: 
 4551: sub get_loadbalancers_config {
 4552:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 4553:     return unless ((ref($servers) eq 'HASH') &&
 4554:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 4555:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 4556:     if (keys(%{$existing}) > 0) {
 4557:         my $oldlonhost;
 4558:         foreach my $key (sort(keys(%{$existing}))) {
 4559:             if ($key eq 'lonhost') {
 4560:                 $oldlonhost = $existing->{'lonhost'};
 4561:                 $currbalancer->{$oldlonhost} = 1;
 4562:             } elsif ($key eq 'targets') {
 4563:                 if ($oldlonhost) {
 4564:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 4565:                 }
 4566:             } elsif ($key eq 'rules') {
 4567:                 if ($oldlonhost) {
 4568:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 4569:                 }
 4570:             } elsif (ref($existing->{$key}) eq 'HASH') {
 4571:                 $currbalancer->{$key} = 1;
 4572:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 4573:                 $currrules->{$key} = $existing->{$key}{'rules'};
 4574:             }
 4575:         }
 4576:     } else {
 4577:         my ($balancerref,$targetsref) =
 4578:                 &Apache::lonnet::get_lonbalancer_config($servers);
 4579:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 4580:             foreach my $server (sort(keys(%{$balancerref}))) {
 4581:                 $currbalancer->{$server} = 1;
 4582:                 $currtargets->{$server} = $targetsref->{$server};
 4583:             }
 4584:         }
 4585:     }
 4586:     return;
 4587: }
 4588: 
 4589: sub loadbalancing_rules {
 4590:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 4591:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 4592:         $css_class,$balnum,$islast) = @_;
 4593:     my $output;
 4594:     my $num = 0;
 4595:     my ($alltypes,$othertypes,$titles) =
 4596:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 4597:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 4598:         foreach my $type (@{$alltypes}) {
 4599:             $num ++;
 4600:             my $current;
 4601:             if (ref($currrules) eq 'HASH') {
 4602:                 $current = $currrules->{$type};
 4603:             }
 4604:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 4605:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 4606:                     $current = '';
 4607:                 }
 4608:             }
 4609:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 4610:                                              $servers,$currbalancer,$lonhost,$dom,
 4611:                                              $targets_div_style,$homedom_div_style,
 4612:                                              $css_class,$balnum,$num,$islast);
 4613:         }
 4614:     }
 4615:     return $output;
 4616: }
 4617: 
 4618: sub loadbalancing_titles {
 4619:     my ($dom,$intdom,$usertypes,$types) = @_;
 4620:     my %othertypes = (
 4621:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 4622:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 4623:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 4624:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 4625:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 4626:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 4627:                      );
 4628:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 4629:     if (ref($types) eq 'ARRAY') {
 4630:         unshift(@alltypes,@{$types},'default');
 4631:     }
 4632:     my %titles;
 4633:     foreach my $type (@alltypes) {
 4634:         if ($type =~ /^_LC_/) {
 4635:             $titles{$type} = $othertypes{$type};
 4636:         } elsif ($type eq 'default') {
 4637:             $titles{$type} = &mt('All users from [_1]',$dom);
 4638:             if (ref($types) eq 'ARRAY') {
 4639:                 if (@{$types} > 0) {
 4640:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 4641:                 }
 4642:             }
 4643:         } elsif (ref($usertypes) eq 'HASH') {
 4644:             $titles{$type} = $usertypes->{$type};
 4645:         }
 4646:     }
 4647:     return (\@alltypes,\%othertypes,\%titles);
 4648: }
 4649: 
 4650: sub loadbalance_rule_row {
 4651:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 4652:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 4653:     my @rulenames;
 4654:     my %ruletitles = &offloadtype_text();
 4655:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 4656:         @rulenames = ('balancer','offloadedto','specific');
 4657:     } else {
 4658:         @rulenames = ('default','homeserver');
 4659:         if ($type eq '_LC_external') {
 4660:             push(@rulenames,'externalbalancer');
 4661:         } else {
 4662:             push(@rulenames,'specific');
 4663:         }
 4664:         push(@rulenames,'none');
 4665:     }
 4666:     my $style = $targets_div_style;
 4667:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 4668:         $style = $homedom_div_style;
 4669:     }
 4670:     my $space;
 4671:     if ($islast && $num == 1) {
 4672:         $space = '<div display="inline-block">&nbsp;</div>';
 4673:     }
 4674:     my $output =
 4675:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 4676:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 4677:         '<td valaign="top">'.$space.
 4678:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 4679:     for (my $i=0; $i<@rulenames; $i++) {
 4680:         my $rule = $rulenames[$i];
 4681:         my ($checked,$extra);
 4682:         if ($rulenames[$i] eq 'default') {
 4683:             $rule = '';
 4684:         }
 4685:         if ($rulenames[$i] eq 'specific') {
 4686:             if (ref($servers) eq 'HASH') {
 4687:                 my $default;
 4688:                 if (($current ne '') && (exists($servers->{$current}))) {
 4689:                     $checked = ' checked="checked"';
 4690:                 }
 4691:                 unless ($checked) {
 4692:                     $default = ' selected="selected"';
 4693:                 }
 4694:                 $extra =
 4695:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 4696:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 4697:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 4698:                     '<option value=""'.$default.'></option>'."\n";
 4699:                 foreach my $server (sort(keys(%{$servers}))) {
 4700:                     if (ref($currbalancer) eq 'HASH') {
 4701:                         next if (exists($currbalancer->{$server}));
 4702:                     }
 4703:                     my $selected;
 4704:                     if ($server eq $current) {
 4705:                         $selected = ' selected="selected"';
 4706:                     }
 4707:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 4708:                 }
 4709:                 $extra .= '</select>';
 4710:             }
 4711:         } elsif ($rule eq $current) {
 4712:             $checked = ' checked="checked"';
 4713:         }
 4714:         $output .= '<span class="LC_nobreak"><label>'.
 4715:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 4716:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 4717:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 4718:                    ')"'.$checked.' />&nbsp;';
 4719:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 4720:             $output .= $ruletitles{'particular'};
 4721:         } else {
 4722:             $output .= $ruletitles{$rulenames[$i]};
 4723:         }
 4724:         $output .= '</label>'.$extra.'</span><br />'."\n";
 4725:     }
 4726:     $output .= '</div></td></tr>'."\n";
 4727:     return $output;
 4728: }
 4729: 
 4730: sub offloadtype_text {
 4731:     my %ruletitles = &Apache::lonlocal::texthash (
 4732:            'default'          => 'Offloads to default destinations',
 4733:            'homeserver'       => "Offloads to user's home server",
 4734:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 4735:            'specific'         => 'Offloads to specific server',
 4736:            'none'             => 'No offload',
 4737:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 4738:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 4739:            'particular'       => 'Session hosted (after re-auth) on server:',
 4740:     );
 4741:     return %ruletitles;
 4742: }
 4743: 
 4744: sub sparestype_titles {
 4745:     my %typestitles = &Apache::lonlocal::texthash (
 4746:                           'primary' => 'primary',
 4747:                           'default' => 'default',
 4748:                       );
 4749:     return %typestitles;
 4750: }
 4751: 
 4752: sub contact_titles {
 4753:     my %titles = &Apache::lonlocal::texthash (
 4754:                    'supportemail'    => 'Support E-mail address',
 4755:                    'adminemail'      => 'Default Server Admin E-mail address',
 4756:                    'errormail'       => 'Error reports to be e-mailed to',
 4757:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 4758:                    'helpdeskmail'    => "Helpdesk requests for this domain's users",
 4759:                    'otherdomsmail'   => 'Helpdesk requests for other (unconfigured) domains',
 4760:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 4761:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 4762:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 4763:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 4764:                  );
 4765:     my %short_titles = &Apache::lonlocal::texthash (
 4766:                            adminemail   => 'Admin E-mail address',
 4767:                            supportemail => 'Support E-mail',
 4768:                        );   
 4769:     return (\%titles,\%short_titles);
 4770: }
 4771: 
 4772: sub helpform_fields {
 4773:     my %titles =  &Apache::lonlocal::texthash (
 4774:                        'username'   => 'Name',
 4775:                        'user'       => 'Username/domain',
 4776:                        'phone'      => 'Phone',
 4777:                        'cc'         => 'Cc e-mail',
 4778:                        'course'     => 'Course Details',
 4779:                        'section'    => 'Sections',
 4780:                        'screenshot' => 'File upload',
 4781:     );
 4782:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 4783:     my %possoptions = (
 4784:                         username     => ['yes','no','req'],
 4785:                         phone        => ['yes','no','req'],
 4786:                         user         => ['yes','no'],
 4787:                         cc           => ['yes','no'],
 4788:                         course       => ['yes','no'],
 4789:                         section      => ['yes','no'],
 4790:                         screenshot   => ['yes','no'],
 4791:                       );
 4792:     my %fieldoptions = &Apache::lonlocal::texthash (
 4793:                          'yes'  => 'Optional',
 4794:                          'req'  => 'Required',
 4795:                          'no'   => "Not shown",
 4796:     );
 4797:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 4798: }
 4799: 
 4800: sub tool_titles {
 4801:     my %titles = &Apache::lonlocal::texthash (
 4802:                      aboutme    => 'Personal web page',
 4803:                      blog       => 'Blog',
 4804:                      webdav     => 'WebDAV',
 4805:                      portfolio  => 'Portfolio',
 4806:                      official   => 'Official courses (with institutional codes)',
 4807:                      unofficial => 'Unofficial courses',
 4808:                      community  => 'Communities',
 4809:                      textbook   => 'Textbook courses',
 4810:                  );
 4811:     return %titles;
 4812: }
 4813: 
 4814: sub courserequest_titles {
 4815:     my %titles = &Apache::lonlocal::texthash (
 4816:                                    official   => 'Official',
 4817:                                    unofficial => 'Unofficial',
 4818:                                    community  => 'Communities',
 4819:                                    textbook   => 'Textbook',
 4820:                                    norequest  => 'Not allowed',
 4821:                                    approval   => 'Approval by Dom. Coord.',
 4822:                                    validate   => 'With validation',
 4823:                                    autolimit  => 'Numerical limit',
 4824:                                    unlimited  => '(blank for unlimited)',
 4825:                  );
 4826:     return %titles;
 4827: }
 4828: 
 4829: sub authorrequest_titles {
 4830:     my %titles = &Apache::lonlocal::texthash (
 4831:                                    norequest  => 'Not allowed',
 4832:                                    approval   => 'Approval by Dom. Coord.',
 4833:                                    automatic  => 'Automatic approval',
 4834:                  );
 4835:     return %titles;
 4836: }
 4837: 
 4838: sub courserequest_conditions {
 4839:     my %conditions = &Apache::lonlocal::texthash (
 4840:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 4841:        validate   => '(Processing of request subject to institutional validation).',
 4842:                  );
 4843:     return %conditions;
 4844: }
 4845: 
 4846: 
 4847: sub print_usercreation {
 4848:     my ($position,$dom,$settings,$rowtotal) = @_;
 4849:     my $numinrow = 4;
 4850:     my $datatable;
 4851:     if ($position eq 'top') {
 4852:         $$rowtotal ++;
 4853:         my $rowcount = 0;
 4854:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 4855:         if (ref($rules) eq 'HASH') {
 4856:             if (keys(%{$rules}) > 0) {
 4857:                 $datatable .= &user_formats_row('username',$settings,$rules,
 4858:                                                 $ruleorder,$numinrow,$rowcount);
 4859:                 $$rowtotal ++;
 4860:                 $rowcount ++;
 4861:             }
 4862:         }
 4863:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 4864:         if (ref($idrules) eq 'HASH') {
 4865:             if (keys(%{$idrules}) > 0) {
 4866:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 4867:                                                 $idruleorder,$numinrow,$rowcount);
 4868:                 $$rowtotal ++;
 4869:                 $rowcount ++;
 4870:             }
 4871:         }
 4872:         if ($rowcount == 0) {
 4873:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 4874:             $$rowtotal ++;
 4875:             $rowcount ++;
 4876:         }
 4877:     } elsif ($position eq 'middle') {
 4878:         my @creators = ('author','course','requestcrs');
 4879:         my ($rules,$ruleorder) =
 4880:             &Apache::lonnet::inst_userrules($dom,'username');
 4881:         my %lt = &usercreation_types();
 4882:         my %checked;
 4883:         if (ref($settings) eq 'HASH') {
 4884:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 4885:                 foreach my $item (@creators) {
 4886:                     $checked{$item} = $settings->{'cancreate'}{$item};
 4887:                 }
 4888:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 4889:                 foreach my $item (@creators) {
 4890:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 4891:                         $checked{$item} = 'none';
 4892:                     }
 4893:                 }
 4894:             }
 4895:         }
 4896:         my $rownum = 0;
 4897:         foreach my $item (@creators) {
 4898:             $rownum ++;
 4899:             if ($checked{$item} eq '') {
 4900:                 $checked{$item} = 'any';
 4901:             }
 4902:             my $css_class;
 4903:             if ($rownum%2) {
 4904:                 $css_class = '';
 4905:             } else {
 4906:                 $css_class = ' class="LC_odd_row" ';
 4907:             }
 4908:             $datatable .= '<tr'.$css_class.'>'.
 4909:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 4910:                          '</span></td><td align="right">';
 4911:             my @options = ('any');
 4912:             if (ref($rules) eq 'HASH') {
 4913:                 if (keys(%{$rules}) > 0) {
 4914:                     push(@options,('official','unofficial'));
 4915:                 }
 4916:             }
 4917:             push(@options,'none');
 4918:             foreach my $option (@options) {
 4919:                 my $type = 'radio';
 4920:                 my $check = ' ';
 4921:                 if ($checked{$item} eq $option) {
 4922:                     $check = ' checked="checked" ';
 4923:                 } 
 4924:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4925:                               '<input type="'.$type.'" name="can_createuser_'.
 4926:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 4927:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 4928:             }
 4929:             $datatable .= '</td></tr>';
 4930:         }
 4931:     } else {
 4932:         my @contexts = ('author','course','domain');
 4933:         my @authtypes = ('int','krb4','krb5','loc');
 4934:         my %checked;
 4935:         if (ref($settings) eq 'HASH') {
 4936:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 4937:                 foreach my $item (@contexts) {
 4938:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 4939:                         foreach my $auth (@authtypes) {
 4940:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 4941:                                 $checked{$item}{$auth} = ' checked="checked" ';
 4942:                             }
 4943:                         }
 4944:                     }
 4945:                 }
 4946:             }
 4947:         } else {
 4948:             foreach my $item (@contexts) {
 4949:                 foreach my $auth (@authtypes) {
 4950:                     $checked{$item}{$auth} = ' checked="checked" ';
 4951:                 }
 4952:             }
 4953:         }
 4954:         my %title = &context_names();
 4955:         my %authname = &authtype_names();
 4956:         my $rownum = 0;
 4957:         my $css_class; 
 4958:         foreach my $item (@contexts) {
 4959:             if ($rownum%2) {
 4960:                 $css_class = '';
 4961:             } else {
 4962:                 $css_class = ' class="LC_odd_row" ';
 4963:             }
 4964:             $datatable .=   '<tr'.$css_class.'>'.
 4965:                             '<td>'.$title{$item}.
 4966:                             '</td><td class="LC_left_item">'.
 4967:                             '<span class="LC_nobreak">';
 4968:             foreach my $auth (@authtypes) {
 4969:                 $datatable .= '<label>'. 
 4970:                               '<input type="checkbox" name="'.$item.'_auth" '.
 4971:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 4972:                               $authname{$auth}.'</label>&nbsp;';
 4973:             }
 4974:             $datatable .= '</span></td></tr>';
 4975:             $rownum ++;
 4976:         }
 4977:         $$rowtotal += $rownum;
 4978:     }
 4979:     return $datatable;
 4980: }
 4981: 
 4982: sub print_selfcreation {
 4983:     my ($position,$dom,$settings,$rowtotal) = @_;
 4984:     my (@selfcreate,$createsettings,$processing,$datatable);
 4985:     if (ref($settings) eq 'HASH') {
 4986:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 4987:             $createsettings = $settings->{'cancreate'};
 4988:             if (ref($createsettings) eq 'HASH') {
 4989:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 4990:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 4991:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 4992:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 4993:                         @selfcreate = ('email','login','sso');
 4994:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 4995:                         @selfcreate = ($createsettings->{'selfcreate'});
 4996:                     }
 4997:                 }
 4998:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 4999:                     $processing = $createsettings->{'selfcreateprocessing'};
 5000:                 }
 5001:             }
 5002:         }
 5003:     }
 5004:     my %radiohash;
 5005:     my $numinrow = 4;
 5006:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 5007:     if ($position eq 'top') {
 5008:         my %choices = &Apache::lonlocal::texthash (
 5009:                                                       cancreate_login      => 'Institutional Login',
 5010:                                                       cancreate_sso        => 'Institutional Single Sign On',
 5011:                                                   );
 5012:         my @toggles = sort(keys(%choices));
 5013:         my %defaultchecked = (
 5014:                                'cancreate_login' => 'off',
 5015:                                'cancreate_sso'   => 'off',
 5016:                              );
 5017:         my ($onclick,$itemcount);
 5018:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5019:                                                      \%choices,$itemcount,$onclick);
 5020:         $$rowtotal += $itemcount;
 5021:         
 5022:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5023: 
 5024:         if (ref($usertypes) eq 'HASH') {
 5025:             if (keys(%{$usertypes}) > 0) {
 5026:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 5027:                                              $dom,$numinrow,$othertitle,
 5028:                                              'statustocreate',$$rowtotal);
 5029:                 $$rowtotal ++;
 5030:             }
 5031:         }
 5032:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 5033:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5034:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 5035:         my $rem;
 5036:         my $numperrow = 2;
 5037:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 5038:         $datatable .= '<tr'.$css_class.'>'.
 5039:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 5040:                      '<td class="LC_left_item">'."\n".
 5041:                      '<table><tr><td>'."\n";
 5042:         for (my $i=0; $i<@fields; $i++) {
 5043:             $rem = $i%($numperrow);
 5044:             if ($rem == 0) {
 5045:                 if ($i > 0) {
 5046:                     $datatable .= '</tr>';
 5047:                 }
 5048:                 $datatable .= '<tr>';
 5049:             }
 5050:             my $currval;
 5051:             if (ref($createsettings) eq 'HASH') {
 5052:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 5053:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 5054:                 }
 5055:             }
 5056:             $datatable .= '<td class="LC_left_item">'.
 5057:                           '<span class="LC_nobreak">'.
 5058:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 5059:                           'value="'.$currval.'" size="10" />&nbsp;'.
 5060:                           $fieldtitles{$fields[$i]}.'</span></td>';
 5061:         }
 5062:         my $colsleft = $numperrow - $rem;
 5063:         if ($colsleft > 1 ) {
 5064:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5065:                          '&nbsp;</td>';
 5066:         } elsif ($colsleft == 1) {
 5067:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 5068:         }
 5069:         $datatable .= '</tr></table></td></tr>';
 5070:         $$rowtotal ++;
 5071:     } elsif ($position eq 'middle') {
 5072:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 5073:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5074:         $usertypes->{'default'} = $othertitle;
 5075:         if (ref($types) eq 'ARRAY') {
 5076:             push(@{$types},'default');
 5077:             $usertypes->{'default'} = $othertitle;
 5078:             foreach my $status (@{$types}) {
 5079:                 $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 5080:                                                        $numinrow,$$rowtotal,$usertypes);
 5081:                 $$rowtotal ++;
 5082:             }
 5083:         }
 5084:     } else {
 5085:         my %choices = &Apache::lonlocal::texthash (
 5086:                                                       cancreate_email => 'E-mail address as username',
 5087:                                                   );
 5088:         my @toggles = sort(keys(%choices));
 5089:         my %defaultchecked = (
 5090:                                'cancreate_email' => 'off',
 5091:                              );
 5092:         my $itemcount = 0;
 5093:         my $display = 'none';
 5094:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 5095:             $display = 'block';
 5096:         }
 5097:         my $onclick = "toggleDisplay(this.form,'emailoptions');";
 5098:         my $additional = '<div id="emailoptions" style="display: '.$display.'">';
 5099:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5100:         my $usertypes = {};
 5101:         my $order = [];
 5102:         if ((ref($domdefaults{'inststatustypes'}) eq 'HASH') && (ref($domdefaults{'inststatusguest'}) eq 'ARRAY')) {
 5103:             $usertypes = $domdefaults{'inststatustypes'};
 5104:             $order = $domdefaults{'inststatusguest'};
 5105:         }
 5106:         if (ref($order) eq 'ARRAY') {
 5107:             push(@{$order},'default');
 5108:             if (@{$order} > 1) {
 5109:                 $usertypes->{'default'} = &mt('Other users');
 5110:                 $additional .= '<table><tr>';
 5111:                 foreach my $status (@{$order}) {
 5112:                     $additional .= '<th>'.$usertypes->{$status}.'</th>';
 5113:                 }
 5114:                 $additional .= '</tr><tr>';
 5115:                 foreach my $status (@{$order}) {
 5116:                     $additional .= '<td>'.&email_as_username($rowtotal,$processing,$status).'</td>';
 5117:                 }
 5118:                 $additional .= '</tr></table>';
 5119:             } else {
 5120:                 $usertypes->{'default'} = &mt('All users');
 5121:                 $additional .= &email_as_username($rowtotal,$processing);
 5122:             }
 5123:         }
 5124:         $additional .= '</div>'."\n";
 5125: 
 5126:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5127:                                                      \%choices,$$rowtotal,$onclick,$additional);
 5128:         $$rowtotal ++;
 5129:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal);
 5130:         $$rowtotal ++;
 5131:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 5132:         $numinrow = 1;
 5133:         if (ref($order) eq 'ARRAY') {
 5134:             foreach my $status (@{$order}) {
 5135:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 5136:                                                        $numinrow,$$rowtotal,$usertypes,$infofields,$infotitles);
 5137:                 $$rowtotal ++;
 5138:             }
 5139:         }
 5140:         my ($emailrules,$emailruleorder) =
 5141:             &Apache::lonnet::inst_userrules($dom,'email');
 5142:         if (ref($emailrules) eq 'HASH') {
 5143:             if (keys(%{$emailrules}) > 0) {
 5144:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 5145:                                                 $emailruleorder,$numinrow,$$rowtotal);
 5146:                 $$rowtotal ++;
 5147:             }
 5148:         }
 5149:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal);
 5150:     }
 5151:     return $datatable;
 5152: }
 5153: 
 5154: sub email_as_username {
 5155:     my ($rowtotal,$processing,$type) = @_;
 5156:     my %choices =
 5157:         &Apache::lonlocal::texthash (
 5158:                                       automatic => 'Automatic approval',
 5159:                                       approval  => 'Queued for approval',
 5160:                                     );
 5161:     my $output;
 5162:     foreach my $option ('automatic','approval') {
 5163:         my $checked;
 5164:         if (ref($processing) eq 'HASH') {
 5165:             if ($type eq '') {   
 5166:                 if (!exists($processing->{'default'})) {
 5167:                     if ($option eq 'automatic') {
 5168:                         $checked = ' checked="checked"';
 5169:                     }
 5170:                 } else {
 5171:                     if ($processing->{'default'} eq $option) {
 5172:                         $checked = ' checked="checked"';
 5173:                     }
 5174:                 }
 5175:             } else {
 5176:                 if (!exists($processing->{$type})) {
 5177:                     if ($option eq 'automatic') {
 5178:                         $checked = ' checked="checked"';
 5179:                     }
 5180:                 } else {
 5181:                     if ($processing->{$type} eq $option) {
 5182:                         $checked = ' checked="checked"';
 5183:                     }
 5184:                 }
 5185:             }
 5186:         } elsif ($option eq 'automatic') {
 5187:             $checked = ' checked="checked"'; 
 5188:         }
 5189:         my $name = 'cancreate_emailprocess';
 5190:         if (($type ne '') && ($type ne 'default')) {
 5191:             $name .= '_'.$type;
 5192:         }
 5193:         $output .= '<span class="LC_nobreak"><label>'.
 5194:                    '<input type="radio" name="'.$name.'"'.
 5195:                    $checked.' value="'.$option.'" />'.
 5196:                    $choices{$option}.'</label></span>';
 5197:         if ($type eq '') {
 5198:             $output .= '&nbsp;';
 5199:         } else {
 5200:             $output .= '<br />';
 5201:         }
 5202:     }
 5203:     $$rowtotal ++;
 5204:     return $output;
 5205: }
 5206: 
 5207: sub captcha_choice {
 5208:     my ($context,$settings,$itemcount) = @_;
 5209:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 5210:         $vertext,$currver); 
 5211:     my %lt = &captcha_phrases();
 5212:     $keyentry = 'hidden';
 5213:     if ($context eq 'cancreate') {
 5214:         $rowname = &mt('CAPTCHA validation');
 5215:     } elsif ($context eq 'login') {
 5216:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 5217:     }
 5218:     if (ref($settings) eq 'HASH') {
 5219:         if ($settings->{'captcha'}) {
 5220:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 5221:         } else {
 5222:             $checked{'original'} = ' checked="checked"';
 5223:         }
 5224:         if ($settings->{'captcha'} eq 'recaptcha') {
 5225:             $pubtext = $lt{'pub'};
 5226:             $privtext = $lt{'priv'};
 5227:             $keyentry = 'text';
 5228:             $vertext = $lt{'ver'};
 5229:             $currver = $settings->{'recaptchaversion'};
 5230:             if ($currver ne '2') {
 5231:                 $currver = 1;
 5232:             }
 5233:         }
 5234:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 5235:             $currpub = $settings->{'recaptchakeys'}{'public'};
 5236:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 5237:         }
 5238:     } else {
 5239:         $checked{'original'} = ' checked="checked"';
 5240:     }
 5241:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5242:     my $output = '<tr'.$css_class.'>'.
 5243:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 5244:                  '<table><tr><td>'."\n";
 5245:     foreach my $option ('original','recaptcha','notused') {
 5246:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 5247:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 5248:                    $lt{$option}.'</label></span>';
 5249:         unless ($option eq 'notused') {
 5250:             $output .= ('&nbsp;'x2)."\n";
 5251:         }
 5252:     }
 5253: #
 5254: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 5255: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 5256: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 5257: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 5258: #
 5259:     $output .= '</td></tr>'."\n".
 5260:                '<tr><td>'."\n".
 5261:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 5262:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 5263:                $currpub.'" size="40" /></span><br />'."\n".
 5264:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 5265:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 5266:                $currpriv.'" size="40" /></span><br />'.
 5267:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 5268:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 5269:                $currver.'" size="3" /></span><br />'.
 5270:                '</td></tr></table>'."\n".
 5271:                '</td></tr>';
 5272:     return $output;
 5273: }
 5274: 
 5275: sub user_formats_row {
 5276:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 5277:     my $output;
 5278:     my %text = (
 5279:                    'username' => 'new usernames',
 5280:                    'id'       => 'IDs',
 5281:                    'email'    => 'self-created accounts (e-mail)',
 5282:                );
 5283:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 5284:     $output = '<tr '.$css_class.'>'.
 5285:               '<td><span class="LC_nobreak">';
 5286:     if ($type eq 'email') {
 5287:         $output .= &mt("Formats disallowed for $text{$type}: ");
 5288:     } else {
 5289:         $output .= &mt("Format rules to check for $text{$type}: ");
 5290:     }
 5291:     $output .= '</span></td>'.
 5292:                '<td class="LC_left_item" colspan="2"><table>';
 5293:     my $rem;
 5294:     if (ref($ruleorder) eq 'ARRAY') {
 5295:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 5296:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 5297:                 my $rem = $i%($numinrow);
 5298:                 if ($rem == 0) {
 5299:                     if ($i > 0) {
 5300:                         $output .= '</tr>';
 5301:                     }
 5302:                     $output .= '<tr>';
 5303:                 }
 5304:                 my $check = ' ';
 5305:                 if (ref($settings) eq 'HASH') {
 5306:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 5307:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 5308:                             $check = ' checked="checked" ';
 5309:                         }
 5310:                     }
 5311:                 }
 5312:                 $output .= '<td class="LC_left_item">'.
 5313:                            '<span class="LC_nobreak"><label>'.
 5314:                            '<input type="checkbox" name="'.$type.'_rule" '.
 5315:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 5316:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 5317:             }
 5318:         }
 5319:         $rem = @{$ruleorder}%($numinrow);
 5320:     }
 5321:     my $colsleft = $numinrow - $rem;
 5322:     if ($colsleft > 1 ) {
 5323:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5324:                    '&nbsp;</td>';
 5325:     } elsif ($colsleft == 1) {
 5326:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 5327:     }
 5328:     $output .= '</tr></table></td></tr>';
 5329:     return $output;
 5330: }
 5331: 
 5332: sub usercreation_types {
 5333:     my %lt = &Apache::lonlocal::texthash (
 5334:                     author     => 'When adding a co-author',
 5335:                     course     => 'When adding a user to a course',
 5336:                     requestcrs => 'When requesting a course',
 5337:                     any        => 'Any',
 5338:                     official   => 'Institutional only ',
 5339:                     unofficial => 'Non-institutional only',
 5340:                     none       => 'None',
 5341:     );
 5342:     return %lt;
 5343: }
 5344: 
 5345: sub selfcreation_types {
 5346:     my %lt = &Apache::lonlocal::texthash (
 5347:                     selfcreate => 'User creates own account',
 5348:                     any        => 'Any',
 5349:                     official   => 'Institutional only ',
 5350:                     unofficial => 'Non-institutional only',
 5351:                     email      => 'E-mail address',
 5352:                     login      => 'Institutional Login',
 5353:                     sso        => 'SSO',
 5354:              );
 5355: }
 5356: 
 5357: sub authtype_names {
 5358:     my %lt = &Apache::lonlocal::texthash(
 5359:                       int    => 'Internal',
 5360:                       krb4   => 'Kerberos 4',
 5361:                       krb5   => 'Kerberos 5',
 5362:                       loc    => 'Local',
 5363:                   );
 5364:     return %lt;
 5365: }
 5366: 
 5367: sub context_names {
 5368:     my %context_title = &Apache::lonlocal::texthash(
 5369:        author => 'Creating users when an Author',
 5370:        course => 'Creating users when in a course',
 5371:        domain => 'Creating users when a Domain Coordinator',
 5372:     );
 5373:     return %context_title;
 5374: }
 5375: 
 5376: sub print_usermodification {
 5377:     my ($position,$dom,$settings,$rowtotal) = @_;
 5378:     my $numinrow = 4;
 5379:     my ($context,$datatable,$rowcount);
 5380:     if ($position eq 'top') {
 5381:         $rowcount = 0;
 5382:         $context = 'author'; 
 5383:         foreach my $role ('ca','aa') {
 5384:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 5385:                                                    $numinrow,$rowcount);
 5386:             $$rowtotal ++;
 5387:             $rowcount ++;
 5388:         }
 5389:     } elsif ($position eq 'bottom') {
 5390:         $context = 'course';
 5391:         $rowcount = 0;
 5392:         foreach my $role ('st','ep','ta','in','cr') {
 5393:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 5394:                                                    $numinrow,$rowcount);
 5395:             $$rowtotal ++;
 5396:             $rowcount ++;
 5397:         }
 5398:     }
 5399:     return $datatable;
 5400: }
 5401: 
 5402: sub print_defaults {
 5403:     my ($position,$dom,$settings,$rowtotal) = @_;
 5404:     my $rownum = 0;
 5405:     my ($datatable,$css_class);
 5406:     if ($position eq 'top') {
 5407:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 5408:                      'datelocale_def','portal_def');
 5409:         my %defaults;
 5410:         if (ref($settings) eq 'HASH') {
 5411:             %defaults = %{$settings};
 5412:         } else {
 5413:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 5414:             foreach my $item (@items) {
 5415:                 $defaults{$item} = $domdefaults{$item};
 5416:             }
 5417:         }
 5418:         my $titles = &defaults_titles($dom);
 5419:         foreach my $item (@items) {
 5420:             if ($rownum%2) {
 5421:                 $css_class = '';
 5422:             } else {
 5423:                 $css_class = ' class="LC_odd_row" ';
 5424:             }
 5425:             $datatable .= '<tr'.$css_class.'>'.
 5426:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 5427:                           '</span></td><td class="LC_right_item" colspan="3">';
 5428:             if ($item eq 'auth_def') {
 5429:                 my @authtypes = ('internal','krb4','krb5','localauth');
 5430:                 my %shortauth = (
 5431:                                  internal => 'int',
 5432:                                  krb4 => 'krb4',
 5433:                                  krb5 => 'krb5',
 5434:                                  localauth  => 'loc'
 5435:                                 );
 5436:                 my %authnames = &authtype_names();
 5437:                 foreach my $auth (@authtypes) {
 5438:                     my $checked = ' ';
 5439:                     if ($defaults{$item} eq $auth) {
 5440:                         $checked = ' checked="checked" ';
 5441:                     }
 5442:                     $datatable .= '<label><input type="radio" name="'.$item.
 5443:                                   '" value="'.$auth.'"'.$checked.'/>'.
 5444:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 5445:                 }
 5446:             } elsif ($item eq 'timezone_def') {
 5447:                 my $includeempty = 1;
 5448:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 5449:             } elsif ($item eq 'datelocale_def') {
 5450:                 my $includeempty = 1;
 5451:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 5452:             } elsif ($item eq 'lang_def') {
 5453:                 my $includeempty = 1;
 5454:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 5455:             } else {
 5456:                 my $size;
 5457:                 if ($item eq 'portal_def') {
 5458:                     $size = ' size="25"';
 5459:                 }
 5460:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 5461:                               $defaults{$item}.'"'.$size.' />';
 5462:             }
 5463:             $datatable .= '</td></tr>';
 5464:             $rownum ++;
 5465:         }
 5466:     } else {
 5467:         my (%defaults);
 5468:         if (ref($settings) eq 'HASH') {
 5469:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH') &&
 5470:                 (ref($settings->{'inststatusguest'}) eq 'ARRAY')) {
 5471:                 my $maxnum = @{$settings->{'inststatusorder'}};
 5472:                 for (my $i=0; $i<$maxnum; $i++) {
 5473:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 5474:                     my $item = $settings->{'inststatusorder'}->[$i];
 5475:                     my $title = $settings->{'inststatustypes'}->{$item};
 5476:                     my $guestok;
 5477:                     if (grep(/^\Q$item\E$/,@{$settings->{'inststatusguest'}})) {
 5478:                         $guestok = 1;
 5479:                     }
 5480:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 5481:                     $datatable .= '<tr'.$css_class.'>'.
 5482:                                   '<td><span class="LC_nobreak">'.
 5483:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 5484:                     for (my $k=0; $k<=$maxnum; $k++) {
 5485:                         my $vpos = $k+1;
 5486:                         my $selstr;
 5487:                         if ($k == $i) {
 5488:                             $selstr = ' selected="selected" ';
 5489:                         }
 5490:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5491:                     }
 5492:                     my ($checkedon,$checkedoff);
 5493:                     $checkedoff = ' checked="checked"';
 5494:                     if ($guestok) {
 5495:                         $checkedon = $checkedoff;
 5496:                         $checkedoff = ''; 
 5497:                     }
 5498:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 5499:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 5500:                                   &mt('delete').'</span></td>'.
 5501:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed:').
 5502:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 5503:                                   '</span></td>'.
 5504:                                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 5505:                                   '<label><input type="radio" value="1" name="inststatus_guest_'.$item.'"'.$checkedon.' />'.
 5506:                                   &mt('Yes').'</label>'.('&nbsp;'x2).
 5507:                                   '<label><input type="radio" value="0" name="inststatus_guest_'.$item.'"'.$checkedoff.' />'.
 5508:                                   &mt('No').'</label></span></td></tr>';
 5509:                 }
 5510:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 5511:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 5512:                 $datatable .= '<tr '.$css_class.'>'.
 5513:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 5514:                 for (my $k=0; $k<=$maxnum; $k++) {
 5515:                     my $vpos = $k+1;
 5516:                     my $selstr;
 5517:                     if ($k == $maxnum) {
 5518:                         $selstr = ' selected="selected" ';
 5519:                     }
 5520:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5521:                 }
 5522:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 5523:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 5524:                               '&nbsp;'.&mt('(new)').
 5525:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 5526:                               &mt('Name displayed:').
 5527:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 5528:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 5529:                               '<label><input type="radio" value="1" name="addinststatus_guest" />'.
 5530:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 5531:                               '<label><input type="radio" value="0" name="addinststatus_guest" />'.
 5532:                               &mt('No').'</label></span></td></tr>';
 5533:                               '</tr>'."\n";
 5534:                 $rownum ++;
 5535:             }
 5536:         }
 5537:     }
 5538:     $$rowtotal += $rownum;
 5539:     return $datatable;
 5540: }
 5541: 
 5542: sub get_languages_hash {
 5543:     my %langchoices;
 5544:     foreach my $id (&Apache::loncommon::languageids()) {
 5545:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 5546:         if ($code ne '') {
 5547:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 5548:         }
 5549:     }
 5550:     return %langchoices;
 5551: }
 5552: 
 5553: sub defaults_titles {
 5554:     my ($dom) = @_;
 5555:     my %titles = &Apache::lonlocal::texthash (
 5556:                    'auth_def'      => 'Default authentication type',
 5557:                    'auth_arg_def'  => 'Default authentication argument',
 5558:                    'lang_def'      => 'Default language',
 5559:                    'timezone_def'  => 'Default timezone',
 5560:                    'datelocale_def' => 'Default locale for dates',
 5561:                    'portal_def'     => 'Portal/Default URL',
 5562:                  );
 5563:     if ($dom) {
 5564:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 5565:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 5566:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 5567:         $protocol = 'http' if ($protocol ne 'https');
 5568:         if ($uint_dom) {
 5569:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 5570:                                          $uint_dom);
 5571:         }
 5572:     }
 5573:     return (\%titles);
 5574: }
 5575: 
 5576: sub print_scantronformat {
 5577:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 5578:     my $itemcount = 1;
 5579:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 5580:         %confhash);
 5581:     my $switchserver = &check_switchserver($dom,$confname);
 5582:     my %lt = &Apache::lonlocal::texthash (
 5583:                 default => 'Default bubblesheet format file error',
 5584:                 custom  => 'Custom bubblesheet format file error',
 5585:              );
 5586:     my %scantronfiles = (
 5587:         default => 'default.tab',
 5588:         custom => 'custom.tab',
 5589:     );
 5590:     foreach my $key (keys(%scantronfiles)) {
 5591:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 5592:                               .$scantronfiles{$key};
 5593:     }
 5594:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 5595:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 5596:         if (!$switchserver) {
 5597:             my $servadm = $r->dir_config('lonAdmEMail');
 5598:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 5599:             if ($configuserok eq 'ok') {
 5600:                 if ($author_ok eq 'ok') {
 5601:                     my %legacyfile = (
 5602:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 5603:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 5604:                     );
 5605:                     my %md5chk;
 5606:                     foreach my $type (keys(%legacyfile)) {
 5607:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 5608:                         chomp($md5chk{$type});
 5609:                     }
 5610:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 5611:                         foreach my $type (keys(%legacyfile)) {
 5612:                             ($scantronurls{$type},my $error) = 
 5613:                                 &legacy_scantronformat($r,$dom,$confname,
 5614:                                                  $type,$legacyfile{$type},
 5615:                                                  $scantronurls{$type},
 5616:                                                  $scantronfiles{$type});
 5617:                             if ($error ne '') {
 5618:                                 $error{$type} = $error;
 5619:                             }
 5620:                         }
 5621:                         if (keys(%error) == 0) {
 5622:                             $is_custom = 1;
 5623:                             $confhash{'scantron'}{'scantronformat'} = 
 5624:                                 $scantronurls{'custom'};
 5625:                             my $putresult = 
 5626:                                 &Apache::lonnet::put_dom('configuration',
 5627:                                                          \%confhash,$dom);
 5628:                             if ($putresult ne 'ok') {
 5629:                                 $error{'custom'} = 
 5630:                                     '<span class="LC_error">'.
 5631:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 5632:                             }
 5633:                         }
 5634:                     } else {
 5635:                         ($scantronurls{'default'},my $error) =
 5636:                             &legacy_scantronformat($r,$dom,$confname,
 5637:                                           'default',$legacyfile{'default'},
 5638:                                           $scantronurls{'default'},
 5639:                                           $scantronfiles{'default'});
 5640:                         if ($error eq '') {
 5641:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 5642:                             my $putresult =
 5643:                                 &Apache::lonnet::put_dom('configuration',
 5644:                                                          \%confhash,$dom);
 5645:                             if ($putresult ne 'ok') {
 5646:                                 $error{'default'} =
 5647:                                     '<span class="LC_error">'.
 5648:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 5649:                             }
 5650:                         } else {
 5651:                             $error{'default'} = $error;
 5652:                         }
 5653:                     }
 5654:                 }
 5655:             }
 5656:         } else {
 5657:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 5658:         }
 5659:     }
 5660:     if (ref($settings) eq 'HASH') {
 5661:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 5662:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 5663:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 5664:                 $scantronurl = '';
 5665:             } else {
 5666:                 $scantronurl = $settings->{'scantronformat'};
 5667:             }
 5668:             $is_custom = 1;
 5669:         } else {
 5670:             $scantronurl = $scantronurls{'default'};
 5671:         }
 5672:     } else {
 5673:         if ($is_custom) {
 5674:             $scantronurl = $scantronurls{'custom'};
 5675:         } else {
 5676:             $scantronurl = $scantronurls{'default'};
 5677:         }
 5678:     }
 5679:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5680:     $datatable .= '<tr'.$css_class.'>';
 5681:     if (!$is_custom) {
 5682:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 5683:                       '<span class="LC_nobreak">';
 5684:         if ($scantronurl) {
 5685:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 5686:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 5687:         } else {
 5688:             $datatable = &mt('File unavailable for display');
 5689:         }
 5690:         $datatable .= '</span></td>';
 5691:         if (keys(%error) == 0) { 
 5692:             $datatable .= '<td valign="bottom">';
 5693:             if (!$switchserver) {
 5694:                 $datatable .= &mt('Upload:').'<br />';
 5695:             }
 5696:         } else {
 5697:             my $errorstr;
 5698:             foreach my $key (sort(keys(%error))) {
 5699:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 5700:             }
 5701:             $datatable .= '<td>'.$errorstr;
 5702:         }
 5703:     } else {
 5704:         if (keys(%error) > 0) {
 5705:             my $errorstr;
 5706:             foreach my $key (sort(keys(%error))) {
 5707:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 5708:             } 
 5709:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 5710:         } elsif ($scantronurl) {
 5711:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 5712:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 5713:             $datatable .= '<td><span class="LC_nobreak">'.
 5714:                           $link.
 5715:                           '<label><input type="checkbox" name="scantronformat_del"'.
 5716:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 5717:                           '<td><span class="LC_nobreak">&nbsp;'.
 5718:                           &mt('Replace:').'</span><br />';
 5719:         }
 5720:     }
 5721:     if (keys(%error) == 0) {
 5722:         if ($switchserver) {
 5723:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 5724:         } else {
 5725:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 5726:                          '<input type="file" name="scantronformat" /></span>';
 5727:         }
 5728:     }
 5729:     $datatable .= '</td></tr>';
 5730:     $$rowtotal ++;
 5731:     return $datatable;
 5732: }
 5733: 
 5734: sub legacy_scantronformat {
 5735:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 5736:     my ($url,$error);
 5737:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 5738:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 5739:         (my $result,$url) =
 5740:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 5741:                          '','',$newfile);
 5742:         if ($result ne 'ok') {
 5743:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 5744:         }
 5745:     }
 5746:     return ($url,$error);
 5747: }
 5748: 
 5749: sub print_coursecategories {
 5750:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 5751:     my $datatable;
 5752:     if ($position eq 'top') {
 5753:         my (%checked);
 5754:         my @catitems = ('unauth','auth');
 5755:         my @cattypes = ('std','domonly','codesrch','none');
 5756:         $checked{'unauth'} = 'std';
 5757:         $checked{'auth'} = 'std';
 5758:         if (ref($settings) eq 'HASH') {
 5759:             foreach my $type (@cattypes) {
 5760:                 if ($type eq $settings->{'unauth'}) {
 5761:                     $checked{'unauth'} = $type;
 5762:                 }
 5763:                 if ($type eq $settings->{'auth'}) {
 5764:                     $checked{'auth'} = $type;
 5765:                 }
 5766:             }
 5767:         }
 5768:         my %lt = &Apache::lonlocal::texthash (
 5769:                                                unauth   => 'Catalog type for unauthenticated users',
 5770:                                                auth     => 'Catalog type for authenticated users',
 5771:                                                none     => 'No catalog',
 5772:                                                std      => 'Standard catalog',
 5773:                                                domonly  => 'Domain-only catalog',
 5774:                                                codesrch => "Code search form",
 5775:                                              );
 5776:        my $itemcount = 0;
 5777:        foreach my $item (@catitems) {
 5778:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 5779:            $datatable .= '<tr '.$css_class.'>'.
 5780:                          '<td>'.$lt{$item}.'</td>'.
 5781:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 5782:            foreach my $type (@cattypes) {
 5783:                my $ischecked;
 5784:                if ($checked{$item} eq $type) {
 5785:                    $ischecked=' checked="checked"';
 5786:                }
 5787:                $datatable .= '<label>'.
 5788:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 5789:                              ' />'.$lt{$type}.'</label>&nbsp;';
 5790:            }
 5791:            $datatable .= '</td></tr>';
 5792:            $itemcount ++;
 5793:         }
 5794:         $$rowtotal += $itemcount;
 5795:     } elsif ($position eq 'middle') {
 5796:         my $toggle_cats_crs = ' ';
 5797:         my $toggle_cats_dom = ' checked="checked" ';
 5798:         my $can_cat_crs = ' ';
 5799:         my $can_cat_dom = ' checked="checked" ';
 5800:         my $toggle_catscomm_comm = ' ';
 5801:         my $toggle_catscomm_dom = ' checked="checked" ';
 5802:         my $can_catcomm_comm = ' ';
 5803:         my $can_catcomm_dom = ' checked="checked" ';
 5804: 
 5805:         if (ref($settings) eq 'HASH') {
 5806:             if ($settings->{'togglecats'} eq 'crs') {
 5807:                 $toggle_cats_crs = $toggle_cats_dom;
 5808:                 $toggle_cats_dom = ' ';
 5809:             }
 5810:             if ($settings->{'categorize'} eq 'crs') {
 5811:                 $can_cat_crs = $can_cat_dom;
 5812:                 $can_cat_dom = ' ';
 5813:             }
 5814:             if ($settings->{'togglecatscomm'} eq 'comm') {
 5815:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 5816:                 $toggle_catscomm_dom = ' ';
 5817:             }
 5818:             if ($settings->{'categorizecomm'} eq 'comm') {
 5819:                 $can_catcomm_comm = $can_catcomm_dom;
 5820:                 $can_catcomm_dom = ' ';
 5821:             }
 5822:         }
 5823:         my %title = &Apache::lonlocal::texthash (
 5824:                      togglecats     => 'Show/Hide a course in catalog',
 5825:                      togglecatscomm => 'Show/Hide a community in catalog',
 5826:                      categorize     => 'Assign a category to a course',
 5827:                      categorizecomm => 'Assign a category to a community',
 5828:                     );
 5829:         my %level = &Apache::lonlocal::texthash (
 5830:                      dom  => 'Set in Domain',
 5831:                      crs  => 'Set in Course',
 5832:                      comm => 'Set in Community',
 5833:                     );
 5834:         $datatable = '<tr class="LC_odd_row">'.
 5835:                   '<td>'.$title{'togglecats'}.'</td>'.
 5836:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 5837:                   '<input type="radio" name="togglecats"'.
 5838:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 5839:                   '<label><input type="radio" name="togglecats"'.
 5840:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 5841:                   '</tr><tr>'.
 5842:                   '<td>'.$title{'categorize'}.'</td>'.
 5843:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 5844:                   '<label><input type="radio" name="categorize"'.
 5845:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 5846:                   '<label><input type="radio" name="categorize"'.
 5847:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 5848:                   '</tr><tr class="LC_odd_row">'.
 5849:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 5850:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 5851:                   '<input type="radio" name="togglecatscomm"'.
 5852:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 5853:                   '<label><input type="radio" name="togglecatscomm"'.
 5854:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 5855:                   '</tr><tr>'.
 5856:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 5857:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 5858:                   '<label><input type="radio" name="categorizecomm"'.
 5859:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 5860:                   '<label><input type="radio" name="categorizecomm"'.
 5861:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 5862:                   '</tr>';
 5863:         $$rowtotal += 4;
 5864:     } else {
 5865:         my $css_class;
 5866:         my $itemcount = 1;
 5867:         my $cathash; 
 5868:         if (ref($settings) eq 'HASH') {
 5869:             $cathash = $settings->{'cats'};
 5870:         }
 5871:         if (ref($cathash) eq 'HASH') {
 5872:             my (@cats,@trails,%allitems,%idx,@jsarray);
 5873:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 5874:                                                    \%allitems,\%idx,\@jsarray);
 5875:             my $maxdepth = scalar(@cats);
 5876:             my $colattrib = '';
 5877:             if ($maxdepth > 2) {
 5878:                 $colattrib = ' colspan="2" ';
 5879:             }
 5880:             my @path;
 5881:             if (@cats > 0) {
 5882:                 if (ref($cats[0]) eq 'ARRAY') {
 5883:                     my $numtop = @{$cats[0]};
 5884:                     my $maxnum = $numtop;
 5885:                     my %default_names = (
 5886:                           instcode    => &mt('Official courses'),
 5887:                           communities => &mt('Communities'),
 5888:                     );
 5889: 
 5890:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 5891:                         ($cathash->{'instcode::0'} eq '') ||
 5892:                         (!grep(/^communities$/,@{$cats[0]})) || 
 5893:                         ($cathash->{'communities::0'} eq '')) {
 5894:                         $maxnum ++;
 5895:                     }
 5896:                     my $lastidx;
 5897:                     for (my $i=0; $i<$numtop; $i++) {
 5898:                         my $parent = $cats[0][$i];
 5899:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5900:                         my $item = &escape($parent).'::0';
 5901:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 5902:                         $lastidx = $idx{$item};
 5903:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5904:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 5905:                         for (my $k=0; $k<=$maxnum; $k++) {
 5906:                             my $vpos = $k+1;
 5907:                             my $selstr;
 5908:                             if ($k == $i) {
 5909:                                 $selstr = ' selected="selected" ';
 5910:                             }
 5911:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5912:                         }
 5913:                         $datatable .= '</select></span></td><td>';
 5914:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 5915:                             $datatable .=  '<span class="LC_nobreak">'
 5916:                                            .$default_names{$parent}.'</span>';
 5917:                             if ($parent eq 'instcode') {
 5918:                                 $datatable .= '<br /><span class="LC_nobreak">('
 5919:                                               .&mt('with institutional codes')
 5920:                                               .')</span></td><td'.$colattrib.'>';
 5921:                             } else {
 5922:                                 $datatable .= '<table><tr><td>';
 5923:                             }
 5924:                             $datatable .= '<span class="LC_nobreak">'
 5925:                                           .'<label><input type="radio" name="'
 5926:                                           .$parent.'" value="1" checked="checked" />'
 5927:                                           .&mt('Display').'</label>';
 5928:                             if ($parent eq 'instcode') {
 5929:                                 $datatable .= '&nbsp;';
 5930:                             } else {
 5931:                                 $datatable .= '</span></td></tr><tr><td>'
 5932:                                               .'<span class="LC_nobreak">';
 5933:                             }
 5934:                             $datatable .= '<label><input type="radio" name="'
 5935:                                           .$parent.'" value="0" />'
 5936:                                           .&mt('Do not display').'</label></span>';
 5937:                             if ($parent eq 'communities') {
 5938:                                 $datatable .= '</td></tr></table>';
 5939:                             }
 5940:                             $datatable .= '</td>';
 5941:                         } else {
 5942:                             $datatable .= $parent
 5943:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 5944:                                           .'<input type="checkbox" name="deletecategory" '
 5945:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 5946:                         }
 5947:                         my $depth = 1;
 5948:                         push(@path,$parent);
 5949:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 5950:                         pop(@path);
 5951:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 5952:                         $itemcount ++;
 5953:                     }
 5954:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5955:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 5956:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 5957:                     for (my $k=0; $k<=$maxnum; $k++) {
 5958:                         my $vpos = $k+1;
 5959:                         my $selstr;
 5960:                         if ($k == $numtop) {
 5961:                             $selstr = ' selected="selected" ';
 5962:                         }
 5963:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5964:                     }
 5965:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 5966:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 5967:                                   .'</tr>'."\n";
 5968:                     $itemcount ++;
 5969:                     foreach my $default ('instcode','communities') {
 5970:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 5971:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5972:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 5973:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 5974:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 5975:                             for (my $k=0; $k<=$maxnum; $k++) {
 5976:                                 my $vpos = $k+1;
 5977:                                 my $selstr;
 5978:                                 if ($k == $maxnum) {
 5979:                                     $selstr = ' selected="selected" ';
 5980:                                 }
 5981:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5982:                             }
 5983:                             $datatable .= '</select></span></td>'.
 5984:                                           '<td><span class="LC_nobreak">'.
 5985:                                           $default_names{$default}.'</span>';
 5986:                             if ($default eq 'instcode') {
 5987:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 5988:                                               .&mt('with institutional codes').')</span>';
 5989:                             }
 5990:                             $datatable .= '</td>'
 5991:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 5992:                                           .&mt('Display').'</label>&nbsp;'
 5993:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 5994:                                           .&mt('Do not display').'</label></span></td></tr>';
 5995:                         }
 5996:                     }
 5997:                 }
 5998:             } else {
 5999:                 $datatable .= &initialize_categories($itemcount);
 6000:             }
 6001:         } else {
 6002:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td>'
 6003:                           .&initialize_categories($itemcount);
 6004:         }
 6005:         $$rowtotal += $itemcount;
 6006:     }
 6007:     return $datatable;
 6008: }
 6009: 
 6010: sub print_serverstatuses {
 6011:     my ($dom,$settings,$rowtotal) = @_;
 6012:     my $datatable;
 6013:     my @pages = &serverstatus_pages();
 6014:     my (%namedaccess,%machineaccess);
 6015:     foreach my $type (@pages) {
 6016:         $namedaccess{$type} = '';
 6017:         $machineaccess{$type}= '';
 6018:     }
 6019:     if (ref($settings) eq 'HASH') {
 6020:         foreach my $type (@pages) {
 6021:             if (exists($settings->{$type})) {
 6022:                 if (ref($settings->{$type}) eq 'HASH') {
 6023:                     foreach my $key (keys(%{$settings->{$type}})) {
 6024:                         if ($key eq 'namedusers') {
 6025:                             $namedaccess{$type} = $settings->{$type}->{$key};
 6026:                         } elsif ($key eq 'machines') {
 6027:                             $machineaccess{$type} = $settings->{$type}->{$key};
 6028:                         }
 6029:                     }
 6030:                 }
 6031:             }
 6032:         }
 6033:     }
 6034:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 6035:     my $rownum = 0;
 6036:     my $css_class;
 6037:     foreach my $type (@pages) {
 6038:         $rownum ++;
 6039:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 6040:         $datatable .= '<tr'.$css_class.'>'.
 6041:                       '<td><span class="LC_nobreak">'.
 6042:                       $titles->{$type}.'</span></td>'.
 6043:                       '<td class="LC_left_item">'.
 6044:                       '<input type="text" name="'.$type.'_namedusers" '.
 6045:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 6046:                       '<td class="LC_right_item">'.
 6047:                       '<span class="LC_nobreak">'.
 6048:                       '<input type="text" name="'.$type.'_machines" '.
 6049:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 6050:                       '</td></tr>'."\n";
 6051:     }
 6052:     $$rowtotal += $rownum;
 6053:     return $datatable;
 6054: }
 6055: 
 6056: sub serverstatus_pages {
 6057:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 6058:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 6059:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 6060:             'uniquecodes','diskusage','coursecatalog');
 6061: }
 6062: 
 6063: sub defaults_javascript {
 6064:     my ($settings) = @_;
 6065:     return unless (ref($settings) eq 'HASH');
 6066:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 6067:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 6068:         if ($maxnum eq '') {
 6069:             $maxnum = 0;
 6070:         }
 6071:         $maxnum ++;
 6072:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 6073:         return <<"ENDSCRIPT";
 6074: <script type="text/javascript">
 6075: // <![CDATA[
 6076: function reorderTypes(form,caller) {
 6077:     var changedVal;
 6078: $jstext 
 6079:     var newpos = 'addinststatus_pos';
 6080:     var current = new Array;
 6081:     var maxh = $maxnum;
 6082:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 6083:     var oldVal;
 6084:     if (caller == newpos) {
 6085:         changedVal = newitemVal;
 6086:     } else {
 6087:         var curritem = 'inststatus_pos_'+caller;
 6088:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 6089:         current[newitemVal] = newpos;
 6090:     }
 6091:     for (var i=0; i<inststatuses.length; i++) {
 6092:         if (inststatuses[i] != caller) {
 6093:             var elementName = 'inststatus_pos_'+inststatuses[i];
 6094:             if (form.elements[elementName]) {
 6095:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 6096:                 current[currVal] = elementName;
 6097:             }
 6098:         }
 6099:     }
 6100:     for (var j=0; j<maxh; j++) {
 6101:         if (current[j] == undefined) {
 6102:             oldVal = j;
 6103:         }
 6104:     }
 6105:     if (oldVal < changedVal) {
 6106:         for (var k=oldVal+1; k<=changedVal ; k++) {
 6107:            var elementName = current[k];
 6108:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 6109:         }
 6110:     } else {
 6111:         for (var k=changedVal; k<oldVal; k++) {
 6112:             var elementName = current[k];
 6113:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 6114:         }
 6115:     }
 6116:     return;
 6117: }
 6118: 
 6119: // ]]>
 6120: </script>
 6121: 
 6122: ENDSCRIPT
 6123:     }
 6124: }
 6125: 
 6126: sub coursecategories_javascript {
 6127:     my ($settings) = @_;
 6128:     my ($output,$jstext,$cathash);
 6129:     if (ref($settings) eq 'HASH') {
 6130:         $cathash = $settings->{'cats'};
 6131:     }
 6132:     if (ref($cathash) eq 'HASH') {
 6133:         my (@cats,@jsarray,%idx);
 6134:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 6135:         if (@jsarray > 0) {
 6136:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 6137:             for (my $i=0; $i<@jsarray; $i++) {
 6138:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 6139:                     my $catstr = join('","',@{$jsarray[$i]});
 6140:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 6141:                 }
 6142:             }
 6143:         }
 6144:     } else {
 6145:         $jstext  = '    var categories = Array(1);'."\n".
 6146:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 6147:     }
 6148:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 6149:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 6150:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
 6151:     &js_escape(\$instcode_reserved);
 6152:     &js_escape(\$communities_reserved);
 6153:     &js_escape(\$choose_again);
 6154:     $output = <<"ENDSCRIPT";
 6155: <script type="text/javascript">
 6156: // <![CDATA[
 6157: function reorderCats(form,parent,item,idx) {
 6158:     var changedVal;
 6159: $jstext
 6160:     var newpos = 'addcategory_pos';
 6161:     if (parent == '') {
 6162:         var has_instcode = 0;
 6163:         var maxtop = categories[idx].length;
 6164:         for (var j=0; j<maxtop; j++) {
 6165:             if (categories[idx][j] == 'instcode::0') {
 6166:                 has_instcode == 1;
 6167:             }
 6168:         }
 6169:         if (has_instcode == 0) {
 6170:             categories[idx][maxtop] = 'instcode_pos';
 6171:         }
 6172:     } else {
 6173:         newpos += '_'+parent;
 6174:     }
 6175:     var maxh = 1 + categories[idx].length;
 6176:     var current = new Array;
 6177:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 6178:     if (item == newpos) {
 6179:         changedVal = newitemVal;
 6180:     } else {
 6181:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 6182:         current[newitemVal] = newpos;
 6183:     }
 6184:     for (var i=0; i<categories[idx].length; i++) {
 6185:         var elementName = categories[idx][i];
 6186:         if (elementName != item) {
 6187:             if (form.elements[elementName]) {
 6188:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 6189:                 current[currVal] = elementName;
 6190:             }
 6191:         }
 6192:     }
 6193:     var oldVal;
 6194:     for (var j=0; j<maxh; j++) {
 6195:         if (current[j] == undefined) {
 6196:             oldVal = j;
 6197:         }
 6198:     }
 6199:     if (oldVal < changedVal) {
 6200:         for (var k=oldVal+1; k<=changedVal ; k++) {
 6201:            var elementName = current[k];
 6202:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 6203:         }
 6204:     } else {
 6205:         for (var k=changedVal; k<oldVal; k++) {
 6206:             var elementName = current[k];
 6207:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 6208:         }
 6209:     }
 6210:     return;
 6211: }
 6212: 
 6213: function categoryCheck(form) {
 6214:     if (form.elements['addcategory_name'].value == 'instcode') {
 6215:         alert('$instcode_reserved\\n$choose_again');
 6216:         return false;
 6217:     }
 6218:     if (form.elements['addcategory_name'].value == 'communities') {
 6219:         alert('$communities_reserved\\n$choose_again');
 6220:         return false;
 6221:     }
 6222:     return true;
 6223: }
 6224: 
 6225: // ]]>
 6226: </script>
 6227: 
 6228: ENDSCRIPT
 6229:     return $output;
 6230: }
 6231: 
 6232: sub initialize_categories {
 6233:     my ($itemcount) = @_;
 6234:     my ($datatable,$css_class,$chgstr);
 6235:     my %default_names = (
 6236:                       instcode    => 'Official courses (with institutional codes)',
 6237:                       communities => 'Communities',
 6238:                         );
 6239:     my $select0 = ' selected="selected"';
 6240:     my $select1 = '';
 6241:     foreach my $default ('instcode','communities') {
 6242:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6243:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 6244:         if ($default eq 'communities') {
 6245:             $select1 = $select0;
 6246:             $select0 = '';
 6247:         }
 6248:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6249:                      .'<select name="'.$default.'_pos">'
 6250:                      .'<option value="0"'.$select0.'>1</option>'
 6251:                      .'<option value="1"'.$select1.'>2</option>'
 6252:                      .'<option value="2">3</option></select>&nbsp;'
 6253:                      .$default_names{$default}
 6254:                      .'</span></td><td><span class="LC_nobreak">'
 6255:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 6256:                      .&mt('Display').'</label>&nbsp;<label>'
 6257:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 6258:                  .'</label></span></td></tr>';
 6259:         $itemcount ++;
 6260:     }
 6261:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6262:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 6263:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6264:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 6265:                   .'<option value="0">1</option>'
 6266:                   .'<option value="1">2</option>'
 6267:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 6268:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 6269:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 6270:     return $datatable;
 6271: }
 6272: 
 6273: sub build_category_rows {
 6274:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 6275:     my ($text,$name,$item,$chgstr);
 6276:     if (ref($cats) eq 'ARRAY') {
 6277:         my $maxdepth = scalar(@{$cats});
 6278:         if (ref($cats->[$depth]) eq 'HASH') {
 6279:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 6280:                 my $numchildren = @{$cats->[$depth]{$parent}};
 6281:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6282:                 $text .= '<td><table class="LC_data_table">';
 6283:                 my ($idxnum,$parent_name,$parent_item);
 6284:                 my $higher = $depth - 1;
 6285:                 if ($higher == 0) {
 6286:                     $parent_name = &escape($parent).'::'.$higher;
 6287:                 } else {
 6288:                     if (ref($path) eq 'ARRAY') {
 6289:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 6290:                     }
 6291:                 }
 6292:                 $parent_item = 'addcategory_pos_'.$parent_name;
 6293:                 for (my $j=0; $j<=$numchildren; $j++) {
 6294:                     if ($j < $numchildren) {
 6295:                         $name = $cats->[$depth]{$parent}[$j];
 6296:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 6297:                         $idxnum = $idx->{$item};
 6298:                     } else {
 6299:                         $name = $parent_name;
 6300:                         $item = $parent_item;
 6301:                     }
 6302:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 6303:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 6304:                     for (my $i=0; $i<=$numchildren; $i++) {
 6305:                         my $vpos = $i+1;
 6306:                         my $selstr;
 6307:                         if ($j == $i) {
 6308:                             $selstr = ' selected="selected" ';
 6309:                         }
 6310:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 6311:                     }
 6312:                     $text .= '</select>&nbsp;';
 6313:                     if ($j < $numchildren) {
 6314:                         my $deeper = $depth+1;
 6315:                         $text .= $name.'&nbsp;'
 6316:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 6317:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 6318:                         if(ref($path) eq 'ARRAY') {
 6319:                             push(@{$path},$name);
 6320:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 6321:                             pop(@{$path});
 6322:                         }
 6323:                     } else {
 6324:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 6325:                         if ($j == $numchildren) {
 6326:                             $text .= $name;
 6327:                         } else {
 6328:                             $text .= $item;
 6329:                         }
 6330:                         $text .= '" value="" />';
 6331:                     }
 6332:                     $text .= '</td></tr>';
 6333:                 }
 6334:                 $text .= '</table></td>';
 6335:             } else {
 6336:                 my $higher = $depth-1;
 6337:                 if ($higher == 0) {
 6338:                     $name = &escape($parent).'::'.$higher;
 6339:                 } else {
 6340:                     if (ref($path) eq 'ARRAY') {
 6341:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 6342:                     }
 6343:                 }
 6344:                 my $colspan;
 6345:                 if ($parent ne 'instcode') {
 6346:                     $colspan = $maxdepth - $depth - 1;
 6347:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 6348:                 }
 6349:             }
 6350:         }
 6351:     }
 6352:     return $text;
 6353: }
 6354: 
 6355: sub modifiable_userdata_row {
 6356:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref) = @_;
 6357:     my ($role,$rolename,$statustype);
 6358:     $role = $item;
 6359:     if ($context eq 'cancreate') {
 6360:         if ($item =~ /^emailusername_(.+)$/) {
 6361:             $statustype = $1;
 6362:             $role = 'emailusername';
 6363:             if (ref($usertypes) eq 'HASH') {
 6364:                 if ($usertypes->{$statustype}) {
 6365:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 6366:                 } else {
 6367:                     $rolename = &mt('Data provided by user');
 6368:                 }
 6369:             }
 6370:         }
 6371:     } elsif ($context eq 'selfcreate') {
 6372:         if (ref($usertypes) eq 'HASH') {
 6373:             $rolename = $usertypes->{$role};
 6374:         } else {
 6375:             $rolename = $role;
 6376:         }
 6377:     } else {
 6378:         if ($role eq 'cr') {
 6379:             $rolename = &mt('Custom role');
 6380:         } else {
 6381:             $rolename = &Apache::lonnet::plaintext($role);
 6382:         }
 6383:     }
 6384:     my (@fields,%fieldtitles);
 6385:     if (ref($fieldsref) eq 'ARRAY') {
 6386:         @fields = @{$fieldsref};
 6387:     } else {
 6388:         @fields = ('lastname','firstname','middlename','generation',
 6389:                    'permanentemail','id');
 6390:     }
 6391:     if ((ref($titlesref) eq 'HASH')) {
 6392:         %fieldtitles = %{$titlesref};
 6393:     } else {
 6394:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 6395:     }
 6396:     my $output;
 6397:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 6398:     $output = '<tr '.$css_class.'>'.
 6399:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 6400:               '<td class="LC_left_item" colspan="2"><table>';
 6401:     my $rem;
 6402:     my %checks;
 6403:     if (ref($settings) eq 'HASH') {
 6404:         if (ref($settings->{$context}) eq 'HASH') {
 6405:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 6406:                 my $hashref = $settings->{$context}->{$role};
 6407:                 if ($role eq 'emailusername') {
 6408:                     if ($statustype) {
 6409:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 6410:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 6411:                             if (ref($hashref) eq 'HASH') { 
 6412:                                 foreach my $field (@fields) {
 6413:                                     if ($hashref->{$field}) {
 6414:                                         $checks{$field} = $hashref->{$field};
 6415:                                     }
 6416:                                 }
 6417:                             }
 6418:                         }
 6419:                     }
 6420:                 } else {
 6421:                     if (ref($hashref) eq 'HASH') {
 6422:                         foreach my $field (@fields) {
 6423:                             if ($hashref->{$field}) {
 6424:                                 $checks{$field} = ' checked="checked" ';
 6425:                             }
 6426:                         }
 6427:                     }
 6428:                 }
 6429:             }
 6430:         }
 6431:     }
 6432:      
 6433:     for (my $i=0; $i<@fields; $i++) {
 6434:         my $rem = $i%($numinrow);
 6435:         if ($rem == 0) {
 6436:             if ($i > 0) {
 6437:                 $output .= '</tr>';
 6438:             }
 6439:             $output .= '<tr>';
 6440:         }
 6441:         my $check = ' ';
 6442:         unless ($role eq 'emailusername') {
 6443:             if (exists($checks{$fields[$i]})) {
 6444:                 $check = $checks{$fields[$i]}
 6445:             } else {
 6446:                 if ($role eq 'st') {
 6447:                     if (ref($settings) ne 'HASH') {
 6448:                         $check = ' checked="checked" '; 
 6449:                     }
 6450:                 }
 6451:             }
 6452:         }
 6453:         $output .= '<td class="LC_left_item">'.
 6454:                    '<span class="LC_nobreak">';
 6455:         if ($role eq 'emailusername') {
 6456:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 6457:                 $checks{$fields[$i]} = 'omit';
 6458:             }
 6459:             foreach my $option ('required','optional','omit') {
 6460:                 my $checked='';
 6461:                 if ($checks{$fields[$i]} eq $option) {
 6462:                     $checked='checked="checked" ';
 6463:                 }
 6464:                 $output .= '<label>'.
 6465:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 6466:                            &mt($option).'</label>'.('&nbsp;' x2);
 6467:             }
 6468:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 6469:         } else {
 6470:             $output .= '<label>'.
 6471:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 6472:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 6473:                        '</label>';
 6474:         }
 6475:         $output .= '</span></td>';
 6476:         $rem = @fields%($numinrow);
 6477:     }
 6478:     my $colsleft = $numinrow - $rem;
 6479:     if ($colsleft > 1 ) {
 6480:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 6481:                    '&nbsp;</td>';
 6482:     } elsif ($colsleft == 1) {
 6483:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 6484:     }
 6485:     $output .= '</tr></table></td></tr>';
 6486:     return $output;
 6487: }
 6488: 
 6489: sub insttypes_row {
 6490:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rownum) = @_;
 6491:     my %lt = &Apache::lonlocal::texthash (
 6492:                       cansearch => 'Users allowed to search',
 6493:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 6494:                       lockablenames => 'User preference to lock name',
 6495:              );
 6496:     my $showdom;
 6497:     if ($context eq 'cansearch') {
 6498:         $showdom = ' ('.$dom.')';
 6499:     }
 6500:     my $class = 'LC_left_item';
 6501:     if ($context eq 'statustocreate') {
 6502:         $class = 'LC_right_item';
 6503:     }
 6504:     my $css_class = ' class="LC_odd_row"';
 6505:     if ($rownum ne '') { 
 6506:         $css_class = ($rownum%2? ' class="LC_odd_row"':'');
 6507:     }
 6508:     my $output = '<tr'.$css_class.'>'.
 6509:                  '<td>'.$lt{$context}.$showdom.
 6510:                  '</td><td class="'.$class.'" colspan="2"><table>';
 6511:     my $rem;
 6512:     if (ref($types) eq 'ARRAY') {
 6513:         for (my $i=0; $i<@{$types}; $i++) {
 6514:             if (defined($usertypes->{$types->[$i]})) {
 6515:                 my $rem = $i%($numinrow);
 6516:                 if ($rem == 0) {
 6517:                     if ($i > 0) {
 6518:                         $output .= '</tr>';
 6519:                     }
 6520:                     $output .= '<tr>';
 6521:                 }
 6522:                 my $check = ' ';
 6523:                 if (ref($settings) eq 'HASH') {
 6524:                     if (ref($settings->{$context}) eq 'ARRAY') {
 6525:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 6526:                             $check = ' checked="checked" ';
 6527:                         }
 6528:                     } elsif ($context eq 'statustocreate') {
 6529:                         $check = ' checked="checked" ';
 6530:                     }
 6531:                 }
 6532:                 $output .= '<td class="LC_left_item">'.
 6533:                            '<span class="LC_nobreak"><label>'.
 6534:                            '<input type="checkbox" name="'.$context.'" '.
 6535:                            'value="'.$types->[$i].'"'.$check.'/>'.
 6536:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 6537:             }
 6538:         }
 6539:         $rem = @{$types}%($numinrow);
 6540:     }
 6541:     my $colsleft = $numinrow - $rem;
 6542:     if (($rem == 0) && (@{$types} > 0)) {
 6543:         $output .= '<tr>';
 6544:     }
 6545:     if ($colsleft > 1) {
 6546:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 6547:     } else {
 6548:         $output .= '<td class="LC_left_item">';
 6549:     }
 6550:     my $defcheck = ' ';
 6551:     if (ref($settings) eq 'HASH') {  
 6552:         if (ref($settings->{$context}) eq 'ARRAY') {
 6553:             if (grep(/^default$/,@{$settings->{$context}})) {
 6554:                 $defcheck = ' checked="checked" ';
 6555:             }
 6556:         } elsif ($context eq 'statustocreate') {
 6557:             $defcheck = ' checked="checked" ';
 6558:         }
 6559:     }
 6560:     $output .= '<span class="LC_nobreak"><label>'.
 6561:                '<input type="checkbox" name="'.$context.'" '.
 6562:                'value="default"'.$defcheck.'/>'.
 6563:                $othertitle.'</label></span></td>'.
 6564:                '</tr></table></td></tr>';
 6565:     return $output;
 6566: }
 6567: 
 6568: sub sorted_searchtitles {
 6569:     my %searchtitles = &Apache::lonlocal::texthash(
 6570:                          'uname' => 'username',
 6571:                          'lastname' => 'last name',
 6572:                          'lastfirst' => 'last name, first name',
 6573:                      );
 6574:     my @titleorder = ('uname','lastname','lastfirst');
 6575:     return (\%searchtitles,\@titleorder);
 6576: }
 6577: 
 6578: sub sorted_searchtypes {
 6579:     my %srchtypes_desc = (
 6580:                            exact    => 'is exact match',
 6581:                            contains => 'contains ..',
 6582:                            begins   => 'begins with ..',
 6583:                          );
 6584:     my @srchtypeorder = ('exact','begins','contains');
 6585:     return (\%srchtypes_desc,\@srchtypeorder);
 6586: }
 6587: 
 6588: sub usertype_update_row {
 6589:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 6590:     my $datatable;
 6591:     my $numinrow = 4;
 6592:     foreach my $type (@{$types}) {
 6593:         if (defined($usertypes->{$type})) {
 6594:             $$rownums ++;
 6595:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 6596:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 6597:                           '</td><td class="LC_left_item"><table>';
 6598:             for (my $i=0; $i<@{$fields}; $i++) {
 6599:                 my $rem = $i%($numinrow);
 6600:                 if ($rem == 0) {
 6601:                     if ($i > 0) {
 6602:                         $datatable .= '</tr>';
 6603:                     }
 6604:                     $datatable .= '<tr>';
 6605:                 }
 6606:                 my $check = ' ';
 6607:                 if (ref($settings) eq 'HASH') {
 6608:                     if (ref($settings->{'fields'}) eq 'HASH') {
 6609:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 6610:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 6611:                                 $check = ' checked="checked" ';
 6612:                             }
 6613:                         }
 6614:                     }
 6615:                 }
 6616: 
 6617:                 if ($i == @{$fields}-1) {
 6618:                     my $colsleft = $numinrow - $rem;
 6619:                     if ($colsleft > 1) {
 6620:                         $datatable .= '<td colspan="'.$colsleft.'">';
 6621:                     } else {
 6622:                         $datatable .= '<td>';
 6623:                     }
 6624:                 } else {
 6625:                     $datatable .= '<td>';
 6626:                 }
 6627:                 $datatable .= '<span class="LC_nobreak"><label>'.
 6628:                               '<input type="checkbox" name="updateable_'.$type.
 6629:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 6630:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 6631:             }
 6632:             $datatable .= '</tr></table></td></tr>';
 6633:         }
 6634:     }
 6635:     return $datatable;
 6636: }
 6637: 
 6638: sub modify_login {
 6639:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 6640:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 6641:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 6642:     %title = ( coursecatalog => 'Display course catalog',
 6643:                adminmail => 'Display administrator E-mail address',
 6644:                helpdesk  => 'Display "Contact Helpdesk" link',
 6645:                newuser => 'Link for visitors to create a user account',
 6646:                loginheader => 'Log-in box header');
 6647:     @offon = ('off','on');
 6648:     if (ref($domconfig{login}) eq 'HASH') {
 6649:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 6650:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 6651:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 6652:             }
 6653:         }
 6654:     }
 6655:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 6656:                                            \%domconfig,\%loginhash);
 6657:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 6658:     foreach my $item (@toggles) {
 6659:         $loginhash{login}{$item} = $env{'form.'.$item};
 6660:     }
 6661:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 6662:     if (ref($colchanges{'login'}) eq 'HASH') {  
 6663:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 6664:                                          \%loginhash);
 6665:     }
 6666: 
 6667:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 6668:     my %domservers = &Apache::lonnet::get_servers($dom);
 6669:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 6670:     if (keys(%servers) > 1) {
 6671:         foreach my $lonhost (keys(%servers)) {
 6672:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 6673:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 6674:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 6675:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 6676:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 6677:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 6678:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 6679:                         $changes{'loginvia'}{$lonhost} = 1;
 6680:                     } else {
 6681:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 6682:                         $changes{'loginvia'}{$lonhost} = 1;
 6683:                     }
 6684:                 } else {
 6685:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 6686:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 6687:                         $changes{'loginvia'}{$lonhost} = 1;
 6688:                     }
 6689:                 }
 6690:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 6691:                     foreach my $item (@loginvia_attribs) {
 6692:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 6693:                     }
 6694:                 } else {
 6695:                     foreach my $item (@loginvia_attribs) {
 6696:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 6697:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 6698:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 6699:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 6700:                                 $new = '/';
 6701:                             }
 6702:                         }
 6703:                         if (($item eq 'custompath') && 
 6704:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 6705:                             $new = '';
 6706:                         }
 6707:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 6708:                             $changes{'loginvia'}{$lonhost} = 1;
 6709:                         }
 6710:                         if ($item eq 'exempt') {
 6711:                             $new = &check_exempt_addresses($new);
 6712:                         }
 6713:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 6714:                     }
 6715:                 }
 6716:             } else {
 6717:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 6718:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 6719:                     $changes{'loginvia'}{$lonhost} = 1;
 6720:                     foreach my $item (@loginvia_attribs) {
 6721:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 6722:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 6723:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 6724:                                 $new = '/';
 6725:                             }
 6726:                         }
 6727:                         if (($item eq 'custompath') && 
 6728:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 6729:                             $new = '';
 6730:                         }
 6731:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 6732:                     }
 6733:                 }
 6734:             }
 6735:         }
 6736:     }
 6737: 
 6738:     my $servadm = $r->dir_config('lonAdmEMail');
 6739:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 6740:     if (ref($domconfig{'login'}) eq 'HASH') {
 6741:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 6742:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 6743:                 if ($lang eq 'nolang') {
 6744:                     push(@currlangs,$lang);
 6745:                 } elsif (defined($langchoices{$lang})) {
 6746:                     push(@currlangs,$lang);
 6747:                 } else {
 6748:                     next;
 6749:                 }
 6750:             }
 6751:         }
 6752:     }
 6753:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 6754:     if (@currlangs > 0) {
 6755:         foreach my $lang (@currlangs) {
 6756:             if (grep(/^\Q$lang\E$/,@delurls)) {
 6757:                 $changes{'helpurl'}{$lang} = 1;
 6758:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 6759:                 $changes{'helpurl'}{$lang} = 1;
 6760:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 6761:                 push(@newlangs,$lang);
 6762:             } else {
 6763:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 6764:             }
 6765:         }
 6766:     }
 6767:     unless (grep(/^nolang$/,@currlangs)) {
 6768:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 6769:             $changes{'helpurl'}{'nolang'} = 1;
 6770:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 6771:             push(@newlangs,'nolang');
 6772:         }
 6773:     }
 6774:     if ($env{'form.loginhelpurl_add_lang'}) {
 6775:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 6776:             ($env{'form.loginhelpurl_add_file.filename'})) {
 6777:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 6778:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 6779:         }
 6780:     }
 6781:     if ((@newlangs > 0) || ($addedfile)) {
 6782:         my $error;
 6783:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 6784:         if ($configuserok eq 'ok') {
 6785:             if ($switchserver) {
 6786:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 6787:             } elsif ($author_ok eq 'ok') {
 6788:                 my @allnew = @newlangs;
 6789:                 if ($addedfile ne '') {
 6790:                     push(@allnew,$addedfile);
 6791:                 }
 6792:                 foreach my $lang (@allnew) {
 6793:                     my $formelem = 'loginhelpurl_'.$lang;
 6794:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 6795:                         $formelem = 'loginhelpurl_add_file';
 6796:                     }
 6797:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 6798:                                                                "help/$lang",'','',$newfile{$lang});
 6799:                     if ($result eq 'ok') {
 6800:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 6801:                         $changes{'helpurl'}{$lang} = 1;
 6802:                     } else {
 6803:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 6804:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 6805:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 6806:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 6807:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 6808:                         }
 6809:                     }
 6810:                 }
 6811:             } else {
 6812:                 $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);
 6813:             }
 6814:         } else {
 6815:             $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);
 6816:         }
 6817:         if ($error) {
 6818:             &Apache::lonnet::logthis($error);
 6819:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6820:         }
 6821:     }
 6822: 
 6823:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
 6824:     if (ref($domconfig{'login'}) eq 'HASH') {
 6825:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
 6826:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
 6827:                 if ($domservers{$lonhost}) {
 6828:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 6829:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
 6830:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
 6831:                     }
 6832:                 }
 6833:             }
 6834:         }
 6835:     }
 6836:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
 6837:     foreach my $lonhost (sort(keys(%domservers))) {
 6838:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 6839:             $changes{'headtag'}{$lonhost} = 1;
 6840:         } else {
 6841:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
 6842:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
 6843:             }
 6844:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
 6845:                 push(@newhosts,$lonhost);
 6846:             } elsif ($currheadtagurls{$lonhost}) {
 6847:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
 6848:                 if ($currexempt{$lonhost}) {
 6849:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
 6850:                         $changes{'headtag'}{$lonhost} = 1;
 6851:                     }
 6852:                 } elsif ($possexempt{$lonhost}) {
 6853:                     $changes{'headtag'}{$lonhost} = 1;
 6854:                 }
 6855:                 if ($possexempt{$lonhost}) {
 6856:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 6857:                 }
 6858:             }
 6859:         }
 6860:     }
 6861:     if (@newhosts) {
 6862:         my $error;
 6863:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 6864:         if ($configuserok eq 'ok') {
 6865:             if ($switchserver) {
 6866:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
 6867:             } elsif ($author_ok eq 'ok') {
 6868:                 foreach my $lonhost (@newhosts) {
 6869:                     my $formelem = 'loginheadtag_'.$lonhost;
 6870:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 6871:                                                                           "login/headtag/$lonhost",'','',
 6872:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
 6873:                     if ($result eq 'ok') {
 6874:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
 6875:                         $changes{'headtag'}{$lonhost} = 1;
 6876:                         if ($possexempt{$lonhost}) {
 6877:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 6878:                         }
 6879:                     } else {
 6880:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
 6881:                                            $newheadtagurls{$lonhost},$result);
 6882:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 6883:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
 6884:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
 6885:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
 6886:                         }
 6887:                     }
 6888:                 }
 6889:             } else {
 6890:                 $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);
 6891:             }
 6892:         } else {
 6893:             $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);
 6894:         }
 6895:         if ($error) {
 6896:             &Apache::lonnet::logthis($error);
 6897:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6898:         }
 6899:     }
 6900:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 6901: 
 6902:     my $defaulthelpfile = '/adm/loginproblems.html';
 6903:     my $defaulttext = &mt('Default in use');
 6904: 
 6905:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 6906:                                              $dom);
 6907:     if ($putresult eq 'ok') {
 6908:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 6909:         my %defaultchecked = (
 6910:                     'coursecatalog' => 'on',
 6911:                     'helpdesk'      => 'on',
 6912:                     'adminmail'     => 'off',
 6913:                     'newuser'       => 'off',
 6914:         );
 6915:         if (ref($domconfig{'login'}) eq 'HASH') {
 6916:             foreach my $item (@toggles) {
 6917:                 if ($defaultchecked{$item} eq 'on') { 
 6918:                     if (($domconfig{'login'}{$item} eq '0') &&
 6919:                         ($env{'form.'.$item} eq '1')) {
 6920:                         $changes{$item} = 1;
 6921:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 6922:                               $domconfig{'login'}{$item} eq '1') &&
 6923:                              ($env{'form.'.$item} eq '0')) {
 6924:                         $changes{$item} = 1;
 6925:                     }
 6926:                 } elsif ($defaultchecked{$item} eq 'off') {
 6927:                     if (($domconfig{'login'}{$item} eq '1') &&
 6928:                         ($env{'form.'.$item} eq '0')) {
 6929:                         $changes{$item} = 1;
 6930:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 6931:                               $domconfig{'login'}{$item} eq '0') &&
 6932:                              ($env{'form.'.$item} eq '1')) {
 6933:                         $changes{$item} = 1;
 6934:                     }
 6935:                 }
 6936:             }
 6937:         }
 6938:         if (keys(%changes) > 0 || $colchgtext) {
 6939:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 6940:             if (ref($lastactref) eq 'HASH') {
 6941:                 $lastactref->{'domainconfig'} = 1;
 6942:             }
 6943:             $resulttext = &mt('Changes made:').'<ul>';
 6944:             foreach my $item (sort(keys(%changes))) {
 6945:                 if ($item eq 'loginvia') {
 6946:                     if (ref($changes{$item}) eq 'HASH') {
 6947:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 6948:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 6949:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 6950:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 6951:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 6952:                                     $protocol = 'http' if ($protocol ne 'https');
 6953:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 6954: 
 6955:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 6956:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 6957:                                     } else {
 6958:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 6959:                                     }
 6960:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 6961:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 6962:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 6963:                                     }
 6964:                                     $resulttext .= '</li>';
 6965:                                 } else {
 6966:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 6967:                                 }
 6968:                             } else {
 6969:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 6970:                             }
 6971:                         }
 6972:                         $resulttext .= '</ul></li>';
 6973:                     }
 6974:                 } elsif ($item eq 'helpurl') {
 6975:                     if (ref($changes{$item}) eq 'HASH') {
 6976:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 6977:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 6978:                                 my ($chg,$link);
 6979:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 6980:                                 if ($lang eq 'nolang') {
 6981:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 6982:                                 } else {
 6983:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 6984:                                 }
 6985:                                 $resulttext .= '<li>'.$chg.'</li>';
 6986:                             } else {
 6987:                                 my $chg;
 6988:                                 if ($lang eq 'nolang') {
 6989:                                     $chg = &mt('custom log-in help file for no preferred language');
 6990:                                 } else {
 6991:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 6992:                                 }
 6993:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 6994:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 6995:                                                       '?inhibitmenu=yes',$chg,600,500).
 6996:                                                '</li>';
 6997:                             }
 6998:                         }
 6999:                     }
 7000:                 } elsif ($item eq 'headtag') {
 7001:                     if (ref($changes{$item}) eq 'HASH') {
 7002:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 7003:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 7004:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
 7005:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 7006:                                 $resulttext .= '<li><a href="'.
 7007:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
 7008:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 7009:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
 7010:                                 if ($possexempt{$lonhost}) {
 7011:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
 7012:                                 } else {
 7013:                                     $resulttext .= &mt('included for any client IP');
 7014:                                 }
 7015:                                 $resulttext .= '</li>';
 7016:                             }
 7017:                         }
 7018:                     }
 7019:                 } elsif ($item eq 'captcha') {
 7020:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7021:                         my $chgtxt;
 7022:                         if ($loginhash{'login'}{$item} eq 'notused') {
 7023:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 7024:                         } else {
 7025:                             my %captchas = &captcha_phrases();
 7026:                             if ($captchas{$loginhash{'login'}{$item}}) {
 7027:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 7028:                             } else {
 7029:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 7030:                             }
 7031:                         }
 7032:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 7033:                     }
 7034:                 } elsif ($item eq 'recaptchakeys') {
 7035:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7036:                         my ($privkey,$pubkey);
 7037:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 7038:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 7039:                             $privkey = $loginhash{'login'}{$item}{'private'};
 7040:                         }
 7041:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 7042:                         if (!$pubkey) {
 7043:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 7044:                         } else {
 7045:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 7046:                         }
 7047:                         if (!$privkey) {
 7048:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 7049:                         } else {
 7050:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 7051:                         }
 7052:                         $chgtxt .= '</ul>';
 7053:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 7054:                     }
 7055:                 } elsif ($item eq 'recaptchaversion') {
 7056:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7057:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
 7058:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
 7059:                                            '</li>';
 7060:                         }
 7061:                     }
 7062:                 } else {
 7063:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 7064:                 }
 7065:             }
 7066:             $resulttext .= $colchgtext.'</ul>';
 7067:         } else {
 7068:             $resulttext = &mt('No changes made to log-in page settings');
 7069:         }
 7070:     } else {
 7071:         $resulttext = '<span class="LC_error">'.
 7072: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7073:     }
 7074:     if ($errors) {
 7075:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 7076:                        $errors.'</ul>';
 7077:     }
 7078:     return $resulttext;
 7079: }
 7080: 
 7081: sub check_exempt_addresses {
 7082:     my ($iplist) = @_;
 7083:     $iplist =~ s/^\s+//;
 7084:     $iplist =~ s/\s+$//;
 7085:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
 7086:     my (@okips,$new);
 7087:     foreach my $ip (@poss_ips) {
 7088:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 7089:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 7090:                 push(@okips,$ip);
 7091:             }
 7092:         }
 7093:     }
 7094:     if (@okips > 0) {
 7095:         $new = join(',',@okips);
 7096:     } else {
 7097:         $new = '';
 7098:     }
 7099:     return $new;
 7100: }
 7101: 
 7102: sub color_font_choices {
 7103:     my %choices =
 7104:         &Apache::lonlocal::texthash (
 7105:             img => "Header",
 7106:             bgs => "Background colors",
 7107:             links => "Link colors",
 7108:             images => "Images",
 7109:             font => "Font color",
 7110:             fontmenu => "Font menu",
 7111:             pgbg => "Page",
 7112:             tabbg => "Header",
 7113:             sidebg => "Border",
 7114:             link => "Link",
 7115:             alink => "Active link",
 7116:             vlink => "Visited link",
 7117:         );
 7118:     return %choices;
 7119: }
 7120: 
 7121: sub modify_rolecolors {
 7122:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 7123:     my ($resulttext,%rolehash);
 7124:     $rolehash{'rolecolors'} = {};
 7125:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 7126:         if ($domconfig{'rolecolors'} eq '') {
 7127:             $domconfig{'rolecolors'} = {};
 7128:         }
 7129:     }
 7130:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 7131:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 7132:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 7133:                                              $dom);
 7134:     if ($putresult eq 'ok') {
 7135:         if (keys(%changes) > 0) {
 7136:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 7137:             if (ref($lastactref) eq 'HASH') {
 7138:                 $lastactref->{'domainconfig'} = 1;
 7139:             }
 7140:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 7141:                                              $rolehash{'rolecolors'});
 7142:         } else {
 7143:             $resulttext = &mt('No changes made to default color schemes');
 7144:         }
 7145:     } else {
 7146:         $resulttext = '<span class="LC_error">'.
 7147: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7148:     }
 7149:     if ($errors) {
 7150:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 7151:                        $errors.'</ul>';
 7152:     }
 7153:     return $resulttext;
 7154: }
 7155: 
 7156: sub modify_colors {
 7157:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 7158:     my (%changes,%choices);
 7159:     my @bgs;
 7160:     my @links = ('link','alink','vlink');
 7161:     my @logintext;
 7162:     my @images;
 7163:     my $servadm = $r->dir_config('lonAdmEMail');
 7164:     my $errors;
 7165:     my %defaults;
 7166:     foreach my $role (@{$roles}) {
 7167:         if ($role eq 'login') {
 7168:             %choices = &login_choices();
 7169:             @logintext = ('textcol','bgcol');
 7170:         } else {
 7171:             %choices = &color_font_choices();
 7172:         }
 7173:         if ($role eq 'login') {
 7174:             @images = ('img','logo','domlogo','login');
 7175:             @bgs = ('pgbg','mainbg','sidebg');
 7176:         } else {
 7177:             @images = ('img');
 7178:             @bgs = ('pgbg','tabbg','sidebg');
 7179:         }
 7180:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 7181:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 7182:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 7183:         }
 7184:         if ($role eq 'login') {
 7185:             foreach my $item (@logintext) {
 7186:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 7187:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 7188:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 7189:                 }
 7190:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 7191:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 7192:                 }
 7193:             }
 7194:         } else {
 7195:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 7196:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 7197:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 7198:             }
 7199:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 7200:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 7201:             }
 7202:         }
 7203:         foreach my $item (@bgs) {
 7204:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 7205:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 7206:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 7207:             }
 7208:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 7209:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 7210:             }
 7211:         }
 7212:         foreach my $item (@links) {
 7213:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 7214:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 7215:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 7216:             }
 7217:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 7218:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 7219:             }
 7220:         }
 7221:         my ($configuserok,$author_ok,$switchserver) = 
 7222:             &config_check($dom,$confname,$servadm);
 7223:         my ($width,$height) = &thumb_dimensions();
 7224:         if (ref($domconfig->{$role}) ne 'HASH') {
 7225:             $domconfig->{$role} = {};
 7226:         }
 7227:         foreach my $img (@images) {
 7228:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 7229:                 if (defined($env{'form.login_showlogo_'.$img})) {
 7230:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 7231:                 } else { 
 7232:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 7233:                 }
 7234:             } 
 7235: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 7236: 		 && !defined($domconfig->{$role}{$img})
 7237: 		 && !$env{'form.'.$role.'_del_'.$img}
 7238: 		 && $env{'form.'.$role.'_import_'.$img}) {
 7239: 		# import the old configured image from the .tab setting
 7240: 		# if they haven't provided a new one 
 7241: 		$domconfig->{$role}{$img} = 
 7242: 		    $env{'form.'.$role.'_import_'.$img};
 7243: 	    }
 7244:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 7245:                 my $error;
 7246:                 if ($configuserok eq 'ok') {
 7247:                     if ($switchserver) {
 7248:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 7249:                     } else {
 7250:                         if ($author_ok eq 'ok') {
 7251:                             my ($result,$logourl) = 
 7252:                                 &publishlogo($r,'upload',$role.'_'.$img,
 7253:                                            $dom,$confname,$img,$width,$height);
 7254:                             if ($result eq 'ok') {
 7255:                                 $confhash->{$role}{$img} = $logourl;
 7256:                                 $changes{$role}{'images'}{$img} = 1;
 7257:                             } else {
 7258:                                 $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);
 7259:                             }
 7260:                         } else {
 7261:                             $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);
 7262:                         }
 7263:                     }
 7264:                 } else {
 7265:                     $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);
 7266:                 }
 7267:                 if ($error) {
 7268:                     &Apache::lonnet::logthis($error);
 7269:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7270:                 }
 7271:             } elsif ($domconfig->{$role}{$img} ne '') {
 7272:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 7273:                     my $error;
 7274:                     if ($configuserok eq 'ok') {
 7275: # is confname an author?
 7276:                         if ($switchserver eq '') {
 7277:                             if ($author_ok eq 'ok') {
 7278:                                 my ($result,$logourl) = 
 7279:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 7280:                                             $dom,$confname,$img,$width,$height);
 7281:                                 if ($result eq 'ok') {
 7282:                                     $confhash->{$role}{$img} = $logourl;
 7283: 				    $changes{$role}{'images'}{$img} = 1;
 7284:                                 }
 7285:                             }
 7286:                         }
 7287:                     }
 7288:                 }
 7289:             }
 7290:         }
 7291:         if (ref($domconfig) eq 'HASH') {
 7292:             if (ref($domconfig->{$role}) eq 'HASH') {
 7293:                 foreach my $img (@images) {
 7294:                     if ($domconfig->{$role}{$img} ne '') {
 7295:                         if ($env{'form.'.$role.'_del_'.$img}) {
 7296:                             $confhash->{$role}{$img} = '';
 7297:                             $changes{$role}{'images'}{$img} = 1;
 7298:                         } else {
 7299:                             if ($confhash->{$role}{$img} eq '') {
 7300:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 7301:                             }
 7302:                         }
 7303:                     } else {
 7304:                         if ($env{'form.'.$role.'_del_'.$img}) {
 7305:                             $confhash->{$role}{$img} = '';
 7306:                             $changes{$role}{'images'}{$img} = 1;
 7307:                         } 
 7308:                     }
 7309:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 7310:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 7311:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 7312:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 7313:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 7314:                             }
 7315:                         } else {
 7316:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 7317:                                 $changes{$role}{'showlogo'}{$img} = 1;
 7318:                             }
 7319:                         }
 7320:                     }
 7321:                 }
 7322:                 if ($domconfig->{$role}{'font'} ne '') {
 7323:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 7324:                         $changes{$role}{'font'} = 1;
 7325:                     }
 7326:                 } else {
 7327:                     if ($confhash->{$role}{'font'}) {
 7328:                         $changes{$role}{'font'} = 1;
 7329:                     }
 7330:                 }
 7331:                 if ($role ne 'login') {
 7332:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 7333:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 7334:                             $changes{$role}{'fontmenu'} = 1;
 7335:                         }
 7336:                     } else {
 7337:                         if ($confhash->{$role}{'fontmenu'}) {
 7338:                             $changes{$role}{'fontmenu'} = 1;
 7339:                         }
 7340:                     }
 7341:                 }
 7342:                 foreach my $item (@bgs) {
 7343:                     if ($domconfig->{$role}{$item} ne '') {
 7344:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 7345:                             $changes{$role}{'bgs'}{$item} = 1;
 7346:                         } 
 7347:                     } else {
 7348:                         if ($confhash->{$role}{$item}) {
 7349:                             $changes{$role}{'bgs'}{$item} = 1;
 7350:                         }
 7351:                     }
 7352:                 }
 7353:                 foreach my $item (@links) {
 7354:                     if ($domconfig->{$role}{$item} ne '') {
 7355:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 7356:                             $changes{$role}{'links'}{$item} = 1;
 7357:                         }
 7358:                     } else {
 7359:                         if ($confhash->{$role}{$item}) {
 7360:                             $changes{$role}{'links'}{$item} = 1;
 7361:                         }
 7362:                     }
 7363:                 }
 7364:                 foreach my $item (@logintext) {
 7365:                     if ($domconfig->{$role}{$item} ne '') {
 7366:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 7367:                             $changes{$role}{'logintext'}{$item} = 1;
 7368:                         }
 7369:                     } else {
 7370:                         if ($confhash->{$role}{$item}) {
 7371:                             $changes{$role}{'logintext'}{$item} = 1;
 7372:                         }
 7373:                     }
 7374:                 }
 7375:             } else {
 7376:                 &default_change_checker($role,\@images,\@links,\@bgs,
 7377:                                         \@logintext,$confhash,\%changes); 
 7378:             }
 7379:         } else {
 7380:             &default_change_checker($role,\@images,\@links,\@bgs,
 7381:                                     \@logintext,$confhash,\%changes); 
 7382:         }
 7383:     }
 7384:     return ($errors,%changes);
 7385: }
 7386: 
 7387: sub config_check {
 7388:     my ($dom,$confname,$servadm) = @_;
 7389:     my ($configuserok,$author_ok,$switchserver,%currroles);
 7390:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 7391:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 7392:                                                    $confname,$servadm);
 7393:     if ($configuserok eq 'ok') {
 7394:         $switchserver = &check_switchserver($dom,$confname);
 7395:         if ($switchserver eq '') {
 7396:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 7397:         }
 7398:     }
 7399:     return ($configuserok,$author_ok,$switchserver);
 7400: }
 7401: 
 7402: sub default_change_checker {
 7403:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 7404:     foreach my $item (@{$links}) {
 7405:         if ($confhash->{$role}{$item}) {
 7406:             $changes->{$role}{'links'}{$item} = 1;
 7407:         }
 7408:     }
 7409:     foreach my $item (@{$bgs}) {
 7410:         if ($confhash->{$role}{$item}) {
 7411:             $changes->{$role}{'bgs'}{$item} = 1;
 7412:         }
 7413:     }
 7414:     foreach my $item (@{$logintext}) {
 7415:         if ($confhash->{$role}{$item}) {
 7416:             $changes->{$role}{'logintext'}{$item} = 1;
 7417:         }
 7418:     }
 7419:     foreach my $img (@{$images}) {
 7420:         if ($env{'form.'.$role.'_del_'.$img}) {
 7421:             $confhash->{$role}{$img} = '';
 7422:             $changes->{$role}{'images'}{$img} = 1;
 7423:         }
 7424:         if ($role eq 'login') {
 7425:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 7426:                 $changes->{$role}{'showlogo'}{$img} = 1;
 7427:             }
 7428:         }
 7429:     }
 7430:     if ($confhash->{$role}{'font'}) {
 7431:         $changes->{$role}{'font'} = 1;
 7432:     }
 7433: }
 7434: 
 7435: sub display_colorchgs {
 7436:     my ($dom,$changes,$roles,$confhash) = @_;
 7437:     my (%choices,$resulttext);
 7438:     if (!grep(/^login$/,@{$roles})) {
 7439:         $resulttext = &mt('Changes made:').'<br />';
 7440:     }
 7441:     foreach my $role (@{$roles}) {
 7442:         if ($role eq 'login') {
 7443:             %choices = &login_choices();
 7444:         } else {
 7445:             %choices = &color_font_choices();
 7446:         }
 7447:         if (ref($changes->{$role}) eq 'HASH') {
 7448:             if ($role ne 'login') {
 7449:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 7450:             }
 7451:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 7452:                 if ($role ne 'login') {
 7453:                     $resulttext .= '<ul>';
 7454:                 }
 7455:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 7456:                     if ($role ne 'login') {
 7457:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 7458:                     }
 7459:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 7460:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 7461:                             if ($confhash->{$role}{$key}{$item}) {
 7462:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 7463:                             } else {
 7464:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 7465:                             }
 7466:                         } elsif ($confhash->{$role}{$item} eq '') {
 7467:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 7468:                         } else {
 7469:                             my $newitem = $confhash->{$role}{$item};
 7470:                             if ($key eq 'images') {
 7471:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 7472:                             }
 7473:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 7474:                         }
 7475:                     }
 7476:                     if ($role ne 'login') {
 7477:                         $resulttext .= '</ul></li>';
 7478:                     }
 7479:                 } else {
 7480:                     if ($confhash->{$role}{$key} eq '') {
 7481:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 7482:                     } else {
 7483:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 7484:                     }
 7485:                 }
 7486:                 if ($role ne 'login') {
 7487:                     $resulttext .= '</ul>';
 7488:                 }
 7489:             }
 7490:         }
 7491:     }
 7492:     return $resulttext;
 7493: }
 7494: 
 7495: sub thumb_dimensions {
 7496:     return ('200','50');
 7497: }
 7498: 
 7499: sub check_dimensions {
 7500:     my ($inputfile) = @_;
 7501:     my ($fullwidth,$fullheight);
 7502:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 7503:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 7504:             my $imageinfo = <PIPE>;
 7505:             if (!close(PIPE)) {
 7506:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 7507:             }
 7508:             chomp($imageinfo);
 7509:             my ($fullsize) = 
 7510:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 7511:             if ($fullsize) {
 7512:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 7513:             }
 7514:         }
 7515:     }
 7516:     return ($fullwidth,$fullheight);
 7517: }
 7518: 
 7519: sub check_configuser {
 7520:     my ($uhome,$dom,$confname,$servadm) = @_;
 7521:     my ($configuserok,%currroles);
 7522:     if ($uhome eq 'no_host') {
 7523:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 7524:         my $configpass = &LONCAPA::Enrollment::create_password();
 7525:         $configuserok = 
 7526:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 7527:                              $configpass,'','','','','',undef,$servadm);
 7528:     } else {
 7529:         $configuserok = 'ok';
 7530:         %currroles = 
 7531:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 7532:     }
 7533:     return ($configuserok,%currroles);
 7534: }
 7535: 
 7536: sub check_authorstatus {
 7537:     my ($dom,$confname,%currroles) = @_;
 7538:     my $author_ok;
 7539:     if (!$currroles{':'.$dom.':au'}) {
 7540:         my $start = time;
 7541:         my $end = 0;
 7542:         $author_ok = 
 7543:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 7544:                                         'au',$end,$start,'','','domconfig');
 7545:     } else {
 7546:         $author_ok = 'ok';
 7547:     }
 7548:     return $author_ok;
 7549: }
 7550: 
 7551: sub publishlogo {
 7552:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 7553:     my ($output,$fname,$logourl);
 7554:     if ($action eq 'upload') {
 7555:         $fname=$env{'form.'.$formname.'.filename'};
 7556:         chop($env{'form.'.$formname});
 7557:     } else {
 7558:         ($fname) = ($formname =~ /([^\/]+)$/);
 7559:     }
 7560:     if ($savefileas ne '') {
 7561:         $fname = $savefileas;
 7562:     }
 7563:     $fname=&Apache::lonnet::clean_filename($fname);
 7564: # See if there is anything left
 7565:     unless ($fname) { return ('error: no uploaded file'); }
 7566:     $fname="$subdir/$fname";
 7567:     my $docroot=$r->dir_config('lonDocRoot');
 7568:     my $filepath="$docroot/priv";
 7569:     my $relpath = "$dom/$confname";
 7570:     my ($fnamepath,$file,$fetchthumb);
 7571:     $file=$fname;
 7572:     if ($fname=~m|/|) {
 7573:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 7574:     }
 7575:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 7576:     my $count;
 7577:     for ($count=5;$count<=$#parts;$count++) {
 7578:         $filepath.="/$parts[$count]";
 7579:         if ((-e $filepath)!=1) {
 7580:             mkdir($filepath,02770);
 7581:         }
 7582:     }
 7583:     # Check for bad extension and disallow upload
 7584:     if ($file=~/\.(\w+)$/ &&
 7585:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 7586:         $output = 
 7587:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 7588:     } elsif ($file=~/\.(\w+)$/ &&
 7589:         !defined(&Apache::loncommon::fileembstyle($1))) {
 7590:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 7591:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 7592:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 7593:     } elsif (-d "$filepath/$file") {
 7594:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 7595:     } else {
 7596:         my $source = $filepath.'/'.$file;
 7597:         my $logfile;
 7598:         if (!open($logfile,">>$source".'.log')) {
 7599:             return (&mt('No write permission to Authoring Space'));
 7600:         }
 7601:         print $logfile
 7602: "\n================= Publish ".localtime()." ================\n".
 7603: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 7604: # Save the file
 7605:         if (!open(FH,'>'.$source)) {
 7606:             &Apache::lonnet::logthis('Failed to create '.$source);
 7607:             return (&mt('Failed to create file'));
 7608:         }
 7609:         if ($action eq 'upload') {
 7610:             if (!print FH ($env{'form.'.$formname})) {
 7611:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 7612:                 return (&mt('Failed to write file'));
 7613:             }
 7614:         } else {
 7615:             my $original = &Apache::lonnet::filelocation('',$formname);
 7616:             if(!copy($original,$source)) {
 7617:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 7618:                 return (&mt('Failed to write file'));
 7619:             }
 7620:         }
 7621:         close(FH);
 7622:         chmod(0660, $source); # Permissions to rw-rw---.
 7623: 
 7624:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 7625:         my $copyfile=$targetdir.'/'.$file;
 7626: 
 7627:         my @parts=split(/\//,$targetdir);
 7628:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 7629:         for (my $count=5;$count<=$#parts;$count++) {
 7630:             $path.="/$parts[$count]";
 7631:             if (!-e $path) {
 7632:                 print $logfile "\nCreating directory ".$path;
 7633:                 mkdir($path,02770);
 7634:             }
 7635:         }
 7636:         my $versionresult;
 7637:         if (-e $copyfile) {
 7638:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 7639:         } else {
 7640:             $versionresult = 'ok';
 7641:         }
 7642:         if ($versionresult eq 'ok') {
 7643:             if (copy($source,$copyfile)) {
 7644:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 7645:                 $output = 'ok';
 7646:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 7647:                 push(@{$modified_urls},[$copyfile,$source]);
 7648:                 my $metaoutput = 
 7649:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 7650:                 unless ($registered_cleanup) {
 7651:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 7652:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 7653:                     $registered_cleanup=1;
 7654:                 }
 7655:             } else {
 7656:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 7657:                 $output = &mt('Failed to copy file to RES space').", $!";
 7658:             }
 7659:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 7660:                 my $inputfile = $filepath.'/'.$file;
 7661:                 my $outfile = $filepath.'/'.'tn-'.$file;
 7662:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 7663:                 if ($fullwidth ne '' && $fullheight ne '') { 
 7664:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 7665:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 7666:                         system("convert -sample $thumbsize $inputfile $outfile");
 7667:                         chmod(0660, $filepath.'/tn-'.$file);
 7668:                         if (-e $outfile) {
 7669:                             my $copyfile=$targetdir.'/tn-'.$file;
 7670:                             if (copy($outfile,$copyfile)) {
 7671:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 7672:                                 my $thumb_metaoutput = 
 7673:                                     &write_metadata($dom,$confname,$formname,
 7674:                                                     $targetdir,'tn-'.$file,$logfile);
 7675:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 7676:                                 unless ($registered_cleanup) {
 7677:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 7678:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 7679:                                     $registered_cleanup=1;
 7680:                                 }
 7681:                             } else {
 7682:                                 print $logfile "\nUnable to write ".$copyfile.
 7683:                                                ':'.$!."\n";
 7684:                             }
 7685:                         }
 7686:                     }
 7687:                 }
 7688:             }
 7689:         } else {
 7690:             $output = $versionresult;
 7691:         }
 7692:     }
 7693:     return ($output,$logourl);
 7694: }
 7695: 
 7696: sub logo_versioning {
 7697:     my ($targetdir,$file,$logfile) = @_;
 7698:     my $target = $targetdir.'/'.$file;
 7699:     my ($maxversion,$fn,$extn,$output);
 7700:     $maxversion = 0;
 7701:     if ($file =~ /^(.+)\.(\w+)$/) {
 7702:         $fn=$1;
 7703:         $extn=$2;
 7704:     }
 7705:     opendir(DIR,$targetdir);
 7706:     while (my $filename=readdir(DIR)) {
 7707:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 7708:             $maxversion=($1>$maxversion)?$1:$maxversion;
 7709:         }
 7710:     }
 7711:     $maxversion++;
 7712:     print $logfile "\nCreating old version ".$maxversion."\n";
 7713:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 7714:     if (copy($target,$copyfile)) {
 7715:         print $logfile "Copied old target to ".$copyfile."\n";
 7716:         $copyfile=$copyfile.'.meta';
 7717:         if (copy($target.'.meta',$copyfile)) {
 7718:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 7719:             $output = 'ok';
 7720:         } else {
 7721:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 7722:             $output = &mt('Failed to copy old meta').", $!, ";
 7723:         }
 7724:     } else {
 7725:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 7726:         $output = &mt('Failed to copy old target').", $!, ";
 7727:     }
 7728:     return $output;
 7729: }
 7730: 
 7731: sub write_metadata {
 7732:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 7733:     my (%metadatafields,%metadatakeys,$output);
 7734:     $metadatafields{'title'}=$formname;
 7735:     $metadatafields{'creationdate'}=time;
 7736:     $metadatafields{'lastrevisiondate'}=time;
 7737:     $metadatafields{'copyright'}='public';
 7738:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 7739:                                          $env{'user.domain'};
 7740:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 7741:     $metadatafields{'domain'}=$dom;
 7742:     {
 7743:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 7744:         my $mfh;
 7745:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 7746:             foreach (sort(keys(%metadatafields))) {
 7747:                 unless ($_=~/\./) {
 7748:                     my $unikey=$_;
 7749:                     $unikey=~/^([A-Za-z]+)/;
 7750:                     my $tag=$1;
 7751:                     $tag=~tr/A-Z/a-z/;
 7752:                     print $mfh "\n\<$tag";
 7753:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 7754:                         my $value=$metadatafields{$unikey.'.'.$_};
 7755:                         $value=~s/\"/\'\'/g;
 7756:                         print $mfh ' '.$_.'="'.$value.'"';
 7757:                     }
 7758:                     print $mfh '>'.
 7759:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 7760:                             .'</'.$tag.'>';
 7761:                 }
 7762:             }
 7763:             $output = 'ok';
 7764:             print $logfile "\nWrote metadata";
 7765:             close($mfh);
 7766:         } else {
 7767:             print $logfile "\nFailed to open metadata file";
 7768:             $output = &mt('Could not write metadata');
 7769:         }
 7770:     }
 7771:     return $output;
 7772: }
 7773: 
 7774: sub notifysubscribed {
 7775:     foreach my $targetsource (@{$modified_urls}){
 7776:         next unless (ref($targetsource) eq 'ARRAY');
 7777:         my ($target,$source)=@{$targetsource};
 7778:         if ($source ne '') {
 7779:             if (open(my $logfh,'>>'.$source.'.log')) {
 7780:                 print $logfh "\nCleanup phase: Notifications\n";
 7781:                 my @subscribed=&subscribed_hosts($target);
 7782:                 foreach my $subhost (@subscribed) {
 7783:                     print $logfh "\nNotifying host ".$subhost.':';
 7784:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 7785:                     print $logfh $reply;
 7786:                 }
 7787:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 7788:                 foreach my $subhost (@subscribedmeta) {
 7789:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 7790:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 7791:                                                         $subhost);
 7792:                     print $logfh $reply;
 7793:                 }
 7794:                 print $logfh "\n============ Done ============\n";
 7795:                 close($logfh);
 7796:             }
 7797:         }
 7798:     }
 7799:     return OK;
 7800: }
 7801: 
 7802: sub subscribed_hosts {
 7803:     my ($target) = @_;
 7804:     my @subscribed;
 7805:     if (open(my $fh,"<$target.subscription")) {
 7806:         while (my $subline=<$fh>) {
 7807:             if ($subline =~ /^($match_lonid):/) {
 7808:                 my $host = $1;
 7809:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 7810:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 7811:                         push(@subscribed,$host);
 7812:                     }
 7813:                 }
 7814:             }
 7815:         }
 7816:     }
 7817:     return @subscribed;
 7818: }
 7819: 
 7820: sub check_switchserver {
 7821:     my ($dom,$confname) = @_;
 7822:     my ($allowed,$switchserver);
 7823:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 7824:     if ($home eq 'no_host') {
 7825:         $home = &Apache::lonnet::domain($dom,'primary');
 7826:     }
 7827:     my @ids=&Apache::lonnet::current_machine_ids();
 7828:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 7829:     if (!$allowed) {
 7830: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 7831:     }
 7832:     return $switchserver;
 7833: }
 7834: 
 7835: sub modify_quotas {
 7836:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 7837:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 7838:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 7839:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 7840:         $validationfieldsref);
 7841:     if ($action eq 'quotas') {
 7842:         $context = 'tools'; 
 7843:     } else {
 7844:         $context = $action;
 7845:     }
 7846:     if ($context eq 'requestcourses') {
 7847:         @usertools = ('official','unofficial','community','textbook');
 7848:         @options =('norequest','approval','validate','autolimit');
 7849:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 7850:         %titles = &courserequest_titles();
 7851:         $toolregexp = join('|',@usertools);
 7852:         %conditions = &courserequest_conditions();
 7853:         $confname = $dom.'-domainconfig';
 7854:         my $servadm = $r->dir_config('lonAdmEMail');
 7855:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 7856:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 7857:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 7858:     } elsif ($context eq 'requestauthor') {
 7859:         @usertools = ('author');
 7860:         %titles = &authorrequest_titles();
 7861:     } else {
 7862:         @usertools = ('aboutme','blog','webdav','portfolio');
 7863:         %titles = &tool_titles();
 7864:     }
 7865:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 7866:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7867:     foreach my $key (keys(%env)) {
 7868:         if ($context eq 'requestcourses') {
 7869:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 7870:                 my $item = $1;
 7871:                 my $type = $2;
 7872:                 if ($type =~ /^limit_(.+)/) {
 7873:                     $limithash{$item}{$1} = $env{$key};
 7874:                 } else {
 7875:                     $confhash{$item}{$type} = $env{$key};
 7876:                 }
 7877:             }
 7878:         } elsif ($context eq 'requestauthor') {
 7879:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 7880:                 $confhash{$1} = $env{$key};
 7881:             }
 7882:         } else {
 7883:             if ($key =~ /^form\.quota_(.+)$/) {
 7884:                 $confhash{'defaultquota'}{$1} = $env{$key};
 7885:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 7886:                 $confhash{'authorquota'}{$1} = $env{$key};
 7887:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 7888:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 7889:             }
 7890:         }
 7891:     }
 7892:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 7893:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 7894:         @approvalnotify = sort(@approvalnotify);
 7895:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 7896:         my @crstypes = ('official','unofficial','community','textbook');
 7897:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 7898:         foreach my $type (@hasuniquecode) {
 7899:             if (grep(/^\Q$type\E$/,@crstypes)) {
 7900:                 $confhash{'uniquecode'}{$type} = 1;
 7901:             }
 7902:         }
 7903:         my (%newbook,%allpos);
 7904:         if ($context eq 'requestcourses') {
 7905:             foreach my $type ('textbooks','templates') {
 7906:                 @{$allpos{$type}} = (); 
 7907:                 my $invalid;
 7908:                 if ($type eq 'textbooks') {
 7909:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
 7910:                 } else {
 7911:                     $invalid = &mt('Invalid LON-CAPA course for template');
 7912:                 }
 7913:                 if ($env{'form.'.$type.'_addbook'}) {
 7914:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
 7915:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
 7916:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
 7917:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
 7918:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 7919:                         } else {
 7920:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
 7921:                             my $position = $env{'form.'.$type.'_addbook_pos'};
 7922:                             $position =~ s/\D+//g;
 7923:                             if ($position ne '') {
 7924:                                 $allpos{$type}[$position] = $newbook{$type};
 7925:                             }
 7926:                         }
 7927:                     } else {
 7928:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 7929:                     }
 7930:                 }
 7931:             } 
 7932:         }
 7933:         if (ref($domconfig{$action}) eq 'HASH') {
 7934:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 7935:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 7936:                     $changes{'notify'}{'approval'} = 1;
 7937:                 }
 7938:             } else {
 7939:                 if ($confhash{'notify'}{'approval'}) {
 7940:                     $changes{'notify'}{'approval'} = 1;
 7941:                 }
 7942:             }
 7943:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 7944:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 7945:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 7946:                         unless ($confhash{'uniquecode'}{$crstype}) {
 7947:                             $changes{'uniquecode'} = 1;
 7948:                         }
 7949:                     }
 7950:                     unless ($changes{'uniquecode'}) {
 7951:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 7952:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 7953:                                 $changes{'uniquecode'} = 1;
 7954:                             }
 7955:                         }
 7956:                     }
 7957:                } else {
 7958:                    $changes{'uniquecode'} = 1;
 7959:                }
 7960:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 7961:                 $changes{'uniquecode'} = 1;
 7962:             }
 7963:             if ($context eq 'requestcourses') {
 7964:                 foreach my $type ('textbooks','templates') {
 7965:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
 7966:                         my %deletions;
 7967:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
 7968:                         if (@todelete) {
 7969:                             map { $deletions{$_} = 1; } @todelete;
 7970:                         }
 7971:                         my %imgdeletions;
 7972:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
 7973:                         if (@todeleteimages) {
 7974:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
 7975:                         }
 7976:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
 7977:                         for (my $i=0; $i<=$maxnum; $i++) {
 7978:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
 7979:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
 7980:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
 7981:                                 if ($deletions{$key}) {
 7982:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
 7983:                                         #FIXME need to obsolete item in RES space
 7984:                                     }
 7985:                                     next;
 7986:                                 } else {
 7987:                                     my $newpos = $env{'form.'.$itemid};
 7988:                                     $newpos =~ s/\D+//g;
 7989:                                     foreach my $item ('subject','title','publisher','author') {
 7990:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 7991:                                                  ($type eq 'templates'));
 7992:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
 7993:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
 7994:                                             $changes{$type}{$key} = 1;
 7995:                                         }
 7996:                                     }
 7997:                                     $allpos{$type}[$newpos] = $key;
 7998:                                 }
 7999:                                 if ($imgdeletions{$key}) {
 8000:                                     $changes{$type}{$key} = 1;
 8001:                                     #FIXME need to obsolete item in RES space
 8002:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
 8003:                                     my ($cdom,$cnum) = split(/_/,$key);
 8004:                                     my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
 8005:                                                                                   $cdom,$cnum,$type,$configuserok,
 8006:                                                                                   $switchserver,$author_ok);
 8007:                                     if ($imgurl) {
 8008:                                         $confhash{$type}{$key}{'image'} = $imgurl;
 8009:                                         $changes{$type}{$key} = 1; 
 8010:                                     }
 8011:                                     if ($error) {
 8012:                                         &Apache::lonnet::logthis($error);
 8013:                                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8014:                                     } 
 8015:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
 8016:                                     $confhash{$type}{$key}{'image'} = 
 8017:                                         $domconfig{$action}{$type}{$key}{'image'};
 8018:                                 }
 8019:                             }
 8020:                         }
 8021:                     }
 8022:                 }
 8023:             }
 8024:         } else {
 8025:             if ($confhash{'notify'}{'approval'}) {
 8026:                 $changes{'notify'}{'approval'} = 1;
 8027:             }
 8028:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 8029:                 $changes{'uniquecode'} = 1;
 8030:             }
 8031:         }
 8032:         if ($context eq 'requestcourses') {
 8033:             foreach my $type ('textbooks','templates') {
 8034:                 if ($newbook{$type}) {
 8035:                     $changes{$type}{$newbook{$type}} = 1;
 8036:                     foreach my $item ('subject','title','publisher','author') {
 8037:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 8038:                                  ($type eq 'template'));
 8039:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
 8040:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
 8041:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
 8042:                         }
 8043:                     }
 8044:                     if ($type eq 'textbooks') {
 8045:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
 8046:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
 8047:                             my ($imageurl,$error) =
 8048:                                 &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
 8049:                                                         $configuserok,$switchserver,$author_ok);
 8050:                             if ($imageurl) {
 8051:                                 $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
 8052:                             }
 8053:                             if ($error) {
 8054:                                 &Apache::lonnet::logthis($error);
 8055:                                 $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8056:                             }
 8057:                         }
 8058:                     }
 8059:                 }
 8060:                 if (@{$allpos{$type}} > 0) {
 8061:                     my $idx = 0;
 8062:                     foreach my $item (@{$allpos{$type}}) {
 8063:                         if ($item ne '') {
 8064:                             $confhash{$type}{$item}{'order'} = $idx;
 8065:                             if (ref($domconfig{$action}) eq 'HASH') {
 8066:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
 8067:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
 8068:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
 8069:                                             $changes{$type}{$item} = 1;
 8070:                                         }
 8071:                                     }
 8072:                                 }
 8073:                             }
 8074:                             $idx ++;
 8075:                         }
 8076:                     }
 8077:                 }
 8078:             }
 8079:             if (ref($validationitemsref) eq 'ARRAY') {
 8080:                 foreach my $item (@{$validationitemsref}) {
 8081:                     if ($item eq 'fields') {
 8082:                         my @changed;
 8083:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 8084:                         if (@{$confhash{'validation'}{$item}} > 0) {
 8085:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 8086:                         }
 8087:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8088:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8089:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 8090:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 8091:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
 8092:                                 } else {
 8093:                                     @changed = @{$confhash{'validation'}{$item}};
 8094:                                 }
 8095:                             } else {
 8096:                                 @changed = @{$confhash{'validation'}{$item}};
 8097:                             }
 8098:                         } else {
 8099:                             @changed = @{$confhash{'validation'}{$item}};
 8100:                         }
 8101:                         if (@changed) {
 8102:                             if ($confhash{'validation'}{$item}) {
 8103:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 8104:                             } else {
 8105:                                 $changes{'validation'}{$item} = &mt('None');
 8106:                             }
 8107:                         }
 8108:                     } else {
 8109:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 8110:                         if ($item eq 'markup') {
 8111:                             if ($env{'form.requestcourses_validation_'.$item}) {
 8112:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 8113:                             }
 8114:                         }
 8115:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8116:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8117:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 8118:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 8119:                                 }
 8120:                             } else {
 8121:                                 if ($confhash{'validation'}{$item} ne '') {
 8122:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 8123:                                 }
 8124:                             }
 8125:                         } else {
 8126:                             if ($confhash{'validation'}{$item} ne '') {
 8127:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 8128:                             }
 8129:                         }
 8130:                     }
 8131:                 }
 8132:             }
 8133:             if ($env{'form.validationdc'}) {
 8134:                 my $newval = $env{'form.validationdc'};
 8135:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
 8136:                 if (exists($domcoords{$newval})) {
 8137:                     $confhash{'validation'}{'dc'} = $newval;
 8138:                 }
 8139:             }
 8140:             if (ref($confhash{'validation'}) eq 'HASH') {
 8141:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8142:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8143:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 8144:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 8145:                                 if ($confhash{'validation'}{'dc'} eq '') {
 8146:                                     $changes{'validation'}{'dc'} = &mt('None');
 8147:                                 } else {
 8148:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8149:                                 }
 8150:                             }
 8151:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
 8152:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8153:                         }
 8154:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 8155:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8156:                     }
 8157:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 8158:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8159:                 }
 8160:             } else {
 8161:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8162:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8163:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 8164:                             $changes{'validation'}{'dc'} = &mt('None');
 8165:                         }
 8166:                     }
 8167:                 }
 8168:             }
 8169:         }
 8170:     } else {
 8171:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 8172:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 8173:     }
 8174:     foreach my $item (@usertools) {
 8175:         foreach my $type (@{$types},'default','_LC_adv') {
 8176:             my $unset; 
 8177:             if ($context eq 'requestcourses') {
 8178:                 $unset = '0';
 8179:                 if ($type eq '_LC_adv') {
 8180:                     $unset = '';
 8181:                 }
 8182:                 if ($confhash{$item}{$type} eq 'autolimit') {
 8183:                     $confhash{$item}{$type} .= '=';
 8184:                     unless ($limithash{$item}{$type} =~ /\D/) {
 8185:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 8186:                     }
 8187:                 }
 8188:             } elsif ($context eq 'requestauthor') {
 8189:                 $unset = '0';
 8190:                 if ($type eq '_LC_adv') {
 8191:                     $unset = '';
 8192:                 }
 8193:             } else {
 8194:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 8195:                     $confhash{$item}{$type} = 1;
 8196:                 } else {
 8197:                     $confhash{$item}{$type} = 0;
 8198:                 }
 8199:             }
 8200:             if (ref($domconfig{$action}) eq 'HASH') {
 8201:                 if ($action eq 'requestauthor') {
 8202:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 8203:                         $changes{$type} = 1;
 8204:                     }
 8205:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 8206:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 8207:                         $changes{$item}{$type} = 1;
 8208:                     }
 8209:                 } else {
 8210:                     if ($context eq 'requestcourses') {
 8211:                         if ($confhash{$item}{$type} ne $unset) {
 8212:                             $changes{$item}{$type} = 1;
 8213:                         }
 8214:                     } else {
 8215:                         if (!$confhash{$item}{$type}) {
 8216:                             $changes{$item}{$type} = 1;
 8217:                         }
 8218:                     }
 8219:                 }
 8220:             } else {
 8221:                 if ($context eq 'requestcourses') {
 8222:                     if ($confhash{$item}{$type} ne $unset) {
 8223:                         $changes{$item}{$type} = 1;
 8224:                     }
 8225:                 } elsif ($context eq 'requestauthor') {
 8226:                     if ($confhash{$type} ne $unset) {
 8227:                         $changes{$type} = 1;
 8228:                     }
 8229:                 } else {
 8230:                     if (!$confhash{$item}{$type}) {
 8231:                         $changes{$item}{$type} = 1;
 8232:                     }
 8233:                 }
 8234:             }
 8235:         }
 8236:     }
 8237:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 8238:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 8239:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 8240:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 8241:                     if (exists($confhash{'defaultquota'}{$key})) {
 8242:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 8243:                             $changes{'defaultquota'}{$key} = 1;
 8244:                         }
 8245:                     } else {
 8246:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 8247:                     }
 8248:                 }
 8249:             } else {
 8250:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 8251:                     if (exists($confhash{'defaultquota'}{$key})) {
 8252:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 8253:                             $changes{'defaultquota'}{$key} = 1;
 8254:                         }
 8255:                     } else {
 8256:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 8257:                     }
 8258:                 }
 8259:             }
 8260:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 8261:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 8262:                     if (exists($confhash{'authorquota'}{$key})) {
 8263:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 8264:                             $changes{'authorquota'}{$key} = 1;
 8265:                         }
 8266:                     } else {
 8267:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 8268:                     }
 8269:                 }
 8270:             }
 8271:         }
 8272:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 8273:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 8274:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 8275:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 8276:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 8277:                             $changes{'defaultquota'}{$key} = 1;
 8278:                         }
 8279:                     } else {
 8280:                         if (!exists($domconfig{'quotas'}{$key})) {
 8281:                             $changes{'defaultquota'}{$key} = 1;
 8282:                         }
 8283:                     }
 8284:                 } else {
 8285:                     $changes{'defaultquota'}{$key} = 1;
 8286:                 }
 8287:             }
 8288:         }
 8289:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 8290:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 8291:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 8292:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 8293:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 8294:                             $changes{'authorquota'}{$key} = 1;
 8295:                         }
 8296:                     } else {
 8297:                         $changes{'authorquota'}{$key} = 1;
 8298:                     }
 8299:                 } else {
 8300:                     $changes{'authorquota'}{$key} = 1;
 8301:                 }
 8302:             }
 8303:         }
 8304:     }
 8305: 
 8306:     if ($context eq 'requestauthor') {
 8307:         $domdefaults{'requestauthor'} = \%confhash;
 8308:     } else {
 8309:         foreach my $key (keys(%confhash)) {
 8310:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
 8311:                 $domdefaults{$key} = $confhash{$key};
 8312:             }
 8313:         }
 8314:     }
 8315: 
 8316:     my %quotahash = (
 8317:                       $action => { %confhash }
 8318:                     );
 8319:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 8320:                                              $dom);
 8321:     if ($putresult eq 'ok') {
 8322:         if (keys(%changes) > 0) {
 8323:             my $cachetime = 24*60*60;
 8324:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 8325:             if (ref($lastactref) eq 'HASH') {
 8326:                 $lastactref->{'domdefaults'} = 1;
 8327:             }
 8328:             $resulttext = &mt('Changes made:').'<ul>';
 8329:             unless (($context eq 'requestcourses') ||
 8330:                     ($context eq 'requestauthor')) {
 8331:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 8332:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 8333:                     foreach my $type (@{$types},'default') {
 8334:                         if (defined($changes{'defaultquota'}{$type})) {
 8335:                             my $typetitle = $usertypes->{$type};
 8336:                             if ($type eq 'default') {
 8337:                                 $typetitle = $othertitle;
 8338:                             }
 8339:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 8340:                         }
 8341:                     }
 8342:                     $resulttext .= '</ul></li>';
 8343:                 }
 8344:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 8345:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 8346:                     foreach my $type (@{$types},'default') {
 8347:                         if (defined($changes{'authorquota'}{$type})) {
 8348:                             my $typetitle = $usertypes->{$type};
 8349:                             if ($type eq 'default') {
 8350:                                 $typetitle = $othertitle;
 8351:                             }
 8352:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 8353:                         }
 8354:                     }
 8355:                     $resulttext .= '</ul></li>';
 8356:                 }
 8357:             }
 8358:             my %newenv;
 8359:             foreach my $item (@usertools) {
 8360:                 my (%haschgs,%inconf);
 8361:                 if ($context eq 'requestauthor') {
 8362:                     %haschgs = %changes;
 8363:                     %inconf = %confhash;
 8364:                 } else {
 8365:                     if (ref($changes{$item}) eq 'HASH') {
 8366:                         %haschgs = %{$changes{$item}};
 8367:                     }
 8368:                     if (ref($confhash{$item}) eq 'HASH') {
 8369:                         %inconf = %{$confhash{$item}};
 8370:                     }
 8371:                 }
 8372:                 if (keys(%haschgs) > 0) {
 8373:                     my $newacc = 
 8374:                         &Apache::lonnet::usertools_access($env{'user.name'},
 8375:                                                           $env{'user.domain'},
 8376:                                                           $item,'reload',$context);
 8377:                     if (($context eq 'requestcourses') ||
 8378:                         ($context eq 'requestauthor')) {
 8379:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 8380:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 8381:                         }
 8382:                     } else {
 8383:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 8384:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 8385:                         }
 8386:                     }
 8387:                     unless ($context eq 'requestauthor') {
 8388:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 8389:                     }
 8390:                     foreach my $type (@{$types},'default','_LC_adv') {
 8391:                         if ($haschgs{$type}) {
 8392:                             my $typetitle = $usertypes->{$type};
 8393:                             if ($type eq 'default') {
 8394:                                 $typetitle = $othertitle;
 8395:                             } elsif ($type eq '_LC_adv') {
 8396:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 8397:                             }
 8398:                             if ($inconf{$type}) {
 8399:                                 if ($context eq 'requestcourses') {
 8400:                                     my $cond;
 8401:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 8402:                                         if ($1 eq '') {
 8403:                                             $cond = &mt('(Automatic processing of any request).');
 8404:                                         } else {
 8405:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 8406:                                         }
 8407:                                     } else { 
 8408:                                         $cond = $conditions{$inconf{$type}};
 8409:                                     }
 8410:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 8411:                                 } elsif ($context eq 'requestauthor') {
 8412:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 8413:                                                              $titles{$inconf{$type}},$typetitle);
 8414: 
 8415:                                 } else {
 8416:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 8417:                                 }
 8418:                             } else {
 8419:                                 if ($type eq '_LC_adv') {
 8420:                                     if ($inconf{$type} eq '0') {
 8421:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 8422:                                     } else { 
 8423:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 8424:                                     }
 8425:                                 } else {
 8426:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 8427:                                 }
 8428:                             }
 8429:                         }
 8430:                     }
 8431:                     unless ($context eq 'requestauthor') {
 8432:                         $resulttext .= '</ul></li>';
 8433:                     }
 8434:                 }
 8435:             }
 8436:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 8437:                 if (ref($changes{'notify'}) eq 'HASH') {
 8438:                     if ($changes{'notify'}{'approval'}) {
 8439:                         if (ref($confhash{'notify'}) eq 'HASH') {
 8440:                             if ($confhash{'notify'}{'approval'}) {
 8441:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 8442:                             } else {
 8443:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 8444:                             }
 8445:                         }
 8446:                     }
 8447:                 }
 8448:             }
 8449:             if ($action eq 'requestcourses') {
 8450:                 my @offon = ('off','on');
 8451:                 if ($changes{'uniquecode'}) {
 8452:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 8453:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 8454:                         $resulttext .= '<li>'.
 8455:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 8456:                                        '</li>';
 8457:                     } else {
 8458:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 8459:                                        '</li>';
 8460:                     }
 8461:                 }
 8462:                 foreach my $type ('textbooks','templates') {
 8463:                     if (ref($changes{$type}) eq 'HASH') {
 8464:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
 8465:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
 8466:                             my %coursehash = &Apache::lonnet::coursedescription($key);
 8467:                             my $coursetitle = $coursehash{'description'};
 8468:                             my $position = $confhash{$type}{$key}{'order'} + 1;
 8469:                             $resulttext .= '<li>';
 8470:                             foreach my $item ('subject','title','publisher','author') {
 8471:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
 8472:                                          ($type eq 'templates'));
 8473:                                 my $name = $item.':';
 8474:                                 $name =~ s/^(\w)/\U$1/;
 8475:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
 8476:                             }
 8477:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 8478:                             if ($type eq 'textbooks') {
 8479:                                 if ($confhash{$type}{$key}{'image'}) {
 8480:                                     $resulttext .= ' '.&mt('Image: [_1]',
 8481:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
 8482:                                                    ' alt="Textbook cover" />').'<br />';
 8483:                                 }
 8484:                             }
 8485:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 8486:                         }
 8487:                         $resulttext .= '</ul></li>';
 8488:                     }
 8489:                 }
 8490:                 if (ref($changes{'validation'}) eq 'HASH') {
 8491:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 8492:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 8493:                         foreach my $item (@{$validationitemsref}) {
 8494:                             if (exists($changes{'validation'}{$item})) {
 8495:                                 if ($item eq 'markup') {
 8496:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 8497:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 8498:                                 } else {
 8499:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 8500:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 8501:                                 }
 8502:                             }
 8503:                         }
 8504:                         if (exists($changes{'validation'}{'dc'})) {
 8505:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 8506:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 8507:                         }
 8508:                     }
 8509:                 }
 8510:             }
 8511:             $resulttext .= '</ul>';
 8512:             if (keys(%newenv)) {
 8513:                 &Apache::lonnet::appenv(\%newenv);
 8514:             }
 8515:         } else {
 8516:             if ($context eq 'requestcourses') {
 8517:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 8518:             } elsif ($context eq 'requestauthor') {
 8519:                 $resulttext = &mt('No changes made to rights to request author space.');
 8520:             } else {
 8521:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 8522:             }
 8523:         }
 8524:     } else {
 8525:         $resulttext = '<span class="LC_error">'.
 8526: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 8527:     }
 8528:     if ($errors) {
 8529:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 8530:                        '<ul>'.$errors.'</ul></p>';
 8531:     }
 8532:     return $resulttext;
 8533: }
 8534: 
 8535: sub process_textbook_image {
 8536:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
 8537:     my $filename = $env{'form.'.$caller.'.filename'};
 8538:     my ($error,$url);
 8539:     my ($width,$height) = (50,50);
 8540:     if ($configuserok eq 'ok') {
 8541:         if ($switchserver) {
 8542:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 8543:                          $switchserver);
 8544:         } elsif ($author_ok eq 'ok') {
 8545:             my ($result,$imageurl) =
 8546:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 8547:                              "$type/$dom/$cnum/cover",$width,$height);
 8548:             if ($result eq 'ok') {
 8549:                 $url = $imageurl;
 8550:             } else {
 8551:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 8552:             }
 8553:         } else {
 8554:             $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);
 8555:         }
 8556:     } else {
 8557:         $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);
 8558:     }
 8559:     return ($url,$error);
 8560: }
 8561: 
 8562: sub modify_autoenroll {
 8563:     my ($dom,$lastactref,%domconfig) = @_;
 8564:     my ($resulttext,%changes);
 8565:     my %currautoenroll;
 8566:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 8567:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 8568:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 8569:         }
 8570:     }
 8571:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 8572:     my %title = ( run => 'Auto-enrollment active',
 8573:                   sender => 'Sender for notification messages',
 8574:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
 8575:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
 8576:     my @offon = ('off','on');
 8577:     my $sender_uname = $env{'form.sender_uname'};
 8578:     my $sender_domain = $env{'form.sender_domain'};
 8579:     if ($sender_domain eq '') {
 8580:         $sender_uname = '';
 8581:     } elsif ($sender_uname eq '') {
 8582:         $sender_domain = '';
 8583:     }
 8584:     my $coowners = $env{'form.autoassign_coowners'};
 8585:     my $failsafe = $env{'form.autoenroll_failsafe'};
 8586:     $failsafe =~ s{^\s+|\s+$}{}g;
 8587:     if ($failsafe =~ /\D/) {
 8588:         undef($failsafe);
 8589:     }
 8590:     my %autoenrollhash =  (
 8591:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 8592:                                        'sender_uname' => $sender_uname,
 8593:                                        'sender_domain' => $sender_domain,
 8594:                                        'co-owners' => $coowners,
 8595:                                        'autofailsafe' => $failsafe,
 8596:                                 }
 8597:                      );
 8598:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 8599:                                              $dom);
 8600:     if ($putresult eq 'ok') {
 8601:         if (exists($currautoenroll{'run'})) {
 8602:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 8603:                  $changes{'run'} = 1;
 8604:              }
 8605:         } elsif ($autorun) {
 8606:             if ($env{'form.autoenroll_run'} ne '1') {
 8607:                  $changes{'run'} = 1;
 8608:             }
 8609:         }
 8610:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 8611:             $changes{'sender'} = 1;
 8612:         }
 8613:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 8614:             $changes{'sender'} = 1;
 8615:         }
 8616:         if ($currautoenroll{'co-owners'} ne '') {
 8617:             if ($currautoenroll{'co-owners'} ne $coowners) {
 8618:                 $changes{'coowners'} = 1;
 8619:             }
 8620:         } elsif ($coowners) {
 8621:             $changes{'coowners'} = 1;
 8622:         }
 8623:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
 8624:             $changes{'autofailsafe'} = 1;
 8625:         }
 8626:         if (keys(%changes) > 0) {
 8627:             $resulttext = &mt('Changes made:').'<ul>';
 8628:             if ($changes{'run'}) {
 8629:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 8630:             }
 8631:             if ($changes{'sender'}) {
 8632:                 if ($sender_uname eq '' || $sender_domain eq '') {
 8633:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 8634:                 } else {
 8635:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 8636:                 }
 8637:             }
 8638:             if ($changes{'coowners'}) {
 8639:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 8640:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 8641:                 if (ref($lastactref) eq 'HASH') {
 8642:                     $lastactref->{'domainconfig'} = 1;
 8643:                 }
 8644:             }
 8645:             if ($changes{'autofailsafe'}) {
 8646:                 if ($failsafe ne '') {
 8647:                     $resulttext .= '<li>'.&mt("$title{'failsafe'} set to [_1]",$failsafe).'</li>';
 8648:                 } else {
 8649:                     $resulttext .= '<li>'.&mt("$title{'failsafe'} deleted");
 8650:                 }
 8651:                 &Apache::lonnet::get_domain_defaults($dom,1);
 8652:                 if (ref($lastactref) eq 'HASH') {
 8653:                     $lastactref->{'domdefaults'} = 1;
 8654:                 }
 8655:             }
 8656:             $resulttext .= '</ul>';
 8657:         } else {
 8658:             $resulttext = &mt('No changes made to auto-enrollment settings');
 8659:         }
 8660:     } else {
 8661:         $resulttext = '<span class="LC_error">'.
 8662: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 8663:     }
 8664:     return $resulttext;
 8665: }
 8666: 
 8667: sub modify_autoupdate {
 8668:     my ($dom,%domconfig) = @_;
 8669:     my ($resulttext,%currautoupdate,%fields,%changes);
 8670:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 8671:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 8672:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 8673:         }
 8674:     }
 8675:     my @offon = ('off','on');
 8676:     my %title = &Apache::lonlocal::texthash (
 8677:                    run => 'Auto-update:',
 8678:                    classlists => 'Updates to user information in classlists?'
 8679:                 );
 8680:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8681:     my %fieldtitles = &Apache::lonlocal::texthash (
 8682:                         id => 'Student/Employee ID',
 8683:                         permanentemail => 'E-mail address',
 8684:                         lastname => 'Last Name',
 8685:                         firstname => 'First Name',
 8686:                         middlename => 'Middle Name',
 8687:                         generation => 'Generation',
 8688:                       );
 8689:     $othertitle = &mt('All users');
 8690:     if (keys(%{$usertypes}) >  0) {
 8691:         $othertitle = &mt('Other users');
 8692:     }
 8693:     foreach my $key (keys(%env)) {
 8694:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 8695:             my ($usertype,$item) = ($1,$2);
 8696:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 8697:                 if ($usertype eq 'default') {   
 8698:                     push(@{$fields{$1}},$2);
 8699:                 } elsif (ref($types) eq 'ARRAY') {
 8700:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 8701:                         push(@{$fields{$1}},$2);
 8702:                     }
 8703:                 }
 8704:             }
 8705:         }
 8706:     }
 8707:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 8708:     @lockablenames = sort(@lockablenames);
 8709:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 8710:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 8711:         if (@changed) {
 8712:             $changes{'lockablenames'} = 1;
 8713:         }
 8714:     } else {
 8715:         if (@lockablenames) {
 8716:             $changes{'lockablenames'} = 1;
 8717:         }
 8718:     }
 8719:     my %updatehash = (
 8720:                       autoupdate => { run => $env{'form.autoupdate_run'},
 8721:                                       classlists => $env{'form.classlists'},
 8722:                                       fields => {%fields},
 8723:                                       lockablenames => \@lockablenames,
 8724:                                     }
 8725:                      );
 8726:     foreach my $key (keys(%currautoupdate)) {
 8727:         if (($key eq 'run') || ($key eq 'classlists')) {
 8728:             if (exists($updatehash{autoupdate}{$key})) {
 8729:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 8730:                     $changes{$key} = 1;
 8731:                 }
 8732:             }
 8733:         } elsif ($key eq 'fields') {
 8734:             if (ref($currautoupdate{$key}) eq 'HASH') {
 8735:                 foreach my $item (@{$types},'default') {
 8736:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 8737:                         my $change = 0;
 8738:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 8739:                             if (!exists($fields{$item})) {
 8740:                                 $change = 1;
 8741:                                 last;
 8742:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 8743:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 8744:                                     $change = 1;
 8745:                                     last;
 8746:                                 }
 8747:                             }
 8748:                         }
 8749:                         if ($change) {
 8750:                             push(@{$changes{$key}},$item);
 8751:                         }
 8752:                     } 
 8753:                 }
 8754:             }
 8755:         } elsif ($key eq 'lockablenames') {
 8756:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 8757:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 8758:                 if (@changed) {
 8759:                     $changes{'lockablenames'} = 1;
 8760:                 }
 8761:             } else {
 8762:                 if (@lockablenames) {
 8763:                     $changes{'lockablenames'} = 1;
 8764:                 }
 8765:             }
 8766:         }
 8767:     }
 8768:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 8769:         if (@lockablenames) {
 8770:             $changes{'lockablenames'} = 1;
 8771:         }
 8772:     }
 8773:     foreach my $item (@{$types},'default') {
 8774:         if (defined($fields{$item})) {
 8775:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 8776:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 8777:                     my $change = 0;
 8778:                     if (ref($fields{$item}) eq 'ARRAY') {
 8779:                         foreach my $type (@{$fields{$item}}) {
 8780:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 8781:                                 $change = 1;
 8782:                                 last;
 8783:                             }
 8784:                         }
 8785:                     }
 8786:                     if ($change) {
 8787:                         push(@{$changes{'fields'}},$item);
 8788:                     }
 8789:                 } else {
 8790:                     push(@{$changes{'fields'}},$item);
 8791:                 }
 8792:             } else {
 8793:                 push(@{$changes{'fields'}},$item);
 8794:             }
 8795:         }
 8796:     }
 8797:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 8798:                                              $dom);
 8799:     if ($putresult eq 'ok') {
 8800:         if (keys(%changes) > 0) {
 8801:             $resulttext = &mt('Changes made:').'<ul>';
 8802:             foreach my $key (sort(keys(%changes))) {
 8803:                 if ($key eq 'lockablenames') {
 8804:                     $resulttext .= '<li>';
 8805:                     if (@lockablenames) {
 8806:                         $usertypes->{'default'} = $othertitle;
 8807:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 8808:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 8809:                     } else {
 8810:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 8811:                     }
 8812:                     $resulttext .= '</li>';
 8813:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 8814:                     foreach my $item (@{$changes{$key}}) {
 8815:                         my @newvalues;
 8816:                         foreach my $type (@{$fields{$item}}) {
 8817:                             push(@newvalues,$fieldtitles{$type});
 8818:                         }
 8819:                         my $newvaluestr;
 8820:                         if (@newvalues > 0) {
 8821:                             $newvaluestr = join(', ',@newvalues);
 8822:                         } else {
 8823:                             $newvaluestr = &mt('none');
 8824:                         }
 8825:                         if ($item eq 'default') {
 8826:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 8827:                         } else {
 8828:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 8829:                         }
 8830:                     }
 8831:                 } else {
 8832:                     my $newvalue;
 8833:                     if ($key eq 'run') {
 8834:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 8835:                     } else {
 8836:                         $newvalue = $offon[$env{'form.'.$key}];
 8837:                     }
 8838:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 8839:                 }
 8840:             }
 8841:             $resulttext .= '</ul>';
 8842:         } else {
 8843:             $resulttext = &mt('No changes made to autoupdates');
 8844:         }
 8845:     } else {
 8846:         $resulttext = '<span class="LC_error">'.
 8847: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 8848:     }
 8849:     return $resulttext;
 8850: }
 8851: 
 8852: sub modify_autocreate {
 8853:     my ($dom,%domconfig) = @_;
 8854:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 8855:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 8856:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 8857:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 8858:         }
 8859:     }
 8860:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 8861:                  req => 'Auto-creation of validated requests for official courses',
 8862:                  xmldc => 'Identity of course creator of courses from XML files',
 8863:                );
 8864:     my @types = ('xml','req');
 8865:     foreach my $item (@types) {
 8866:         $newvals{$item} = $env{'form.autocreate_'.$item};
 8867:         $newvals{$item} =~ s/\D//g;
 8868:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 8869:     }
 8870:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 8871:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
 8872:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 8873:         $newvals{'xmldc'} = '';
 8874:     } 
 8875:     %autocreatehash =  (
 8876:                         autocreate => { xml => $newvals{'xml'},
 8877:                                         req => $newvals{'req'},
 8878:                                       }
 8879:                        );
 8880:     if ($newvals{'xmldc'} ne '') {
 8881:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 8882:     }
 8883:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 8884:                                              $dom);
 8885:     if ($putresult eq 'ok') {
 8886:         my @items = @types;
 8887:         if ($newvals{'xml'}) {
 8888:             push(@items,'xmldc');
 8889:         }
 8890:         foreach my $item (@items) {
 8891:             if (exists($currautocreate{$item})) {
 8892:                 if ($currautocreate{$item} ne $newvals{$item}) {
 8893:                     $changes{$item} = 1;
 8894:                 }
 8895:             } elsif ($newvals{$item}) {
 8896:                 $changes{$item} = 1;
 8897:             }
 8898:         }
 8899:         if (keys(%changes) > 0) {
 8900:             my @offon = ('off','on'); 
 8901:             $resulttext = &mt('Changes made:').'<ul>';
 8902:             foreach my $item (@types) {
 8903:                 if ($changes{$item}) {
 8904:                     my $newtxt = $offon[$newvals{$item}];
 8905:                     $resulttext .= '<li>'.
 8906:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
 8907:                                        '<b>','</b>').
 8908:                                    '</li>';
 8909:                 }
 8910:             }
 8911:             if ($changes{'xmldc'}) {
 8912:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 8913:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 8914:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
 8915:             }
 8916:             $resulttext .= '</ul>';
 8917:         } else {
 8918:             $resulttext = &mt('No changes made to auto-creation settings');
 8919:         }
 8920:     } else {
 8921:         $resulttext = '<span class="LC_error">'.
 8922:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8923:     }
 8924:     return $resulttext;
 8925: }
 8926: 
 8927: sub modify_directorysrch {
 8928:     my ($dom,%domconfig) = @_;
 8929:     my ($resulttext,%changes);
 8930:     my %currdirsrch;
 8931:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 8932:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 8933:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 8934:         }
 8935:     }
 8936:     my %title = ( available => 'Institutional directory search available',
 8937:                   localonly => 'Other domains can search institution',
 8938:                   lcavailable => 'LON-CAPA directory search available',
 8939:                   lclocalonly => 'Other domains can search LON-CAPA domain',
 8940:                   searchby => 'Search types',
 8941:                   searchtypes => 'Search latitude');
 8942:     my @offon = ('off','on');
 8943:     my @otherdoms = ('Yes','No');
 8944: 
 8945:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 8946:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 8947:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 8948: 
 8949:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8950:     if (keys(%{$usertypes}) == 0) {
 8951:         @cansearch = ('default');
 8952:     } else {
 8953:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 8954:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 8955:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 8956:                     push(@{$changes{'cansearch'}},$type);
 8957:                 }
 8958:             }
 8959:             foreach my $type (@cansearch) {
 8960:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 8961:                     push(@{$changes{'cansearch'}},$type);
 8962:                 }
 8963:             }
 8964:         } else {
 8965:             push(@{$changes{'cansearch'}},@cansearch);
 8966:         }
 8967:     }
 8968: 
 8969:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 8970:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 8971:             if (!grep(/^\Q$by\E$/,@searchby)) {
 8972:                 push(@{$changes{'searchby'}},$by);
 8973:             }
 8974:         }
 8975:         foreach my $by (@searchby) {
 8976:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 8977:                 push(@{$changes{'searchby'}},$by);
 8978:             }
 8979:         }
 8980:     } else {
 8981:         push(@{$changes{'searchby'}},@searchby);
 8982:     }
 8983: 
 8984:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 8985:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 8986:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 8987:                 push(@{$changes{'searchtypes'}},$type);
 8988:             }
 8989:         }
 8990:         foreach my $type (@searchtypes) {
 8991:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 8992:                 push(@{$changes{'searchtypes'}},$type);
 8993:             }
 8994:         }
 8995:     } else {
 8996:         if (exists($currdirsrch{'searchtypes'})) {
 8997:             foreach my $type (@searchtypes) {  
 8998:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 8999:                     push(@{$changes{'searchtypes'}},$type);
 9000:                 }
 9001:             }
 9002:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 9003:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 9004:             }   
 9005:         } else {
 9006:             push(@{$changes{'searchtypes'}},@searchtypes); 
 9007:         }
 9008:     }
 9009: 
 9010:     my %dirsrch_hash =  (
 9011:             directorysrch => { available => $env{'form.dirsrch_available'},
 9012:                                cansearch => \@cansearch,
 9013:                                localonly => $env{'form.dirsrch_instlocalonly'},
 9014:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
 9015:                                lcavailable => $env{'form.dirsrch_domavailable'},
 9016:                                searchby => \@searchby,
 9017:                                searchtypes => \@searchtypes,
 9018:                              }
 9019:             );
 9020:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 9021:                                              $dom);
 9022:     if ($putresult eq 'ok') {
 9023:         if (exists($currdirsrch{'available'})) {
 9024:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 9025:                  $changes{'available'} = 1;
 9026:              }
 9027:         } else {
 9028:             if ($env{'form.dirsrch_available'} eq '1') {
 9029:                 $changes{'available'} = 1;
 9030:             }
 9031:         }
 9032:         if (exists($currdirsrch{'lcavailable'})) {
 9033:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
 9034:                 $changes{'lcavailable'} = 1;
 9035:             }
 9036:         } else {
 9037:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
 9038:                 $changes{'lcavailable'} = 1;
 9039:             }
 9040:         }
 9041:         if (exists($currdirsrch{'localonly'})) {
 9042:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
 9043:                 $changes{'localonly'} = 1;
 9044:             }
 9045:         } else {
 9046:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
 9047:                 $changes{'localonly'} = 1;
 9048:             }
 9049:         }
 9050:         if (exists($currdirsrch{'lclocalonly'})) {
 9051:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
 9052:                 $changes{'lclocalonly'} = 1;
 9053:             }
 9054:         } else {
 9055:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
 9056:                 $changes{'lclocalonly'} = 1;
 9057:             }
 9058:         }
 9059:         if (keys(%changes) > 0) {
 9060:             $resulttext = &mt('Changes made:').'<ul>';
 9061:             if ($changes{'available'}) {
 9062:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 9063:             }
 9064:             if ($changes{'lcavailable'}) {
 9065:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
 9066:             }
 9067:             if ($changes{'localonly'}) {
 9068:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
 9069:             }
 9070:             if ($changes{'lclocalonly'}) {
 9071:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
 9072:             }
 9073:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 9074:                 my $chgtext;
 9075:                 if (ref($usertypes) eq 'HASH') {
 9076:                     if (keys(%{$usertypes}) > 0) {
 9077:                         foreach my $type (@{$types}) {
 9078:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 9079:                                 $chgtext .= $usertypes->{$type}.'; ';
 9080:                             }
 9081:                         }
 9082:                         if (grep(/^default$/,@cansearch)) {
 9083:                             $chgtext .= $othertitle;
 9084:                         } else {
 9085:                             $chgtext =~ s/\; $//;
 9086:                         }
 9087:                         $resulttext .=
 9088:                             '<li>'.
 9089:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
 9090:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
 9091:                             '</li>';
 9092:                     }
 9093:                 }
 9094:             }
 9095:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 9096:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 9097:                 my $chgtext;
 9098:                 foreach my $type (@{$titleorder}) {
 9099:                     if (grep(/^\Q$type\E$/,@searchby)) {
 9100:                         if (defined($searchtitles->{$type})) {
 9101:                             $chgtext .= $searchtitles->{$type}.'; ';
 9102:                         }
 9103:                     }
 9104:                 }
 9105:                 $chgtext =~ s/\; $//;
 9106:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 9107:             }
 9108:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 9109:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 9110:                 my $chgtext;
 9111:                 foreach my $type (@{$srchtypeorder}) {
 9112:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 9113:                         if (defined($srchtypes_desc->{$type})) {
 9114:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 9115:                         }
 9116:                     }
 9117:                 }
 9118:                 $chgtext =~ s/\; $//;
 9119:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
 9120:             }
 9121:             $resulttext .= '</ul>';
 9122:         } else {
 9123:             $resulttext = &mt('No changes made to directory search settings');
 9124:         }
 9125:     } else {
 9126:         $resulttext = '<span class="LC_error">'.
 9127:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 9128:     }
 9129:     return $resulttext;
 9130: }
 9131: 
 9132: sub modify_contacts {
 9133:     my ($dom,$lastactref,%domconfig) = @_;
 9134:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 9135:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 9136:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 9137:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 9138:         }
 9139:     }
 9140:     my (%others,%to,%bcc,%includestr,%includeloc);
 9141:     my @contacts = ('supportemail','adminemail');
 9142:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
 9143:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail');
 9144:     my @toggles = ('reporterrors','reportupdates');
 9145:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 9146:     foreach my $type (@mailings) {
 9147:         @{$newsetting{$type}} = 
 9148:             &Apache::loncommon::get_env_multiple('form.'.$type);
 9149:         foreach my $item (@contacts) {
 9150:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 9151:                 $contacts_hash{contacts}{$type}{$item} = 1;
 9152:             } else {
 9153:                 $contacts_hash{contacts}{$type}{$item} = 0;
 9154:             }
 9155:         }
 9156:         $others{$type} = $env{'form.'.$type.'_others'};
 9157:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 9158:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 9159:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 9160:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 9161:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
 9162:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
 9163:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
 9164:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
 9165:             }
 9166:         }
 9167:     }
 9168:     foreach my $item (@contacts) {
 9169:         $to{$item} = $env{'form.'.$item};
 9170:         $contacts_hash{'contacts'}{$item} = $to{$item};
 9171:     }
 9172:     foreach my $item (@toggles) {
 9173:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
 9174:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
 9175:         }
 9176:     }
 9177:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
 9178:         foreach my $field (@{$fields}) {
 9179:             if (ref($possoptions->{$field}) eq 'ARRAY') {
 9180:                 my $value = $env{'form.helpform_'.$field};
 9181:                 $value =~ s/^\s+|\s+$//g;
 9182:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
 9183:                     $contacts_hash{contacts}{'helpform'}{$field} = $value;
 9184:                     if ($field eq 'screenshot') {
 9185:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
 9186:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
 9187:                             $contacts_hash{contacts}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
 9188:                         }
 9189:                     }
 9190:                 }
 9191:             }
 9192:         }
 9193:     }
 9194:     if (keys(%currsetting) > 0) {
 9195:         foreach my $item (@contacts) {
 9196:             if ($to{$item} ne $currsetting{$item}) {
 9197:                 $changes{$item} = 1;
 9198:             }
 9199:         }
 9200:         foreach my $type (@mailings) {
 9201:             foreach my $item (@contacts) {
 9202:                 if (ref($currsetting{$type}) eq 'HASH') {
 9203:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 9204:                         push(@{$changes{$type}},$item);
 9205:                     }
 9206:                 } else {
 9207:                     push(@{$changes{$type}},@{$newsetting{$type}});
 9208:                 }
 9209:             }
 9210:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 9211:                 push(@{$changes{$type}},'others');
 9212:             }
 9213:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 9214:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 9215:                     push(@{$changes{$type}},'bcc'); 
 9216:                 }
 9217:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
 9218:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
 9219:                     push(@{$changes{$type}},'include');
 9220:                 }
 9221:             }
 9222:         }
 9223:         if (ref($fields) eq 'ARRAY') {
 9224:             if (ref($currsetting{'helpform'}) eq 'HASH') {
 9225:                 foreach my $field (@{$fields}) {
 9226:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
 9227:                         push(@{$changes{'helpform'}},$field);
 9228:                     }
 9229:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
 9230:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
 9231:                             push(@{$changes{'helpform'}},'maxsize');
 9232:                         }
 9233:                     }
 9234:                 }
 9235:             } else {
 9236:                 foreach my $field (@{$fields}) {
 9237:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
 9238:                         push(@{$changes{'helpform'}},$field);
 9239:                     }
 9240:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
 9241:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
 9242:                             push(@{$changes{'helpform'}},'maxsize');
 9243:                         }
 9244:                     }
 9245:                 }
 9246:             }
 9247:         }
 9248:     } else {
 9249:         my %default;
 9250:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 9251:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 9252:         $default{'errormail'} = 'adminemail';
 9253:         $default{'packagesmail'} = 'adminemail';
 9254:         $default{'helpdeskmail'} = 'supportemail';
 9255:         $default{'otherdomsmail'} = 'supportemail';
 9256:         $default{'lonstatusmail'} = 'adminemail';
 9257:         $default{'requestsmail'} = 'adminemail';
 9258:         $default{'updatesmail'} = 'adminemail';
 9259:         foreach my $item (@contacts) {
 9260:            if ($to{$item} ne $default{$item}) {
 9261:                $changes{$item} = 1;
 9262:            }
 9263:         }
 9264:         foreach my $type (@mailings) {
 9265:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 9266:                 push(@{$changes{$type}},@{$newsetting{$type}});
 9267:             }
 9268:             if ($others{$type} ne '') {
 9269:                 push(@{$changes{$type}},'others');
 9270:             }
 9271:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 9272:                 if ($bcc{$type} ne '') {
 9273:                     push(@{$changes{$type}},'bcc');
 9274:                 }
 9275:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
 9276:                     push(@{$changes{$type}},'include');
 9277:                 }
 9278:             }
 9279:         }
 9280:         if (ref($fields) eq 'ARRAY') {
 9281:             foreach my $field (@{$fields}) {
 9282:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
 9283:                     push(@{$changes{'helpform'}},$field);
 9284:                 }
 9285:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
 9286:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
 9287:                         push(@{$changes{'helpform'}},'maxsize');
 9288:                     }
 9289:                 }
 9290:             }
 9291:         }
 9292:     }
 9293:     foreach my $item (@toggles) {
 9294:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
 9295:             $changes{$item} = 1;
 9296:         } elsif ((!$env{'form.'.$item}) &&
 9297:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
 9298:             $changes{$item} = 1;
 9299:         }
 9300:     }
 9301:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 9302:                                              $dom);
 9303:     if ($putresult eq 'ok') {
 9304:         if (keys(%changes) > 0) {
 9305:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 9306:             if (ref($lastactref) eq 'HASH') {
 9307:                 $lastactref->{'domainconfig'} = 1;
 9308:             }
 9309:             my ($titles,$short_titles)  = &contact_titles();
 9310:             $resulttext = &mt('Changes made:').'<ul>';
 9311:             foreach my $item (@contacts) {
 9312:                 if ($changes{$item}) {
 9313:                     $resulttext .= '<li>'.$titles->{$item}.
 9314:                                     &mt(' set to: ').
 9315:                                     '<span class="LC_cusr_emph">'.
 9316:                                     $to{$item}.'</span></li>';
 9317:                 }
 9318:             }
 9319:             foreach my $type (@mailings) {
 9320:                 if (ref($changes{$type}) eq 'ARRAY') {
 9321:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 9322:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
 9323:                     } else {
 9324:                         $resulttext .= '<li>'.$titles->{$type}.': ';
 9325:                     }
 9326:                     my @text;
 9327:                     foreach my $item (@{$newsetting{$type}}) {
 9328:                         push(@text,$short_titles->{$item});
 9329:                     }
 9330:                     if ($others{$type} ne '') {
 9331:                         push(@text,$others{$type});
 9332:                     }
 9333:                     if (@text) {
 9334:                         $resulttext .= '<span class="LC_cusr_emph">'.
 9335:                                        join(', ',@text).'</span>';
 9336:                     }
 9337:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 9338:                         if ($bcc{$type} ne '') {
 9339:                             my $bcctext;
 9340:                             if (@text) {
 9341:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
 9342:                             } else {
 9343:                                 $bcctext = '(Bcc)';
 9344:                             }
 9345:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 9346:                         } elsif (!@text) {
 9347:                             $resulttext .= &mt('No one');
 9348:                         }
 9349:                         if ($includestr{$type} ne '') {
 9350:                             if ($includeloc{$type} eq 'b') {
 9351:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
 9352:                             } elsif ($includeloc{$type} eq 's') {
 9353:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
 9354:                             }
 9355:                         }
 9356:                     } elsif (!@text) {
 9357:                         $resulttext .= &mt('No recipients');
 9358:                     }
 9359:                     $resulttext .= '</li>';
 9360:                 }
 9361:             }
 9362:             my @offon = ('off','on');
 9363:             if ($changes{'reporterrors'}) {
 9364:                 $resulttext .= '<li>'.
 9365:                                &mt('E-mail error reports to [_1] set to "'.
 9366:                                    $offon[$env{'form.reporterrors'}].'".',
 9367:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 9368:                                        &mt('LON-CAPA core group - MSU'),600,500)).
 9369:                                '</li>';
 9370:             }
 9371:             if ($changes{'reportupdates'}) {
 9372:                 $resulttext .= '<li>'.
 9373:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
 9374:                                     $offon[$env{'form.reportupdates'}].'".',
 9375:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 9376:                                         &mt('LON-CAPA core group - MSU'),600,500)).
 9377:                                 '</li>';
 9378:             }
 9379:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
 9380:                 my (@optional,@required,@unused,$maxsizechg);
 9381:                 foreach my $field (@{$changes{'helpform'}}) {
 9382:                     if ($field eq 'maxsize') {
 9383:                         $maxsizechg = 1;
 9384:                         next;
 9385:                     }
 9386:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
 9387:                         push(@optional,$field);
 9388:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
 9389:                         push(@unused,$field);
 9390:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
 9391:                         push(@required,$field);
 9392:                     }
 9393:                 }
 9394:                 if (@optional) {
 9395:                     $resulttext .= '<li>'.
 9396:                                    &mt('Help form fields changed to "Optional": [_1].',
 9397:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
 9398:                                    '</li>';
 9399:                 }
 9400:                 if (@required) {
 9401:                     $resulttext .= '<li>'.
 9402:                                    &mt('Help form fields changed to "Required": [_1].',
 9403:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
 9404:                                    '</li>';
 9405:                 }
 9406:                 if (@unused) {
 9407:                     $resulttext .= '<li>'.
 9408:                                    &mt('Help form fields changed to "Not shown": [_1].',
 9409:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
 9410:                                    '</li>';
 9411:                 }
 9412:                 if ($maxsizechg) {
 9413:                     $resulttext .= '<li>'.
 9414:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
 9415:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
 9416:                                    '</li>';
 9417: 
 9418:                 }
 9419:             }
 9420:             $resulttext .= '</ul>';
 9421:         } else {
 9422:             $resulttext = &mt('No changes made to contacts and form settings');
 9423:         }
 9424:     } else {
 9425:         $resulttext = '<span class="LC_error">'.
 9426:             &mt('An error occurred: [_1].',$putresult).'</span>';
 9427:     }
 9428:     return $resulttext;
 9429: }
 9430: 
 9431: sub modify_usercreation {
 9432:     my ($dom,%domconfig) = @_;
 9433:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
 9434:     my $warningmsg;
 9435:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 9436:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 9437:             if ($key eq 'cancreate') {
 9438:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 9439:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 9440:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 9441:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
 9442:                             ($item eq 'recaptchaversion')) {
 9443:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 9444:                         } else {
 9445:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 9446:                         }
 9447:                     }
 9448:                 }
 9449:             } elsif ($key eq 'email_rule') {
 9450:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 9451:             } else {
 9452:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 9453:             }
 9454:         }
 9455:     }
 9456:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 9457:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 9458:     my @contexts = ('author','course','requestcrs');
 9459:     foreach my $item(@contexts) {
 9460:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
 9461:     }
 9462:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 9463:         foreach my $item (@contexts) {
 9464:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 9465:                 push(@{$changes{'cancreate'}},$item);
 9466:             }
 9467:         }
 9468:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 9469:         foreach my $item (@contexts) {
 9470:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 9471:                 if ($cancreate{$item} ne 'any') {
 9472:                     push(@{$changes{'cancreate'}},$item);
 9473:                 }
 9474:             } else {
 9475:                 if ($cancreate{$item} ne 'none') {
 9476:                     push(@{$changes{'cancreate'}},$item);
 9477:                 }
 9478:             }
 9479:         }
 9480:     } else {
 9481:         foreach my $item (@contexts)  {
 9482:             push(@{$changes{'cancreate'}},$item);
 9483:         }
 9484:     }
 9485: 
 9486:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 9487:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 9488:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 9489:                 push(@{$changes{'username_rule'}},$type);
 9490:             }
 9491:         }
 9492:         foreach my $type (@username_rule) {
 9493:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 9494:                 push(@{$changes{'username_rule'}},$type);
 9495:             }
 9496:         }
 9497:     } else {
 9498:         push(@{$changes{'username_rule'}},@username_rule);
 9499:     }
 9500: 
 9501:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 9502:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 9503:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 9504:                 push(@{$changes{'id_rule'}},$type);
 9505:             }
 9506:         }
 9507:         foreach my $type (@id_rule) {
 9508:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 9509:                 push(@{$changes{'id_rule'}},$type);
 9510:             }
 9511:         }
 9512:     } else {
 9513:         push(@{$changes{'id_rule'}},@id_rule);
 9514:     }
 9515: 
 9516:     my @authen_contexts = ('author','course','domain');
 9517:     my @authtypes = ('int','krb4','krb5','loc');
 9518:     my %authhash;
 9519:     foreach my $item (@authen_contexts) {
 9520:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 9521:         foreach my $auth (@authtypes) {
 9522:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 9523:                 $authhash{$item}{$auth} = 1;
 9524:             } else {
 9525:                 $authhash{$item}{$auth} = 0;
 9526:             }
 9527:         }
 9528:     }
 9529:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 9530:         foreach my $item (@authen_contexts) {
 9531:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 9532:                 foreach my $auth (@authtypes) {
 9533:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 9534:                         push(@{$changes{'authtypes'}},$item);
 9535:                         last;
 9536:                     }
 9537:                 }
 9538:             }
 9539:         }
 9540:     } else {
 9541:         foreach my $item (@authen_contexts) {
 9542:             push(@{$changes{'authtypes'}},$item);
 9543:         }
 9544:     }
 9545: 
 9546:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
 9547:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
 9548:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
 9549:     $save_usercreate{'id_rule'} = \@id_rule;
 9550:     $save_usercreate{'username_rule'} = \@username_rule,
 9551:     $save_usercreate{'authtypes'} = \%authhash;
 9552: 
 9553:     my %usercreation_hash =  (
 9554:         usercreation     => \%save_usercreate,
 9555:     );
 9556: 
 9557:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 9558:                                              $dom);
 9559: 
 9560:     if ($putresult eq 'ok') {
 9561:         if (keys(%changes) > 0) {
 9562:             $resulttext = &mt('Changes made:').'<ul>';
 9563:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 9564:                 my %lt = &usercreation_types();
 9565:                 foreach my $type (@{$changes{'cancreate'}}) {
 9566:                     my $chgtext = $lt{$type}.', ';
 9567:                     if ($cancreate{$type} eq 'none') {
 9568:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 9569:                     } elsif ($cancreate{$type} eq 'any') {
 9570:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 9571:                     } elsif ($cancreate{$type} eq 'official') {
 9572:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 9573:                     } elsif ($cancreate{$type} eq 'unofficial') {
 9574:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 9575:                     }
 9576:                     $resulttext .= '<li>'.$chgtext.'</li>';
 9577:                 }
 9578:             }
 9579:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 9580:                 my ($rules,$ruleorder) = 
 9581:                     &Apache::lonnet::inst_userrules($dom,'username');
 9582:                 my $chgtext = '<ul>';
 9583:                 foreach my $type (@username_rule) {
 9584:                     if (ref($rules->{$type}) eq 'HASH') {
 9585:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 9586:                     }
 9587:                 }
 9588:                 $chgtext .= '</ul>';
 9589:                 if (@username_rule > 0) {
 9590:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 9591:                 } else {
 9592:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 9593:                 }
 9594:             }
 9595:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 9596:                 my ($idrules,$idruleorder) = 
 9597:                     &Apache::lonnet::inst_userrules($dom,'id');
 9598:                 my $chgtext = '<ul>';
 9599:                 foreach my $type (@id_rule) {
 9600:                     if (ref($idrules->{$type}) eq 'HASH') {
 9601:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 9602:                     }
 9603:                 }
 9604:                 $chgtext .= '</ul>';
 9605:                 if (@id_rule > 0) {
 9606:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 9607:                 } else {
 9608:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 9609:                 }
 9610:             }
 9611:             my %authname = &authtype_names();
 9612:             my %context_title = &context_names();
 9613:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 9614:                 my $chgtext = '<ul>';
 9615:                 foreach my $type (@{$changes{'authtypes'}}) {
 9616:                     my @allowed;
 9617:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 9618:                     foreach my $auth (@authtypes) {
 9619:                         if ($authhash{$type}{$auth}) {
 9620:                             push(@allowed,$authname{$auth});
 9621:                         }
 9622:                     }
 9623:                     if (@allowed > 0) {
 9624:                         $chgtext .= join(', ',@allowed).'</li>';
 9625:                     } else {
 9626:                         $chgtext .= &mt('none').'</li>';
 9627:                     }
 9628:                 }
 9629:                 $chgtext .= '</ul>';
 9630:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 9631:                 $resulttext .= '</li>';
 9632:             }
 9633:             $resulttext .= '</ul>';
 9634:         } else {
 9635:             $resulttext = &mt('No changes made to user creation settings');
 9636:         }
 9637:     } else {
 9638:         $resulttext = '<span class="LC_error">'.
 9639:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9640:     }
 9641:     if ($warningmsg ne '') {
 9642:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 9643:     }
 9644:     return $resulttext;
 9645: }
 9646: 
 9647: sub modify_selfcreation {
 9648:     my ($dom,%domconfig) = @_;
 9649:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%changes,%cancreate);
 9650:     my (%save_usercreate,%save_usermodify);
 9651:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9652:     if (ref($types) eq 'ARRAY') {
 9653:         $usertypes->{'default'} = $othertitle;
 9654:         push(@{$types},'default');
 9655:     }
 9656: #
 9657: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
 9658: #
 9659:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 9660:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 9661:             if ($key eq 'cancreate') {
 9662:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 9663:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 9664:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 9665:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') || 
 9666:                             ($item eq 'recaptchaversion') ||
 9667:                             ($item eq 'emailusername') || ($item eq 'notify') ||
 9668:                             ($item eq 'selfcreateprocessing') || ($item eq 'shibenv')) {
 9669:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 9670:                         } else {
 9671:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 9672:                         }
 9673:                     }
 9674:                 }
 9675:             } elsif ($key eq 'email_rule') {
 9676:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 9677:             } else {
 9678:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 9679:             }
 9680:         }
 9681:     }
 9682: #
 9683: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
 9684: #
 9685:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 9686:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 9687:             if ($key eq 'selfcreate') {
 9688:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
 9689:             } else {
 9690:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
 9691:             }
 9692:         }
 9693:     }
 9694: 
 9695:     my @contexts = ('selfcreate');
 9696:     @{$cancreate{'selfcreate'}} = ();
 9697:     %{$cancreate{'emailusername'}} = ();
 9698:     @{$cancreate{'statustocreate'}} = ();
 9699:     %{$cancreate{'selfcreateprocessing'}} = ();
 9700:     %{$cancreate{'shibenv'}} = ();
 9701:     my %selfcreatetypes = (
 9702:                              sso   => 'users authenticated by institutional single sign on',
 9703:                              login => 'users authenticated by institutional log-in',
 9704:                              email => 'users who provide a valid e-mail address for use as username',
 9705:                           );
 9706: #
 9707: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
 9708: # is permitted.
 9709: #
 9710: 
 9711:     my @statuses;
 9712:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 9713:         if (ref($domconfig{'inststatus'}{'inststatusguest'}) eq 'ARRAY') {
 9714:             @statuses = @{$domconfig{'inststatus'}{'inststatusguest'}};
 9715:         }
 9716:     }
 9717:     push(@statuses,'default');
 9718: 
 9719:     foreach my $item ('login','sso','email') {
 9720:         if ($item eq 'email') {
 9721:             if ($env{'form.cancreate_email'}) {
 9722:                 push(@{$cancreate{'selfcreate'}},'email');
 9723:                 push(@contexts,'selfcreateprocessing');
 9724:                 foreach my $type (@statuses) {
 9725:                     if ($type eq 'default') {
 9726:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess'};
 9727:                     } else { 
 9728:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
 9729:                     }
 9730:                 }
 9731:             }
 9732:         } else {
 9733:             if ($env{'form.cancreate_'.$item}) {
 9734:                 push(@{$cancreate{'selfcreate'}},$item);
 9735:             }
 9736:         }
 9737:     }
 9738:     my (@email_rule,%userinfo,%savecaptcha);
 9739:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 9740: #
 9741: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
 9742: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
 9743: #
 9744: 
 9745:     if ($env{'form.cancreate_email'}) {
 9746:         push(@contexts,'emailusername');
 9747:         if (ref($types) eq 'ARRAY') {
 9748:             foreach my $type (@{$types}) {
 9749:                 if (ref($infofields) eq 'ARRAY') {
 9750:                     foreach my $field (@{$infofields}) {
 9751:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
 9752:                             $cancreate{'emailusername'}{$type}{$field} = $1;
 9753:                         }
 9754:                     }
 9755:                 }
 9756:             }
 9757:         }
 9758: #
 9759: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
 9760: # queued requests for self-creation of account using e-mail address as username
 9761: #
 9762: 
 9763:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
 9764:         @approvalnotify = sort(@approvalnotify);
 9765:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
 9766:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 9767:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
 9768:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
 9769:                     push(@{$changes{'cancreate'}},'notify');
 9770:                 }
 9771:             } else {
 9772:                 if ($cancreate{'notify'}{'approval'}) {
 9773:                     push(@{$changes{'cancreate'}},'notify');
 9774:                 }
 9775:             }
 9776:         } elsif ($cancreate{'notify'}{'approval'}) {
 9777:             push(@{$changes{'cancreate'}},'notify');
 9778:         }
 9779: 
 9780: #
 9781: # Retrieve rules (if any) governing types of e-mail address which may be used as a username
 9782: #
 9783:         @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 9784:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
 9785:         if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 9786:             if (@{$curr_usercreation{'email_rule'}} > 0) {
 9787:                 foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 9788:                     if (!grep(/^\Q$type\E$/,@email_rule)) {
 9789:                         push(@{$changes{'email_rule'}},$type);
 9790:                     }
 9791:                 }
 9792:             }
 9793:             if (@email_rule > 0) {
 9794:                 foreach my $type (@email_rule) {
 9795:                     if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 9796:                         push(@{$changes{'email_rule'}},$type);
 9797:                     }
 9798:                 }
 9799:             }
 9800:         } elsif (@email_rule > 0) {
 9801:             push(@{$changes{'email_rule'}},@email_rule);
 9802:         }
 9803:     }
 9804: #  
 9805: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
 9806: # institutional log-in.
 9807: #
 9808:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
 9809:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9810:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
 9811:                ($domdefaults{'auth_def'} eq 'localauth'))) {
 9812:             $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.').' '.
 9813:                           &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.');
 9814:         }
 9815:     }
 9816:     my @fields = ('lastname','firstname','middlename','generation',
 9817:                   'permanentemail','id');
 9818:     my @shibfields = (@fields,'inststatus');
 9819:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9820: #
 9821: # Where usernames may created for institutional log-in and/or institutional single sign on:
 9822: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
 9823: # may self-create accounts 
 9824: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
 9825: # which the user may supply, if institutional data is unavailable.
 9826: #
 9827:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
 9828:         if (ref($types) eq 'ARRAY') {
 9829:             if (@{$types} > 1) {
 9830:                 @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
 9831:                 push(@contexts,'statustocreate');
 9832:             } else {
 9833:                 undef($cancreate{'statustocreate'});
 9834:             } 
 9835:             foreach my $type (@{$types}) {
 9836:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
 9837:                 foreach my $field (@fields) {
 9838:                     if (grep(/^\Q$field\E$/,@modifiable)) {
 9839:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
 9840:                     } else {
 9841:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
 9842:                     }
 9843:                 }
 9844:             }
 9845:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
 9846:                 foreach my $type (@{$types}) {
 9847:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
 9848:                         foreach my $field (@fields) {
 9849:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
 9850:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
 9851:                                 push(@{$changes{'selfcreate'}},$type);
 9852:                                 last;
 9853:                             }
 9854:                         }
 9855:                     }
 9856:                 }
 9857:             } else {
 9858:                 foreach my $type (@{$types}) {
 9859:                     push(@{$changes{'selfcreate'}},$type);
 9860:                 }
 9861:             }
 9862:         }
 9863:         foreach my $field (@shibfields) {
 9864:             if ($env{'form.shibenv_'.$field} ne '') {
 9865:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
 9866:             }
 9867:         }
 9868:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 9869:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
 9870:                 foreach my $field (@shibfields) {
 9871:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
 9872:                         push(@{$changes{'cancreate'}},'shibenv');
 9873:                     }
 9874:                 }
 9875:             } else {
 9876:                 foreach my $field (@shibfields) {
 9877:                     if ($env{'form.shibenv_'.$field}) {
 9878:                         push(@{$changes{'cancreate'}},'shibenv');
 9879:                         last;
 9880:                     }
 9881:                 }
 9882:             }
 9883:         }
 9884:     }
 9885:     foreach my $item (@contexts) {
 9886:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 9887:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 9888:                 if (ref($cancreate{$item}) eq 'ARRAY') {
 9889:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 9890:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9891:                             push(@{$changes{'cancreate'}},$item);
 9892:                         }
 9893:                     }
 9894:                 }
 9895:             }
 9896:             if (ref($cancreate{$item}) eq 'ARRAY') {
 9897:                 foreach my $type (@{$cancreate{$item}}) {
 9898:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 9899:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9900:                             push(@{$changes{'cancreate'}},$item);
 9901:                         }
 9902:                     }
 9903:                 }
 9904:             }
 9905:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
 9906:             if (ref($cancreate{$item}) eq 'HASH') {
 9907:                 foreach my $curr (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
 9908:                     if (ref($curr_usercreation{'cancreate'}{$item}{$curr}) eq 'HASH') {
 9909:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$curr}})) {
 9910:                             unless ($curr_usercreation{'cancreate'}{$item}{$curr}{$field} eq $cancreate{$item}{$curr}{$field}) {
 9911:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9912:                                     push(@{$changes{'cancreate'}},$item);
 9913:                                 }
 9914:                             }
 9915:                         }
 9916:                     } elsif ($item eq 'selfcreateprocessing') {
 9917:                         if ($cancreate{$item}{$curr} ne $curr_usercreation{'cancreate'}{$item}{$curr}) {
 9918:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9919:                                 push(@{$changes{'cancreate'}},$item);
 9920:                             }
 9921:                         }
 9922:                     } else {
 9923:                         if (!$cancreate{$item}{$curr}) {
 9924:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9925:                                 push(@{$changes{'cancreate'}},$item);
 9926:                             }
 9927:                         }
 9928:                     }
 9929:                 }
 9930:                 foreach my $field (keys(%{$cancreate{$item}})) {
 9931:                     if (ref($cancreate{$item}{$field}) eq 'HASH') {
 9932:                         foreach my $inner (keys(%{$cancreate{$item}{$field}})) {
 9933:                             if (ref($curr_usercreation{'cancreate'}{$item}{$field}) eq 'HASH') {
 9934:                                 unless ($curr_usercreation{'cancreate'}{$item}{$field}{$inner} eq $cancreate{$item}{$field}{$inner}) {
 9935:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9936:                                         push(@{$changes{'cancreate'}},$item);
 9937:                                     }
 9938:                                 }
 9939:                             } else {
 9940:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9941:                                     push(@{$changes{'cancreate'}},$item);
 9942:                                 }
 9943:                             }
 9944:                         }
 9945:                     } elsif ($item eq 'selfcreateprocessing') {
 9946:                         if ($cancreate{$item}{$field} ne $curr_usercreation{'cancreate'}{$item}{$field}) {
 9947:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9948:                                 push(@{$changes{'cancreate'}},$item);
 9949:                             }
 9950:                         }
 9951:                     } else {
 9952:                         if (!$curr_usercreation{'cancreate'}{$item}{$field}) {
 9953:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9954:                                 push(@{$changes{'cancreate'}},$item);
 9955:                             }
 9956:                         }
 9957:                     }
 9958:                 }
 9959:             }
 9960:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
 9961:             if (ref($cancreate{$item}) eq 'ARRAY') {
 9962:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
 9963:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9964:                         push(@{$changes{'cancreate'}},$item);
 9965:                     }
 9966:                 }
 9967:             } elsif (ref($cancreate{$item}) eq 'HASH') {
 9968:                 if (!$cancreate{$item}{$curr_usercreation{'cancreate'}{$item}}) {
 9969:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9970:                         push(@{$changes{'cancreate'}},$item);
 9971:                     }
 9972:                 }
 9973:             }
 9974:         } elsif ($item eq 'emailusername') {
 9975:             if (ref($cancreate{$item}) eq 'HASH') {
 9976:                 foreach my $type (keys(%{$cancreate{$item}})) {
 9977:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
 9978:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
 9979:                             if ($cancreate{$item}{$type}{$field}) {
 9980:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9981:                                     push(@{$changes{'cancreate'}},$item);
 9982:                                 }
 9983:                                 last;
 9984:                             }
 9985:                         }
 9986:                     }
 9987:                 }
 9988:             }
 9989:         }
 9990:     }
 9991: #
 9992: # Populate %save_usercreate hash with updates to self-creation configuration.
 9993: #
 9994:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
 9995:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
 9996:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
 9997:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
 9998:     if (ref($cancreate{'notify'}) eq 'HASH') {
 9999:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
10000:     }
10001:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
10002:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
10003:     }
10004:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
10005:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
10006:     }
10007:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
10008:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
10009:     }
10010:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
10011:     $save_usercreate{'emailrule'} = \@email_rule;
10012: 
10013:     my %userconfig_hash = (
10014:             usercreation     => \%save_usercreate,
10015:             usermodification => \%save_usermodify,
10016:     );
10017:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
10018:                                              $dom);
10019: #
10020: # Accumulate details of changes to domain cofiguration for self-creation of usernames in $resulttext
10021: #
10022:     if ($putresult eq 'ok') {
10023:         if (keys(%changes) > 0) {
10024:             $resulttext = &mt('Changes made:').'<ul>';
10025:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
10026:                 my %lt = &selfcreation_types();
10027:                 foreach my $type (@{$changes{'cancreate'}}) {
10028:                     my $chgtext;
10029:                     if ($type eq 'selfcreate') {
10030:                         if (@{$cancreate{$type}} == 0) {
10031:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
10032:                         } else {
10033:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
10034:                                         '<ul>';
10035:                             foreach my $case (@{$cancreate{$type}}) {
10036:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
10037:                             }
10038:                             $chgtext .= '</ul>';
10039:                             if (ref($cancreate{$type}) eq 'ARRAY') {
10040:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
10041:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
10042:                                         if (@{$cancreate{'statustocreate'}} == 0) {
10043:                                             $chgtext .= '<br />'.
10044:                                                         '<span class="LC_warning">'.
10045:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
10046:                                                         '</span>';
10047:                                         }
10048:                                     }
10049:                                 }
10050:                             }
10051:                         }
10052:                     } elsif ($type eq 'shibenv') {
10053:                         if (keys(%{$cancreate{$type}}) == 0) {
10054:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information'); 
10055:                         } else {
10056:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
10057:                                         '<ul>';
10058:                             foreach my $field (@shibfields) {
10059:                                 next if ($cancreate{$type}{$field} eq '');
10060:                                 if ($field eq 'inststatus') {
10061:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
10062:                                 } else {
10063:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
10064:                                 }
10065:                             }
10066:                             $chgtext .= '</ul>';
10067:                         }  
10068:                     } elsif ($type eq 'statustocreate') {
10069:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
10070:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
10071:                             if (@{$cancreate{'selfcreate'}} > 0) {
10072:                                 if (@{$cancreate{'statustocreate'}} == 0) {
10073:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
10074:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
10075:                                         $chgtext .= '<br />'.
10076:                                                     '<span class="LC_warning">'.
10077:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
10078:                                                     '</span>';
10079:                                     }
10080:                                 } elsif (ref($usertypes) eq 'HASH') {
10081:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
10082:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
10083:                                     } else {
10084:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
10085:                                     }
10086:                                     $chgtext .= '<ul>';
10087:                                     foreach my $case (@{$cancreate{$type}}) {
10088:                                         if ($case eq 'default') {
10089:                                             $chgtext .= '<li>'.$othertitle.'</li>';
10090:                                         } else {
10091:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
10092:                                         }
10093:                                     }
10094:                                     $chgtext .= '</ul>';
10095:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
10096:                                         $chgtext .= '<br /><span class="LC_warning">'.
10097:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
10098:                                                     '</span>';
10099:                                     }
10100:                                 }
10101:                             } else {
10102:                                 if (@{$cancreate{$type}} == 0) {
10103:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
10104:                                 } else {
10105:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
10106:                                 }
10107:                             }
10108:                         }
10109:                     } elsif ($type eq 'selfcreateprocessing') {
10110:                         my %choices = &Apache::lonlocal::texthash (
10111:                                                                     automatic => 'Automatic approval',
10112:                                                                     approval  => 'Queued for approval',
10113:                                                                   );
10114:                         if (@statuses > 1) {
10115:                             $chgtext .= &mt('Processing of requests to create account with e-mail address as username set as follows:'). 
10116:                                         '<ul>';
10117:                            foreach my $type (@statuses) {
10118:                                if ($type eq 'default') {
10119:                                    $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
10120:                                } else {
10121:                                    $chgtext .= '<li>'.$usertypes->{$type}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
10122:                                }
10123:                            }
10124:                            $chgtext .= '</ul>';
10125:                         } else {
10126:                            $chgtext .= &mt('Processing of requests to create account with e-mail address as username set to: "[_1]"',
10127:                                          $choices{$cancreate{'selfcreateprocessing'}{'default'}});
10128:                         }
10129:                     } elsif ($type eq 'captcha') {
10130:                         if ($savecaptcha{$type} eq 'notused') {
10131:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
10132:                         } else {
10133:                             my %captchas = &captcha_phrases();
10134:                             if ($captchas{$savecaptcha{$type}}) {
10135:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
10136:                             } else {
10137:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
10138:                             }
10139:                         }
10140:                     } elsif ($type eq 'recaptchakeys') {
10141:                         my ($privkey,$pubkey);
10142:                         if (ref($savecaptcha{$type}) eq 'HASH') {
10143:                             $pubkey = $savecaptcha{$type}{'public'};
10144:                             $privkey = $savecaptcha{$type}{'private'};
10145:                         }
10146:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
10147:                         if (!$pubkey) {
10148:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
10149:                         } else {
10150:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
10151:                         }
10152:                         if (!$privkey) {
10153:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
10154:                         } else {
10155:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
10156:                         }
10157:                         $chgtext .= '</ul>';
10158:                     } elsif ($type eq 'recaptchaversion') {
10159:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
10160:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
10161:                         }
10162:                     } elsif ($type eq 'emailusername') {
10163:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
10164:                             if (ref($types) eq 'ARRAY') {
10165:                                 foreach my $type (@{$types}) {
10166:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
10167:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
10168:                                             $chgtext .= &mt('When self-creating account with e-mail as username, the following information will be provided by [_1]:',"'$usertypes->{$type}'").
10169:                                                     '<ul>';
10170:                                             foreach my $field (@{$infofields}) {
10171:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
10172:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
10173:                                                 }
10174:                                             }
10175:                                             $chgtext .= '</ul>';
10176:                                         } else {
10177:                                             $chgtext .= &mt('When self creating account with e-mail as username, no information besides e-mail address will be provided by [_1].',"'$usertypes->{$type}'").'<br />';
10178:                                         }
10179:                                     } else {
10180:                                         $chgtext .= &mt('When self creating account with e-mail as username, no information besides e-mail address will be provided by [_1].',"'$usertypes->{$type}'").'<br />';
10181:                                     }
10182:                                 }
10183:                             }
10184:                         }
10185:                     } elsif ($type eq 'notify') {
10186:                         $chgtext = &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
10187:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
10188:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
10189:                                 if ($cancreate{'notify'}{'approval'}) {
10190:                                     $chgtext = &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
10191:                                 }
10192:                             }
10193:                         }
10194:                     }
10195:                     if ($chgtext) {
10196:                         $resulttext .= '<li>'.$chgtext.'</li>';
10197:                     }
10198:                 }
10199:             }
10200:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
10201:                 my ($emailrules,$emailruleorder) =
10202:                     &Apache::lonnet::inst_userrules($dom,'email');
10203:                 my $chgtext = '<ul>';
10204:                 foreach my $type (@email_rule) {
10205:                     if (ref($emailrules->{$type}) eq 'HASH') {
10206:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
10207:                     }
10208:                 }
10209:                 $chgtext .= '</ul>';
10210:                 if (@email_rule > 0) {
10211:                     $resulttext .= '<li>'.
10212:                                    &mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').
10213:                                        $chgtext.
10214:                                    '</li>';
10215:                 } else {
10216:                     $resulttext .= '<li>'.
10217:                                    &mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').
10218:                                    '</li>';
10219:                 }
10220:             }
10221:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
10222:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
10223:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
10224:                 foreach my $type (@{$changes{'selfcreate'}}) {
10225:                     my $typename = $type;
10226:                     if (ref($usertypes) eq 'HASH') {
10227:                         if ($usertypes->{$type} ne '') {
10228:                             $typename = $usertypes->{$type};
10229:                         }
10230:                     }
10231:                     my @modifiable;
10232:                     $resulttext .= '<li>'.
10233:                                     &mt('Self-creation of account by users with status: [_1]',
10234:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
10235:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
10236:                     foreach my $field (@fields) {
10237:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
10238:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
10239:                         }
10240:                     }
10241:                     if (@modifiable > 0) {
10242:                         $resulttext .= join(', ',@modifiable);
10243:                     } else {
10244:                         $resulttext .= &mt('none');
10245:                     }
10246:                     $resulttext .= '</li>';
10247:                 }
10248:                 $resulttext .= '</ul></li>';
10249:             }
10250:             $resulttext .= '</ul>';
10251:         } else {
10252:             $resulttext = &mt('No changes made to self-creation settings');
10253:         }
10254:     } else {
10255:         $resulttext = '<span class="LC_error">'.
10256:             &mt('An error occurred: [_1]',$putresult).'</span>';
10257:     }
10258:     if ($warningmsg ne '') {
10259:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
10260:     }
10261:     return $resulttext;
10262: }
10263: 
10264: sub process_captcha {
10265:     my ($container,$changes,$newsettings,$current) = @_;
10266:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
10267:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
10268:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
10269:         $newsettings->{'captcha'} = 'original';
10270:     }
10271:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
10272:         if ($container eq 'cancreate') {
10273:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
10274:                 push(@{$changes->{'cancreate'}},'captcha');
10275:             } elsif (!defined($changes->{'cancreate'})) {
10276:                 $changes->{'cancreate'} = ['captcha'];
10277:             }
10278:         } else {
10279:             $changes->{'captcha'} = 1;
10280:         }
10281:     }
10282:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
10283:     if ($newsettings->{'captcha'} eq 'recaptcha') {
10284:         $newpub = $env{'form.'.$container.'_recaptchapub'};
10285:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
10286:         $newpub =~ s/[^\w\-]//g;
10287:         $newpriv =~ s/[^\w\-]//g;
10288:         $newsettings->{'recaptchakeys'} = {
10289:                                              public  => $newpub,
10290:                                              private => $newpriv,
10291:                                           };
10292:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
10293:         $newversion =~ s/\D//g;
10294:         if ($newversion ne '2') {
10295:             $newversion = 1;
10296:         }
10297:         $newsettings->{'recaptchaversion'} = $newversion;
10298:     }
10299:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
10300:         $currpub = $current->{'recaptchakeys'}{'public'};
10301:         $currpriv = $current->{'recaptchakeys'}{'private'};
10302:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
10303:             $newsettings->{'recaptchakeys'} = {
10304:                                                  public  => '',
10305:                                                  private => '',
10306:                                               }
10307:         }
10308:     }
10309:     if ($current->{'captcha'} eq 'recaptcha') {
10310:         $currversion = $current->{'recaptchaversion'};
10311:         if ($currversion ne '2') {
10312:             $currversion = 1;
10313:         }
10314:     }
10315:     if ($currversion ne $newversion) {
10316:         if ($container eq 'cancreate') {
10317:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
10318:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
10319:             } elsif (!defined($changes->{'cancreate'})) {
10320:                 $changes->{'cancreate'} = ['recaptchaversion'];
10321:             }
10322:         } else {
10323:             $changes->{'recaptchaversion'} = 1;
10324:         }
10325:     }
10326:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
10327:         if ($container eq 'cancreate') {
10328:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
10329:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
10330:             } elsif (!defined($changes->{'cancreate'})) {
10331:                 $changes->{'cancreate'} = ['recaptchakeys'];
10332:             }
10333:         } else {
10334:             $changes->{'recaptchakeys'} = 1;
10335:         }
10336:     }
10337:     return;
10338: }
10339: 
10340: sub modify_usermodification {
10341:     my ($dom,%domconfig) = @_;
10342:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
10343:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
10344:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
10345:             if ($key eq 'selfcreate') {
10346:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
10347:             } else {  
10348:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
10349:             }
10350:         }
10351:     }
10352:     my @contexts = ('author','course');
10353:     my %context_title = (
10354:                            author => 'In author context',
10355:                            course => 'In course context',
10356:                         );
10357:     my @fields = ('lastname','firstname','middlename','generation',
10358:                   'permanentemail','id');
10359:     my %roles = (
10360:                   author => ['ca','aa'],
10361:                   course => ['st','ep','ta','in','cr'],
10362:                 );
10363:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
10364:     foreach my $context (@contexts) {
10365:         foreach my $role (@{$roles{$context}}) {
10366:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
10367:             foreach my $item (@fields) {
10368:                 if (grep(/^\Q$item\E$/,@modifiable)) {
10369:                     $modifyhash{$context}{$role}{$item} = 1;
10370:                 } else {
10371:                     $modifyhash{$context}{$role}{$item} = 0;
10372:                 }
10373:             }
10374:         }
10375:         if (ref($curr_usermodification{$context}) eq 'HASH') {
10376:             foreach my $role (@{$roles{$context}}) {
10377:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
10378:                     foreach my $field (@fields) {
10379:                         if ($modifyhash{$context}{$role}{$field} ne 
10380:                                 $curr_usermodification{$context}{$role}{$field}) {
10381:                             push(@{$changes{$context}},$role);
10382:                             last;
10383:                         }
10384:                     }
10385:                 }
10386:             }
10387:         } else {
10388:             foreach my $context (@contexts) {
10389:                 foreach my $role (@{$roles{$context}}) {
10390:                     push(@{$changes{$context}},$role);
10391:                 }
10392:             }
10393:         }
10394:     }
10395:     my %usermodification_hash =  (
10396:                                    usermodification => \%modifyhash,
10397:                                  );
10398:     my $putresult = &Apache::lonnet::put_dom('configuration',
10399:                                              \%usermodification_hash,$dom);
10400:     if ($putresult eq 'ok') {
10401:         if (keys(%changes) > 0) {
10402:             $resulttext = &mt('Changes made: ').'<ul>';
10403:             foreach my $context (@contexts) {
10404:                 if (ref($changes{$context}) eq 'ARRAY') {
10405:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
10406:                     if (ref($changes{$context}) eq 'ARRAY') {
10407:                         foreach my $role (@{$changes{$context}}) {
10408:                             my $rolename;
10409:                             if ($role eq 'cr') {
10410:                                 $rolename = &mt('Custom');
10411:                             } else {
10412:                                 $rolename = &Apache::lonnet::plaintext($role);
10413:                             }
10414:                             my @modifiable;
10415:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
10416:                             foreach my $field (@fields) {
10417:                                 if ($modifyhash{$context}{$role}{$field}) {
10418:                                     push(@modifiable,$fieldtitles{$field});
10419:                                 }
10420:                             }
10421:                             if (@modifiable > 0) {
10422:                                 $resulttext .= join(', ',@modifiable);
10423:                             } else {
10424:                                 $resulttext .= &mt('none'); 
10425:                             }
10426:                             $resulttext .= '</li>';
10427:                         }
10428:                         $resulttext .= '</ul></li>';
10429:                     }
10430:                 }
10431:             }
10432:             $resulttext .= '</ul>';
10433:         } else {
10434:             $resulttext = &mt('No changes made to user modification settings');
10435:         }
10436:     } else {
10437:         $resulttext = '<span class="LC_error">'.
10438:             &mt('An error occurred: [_1]',$putresult).'</span>';
10439:     }
10440:     return $resulttext;
10441: }
10442: 
10443: sub modify_defaults {
10444:     my ($dom,$lastactref,%domconfig) = @_;
10445:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
10446:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10447:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
10448:     my @authtypes = ('internal','krb4','krb5','localauth');
10449:     foreach my $item (@items) {
10450:         $newvalues{$item} = $env{'form.'.$item};
10451:         if ($item eq 'auth_def') {
10452:             if ($newvalues{$item} ne '') {
10453:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
10454:                     push(@errors,$item);
10455:                 }
10456:             }
10457:         } elsif ($item eq 'lang_def') {
10458:             if ($newvalues{$item} ne '') {
10459:                 if ($newvalues{$item} =~ /^(\w+)/) {
10460:                     my $langcode = $1;
10461:                     if ($langcode ne 'x_chef') {
10462:                         if (code2language($langcode) eq '') {
10463:                             push(@errors,$item);
10464:                         }
10465:                     }
10466:                 } else {
10467:                     push(@errors,$item);
10468:                 }
10469:             }
10470:         } elsif ($item eq 'timezone_def') {
10471:             if ($newvalues{$item} ne '') {
10472:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
10473:                     push(@errors,$item);   
10474:                 }
10475:             }
10476:         } elsif ($item eq 'datelocale_def') {
10477:             if ($newvalues{$item} ne '') {
10478:                 my @datelocale_ids = DateTime::Locale->ids();
10479:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
10480:                     push(@errors,$item);
10481:                 }
10482:             }
10483:         } elsif ($item eq 'portal_def') {
10484:             if ($newvalues{$item} ne '') {
10485:                 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])\/?$/) {
10486:                     push(@errors,$item);
10487:                 }
10488:             }
10489:         }
10490:         if (grep(/^\Q$item\E$/,@errors)) {
10491:             $newvalues{$item} = $domdefaults{$item};
10492:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
10493:             $changes{$item} = 1;
10494:         }
10495:         $domdefaults{$item} = $newvalues{$item};
10496:     }
10497:     my %defaults_hash = (
10498:                          defaults => \%newvalues,
10499:                         );
10500:     my $title = &defaults_titles();
10501: 
10502:     my $currinststatus;
10503:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
10504:         $currinststatus = $domconfig{'inststatus'};
10505:     } else {
10506:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10507:         $currinststatus = {
10508:                              inststatustypes => $usertypes,
10509:                              inststatusorder => $types,
10510:                              inststatusguest => [],
10511:                           };
10512:     }
10513:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
10514:     my @allpos;
10515:     my %guests;
10516:     my %alltypes;
10517:     my ($currtitles,$currguests,$currorder);
10518:     if (ref($currinststatus) eq 'HASH') {
10519:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
10520:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
10521:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
10522:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
10523:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
10524:                     }
10525:                 }
10526:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
10527:                     my $position = $env{'form.inststatus_pos_'.$type};
10528:                     $position =~ s/\D+//g;
10529:                     $allpos[$position] = $type;
10530:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
10531:                     $alltypes{$type} =~ s/`//g;
10532:                     if ($env{'form.inststatus_guest_'.$type}) {
10533:                         $guests{$type} = 1;
10534:                     }
10535:                 }
10536:             }
10537:             if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
10538:                 $currguests = join(',',@{$currinststatus->{'inststatusguest'}});
10539:             }
10540:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
10541:             $currtitles =~ s/,$//;
10542:         }
10543:     }
10544:     if ($env{'form.addinststatus'}) {
10545:         my $newtype = $env{'form.addinststatus'};
10546:         $newtype =~ s/\W//g;
10547:         unless (exists($alltypes{$newtype})) {
10548:             if ($env{'form.addinststatus_guest'}) {
10549:                 $guests{$newtype} = 1;
10550:             }
10551:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
10552:             $alltypes{$newtype} =~ s/`//g; 
10553:             my $position = $env{'form.addinststatus_pos'};
10554:             $position =~ s/\D+//g;
10555:             if ($position ne '') {
10556:                 $allpos[$position] = $newtype;
10557:             }
10558:         }
10559:     }
10560:     my (@orderedstatus,@orderedguests);
10561:     foreach my $type (@allpos) {
10562:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
10563:             push(@orderedstatus,$type);
10564:             if ($guests{$type}) {
10565:                 push(@orderedguests,$type);
10566:             }
10567:         }
10568:     }
10569:     foreach my $type (keys(%alltypes)) {
10570:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
10571:             delete($alltypes{$type});
10572:         }
10573:     }
10574:     $defaults_hash{'inststatus'} = {
10575:                                      inststatustypes => \%alltypes,
10576:                                      inststatusorder => \@orderedstatus,
10577:                                      inststatusguest => \@orderedguests,
10578:                                    };
10579:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
10580:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
10581:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
10582:         }
10583:     }
10584:     if ($currorder ne join(',',@orderedstatus)) {
10585:         $changes{'inststatus'}{'inststatusorder'} = 1;
10586:     }
10587:     if ($currguests ne join(',',@orderedguests)) {
10588:         $changes{'inststatus'}{'inststatusguest'} = 1;
10589:     }
10590:     my $newtitles;
10591:     foreach my $item (@orderedstatus) {
10592:         $newtitles .= $alltypes{$item}.',';
10593:     }
10594:     $newtitles =~ s/,$//;
10595:     if ($currtitles ne $newtitles) {
10596:         $changes{'inststatus'}{'inststatustypes'} = 1;
10597:     }
10598:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
10599:                                              $dom);
10600:     if ($putresult eq 'ok') {
10601:         if (keys(%changes) > 0) {
10602:             $resulttext = &mt('Changes made:').'<ul>';
10603:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
10604:             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";
10605:             foreach my $item (sort(keys(%changes))) {
10606:                 if ($item eq 'inststatus') {
10607:                     if (ref($changes{'inststatus'}) eq 'HASH') {
10608:                         if (($changes{'inststatus'}{'inststatustypes'}) || $changes{'inststatus'}{'inststatusorder'}) {
10609:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
10610:                             foreach my $type (@orderedstatus) { 
10611:                                 $resulttext .= $alltypes{$type}.', ';
10612:                             }
10613:                             $resulttext =~ s/, $//;
10614:                             $resulttext .= '</li>';
10615:                         }
10616:                         if ($changes{'inststatus'}{'inststatusguest'}) {
10617:                             $resulttext .= '<li>'; 
10618:                             if (@orderedguests) {
10619:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to:').' ';
10620:                                 foreach my $type (@orderedguests) {
10621:                                     $resulttext .= $alltypes{$type}.', ';
10622:                                 }
10623:                                 $resulttext =~ s/, $//;
10624:                             } else {
10625:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to none.');
10626:                             }
10627:                             $resulttext .= '</li>';
10628:                         }
10629:                     }
10630:                 } else {
10631:                     my $value = $env{'form.'.$item};
10632:                     if ($value eq '') {
10633:                         $value = &mt('none');
10634:                     } elsif ($item eq 'auth_def') {
10635:                         my %authnames = &authtype_names();
10636:                         my %shortauth = (
10637:                                           internal   => 'int',
10638:                                           krb4       => 'krb4',
10639:                                           krb5       => 'krb5',
10640:                                           localauth  => 'loc',
10641:                         );
10642:                         $value = $authnames{$shortauth{$value}};
10643:                     }
10644:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
10645:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
10646:                 }
10647:             }
10648:             $resulttext .= '</ul>';
10649:             $mailmsgtext .= "\n";
10650:             my $cachetime = 24*60*60;
10651:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10652:             if (ref($lastactref) eq 'HASH') {
10653:                 $lastactref->{'domdefaults'} = 1;
10654:             }
10655:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
10656:                 my $notify = 1;
10657:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
10658:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
10659:                         $notify = 0;
10660:                     }
10661:                 }
10662:                 if ($notify) {
10663:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
10664:                                                "LON-CAPA Domain Settings Change - $dom",
10665:                                                $mailmsgtext);
10666:                 }
10667:             }
10668:         } else {
10669:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
10670:         }
10671:     } else {
10672:         $resulttext = '<span class="LC_error">'.
10673:             &mt('An error occurred: [_1]',$putresult).'</span>';
10674:     }
10675:     if (@errors > 0) {
10676:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
10677:         foreach my $item (@errors) {
10678:             $resulttext .= ' "'.$title->{$item}.'",';
10679:         }
10680:         $resulttext =~ s/,$//;
10681:     }
10682:     return $resulttext;
10683: }
10684: 
10685: sub modify_scantron {
10686:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
10687:     my ($resulttext,%confhash,%changes,$errors);
10688:     my $custom = 'custom.tab';
10689:     my $default = 'default.tab';
10690:     my $servadm = $r->dir_config('lonAdmEMail');
10691:     my ($configuserok,$author_ok,$switchserver) = 
10692:         &config_check($dom,$confname,$servadm);
10693:     if ($env{'form.scantronformat.filename'} ne '') {
10694:         my $error;
10695:         if ($configuserok eq 'ok') {
10696:             if ($switchserver) {
10697:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
10698:             } else {
10699:                 if ($author_ok eq 'ok') {
10700:                     my ($result,$scantronurl) =
10701:                         &publishlogo($r,'upload','scantronformat',$dom,
10702:                                      $confname,'scantron','','',$custom);
10703:                     if ($result eq 'ok') {
10704:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
10705:                         $changes{'scantronformat'} = 1;
10706:                     } else {
10707:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
10708:                     }
10709:                 } else {
10710:                     $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);
10711:                 }
10712:             }
10713:         } else {
10714:             $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);
10715:         }
10716:         if ($error) {
10717:             &Apache::lonnet::logthis($error);
10718:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10719:         }
10720:     }
10721:     if (ref($domconfig{'scantron'}) eq 'HASH') {
10722:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
10723:             if ($env{'form.scantronformat_del'}) {
10724:                 $confhash{'scantron'}{'scantronformat'} = '';
10725:                 $changes{'scantronformat'} = 1;
10726:             }
10727:         }
10728:     }
10729:     if (keys(%confhash) > 0) {
10730:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
10731:                                                  $dom);
10732:         if ($putresult eq 'ok') {
10733:             if (keys(%changes) > 0) {
10734:                 if (ref($confhash{'scantron'}) eq 'HASH') {
10735:                     $resulttext = &mt('Changes made:').'<ul>';
10736:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
10737:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
10738:                     } else {
10739:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
10740:                     }
10741:                     $resulttext .= '</ul>';
10742:                 } else {
10743:                     $resulttext = &mt('Changes made to bubblesheet format file.');
10744:                 }
10745:                 $resulttext .= '</ul>';
10746:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
10747:                 if (ref($lastactref) eq 'HASH') {
10748:                     $lastactref->{'domainconfig'} = 1;
10749:                 }
10750:             } else {
10751:                 $resulttext = &mt('No changes made to bubblesheet format file');
10752:             }
10753:         } else {
10754:             $resulttext = '<span class="LC_error">'.
10755:                 &mt('An error occurred: [_1]',$putresult).'</span>';
10756:         }
10757:     } else {
10758:         $resulttext = &mt('No changes made to bubblesheet format file'); 
10759:     }
10760:     if ($errors) {
10761:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
10762:                        $errors.'</ul>';
10763:     }
10764:     return $resulttext;
10765: }
10766: 
10767: sub modify_coursecategories {
10768:     my ($dom,$lastactref,%domconfig) = @_;
10769:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
10770:         $cathash);
10771:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
10772:     my @catitems = ('unauth','auth');
10773:     my @cattypes = ('std','domonly','codesrch','none');
10774:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
10775:         $cathash = $domconfig{'coursecategories'}{'cats'};
10776:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
10777:             $changes{'togglecats'} = 1;
10778:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
10779:         }
10780:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
10781:             $changes{'categorize'} = 1;
10782:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
10783:         }
10784:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
10785:             $changes{'togglecatscomm'} = 1;
10786:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
10787:         }
10788:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
10789:             $changes{'categorizecomm'} = 1;
10790:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
10791:         }
10792:         foreach my $item (@catitems) {
10793:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
10794:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
10795:                     $changes{$item} = 1;
10796:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
10797:                 }
10798:             }
10799:         }
10800:     } else {
10801:         $changes{'togglecats'} = 1;
10802:         $changes{'categorize'} = 1;
10803:         $changes{'togglecatscomm'} = 1;
10804:         $changes{'categorizecomm'} = 1;
10805:         $domconfig{'coursecategories'} = {
10806:                                              togglecats => $env{'form.togglecats'},
10807:                                              categorize => $env{'form.categorize'},
10808:                                              togglecatscomm => $env{'form.togglecatscomm'},
10809:                                              categorizecomm => $env{'form.categorizecomm'},
10810:                                          };
10811:         foreach my $item (@catitems) {
10812:             if ($env{'form.coursecat_'.$item} ne 'std') {
10813:                 $changes{$item} = 1;
10814:             }
10815:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
10816:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
10817:             }
10818:         }
10819:     }
10820:     if (ref($cathash) eq 'HASH') {
10821:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
10822:             push (@deletecategory,'instcode::0');
10823:         }
10824:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
10825:             push(@deletecategory,'communities::0');
10826:         }
10827:     }
10828:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
10829:     if (ref($cathash) eq 'HASH') {
10830:         if (@deletecategory > 0) {
10831:             #FIXME Need to remove category from all courses using a deleted category 
10832:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
10833:             foreach my $item (@deletecategory) {
10834:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
10835:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
10836:                     $deletions{$item} = 1;
10837:                     &recurse_cat_deletes($item,$cathash,\%deletions);
10838:                 }
10839:             }
10840:         }
10841:         foreach my $item (keys(%{$cathash})) {
10842:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
10843:             if ($cathash->{$item} ne $env{'form.'.$item}) {
10844:                 $reorderings{$item} = 1;
10845:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
10846:             }
10847:             if ($env{'form.addcategory_name_'.$item} ne '') {
10848:                 my $newcat = $env{'form.addcategory_name_'.$item};
10849:                 my $newdepth = $depth+1;
10850:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
10851:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
10852:                 $adds{$newitem} = 1; 
10853:             }
10854:             if ($env{'form.subcat_'.$item} ne '') {
10855:                 my $newcat = $env{'form.subcat_'.$item};
10856:                 my $newdepth = $depth+1;
10857:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
10858:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
10859:                 $adds{$newitem} = 1;
10860:             }
10861:         }
10862:     }
10863:     if ($env{'form.instcode'} eq '1') {
10864:         if (ref($cathash) eq 'HASH') {
10865:             my $newitem = 'instcode::0';
10866:             if ($cathash->{$newitem} eq '') {  
10867:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
10868:                 $adds{$newitem} = 1;
10869:             }
10870:         } else {
10871:             my $newitem = 'instcode::0';
10872:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
10873:             $adds{$newitem} = 1;
10874:         }
10875:     }
10876:     if ($env{'form.communities'} eq '1') {
10877:         if (ref($cathash) eq 'HASH') {
10878:             my $newitem = 'communities::0';
10879:             if ($cathash->{$newitem} eq '') {
10880:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
10881:                 $adds{$newitem} = 1;
10882:             }
10883:         } else {
10884:             my $newitem = 'communities::0';
10885:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
10886:             $adds{$newitem} = 1;
10887:         }
10888:     }
10889:     if ($env{'form.addcategory_name'} ne '') {
10890:         if (($env{'form.addcategory_name'} ne 'instcode') &&
10891:             ($env{'form.addcategory_name'} ne 'communities')) {
10892:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
10893:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
10894:             $adds{$newitem} = 1;
10895:         }
10896:     }
10897:     my $putresult;
10898:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
10899:         if (keys(%deletions) > 0) {
10900:             foreach my $key (keys(%deletions)) {
10901:                 if ($predelallitems{$key} ne '') {
10902:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
10903:                 }
10904:             }
10905:         }
10906:         my (@chkcats,@chktrails,%chkallitems);
10907:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
10908:         if (ref($chkcats[0]) eq 'ARRAY') {
10909:             my $depth = 0;
10910:             my $chg = 0;
10911:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
10912:                 my $name = $chkcats[0][$i];
10913:                 my $item;
10914:                 if ($name eq '') {
10915:                     $chg ++;
10916:                 } else {
10917:                     $item = &escape($name).'::0';
10918:                     if ($chg) {
10919:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
10920:                     }
10921:                     $depth ++; 
10922:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
10923:                     $depth --;
10924:                 }
10925:             }
10926:         }
10927:     }
10928:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
10929:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
10930:         if ($putresult eq 'ok') {
10931:             my %title = (
10932:                          togglecats     => 'Show/Hide a course in catalog',
10933:                          categorize     => 'Assign a category to a course',
10934:                          togglecatscomm => 'Show/Hide a community in catalog',
10935:                          categorizecomm => 'Assign a category to a community',
10936:                         );
10937:             my %level = (
10938:                          dom  => 'set in Domain ("Modify Course/Community")',
10939:                          crs  => 'set in Course ("Course Configuration")',
10940:                          comm => 'set in Community ("Community Configuration")',
10941:                          none     => 'No catalog',
10942:                          std      => 'Standard catalog',
10943:                          domonly  => 'Domain-only catalog',
10944:                          codesrch => 'Code search form',
10945:                         );
10946:             $resulttext = &mt('Changes made:').'<ul>';
10947:             if ($changes{'togglecats'}) {
10948:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
10949:             }
10950:             if ($changes{'categorize'}) {
10951:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
10952:             }
10953:             if ($changes{'togglecatscomm'}) {
10954:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
10955:             }
10956:             if ($changes{'categorizecomm'}) {
10957:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
10958:             }
10959:             if ($changes{'unauth'}) {
10960:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
10961:             }
10962:             if ($changes{'auth'}) {
10963:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
10964:             }
10965:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
10966:                 my $cathash;
10967:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
10968:                     $cathash = $domconfig{'coursecategories'}{'cats'};
10969:                 } else {
10970:                     $cathash = {};
10971:                 } 
10972:                 my (@cats,@trails,%allitems);
10973:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
10974:                 if (keys(%deletions) > 0) {
10975:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
10976:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
10977:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
10978:                     }
10979:                     $resulttext .= '</ul></li>';
10980:                 }
10981:                 if (keys(%reorderings) > 0) {
10982:                     my %sort_by_trail;
10983:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
10984:                     foreach my $key (keys(%reorderings)) {
10985:                         if ($allitems{$key} ne '') {
10986:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
10987:                         }
10988:                     }
10989:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
10990:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
10991:                     }
10992:                     $resulttext .= '</ul></li>';
10993:                 }
10994:                 if (keys(%adds) > 0) {
10995:                     my %sort_by_trail;
10996:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
10997:                     foreach my $key (keys(%adds)) {
10998:                         if ($allitems{$key} ne '') {
10999:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
11000:                         }
11001:                     }
11002:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
11003:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
11004:                     }
11005:                     $resulttext .= '</ul></li>';
11006:                 }
11007:             }
11008:             $resulttext .= '</ul>';
11009:             if ($changes{'unauth'} || $changes{'auth'}) {
11010:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
11011:                 if ($changes{'auth'}) {
11012:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
11013:                 }
11014:                 if ($changes{'unauth'}) {
11015:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
11016:                 }
11017:                 my $cachetime = 24*60*60;
11018:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
11019:                 if (ref($lastactref) eq 'HASH') {
11020:                     $lastactref->{'domdefaults'} = 1;
11021:                 }
11022:             }
11023:         } else {
11024:             $resulttext = '<span class="LC_error">'.
11025:                           &mt('An error occurred: [_1]',$putresult).'</span>';
11026:         }
11027:     } else {
11028:         $resulttext = &mt('No changes made to course and community categories');
11029:     }
11030:     return $resulttext;
11031: }
11032: 
11033: sub modify_serverstatuses {
11034:     my ($dom,%domconfig) = @_;
11035:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
11036:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
11037:         %currserverstatus = %{$domconfig{'serverstatuses'}};
11038:     }
11039:     my @pages = &serverstatus_pages();
11040:     foreach my $type (@pages) {
11041:         $newserverstatus{$type}{'namedusers'} = '';
11042:         $newserverstatus{$type}{'machines'} = '';
11043:         if (defined($env{'form.'.$type.'_namedusers'})) {
11044:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
11045:             my @okusers;
11046:             foreach my $user (@users) {
11047:                 my ($uname,$udom) = split(/:/,$user);
11048:                 if (($udom =~ /^$match_domain$/) &&   
11049:                     (&Apache::lonnet::domain($udom)) &&
11050:                     ($uname =~ /^$match_username$/)) {
11051:                     if (!grep(/^\Q$user\E/,@okusers)) {
11052:                         push(@okusers,$user);
11053:                     }
11054:                 }
11055:             }
11056:             if (@okusers > 0) {
11057:                  @okusers = sort(@okusers);
11058:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
11059:             }
11060:         }
11061:         if (defined($env{'form.'.$type.'_machines'})) {
11062:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
11063:             my @okmachines;
11064:             foreach my $ip (@machines) {
11065:                 my @parts = split(/\./,$ip);
11066:                 next if (@parts < 4);
11067:                 my $badip = 0;
11068:                 for (my $i=0; $i<4; $i++) {
11069:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
11070:                         $badip = 1;
11071:                         last;
11072:                     }
11073:                 }
11074:                 if (!$badip) {
11075:                     push(@okmachines,$ip);     
11076:                 }
11077:             }
11078:             @okmachines = sort(@okmachines);
11079:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
11080:         }
11081:     }
11082:     my %serverstatushash =  (
11083:                                 serverstatuses => \%newserverstatus,
11084:                             );
11085:     foreach my $type (@pages) {
11086:         foreach my $setting ('namedusers','machines') {
11087:             my (@current,@new);
11088:             if (ref($currserverstatus{$type}) eq 'HASH') {
11089:                 if ($currserverstatus{$type}{$setting} ne '') { 
11090:                     @current = split(/,/,$currserverstatus{$type}{$setting});
11091:                 }
11092:             }
11093:             if ($newserverstatus{$type}{$setting} ne '') {
11094:                 @new = split(/,/,$newserverstatus{$type}{$setting});
11095:             }
11096:             if (@current > 0) {
11097:                 if (@new > 0) {
11098:                     foreach my $item (@current) {
11099:                         if (!grep(/^\Q$item\E$/,@new)) {
11100:                             $changes{$type}{$setting} = 1;
11101:                             last;
11102:                         }
11103:                     }
11104:                     foreach my $item (@new) {
11105:                         if (!grep(/^\Q$item\E$/,@current)) {
11106:                             $changes{$type}{$setting} = 1;
11107:                             last;
11108:                         }
11109:                     }
11110:                 } else {
11111:                     $changes{$type}{$setting} = 1;
11112:                 }
11113:             } elsif (@new > 0) {
11114:                 $changes{$type}{$setting} = 1;
11115:             }
11116:         }
11117:     }
11118:     if (keys(%changes) > 0) {
11119:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
11120:         my $putresult = &Apache::lonnet::put_dom('configuration',
11121:                                                  \%serverstatushash,$dom);
11122:         if ($putresult eq 'ok') {
11123:             $resulttext .= &mt('Changes made:').'<ul>';
11124:             foreach my $type (@pages) {
11125:                 if (ref($changes{$type}) eq 'HASH') {
11126:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
11127:                     if ($changes{$type}{'namedusers'}) {
11128:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
11129:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
11130:                         } else {
11131:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
11132:                         }
11133:                     }
11134:                     if ($changes{$type}{'machines'}) {
11135:                         if ($newserverstatus{$type}{'machines'} eq '') {
11136:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
11137:                         } else {
11138:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
11139:                         }
11140: 
11141:                     }
11142:                     $resulttext .= '</ul></li>';
11143:                 }
11144:             }
11145:             $resulttext .= '</ul>';
11146:         } else {
11147:             $resulttext = '<span class="LC_error">'.
11148:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
11149: 
11150:         }
11151:     } else {
11152:         $resulttext = &mt('No changes made to access to server status pages');
11153:     }
11154:     return $resulttext;
11155: }
11156: 
11157: sub modify_helpsettings {
11158:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
11159:     my ($resulttext,$errors,%changes,%helphash);
11160:     my %defaultchecked = ('submitbugs' => 'on');
11161:     my @offon = ('off','on');
11162:     my @toggles = ('submitbugs');
11163:     my %current = ('submitbugs' => '',
11164:                    'adhoc'      => {},
11165:                   );
11166:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
11167:         %current = %{$domconfig{'helpsettings'}};
11168:     }
11169:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11170:     foreach my $item (@toggles) {
11171:         if ($defaultchecked{$item} eq 'on') { 
11172:             if ($current{$item} eq '') {
11173:                 if ($env{'form.'.$item} eq '0') {
11174:                     $changes{$item} = 1;
11175:                 }
11176:             } elsif ($current{$item} ne $env{'form.'.$item}) {
11177:                 $changes{$item} = 1;
11178:             }
11179:         } elsif ($defaultchecked{$item} eq 'off') {
11180:             if ($current{$item} eq '') {
11181:                 if ($env{'form.'.$item} eq '1') {
11182:                     $changes{$item} = 1;
11183:                 }
11184:             } elsif ($current{$item} ne $env{'form.'.$item}) {
11185:                 $changes{$item} = 1;
11186:             }
11187:         }
11188:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
11189:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
11190:         }
11191:     }
11192:     my $maxnum = $env{'form.helproles_maxnum'};
11193:     my $confname = $dom.'-domainconfig';
11194:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
11195:     my (@allpos,%newsettings,%changedprivs,$newrole);
11196:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
11197:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
11198:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
11199:     my %lt = &Apache::lonlocal::texthash(
11200:                     s      => 'system',
11201:                     d      => 'domain',
11202:                     order  => 'Display order',
11203:                     access => 'Role usage',
11204:                     all    => 'All with domain helpdesk or helpdesk assistant role',
11205:                     dh     => 'All with domain helpdesk role',
11206:                     da     => 'All with domain helpdesk assistant role',
11207:                     none   => 'None',
11208:                     status => 'Determined based on institutional status',
11209:                     inc    => 'Include all, but exclude specific personnel',
11210:                     exc    => 'Exclude all, but include specific personnel',
11211:     );
11212:     for (my $num=0; $num<=$maxnum; $num++) {
11213:         my ($prefix,$identifier,$rolename,%curr);
11214:         if ($num == $maxnum) {
11215:             next unless ($env{'form.newcusthelp'} == $maxnum);
11216:             $identifier = 'custhelp'.$num;
11217:             $prefix = 'helproles_'.$num;
11218:             $rolename = $env{'form.custhelpname'.$num};
11219:             $rolename=~s/[^A-Za-z0-9]//gs;
11220:             next if ($rolename eq '');
11221:             next if (exists($existing{'rolesdef_'.$rolename}));
11222:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
11223:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
11224:                                                      $newprivs{'c'},$confname,$dom);
11225:             if ($result ne 'ok') {
11226:                 $errors .= '<li><span class="LC_error">'.
11227:                            &mt('An error occurred storing the new custom role: [_1]',
11228:                            $result).'</span></li>';
11229:                 next;
11230:             } else {
11231:                 $changedprivs{$rolename} = \%newprivs;
11232:                 $newrole = $rolename;
11233:             }
11234:         } else {
11235:             $prefix = 'helproles_'.$num;
11236:             $rolename = $env{'form.'.$prefix};
11237:             next if ($rolename eq '');
11238:             next unless (exists($existing{'rolesdef_'.$rolename}));
11239:             $identifier = 'custhelp'.$num;
11240:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
11241:             my %currprivs;
11242:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
11243:                 split(/\_/,$existing{'rolesdef_'.$rolename});
11244:             foreach my $level ('c','d','s') {
11245:                 if ($newprivs{$level} ne $currprivs{$level}) {
11246:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
11247:                                                              $newprivs{'c'},$confname,$dom);
11248:                     if ($result ne 'ok') {
11249:                         $errors .= '<li><span class="LC_error">'.
11250:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
11251:                                        $rolename,$result).'</span></li>';
11252:                     } else {
11253:                         $changedprivs{$rolename} = \%newprivs;
11254:                     }
11255:                     last;
11256:                 }
11257:             }
11258:             if (ref($current{'adhoc'}) eq 'HASH') {
11259:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
11260:                     %curr = %{$current{'adhoc'}{$rolename}};
11261:                 }
11262:             }
11263:         }
11264:         my $newpos = $env{'form.'.$prefix.'_pos'};
11265:         $newpos =~ s/\D+//g;
11266:         $allpos[$newpos] = $rolename;
11267:         my $newdesc = $env{'form.'.$prefix.'_desc'};
11268:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
11269:         if ($curr{'desc'}) {
11270:             if ($curr{'desc'} ne $newdesc) {
11271:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
11272:                 $newsettings{$rolename}{'desc'} = $newdesc;
11273:             }
11274:         } elsif ($newdesc ne '') {
11275:             $changes{'customrole'}{$rolename}{'desc'} = 1;
11276:             $newsettings{$rolename}{'desc'} = $newdesc;
11277:         }
11278:         my $access = $env{'form.'.$prefix.'_access'};
11279:         if (grep(/^\Q$access\E$/,@accesstypes)) {
11280:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
11281:             if ($access eq 'status') {
11282:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
11283:                 if (scalar(@statuses) == 0) {
11284:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
11285:                 } else {
11286:                     my (@shownstatus,$numtypes);
11287:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
11288:                     if (ref($types) eq 'ARRAY') {
11289:                         $numtypes = scalar(@{$types});
11290:                         foreach my $type (sort(@statuses)) {
11291:                             if ($type eq 'default') {
11292:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
11293:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
11294:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
11295:                                 push(@shownstatus,$usertypes->{$type});
11296:                             }
11297:                         }
11298:                     }
11299:                     if (grep(/^default$/,@statuses)) {
11300:                         push(@shownstatus,$othertitle);
11301:                     }
11302:                     if (scalar(@shownstatus) == 1+$numtypes) {
11303:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
11304:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
11305:                     } else {
11306:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
11307:                         if (ref($curr{'status'}) eq 'ARRAY') {
11308:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
11309:                             if (@diffs) {
11310:                                 $changes{'customrole'}{$rolename}{$access} = 1;
11311:                             }
11312:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
11313:                             $changes{'customrole'}{$rolename}{$access} = 1;
11314:                         }
11315:                     }
11316:                 }
11317:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
11318:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
11319:                 my @newspecstaff;
11320:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
11321:                 foreach my $person (sort(@personnel)) {
11322:                     if ($domhelpdesk{$person}) {
11323:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
11324:                     }
11325:                 }
11326:                 if (ref($curr{$access}) eq 'ARRAY') {
11327:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
11328:                     if (@diffs) {
11329:                         $changes{'customrole'}{$rolename}{$access} = 1;
11330:                     }
11331:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
11332:                     $changes{'customrole'}{$rolename}{$access} = 1;
11333:                 }
11334:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
11335:                     my ($uname,$udom) = split(/:/,$person);
11336:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
11337:                 }
11338:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
11339:             }
11340:         } else {
11341:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
11342:         }
11343:         unless ($curr{'access'} eq $access) {
11344:             $changes{'customrole'}{$rolename}{'access'} = 1;
11345:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
11346:         }
11347:     }
11348:     if (@allpos > 0) {
11349:         my $idx = 0;
11350:         foreach my $rolename (@allpos) {
11351:             if ($rolename ne '') {
11352:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
11353:                 if (ref($current{'adhoc'}) eq 'HASH') {
11354:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
11355:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
11356:                             $changes{'customrole'}{$rolename}{'order'} = 1;
11357:                             $newsettings{$rolename}{'order'} = $idx+1;
11358:                         }
11359:                     }
11360:                 }
11361:                 $idx ++;
11362:             }
11363:         }
11364:     }
11365:     my $putresult;
11366:     if (keys(%changes) > 0) {
11367:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
11368:         if ($putresult eq 'ok') {
11369:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
11370:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
11371:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
11372:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
11373:                 }
11374:             }
11375:             my $cachetime = 24*60*60;
11376:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
11377:             if (ref($lastactref) eq 'HASH') {
11378:                 $lastactref->{'domdefaults'} = 1;
11379:             }
11380:         } else {
11381:             $errors .= '<li><span class="LC_error">'.
11382:                        &mt('An error occurred storing the settings: [_1]',
11383:                            $putresult).'</span></li>';
11384:         }
11385:     }
11386:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
11387:         $resulttext = &mt('Changes made:').'<ul>';
11388:         my (%shownprivs,@levelorder);
11389:         @levelorder = ('c','d','s');
11390:         if ((keys(%changes)) && ($putresult eq 'ok')) {
11391:             foreach my $item (sort(keys(%changes))) {
11392:                 if ($item eq 'submitbugs') {
11393:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
11394:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
11395:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
11396:                 } elsif ($item eq 'customrole') {
11397:                     if (ref($changes{'customrole'}) eq 'HASH') {
11398:                         my @keyorder = ('order','desc','access','status','exc','inc');
11399:                         my %keytext = &Apache::lonlocal::texthash(
11400:                                                                    order  => 'Order',
11401:                                                                    desc   => 'Role description',
11402:                                                                    access => 'Role usage',
11403:                                                                    status => 'Allowed instituional types',
11404:                                                                    exc    => 'Allowed personnel',
11405:                                                                    inc    => 'Disallowed personnel',
11406:                         );
11407:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
11408:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
11409:                                 if ($role eq $newrole) {
11410:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
11411:                                                               $role).'<ul>';
11412:                                 } else {
11413:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
11414:                                                               $role).'<ul>';
11415:                                 }
11416:                                 foreach my $key (@keyorder) {
11417:                                     if ($changes{'customrole'}{$role}{$key}) {
11418:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
11419:                                                                   $keytext{$key},$newsettings{$role}{$key}).
11420:                                                        '</li>';
11421:                                     }
11422:                                 }
11423:                                 if (ref($changedprivs{$role}) eq 'HASH') {
11424:                                     $shownprivs{$role} = 1;
11425:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
11426:                                     foreach my $level (@levelorder) {
11427:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
11428:                                             next if ($item eq '');
11429:                                             my ($priv) = split(/\&/,$item,2);
11430:                                             if (&Apache::lonnet::plaintext($priv)) {
11431:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
11432:                                                 unless ($level eq 'c') {
11433:                                                     $resulttext .= ' ('.$lt{$level}.')';
11434:                                                 }
11435:                                                 $resulttext .= '</li>';
11436:                                             }
11437:                                         }
11438:                                     }
11439:                                     $resulttext .= '</ul>';
11440:                                 }
11441:                                 $resulttext .= '</ul></li>';
11442:                             }
11443:                         }
11444:                     }
11445:                 }
11446:             }
11447:         }
11448:         if (keys(%changedprivs)) {
11449:             foreach my $role (sort(keys(%changedprivs))) {
11450:                 unless ($shownprivs{$role}) {
11451:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
11452:                                               $role).'<ul>'.
11453:                                    '<li>'.&mt('Privileges set to :').'<ul>';
11454:                     foreach my $level (@levelorder) {
11455:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
11456:                             next if ($item eq '');
11457:                             my ($priv) = split(/\&/,$item,2);
11458:                             if (&Apache::lonnet::plaintext($priv)) {
11459:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
11460:                                 unless ($level eq 'c') {
11461:                                     $resulttext .= ' ('.$lt{$level}.')';
11462:                                 }
11463:                                 $resulttext .= '</li>';
11464:                             }
11465:                         }
11466:                     }
11467:                     $resulttext .= '</ul></li></ul></li>';
11468:                 }
11469:             }
11470:         }
11471:         $resulttext .= '</ul>';
11472:     } else {
11473:         $resulttext = &mt('No changes made to help settings');
11474:     }
11475:     if ($errors) {
11476:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
11477:                                     $errors.'</ul>';
11478:     }
11479:     return $resulttext;
11480: }
11481: 
11482: sub modify_coursedefaults {
11483:     my ($dom,$lastactref,%domconfig) = @_;
11484:     my ($resulttext,$errors,%changes,%defaultshash);
11485:     my %defaultchecked = (
11486:                            'uselcmath'       => 'on',
11487:                            'usejsme'         => 'on'
11488:                          );
11489:     my @toggles = ('uselcmath','usejsme');
11490:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
11491:                    'uploadquota_community','uploadquota_textbook','mysqltables_official',
11492:                    'mysqltables_unofficial','mysqltables_community','mysqltables_textbook');
11493:     my @types = ('official','unofficial','community','textbook');
11494:     my %staticdefaults = (
11495:                            anonsurvey_threshold => 10,
11496:                            uploadquota          => 500,
11497:                            postsubmit           => 60,
11498:                            mysqltables          => 172800,
11499:                          );
11500: 
11501:     $defaultshash{'coursedefaults'} = {};
11502: 
11503:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
11504:         if ($domconfig{'coursedefaults'} eq '') {
11505:             $domconfig{'coursedefaults'} = {};
11506:         }
11507:     }
11508: 
11509:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
11510:         foreach my $item (@toggles) {
11511:             if ($defaultchecked{$item} eq 'on') {
11512:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
11513:                     ($env{'form.'.$item} eq '0')) {
11514:                     $changes{$item} = 1;
11515:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
11516:                     $changes{$item} = 1;
11517:                 }
11518:             } elsif ($defaultchecked{$item} eq 'off') {
11519:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
11520:                     ($env{'form.'.$item} eq '1')) {
11521:                     $changes{$item} = 1;
11522:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
11523:                     $changes{$item} = 1;
11524:                 }
11525:             }
11526:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
11527:         }
11528:         foreach my $item (@numbers) {
11529:             my ($currdef,$newdef);
11530:             $newdef = $env{'form.'.$item};
11531:             if ($item eq 'anonsurvey_threshold') {
11532:                 $currdef = $domconfig{'coursedefaults'}{$item};
11533:                 $newdef =~ s/\D//g;
11534:                 if ($newdef eq '' || $newdef < 1) {
11535:                     $newdef = 1;
11536:                 }
11537:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
11538:             } else {
11539:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
11540:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
11541:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
11542:                 }
11543:                 $newdef =~ s/[^\w.\-]//g;
11544:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
11545:             }
11546:             if ($currdef ne $newdef) {
11547:                 my $staticdef;
11548:                 if ($item eq 'anonsurvey_threshold') {
11549:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
11550:                         $changes{$item} = 1;
11551:                     }
11552:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
11553:                     my $setting = $1;
11554:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
11555:                         $changes{$setting} = 1;
11556:                     }
11557:                 }
11558:             }
11559:         }
11560:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
11561:         my @currclonecode;
11562:         if (ref($currclone) eq 'HASH') {
11563:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
11564:                 @currclonecode = @{$currclone->{'instcode'}};
11565:             }
11566:         }
11567:         my $newclone;
11568:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
11569:             $newclone = $env{'form.canclone'};
11570:         }
11571:         if ($newclone eq 'instcode') {
11572:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
11573:             my (%codedefaults,@code_order,@clonecode);
11574:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
11575:                                                     \@code_order);
11576:             foreach my $item (@code_order) {
11577:                 if (grep(/^\Q$item\E$/,@newcodes)) {
11578:                     push(@clonecode,$item);
11579:                 }
11580:             }
11581:             if (@clonecode) {
11582:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
11583:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
11584:                 if (@diffs) {
11585:                     $changes{'canclone'} = 1;
11586:                 }
11587:             } else {
11588:                 $newclone eq '';
11589:             }
11590:         } elsif ($newclone ne '') {
11591:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
11592:         }
11593:         if ($newclone ne $currclone) {
11594:             $changes{'canclone'} = 1;
11595:         }
11596:         my %credits;
11597:         foreach my $type (@types) {
11598:             unless ($type eq 'community') {
11599:                 $credits{$type} = $env{'form.'.$type.'_credits'};
11600:                 $credits{$type} =~ s/[^\d.]+//g;
11601:             }
11602:         }
11603:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
11604:             ($env{'form.coursecredits'} eq '1')) {
11605:             $changes{'coursecredits'} = 1;
11606:             foreach my $type (keys(%credits)) {
11607:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
11608:             }
11609:         } else {
11610:             if ($env{'form.coursecredits'} eq '1') {
11611:                 foreach my $type (@types) {
11612:                     unless ($type eq 'community') {
11613:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
11614:                             $changes{'coursecredits'} = 1;
11615:                         }
11616:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
11617:                     }
11618:                 }
11619:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
11620:                 foreach my $type (@types) {
11621:                     unless ($type eq 'community') {
11622:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
11623:                             $changes{'coursecredits'} = 1;
11624:                             last;
11625:                         }
11626:                     }
11627:                 }
11628:             }
11629:         }
11630:         if ($env{'form.postsubmit'} eq '1') {
11631:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
11632:             my %currtimeout;
11633:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
11634:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
11635:                     $changes{'postsubmit'} = 1;
11636:                 }
11637:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
11638:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
11639:                 }
11640:             } else {
11641:                 $changes{'postsubmit'} = 1;
11642:             }
11643:             foreach my $type (@types) {
11644:                 my $timeout = $env{'form.'.$type.'_timeout'};
11645:                 $timeout =~ s/\D//g;
11646:                 if ($timeout == $staticdefaults{'postsubmit'}) {
11647:                     $timeout = '';
11648:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
11649:                     $timeout = '0';
11650:                 }
11651:                 unless ($timeout eq '') {
11652:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
11653:                 }
11654:                 if (exists($currtimeout{$type})) {
11655:                     if ($timeout ne $currtimeout{$type}) {
11656:                         $changes{'postsubmit'} = 1;
11657:                     }
11658:                 } elsif ($timeout ne '') {
11659:                     $changes{'postsubmit'} = 1;
11660:                 }
11661:             }
11662:         } else {
11663:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
11664:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
11665:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
11666:                     $changes{'postsubmit'} = 1;
11667:                 }
11668:             } else {
11669:                 $changes{'postsubmit'} = 1;
11670:             }
11671:         }
11672:     }
11673:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
11674:                                              $dom);
11675:     if ($putresult eq 'ok') {
11676:         if (keys(%changes) > 0) {
11677:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11678:             if (($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
11679:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
11680:                 ($changes{'canclone'}) || ($changes{'mysqltables'})) {
11681:                 foreach my $item ('uselcmath','usejsme') {
11682:                     if ($changes{$item}) {
11683:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
11684:                     }
11685:                 }
11686:                 if ($changes{'coursecredits'}) {
11687:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
11688:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
11689:                             $domdefaults{$type.'credits'} =
11690:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
11691:                         }
11692:                     }
11693:                 }
11694:                 if ($changes{'postsubmit'}) {
11695:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
11696:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
11697:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
11698:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
11699:                                 $domdefaults{$type.'postsubtimeout'} =
11700:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
11701:                             }
11702:                         }
11703:                     }
11704:                 }
11705:                 if ($changes{'uploadquota'}) {
11706:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
11707:                         foreach my $type (@types) {
11708:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
11709:                         }
11710:                     }
11711:                 }
11712:                 if ($changes{'canclone'}) {
11713:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
11714:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
11715:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
11716:                             if (@clonecodes) {
11717:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
11718:                             }
11719:                         }
11720:                     } else {
11721:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
11722:                     }
11723:                 }
11724:                 my $cachetime = 24*60*60;
11725:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
11726:                 if (ref($lastactref) eq 'HASH') {
11727:                     $lastactref->{'domdefaults'} = 1;
11728:                 }
11729:             }
11730:             $resulttext = &mt('Changes made:').'<ul>';
11731:             foreach my $item (sort(keys(%changes))) {
11732:                 if ($item eq 'uselcmath') {
11733:                     if ($env{'form.'.$item} eq '1') {
11734:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
11735:                     } else {
11736:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
11737:                     }
11738:                 } elsif ($item eq 'usejsme') {
11739:                     if ($env{'form.'.$item} eq '1') {
11740:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
11741:                     } else {
11742:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
11743:                     }
11744:                 } elsif ($item eq 'anonsurvey_threshold') {
11745:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
11746:                 } elsif ($item eq 'uploadquota') {
11747:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
11748:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
11749:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
11750:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
11751:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
11752: 
11753:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
11754:                                        '</ul>'.
11755:                                        '</li>';
11756:                     } else {
11757:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
11758:                     }
11759:                 } elsif ($item eq 'mysqltables') {
11760:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
11761:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
11762:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
11763:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
11764:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
11765:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
11766:                                        '</ul>'.
11767:                                        '</li>';
11768:                     } else {
11769:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
11770:                     }
11771:                 } elsif ($item eq 'postsubmit') {
11772:                     if ($domdefaults{'postsubmit'} eq 'off') {
11773:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
11774:                     } else {
11775:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
11776:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
11777:                             $resulttext .= &mt('durations:').'<ul>';
11778:                             foreach my $type (@types) {
11779:                                 $resulttext .= '<li>';
11780:                                 my $timeout;
11781:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
11782:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
11783:                                 }
11784:                                 my $display;
11785:                                 if ($timeout eq '0') {
11786:                                     $display = &mt('unlimited');
11787:                                 } elsif ($timeout eq '') {
11788:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
11789:                                 } else {
11790:                                     $display = &mt('[quant,_1,second]',$timeout);
11791:                                 }
11792:                                 if ($type eq 'community') {
11793:                                     $resulttext .= &mt('Communities');
11794:                                 } elsif ($type eq 'official') {
11795:                                     $resulttext .= &mt('Official courses');
11796:                                 } elsif ($type eq 'unofficial') {
11797:                                     $resulttext .= &mt('Unofficial courses');
11798:                                 } elsif ($type eq 'textbook') {
11799:                                     $resulttext .= &mt('Textbook courses');
11800:                                 }
11801:                                 $resulttext .= ' -- '.$display.'</li>';
11802:                             }
11803:                             $resulttext .= '</ul>';
11804:                         }
11805:                         $resulttext .= '</li>';
11806:                     }
11807:                 } elsif ($item eq 'coursecredits') {
11808:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
11809:                         if (($domdefaults{'officialcredits'} eq '') &&
11810:                             ($domdefaults{'unofficialcredits'} eq '') &&
11811:                             ($domdefaults{'textbookcredits'} eq '')) {
11812:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
11813:                         } else {
11814:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
11815:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
11816:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
11817:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
11818:                                            '</ul>'.
11819:                                            '</li>';
11820:                         }
11821:                     } else {
11822:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
11823:                     }
11824:                 } elsif ($item eq 'canclone') {
11825:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
11826:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
11827:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
11828:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
11829:                         }
11830:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
11831:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
11832:                     } else {
11833:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
11834:                     }
11835:                 }
11836:             }
11837:             $resulttext .= '</ul>';
11838:         } else {
11839:             $resulttext = &mt('No changes made to course defaults');
11840:         }
11841:     } else {
11842:         $resulttext = '<span class="LC_error">'.
11843:             &mt('An error occurred: [_1]',$putresult).'</span>';
11844:     }
11845:     return $resulttext;
11846: }
11847: 
11848: sub modify_selfenrollment {
11849:     my ($dom,$lastactref,%domconfig) = @_;
11850:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
11851:     my @types = ('official','unofficial','community','textbook');
11852:     my %titles = &tool_titles();
11853:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
11854:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
11855:     $ordered{'default'} = ['types','registered','approval','limit'];
11856: 
11857:     my (%roles,%shown,%toplevel);
11858:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
11859: 
11860:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
11861:         if ($domconfig{'selfenrollment'} eq '') {
11862:             $domconfig{'selfenrollment'} = {};
11863:         }
11864:     }
11865:     %toplevel = (
11866:                   admin      => 'Configuration Rights',
11867:                   default    => 'Default settings',
11868:                   validation => 'Validation of self-enrollment requests',
11869:                 );
11870:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
11871: 
11872:     if (ref($ordered{'admin'}) eq 'ARRAY') {
11873:         foreach my $item (@{$ordered{'admin'}}) {
11874:             foreach my $type (@types) {
11875:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
11876:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
11877:                 } else {
11878:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
11879:                 }
11880:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
11881:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
11882:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
11883:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
11884:                             push(@{$changes{'admin'}{$type}},$item);
11885:                         }
11886:                     } else {
11887:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
11888:                             push(@{$changes{'admin'}{$type}},$item);
11889:                         }
11890:                     }
11891:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
11892:                     push(@{$changes{'admin'}{$type}},$item);
11893:                 }
11894:             }
11895:         }
11896:     }
11897: 
11898:     foreach my $item (@{$ordered{'default'}}) {
11899:         foreach my $type (@types) {
11900:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
11901:             if ($item eq 'types') {
11902:                 unless (($value eq 'all') || ($value eq 'dom')) {
11903:                     $value = '';
11904:                 }
11905:             } elsif ($item eq 'registered') {
11906:                 unless ($value eq '1') {
11907:                     $value = 0;
11908:                 }
11909:             } elsif ($item eq 'approval') {
11910:                 unless ($value =~ /^[012]$/) {
11911:                     $value = 0;
11912:                 }
11913:             } else {
11914:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
11915:                     $value = 'none';
11916:                 }
11917:             }
11918:             $selfenrollhash{'default'}{$type}{$item} = $value;
11919:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
11920:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
11921:                     if ($selfenrollhash{'default'}{$type}{$item} ne
11922:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
11923:                          push(@{$changes{'default'}{$type}},$item);
11924:                     }
11925:                 } else {
11926:                     push(@{$changes{'default'}{$type}},$item);
11927:                 }
11928:             } else {
11929:                 push(@{$changes{'default'}{$type}},$item);
11930:             }
11931:             if ($item eq 'limit') {
11932:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
11933:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
11934:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
11935:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
11936:                     }
11937:                 } else {
11938:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
11939:                 }
11940:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
11941:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
11942:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
11943:                          push(@{$changes{'default'}{$type}},'cap');
11944:                     }
11945:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
11946:                     push(@{$changes{'default'}{$type}},'cap');
11947:                 }
11948:             }
11949:         }
11950:     }
11951: 
11952:     foreach my $item (@{$itemsref}) {
11953:         if ($item eq 'fields') {
11954:             my @changed;
11955:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
11956:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
11957:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
11958:             }
11959:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
11960:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
11961:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
11962:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
11963:                 } else {
11964:                     @changed = @{$selfenrollhash{'validation'}{$item}};
11965:                 }
11966:             } else {
11967:                 @changed = @{$selfenrollhash{'validation'}{$item}};
11968:             }
11969:             if (@changed) {
11970:                 if ($selfenrollhash{'validation'}{$item}) { 
11971:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
11972:                 } else {
11973:                     $changes{'validation'}{$item} = &mt('None');
11974:                 }
11975:             }
11976:         } else {
11977:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
11978:             if ($item eq 'markup') {
11979:                if ($env{'form.selfenroll_validation_'.$item}) {
11980:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
11981:                }
11982:             }
11983:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
11984:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
11985:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
11986:                 }
11987:             }
11988:         }
11989:     }
11990: 
11991:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
11992:                                              $dom);
11993:     if ($putresult eq 'ok') {
11994:         if (keys(%changes) > 0) {
11995:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11996:             $resulttext = &mt('Changes made:').'<ul>';
11997:             foreach my $key ('admin','default','validation') {
11998:                 if (ref($changes{$key}) eq 'HASH') {
11999:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
12000:                     if ($key eq 'validation') {
12001:                         foreach my $item (@{$itemsref}) {
12002:                             if (exists($changes{$key}{$item})) {
12003:                                 if ($item eq 'markup') {
12004:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
12005:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
12006:                                 } else {  
12007:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
12008:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
12009:                                 }
12010:                             }
12011:                         }
12012:                     } else {
12013:                         foreach my $type (@types) {
12014:                             if ($type eq 'community') {
12015:                                 $roles{'1'} = &mt('Community personnel');
12016:                             } else {
12017:                                 $roles{'1'} = &mt('Course personnel');
12018:                             }
12019:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
12020:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
12021:                                     if ($key eq 'admin') {
12022:                                         my @mgrdc = ();
12023:                                         if (ref($ordered{$key}) eq 'ARRAY') {
12024:                                             foreach my $item (@{$ordered{'admin'}}) {
12025:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
12026:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
12027:                                                         push(@mgrdc,$item);
12028:                                                     }
12029:                                                 }
12030:                                             }
12031:                                             if (@mgrdc) {
12032:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
12033:                                             } else {
12034:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
12035:                                             }
12036:                                         }
12037:                                     } else {
12038:                                         if (ref($ordered{$key}) eq 'ARRAY') {
12039:                                             foreach my $item (@{$ordered{$key}}) {
12040:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
12041:                                                     $domdefaults{$type.'selfenroll'.$item} =
12042:                                                         $selfenrollhash{$key}{$type}{$item};
12043:                                                 }
12044:                                             }
12045:                                         }
12046:                                     }
12047:                                 }
12048:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
12049:                                 foreach my $item (@{$ordered{$key}}) {
12050:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
12051:                                         $resulttext .= '<li>';
12052:                                         if ($key eq 'admin') {
12053:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
12054:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
12055:                                         } else {
12056:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
12057:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
12058:                                         }
12059:                                         $resulttext .= '</li>';
12060:                                     }
12061:                                 }
12062:                                 $resulttext .= '</ul></li>';
12063:                             }
12064:                         }
12065:                         $resulttext .= '</ul></li>'; 
12066:                     }
12067:                 }
12068:                 if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
12069:                     my $cachetime = 24*60*60;
12070:                     &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12071:                     if (ref($lastactref) eq 'HASH') {
12072:                         $lastactref->{'domdefaults'} = 1;
12073:                     }
12074:                 }
12075:             }
12076:             $resulttext .= '</ul>';
12077:         } else {
12078:             $resulttext = &mt('No changes made to self-enrollment settings');
12079:         }
12080:     } else {
12081:         $resulttext = '<span class="LC_error">'.
12082:             &mt('An error occurred: [_1]',$putresult).'</span>';
12083:     }
12084:     return $resulttext;
12085: }
12086: 
12087: sub modify_usersessions {
12088:     my ($dom,$lastactref,%domconfig) = @_;
12089:     my @hostingtypes = ('version','excludedomain','includedomain');
12090:     my @offloadtypes = ('primary','default');
12091:     my %types = (
12092:                   remote => \@hostingtypes,
12093:                   hosted => \@hostingtypes,
12094:                   spares => \@offloadtypes,
12095:                 );
12096:     my @prefixes = ('remote','hosted','spares');
12097:     my @lcversions = &Apache::lonnet::all_loncaparevs();
12098:     my (%by_ip,%by_location,@intdoms);
12099:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
12100:     my @locations = sort(keys(%by_location));
12101:     my (%defaultshash,%changes);
12102:     foreach my $prefix (@prefixes) {
12103:         $defaultshash{'usersessions'}{$prefix} = {};
12104:     }
12105:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12106:     my $resulttext;
12107:     my %iphost = &Apache::lonnet::get_iphost();
12108:     foreach my $prefix (@prefixes) {
12109:         next if ($prefix eq 'spares');
12110:         foreach my $type (@{$types{$prefix}}) {
12111:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
12112:             if ($type eq 'version') {
12113:                 my $value = $env{'form.'.$prefix.'_'.$type};
12114:                 my $okvalue;
12115:                 if ($value ne '') {
12116:                     if (grep(/^\Q$value\E$/,@lcversions)) {
12117:                         $okvalue = $value;
12118:                     }
12119:                 }
12120:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
12121:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
12122:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
12123:                             if ($inuse == 0) {
12124:                                 $changes{$prefix}{$type} = 1;
12125:                             } else {
12126:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
12127:                                     $changes{$prefix}{$type} = 1;
12128:                                 }
12129:                                 if ($okvalue ne '') {
12130:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
12131:                                 } 
12132:                             }
12133:                         } else {
12134:                             if (($inuse == 1) && ($okvalue ne '')) {
12135:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
12136:                                 $changes{$prefix}{$type} = 1;
12137:                             }
12138:                         }
12139:                     } else {
12140:                         if (($inuse == 1) && ($okvalue ne '')) {
12141:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
12142:                             $changes{$prefix}{$type} = 1;
12143:                         }
12144:                     }
12145:                 } else {
12146:                     if (($inuse == 1) && ($okvalue ne '')) {
12147:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
12148:                         $changes{$prefix}{$type} = 1;
12149:                     }
12150:                 }
12151:             } else {
12152:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
12153:                 my @okvals;
12154:                 foreach my $val (@vals) {
12155:                     if ($val =~ /:/) {
12156:                         my @items = split(/:/,$val);
12157:                         foreach my $item (@items) {
12158:                             if (ref($by_location{$item}) eq 'ARRAY') {
12159:                                 push(@okvals,$item);
12160:                             }
12161:                         }
12162:                     } else {
12163:                         if (ref($by_location{$val}) eq 'ARRAY') {
12164:                             push(@okvals,$val);
12165:                         }
12166:                     }
12167:                 }
12168:                 @okvals = sort(@okvals);
12169:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
12170:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
12171:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
12172:                             if ($inuse == 0) {
12173:                                 $changes{$prefix}{$type} = 1; 
12174:                             } else {
12175:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
12176:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
12177:                                 if (@changed > 0) {
12178:                                     $changes{$prefix}{$type} = 1;
12179:                                 }
12180:                             }
12181:                         } else {
12182:                             if ($inuse == 1) {
12183:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
12184:                                 $changes{$prefix}{$type} = 1;
12185:                             }
12186:                         } 
12187:                     } else {
12188:                         if ($inuse == 1) {
12189:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
12190:                             $changes{$prefix}{$type} = 1;
12191:                         }
12192:                     }
12193:                 } else {
12194:                     if ($inuse == 1) {
12195:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
12196:                         $changes{$prefix}{$type} = 1;
12197:                     }
12198:                 }
12199:             }
12200:         }
12201:     }
12202: 
12203:     my @alldoms = &Apache::lonnet::all_domains();
12204:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
12205:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
12206:     my $savespares;
12207: 
12208:     foreach my $lonhost (sort(keys(%servers))) {
12209:         my $serverhomeID =
12210:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
12211:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
12212:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
12213:         my %spareschg;
12214:         foreach my $type (@{$types{'spares'}}) {
12215:             my @okspares;
12216:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
12217:             foreach my $server (@checked) {
12218:                 if (&Apache::lonnet::hostname($server) ne '') {
12219:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
12220:                         unless (grep(/^\Q$server\E$/,@okspares)) {
12221:                             push(@okspares,$server);
12222:                         }
12223:                     }
12224:                 }
12225:             }
12226:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
12227:             my $newspare;
12228:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
12229:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
12230:                     $newspare = $new;
12231:                 }
12232:             }
12233:             my @spares;
12234:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
12235:                 @spares = sort(@okspares,$newspare);
12236:             } else {
12237:                 @spares = sort(@okspares);
12238:             }
12239:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
12240:             if (ref($spareid{$lonhost}) eq 'HASH') {
12241:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
12242:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
12243:                     if (@diffs > 0) {
12244:                         $spareschg{$type} = 1;
12245:                     }
12246:                 }
12247:             }
12248:         }
12249:         if (keys(%spareschg) > 0) {
12250:             $changes{'spares'}{$lonhost} = \%spareschg;
12251:         }
12252:     }
12253:     $defaultshash{'usersessions'}{'offloadnow'} = {};
12254:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
12255:     my @okoffload;
12256:     if (@offloadnow) {
12257:         foreach my $server (@offloadnow) {
12258:             if (&Apache::lonnet::hostname($server) ne '') {
12259:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
12260:                     push(@okoffload,$server);
12261:                 }
12262:             }
12263:         }
12264:         if (@okoffload) {
12265:             foreach my $lonhost (@okoffload) {
12266:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
12267:             }
12268:         }
12269:     }
12270:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
12271:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
12272:             if (ref($changes{'spares'}) eq 'HASH') {
12273:                 if (keys(%{$changes{'spares'}}) > 0) {
12274:                     $savespares = 1;
12275:                 }
12276:             }
12277:         } else {
12278:             $savespares = 1;
12279:         }
12280:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
12281:             foreach my $lonhost (keys(%{$domconfig{'usersessions'}{'offloadnow'}})) {
12282:                 unless ($defaultshash{'usersessions'}{'offloadnow'}{$lonhost}) {
12283:                     $changes{'offloadnow'} = 1;
12284:                     last;
12285:                 }
12286:             }
12287:             unless ($changes{'offloadnow'}) {
12288:                 foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{'offloadnow'}})) {
12289:                     unless ($domconfig{'usersessions'}{'offloadnow'}{$lonhost}) {
12290:                         $changes{'offloadnow'} = 1;
12291:                         last;
12292:                     }
12293:                 }
12294:             }
12295:         } elsif (@okoffload) {
12296:             $changes{'offloadnow'} = 1;
12297:         }
12298:     } elsif (@okoffload) {
12299:         $changes{'offloadnow'} = 1;
12300:     }
12301:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
12302:     if ((keys(%changes) > 0) || ($savespares)) {
12303:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
12304:                                                  $dom);
12305:         if ($putresult eq 'ok') {
12306:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
12307:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
12308:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
12309:                 }
12310:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
12311:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
12312:                 }
12313:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
12314:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
12315:                 }
12316:             }
12317:             my $cachetime = 24*60*60;
12318:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12319:             if (ref($lastactref) eq 'HASH') {
12320:                 $lastactref->{'domdefaults'} = 1;
12321:             }
12322:             if (keys(%changes) > 0) {
12323:                 my %lt = &usersession_titles();
12324:                 $resulttext = &mt('Changes made:').'<ul>';
12325:                 foreach my $prefix (@prefixes) {
12326:                     if (ref($changes{$prefix}) eq 'HASH') {
12327:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
12328:                         if ($prefix eq 'spares') {
12329:                             if (ref($changes{$prefix}) eq 'HASH') {
12330:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
12331:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
12332:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
12333:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
12334:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
12335:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
12336:                                         foreach my $type (@{$types{$prefix}}) {
12337:                                             if ($changes{$prefix}{$lonhost}{$type}) {
12338:                                                 my $offloadto = &mt('None');
12339:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
12340:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
12341:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
12342:                                                     }
12343:                                                 }
12344:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
12345:                                             }
12346:                                         }
12347:                                     }
12348:                                     $resulttext .= '</li>';
12349:                                 }
12350:                             }
12351:                         } else {
12352:                             foreach my $type (@{$types{$prefix}}) {
12353:                                 if (defined($changes{$prefix}{$type})) {
12354:                                     my $newvalue;
12355:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
12356:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
12357:                                             if ($type eq 'version') {
12358:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
12359:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
12360:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
12361:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
12362:                                                 }
12363:                                             }
12364:                                         }
12365:                                     }
12366:                                     if ($newvalue eq '') {
12367:                                         if ($type eq 'version') {
12368:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
12369:                                         } else {
12370:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
12371:                                         }
12372:                                     } else {
12373:                                         if ($type eq 'version') {
12374:                                             $newvalue .= ' '.&mt('(or later)'); 
12375:                                         }
12376:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
12377:                                     }
12378:                                 }
12379:                             }
12380:                         }
12381:                         $resulttext .= '</ul>';
12382:                     }
12383:                 }
12384:                 if ($changes{'offloadnow'}) {
12385:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
12386:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
12387:                             $resulttext .= '<li>'.&mt('Switch active users on next access, for server(s):').'<ul>';
12388:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
12389:                                 $resulttext .= '<li>'.$lonhost.'</li>';
12390:                             }
12391:                             $resulttext .= '</ul>';
12392:                         } else {
12393:                             $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.');
12394:                         }
12395:                     } else {
12396:                         $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.').'</li>';
12397:                     }
12398:                 }
12399:                 $resulttext .= '</ul>';
12400:             } else {
12401:                 $resulttext = $nochgmsg;
12402:             }
12403:         } else {
12404:             $resulttext = '<span class="LC_error">'.
12405:                           &mt('An error occurred: [_1]',$putresult).'</span>';
12406:         }
12407:     } else {
12408:         $resulttext = $nochgmsg;
12409:     }
12410:     return $resulttext;
12411: }
12412: 
12413: sub modify_loadbalancing {
12414:     my ($dom,%domconfig) = @_;
12415:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
12416:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
12417:     my ($othertitle,$usertypes,$types) =
12418:         &Apache::loncommon::sorted_inst_types($dom);
12419:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
12420:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
12421:     my @sparestypes = ('primary','default');
12422:     my %typetitles = &sparestype_titles();
12423:     my $resulttext;
12424:     my (%currbalancer,%currtargets,%currrules,%existing);
12425:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
12426:         %existing = %{$domconfig{'loadbalancing'}};
12427:     }
12428:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
12429:                               \%currtargets,\%currrules);
12430:     my ($saveloadbalancing,%defaultshash,%changes);
12431:     my ($alltypes,$othertypes,$titles) =
12432:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
12433:     my %ruletitles = &offloadtype_text();
12434:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
12435:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
12436:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
12437:         if ($balancer eq '') {
12438:             next;
12439:         }
12440:         if (!exists($servers{$balancer})) {
12441:             if (exists($currbalancer{$balancer})) {
12442:                 push(@{$changes{'delete'}},$balancer);
12443:             }
12444:             next;
12445:         }
12446:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
12447:             push(@{$changes{'delete'}},$balancer);
12448:             next;
12449:         }
12450:         if (!exists($currbalancer{$balancer})) {
12451:             push(@{$changes{'add'}},$balancer);
12452:         }
12453:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
12454:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
12455:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
12456:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
12457:             $saveloadbalancing = 1;
12458:         }
12459:         foreach my $sparetype (@sparestypes) {
12460:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
12461:             my @offloadto;
12462:             foreach my $target (@targets) {
12463:                 if (($servers{$target}) && ($target ne $balancer)) {
12464:                     if ($sparetype eq 'default') {
12465:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
12466:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
12467:                         }
12468:                     }
12469:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
12470:                         push(@offloadto,$target);
12471:                     }
12472:                 }
12473:             }
12474:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
12475:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
12476:                     push(@offloadto,$balancer);
12477:                 }
12478:             }
12479:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
12480:         }
12481:         if (ref($currtargets{$balancer}) eq 'HASH') {
12482:             foreach my $sparetype (@sparestypes) {
12483:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
12484:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
12485:                     if (@targetdiffs > 0) {
12486:                         $changes{'curr'}{$balancer}{'targets'} = 1;
12487:                     }
12488:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
12489:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
12490:                         $changes{'curr'}{$balancer}{'targets'} = 1;
12491:                     }
12492:                 }
12493:             }
12494:         } else {
12495:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
12496:                 foreach my $sparetype (@sparestypes) {
12497:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
12498:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
12499:                             $changes{'curr'}{$balancer}{'targets'} = 1;
12500:                         }
12501:                     }
12502:                 }
12503:             }
12504:         }
12505:         my $ishomedom;
12506:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
12507:             $ishomedom = 1;
12508:         }
12509:         if (ref($alltypes) eq 'ARRAY') {
12510:             foreach my $type (@{$alltypes}) {
12511:                 my $rule;
12512:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
12513:                          (!$ishomedom)) {
12514:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
12515:                 }
12516:                 if ($rule eq 'specific') {
12517:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
12518:                     if (exists($servers{$specifiedhost})) {
12519:                         $rule = $specifiedhost;
12520:                     }
12521:                 }
12522:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
12523:                 if (ref($currrules{$balancer}) eq 'HASH') {
12524:                     if ($rule ne $currrules{$balancer}{$type}) {
12525:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
12526:                     }
12527:                 } elsif ($rule ne '') {
12528:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
12529:                 }
12530:             }
12531:         }
12532:     }
12533:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
12534:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
12535:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
12536:             $defaultshash{'loadbalancing'} = {};
12537:         }
12538:         my $putresult = &Apache::lonnet::put_dom('configuration',
12539:                                                  \%defaultshash,$dom);
12540:         if ($putresult eq 'ok') {
12541:             if (keys(%changes) > 0) {
12542:                 my %toupdate;
12543:                 if (ref($changes{'delete'}) eq 'ARRAY') {
12544:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
12545:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
12546:                         $toupdate{$balancer} = 1;
12547:                     }
12548:                 }
12549:                 if (ref($changes{'add'}) eq 'ARRAY') {
12550:                     foreach my $balancer (sort(@{$changes{'add'}})) {
12551:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
12552:                         $toupdate{$balancer} = 1;
12553:                     }
12554:                 }
12555:                 if (ref($changes{'curr'}) eq 'HASH') {
12556:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
12557:                         $toupdate{$balancer} = 1;
12558:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
12559:                             if ($changes{'curr'}{$balancer}{'targets'}) {
12560:                                 my %offloadstr;
12561:                                 foreach my $sparetype (@sparestypes) {
12562:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
12563:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
12564:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
12565:                                         }
12566:                                     }
12567:                                 }
12568:                                 if (keys(%offloadstr) == 0) {
12569:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
12570:                                 } else {
12571:                                     my $showoffload;
12572:                                     foreach my $sparetype (@sparestypes) {
12573:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
12574:                                         if (defined($offloadstr{$sparetype})) {
12575:                                             $showoffload .= $offloadstr{$sparetype};
12576:                                         } else {
12577:                                             $showoffload .= &mt('None');
12578:                                         }
12579:                                         $showoffload .= ('&nbsp;'x3);
12580:                                     }
12581:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
12582:                                 }
12583:                             }
12584:                         }
12585:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
12586:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
12587:                                 foreach my $type (@{$alltypes}) {
12588:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
12589:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
12590:                                         my $balancetext;
12591:                                         if ($rule eq '') {
12592:                                             $balancetext =  $ruletitles{'default'};
12593:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
12594:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
12595:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
12596:                                                 foreach my $sparetype (@sparestypes) {
12597:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
12598:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
12599:                                                     }
12600:                                                 }
12601:                                                 foreach my $item (@{$alltypes}) {
12602:                                                     next if ($item =~  /^_LC_ipchange/);
12603:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
12604:                                                     if ($hasrule eq 'homeserver') {
12605:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
12606:                                                     } else {
12607:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
12608:                                                             if ($servers{$hasrule}) {
12609:                                                                 $toupdate{$hasrule} = 1;
12610:                                                             }
12611:                                                         }
12612:                                                     }
12613:                                                 }
12614:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
12615:                                                     $balancetext =  $ruletitles{$rule};
12616:                                                 } else {
12617:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
12618:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
12619:                                                     if ($receiver) {
12620:                                                         $toupdate{$receiver};
12621:                                                     }
12622:                                                 }
12623:                                             } else {
12624:                                                 $balancetext =  $ruletitles{$rule};
12625:                                             }
12626:                                         } else {
12627:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
12628:                                         }
12629:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
12630:                                     }
12631:                                 }
12632:                             }
12633:                         }
12634:                         if (keys(%toupdate)) {
12635:                             my %thismachine;
12636:                             my $updatedhere;
12637:                             my $cachetime = 60*60*24;
12638:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
12639:                             foreach my $lonhost (keys(%toupdate)) {
12640:                                 if ($thismachine{$lonhost}) {
12641:                                     unless ($updatedhere) {
12642:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
12643:                                                                       $defaultshash{'loadbalancing'},
12644:                                                                       $cachetime);
12645:                                         $updatedhere = 1;
12646:                                     }
12647:                                 } else {
12648:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
12649:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
12650:                                 }
12651:                             }
12652:                         }
12653:                     }
12654:                 }
12655:                 if ($resulttext ne '') {
12656:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
12657:                 } else {
12658:                     $resulttext = $nochgmsg;
12659:                 }
12660:             } else {
12661:                 $resulttext = $nochgmsg;
12662:             }
12663:         } else {
12664:             $resulttext = '<span class="LC_error">'.
12665:                           &mt('An error occurred: [_1]',$putresult).'</span>';
12666:         }
12667:     } else {
12668:         $resulttext = $nochgmsg;
12669:     }
12670:     return $resulttext;
12671: }
12672: 
12673: sub recurse_check {
12674:     my ($chkcats,$categories,$depth,$name) = @_;
12675:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
12676:         my $chg = 0;
12677:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
12678:             my $category = $chkcats->[$depth]{$name}[$j];
12679:             my $item;
12680:             if ($category eq '') {
12681:                 $chg ++;
12682:             } else {
12683:                 my $deeper = $depth + 1;
12684:                 $item = &escape($category).':'.&escape($name).':'.$depth;
12685:                 if ($chg) {
12686:                     $categories->{$item} -= $chg;
12687:                 }
12688:                 &recurse_check($chkcats,$categories,$deeper,$category);
12689:                 $deeper --;
12690:             }
12691:         }
12692:     }
12693:     return;
12694: }
12695: 
12696: sub recurse_cat_deletes {
12697:     my ($item,$coursecategories,$deletions) = @_;
12698:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
12699:     my $subdepth = $depth + 1;
12700:     if (ref($coursecategories) eq 'HASH') {
12701:         foreach my $subitem (keys(%{$coursecategories})) {
12702:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
12703:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
12704:                 delete($coursecategories->{$subitem});
12705:                 $deletions->{$subitem} = 1;
12706:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
12707:             }
12708:         }
12709:     }
12710:     return;
12711: }
12712: 
12713: sub active_dc_picker {
12714:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
12715:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
12716:     my @domcoord = keys(%domcoords);
12717:     if (keys(%currhash)) {
12718:         foreach my $dc (keys(%currhash)) {
12719:             unless (exists($domcoords{$dc})) {
12720:                 push(@domcoord,$dc);
12721:             }
12722:         }
12723:     }
12724:     @domcoord = sort(@domcoord);
12725:     my $numdcs = scalar(@domcoord);
12726:     my $rows = 0;
12727:     my $table;
12728:     if ($numdcs > 1) {
12729:         $table = '<table>';
12730:         for (my $i=0; $i<@domcoord; $i++) {
12731:             my $rem = $i%($numinrow);
12732:             if ($rem == 0) {
12733:                 if ($i > 0) {
12734:                     $table .= '</tr>';
12735:                 }
12736:                 $table .= '<tr>';
12737:                 $rows ++;
12738:             }
12739:             my $check = '';
12740:             if ($inputtype eq 'radio') {
12741:                 if (keys(%currhash) == 0) {
12742:                     if (!$i) {
12743:                         $check = ' checked="checked"';
12744:                     }
12745:                 } elsif (exists($currhash{$domcoord[$i]})) {
12746:                     $check = ' checked="checked"';
12747:                 }
12748:             } else {
12749:                 if (exists($currhash{$domcoord[$i]})) {
12750:                     $check = ' checked="checked"';
12751:                 }
12752:             }
12753:             if ($i == @domcoord - 1) {
12754:                 my $colsleft = $numinrow - $rem;
12755:                 if ($colsleft > 1) {
12756:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
12757:                 } else {
12758:                     $table .= '<td class="LC_left_item">';
12759:                 }
12760:             } else {
12761:                 $table .= '<td class="LC_left_item">';
12762:             }
12763:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
12764:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
12765:             $table .= '<span class="LC_nobreak"><label>'.
12766:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
12767:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
12768:             if ($user ne $dcname.':'.$dcdom) {
12769:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
12770:             }
12771:             $table .= '</label></span></td>';
12772:         }
12773:         $table .= '</tr></table>';
12774:     } elsif ($numdcs == 1) {
12775:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
12776:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
12777:         if ($inputtype eq 'radio') {
12778:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
12779:             if ($user ne $dcname.':'.$dcdom) {
12780:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
12781:             }
12782:         } else {
12783:             my $check;
12784:             if (exists($currhash{$domcoord[0]})) {
12785:                 $check = ' checked="checked"';
12786:             }
12787:             $table = '<span class="LC_nobreak"><label>'.
12788:                      '<input type="checkbox" name="'.$name.'" '.
12789:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
12790:             if ($user ne $dcname.':'.$dcdom) {
12791:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
12792:             }
12793:             $table .= '</label></span>';
12794:             $rows ++;
12795:         }
12796:     }
12797:     return ($numdcs,$table,$rows);
12798: }
12799: 
12800: sub usersession_titles {
12801:     return &Apache::lonlocal::texthash(
12802:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
12803:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
12804:                spares => 'Servers offloaded to, when busy',
12805:                version => 'LON-CAPA version requirement',
12806:                excludedomain => 'Allow all, but exclude specific domains',
12807:                includedomain => 'Deny all, but include specific domains',
12808:                primary => 'Primary (checked first)',
12809:                default => 'Default',
12810:            );
12811: }
12812: 
12813: sub id_for_thisdom {
12814:     my (%servers) = @_;
12815:     my %altids;
12816:     foreach my $server (keys(%servers)) {
12817:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
12818:         if ($serverhome ne $server) {
12819:             $altids{$serverhome} = $server;
12820:         }
12821:     }
12822:     return %altids;
12823: }
12824: 
12825: sub count_servers {
12826:     my ($currbalancer,%servers) = @_;
12827:     my (@spares,$numspares);
12828:     foreach my $lonhost (sort(keys(%servers))) {
12829:         next if ($currbalancer eq $lonhost);
12830:         push(@spares,$lonhost);
12831:     }
12832:     if ($currbalancer) {
12833:         $numspares = scalar(@spares);
12834:     } else {
12835:         $numspares = scalar(@spares) - 1;
12836:     }
12837:     return ($numspares,@spares);
12838: }
12839: 
12840: sub lonbalance_targets_js {
12841:     my ($dom,$types,$servers,$settings) = @_;
12842:     my $select = &mt('Select');
12843:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
12844:     if (ref($servers) eq 'HASH') {
12845:         $alltargets = join("','",sort(keys(%{$servers})));
12846:         my @homedoms;
12847:         foreach my $server (sort(keys(%{$servers}))) {
12848:             if (&Apache::lonnet::host_domain($server) eq $dom) {
12849:                 push(@homedoms,'1');
12850:             } else {
12851:                 push(@homedoms,'0');
12852:             }
12853:         }
12854:         $allishome = join("','",@homedoms);
12855:     }
12856:     if (ref($types) eq 'ARRAY') {
12857:         if (@{$types} > 0) {
12858:             @alltypes = @{$types};
12859:         }
12860:     }
12861:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
12862:     $allinsttypes = join("','",@alltypes);
12863:     my (%currbalancer,%currtargets,%currrules,%existing);
12864:     if (ref($settings) eq 'HASH') {
12865:         %existing = %{$settings};
12866:     }
12867:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
12868:                               \%currtargets,\%currrules);
12869:     my $balancers = join("','",sort(keys(%currbalancer)));
12870:     return <<"END";
12871: 
12872: <script type="text/javascript">
12873: // <![CDATA[
12874: 
12875: currBalancers = new Array('$balancers');
12876: 
12877: function toggleTargets(balnum) {
12878:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
12879:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
12880:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
12881:     var prevbalancer = prevhostitem.value;
12882:     var baltotal = document.getElementById('loadbalancing_total').value;
12883:     prevhostitem.value = balancer;
12884:     if (prevbalancer != '') {
12885:         var prevIdx = currBalancers.indexOf(prevbalancer);
12886:         if (prevIdx != -1) {
12887:             currBalancers.splice(prevIdx,1);
12888:         }
12889:     }
12890:     if (balancer == '') {
12891:         hideSpares(balnum);
12892:     } else {
12893:         var currIdx = currBalancers.indexOf(balancer);
12894:         if (currIdx == -1) {
12895:             currBalancers.push(balancer);
12896:         }
12897:         var homedoms = new Array('$allishome');
12898:         var ishomedom = homedoms[lonhostitem.selectedIndex];
12899:         showSpares(balancer,ishomedom,balnum);
12900:     }
12901:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
12902:     return;
12903: }
12904: 
12905: function showSpares(balancer,ishomedom,balnum) {
12906:     var alltargets = new Array('$alltargets');
12907:     var insttypes = new Array('$allinsttypes');
12908:     var offloadtypes = new Array('primary','default');
12909: 
12910:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
12911:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
12912:  
12913:     for (var i=0; i<offloadtypes.length; i++) {
12914:         var count = 0;
12915:         for (var j=0; j<alltargets.length; j++) {
12916:             if (alltargets[j] != balancer) {
12917:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
12918:                 item.value = alltargets[j];
12919:                 item.style.textAlign='left';
12920:                 item.style.textFace='normal';
12921:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
12922:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
12923:                     item.disabled = '';
12924:                 } else {
12925:                     item.disabled = 'disabled';
12926:                     item.checked = false;
12927:                 }
12928:                 count ++;
12929:             }
12930:         }
12931:     }
12932:     for (var k=0; k<insttypes.length; k++) {
12933:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
12934:             if (ishomedom == 1) {
12935:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
12936:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
12937:             } else {
12938:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
12939:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
12940:             }
12941:         } else {
12942:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
12943:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
12944:         }
12945:         if ((insttypes[k] != '_LC_external') && 
12946:             ((insttypes[k] != '_LC_internetdom') ||
12947:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
12948:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
12949:             item.options.length = 0;
12950:             item.options[0] = new Option("","",true,true);
12951:             var idx = 0;
12952:             for (var m=0; m<alltargets.length; m++) {
12953:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
12954:                     idx ++;
12955:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
12956:                 }
12957:             }
12958:         }
12959:     }
12960:     return;
12961: }
12962: 
12963: function hideSpares(balnum) {
12964:     var alltargets = new Array('$alltargets');
12965:     var insttypes = new Array('$allinsttypes');
12966:     var offloadtypes = new Array('primary','default');
12967: 
12968:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
12969:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
12970: 
12971:     var total = alltargets.length - 1;
12972:     for (var i=0; i<offloadtypes; i++) {
12973:         for (var j=0; j<total; j++) {
12974:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
12975:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
12976:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
12977:         }
12978:     }
12979:     for (var k=0; k<insttypes.length; k++) {
12980:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
12981:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
12982:         if (insttypes[k] != '_LC_external') {
12983:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
12984:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
12985:         }
12986:     }
12987:     return;
12988: }
12989: 
12990: function checkOffloads(item,balnum,type) {
12991:     var alltargets = new Array('$alltargets');
12992:     var offloadtypes = new Array('primary','default');
12993:     if (item.checked) {
12994:         var total = alltargets.length - 1;
12995:         var other;
12996:         if (type == offloadtypes[0]) {
12997:             other = offloadtypes[1];
12998:         } else {
12999:             other = offloadtypes[0];
13000:         }
13001:         for (var i=0; i<total; i++) {
13002:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
13003:             if (server == item.value) {
13004:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
13005:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
13006:                 }
13007:             }
13008:         }
13009:     }
13010:     return;
13011: }
13012: 
13013: function singleServerToggle(balnum,type) {
13014:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
13015:     if (offloadtoSelIdx == 0) {
13016:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
13017:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
13018: 
13019:     } else {
13020:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
13021:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
13022:     }
13023:     return;
13024: }
13025: 
13026: function balanceruleChange(formname,balnum,type) {
13027:     if (type == '_LC_external') {
13028:         return;
13029:     }
13030:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
13031:     for (var i=0; i<typesRules.length; i++) {
13032:         if (formname.elements[typesRules[i]].checked) {
13033:             if (formname.elements[typesRules[i]].value != 'specific') {
13034:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
13035:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
13036:             } else {
13037:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
13038:             }
13039:         }
13040:     }
13041:     return;
13042: }
13043: 
13044: function balancerDeleteChange(balnum) {
13045:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
13046:     var baltotal = document.getElementById('loadbalancing_total').value;
13047:     var addtarget;
13048:     var removetarget;
13049:     var action = 'delete';
13050:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
13051:         var lonhost = hostitem.value;
13052:         var currIdx = currBalancers.indexOf(lonhost);
13053:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
13054:             if (currIdx != -1) {
13055:                 currBalancers.splice(currIdx,1);
13056:             }
13057:             addtarget = lonhost;
13058:         } else {
13059:             if (currIdx == -1) {
13060:                 currBalancers.push(lonhost);
13061:             }
13062:             removetarget = lonhost;
13063:             action = 'undelete';
13064:         }
13065:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
13066:     }
13067:     return;
13068: }
13069: 
13070: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
13071:     if (baltotal > 1) {
13072:         var offloadtypes = new Array('primary','default');
13073:         var alltargets = new Array('$alltargets');
13074:         var insttypes = new Array('$allinsttypes');
13075:         for (var i=0; i<baltotal; i++) {
13076:             if (i != balnum) {
13077:                 for (var j=0; j<offloadtypes.length; j++) {
13078:                     var total = alltargets.length - 1;
13079:                     for (var k=0; k<total; k++) {
13080:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
13081:                         var server = serveritem.value;
13082:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
13083:                             if (server == addtarget) {
13084:                                 serveritem.disabled = '';
13085:                             }
13086:                         }
13087:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
13088:                             if (server == removetarget) {
13089:                                 serveritem.disabled = 'disabled';
13090:                                 serveritem.checked = false;
13091:                             }
13092:                         }
13093:                     }
13094:                 }
13095:                 for (var j=0; j<insttypes.length; j++) {
13096:                     if (insttypes[j] != '_LC_external') {
13097:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
13098:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
13099:                             var currSel = singleserver.selectedIndex;
13100:                             var currVal = singleserver.options[currSel].value;
13101:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
13102:                                 var numoptions = singleserver.options.length;
13103:                                 var needsnew = 1;
13104:                                 for (var k=0; k<numoptions; k++) {
13105:                                     if (singleserver.options[k] == addtarget) {
13106:                                         needsnew = 0;
13107:                                         break;
13108:                                     }
13109:                                 }
13110:                                 if (needsnew == 1) {
13111:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
13112:                                 }
13113:                             }
13114:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
13115:                                 singleserver.options.length = 0;
13116:                                 if ((currVal) && (currVal != removetarget)) {
13117:                                     singleserver.options[0] = new Option("","",false,false);
13118:                                 } else {
13119:                                     singleserver.options[0] = new Option("","",true,true);
13120:                                 }
13121:                                 var idx = 0;
13122:                                 for (var m=0; m<alltargets.length; m++) {
13123:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
13124:                                         idx ++;
13125:                                         if (currVal == alltargets[m]) {
13126:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
13127:                                         } else {
13128:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
13129:                                         }
13130:                                     }
13131:                                 }
13132:                             }
13133:                         }
13134:                     }
13135:                 }
13136:             }
13137:         }
13138:     }
13139:     return;
13140: }
13141: 
13142: // ]]>
13143: </script>
13144: 
13145: END
13146: }
13147: 
13148: sub new_spares_js {
13149:     my @sparestypes = ('primary','default');
13150:     my $types = join("','",@sparestypes);
13151:     my $select = &mt('Select');
13152:     return <<"END";
13153: 
13154: <script type="text/javascript">
13155: // <![CDATA[
13156: 
13157: function updateNewSpares(formname,lonhost) {
13158:     var types = new Array('$types');
13159:     var include = new Array();
13160:     var exclude = new Array();
13161:     for (var i=0; i<types.length; i++) {
13162:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
13163:         for (var j=0; j<spareboxes.length; j++) {
13164:             if (formname.elements[spareboxes[j]].checked) {
13165:                 exclude.push(formname.elements[spareboxes[j]].value);
13166:             } else {
13167:                 include.push(formname.elements[spareboxes[j]].value);
13168:             }
13169:         }
13170:     }
13171:     for (var i=0; i<types.length; i++) {
13172:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
13173:         var selIdx = newSpare.selectedIndex;
13174:         var currnew = newSpare.options[selIdx].value;
13175:         var okSpares = new Array();
13176:         for (var j=0; j<newSpare.options.length; j++) {
13177:             var possible = newSpare.options[j].value;
13178:             if (possible != '') {
13179:                 if (exclude.indexOf(possible) == -1) {
13180:                     okSpares.push(possible);
13181:                 } else {
13182:                     if (currnew == possible) {
13183:                         selIdx = 0;
13184:                     }
13185:                 }
13186:             }
13187:         }
13188:         for (var k=0; k<include.length; k++) {
13189:             if (okSpares.indexOf(include[k]) == -1) {
13190:                 okSpares.push(include[k]);
13191:             }
13192:         }
13193:         okSpares.sort();
13194:         newSpare.options.length = 0;
13195:         if (selIdx == 0) {
13196:             newSpare.options[0] = new Option("$select","",true,true);
13197:         } else {
13198:             newSpare.options[0] = new Option("$select","",false,false);
13199:         }
13200:         for (var m=0; m<okSpares.length; m++) {
13201:             var idx = m+1;
13202:             var selThis = 0;
13203:             if (selIdx != 0) {
13204:                 if (okSpares[m] == currnew) {
13205:                     selThis = 1;
13206:                 }
13207:             }
13208:             if (selThis == 1) {
13209:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
13210:             } else {
13211:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
13212:             }
13213:         }
13214:     }
13215:     return;
13216: }
13217: 
13218: function checkNewSpares(lonhost,type) {
13219:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
13220:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
13221:     if (chosen != '') { 
13222:         var othertype;
13223:         var othernewSpare;
13224:         if (type == 'primary') {
13225:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
13226:         }
13227:         if (type == 'default') {
13228:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
13229:         }
13230:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
13231:             othernewSpare.selectedIndex = 0;
13232:         }
13233:     }
13234:     return;
13235: }
13236: 
13237: // ]]>
13238: </script>
13239: 
13240: END
13241: 
13242: }
13243: 
13244: sub common_domprefs_js {
13245:     return <<"END";
13246: 
13247: <script type="text/javascript">
13248: // <![CDATA[
13249: 
13250: function getIndicesByName(formname,item) {
13251:     var group = new Array();
13252:     for (var i=0;i<formname.elements.length;i++) {
13253:         if (formname.elements[i].name == item) {
13254:             group.push(formname.elements[i].id);
13255:         }
13256:     }
13257:     return group;
13258: }
13259: 
13260: // ]]>
13261: </script>
13262: 
13263: END
13264: 
13265: }
13266: 
13267: sub recaptcha_js {
13268:     my %lt = &captcha_phrases();
13269:     return <<"END";
13270: 
13271: <script type="text/javascript">
13272: // <![CDATA[
13273: 
13274: function updateCaptcha(caller,context) {
13275:     var privitem;
13276:     var pubitem;
13277:     var privtext;
13278:     var pubtext;
13279:     var versionitem;
13280:     var versiontext;
13281:     if (document.getElementById(context+'_recaptchapub')) {
13282:         pubitem = document.getElementById(context+'_recaptchapub');
13283:     } else {
13284:         return;
13285:     }
13286:     if (document.getElementById(context+'_recaptchapriv')) {
13287:         privitem = document.getElementById(context+'_recaptchapriv');
13288:     } else {
13289:         return;
13290:     }
13291:     if (document.getElementById(context+'_recaptchapubtxt')) {
13292:         pubtext = document.getElementById(context+'_recaptchapubtxt');
13293:     } else {
13294:         return;
13295:     }
13296:     if (document.getElementById(context+'_recaptchaprivtxt')) {
13297:         privtext = document.getElementById(context+'_recaptchaprivtxt');
13298:     } else {
13299:         return;
13300:     }
13301:     if (document.getElementById(context+'_recaptchaversion')) {
13302:         versionitem = document.getElementById(context+'_recaptchaversion');
13303:     } else {
13304:         return;
13305:     }
13306:     if (document.getElementById(context+'_recaptchavertxt')) {
13307:         versiontext = document.getElementById(context+'_recaptchavertxt');
13308:     } else {
13309:         return;
13310:     }
13311:     if (caller.checked) {
13312:         if (caller.value == 'recaptcha') {
13313:             pubitem.type = 'text';
13314:             privitem.type = 'text';
13315:             pubitem.size = '40';
13316:             privitem.size = '40';
13317:             pubtext.innerHTML = "$lt{'pub'}";
13318:             privtext.innerHTML = "$lt{'priv'}";
13319:             versionitem.type = 'text';
13320:             versionitem.size = '3';
13321:             versiontext.innerHTML = "$lt{'ver'}";
13322:         } else {
13323:             pubitem.type = 'hidden';
13324:             privitem.type = 'hidden';
13325:             versionitem.type = 'hidden';
13326:             pubtext.innerHTML = '';
13327:             privtext.innerHTML = '';
13328:             versiontext.innerHTML = '';
13329:         }
13330:     }
13331:     return;
13332: }
13333: 
13334: // ]]>
13335: </script>
13336: 
13337: END
13338: 
13339: }
13340: 
13341: sub toggle_display_js {
13342:     return <<"END";
13343: 
13344: <script type="text/javascript">
13345: // <![CDATA[
13346: 
13347: function toggleDisplay(domForm,caller) {
13348:     if (document.getElementById(caller)) {
13349:         var divitem = document.getElementById(caller);
13350:         var optionsElement = domForm.coursecredits;
13351:         var checkval = 1;
13352:         var dispval = 'block';
13353:         if (caller == 'emailoptions') {
13354:             optionsElement = domForm.cancreate_email; 
13355:         }
13356:         if (caller == 'studentsubmission') {
13357:             optionsElement = domForm.postsubmit;
13358:         }
13359:         if (caller == 'cloneinstcode') {
13360:             optionsElement = domForm.canclone;
13361:             checkval = 'instcode';
13362:         }
13363:         if (optionsElement.length) {
13364:             var currval;
13365:             for (var i=0; i<optionsElement.length; i++) {
13366:                 if (optionsElement[i].checked) {
13367:                    currval = optionsElement[i].value;
13368:                 }
13369:             }
13370:             if (currval == checkval) {
13371:                 divitem.style.display = dispval;
13372:             } else {
13373:                 divitem.style.display = 'none';
13374:             }
13375:         }
13376:     }
13377:     return;
13378: }
13379: 
13380: // ]]>
13381: </script>
13382: 
13383: END
13384: 
13385: }
13386: 
13387: sub captcha_phrases {
13388:     return &Apache::lonlocal::texthash (
13389:                  priv => 'Private key',
13390:                  pub  => 'Public key',
13391:                  original  => 'original (CAPTCHA)',
13392:                  recaptcha => 'successor (ReCAPTCHA)',
13393:                  notused   => 'unused',
13394:                  ver => 'ReCAPTCHA version (1 or 2)',
13395:     );
13396: }
13397: 
13398: sub devalidate_remote_domconfs {
13399:     my ($dom,$cachekeys) = @_;
13400:     return unless (ref($cachekeys) eq 'HASH');
13401:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
13402:     my %thismachine;
13403:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
13404:     my @posscached = ('domainconfig','domdefaults');
13405:     if (keys(%servers)) {
13406:         foreach my $server (keys(%servers)) {
13407:             next if ($thismachine{$server});
13408:             my @cached;
13409:             foreach my $name (@posscached) {
13410:                 if ($cachekeys->{$name}) {
13411:                     push(@cached,&escape($name).':'.&escape($dom));
13412:                 }
13413:             }
13414:             if (@cached) {
13415:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
13416:             }
13417:         }
13418:     }
13419:     return;
13420: }
13421: 
13422: 1;

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