File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.77: download - view: text, annotated - select for diffs
Sun Jan 22 13:28:05 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.285, 1.287.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.160.6.77 2017/01/22 13:28:05 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA#
   23: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: #
   28: ###############################################################
   29: ##############################################################
   30: 
   31: =pod
   32: 
   33: =head1 NAME
   34: 
   35: Apache::domainprefs.pm
   36: 
   37: =head1 SYNOPSIS
   38: 
   39: Handles configuration of a LON-CAPA domain.  
   40: 
   41: This is part of the LearningOnline Network with CAPA project
   42: described at http://www.lon-capa.org.
   43: 
   44: 
   45: =head1 OVERVIEW
   46: 
   47: Each institution using LON-CAPA will typically have a single domain designated 
   48: for use by individuals affiliated with the institution.  Accordingly, each domain
   49: may define a default set of logos and a color scheme which can be used to "brand"
   50: the LON-CAPA instance. In addition, an institution will typically have a language
   51: and timezone which are used for the majority of courses.
   52: 
   53: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   54: host of other domain-wide settings which determine the types of functionality
   55: available to users and courses in the domain.
   56: 
   57: There is also a mechanism to configure cataloging of courses in the domain, and
   58: controls on the operation of automated processes which govern such things as
   59: roster updates, user directory updates and processing of course requests.
   60: 
   61: The domain coordination manual which is built dynamically on install/update of 
   62: LON-CAPA from the relevant help items provides more information about domain 
   63: configuration.
   64: 
   65: Most of the domain settings are stored in the configuration.db GDBM file which is
   66: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   67: where $dom is the domain.  The configuration.db stores settings in a number of 
   68: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   69: the domain as files (e.g., image files for logos etc., or plain text files for
   70: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   71: session hosted on the primary library server in the domain, as these files are 
   72: stored in author space belonging to a special $dom-domainconfig user.   
   73: 
   74: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   75: the current settings, and provides an interface to make modifications.
   76: 
   77: =head1 SUBROUTINES
   78: 
   79: =over
   80: 
   81: =item print_quotas()
   82: 
   83: Inputs: 4 
   84: 
   85: $dom,$settings,$rowtotal,$action.
   86: 
   87: $dom is the domain, $settings is a reference to a hash of current settings for
   88: the current context, $rowtotal is a reference to the scalar used to record the 
   89: number of rows displayed on the page, and $action is the context (quotas, 
   90: requestcourses or requestauthor).
   91: 
   92: The print_quotas routine was orginally created to display/store information
   93: about default quota sizes for portfolio spaces for the different types of 
   94: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   95: but is now also used to manage availability of user tools: 
   96: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   97: used by course owners to request creation of a course, and to display/store
   98: default quota sizes for Authoring Spaces.
   99: 
  100: Outputs: 1
  101: 
  102: $datatable  - HTML containing form elements which allow settings to be changed. 
  103: 
  104: In the case of course requests, radio buttons are displayed for each institutional
  105: affiliate type (and also default, and _LC_adv) for each of the course types 
  106: (official, unofficial, community, and textbook).  In each case the radio buttons 
  107: allow the selection of one of four values:
  108: 
  109: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  110: which have the following effects:
  111: 
  112: 0
  113: 
  114: =over
  115: 
  116: - course requests are not allowed for this course types/affiliation
  117: 
  118: =back
  119: 
  120: approval 
  121: 
  122: =over 
  123: 
  124: - course requests must be approved by a Doman Coordinator in the 
  125: course's domain
  126: 
  127: =back
  128: 
  129: validate 
  130: 
  131: =over
  132: 
  133: - an institutional validation (e.g., check requestor is instructor
  134: of record) needs to be passed before the course will be created.  The required
  135: validation is in localenroll.pm on the primary library server for the course 
  136: domain.
  137: 
  138: =back
  139: 
  140: autolimit 
  141: 
  142: =over
  143:  
  144: - course requests will be processed automatically up to a limit of
  145: N requests for the course type for the particular requestor.
  146: If N is undefined, there is no limit to the number of course requests
  147: which a course owner may submit and have processed automatically. 
  148: 
  149: =back
  150: 
  151: =item modify_quotas() 
  152: 
  153: =back
  154: 
  155: =cut
  156: 
  157: package Apache::domainprefs;
  158: 
  159: use strict;
  160: use Apache::Constants qw(:common :http);
  161: use Apache::lonnet;
  162: use Apache::loncommon();
  163: use Apache::lonhtmlcommon();
  164: use Apache::lonlocal;
  165: use Apache::lonmsg();
  166: use Apache::lonconfigsettings;
  167: use Apache::lonuserutils();
  168: use Apache::loncoursequeueadmin();
  169: use LONCAPA qw(:DEFAULT :match);
  170: use LONCAPA::Enrollment;
  171: use LONCAPA::lonauthcgi();
  172: use File::Copy;
  173: use Locale::Language;
  174: use DateTime::TimeZone;
  175: use DateTime::Locale;
  176: 
  177: my $registered_cleanup;
  178: my $modified_urls;
  179: 
  180: sub handler {
  181:     my $r=shift;
  182:     if ($r->header_only) {
  183:         &Apache::loncommon::content_type($r,'text/html');
  184:         $r->send_http_header;
  185:         return OK;
  186:     }
  187: 
  188:     my $context = 'domain';
  189:     my $dom = $env{'request.role.domain'};
  190:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  191:     if (&Apache::lonnet::allowed('mau',$dom)) {
  192:         &Apache::loncommon::content_type($r,'text/html');
  193:         $r->send_http_header;
  194:     } else {
  195:         $env{'user.error.msg'}=
  196:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  197:         return HTTP_NOT_ACCEPTABLE;
  198:     }
  199: 
  200:     $registered_cleanup=0;
  201:     @{$modified_urls}=();
  202: 
  203:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  204:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  205:                                             ['phase','actions']);
  206:     my $phase = 'pickactions';
  207:     if ( exists($env{'form.phase'}) ) {
  208:         $phase = $env{'form.phase'};
  209:     }
  210:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  211:     my %domconfig =
  212:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  213:                 'quotas','autoenroll','autoupdate','autocreate',
  214:                 'directorysrch','usercreation','usermodification',
  215:                 'contacts','defaults','scantron','coursecategories',
  216:                 'serverstatuses','requestcourses','helpsettings',
  217:                 'coursedefaults','usersessions','loadbalancing',
  218:                 'requestauthor','selfenrollment','inststatus'],$dom);
  219:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  220:                        'autoupdate','autocreate','directorysrch','contacts',
  221:                        'usercreation','selfcreation','usermodification','scantron',
  222:                        'requestcourses','requestauthor','coursecategories',
  223:                        'serverstatuses','helpsettings','coursedefaults',
  224:                        'selfenrollment','usersessions');
  225:     my %existing;
  226:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  227:         %existing = %{$domconfig{'loadbalancing'}};
  228:     }
  229:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  230:         push(@prefs_order,'loadbalancing');
  231:     }
  232:     my %prefs = (
  233:         'rolecolors' =>
  234:                    { text => 'Default color schemes',
  235:                      help => 'Domain_Configuration_Color_Schemes',
  236:                      header => [{col1 => 'Student Settings',
  237:                                  col2 => '',},
  238:                                 {col1 => 'Coordinator Settings',
  239:                                  col2 => '',},
  240:                                 {col1 => 'Author Settings',
  241:                                  col2 => '',},
  242:                                 {col1 => 'Administrator Settings',
  243:                                  col2 => '',}],
  244:                       print => \&print_rolecolors,
  245:                       modify => \&modify_rolecolors,
  246:                     },
  247:         'login' =>
  248:                     { text => 'Log-in page options',
  249:                       help => 'Domain_Configuration_Login_Page',
  250:                       header => [{col1 => 'Log-in Page Items',
  251:                                   col2 => '',},
  252:                                  {col1 => 'Log-in Help',
  253:                                   col2 => 'Value'},
  254:                                  {col1 => 'Custom HTML in document head',
  255:                                   col2 => 'Value'}],
  256:                       print => \&print_login,
  257:                       modify => \&modify_login,
  258:                     },
  259:         'defaults' => 
  260:                     { text => 'Default authentication/language/timezone/portal/types',
  261:                       help => 'Domain_Configuration_LangTZAuth',
  262:                       header => [{col1 => 'Setting',
  263:                                   col2 => 'Value'},
  264:                                  {col1 => 'Institutional user types',
  265:                                   col2 => 'Assignable to e-mail usernames'}],
  266:                       print => \&print_defaults,
  267:                       modify => \&modify_defaults,
  268:                     },
  269:         'quotas' => 
  270:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  271:                       help => 'Domain_Configuration_Quotas',
  272:                       header => [{col1 => 'User affiliation',
  273:                                   col2 => 'Available tools',
  274:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  275:                       print => \&print_quotas,
  276:                       modify => \&modify_quotas,
  277:                     },
  278:         'autoenroll' =>
  279:                    { text => 'Auto-enrollment settings',
  280:                      help => 'Domain_Configuration_Auto_Enrollment',
  281:                      header => [{col1 => 'Configuration setting',
  282:                                  col2 => 'Value(s)'}],
  283:                      print => \&print_autoenroll,
  284:                      modify => \&modify_autoenroll,
  285:                    },
  286:         'autoupdate' => 
  287:                    { text => 'Auto-update settings',
  288:                      help => 'Domain_Configuration_Auto_Updates',
  289:                      header => [{col1 => 'Setting',
  290:                                  col2 => 'Value',},
  291:                                 {col1 => 'Setting',
  292:                                  col2 => 'Affiliation'},
  293:                                 {col1 => 'User population',
  294:                                  col2 => 'Updatable user data'}],
  295:                      print => \&print_autoupdate,
  296:                      modify => \&modify_autoupdate,
  297:                   },
  298:         'autocreate' => 
  299:                   { text => 'Auto-course creation settings',
  300:                      help => 'Domain_Configuration_Auto_Creation',
  301:                      header => [{col1 => 'Configuration Setting',
  302:                                  col2 => 'Value',}],
  303:                      print => \&print_autocreate,
  304:                      modify => \&modify_autocreate,
  305:                   },
  306:         'directorysrch' => 
  307:                   { text => 'Directory searches',
  308:                     help => 'Domain_Configuration_InstDirectory_Search',
  309:                     header => [{col1 => 'Institutional Directory Setting',
  310:                                 col2 => 'Value',},
  311:                                {col1 => 'LON-CAPA Directory Setting',
  312:                                 col2 => 'Value',}],
  313:                     print => \&print_directorysrch,
  314:                     modify => \&modify_directorysrch,
  315:                   },
  316:         'contacts' =>
  317:                   { text => 'Contact Information',
  318:                     help => 'Domain_Configuration_Contact_Info',
  319:                     header => [{col1 => 'Setting',
  320:                                 col2 => 'Value',}],
  321:                     print => \&print_contacts,
  322:                     modify => \&modify_contacts,
  323:                   },
  324:         'usercreation' => 
  325:                   { text => 'User creation',
  326:                     help => 'Domain_Configuration_User_Creation',
  327:                     header => [{col1 => 'Format rule type',
  328:                                 col2 => 'Format rules in force'},
  329:                                {col1 => 'User account creation',
  330:                                 col2 => 'Usernames which may be created',},
  331:                                {col1 => 'Context',
  332:                                 col2 => 'Assignable authentication types'}],
  333:                     print => \&print_usercreation,
  334:                     modify => \&modify_usercreation,
  335:                   },
  336:         'selfcreation' => 
  337:                   { text => 'Users self-creating accounts',
  338:                     help => 'Domain_Configuration_Self_Creation', 
  339:                     header => [{col1 => 'Self-creation with institutional username',
  340:                                 col2 => 'Enabled?'},
  341:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  342:                                 col2 => 'Information user can enter'},
  343:                                {col1 => 'Self-creation with e-mail as username',
  344:                                 col2 => 'Settings'}],
  345:                     print => \&print_selfcreation,
  346:                     modify => \&modify_selfcreation,
  347:                   },
  348:         'usermodification' =>
  349:                   { text => 'User modification',
  350:                     help => 'Domain_Configuration_User_Modification',
  351:                     header => [{col1 => 'Target user has role',
  352:                                 col2 => 'User information updatable in author context'},
  353:                                {col1 => 'Target user has role',
  354:                                 col2 => 'User information updatable in course context'}],
  355:                     print => \&print_usermodification,
  356:                     modify => \&modify_usermodification,
  357:                   },
  358:         'scantron' =>
  359:                   { text => 'Bubblesheet format file',
  360:                     help => 'Domain_Configuration_Scantron_Format',
  361:                     header => [ {col1 => 'Item',
  362:                                  col2 => '',
  363:                               }],
  364:                     print => \&print_scantron,
  365:                     modify => \&modify_scantron,
  366:                   },
  367:         'requestcourses' => 
  368:                  {text => 'Request creation of courses',
  369:                   help => 'Domain_Configuration_Request_Courses',
  370:                   header => [{col1 => 'User affiliation',
  371:                               col2 => 'Availability/Processing of requests',},
  372:                              {col1 => 'Setting',
  373:                               col2 => 'Value'},
  374:                              {col1 => 'Available textbooks',
  375:                               col2 => ''},
  376:                              {col1 => 'Available templates',
  377:                               col2 => ''},
  378:                              {col1 => 'Validation (not official courses)',
  379:                               col2 => 'Value'},],
  380:                   print => \&print_quotas,
  381:                   modify => \&modify_quotas,
  382:                  },
  383:         'requestauthor' =>
  384:                  {text => 'Request Authoring Space',
  385:                   help => 'Domain_Configuration_Request_Author',
  386:                   header => [{col1 => 'User affiliation',
  387:                               col2 => 'Availability/Processing of requests',},
  388:                              {col1 => 'Setting',
  389:                               col2 => 'Value'}],
  390:                   print => \&print_quotas,
  391:                   modify => \&modify_quotas,
  392:                  },
  393:         'coursecategories' =>
  394:                   { text => 'Cataloging of courses/communities',
  395:                     help => 'Domain_Configuration_Cataloging_Courses',
  396:                     header => [{col1 => 'Catalog type/availability',
  397:                                 col2 => '',},
  398:                                {col1 => 'Category settings for standard catalog',
  399:                                 col2 => '',},
  400:                                {col1 => 'Categories',
  401:                                 col2 => '',
  402:                                }],
  403:                     print => \&print_coursecategories,
  404:                     modify => \&modify_coursecategories,
  405:                   },
  406:         'serverstatuses' =>
  407:                  {text   => 'Access to server status pages',
  408:                   help   => 'Domain_Configuration_Server_Status',
  409:                   header => [{col1 => 'Status Page',
  410:                               col2 => 'Other named users',
  411:                               col3 => 'Specific IPs',
  412:                             }],
  413:                   print => \&print_serverstatuses,
  414:                   modify => \&modify_serverstatuses,
  415:                  },
  416:         'helpsettings' =>
  417:                  {text   => 'Support settings',
  418:                   help   => 'Domain_Configuration_Help_Settings',
  419:                   header => [{col1 => 'Help Page Settings (logged-in users)',
  420:                               col2 => 'Value'},
  421:                              {col1 => 'Helpdesk Roles',
  422:                               col2 => 'Settings'},],
  423:                   print  => \&print_helpsettings,
  424:                   modify => \&modify_helpsettings,
  425:                  },
  426:         'coursedefaults' => 
  427:                  {text => 'Course/Community defaults',
  428:                   help => 'Domain_Configuration_Course_Defaults',
  429:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  430:                               col2 => 'Value',},
  431:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  432:                               col2 => 'Value',},],
  433:                   print => \&print_coursedefaults,
  434:                   modify => \&modify_coursedefaults,
  435:                  },
  436:         'selfenrollment' => 
  437:                  {text   => 'Self-enrollment in Course/Community',
  438:                   help   => 'Domain_Configuration_Selfenrollment',
  439:                   header => [{col1 => 'Configuration Rights',
  440:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  441:                              {col1 => 'Defaults',
  442:                               col2 => 'Value'},
  443:                              {col1 => 'Self-enrollment validation (optional)',
  444:                               col2 => 'Value'},],
  445:                   print => \&print_selfenrollment,
  446:                   modify => \&modify_selfenrollment,
  447:                  },
  448:         'usersessions' =>
  449:                  {text  => 'User session hosting/offloading',
  450:                   help  => 'Domain_Configuration_User_Sessions',
  451:                   header => [{col1 => 'Domain server',
  452:                               col2 => 'Servers to offload sessions to when busy'},
  453:                              {col1 => 'Hosting of users from other domains',
  454:                               col2 => 'Rules'},
  455:                              {col1 => "Hosting domain's own users elsewhere",
  456:                               col2 => 'Rules'}],
  457:                   print => \&print_usersessions,
  458:                   modify => \&modify_usersessions,
  459:                  },
  460:          'loadbalancing' =>
  461:                  {text  => 'Dedicated Load Balancer(s)',
  462:                   help  => 'Domain_Configuration_Load_Balancing',
  463:                   header => [{col1 => 'Balancers',
  464:                               col2 => 'Default destinations',
  465:                               col3 => 'User affiliation',
  466:                               col4 => 'Overrides'},
  467:                             ],
  468:                   print => \&print_loadbalancing,
  469:                   modify => \&modify_loadbalancing,
  470:                  },
  471:     );
  472:     if (keys(%servers) > 1) {
  473:         $prefs{'login'}  = { text   => 'Log-in page options',
  474:                              help   => 'Domain_Configuration_Login_Page',
  475:                             header => [{col1 => 'Log-in Service',
  476:                                         col2 => 'Server Setting',},
  477:                                        {col1 => 'Log-in Page Items',
  478:                                         col2 => ''},
  479:                                        {col1 => 'Log-in Help',
  480:                                         col2 => 'Value'},
  481:                                        {col1 => 'Custom HTML in document head',
  482:                                         col2 => 'Value'}],
  483:                             print => \&print_login,
  484:                             modify => \&modify_login,
  485:                            };
  486:     }
  487: 
  488:     my @roles = ('student','coordinator','author','admin');
  489:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  490:     &Apache::lonhtmlcommon::add_breadcrumb
  491:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  492:       text=>"Settings to display/modify"});
  493:     my $confname = $dom.'-domainconfig';
  494: 
  495:     if ($phase eq 'process') {
  496:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  497:                                                               \%prefs,\%domconfig,$confname,\@roles);
  498:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  499:             $r->rflush();
  500:             &devalidate_remote_domconfs($dom,$result);
  501:         }
  502:     } elsif ($phase eq 'display') {
  503:         my $js = &recaptcha_js().
  504:                  &toggle_display_js();
  505:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  506:             my ($othertitle,$usertypes,$types) =
  507:                 &Apache::loncommon::sorted_inst_types($dom);
  508:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  509:                                           $domconfig{'loadbalancing'}).
  510:                    &new_spares_js().
  511:                    &common_domprefs_js().
  512:                    &Apache::loncommon::javascript_array_indexof();
  513:         }
  514:         if (grep(/^requestcourses$/,@actions)) {
  515:             my $javascript_validations;
  516:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  517:             $js .= <<END;
  518: <script type="text/javascript">
  519: $javascript_validations
  520: </script>
  521: $coursebrowserjs
  522: END
  523:         }
  524:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  525:     } else {
  526: # check if domconfig user exists for the domain.
  527:         my $servadm = $r->dir_config('lonAdmEMail');
  528:         my ($configuserok,$author_ok,$switchserver) =
  529:             &config_check($dom,$confname,$servadm);
  530:         unless ($configuserok eq 'ok') {
  531:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  532:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  533:                           $confname).
  534:                       '<br />'
  535:             );
  536:             if ($switchserver) {
  537:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  538:                           '<br />'.
  539:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  540:                           '<br />'.
  541:                           &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).
  542:                           '<br />'.
  543:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  544:                 );
  545:             } else {
  546:                 $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.').
  547:                           '<br />'.
  548:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  549:                 );
  550:             }
  551:             $r->print(&Apache::loncommon::end_page());
  552:             return OK;
  553:         }
  554:         if (keys(%domconfig) == 0) {
  555:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  556:             my @ids=&Apache::lonnet::current_machine_ids();
  557:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  558:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  559:                 my @loginimages = ('img','logo','domlogo','login');
  560:                 my $custom_img_count = 0;
  561:                 foreach my $img (@loginimages) {
  562:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  563:                         $custom_img_count ++;
  564:                     }
  565:                 }
  566:                 foreach my $role (@roles) {
  567:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  568:                         $custom_img_count ++;
  569:                     }
  570:                 }
  571:                 if ($custom_img_count > 0) {
  572:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  573:                     my $switch_server = &check_switchserver($dom,$confname);
  574:                     $r->print(
  575:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  576:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  577:     &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 />'.
  578:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  579:                     if ($switch_server) {
  580:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  581:                     }
  582:                     $r->print(&Apache::loncommon::end_page());
  583:                     return OK;
  584:                 }
  585:             }
  586:         }
  587:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  588:     }
  589:     return OK;
  590: }
  591: 
  592: sub process_changes {
  593:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  594:     my %domconfig;
  595:     if (ref($values) eq 'HASH') {
  596:         %domconfig = %{$values};
  597:     }
  598:     my $output;
  599:     if ($action eq 'login') {
  600:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  601:     } elsif ($action eq 'rolecolors') {
  602:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  603:                                      $lastactref,%domconfig);
  604:     } elsif ($action eq 'quotas') {
  605:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  606:     } elsif ($action eq 'autoenroll') {
  607:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  608:     } elsif ($action eq 'autoupdate') {
  609:         $output = &modify_autoupdate($dom,%domconfig);
  610:     } elsif ($action eq 'autocreate') {
  611:         $output = &modify_autocreate($dom,%domconfig);
  612:     } elsif ($action eq 'directorysrch') {
  613:         $output = &modify_directorysrch($dom,%domconfig);
  614:     } elsif ($action eq 'usercreation') {
  615:         $output = &modify_usercreation($dom,%domconfig);
  616:     } elsif ($action eq 'selfcreation') {
  617:         $output = &modify_selfcreation($dom,%domconfig);
  618:     } elsif ($action eq 'usermodification') {
  619:         $output = &modify_usermodification($dom,%domconfig);
  620:     } elsif ($action eq 'contacts') {
  621:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  622:     } elsif ($action eq 'defaults') {
  623:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  624:     } elsif ($action eq 'scantron') {
  625:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  626:     } elsif ($action eq 'coursecategories') {
  627:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  628:     } elsif ($action eq 'serverstatuses') {
  629:         $output = &modify_serverstatuses($dom,%domconfig);
  630:     } elsif ($action eq 'requestcourses') {
  631:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  632:     } elsif ($action eq 'requestauthor') {
  633:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  634:     } elsif ($action eq 'helpsettings') {
  635:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  636:     } elsif ($action eq 'coursedefaults') {
  637:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  638:     } elsif ($action eq 'selfenrollment') {
  639:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  640:     } elsif ($action eq 'usersessions') {
  641:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  642:     } elsif ($action eq 'loadbalancing') {
  643:         $output = &modify_loadbalancing($dom,%domconfig);
  644:     }
  645:     return $output;
  646: }
  647: 
  648: sub print_config_box {
  649:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  650:     my $rowtotal = 0;
  651:     my $output;
  652:     if ($action eq 'coursecategories') {
  653:         $output = &coursecategories_javascript($settings);
  654:     } elsif ($action eq 'defaults') {
  655:         $output = &defaults_javascript($settings); 
  656:     } elsif ($action eq 'helpsettings') {
  657:         my (%privs,%levelscurrent);
  658:         my %full=();
  659:         my %levels=(
  660:                      course => {},
  661:                      domain => {},
  662:                      system => {},
  663:                    );
  664:         my $context = 'domain';
  665:         my $crstype = 'Course';
  666:         my $formname = 'display';
  667:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  668:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  669:         $output =
  670:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full,
  671:                                                       \@templateroles);
  672:     }
  673:     $output .=
  674:          '<table class="LC_nested_outer">
  675:           <tr>
  676:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  677:            &mt($item->{text}).'&nbsp;'.
  678:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  679:           '</tr>';
  680:     $rowtotal ++;
  681:     my $numheaders = 1;
  682:     if (ref($item->{'header'}) eq 'ARRAY') {
  683:         $numheaders = scalar(@{$item->{'header'}});
  684:     }
  685:     if ($numheaders > 1) {
  686:         my $colspan = '';
  687:         my $rightcolspan = '';
  688:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  689:             ($action eq 'directorysrch') ||
  690:             (($action eq 'login') && ($numheaders < 4))) {
  691:             $colspan = ' colspan="2"';
  692:         }
  693:         if ($action eq 'usersessions') {
  694:             $rightcolspan = ' colspan="3"'; 
  695:         }
  696:         $output .= '
  697:           <tr>
  698:            <td>
  699:             <table class="LC_nested">
  700:              <tr class="LC_info_row">
  701:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  702:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  703:              </tr>';
  704:         $rowtotal ++;
  705:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  706:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  707:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'directorysrch') ||
  708:             ($action eq 'helpsettings')) {
  709:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  710:         } elsif ($action eq 'coursecategories') {
  711:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  712:         } elsif ($action eq 'login') {
  713:             if ($numheaders == 4) {
  714:                 $colspan = ' colspan="2"';
  715:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  716:             } else {
  717:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  718:             }
  719:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  720:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  721:         } elsif ($action eq 'rolecolors') {
  722:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  723:         }
  724:         $output .= '
  725:            </table>
  726:           </td>
  727:          </tr>
  728:          <tr>
  729:            <td>
  730:             <table class="LC_nested">
  731:              <tr class="LC_info_row">
  732:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  733:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  734:              </tr>';
  735:             $rowtotal ++;
  736:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  737:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  738:             ($action eq 'usersessions') || ($action eq 'coursecategories')) {
  739:             if ($action eq 'coursecategories') {
  740:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  741:                 $colspan = ' colspan="2"';
  742:             } else {
  743:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  744:             }
  745:             $output .= '
  746:            </table>
  747:           </td>
  748:          </tr>
  749:          <tr>
  750:            <td>
  751:             <table class="LC_nested">
  752:              <tr class="LC_info_row">
  753:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  754:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  755:              </tr>'."\n";
  756:             if ($action eq 'coursecategories') {
  757:                 $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  758:             } else {
  759:                 $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  760:             }
  761:             $rowtotal ++;
  762:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  763:                  ($action eq 'defaults') || ($action eq 'directorysrch') ||
  764:                  ($action eq 'helpsettings')) {
  765:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  766:         } elsif ($action eq 'login') {
  767:             if ($numheaders == 4) {
  768:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  769:            </table>
  770:           </td>
  771:          </tr>
  772:          <tr>
  773:            <td>
  774:             <table class="LC_nested">
  775:              <tr class="LC_info_row">
  776:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  777:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  778:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  779:                 $rowtotal ++;
  780:             } else {
  781:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  782:             }
  783:             $output .= '
  784:            </table>
  785:           </td>
  786:          </tr>
  787:          <tr>
  788:            <td>
  789:             <table class="LC_nested">
  790:              <tr class="LC_info_row">';
  791:             if ($numheaders == 4) {
  792:                 $output .= '
  793:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  794:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  795:              </tr>';
  796:             } else {
  797:                 $output .= '
  798:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  799:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  800:              </tr>';
  801:             }
  802:             $rowtotal ++;
  803:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal);
  804:         } elsif ($action eq 'requestcourses') {
  805:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  806:             $rowtotal ++;
  807:             $output .= &print_studentcode($settings,\$rowtotal).'
  808:            </table>
  809:           </td>
  810:          </tr>
  811:          <tr>
  812:            <td>
  813:             <table class="LC_nested">
  814:              <tr class="LC_info_row">
  815:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  816:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  817:                        &textbookcourses_javascript($settings).
  818:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
  819:             </table>
  820:            </td>
  821:           </tr>
  822:          <tr>
  823:            <td>
  824:             <table class="LC_nested">
  825:              <tr class="LC_info_row">
  826:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  827:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
  828:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
  829:             </table>
  830:            </td>
  831:           </tr>
  832:           <tr>
  833:            <td>
  834:             <table class="LC_nested">
  835:              <tr class="LC_info_row">
  836:               <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
  837:               <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
  838:              </tr>'.
  839:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
  840:         } elsif ($action eq 'requestauthor') {
  841:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  842:             $rowtotal ++;
  843:         } elsif ($action eq 'rolecolors') {
  844:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  845:            </table>
  846:           </td>
  847:          </tr>
  848:          <tr>
  849:            <td>
  850:             <table class="LC_nested">
  851:              <tr class="LC_info_row">
  852:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  853:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  854:               <td class="LC_right_item" valign="top">'.
  855:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  856:              </tr>'.
  857:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  858:            </table>
  859:           </td>
  860:          </tr>
  861:          <tr>
  862:            <td>
  863:             <table class="LC_nested">
  864:              <tr class="LC_info_row">
  865:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  866:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  867:              </tr>'.
  868:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  869:             $rowtotal += 2;
  870:         }
  871:     } else {
  872:         $output .= '
  873:           <tr>
  874:            <td>
  875:             <table class="LC_nested">
  876:              <tr class="LC_info_row">';
  877:         if ($action eq 'login') {
  878:             $output .= '  
  879:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  880:         } elsif ($action eq 'serverstatuses') {
  881:             $output .= '
  882:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  883:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  884: 
  885:         } else {
  886:             $output .= '
  887:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  888:         }
  889:         if (defined($item->{'header'}->[0]->{'col3'})) {
  890:             $output .= '<td class="LC_left_item" valign="top">'.
  891:                        &mt($item->{'header'}->[0]->{'col2'});
  892:             if ($action eq 'serverstatuses') {
  893:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  894:             } 
  895:         } else {
  896:             $output .= '<td class="LC_right_item" valign="top">'.
  897:                        &mt($item->{'header'}->[0]->{'col2'});
  898:         }
  899:         $output .= '</td>';
  900:         if ($item->{'header'}->[0]->{'col3'}) {
  901:             if (defined($item->{'header'}->[0]->{'col4'})) {
  902:                 $output .= '<td class="LC_left_item" valign="top">'.
  903:                             &mt($item->{'header'}->[0]->{'col3'});
  904:             } else {
  905:                 $output .= '<td class="LC_right_item" valign="top">'.
  906:                            &mt($item->{'header'}->[0]->{'col3'});
  907:             }
  908:             if ($action eq 'serverstatuses') {
  909:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  910:             }
  911:             $output .= '</td>';
  912:         }
  913:         if ($item->{'header'}->[0]->{'col4'}) {
  914:             $output .= '<td class="LC_right_item" valign="top">'.
  915:                        &mt($item->{'header'}->[0]->{'col4'});
  916:         }
  917:         $output .= '</tr>';
  918:         $rowtotal ++;
  919:         if ($action eq 'quotas') {
  920:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  921:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
  922:                  ($action eq 'contacts') || ($action eq 'serverstatuses') ||
  923:                  ($action eq 'loadbalancing')) {
  924:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
  925:         } elsif ($action eq 'scantron') {
  926:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  927:         }
  928:     }
  929:     $output .= '
  930:    </table>
  931:   </td>
  932:  </tr>
  933: </table><br />';
  934:     return ($output,$rowtotal);
  935: }
  936: 
  937: sub print_login {
  938:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  939:     my ($css_class,$datatable);
  940:     my %choices = &login_choices();
  941: 
  942:     if ($caller eq 'service') {
  943:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  944:         my $choice = $choices{'disallowlogin'};
  945:         $css_class = ' class="LC_odd_row"';
  946:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  947:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  948:                       '<th>'.$choices{'server'}.'</th>'.
  949:                       '<th>'.$choices{'serverpath'}.'</th>'.
  950:                       '<th>'.$choices{'custompath'}.'</th>'.
  951:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  952:         my %disallowed;
  953:         if (ref($settings) eq 'HASH') {
  954:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  955:                %disallowed = %{$settings->{'loginvia'}};
  956:             }
  957:         }
  958:         foreach my $lonhost (sort(keys(%servers))) {
  959:             my $direct = 'selected="selected"';
  960:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  961:                 if ($disallowed{$lonhost}{'server'} ne '') {
  962:                     $direct = '';
  963:                 }
  964:             }
  965:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  966:                           '<td><select name="'.$lonhost.'_server">'.
  967:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  968:                           '</option>';
  969:             foreach my $hostid (sort(keys(%servers))) {
  970:                 next if ($servers{$hostid} eq $servers{$lonhost});
  971:                 my $selected = '';
  972:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  973:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  974:                         $selected = 'selected="selected"';
  975:                     }
  976:                 }
  977:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  978:                               $servers{$hostid}.'</option>';
  979:             }
  980:             $datatable .= '</select></td>'.
  981:                           '<td><select name="'.$lonhost.'_serverpath">';
  982:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  983:                 my $pathname = $path;
  984:                 if ($path eq 'custom') {
  985:                     $pathname = &mt('Custom Path').' ->';
  986:                 }
  987:                 my $selected = '';
  988:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  989:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  990:                         $selected = 'selected="selected"';
  991:                     }
  992:                 } elsif ($path eq '') {
  993:                     $selected = 'selected="selected"';
  994:                 }
  995:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  996:             }
  997:             $datatable .= '</select></td>';
  998:             my ($custom,$exempt);
  999:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1000:                 $custom = $disallowed{$lonhost}{'custompath'};
 1001:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1002:             }
 1003:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1004:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1005:                           '</tr>';
 1006:         }
 1007:         $datatable .= '</table></td></tr>';
 1008:         return $datatable;
 1009:     } elsif ($caller eq 'page') {
 1010:         my %defaultchecked = ( 
 1011:                                'coursecatalog' => 'on',
 1012:                                'helpdesk'      => 'on',
 1013:                                'adminmail'     => 'off',
 1014:                                'newuser'       => 'off',
 1015:                              );
 1016:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1017:         my (%checkedon,%checkedoff);
 1018:         foreach my $item (@toggles) {
 1019:             if ($defaultchecked{$item} eq 'on') { 
 1020:                 $checkedon{$item} = ' checked="checked" ';
 1021:                 $checkedoff{$item} = ' ';
 1022:             } elsif ($defaultchecked{$item} eq 'off') {
 1023:                 $checkedoff{$item} = ' checked="checked" ';
 1024:                 $checkedon{$item} = ' ';
 1025:             }
 1026:         }
 1027:         my @images = ('img','logo','domlogo','login');
 1028:         my @logintext = ('textcol','bgcol');
 1029:         my @bgs = ('pgbg','mainbg','sidebg');
 1030:         my @links = ('link','alink','vlink');
 1031:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1032:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1033:         my (%is_custom,%designs);
 1034:         my %defaults = (
 1035:                        font => $defaultdesign{'login.font'},
 1036:                        );
 1037:         foreach my $item (@images) {
 1038:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1039:             $defaults{'showlogo'}{$item} = 1;
 1040:         }
 1041:         foreach my $item (@bgs) {
 1042:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1043:         }
 1044:         foreach my $item (@logintext) {
 1045:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1046:         }
 1047:         foreach my $item (@links) {
 1048:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1049:         }
 1050:         if (ref($settings) eq 'HASH') {
 1051:             foreach my $item (@toggles) {
 1052:                 if ($settings->{$item} eq '1') {
 1053:                     $checkedon{$item} =  ' checked="checked" ';
 1054:                     $checkedoff{$item} = ' ';
 1055:                 } elsif ($settings->{$item} eq '0') {
 1056:                     $checkedoff{$item} =  ' checked="checked" ';
 1057:                     $checkedon{$item} = ' ';
 1058:                 }
 1059:             }
 1060:             foreach my $item (@images) {
 1061:                 if (defined($settings->{$item})) {
 1062:                     $designs{$item} = $settings->{$item};
 1063:                     $is_custom{$item} = 1;
 1064:                 }
 1065:                 if (defined($settings->{'showlogo'}{$item})) {
 1066:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1067:                 }
 1068:             }
 1069:             foreach my $item (@logintext) {
 1070:                 if ($settings->{$item} ne '') {
 1071:                     $designs{'logintext'}{$item} = $settings->{$item};
 1072:                     $is_custom{$item} = 1;
 1073:                 }
 1074:             }
 1075:             if ($settings->{'font'} ne '') {
 1076:                 $designs{'font'} = $settings->{'font'};
 1077:                 $is_custom{'font'} = 1;
 1078:             }
 1079:             foreach my $item (@bgs) {
 1080:                 if ($settings->{$item} ne '') {
 1081:                     $designs{'bgs'}{$item} = $settings->{$item};
 1082:                     $is_custom{$item} = 1;
 1083:                 }
 1084:             }
 1085:             foreach my $item (@links) {
 1086:                 if ($settings->{$item} ne '') {
 1087:                     $designs{'links'}{$item} = $settings->{$item};
 1088:                     $is_custom{$item} = 1;
 1089:                 }
 1090:             }
 1091:         } else {
 1092:             if ($designhash{$dom.'.login.font'} ne '') {
 1093:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1094:                 $is_custom{'font'} = 1;
 1095:             }
 1096:             foreach my $item (@images) {
 1097:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1098:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1099:                     $is_custom{$item} = 1;
 1100:                 }
 1101:             }
 1102:             foreach my $item (@bgs) {
 1103:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1104:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1105:                     $is_custom{$item} = 1;
 1106:                 }
 1107:             }
 1108:             foreach my $item (@links) {
 1109:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1110:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1111:                     $is_custom{$item} = 1;
 1112:                 }
 1113:             }
 1114:         }
 1115:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1116:                                                       logo => 'Institution Logo',
 1117:                                                       domlogo => 'Domain Logo',
 1118:                                                       login => 'Login box');
 1119:         my $itemcount = 1;
 1120:         foreach my $item (@toggles) {
 1121:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1122:             $datatable .=  
 1123:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1124:                 '</td><td>'.
 1125:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1126:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1127:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1128:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1129:                 '</tr>';
 1130:             $itemcount ++;
 1131:         }
 1132:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1133:         $datatable .= '</tr></table></td></tr>';
 1134:     } elsif ($caller eq 'help') {
 1135:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1136:         my $switchserver = &check_switchserver($dom,$confname);
 1137:         my $itemcount = 1;
 1138:         $defaulturl = '/adm/loginproblems.html';
 1139:         $defaulttype = 'default';
 1140:         %lt = &Apache::lonlocal::texthash (
 1141:                      del     => 'Delete?',
 1142:                      rep     => 'Replace:',
 1143:                      upl     => 'Upload:',
 1144:                      default => 'Default',
 1145:                      custom  => 'Custom',
 1146:                                              );
 1147:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1148:         my @currlangs;
 1149:         if (ref($settings) eq 'HASH') {
 1150:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1151:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1152:                     next if ($settings->{'helpurl'}{$key} eq '');
 1153:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1154:                     $type{$key} = 'custom';
 1155:                     unless ($key eq 'nolang') {
 1156:                         push(@currlangs,$key);
 1157:                     }
 1158:                 }
 1159:             } elsif ($settings->{'helpurl'} ne '') {
 1160:                 $type{'nolang'} = 'custom';
 1161:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1162:             }
 1163:         }
 1164:         foreach my $lang ('nolang',sort(@currlangs)) {
 1165:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1166:             $datatable .= '<tr'.$css_class.'>';
 1167:             if ($url{$lang} eq '') {
 1168:                 $url{$lang} = $defaulturl;
 1169:             }
 1170:             if ($type{$lang} eq '') {
 1171:                 $type{$lang} = $defaulttype;
 1172:             }
 1173:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1174:             if ($lang eq 'nolang') {
 1175:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1176:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1177:             } else {
 1178:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1179:                                   $langchoices{$lang},
 1180:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1181:             }
 1182:             $datatable .= '</span></td>'."\n".
 1183:                           '<td class="LC_left_item">';
 1184:             if ($type{$lang} eq 'custom') {
 1185:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1186:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1187:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1188:             } else {
 1189:                 $datatable .= $lt{'upl'};
 1190:             }
 1191:             $datatable .='<br />';
 1192:             if ($switchserver) {
 1193:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1194:             } else {
 1195:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1196:             }
 1197:             $datatable .= '</td></tr>';
 1198:             $itemcount ++;
 1199:         }
 1200:         my @addlangs;
 1201:         foreach my $lang (sort(keys(%langchoices))) {
 1202:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1203:             push(@addlangs,$lang);
 1204:         }
 1205:         if (@addlangs > 0) {
 1206:             my %toadd;
 1207:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1208:             $toadd{''} = &mt('Select');
 1209:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1210:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1211:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1212:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1213:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1214:             if ($switchserver) {
 1215:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1216:             } else {
 1217:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1218:             }
 1219:             $datatable .= '</td></tr>';
 1220:             $itemcount ++;
 1221:         }
 1222:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1223:     } elsif ($caller eq 'headtag') {
 1224:         my %domservers = &Apache::lonnet::get_servers($dom);
 1225:         my $choice = $choices{'headtag'};
 1226:         $css_class = ' class="LC_odd_row"';
 1227:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1228:                       '<td align="left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1229:                       '<th>'.$choices{'current'}.'</th>'.
 1230:                       '<th>'.$choices{'action'}.'</th>'.
 1231:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1232:         my (%currurls,%currexempt);
 1233:         if (ref($settings) eq 'HASH') {
 1234:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1235:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1236:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1237:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1238:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1239:                     }
 1240:                 }
 1241:             }
 1242:         }
 1243:         my %lt = &Apache::lonlocal::texthash(
 1244:                                                del  => 'Delete?',
 1245:                                                rep  => 'Replace:',
 1246:                                                upl  => 'Upload:',
 1247:                                                curr => 'View contents',
 1248:                                                none => 'None',
 1249:         );
 1250:         my $switchserver = &check_switchserver($dom,$confname);
 1251:         foreach my $lonhost (sort(keys(%domservers))) {
 1252:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1253:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1254:             if ($currurls{$lonhost}) {
 1255:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1256:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1257:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1258:                               '">'.$lt{'curr'}.'</a></td>'.
 1259:                               '<td><span class="LC_nobreak"><label>'.
 1260:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1261:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1262:             } else {
 1263:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1264:             }
 1265:             $datatable .='<br />';
 1266:             if ($switchserver) {
 1267:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1268:             } else {
 1269:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1270:             }
 1271:             $datatable .= '</td><td><input type="textbox" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1272:         }
 1273:         $datatable .= '</table></td></tr>';
 1274:     }
 1275:     return $datatable;
 1276: }
 1277: 
 1278: sub login_choices {
 1279:     my %choices =
 1280:         &Apache::lonlocal::texthash (
 1281:             coursecatalog => 'Display Course/Community Catalog link?',
 1282:             adminmail     => "Display Administrator's E-mail Address?",
 1283:             helpdesk      => 'Display "Contact Helpdesk" link',
 1284:             disallowlogin => "Login page requests redirected",
 1285:             hostid        => "Server",
 1286:             server        => "Redirect to:",
 1287:             serverpath    => "Path",
 1288:             custompath    => "Custom", 
 1289:             exempt        => "Exempt IP(s)",
 1290:             directlogin   => "No redirect",
 1291:             newuser       => "Link to create a user account",
 1292:             img           => "Header",
 1293:             logo          => "Main Logo",
 1294:             domlogo       => "Domain Logo",
 1295:             login         => "Log-in Header", 
 1296:             textcol       => "Text color",
 1297:             bgcol         => "Box color",
 1298:             bgs           => "Background colors",
 1299:             links         => "Link colors",
 1300:             font          => "Font color",
 1301:             pgbg          => "Header",
 1302:             mainbg        => "Page",
 1303:             sidebg        => "Login box",
 1304:             link          => "Link",
 1305:             alink         => "Active link",
 1306:             vlink         => "Visited link",
 1307:             headtag       => "Custom markup",
 1308:             action        => "Action",
 1309:             current       => "Current",
 1310:         );
 1311:     return %choices;
 1312: }
 1313: 
 1314: sub print_rolecolors {
 1315:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1316:     my %choices = &color_font_choices();
 1317:     my @bgs = ('pgbg','tabbg','sidebg');
 1318:     my @links = ('link','alink','vlink');
 1319:     my @images = ('img');
 1320:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1321:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1322:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1323:     my (%is_custom,%designs);
 1324:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1325:     if (ref($settings) eq 'HASH') {
 1326:         if (ref($settings->{$role}) eq 'HASH') {
 1327:             if ($settings->{$role}->{'img'} ne '') {
 1328:                 $designs{'img'} = $settings->{$role}->{'img'};
 1329:                 $is_custom{'img'} = 1;
 1330:             }
 1331:             if ($settings->{$role}->{'font'} ne '') {
 1332:                 $designs{'font'} = $settings->{$role}->{'font'};
 1333:                 $is_custom{'font'} = 1;
 1334:             }
 1335:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1336:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1337:                 $is_custom{'fontmenu'} = 1;
 1338:             }
 1339:             foreach my $item (@bgs) {
 1340:                 if ($settings->{$role}->{$item} ne '') {
 1341:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1342:                     $is_custom{$item} = 1;
 1343:                 }
 1344:             }
 1345:             foreach my $item (@links) {
 1346:                 if ($settings->{$role}->{$item} ne '') {
 1347:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1348:                     $is_custom{$item} = 1;
 1349:                 }
 1350:             }
 1351:         }
 1352:     } else {
 1353:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1354:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1355:             $is_custom{'img'} = 1;
 1356:         }
 1357:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1358:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1359:             $is_custom{'fontmenu'} = 1; 
 1360:         }
 1361:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1362:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1363:             $is_custom{'font'} = 1;
 1364:         }
 1365:         foreach my $item (@bgs) {
 1366:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1367:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1368:                 $is_custom{$item} = 1;
 1369:             
 1370:             }
 1371:         }
 1372:         foreach my $item (@links) {
 1373:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1374:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1375:                 $is_custom{$item} = 1;
 1376:             }
 1377:         }
 1378:     }
 1379:     my $itemcount = 1;
 1380:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1381:     $datatable .= '</tr></table></td></tr>';
 1382:     return $datatable;
 1383: }
 1384: 
 1385: sub role_defaults {
 1386:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1387:     my %defaults;
 1388:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1389:         return %defaults;
 1390:     }
 1391:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1392:     if ($role eq 'login') {
 1393:         %defaults = (
 1394:                        font => $defaultdesign{$role.'.font'},
 1395:                     );
 1396:         if (ref($logintext) eq 'ARRAY') {
 1397:             foreach my $item (@{$logintext}) {
 1398:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1399:             }
 1400:         }
 1401:         foreach my $item (@{$images}) {
 1402:             $defaults{'showlogo'}{$item} = 1;
 1403:         }
 1404:     } else {
 1405:         %defaults = (
 1406:                        img => $defaultdesign{$role.'.img'},
 1407:                        font => $defaultdesign{$role.'.font'},
 1408:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1409:                     );
 1410:     }
 1411:     foreach my $item (@{$bgs}) {
 1412:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1413:     }
 1414:     foreach my $item (@{$links}) {
 1415:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1416:     }
 1417:     foreach my $item (@{$images}) {
 1418:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1419:     }
 1420:     return %defaults;
 1421: }
 1422: 
 1423: sub display_color_options {
 1424:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1425:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1426:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1427:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1428:     my $datatable = '<tr'.$css_class.'>'.
 1429:         '<td>'.$choices->{'font'}.'</td>';
 1430:     if (!$is_custom->{'font'}) {
 1431:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1432:     } else {
 1433:         $datatable .= '<td>&nbsp;</td>';
 1434:     }
 1435:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1436: 
 1437:     $datatable .= '<td><span class="LC_nobreak">'.
 1438:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1439:                   ' value="'.$current_color.'" />&nbsp;'.
 1440:                   '&nbsp;</td></tr>';
 1441:     unless ($role eq 'login') { 
 1442:         $datatable .= '<tr'.$css_class.'>'.
 1443:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1444:         if (!$is_custom->{'fontmenu'}) {
 1445:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1446:         } else {
 1447:             $datatable .= '<td>&nbsp;</td>';
 1448:         }
 1449: 	$current_color = $designs->{'fontmenu'} ?
 1450: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1451:         $datatable .= '<td><span class="LC_nobreak">'.
 1452:                       '<input class="colorchooser" type="text" size="10" name="'
 1453: 		      .$role.'_fontmenu"'.
 1454:                       ' value="'.$current_color.'" />&nbsp;'.
 1455:                       '&nbsp;</td></tr>';
 1456:     }
 1457:     my $switchserver = &check_switchserver($dom,$confname);
 1458:     foreach my $img (@{$images}) {
 1459: 	$itemcount ++;
 1460:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1461:         $datatable .= '<tr'.$css_class.'>'.
 1462:                       '<td>'.$choices->{$img};
 1463:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1464:         if ($role eq 'login') {
 1465:             if ($img eq 'login') {
 1466:                 $login_hdr_pick =
 1467:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1468:                 $logincolors =
 1469:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1470:                                        $designs,$defaults);
 1471:             } elsif ($img ne 'domlogo') {
 1472:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1473:             }
 1474:         }
 1475:         $datatable .= '</td>';
 1476:         if ($designs->{$img} ne '') {
 1477:             $imgfile = $designs->{$img};
 1478: 	    $img_import = ($imgfile =~ m{^/adm/});
 1479:         } else {
 1480:             $imgfile = $defaults->{$img};
 1481:         }
 1482:         if ($imgfile) {
 1483:             my ($showfile,$fullsize);
 1484:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1485:                 my $urldir = $1;
 1486:                 my $filename = $2;
 1487:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1488:                 if (@info) {
 1489:                     my $thumbfile = 'tn-'.$filename;
 1490:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1491:                     if (@thumb) {
 1492:                         $showfile = $urldir.'/'.$thumbfile;
 1493:                     } else {
 1494:                         $showfile = $imgfile;
 1495:                     }
 1496:                 } else {
 1497:                     $showfile = '';
 1498:                 }
 1499:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1500:                 $showfile = $imgfile;
 1501:                 my $imgdir = $1;
 1502:                 my $filename = $2;
 1503:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1504:                     $showfile = "/$imgdir/tn-".$filename;
 1505:                 } else {
 1506:                     my $input = $londocroot.$imgfile;
 1507:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1508:                     if (!-e $output) {
 1509:                         my ($width,$height) = &thumb_dimensions();
 1510:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1511:                         if ($fullwidth ne '' && $fullheight ne '') {
 1512:                             if ($fullwidth > $width && $fullheight > $height) { 
 1513:                                 my $size = $width.'x'.$height;
 1514:                                 system("convert -sample $size $input $output");
 1515:                                 $showfile = "/$imgdir/tn-".$filename;
 1516:                             }
 1517:                         }
 1518:                     }
 1519:                 }
 1520:             }
 1521:             if ($showfile) {
 1522:                 if ($showfile =~ m{^/(adm|res)/}) {
 1523:                     if ($showfile =~ m{^/res/}) {
 1524:                         my $local_showfile =
 1525:                             &Apache::lonnet::filelocation('',$showfile);
 1526:                         &Apache::lonnet::repcopy($local_showfile);
 1527:                     }
 1528:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1529:                 }
 1530:                 if ($imgfile) {
 1531:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1532:                         if ($imgfile =~ m{^/res/}) {
 1533:                             my $local_imgfile =
 1534:                                 &Apache::lonnet::filelocation('',$imgfile);
 1535:                             &Apache::lonnet::repcopy($local_imgfile);
 1536:                         }
 1537:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1538:                     } else {
 1539:                         $fullsize = $imgfile;
 1540:                     }
 1541:                 }
 1542:                 $datatable .= '<td>';
 1543:                 if ($img eq 'login') {
 1544:                     $datatable .= $login_hdr_pick;
 1545:                 } 
 1546:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1547:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1548:             } else {
 1549:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1550:                               &mt('Upload:').'<br />';
 1551:             }
 1552:         } else {
 1553:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1554:                           &mt('Upload:').'<br />';
 1555:         }
 1556:         if ($switchserver) {
 1557:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1558:         } else {
 1559:             if ($img ne 'login') { # suppress file selection for Log-in header
 1560:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1561:             }
 1562:         }
 1563:         $datatable .= '</td></tr>';
 1564:     }
 1565:     $itemcount ++;
 1566:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1567:     $datatable .= '<tr'.$css_class.'>'.
 1568:                   '<td>'.$choices->{'bgs'}.'</td>';
 1569:     my $bgs_def;
 1570:     foreach my $item (@{$bgs}) {
 1571:         if (!$is_custom->{$item}) {
 1572:             $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>';
 1573:         }
 1574:     }
 1575:     if ($bgs_def) {
 1576:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1577:     } else {
 1578:         $datatable .= '<td>&nbsp;</td>';
 1579:     }
 1580:     $datatable .= '<td class="LC_right_item">'.
 1581:                   '<table border="0"><tr>';
 1582: 
 1583:     foreach my $item (@{$bgs}) {
 1584:         $datatable .= '<td align="center">'.$choices->{$item};
 1585: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1586:         if ($designs->{'bgs'}{$item}) {
 1587:             $datatable .= '&nbsp;';
 1588:         }
 1589:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1590:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1591:     }
 1592:     $datatable .= '</tr></table></td></tr>';
 1593:     $itemcount ++;
 1594:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1595:     $datatable .= '<tr'.$css_class.'>'.
 1596:                   '<td>'.$choices->{'links'}.'</td>';
 1597:     my $links_def;
 1598:     foreach my $item (@{$links}) {
 1599:         if (!$is_custom->{$item}) {
 1600:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1601:         }
 1602:     }
 1603:     if ($links_def) {
 1604:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1605:     } else {
 1606:         $datatable .= '<td>&nbsp;</td>';
 1607:     }
 1608:     $datatable .= '<td class="LC_right_item">'.
 1609:                   '<table border="0"><tr>';
 1610:     foreach my $item (@{$links}) {
 1611: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1612:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1613:         if ($designs->{'links'}{$item}) {
 1614:             $datatable.='&nbsp;';
 1615:         }
 1616:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1617:                       '" /></td>';
 1618:     }
 1619:     $$rowtotal += $itemcount;
 1620:     return $datatable;
 1621: }
 1622: 
 1623: sub logo_display_options {
 1624:     my ($img,$defaults,$designs) = @_;
 1625:     my $checkedon;
 1626:     if (ref($defaults) eq 'HASH') {
 1627:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1628:             if ($defaults->{'showlogo'}{$img}) {
 1629:                 $checkedon = 'checked="checked" ';     
 1630:             }
 1631:         } 
 1632:     }
 1633:     if (ref($designs) eq 'HASH') {
 1634:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1635:             if (defined($designs->{'showlogo'}{$img})) {
 1636:                 if ($designs->{'showlogo'}{$img} == 0) {
 1637:                     $checkedon = '';
 1638:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1639:                     $checkedon = 'checked="checked" ';
 1640:                 }
 1641:             }
 1642:         }
 1643:     }
 1644:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1645:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1646:            &mt('show').'</label>'."\n";
 1647: }
 1648: 
 1649: sub login_header_options  {
 1650:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1651:     my $output = '';
 1652:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1653:         $output .= &mt('Text default(s):').'<br />';
 1654:         if (!$is_custom->{'textcol'}) {
 1655:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1656:                        '&nbsp;&nbsp;&nbsp;';
 1657:         }
 1658:         if (!$is_custom->{'bgcol'}) {
 1659:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1660:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1661:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1662:         }
 1663:         $output .= '<br />';
 1664:     }
 1665:     $output .='<br />';
 1666:     return $output;
 1667: }
 1668: 
 1669: sub login_text_colors {
 1670:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1671:     my $color_menu = '<table border="0"><tr>';
 1672:     foreach my $item (@{$logintext}) {
 1673:         $color_menu .= '<td align="center">'.$choices->{$item};
 1674:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1675:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1676:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1677:     }
 1678:     $color_menu .= '</tr></table><br />';
 1679:     return $color_menu;
 1680: }
 1681: 
 1682: sub image_changes {
 1683:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1684:     my $output;
 1685:     if ($img eq 'login') {
 1686:             # suppress image for Log-in header
 1687:     } elsif (!$is_custom) {
 1688:         if ($img ne 'domlogo') {
 1689:             $output .= &mt('Default image:').'<br />';
 1690:         } else {
 1691:             $output .= &mt('Default in use:').'<br />';
 1692:         }
 1693:     }
 1694:     if ($img eq 'login') { # suppress image for Log-in header
 1695:         $output .= '<td>'.$logincolors;
 1696:     } else {
 1697:         if ($img_import) {
 1698:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1699:         }
 1700:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1701:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1702:         if ($is_custom) {
 1703:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1704:                        '<input type="checkbox" name="'.
 1705:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1706:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1707:         } else {
 1708:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1709:         }
 1710:     }
 1711:     return $output;
 1712: }
 1713: 
 1714: sub print_quotas {
 1715:     my ($dom,$settings,$rowtotal,$action) = @_;
 1716:     my $context;
 1717:     if ($action eq 'quotas') {
 1718:         $context = 'tools';
 1719:     } else {
 1720:         $context = $action;
 1721:     }
 1722:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1723:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1724:     my $typecount = 0;
 1725:     my ($css_class,%titles);
 1726:     if ($context eq 'requestcourses') {
 1727:         @usertools = ('official','unofficial','community','textbook');
 1728:         @options =('norequest','approval','validate','autolimit');
 1729:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1730:         %titles = &courserequest_titles();
 1731:     } elsif ($context eq 'requestauthor') {
 1732:         @usertools = ('author');
 1733:         @options = ('norequest','approval','automatic');
 1734:         %titles = &authorrequest_titles();
 1735:     } else {
 1736:         @usertools = ('aboutme','blog','webdav','portfolio');
 1737:         %titles = &tool_titles();
 1738:     }
 1739:     if (ref($types) eq 'ARRAY') {
 1740:         foreach my $type (@{$types}) {
 1741:             my ($currdefquota,$currauthorquota);
 1742:             unless (($context eq 'requestcourses') ||
 1743:                     ($context eq 'requestauthor')) {
 1744:                 if (ref($settings) eq 'HASH') {
 1745:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1746:                         $currdefquota = $settings->{defaultquota}->{$type};
 1747:                     } else {
 1748:                         $currdefquota = $settings->{$type};
 1749:                     }
 1750:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1751:                         $currauthorquota = $settings->{authorquota}->{$type};
 1752:                     }
 1753:                 }
 1754:             }
 1755:             if (defined($usertypes->{$type})) {
 1756:                 $typecount ++;
 1757:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1758:                 $datatable .= '<tr'.$css_class.'>'.
 1759:                               '<td>'.$usertypes->{$type}.'</td>'.
 1760:                               '<td class="LC_left_item">';
 1761:                 if ($context eq 'requestcourses') {
 1762:                     $datatable .= '<table><tr>';
 1763:                 }
 1764:                 my %cell;  
 1765:                 foreach my $item (@usertools) {
 1766:                     if ($context eq 'requestcourses') {
 1767:                         my ($curroption,$currlimit);
 1768:                         if (ref($settings) eq 'HASH') {
 1769:                             if (ref($settings->{$item}) eq 'HASH') {
 1770:                                 $curroption = $settings->{$item}->{$type};
 1771:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1772:                                     $currlimit = $1; 
 1773:                                 }
 1774:                             }
 1775:                         }
 1776:                         if (!$curroption) {
 1777:                             $curroption = 'norequest';
 1778:                         }
 1779:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1780:                         foreach my $option (@options) {
 1781:                             my $val = $option;
 1782:                             if ($option eq 'norequest') {
 1783:                                 $val = 0;  
 1784:                             }
 1785:                             if ($option eq 'validate') {
 1786:                                 my $canvalidate = 0;
 1787:                                 if (ref($validations{$item}) eq 'HASH') { 
 1788:                                     if ($validations{$item}{$type}) {
 1789:                                         $canvalidate = 1;
 1790:                                     }
 1791:                                 }
 1792:                                 next if (!$canvalidate);
 1793:                             }
 1794:                             my $checked = '';
 1795:                             if ($option eq $curroption) {
 1796:                                 $checked = ' checked="checked"';
 1797:                             } elsif ($option eq 'autolimit') {
 1798:                                 if ($curroption =~ /^autolimit/) {
 1799:                                     $checked = ' checked="checked"';
 1800:                                 }                       
 1801:                             } 
 1802:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1803:                                   '<input type="radio" name="crsreq_'.$item.
 1804:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1805:                                   $titles{$option}.'</label>';
 1806:                             if ($option eq 'autolimit') {
 1807:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1808:                                                 $item.'_limit_'.$type.'" size="1" '.
 1809:                                                 'value="'.$currlimit.'" />';
 1810:                             }
 1811:                             $cell{$item} .= '</span> ';
 1812:                             if ($option eq 'autolimit') {
 1813:                                 $cell{$item} .= $titles{'unlimited'};
 1814:                             }
 1815:                         }
 1816:                     } elsif ($context eq 'requestauthor') {
 1817:                         my $curroption;
 1818:                         if (ref($settings) eq 'HASH') {
 1819:                             $curroption = $settings->{$type};
 1820:                         }
 1821:                         if (!$curroption) {
 1822:                             $curroption = 'norequest';
 1823:                         }
 1824:                         foreach my $option (@options) {
 1825:                             my $val = $option;
 1826:                             if ($option eq 'norequest') {
 1827:                                 $val = 0;
 1828:                             }
 1829:                             my $checked = '';
 1830:                             if ($option eq $curroption) {
 1831:                                 $checked = ' checked="checked"';
 1832:                             }
 1833:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1834:                                   '<input type="radio" name="authorreq_'.$type.
 1835:                                   '" value="'.$val.'"'.$checked.' />'.
 1836:                                   $titles{$option}.'</label></span>&nbsp; ';
 1837:                         }
 1838:                     } else {
 1839:                         my $checked = 'checked="checked" ';
 1840:                         if (ref($settings) eq 'HASH') {
 1841:                             if (ref($settings->{$item}) eq 'HASH') {
 1842:                                 if ($settings->{$item}->{$type} == 0) {
 1843:                                     $checked = '';
 1844:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1845:                                     $checked =  'checked="checked" ';
 1846:                                 }
 1847:                             }
 1848:                         }
 1849:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1850:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1851:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1852:                                       '</label></span>&nbsp; ';
 1853:                     }
 1854:                 }
 1855:                 if ($context eq 'requestcourses') {
 1856:                     $datatable .= '</tr><tr>';
 1857:                     foreach my $item (@usertools) {
 1858:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1859:                     }
 1860:                     $datatable .= '</tr></table>';
 1861:                 }
 1862:                 $datatable .= '</td>';
 1863:                 unless (($context eq 'requestcourses') ||
 1864:                         ($context eq 'requestauthor')) {
 1865:                     $datatable .= 
 1866:                               '<td class="LC_right_item">'.
 1867:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1868:                               '<input type="text" name="quota_'.$type.
 1869:                               '" value="'.$currdefquota.
 1870:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1871:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1872:                               '<input type="text" name="authorquota_'.$type.
 1873:                               '" value="'.$currauthorquota.
 1874:                               '" size="5" /></span></td>';
 1875:                 }
 1876:                 $datatable .= '</tr>';
 1877:             }
 1878:         }
 1879:     }
 1880:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1881:         $defaultquota = '20';
 1882:         $authorquota = '500';
 1883:         if (ref($settings) eq 'HASH') {
 1884:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1885:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1886:             } elsif (defined($settings->{'default'})) {
 1887:                 $defaultquota = $settings->{'default'};
 1888:             }
 1889:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1890:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1891:             }
 1892:         }
 1893:     }
 1894:     $typecount ++;
 1895:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1896:     $datatable .= '<tr'.$css_class.'>'.
 1897:                   '<td>'.$othertitle.'</td>'.
 1898:                   '<td class="LC_left_item">';
 1899:     if ($context eq 'requestcourses') {
 1900:         $datatable .= '<table><tr>';
 1901:     }
 1902:     my %defcell;
 1903:     foreach my $item (@usertools) {
 1904:         if ($context eq 'requestcourses') {
 1905:             my ($curroption,$currlimit);
 1906:             if (ref($settings) eq 'HASH') {
 1907:                 if (ref($settings->{$item}) eq 'HASH') {
 1908:                     $curroption = $settings->{$item}->{'default'};
 1909:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1910:                         $currlimit = $1;
 1911:                     }
 1912:                 }
 1913:             }
 1914:             if (!$curroption) {
 1915:                 $curroption = 'norequest';
 1916:             }
 1917:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1918:             foreach my $option (@options) {
 1919:                 my $val = $option;
 1920:                 if ($option eq 'norequest') {
 1921:                     $val = 0;
 1922:                 }
 1923:                 if ($option eq 'validate') {
 1924:                     my $canvalidate = 0;
 1925:                     if (ref($validations{$item}) eq 'HASH') {
 1926:                         if ($validations{$item}{'default'}) {
 1927:                             $canvalidate = 1;
 1928:                         }
 1929:                     }
 1930:                     next if (!$canvalidate);
 1931:                 }
 1932:                 my $checked = '';
 1933:                 if ($option eq $curroption) {
 1934:                     $checked = ' checked="checked"';
 1935:                 } elsif ($option eq 'autolimit') {
 1936:                     if ($curroption =~ /^autolimit/) {
 1937:                         $checked = ' checked="checked"';
 1938:                     }
 1939:                 }
 1940:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1941:                                   '<input type="radio" name="crsreq_'.$item.
 1942:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1943:                                   $titles{$option}.'</label>';
 1944:                 if ($option eq 'autolimit') {
 1945:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1946:                                        $item.'_limit_default" size="1" '.
 1947:                                        'value="'.$currlimit.'" />';
 1948:                 }
 1949:                 $defcell{$item} .= '</span> ';
 1950:                 if ($option eq 'autolimit') {
 1951:                     $defcell{$item} .= $titles{'unlimited'};
 1952:                 }
 1953:             }
 1954:         } elsif ($context eq 'requestauthor') {
 1955:             my $curroption;
 1956:             if (ref($settings) eq 'HASH') {
 1957:                 $curroption = $settings->{'default'};
 1958:             }
 1959:             if (!$curroption) {
 1960:                 $curroption = 'norequest';
 1961:             }
 1962:             foreach my $option (@options) {
 1963:                 my $val = $option;
 1964:                 if ($option eq 'norequest') {
 1965:                     $val = 0;
 1966:                 }
 1967:                 my $checked = '';
 1968:                 if ($option eq $curroption) {
 1969:                     $checked = ' checked="checked"';
 1970:                 }
 1971:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1972:                               '<input type="radio" name="authorreq_default"'.
 1973:                               ' value="'.$val.'"'.$checked.' />'.
 1974:                               $titles{$option}.'</label></span>&nbsp; ';
 1975:             }
 1976:         } else {
 1977:             my $checked = 'checked="checked" ';
 1978:             if (ref($settings) eq 'HASH') {
 1979:                 if (ref($settings->{$item}) eq 'HASH') {
 1980:                     if ($settings->{$item}->{'default'} == 0) {
 1981:                         $checked = '';
 1982:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1983:                         $checked = 'checked="checked" ';
 1984:                     }
 1985:                 }
 1986:             }
 1987:             $datatable .= '<span class="LC_nobreak"><label>'.
 1988:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1989:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1990:                           '</label></span>&nbsp; ';
 1991:         }
 1992:     }
 1993:     if ($context eq 'requestcourses') {
 1994:         $datatable .= '</tr><tr>';
 1995:         foreach my $item (@usertools) {
 1996:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1997:         }
 1998:         $datatable .= '</tr></table>';
 1999:     }
 2000:     $datatable .= '</td>';
 2001:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2002:         $datatable .= '<td class="LC_right_item">'.
 2003:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2004:                       '<input type="text" name="defaultquota" value="'.
 2005:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2006:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2007:                       '<input type="text" name="authorquota" value="'.
 2008:                       $authorquota.'" size="5" /></span></td>';
 2009:     }
 2010:     $datatable .= '</tr>';
 2011:     $typecount ++;
 2012:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2013:     $datatable .= '<tr'.$css_class.'>'.
 2014:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2015:     if ($context eq 'requestcourses') {
 2016:         $datatable .= &mt('(overrides affiliation, if set)').
 2017:                       '</td>'.
 2018:                       '<td class="LC_left_item">'.
 2019:                       '<table><tr>';
 2020:     } else {
 2021:         $datatable .= &mt('(overrides affiliation, if checked)').
 2022:                       '</td>'.
 2023:                       '<td class="LC_left_item" colspan="2">'.
 2024:                       '<br />';
 2025:     }
 2026:     my %advcell;
 2027:     foreach my $item (@usertools) {
 2028:         if ($context eq 'requestcourses') {
 2029:             my ($curroption,$currlimit);
 2030:             if (ref($settings) eq 'HASH') {
 2031:                 if (ref($settings->{$item}) eq 'HASH') {
 2032:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2033:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2034:                         $currlimit = $1;
 2035:                     }
 2036:                 }
 2037:             }
 2038:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2039:             my $checked = '';
 2040:             if ($curroption eq '') {
 2041:                 $checked = ' checked="checked"';
 2042:             }
 2043:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2044:                                '<input type="radio" name="crsreq_'.$item.
 2045:                                '__LC_adv" value=""'.$checked.' />'.
 2046:                                &mt('No override set').'</label></span>&nbsp; ';
 2047:             foreach my $option (@options) {
 2048:                 my $val = $option;
 2049:                 if ($option eq 'norequest') {
 2050:                     $val = 0;
 2051:                 }
 2052:                 if ($option eq 'validate') {
 2053:                     my $canvalidate = 0;
 2054:                     if (ref($validations{$item}) eq 'HASH') {
 2055:                         if ($validations{$item}{'_LC_adv'}) {
 2056:                             $canvalidate = 1;
 2057:                         }
 2058:                     }
 2059:                     next if (!$canvalidate);
 2060:                 }
 2061:                 my $checked = '';
 2062:                 if ($val eq $curroption) {
 2063:                     $checked = ' checked="checked"';
 2064:                 } elsif ($option eq 'autolimit') {
 2065:                     if ($curroption =~ /^autolimit/) {
 2066:                         $checked = ' checked="checked"';
 2067:                     }
 2068:                 }
 2069:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2070:                                   '<input type="radio" name="crsreq_'.$item.
 2071:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2072:                                   $titles{$option}.'</label>';
 2073:                 if ($option eq 'autolimit') {
 2074:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2075:                                        $item.'_limit__LC_adv" size="1" '.
 2076:                                        'value="'.$currlimit.'" />';
 2077:                 }
 2078:                 $advcell{$item} .= '</span> ';
 2079:                 if ($option eq 'autolimit') {
 2080:                     $advcell{$item} .= $titles{'unlimited'};
 2081:                 }
 2082:             }
 2083:         } elsif ($context eq 'requestauthor') {
 2084:             my $curroption;
 2085:             if (ref($settings) eq 'HASH') {
 2086:                 $curroption = $settings->{'_LC_adv'};
 2087:             }
 2088:             my $checked = '';
 2089:             if ($curroption eq '') {
 2090:                 $checked = ' checked="checked"';
 2091:             }
 2092:             $datatable .= '<span class="LC_nobreak"><label>'.
 2093:                           '<input type="radio" name="authorreq__LC_adv"'.
 2094:                           ' value=""'.$checked.' />'.
 2095:                           &mt('No override set').'</label></span>&nbsp; ';
 2096:             foreach my $option (@options) {
 2097:                 my $val = $option;
 2098:                 if ($option eq 'norequest') {
 2099:                     $val = 0;
 2100:                 }
 2101:                 my $checked = '';
 2102:                 if ($val eq $curroption) {
 2103:                     $checked = ' checked="checked"';
 2104:                 }
 2105:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2106:                               '<input type="radio" name="authorreq__LC_adv"'.
 2107:                               ' value="'.$val.'"'.$checked.' />'.
 2108:                               $titles{$option}.'</label></span>&nbsp; ';
 2109:             }
 2110:         } else {
 2111:             my $checked = 'checked="checked" ';
 2112:             if (ref($settings) eq 'HASH') {
 2113:                 if (ref($settings->{$item}) eq 'HASH') {
 2114:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2115:                         $checked = '';
 2116:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2117:                         $checked = 'checked="checked" ';
 2118:                     }
 2119:                 }
 2120:             }
 2121:             $datatable .= '<span class="LC_nobreak"><label>'.
 2122:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2123:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2124:                           '</label></span>&nbsp; ';
 2125:         }
 2126:     }
 2127:     if ($context eq 'requestcourses') {
 2128:         $datatable .= '</tr><tr>';
 2129:         foreach my $item (@usertools) {
 2130:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2131:         }
 2132:         $datatable .= '</tr></table>';
 2133:     }
 2134:     $datatable .= '</td></tr>';
 2135:     $$rowtotal += $typecount;
 2136:     return $datatable;
 2137: }
 2138: 
 2139: sub print_requestmail {
 2140:     my ($dom,$action,$settings,$rowtotal) = @_;
 2141:     my ($now,$datatable,%currapp);
 2142:     $now = time;
 2143:     if (ref($settings) eq 'HASH') {
 2144:         if (ref($settings->{'notify'}) eq 'HASH') {
 2145:             if ($settings->{'notify'}{'approval'} ne '') {
 2146:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2147:             }
 2148:         }
 2149:     }
 2150:     my $numinrow = 2;
 2151:     my $css_class;
 2152:     $css_class = ($$rowtotal%2? ' class="LC_odd_row"':'');
 2153:     my $text;
 2154:     if ($action eq 'requestcourses') {
 2155:         $text = &mt('Receive notification of course requests requiring approval');
 2156:     } elsif ($action eq 'requestauthor') {
 2157:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2158:     } else {
 2159:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2160:     }
 2161:     $datatable = '<tr'.$css_class.'>'.
 2162:                  ' <td>'.$text.'</td>'.
 2163:                  ' <td class="LC_left_item">';
 2164:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2165:                                                  $action.'notifyapproval',%currapp);
 2166:     if ($numdc > 0) {
 2167:         $datatable .= $table;
 2168:     } else {
 2169:         $datatable .= &mt('There are no active Domain Coordinators');
 2170:     }
 2171:     $datatable .='</td></tr>';
 2172:     return $datatable;
 2173: }
 2174: 
 2175: sub print_studentcode {
 2176:     my ($settings,$rowtotal) = @_;
 2177:     my $rownum = 0; 
 2178:     my ($output,%current);
 2179:     my @crstypes = ('official','unofficial','community','textbook');
 2180:     if (ref($settings) eq 'HASH') {
 2181:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2182:             foreach my $type (@crstypes) {
 2183:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2184:             }
 2185:         }
 2186:     }
 2187:     $output .= '<tr>'.
 2188:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2189:                '<td class="LC_left_item">';
 2190:     foreach my $type (@crstypes) {
 2191:         my $check = ' ';
 2192:         if ($current{$type}) {
 2193:             $check = ' checked="checked" ';
 2194:         }
 2195:         $output .= '<span class="LC_nobreak"><label>'.
 2196:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2197:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2198:     }
 2199:     $output .= '</td></tr>';
 2200:     $$rowtotal ++;
 2201:     return $output;
 2202: }
 2203: 
 2204: sub print_textbookcourses {
 2205:     my ($dom,$type,$settings,$rowtotal) = @_;
 2206:     my $rownum = 0;
 2207:     my $css_class;
 2208:     my $itemcount = 1;
 2209:     my $maxnum = 0;
 2210:     my $bookshash;
 2211:     if (ref($settings) eq 'HASH') {
 2212:         $bookshash = $settings->{$type};
 2213:     }
 2214:     my %ordered;
 2215:     if (ref($bookshash) eq 'HASH') {
 2216:         foreach my $item (keys(%{$bookshash})) {
 2217:             if (ref($bookshash->{$item}) eq 'HASH') {
 2218:                 my $num = $bookshash->{$item}{'order'};
 2219:                 $ordered{$num} = $item;
 2220:             }
 2221:         }
 2222:     }
 2223:     my $confname = $dom.'-domainconfig';
 2224:     my $switchserver = &check_switchserver($dom,$confname);
 2225:     my $maxnum = scalar(keys(%ordered));
 2226:     my $datatable;
 2227:     if (keys(%ordered)) {
 2228:         my @items = sort { $a <=> $b } keys(%ordered);
 2229:         for (my $i=0; $i<@items; $i++) {
 2230:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2231:             my $key = $ordered{$items[$i]};
 2232:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2233:             my $coursetitle = $coursehash{'description'};
 2234:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2235:             if (ref($bookshash->{$key}) eq 'HASH') {
 2236:                 $subject = $bookshash->{$key}->{'subject'};
 2237:                 $title = $bookshash->{$key}->{'title'};
 2238:                 if ($type eq 'textbooks') {
 2239:                     $publisher = $bookshash->{$key}->{'publisher'};
 2240:                     $author = $bookshash->{$key}->{'author'};
 2241:                     $image = $bookshash->{$key}->{'image'};
 2242:                     if ($image ne '') {
 2243:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2244:                         my $imagethumb = "$path/tn-".$imagefile;
 2245:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2246:                     }
 2247:                 }
 2248:             }
 2249:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2250:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2251:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2252:             for (my $k=0; $k<=$maxnum; $k++) {
 2253:                 my $vpos = $k+1;
 2254:                 my $selstr;
 2255:                 if ($k == $i) {
 2256:                     $selstr = ' selected="selected" ';
 2257:                 }
 2258:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2259:             }
 2260:             $datatable .= '</select>'.('&nbsp;'x2).
 2261:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2262:                 &mt('Delete?').'</label></span></td>'.
 2263:                 '<td colspan="2">'.
 2264:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2265:                 ('&nbsp;'x2).
 2266:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2267:             if ($type eq 'textbooks') {
 2268:                 $datatable .= ('&nbsp;'x2).
 2269:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2270:                               ('&nbsp;'x2).
 2271:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2272:                               ('&nbsp;'x2).
 2273:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2274:                 if ($image) {
 2275:                     $datatable .= '<span class="LC_nobreak">'.
 2276:                                   $imgsrc.
 2277:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2278:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2279:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2280:                 }
 2281:                 if ($switchserver) {
 2282:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2283:                 } else {
 2284:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2285:                 }
 2286:             }
 2287:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2288:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2289:                           $coursetitle.'</span></td></tr>'."\n";
 2290:             $itemcount ++;
 2291:         }
 2292:     }
 2293:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2294:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2295:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2296:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2297:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2298:     for (my $k=0; $k<$maxnum+1; $k++) {
 2299:         my $vpos = $k+1;
 2300:         my $selstr;
 2301:         if ($k == $maxnum) {
 2302:             $selstr = ' selected="selected" ';
 2303:         }
 2304:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2305:     }
 2306:     $datatable .= '</select>&nbsp;'."\n".
 2307:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2308:                   '<td colspan="2">'.
 2309:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2310:                   ('&nbsp;'x2).
 2311:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2312:                   ('&nbsp;'x2);
 2313:     if ($type eq 'textbooks') {
 2314:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2315:                       ('&nbsp;'x2).
 2316:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2317:                       ('&nbsp;'x2).
 2318:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2319:         if ($switchserver) {
 2320:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2321:         } else {
 2322:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2323:         }
 2324:     }
 2325:     $datatable .= '</span>'."\n".
 2326:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2327:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2328:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2329:                   &Apache::loncommon::selectcourse_link
 2330:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course');
 2331:                   '</span></td>'."\n".
 2332:                   '</tr>'."\n";
 2333:     $itemcount ++;
 2334:     return $datatable;
 2335: }
 2336: 
 2337: sub textbookcourses_javascript {
 2338:     my ($settings) = @_;
 2339:     return unless(ref($settings) eq 'HASH');
 2340:     my (%ordered,%total,%jstext);
 2341:     foreach my $type ('textbooks','templates') {
 2342:         $total{$type} = 0;
 2343:         if (ref($settings->{$type}) eq 'HASH') {
 2344:             foreach my $item (keys(%{$settings->{$type}})) {
 2345:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2346:                     my $num = $settings->{$type}->{$item}{'order'};
 2347:                     $ordered{$type}{$num} = $item;
 2348:                 }
 2349:             }
 2350:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2351:         }
 2352:         my @jsarray = ();
 2353:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2354:             push(@jsarray,$ordered{$type}{$item});
 2355:         }
 2356:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2357:     }
 2358:     return <<"ENDSCRIPT";
 2359: <script type="text/javascript">
 2360: // <![CDATA[
 2361: function reorderBooks(form,item,caller) {
 2362:     var changedVal;
 2363: $jstext{'textbooks'};
 2364: $jstext{'templates'};
 2365:     var newpos;
 2366:     var maxh;
 2367:     if (caller == 'textbooks') {  
 2368:         newpos = 'textbooks_addbook_pos';
 2369:         maxh = 1 + $total{'textbooks'};
 2370:     } else {
 2371:         newpos = 'templates_addbook_pos';
 2372:         maxh = 1 + $total{'templates'};
 2373:     }
 2374:     var current = new Array;
 2375:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2376:     if (item == newpos) {
 2377:         changedVal = newitemVal;
 2378:     } else {
 2379:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2380:         current[newitemVal] = newpos;
 2381:     }
 2382:     if (caller == 'textbooks') {
 2383:         for (var i=0; i<textbooks.length; i++) {
 2384:             var elementName = 'textbooks_'+textbooks[i];
 2385:             if (elementName != item) {
 2386:                 if (form.elements[elementName]) {
 2387:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2388:                     current[currVal] = elementName;
 2389:                 }
 2390:             }
 2391:         }
 2392:     }
 2393:     if (caller == 'templates') {
 2394:         for (var i=0; i<templates.length; i++) {
 2395:             var elementName = 'templates_'+templates[i];
 2396:             if (elementName != item) {
 2397:                 if (form.elements[elementName]) {
 2398:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2399:                     current[currVal] = elementName;
 2400:                 }
 2401:             }
 2402:         }
 2403:     }
 2404:     var oldVal;
 2405:     for (var j=0; j<maxh; j++) {
 2406:         if (current[j] == undefined) {
 2407:             oldVal = j;
 2408:         }
 2409:     }
 2410:     if (oldVal < changedVal) {
 2411:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2412:            var elementName = current[k];
 2413:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2414:         }
 2415:     } else {
 2416:         for (var k=changedVal; k<oldVal; k++) {
 2417:             var elementName = current[k];
 2418:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2419:         }
 2420:     }
 2421:     return;
 2422: }
 2423: 
 2424: // ]]>
 2425: </script>
 2426: 
 2427: ENDSCRIPT
 2428: }
 2429: 
 2430: sub print_autoenroll {
 2431:     my ($dom,$settings,$rowtotal) = @_;
 2432:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2433:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,$failsafe);
 2434:     if (ref($settings) eq 'HASH') {
 2435:         if (exists($settings->{'run'})) {
 2436:             if ($settings->{'run'} eq '0') {
 2437:                 $runoff = ' checked="checked" ';
 2438:                 $runon = ' ';
 2439:             } else {
 2440:                 $runon = ' checked="checked" ';
 2441:                 $runoff = ' ';
 2442:             }
 2443:         } else {
 2444:             if ($autorun) {
 2445:                 $runon = ' checked="checked" ';
 2446:                 $runoff = ' ';
 2447:             } else {
 2448:                 $runoff = ' checked="checked" ';
 2449:                 $runon = ' ';
 2450:             }
 2451:         }
 2452:         if (exists($settings->{'co-owners'})) {
 2453:             if ($settings->{'co-owners'} eq '0') {
 2454:                 $coownersoff = ' checked="checked" ';
 2455:                 $coownerson = ' ';
 2456:             } else {
 2457:                 $coownerson = ' checked="checked" ';
 2458:                 $coownersoff = ' ';
 2459:             }
 2460:         } else {
 2461:             $coownersoff = ' checked="checked" ';
 2462:             $coownerson = ' ';
 2463:         }
 2464:         if (exists($settings->{'sender_domain'})) {
 2465:             $defdom = $settings->{'sender_domain'};
 2466:         }
 2467:         if (exists($settings->{'autofailsafe'})) {
 2468:             $failsafe = $settings->{'autofailsafe'};
 2469:         }
 2470:     } else {
 2471:         if ($autorun) {
 2472:             $runon = ' checked="checked" ';
 2473:             $runoff = ' ';
 2474:         } else {
 2475:             $runoff = ' checked="checked" ';
 2476:             $runon = ' ';
 2477:         }
 2478:     }
 2479:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2480:     my $notif_sender;
 2481:     if (ref($settings) eq 'HASH') {
 2482:         $notif_sender = $settings->{'sender_uname'};
 2483:     }
 2484:     my $datatable='<tr class="LC_odd_row">'.
 2485:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2486:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2487:                   '<input type="radio" name="autoenroll_run"'.
 2488:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2489:                   '<label><input type="radio" name="autoenroll_run"'.
 2490:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2491:                   '</tr><tr>'.
 2492:                   '<td>'.&mt('Notification messages - sender').
 2493:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2494:                   &mt('username').':&nbsp;'.
 2495:                   '<input type="text" name="sender_uname" value="'.
 2496:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2497:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2498:                   '<tr class="LC_odd_row">'.
 2499:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2500:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2501:                   '<input type="radio" name="autoassign_coowners"'.
 2502:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2503:                   '<label><input type="radio" name="autoassign_coowners"'.
 2504:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2505:                   '</tr><tr>'.
 2506:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 2507:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2508:                   '<input type="text" name="autoenroll_failsafe"'.
 2509:                   ' value="'.$failsafe.'" size="4" /></td></tr>';
 2510:     $$rowtotal += 4;
 2511:     return $datatable;
 2512: }
 2513: 
 2514: sub print_autoupdate {
 2515:     my ($position,$dom,$settings,$rowtotal) = @_;
 2516:     my $datatable;
 2517:     if ($position eq 'top') {
 2518:         my $updateon = ' ';
 2519:         my $updateoff = ' checked="checked" ';
 2520:         my $classlistson = ' ';
 2521:         my $classlistsoff = ' checked="checked" ';
 2522:         if (ref($settings) eq 'HASH') {
 2523:             if ($settings->{'run'} eq '1') {
 2524:                 $updateon = $updateoff;
 2525:                 $updateoff = ' ';
 2526:             }
 2527:             if ($settings->{'classlists'} eq '1') {
 2528:                 $classlistson = $classlistsoff;
 2529:                 $classlistsoff = ' ';
 2530:             }
 2531:         }
 2532:         my %title = (
 2533:                    run => 'Auto-update active?',
 2534:                    classlists => 'Update information in classlists?',
 2535:                     );
 2536:         $datatable = '<tr class="LC_odd_row">'. 
 2537:                   '<td>'.&mt($title{'run'}).'</td>'.
 2538:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2539:                   '<input type="radio" name="autoupdate_run"'.
 2540:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2541:                   '<label><input type="radio" name="autoupdate_run"'.
 2542:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2543:                   '</tr><tr>'.
 2544:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2545:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2546:                   '<label><input type="radio" name="classlists"'.
 2547:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2548:                   '<label><input type="radio" name="classlists"'.
 2549:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2550:                   '</tr>';
 2551:         $$rowtotal += 2;
 2552:     } elsif ($position eq 'middle') {
 2553:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2554:         my $numinrow = 3;
 2555:         my $locknamesettings;
 2556:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2557:                                      $dom,$numinrow,$othertitle,
 2558:                                     'lockablenames');
 2559:         $$rowtotal ++;
 2560:     } else {
 2561:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2562:         my @fields = ('lastname','firstname','middlename','generation',
 2563:                       'permanentemail','id');
 2564:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2565:         my $numrows = 0;
 2566:         if (ref($types) eq 'ARRAY') {
 2567:             if (@{$types} > 0) {
 2568:                 $datatable = 
 2569:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2570:                                          \@fields,$types,\$numrows);
 2571:                     $$rowtotal += @{$types}; 
 2572:             }
 2573:         }
 2574:         $datatable .= 
 2575:             &usertype_update_row($settings,{'default' => $othertitle},
 2576:                                  \%fieldtitles,\@fields,['default'],
 2577:                                  \$numrows);
 2578:         $$rowtotal ++;     
 2579:     }
 2580:     return $datatable;
 2581: }
 2582: 
 2583: sub print_autocreate {
 2584:     my ($dom,$settings,$rowtotal) = @_;
 2585:     my (%createon,%createoff,%currhash);
 2586:     my @types = ('xml','req');
 2587:     if (ref($settings) eq 'HASH') {
 2588:         foreach my $item (@types) {
 2589:             $createoff{$item} = ' checked="checked" ';
 2590:             $createon{$item} = ' ';
 2591:             if (exists($settings->{$item})) {
 2592:                 if ($settings->{$item}) {
 2593:                     $createon{$item} = ' checked="checked" ';
 2594:                     $createoff{$item} = ' ';
 2595:                 }
 2596:             }
 2597:         }
 2598:         if ($settings->{'xmldc'} ne '') {
 2599:             $currhash{$settings->{'xmldc'}} = 1;
 2600:         }
 2601:     } else {
 2602:         foreach my $item (@types) {
 2603:             $createoff{$item} = ' checked="checked" ';
 2604:             $createon{$item} = ' ';
 2605:         }
 2606:     }
 2607:     $$rowtotal += 2;
 2608:     my $numinrow = 2;
 2609:     my $datatable='<tr class="LC_odd_row">'.
 2610:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2611:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2612:                   '<input type="radio" name="autocreate_xml"'.
 2613:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2614:                   '<label><input type="radio" name="autocreate_xml"'.
 2615:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2616:                   '</td></tr><tr>'.
 2617:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2618:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2619:                   '<input type="radio" name="autocreate_req"'.
 2620:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2621:                   '<label><input type="radio" name="autocreate_req"'.
 2622:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2623:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2624:                                                    'autocreate_xmldc',%currhash);
 2625:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 2626:     if ($numdc > 1) {
 2627:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 2628:                       '</td><td class="LC_left_item">';
 2629:     } else {
 2630:         $datatable .= &mt('Course creation processed as:').
 2631:                       '</td><td class="LC_right_item">';
 2632:     }
 2633:     $datatable .= $dctable.'</td></tr>';
 2634:     $$rowtotal += $rows;
 2635:     return $datatable;
 2636: }
 2637: 
 2638: sub print_directorysrch {
 2639:     my ($position,$dom,$settings,$rowtotal) = @_;
 2640:     my $datatable;
 2641:     if ($position eq 'top') {
 2642:         my $instsrchon = ' ';
 2643:         my $instsrchoff = ' checked="checked" ';
 2644:         my ($exacton,$containson,$beginson);
 2645:         my $instlocalon = ' ';
 2646:         my $instlocaloff = ' checked="checked" ';
 2647:         if (ref($settings) eq 'HASH') {
 2648:             if ($settings->{'available'} eq '1') {
 2649:                 $instsrchon = $instsrchoff;
 2650:                 $instsrchoff = ' ';
 2651:             }
 2652:             if ($settings->{'localonly'} eq '1') {
 2653:                 $instlocalon = $instlocaloff;
 2654:                 $instlocaloff = ' ';
 2655:             }
 2656:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2657:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 2658:                     if ($type eq 'exact') {
 2659:                         $exacton = ' checked="checked" ';
 2660:                     } elsif ($type eq 'contains') {
 2661:                         $containson = ' checked="checked" ';
 2662:                     } elsif ($type eq 'begins') {
 2663:                         $beginson = ' checked="checked" ';
 2664:                     }
 2665:                 }
 2666:             } else {
 2667:                 if ($settings->{'searchtypes'} eq 'exact') {
 2668:                     $exacton = ' checked="checked" ';
 2669:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 2670:                     $containson = ' checked="checked" ';
 2671:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 2672:                     $exacton = ' checked="checked" ';
 2673:                     $containson = ' checked="checked" ';
 2674:                 }
 2675:             }
 2676:         }
 2677:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2678:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2679: 
 2680:         my $numinrow = 4;
 2681:         my $cansrchrow = 0;
 2682:         $datatable='<tr class="LC_odd_row">'.
 2683:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 2684:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2685:                    '<input type="radio" name="dirsrch_available"'.
 2686:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2687:                    '<label><input type="radio" name="dirsrch_available"'.
 2688:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2689:                    '</tr><tr>'.
 2690:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 2691:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2692:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 2693:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2694:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 2695:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2696:                    '</tr>';
 2697:         $$rowtotal += 2;
 2698:         if (ref($usertypes) eq 'HASH') {
 2699:             if (keys(%{$usertypes}) > 0) {
 2700:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2701:                                              $numinrow,$othertitle,'cansearch');
 2702:                 $cansrchrow = 1;
 2703:             }
 2704:         }
 2705:         if ($cansrchrow) {
 2706:             $$rowtotal ++;
 2707:             $datatable .= '<tr>';
 2708:         } else {
 2709:             $datatable .= '<tr class="LC_odd_row">';
 2710:         }
 2711:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2712:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2713:         foreach my $title (@{$titleorder}) {
 2714:             if (defined($searchtitles->{$title})) {
 2715:                 my $check = ' ';
 2716:                 if (ref($settings) eq 'HASH') {
 2717:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2718:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2719:                             $check = ' checked="checked" ';
 2720:                         }
 2721:                     }
 2722:                 }
 2723:                 $datatable .= '<td class="LC_left_item">'.
 2724:                               '<span class="LC_nobreak"><label>'.
 2725:                               '<input type="checkbox" name="searchby" '.
 2726:                               'value="'.$title.'"'.$check.'/>'.
 2727:                               $searchtitles->{$title}.'</label></span></td>';
 2728:             }
 2729:         }
 2730:         $datatable .= '</tr></table></td></tr>';
 2731:         $$rowtotal ++;
 2732:         if ($cansrchrow) {
 2733:             $datatable .= '<tr class="LC_odd_row">';
 2734:         } else {
 2735:             $datatable .= '<tr>';
 2736:         }
 2737:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2738:                       '<td class="LC_left_item" colspan="2">'.
 2739:                       '<span class="LC_nobreak"><label>'.
 2740:                       '<input type="checkbox" name="searchtypes" '.
 2741:                       $exacton.' value="exact" />'.&mt('Exact match').
 2742:                       '</label>&nbsp;'.
 2743:                       '<label><input type="checkbox" name="searchtypes" '.
 2744:                       $beginson.' value="begins" />'.&mt('Begins with').
 2745:                       '</label>&nbsp;'.
 2746:                       '<label><input type="checkbox" name="searchtypes" '.
 2747:                       $containson.' value="contains" />'.&mt('Contains').
 2748:                       '</label></span></td></tr>';
 2749:         $$rowtotal ++;
 2750:     } else {
 2751:         my $domsrchon = ' checked="checked" ';
 2752:         my $domsrchoff = ' ';
 2753:         my $domlocalon = ' ';
 2754:         my $domlocaloff = ' checked="checked" ';
 2755:         if (ref($settings) eq 'HASH') {
 2756:             if ($settings->{'lclocalonly'} eq '1') {
 2757:                 $domlocalon = $domlocaloff;
 2758:                 $domlocaloff = ' ';
 2759:             }
 2760:             if ($settings->{'lcavailable'} eq '0') {
 2761:                 $domsrchoff = $domsrchon;
 2762:                 $domsrchon = ' ';
 2763:             }
 2764:         }
 2765:         $datatable='<tr class="LC_odd_row">'.
 2766:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 2767:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2768:                       '<input type="radio" name="dirsrch_domavailable"'.
 2769:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2770:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 2771:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2772:                       '</tr><tr>'.
 2773:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 2774:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2775:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 2776:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2777:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 2778:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2779:                       '</tr>';
 2780:         $$rowtotal += 2;
 2781:     }
 2782:     return $datatable;
 2783: }
 2784: 
 2785: sub print_contacts {
 2786:     my ($dom,$settings,$rowtotal) = @_;
 2787:     my $datatable;
 2788:     my @contacts = ('adminemail','supportemail');
 2789:     my (%checked,%to,%otheremails,%bccemails);
 2790:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2791:                     'requestsmail','updatesmail','idconflictsmail');
 2792:     foreach my $type (@mailings) {
 2793:         $otheremails{$type} = '';
 2794:     }
 2795:     $bccemails{'helpdeskmail'} = '';
 2796:     if (ref($settings) eq 'HASH') {
 2797:         foreach my $item (@contacts) {
 2798:             if (exists($settings->{$item})) {
 2799:                 $to{$item} = $settings->{$item};
 2800:             }
 2801:         }
 2802:         foreach my $type (@mailings) {
 2803:             if (exists($settings->{$type})) {
 2804:                 if (ref($settings->{$type}) eq 'HASH') {
 2805:                     foreach my $item (@contacts) {
 2806:                         if ($settings->{$type}{$item}) {
 2807:                             $checked{$type}{$item} = ' checked="checked" ';
 2808:                         }
 2809:                     }
 2810:                     $otheremails{$type} = $settings->{$type}{'others'};
 2811:                     if ($type eq 'helpdeskmail') {
 2812:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2813:                     }
 2814:                 }
 2815:             } elsif ($type eq 'lonstatusmail') {
 2816:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2817:             }
 2818:         }
 2819:     } else {
 2820:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2821:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2822:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2823:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2824:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2825:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2826:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2827:         $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2828:         $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 2829:     }
 2830:     my ($titles,$short_titles) = &contact_titles();
 2831:     my $rownum = 0;
 2832:     my $css_class;
 2833:     foreach my $item (@contacts) {
 2834:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2835:         $datatable .= '<tr'.$css_class.'>'. 
 2836:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2837:                   '</span></td><td class="LC_right_item">'.
 2838:                   '<input type="text" name="'.$item.'" value="'.
 2839:                   $to{$item}.'" /></td></tr>';
 2840:         $rownum ++;
 2841:     }
 2842:     foreach my $type (@mailings) {
 2843:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2844:         $datatable .= '<tr'.$css_class.'>'.
 2845:                       '<td><span class="LC_nobreak">'.
 2846:                       $titles->{$type}.': </span></td>'.
 2847:                       '<td class="LC_left_item">'.
 2848:                       '<span class="LC_nobreak">';
 2849:         foreach my $item (@contacts) {
 2850:             $datatable .= '<label>'.
 2851:                           '<input type="checkbox" name="'.$type.'"'.
 2852:                           $checked{$type}{$item}.
 2853:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2854:                           '</label>&nbsp;';
 2855:         }
 2856:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2857:                       '<input type="text" name="'.$type.'_others" '.
 2858:                       'value="'.$otheremails{$type}.'"  />';
 2859:         if ($type eq 'helpdeskmail') {
 2860:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2861:                           '<input type="text" name="'.$type.'_bcc" '.
 2862:                           'value="'.$bccemails{$type}.'"  />';
 2863:         }
 2864:         $datatable .= '</td></tr>'."\n";
 2865:         $rownum ++;
 2866:     }
 2867:     my %choices;
 2868:     $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 2869:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2870:                                    &mt('LON-CAPA core group - MSU'),600,500));
 2871:     $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 2872:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2873:                                     &mt('LON-CAPA core group - MSU'),600,500));
 2874:     my @toggles = ('reporterrors','reportupdates');
 2875:     my %defaultchecked = ('reporterrors'  => 'on',
 2876:                           'reportupdates' => 'on');
 2877:     (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2878:                                                \%choices,$rownum);
 2879:     $datatable .= $reports;
 2880:     $$rowtotal += $rownum;
 2881:     return $datatable;
 2882: }
 2883: 
 2884: sub print_helpsettings {
 2885:     my ($position,$dom,$settings,$rowtotal) = @_;
 2886:     my $confname = $dom.'-domainconfig';
 2887:     my $formname = 'display';
 2888:     my ($datatable,$itemcount);
 2889:     if ($position eq 'top') {
 2890:         $itemcount = 1;
 2891:         my (%choices,%defaultchecked,@toggles);
 2892:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2893:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2894:                                      &mt('LON-CAPA bug tracker'),600,500));
 2895:         %defaultchecked = ('submitbugs' => 'on');
 2896:         @toggles = ('submitbugs');
 2897:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2898:                                                      \%choices,$itemcount);
 2899:         $$rowtotal ++;
 2900:     } else {
 2901:         my $css_class;
 2902:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 2903:         my (%customroles,%ordered,%current);
 2904:         if (ref($settings->{'adhoc'}) eq 'HASH') {
 2905:             %current = %{$settings->{'adhoc'}};
 2906:         }
 2907:         my $count = 0;
 2908:         foreach my $key (sort(keys(%existing))) {
 2909:             if ($key=~/^rolesdef\_(\w+)$/) {
 2910:                 my $rolename = $1;
 2911:                 my (%privs,$order);
 2912:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 2913:                 $customroles{$rolename} = \%privs;
 2914:                 if (ref($current{$rolename}) eq 'HASH') {
 2915:                     $order = $current{$rolename}{'order'};
 2916:                 }
 2917:                 if ($order eq '') {
 2918:                     $order = $count;
 2919:                 }
 2920:                 $ordered{$order} = $rolename;
 2921:                 $count++;
 2922:             }
 2923:         }
 2924:         my $maxnum = scalar(keys(%ordered));
 2925:         my @roles_by_num = ();
 2926:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2927:             push(@roles_by_num,$item);
 2928:         }
 2929:         my $context = 'domprefs';
 2930:         my $crstype = 'Course';
 2931:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2932:         my @accesstypes = ('all','none');
 2933:         my ($numstatustypes,@jsarray);
 2934:         if (ref($types) eq 'ARRAY') {
 2935:             if (@{$types} > 0) {
 2936:                 $numstatustypes = scalar(@{$types});
 2937:                 push(@accesstypes,'status');
 2938:                 @jsarray = ('bystatus');
 2939:             }
 2940:         }
 2941:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh']);
 2942:         if (keys(%domhelpdesk)) {
 2943:             push(@accesstypes,('inc','exc'));
 2944:             push(@jsarray,('notinc','notexc'));
 2945:         }
 2946:         my $hiddenstr = join("','",@jsarray);
 2947:         $datatable .= &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname);
 2948:         my $context = 'domprefs';
 2949:         my $crstype = 'Course';
 2950:         my $prefix = 'helproles_';
 2951:         my $add_class = 'LC_hidden';
 2952:         foreach my $num (@roles_by_num) {
 2953:             my $role = $ordered{$num};
 2954:             my ($desc,$access,@statuses);
 2955:             if (ref($current{$role}) eq 'HASH') {
 2956:                 $desc = $current{$role}{'desc'};
 2957:                 $access = $current{$role}{'access'};
 2958:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 2959:                     @statuses = @{$current{$role}{'insttypes'}};
 2960:                 }
 2961:             }
 2962:             if ($desc eq '') {
 2963:                 $desc = $role;
 2964:             }
 2965:             my $identifier = 'custhelp'.$num;
 2966:             my %full=();
 2967:             my %levels= (
 2968:                          course => {},
 2969:                          domain => {},
 2970:                          system => {},
 2971:                         );
 2972:             my %levelscurrent=(
 2973:                                course => {},
 2974:                                domain => {},
 2975:                                system => {},
 2976:                               );
 2977:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 2978:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 2979:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2980:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 2981:             $datatable .= '<tr '.$css_class.'><td valign="top"><b>'.$role.'</b><br />'.
 2982:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 2983:             for (my $k=0; $k<=$maxnum; $k++) {
 2984:                 my $vpos = $k+1;
 2985:                 my $selstr;
 2986:                 if ($k == $num) {
 2987:                     $selstr = ' selected="selected" ';
 2988:                 }
 2989:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2990:             }
 2991:             $datatable .= '</select>'.('&nbsp;'x2).
 2992:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 2993:                           '</td>'.
 2994:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 2995:                           &mt('Name shown to users:').
 2996:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 2997:                           '</fieldset>'.
 2998:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 2999:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 3000:                           '<fieldset>'.
 3001:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 3002:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 3003:                                                                    \%levelscurrent,$identifier,
 3004:                                                                    'LC_hidden',$prefix.$num.'_privs').
 3005:                           '</fieldset></td>';
 3006:             $itemcount ++;
 3007:         }
 3008:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3009:         my $newcust = 'custhelp'.$count;
 3010:         my (%privs,%levelscurrent);
 3011:         my %full=();
 3012:         my %levels= (
 3013:                      course => {},
 3014:                      domain => {},
 3015:                      system => {},
 3016:                     );
 3017:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 3018:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3019:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 3020:         $datatable .= '<tr '.$css_class.'><td valign="top"><span class="LC_nobreak"><label>'.
 3021:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 3022:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 3023:         for (my $k=0; $k<$maxnum+1; $k++) {
 3024:             my $vpos = $k+1;
 3025:             my $selstr;
 3026:             if ($k == $maxnum) {
 3027:                 $selstr = ' selected="selected" ';
 3028:             }
 3029:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3030:         }
 3031:         $datatable .= '</select>&nbsp;'."\n".
 3032:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 3033:                       '</label></span></td>'.
 3034:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3035:                       '<span class="LC_nobreak">'.
 3036:                       &mt('Internal name:').
 3037:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 3038:                       '</span>'.('&nbsp;'x4).
 3039:                       '<span class="LC_nobreak">'.
 3040:                       &mt('Name shown to users:').
 3041:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 3042:                       '</span></fieldset>'.
 3043:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 3044:                                              $usertypes,$types,\%domhelpdesk).
 3045:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 3046:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 3047:                                                                 \@templateroles,$newcust).
 3048:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 3049:                                                                \%levelscurrent,$newcust).
 3050:                       '</fieldset></td></tr>';
 3051:         $count ++;
 3052:         $$rowtotal += $count;
 3053:     }
 3054:     return $datatable;
 3055: }
 3056: 
 3057: sub adhocbutton {
 3058:     my ($prefix,$num,$field,$visibility) = @_;
 3059:     my %lt = &Apache::lonlocal::texthash(
 3060:                                           show => 'Show details',
 3061:                                           hide => 'Hide details',
 3062:                                         );
 3063:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 3064:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 3065:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 3066:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 3067: }
 3068: 
 3069: sub helpsettings_javascript {
 3070:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 3071:     return unless(ref($roles_by_num) eq 'ARRAY');
 3072:     my %html_js_lt = &Apache::lonlocal::texthash(
 3073:                                           show => 'Show details',
 3074:                                           hide => 'Hide details',
 3075:                                         );
 3076:     &html_escape(\%html_js_lt);
 3077:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 3078:     return <<"ENDSCRIPT";
 3079: <script type="text/javascript">
 3080: // <![CDATA[
 3081: 
 3082: function reorderHelpRoles(form,item) {
 3083:     var changedVal;
 3084: $jstext
 3085:     var newpos = 'helproles_${total}_pos';
 3086:     var maxh = 1 + $total;
 3087:     var current = new Array();
 3088:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3089:     if (item == newpos) {
 3090:         changedVal = newitemVal;
 3091:     } else {
 3092:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3093:         current[newitemVal] = newpos;
 3094:     }
 3095:     for (var i=0; i<helproles.length; i++) {
 3096:         var elementName = 'helproles_'+helproles[i]+'_pos';
 3097:         if (elementName != item) {
 3098:             if (form.elements[elementName]) {
 3099:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3100:                 current[currVal] = elementName;
 3101:             }
 3102:         }
 3103:     }
 3104:     var oldVal;
 3105:     for (var j=0; j<maxh; j++) {
 3106:         if (current[j] == undefined) {
 3107:             oldVal = j;
 3108:         }
 3109:     }
 3110:     if (oldVal < changedVal) {
 3111:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3112:            var elementName = current[k];
 3113:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3114:         }
 3115:     } else {
 3116:         for (var k=changedVal; k<oldVal; k++) {
 3117:             var elementName = current[k];
 3118:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3119:         }
 3120:     }
 3121:     return;
 3122: }
 3123: 
 3124: function helpdeskAccess(num) {
 3125:     var curraccess = null;
 3126:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 3127:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 3128:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 3129:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 3130:             }
 3131:         }
 3132:     }
 3133:     var shown = Array();
 3134:     var hidden = Array();
 3135:     if (curraccess == 'none') {
 3136:         hidden = Array('$hiddenstr');
 3137:     } else {
 3138:         if (curraccess == 'status') {
 3139:             shown = Array('bystatus');
 3140:             hidden = Array('notinc','notexc');
 3141:         } else {
 3142:             if (curraccess == 'exc') {
 3143:                 shown = Array('notexc');
 3144:                 hidden = Array('notinc','bystatus');
 3145:             }
 3146:             if (curraccess == 'inc') {
 3147:                 shown = Array('notinc');
 3148:                 hidden = Array('notexc','bystatus');
 3149:             }
 3150:             if (curraccess == 'all') {
 3151:                 hidden = Array('notinc','notexc','bystatus');
 3152:             }
 3153:         }
 3154:     }
 3155:     if (hidden.length > 0) {
 3156:         for (var i=0; i<hidden.length; i++) {
 3157:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 3158:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 3159:             }
 3160:         }
 3161:     }
 3162:     if (shown.length > 0) {
 3163:         for (var i=0; i<shown.length; i++) {
 3164:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 3165:                 if (shown[i] == 'privs') {
 3166:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 3167:                 } else {
 3168:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 3169:                 }
 3170:             }
 3171:         }
 3172:     }
 3173:     return;
 3174: }
 3175: 
 3176: function toggleHelpdeskItem(num,field) {
 3177:     if (document.getElementById('helproles_'+num+'_'+field)) {
 3178:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 3179:             document.getElementById('helproles_'+num+'_'+field).className =
 3180:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 3181:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3182:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 3183:             }
 3184:         } else {
 3185:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 3186:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3187:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 3188:             }
 3189:         }
 3190:     }
 3191:     return;
 3192: }
 3193: 
 3194: // ]]>
 3195: </script>
 3196: 
 3197: ENDSCRIPT
 3198: }
 3199: 
 3200: sub helpdeskroles_access {
 3201:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 3202:         $usertypes,$types,$domhelpdesk) = @_;
 3203:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 3204:     my %lt = &Apache::lonlocal::texthash(
 3205:                     'rou'    => 'Role usage',
 3206:                     'whi'    => 'Which helpdesk personnel may use this role?',
 3207:                     'all'    => 'All',
 3208:                     'none'   => 'None',
 3209:                     'status' => 'Determined based on institutional status',
 3210:                     'inc'    => 'Include all, but exclude specific personnel',
 3211:                     'exc'    => 'Exclude all, but include specific personnel',
 3212:                   );
 3213:     my %usecheck = (
 3214:                      all => ' checked="checked"',
 3215:                    );
 3216:     my %displaydiv = (
 3217:                       status => 'none',
 3218:                       inc    => 'none',
 3219:                       exc    => 'none',
 3220:                       priv   => 'block',
 3221:                      );
 3222:     my $output;
 3223:     if (ref($current) eq 'HASH') {
 3224:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 3225:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 3226:                 $usecheck{$current->{access}} = $usecheck{'all'};
 3227:                 delete($usecheck{'all'});
 3228:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 3229:                     my $access = $1;
 3230:                     $displaydiv{$access} = 'inline';
 3231:                 } elsif ($current->{access} eq 'none') {
 3232:                     $displaydiv{'priv'} = 'none';
 3233:                 }
 3234:             }
 3235:         }
 3236:     }
 3237:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 3238:               '<p>'.$lt{'whi'}.'</p>';
 3239:     foreach my $access (@{$accesstypes}) {
 3240:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 3241:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 3242:                    $lt{$access}.'</label>';
 3243:         if ($access eq 'status') {
 3244:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 3245:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 3246:                                                                  $othertitle,$usertypes,$types).
 3247:                        '</div>';
 3248:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 3249:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 3250:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3251:                        '</div>';
 3252:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 3253:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 3254:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3255:                        '</div>';
 3256:         }
 3257:         $output .= '</p>';
 3258:     }
 3259:     $output .= '</fieldset>';
 3260:     return $output;
 3261: }
 3262: 
 3263: sub radiobutton_prefs {
 3264:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 3265:         $additional,$align) = @_;
 3266:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 3267:                    (ref($choices) eq 'HASH'));
 3268: 
 3269:     my (%checkedon,%checkedoff,$datatable,$css_class);
 3270: 
 3271:     foreach my $item (@{$toggles}) {
 3272:         if ($defaultchecked->{$item} eq 'on') {
 3273:             $checkedon{$item} = ' checked="checked" ';
 3274:             $checkedoff{$item} = ' ';
 3275:         } elsif ($defaultchecked->{$item} eq 'off') {
 3276:             $checkedoff{$item} = ' checked="checked" ';
 3277:             $checkedon{$item} = ' ';
 3278:         }
 3279:     }
 3280:     if (ref($settings) eq 'HASH') {
 3281:         foreach my $item (@{$toggles}) {
 3282:             if ($settings->{$item} eq '1') {
 3283:                 $checkedon{$item} =  ' checked="checked" ';
 3284:                 $checkedoff{$item} = ' ';
 3285:             } elsif ($settings->{$item} eq '0') {
 3286:                 $checkedoff{$item} =  ' checked="checked" ';
 3287:                 $checkedon{$item} = ' ';
 3288:             }
 3289:         }
 3290:     }
 3291:     if ($onclick) {
 3292:         $onclick = ' onclick="'.$onclick.'"';
 3293:     }
 3294:     foreach my $item (@{$toggles}) {
 3295:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3296:         $datatable .=
 3297:             '<tr'.$css_class.'><td valign="top">'.
 3298:             '<span class="LC_nobreak">'.$choices->{$item}.
 3299:             '</span></td>';
 3300:         if ($align eq 'left') {
 3301:             $datatable .= '<td class="LC_left_item">';
 3302:         } else {
 3303:             $datatable .= '<td class="LC_right_item">';
 3304:         }
 3305:         $datatable .=
 3306:             '<span class="LC_nobreak">'.
 3307:             '<label><input type="radio" name="'.
 3308:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 3309:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 3310:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 3311:             '</span>'.$additional.
 3312:             '</td>'.
 3313:             '</tr>';
 3314:         $itemcount ++;
 3315:     }
 3316:     return ($datatable,$itemcount);
 3317: }
 3318: 
 3319: sub print_coursedefaults {
 3320:     my ($position,$dom,$settings,$rowtotal) = @_;
 3321:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 3322:     my $itemcount = 1;
 3323:     my %choices =  &Apache::lonlocal::texthash (
 3324:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 3325:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 3326:         coursecredits        => 'Credits can be specified for courses',
 3327:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 3328:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 3329:         postsubmit           => 'Disable submit button/keypress following student submission',
 3330:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 3331:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 3332:     );
 3333:     my %staticdefaults = (
 3334:                            anonsurvey_threshold => 10,
 3335:                            uploadquota          => 500,
 3336:                            postsubmit           => 60,
 3337:                            mysqltables          => 172800,
 3338:                          );
 3339:     if ($position eq 'top') {
 3340:         %defaultchecked = (
 3341:                             'uselcmath'       => 'on',
 3342:                             'usejsme'         => 'on',
 3343:                             'canclone'        => 'none',
 3344:                           );
 3345:         @toggles = ('uselcmath','usejsme');
 3346:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3347:                                                      \%choices,$itemcount);
 3348:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3349:         $datatable .=
 3350:             '<tr'.$css_class.'><td valign="top">'.
 3351:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 3352:             '</span></td><td class="LC_left_item">';
 3353:         my $currcanclone = 'none';
 3354:         my $onclick;
 3355:         my @cloneoptions = ('none','domain');
 3356:         my %clonetitles = (
 3357:                              none     => 'No additional course requesters',
 3358:                              domain   => "Any course requester in course's domain",
 3359:                              instcode => 'Course requests for official courses ...',
 3360:                           );
 3361:         my (%codedefaults,@code_order,@posscodes);
 3362:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 3363:                                                     \@code_order) eq 'ok') {
 3364:             if (@code_order > 0) {
 3365:                 push(@cloneoptions,'instcode');
 3366:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 3367:             }
 3368:         }
 3369:         if (ref($settings) eq 'HASH') {
 3370:             if ($settings->{'canclone'}) {
 3371:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 3372:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 3373:                         if (@code_order > 0) {
 3374:                             $currcanclone = 'instcode';
 3375:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 3376:                         }
 3377:                     }
 3378:                 } elsif ($settings->{'canclone'} eq 'domain') {
 3379:                     $currcanclone = $settings->{'canclone'};
 3380:                 }
 3381:             }
 3382:         }
 3383:         foreach my $option (@cloneoptions) {
 3384:             my ($checked,$additional);
 3385:             if ($currcanclone eq $option) {
 3386:                 $checked = ' checked="checked"';
 3387:             }
 3388:             if ($option eq 'instcode') {
 3389:                 if (@code_order) {
 3390:                     my $show = 'none';
 3391:                     if ($checked) {
 3392:                         $show = 'block';
 3393:                     }
 3394:                     $additional = '<div id="cloneinstcode" style="display:'.$show.'" />'.
 3395:                                   &mt('Institutional codes for new and cloned course have identical:').
 3396:                                   '<br />';
 3397:                     foreach my $item (@code_order) {
 3398:                         my $codechk;
 3399:                         if ($checked) {
 3400:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 3401:                                 $codechk = ' checked="checked"';
 3402:                             }
 3403:                         }
 3404:                         $additional .= '<label>'.
 3405:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 3406:                                        $item.'</label>';
 3407:                     }
 3408:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 3409:                 }
 3410:             }
 3411:             $datatable .=
 3412:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 3413:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 3414:                 '</label>&nbsp;'.$additional.'</span><br />';
 3415:         }
 3416:         $datatable .= '</td>'.
 3417:                       '</tr>';
 3418:         $itemcount ++;
 3419:     } else {
 3420:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3421:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 3422:         my $currusecredits = 0;
 3423:         my $postsubmitclient = 1;
 3424:         my @types = ('official','unofficial','community','textbook');
 3425:         if (ref($settings) eq 'HASH') {
 3426:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 3427:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 3428:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 3429:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 3430:                 }
 3431:             }
 3432:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 3433:                 foreach my $type (@types) {
 3434:                     next if ($type eq 'community');
 3435:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 3436:                     if ($defcredits{$type} ne '') {
 3437:                         $currusecredits = 1;
 3438:                     }
 3439:                 }
 3440:             }
 3441:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 3442:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 3443:                     $postsubmitclient = 0;
 3444:                     foreach my $type (@types) {
 3445:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3446:                     }
 3447:                 } else {
 3448:                     foreach my $type (@types) {
 3449:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 3450:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 3451:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 3452:                             } else {
 3453:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3454:                             }
 3455:                         } else {
 3456:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3457:                         }
 3458:                     }
 3459:                 }
 3460:             } else {
 3461:                 foreach my $type (@types) {
 3462:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3463:                 }
 3464:             }
 3465:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 3466:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 3467:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 3468:                 }
 3469:             } else {
 3470:                 foreach my $type (@types) {
 3471:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 3472:                 }
 3473:             }
 3474:         } else {
 3475:             foreach my $type (@types) {
 3476:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3477:             }
 3478:         }
 3479:         if (!$currdefresponder) {
 3480:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 3481:         } elsif ($currdefresponder < 1) {
 3482:             $currdefresponder = 1;
 3483:         }
 3484:         foreach my $type (@types) {
 3485:             if ($curruploadquota{$type} eq '') {
 3486:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 3487:             }
 3488:         }
 3489:         $datatable .=
 3490:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3491:                 $choices{'anonsurvey_threshold'}.
 3492:                 '</span></td>'.
 3493:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 3494:                 '<input type="text" name="anonsurvey_threshold"'.
 3495:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 3496:                 '</td></tr>'."\n";
 3497:         $itemcount ++;
 3498:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3499:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3500:                       $choices{'uploadquota'}.
 3501:                       '</span></td>'.
 3502:                       '<td align="right" class="LC_right_item">'.
 3503:                       '<table><tr>';
 3504:         foreach my $type (@types) {
 3505:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 3506:                            '<input type="text" name="uploadquota_'.$type.'"'.
 3507:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 3508:         }
 3509:         $datatable .= '</tr></table></td></tr>'."\n";
 3510:         $itemcount ++;
 3511:         my $onclick = "toggleDisplay(this.form,'credits');";
 3512:         my $display = 'none';
 3513:         if ($currusecredits) {
 3514:             $display = 'block';
 3515:         }
 3516:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 3517:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 3518:         foreach my $type (@types) {
 3519:             next if ($type eq 'community');
 3520:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 3521:                            '<input type="text" name="'.$type.'_credits"'.
 3522:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 3523:         }
 3524:         $additional .= '</tr></table></div>'."\n";
 3525:         %defaultchecked = ('coursecredits' => 'off');
 3526:         @toggles = ('coursecredits');
 3527:         my $current = {
 3528:                         'coursecredits' => $currusecredits,
 3529:                       };
 3530:         (my $table,$itemcount) =
 3531:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 3532:                                \%choices,$itemcount,$onclick,$additional,'left');
 3533:         $datatable .= $table;
 3534:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 3535:         my $display = 'none';
 3536:         if ($postsubmitclient) {
 3537:             $display = 'block';
 3538:         }
 3539:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 3540:                       &mt('Number of seconds submit is disabled').'<br />'.
 3541:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 3542:                       '<table><tr>';
 3543:         foreach my $type (@types) {
 3544:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 3545:                            '<input type="text" name="'.$type.'_timeout" value="'.
 3546:                            $deftimeout{$type}.'" size="5" /></td>';
 3547:         }
 3548:         $additional .= '</tr></table></div>'."\n";
 3549:         %defaultchecked = ('postsubmit' => 'on');
 3550:         @toggles = ('postsubmit');
 3551:         $current = {
 3552:                        'postsubmit' => $postsubmitclient,
 3553:                    };
 3554:         ($table,$itemcount) =
 3555:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 3556:                                \%choices,$itemcount,$onclick,$additional,'left');
 3557:         $datatable .= $table;
 3558:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3559:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3560:                       $choices{'mysqltables'}.
 3561:                       '</span></td>'.
 3562:                       '<td align="right" class="LC_right_item">'.
 3563:                       '<table><tr>';
 3564:         foreach my $type (@types) {
 3565:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 3566:                            '<input type="text" name="mysqltables_'.$type.'"'.
 3567:                            ' value="'.$currmysql{$type}.'" size="5" /></td>';
 3568:         }
 3569:         $datatable .= '</tr></table></td></tr>'."\n";
 3570:         $itemcount ++;
 3571: 
 3572:     }
 3573:     $$rowtotal += $itemcount;
 3574:     return $datatable;
 3575: }
 3576: 
 3577: sub print_selfenrollment {
 3578:     my ($position,$dom,$settings,$rowtotal) = @_;
 3579:     my ($css_class,$datatable);
 3580:     my $itemcount = 1;
 3581:     my @types = ('official','unofficial','community','textbook');
 3582:     if (($position eq 'top') || ($position eq 'middle')) {
 3583:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 3584:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 3585:         my @rows;
 3586:         my $key;
 3587:         if ($position eq 'top') {
 3588:             $key = 'admin'; 
 3589:             if (ref($rowsref) eq 'ARRAY') {
 3590:                 @rows = @{$rowsref};
 3591:             }
 3592:         } elsif ($position eq 'middle') {
 3593:             $key = 'default';
 3594:             @rows = ('types','registered','approval','limit');
 3595:         }
 3596:         foreach my $row (@rows) {
 3597:             if (defined($titlesref->{$row})) {
 3598:                 $itemcount ++;
 3599:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3600:                 $datatable .= '<tr'.$css_class.'>'.
 3601:                               '<td>'.$titlesref->{$row}.'</td>'.
 3602:                               '<td class="LC_left_item">'.
 3603:                               '<table><tr>';
 3604:                 my (%current,%currentcap);
 3605:                 if (ref($settings) eq 'HASH') {
 3606:                     if (ref($settings->{$key}) eq 'HASH') {
 3607:                         foreach my $type (@types) {
 3608:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 3609:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 3610:                             }
 3611:                             if (($row eq 'limit') && ($key eq 'default')) {
 3612:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 3613:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 3614:                                 }
 3615:                             }
 3616:                         }
 3617:                     }
 3618:                 }
 3619:                 my %roles = (
 3620:                              '0' => &Apache::lonnet::plaintext('dc'),
 3621:                             ); 
 3622:             
 3623:                 foreach my $type (@types) {
 3624:                     unless (($row eq 'registered') && ($key eq 'default')) {
 3625:                         $datatable .= '<th>'.&mt($type).'</th>';
 3626:                     }
 3627:                 }
 3628:                 unless (($row eq 'registered') && ($key eq 'default')) {
 3629:                     $datatable .= '</tr><tr>';
 3630:                 }
 3631:                 foreach my $type (@types) {
 3632:                     if ($type eq 'community') {
 3633:                         $roles{'1'} = &mt('Community personnel');
 3634:                     } else {
 3635:                         $roles{'1'} = &mt('Course personnel');
 3636:                     }
 3637:                     $datatable .= '<td style="vertical-align: top">';
 3638:                     if ($position eq 'top') {
 3639:                         my %checked;
 3640:                         if ($current{$type} eq '0') {
 3641:                             $checked{'0'} = ' checked="checked"';
 3642:                         } else {
 3643:                             $checked{'1'} = ' checked="checked"';
 3644:                         }
 3645:                         foreach my $role ('1','0') {
 3646:                             $datatable .= '<span class="LC_nobreak"><label>'.
 3647:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 3648:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 3649:                                           $roles{$role}.'</label></span> ';
 3650:                         }
 3651:                     } else {
 3652:                         if ($row eq 'types') {
 3653:                             my %checked;
 3654:                             if ($current{$type} =~ /^(all|dom)$/) {
 3655:                                 $checked{$1} = ' checked="checked"';
 3656:                             } else {
 3657:                                 $checked{''} = ' checked="checked"';
 3658:                             }
 3659:                             foreach my $val ('','dom','all') {
 3660:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3661:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3662:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3663:                             }
 3664:                         } elsif ($row eq 'registered') {
 3665:                             my %checked;
 3666:                             if ($current{$type} eq '1') {
 3667:                                 $checked{'1'} = ' checked="checked"';
 3668:                             } else {
 3669:                                 $checked{'0'} = ' checked="checked"';
 3670:                             }
 3671:                             foreach my $val ('0','1') {
 3672:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3673:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3674:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3675:                             }
 3676:                         } elsif ($row eq 'approval') {
 3677:                             my %checked;
 3678:                             if ($current{$type} =~ /^([12])$/) {
 3679:                                 $checked{$1} = ' checked="checked"';
 3680:                             } else {
 3681:                                 $checked{'0'} = ' checked="checked"';
 3682:                             }
 3683:                             for my $val (0..2) {
 3684:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3685:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3686:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3687:                             }
 3688:                         } elsif ($row eq 'limit') {
 3689:                             my %checked;
 3690:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 3691:                                 $checked{$1} = ' checked="checked"';
 3692:                             } else {
 3693:                                 $checked{'none'} = ' checked="checked"';
 3694:                             }
 3695:                             my $cap;
 3696:                             if ($currentcap{$type} =~ /^\d+$/) {
 3697:                                 $cap = $currentcap{$type};
 3698:                             }
 3699:                             foreach my $val ('none','allstudents','selfenrolled') {
 3700:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3701:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3702:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3703:                             }
 3704:                             $datatable .= '<br />'.
 3705:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 3706:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 3707:                                           '</span>'; 
 3708:                         }
 3709:                     }
 3710:                     $datatable .= '</td>';
 3711:                 }
 3712:                 $datatable .= '</tr>';
 3713:             }
 3714:             $datatable .= '</table></td></tr>';
 3715:         }
 3716:     } elsif ($position eq 'bottom') {
 3717:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 3718:     }
 3719:     $$rowtotal += $itemcount;
 3720:     return $datatable;
 3721: }
 3722: 
 3723: sub print_validation_rows {
 3724:     my ($caller,$dom,$settings,$rowtotal) = @_;
 3725:     my ($itemsref,$namesref,$fieldsref);
 3726:     if ($caller eq 'selfenroll') { 
 3727:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 3728:     } elsif ($caller eq 'requestcourses') {
 3729:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 3730:     }
 3731:     my %currvalidation;
 3732:     if (ref($settings) eq 'HASH') {
 3733:         if (ref($settings->{'validation'}) eq 'HASH') {
 3734:             %currvalidation = %{$settings->{'validation'}};
 3735:         }
 3736:     }
 3737:     my $datatable;
 3738:     my $itemcount = 0;
 3739:     foreach my $item (@{$itemsref}) {
 3740:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3741:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3742:                       $namesref->{$item}.
 3743:                       '</span></td>'.
 3744:                       '<td class="LC_left_item">';
 3745:         if (($item eq 'url') || ($item eq 'button')) {
 3746:             $datatable .= '<span class="LC_nobreak">'.
 3747:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 3748:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 3749:         } elsif ($item eq 'fields') {
 3750:             my @currfields;
 3751:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 3752:                 @currfields = @{$currvalidation{$item}};
 3753:             }
 3754:             foreach my $field (@{$fieldsref}) {
 3755:                 my $check = '';
 3756:                 if (grep(/^\Q$field\E$/,@currfields)) {
 3757:                     $check = ' checked="checked"';
 3758:                 }
 3759:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3760:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 3761:                               ' value="'.$field.'"'.$check.' />'.$field.
 3762:                               '</label></span> ';
 3763:             }
 3764:         } elsif ($item eq 'markup') {
 3765:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5" wrap="soft">'.
 3766:                            $currvalidation{$item}.
 3767:                               '</textarea>';
 3768:         }
 3769:         $datatable .= '</td></tr>'."\n";
 3770:         if (ref($rowtotal)) {
 3771:             $itemcount ++;
 3772:         }
 3773:     }
 3774:     if ($caller eq 'requestcourses') {
 3775:         my %currhash;
 3776:         if (ref($settings) eq 'HASH') {
 3777:             if (ref($settings->{'validation'}) eq 'HASH') {
 3778:                 if ($settings->{'validation'}{'dc'} ne '') {
 3779:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 3780:                 }
 3781:             }
 3782:         }
 3783:         my $numinrow = 2;
 3784:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 3785:                                                        'validationdc',%currhash);
 3786:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3787:         $datatable .= '</td></tr><tr'.$css_class.'><td>';
 3788:         if ($numdc > 1) {
 3789:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 3790:         } else {
 3791:             $datatable .=  &mt('Course creation processed as: ');
 3792:         }
 3793:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 3794:         $itemcount ++;
 3795:     }
 3796:     if (ref($rowtotal)) {
 3797:         $$rowtotal += $itemcount;
 3798:     }
 3799:     return $datatable;
 3800: }
 3801: 
 3802: sub print_usersessions {
 3803:     my ($position,$dom,$settings,$rowtotal) = @_;
 3804:     my ($css_class,$datatable,%checked,%choices);
 3805:     my (%by_ip,%by_location,@intdoms);
 3806:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 3807: 
 3808:     my @alldoms = &Apache::lonnet::all_domains();
 3809:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 3810:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3811:     my %altids = &id_for_thisdom(%servers);
 3812:     my $itemcount = 1;
 3813:     if ($position eq 'top') {
 3814:         if (keys(%serverhomes) > 1) {
 3815:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 3816:             my $curroffloadnow;
 3817:             if (ref($settings) eq 'HASH') {
 3818:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 3819:                     $curroffloadnow = $settings->{'offloadnow'};
 3820:                 }
 3821:             }
 3822:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$curroffloadnow,$rowtotal);
 3823:         } else {
 3824:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3825:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 3826:         }
 3827:     } else {
 3828:         if (keys(%by_location) == 0) {
 3829:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3830:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 3831:         } else {
 3832:             my %lt = &usersession_titles();
 3833:             my $numinrow = 5;
 3834:             my $prefix;
 3835:             my @types;
 3836:             if ($position eq 'bottom') {
 3837:                 $prefix = 'remote';
 3838:                 @types = ('version','excludedomain','includedomain');
 3839:             } else {
 3840:                 $prefix = 'hosted';
 3841:                 @types = ('excludedomain','includedomain');
 3842:             }
 3843:             my (%current,%checkedon,%checkedoff);
 3844:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 3845:             my @locations = sort(keys(%by_location));
 3846:             foreach my $type (@types) {
 3847:                 $checkedon{$type} = '';
 3848:                 $checkedoff{$type} = ' checked="checked"';
 3849:             }
 3850:             if (ref($settings) eq 'HASH') {
 3851:                 if (ref($settings->{$prefix}) eq 'HASH') {
 3852:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 3853:                         $current{$key} = $settings->{$prefix}{$key};
 3854:                         if ($key eq 'version') {
 3855:                             if ($current{$key} ne '') {
 3856:                                 $checkedon{$key} = ' checked="checked"';
 3857:                                 $checkedoff{$key} = '';
 3858:                             }
 3859:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 3860:                             $checkedon{$key} = ' checked="checked"';
 3861:                             $checkedoff{$key} = '';
 3862:                         }
 3863:                     }
 3864:                 }
 3865:             }
 3866:             foreach my $type (@types) {
 3867:                 next if ($type ne 'version' && !@locations);
 3868:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3869:                 $datatable .= '<tr'.$css_class.'>
 3870:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 3871:                                <span class="LC_nobreak">&nbsp;
 3872:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 3873:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 3874:                 if ($type eq 'version') {
 3875:                     my $selector = '<select name="'.$prefix.'_version">';
 3876:                     foreach my $version (@lcversions) {
 3877:                         my $selected = '';
 3878:                         if ($current{'version'} eq $version) {
 3879:                             $selected = ' selected="selected"';
 3880:                         }
 3881:                         $selector .= ' <option value="'.$version.'"'.
 3882:                                      $selected.'>'.$version.'</option>';
 3883:                     }
 3884:                     $selector .= '</select> ';
 3885:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 3886:                 } else {
 3887:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 3888:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 3889:                                  ' />'.('&nbsp;'x2).
 3890:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 3891:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 3892:                                  "\n".
 3893:                                  '</div><div><table>';
 3894:                     my $rem;
 3895:                     for (my $i=0; $i<@locations; $i++) {
 3896:                         my ($showloc,$value,$checkedtype);
 3897:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 3898:                             my $ip = $by_location{$locations[$i]}->[0];
 3899:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 3900:                                  $value = join(':',@{$by_ip{$ip}});
 3901:                                 $showloc = join(', ',@{$by_ip{$ip}});
 3902:                                 if (ref($current{$type}) eq 'ARRAY') {
 3903:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 3904:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 3905:                                             $checkedtype = ' checked="checked"';
 3906:                                             last;
 3907:                                         }
 3908:                                     }
 3909:                                 }
 3910:                             }
 3911:                         }
 3912:                         $rem = $i%($numinrow);
 3913:                         if ($rem == 0) {
 3914:                             if ($i > 0) {
 3915:                                 $datatable .= '</tr>';
 3916:                             }
 3917:                             $datatable .= '<tr>';
 3918:                         }
 3919:                         $datatable .= '<td class="LC_left_item">'.
 3920:                                       '<span class="LC_nobreak"><label>'.
 3921:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 3922:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 3923:                                       '</label></span></td>';
 3924:                     }
 3925:                     $rem = @locations%($numinrow);
 3926:                     my $colsleft = $numinrow - $rem;
 3927:                     if ($colsleft > 1 ) {
 3928:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3929:                                       '&nbsp;</td>';
 3930:                     } elsif ($colsleft == 1) {
 3931:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 3932:                     }
 3933:                     $datatable .= '</tr></table>';
 3934:                 }
 3935:                 $datatable .= '</td></tr>';
 3936:                 $itemcount ++;
 3937:             }
 3938:         }
 3939:     }
 3940:     $$rowtotal += $itemcount;
 3941:     return $datatable;
 3942: }
 3943: 
 3944: sub build_location_hashes {
 3945:     my ($intdoms,$by_ip,$by_location) = @_;
 3946:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 3947:                   (ref($by_location) eq 'HASH')); 
 3948:     my %iphost = &Apache::lonnet::get_iphost();
 3949:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 3950:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 3951:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 3952:         foreach my $id (@{$iphost{$primary_ip}}) {
 3953:             my $intdom = &Apache::lonnet::internet_dom($id);
 3954:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 3955:                 push(@{$intdoms},$intdom);
 3956:             }
 3957:         }
 3958:     }
 3959:     foreach my $ip (keys(%iphost)) {
 3960:         if (ref($iphost{$ip}) eq 'ARRAY') {
 3961:             foreach my $id (@{$iphost{$ip}}) {
 3962:                 my $location = &Apache::lonnet::internet_dom($id);
 3963:                 if ($location) {
 3964:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 3965:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3966:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 3967:                             push(@{$by_ip->{$ip}},$location);
 3968:                         }
 3969:                     } else {
 3970:                         $by_ip->{$ip} = [$location];
 3971:                     }
 3972:                 }
 3973:             }
 3974:         }
 3975:     }
 3976:     foreach my $ip (sort(keys(%{$by_ip}))) {
 3977:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3978:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 3979:             my $first = $by_ip->{$ip}->[0];
 3980:             if (ref($by_location->{$first}) eq 'ARRAY') {
 3981:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 3982:                     push(@{$by_location->{$first}},$ip);
 3983:                 }
 3984:             } else {
 3985:                 $by_location->{$first} = [$ip];
 3986:             }
 3987:         }
 3988:     }
 3989:     return;
 3990: }
 3991: 
 3992: sub current_offloads_to {
 3993:     my ($dom,$settings,$servers) = @_;
 3994:     my (%spareid,%otherdomconfigs);
 3995:     if (ref($servers) eq 'HASH') {
 3996:         foreach my $lonhost (sort(keys(%{$servers}))) {
 3997:             my $gotspares;
 3998:             if (ref($settings) eq 'HASH') {
 3999:                 if (ref($settings->{'spares'}) eq 'HASH') {
 4000:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 4001:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 4002:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 4003:                         $gotspares = 1;
 4004:                     }
 4005:                 }
 4006:             }
 4007:             unless ($gotspares) {
 4008:                 my $gotspares;
 4009:                 my $serverhomeID =
 4010:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 4011:                 my $serverhomedom =
 4012:                     &Apache::lonnet::host_domain($serverhomeID);
 4013:                 if ($serverhomedom ne $dom) {
 4014:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 4015:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4016:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4017:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4018:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4019:                                 $gotspares = 1;
 4020:                             }
 4021:                         }
 4022:                     } else {
 4023:                         $otherdomconfigs{$serverhomedom} =
 4024:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 4025:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 4026:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4027:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4028:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 4029:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4030:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4031:                                         $gotspares = 1;
 4032:                                     }
 4033:                                 }
 4034:                             }
 4035:                         }
 4036:                     }
 4037:                 }
 4038:             }
 4039:             unless ($gotspares) {
 4040:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4041:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4042:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4043:                } else {
 4044:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 4045:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 4046:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4047:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4048:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4049:                     } else {
 4050:                         my %what = (
 4051:                              spareid => 1,
 4052:                         );
 4053:                         my ($result,$returnhash) = 
 4054:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 4055:                         if ($result eq 'ok') { 
 4056:                             if (ref($returnhash) eq 'HASH') {
 4057:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 4058:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 4059:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 4060:                                 }
 4061:                             }
 4062:                         }
 4063:                     }
 4064:                 }
 4065:             }
 4066:         }
 4067:     }
 4068:     return %spareid;
 4069: }
 4070: 
 4071: sub spares_row {
 4072:     my ($dom,$servers,$spareid,$serverhomes,$altids,$curroffloadnow,$rowtotal) = @_;
 4073:     my $css_class;
 4074:     my $numinrow = 4;
 4075:     my $itemcount = 1;
 4076:     my $datatable;
 4077:     my %typetitles = &sparestype_titles();
 4078:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 4079:         foreach my $server (sort(keys(%{$servers}))) {
 4080:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 4081:             my ($othercontrol,$serverdom);
 4082:             if ($serverhome ne $server) {
 4083:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 4084:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 4085:             } else {
 4086:                 $serverdom = &Apache::lonnet::host_domain($server);
 4087:                 if ($serverdom ne $dom) {
 4088:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 4089:                 }
 4090:             }
 4091:             next unless (ref($spareid->{$server}) eq 'HASH');
 4092:             my $checkednow;
 4093:             if (ref($curroffloadnow) eq 'HASH') {
 4094:                 if ($curroffloadnow->{$server}) {
 4095:                     $checkednow = ' checked="checked"';
 4096:                 }
 4097:             }
 4098:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4099:             $datatable .= '<tr'.$css_class.'>
 4100:                            <td rowspan="2">
 4101:                             <span class="LC_nobreak">'.
 4102:                           &mt('[_1] when busy, offloads to:'
 4103:                               ,'<b>'.$server.'</b>').'</span><br />'.
 4104:                           '<span class="LC_nobreak">'."\n".
 4105:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 4106:                           '&nbsp;'.&mt('Switch active users on next access').'</label></span>'.
 4107:                           "\n";
 4108:             my (%current,%canselect);
 4109:             my @choices = 
 4110:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 4111:             foreach my $type ('primary','default') {
 4112:                 if (ref($spareid->{$server}) eq 'HASH') {
 4113:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 4114:                         my @spares = @{$spareid->{$server}{$type}};
 4115:                         if (@spares > 0) {
 4116:                             if ($othercontrol) {
 4117:                                 $current{$type} = join(', ',@spares);
 4118:                             } else {
 4119:                                 $current{$type} .= '<table>';
 4120:                                 my $numspares = scalar(@spares);
 4121:                                 for (my $i=0;  $i<@spares; $i++) {
 4122:                                     my $rem = $i%($numinrow);
 4123:                                     if ($rem == 0) {
 4124:                                         if ($i > 0) {
 4125:                                             $current{$type} .= '</tr>';
 4126:                                         }
 4127:                                         $current{$type} .= '<tr>';
 4128:                                     }
 4129:                                     $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;'.
 4130:                                                        $spareid->{$server}{$type}[$i].
 4131:                                                        '</label></td>'."\n";
 4132:                                 }
 4133:                                 my $rem = @spares%($numinrow);
 4134:                                 my $colsleft = $numinrow - $rem;
 4135:                                 if ($colsleft > 1 ) {
 4136:                                     $current{$type} .= '<td colspan="'.$colsleft.
 4137:                                                        '" class="LC_left_item">'.
 4138:                                                        '&nbsp;</td>';
 4139:                                 } elsif ($colsleft == 1) {
 4140:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 4141:                                 }
 4142:                                 $current{$type} .= '</tr></table>';
 4143:                             }
 4144:                         }
 4145:                     }
 4146:                     if ($current{$type} eq '') {
 4147:                         $current{$type} = &mt('None specified');
 4148:                     }
 4149:                     if ($othercontrol) {
 4150:                         if ($type eq 'primary') {
 4151:                             $canselect{$type} = $othercontrol;
 4152:                         }
 4153:                     } else {
 4154:                         $canselect{$type} = 
 4155:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 4156:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 4157:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 4158:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 4159:                         if (@choices > 0) {
 4160:                             foreach my $lonhost (@choices) {
 4161:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 4162:                             }
 4163:                         }
 4164:                         $canselect{$type} .= '</select>'."\n";
 4165:                     }
 4166:                 } else {
 4167:                     $current{$type} = &mt('Could not be determined');
 4168:                     if ($type eq 'primary') {
 4169:                         $canselect{$type} =  $othercontrol;
 4170:                     }
 4171:                 }
 4172:                 if ($type eq 'default') {
 4173:                     $datatable .= '<tr'.$css_class.'>';
 4174:                 }
 4175:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 4176:                               '<td>'.$current{$type}.'</td>'."\n".
 4177:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 4178:             }
 4179:             $itemcount ++;
 4180:         }
 4181:     }
 4182:     $$rowtotal += $itemcount;
 4183:     return $datatable;
 4184: }
 4185: 
 4186: sub possible_newspares {
 4187:     my ($server,$currspares,$serverhomes,$altids) = @_;
 4188:     my $serverhostname = &Apache::lonnet::hostname($server);
 4189:     my %excluded;
 4190:     if ($serverhostname ne '') {
 4191:         %excluded = (
 4192:                        $serverhostname => 1,
 4193:                     );
 4194:     }
 4195:     if (ref($currspares) eq 'HASH') {
 4196:         foreach my $type (keys(%{$currspares})) {
 4197:             if (ref($currspares->{$type}) eq 'ARRAY') {
 4198:                 if (@{$currspares->{$type}} > 0) {
 4199:                     foreach my $curr (@{$currspares->{$type}}) {
 4200:                         my $hostname = &Apache::lonnet::hostname($curr);
 4201:                         $excluded{$hostname} = 1;
 4202:                     }
 4203:                 }
 4204:             }
 4205:         }
 4206:     }
 4207:     my @choices;
 4208:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 4209:         if (keys(%{$serverhomes}) > 1) {
 4210:             foreach my $name (sort(keys(%{$serverhomes}))) {
 4211:                 unless ($excluded{$name}) {
 4212:                     if (exists($altids->{$serverhomes->{$name}})) {
 4213:                         push(@choices,$altids->{$serverhomes->{$name}});
 4214:                     } else {
 4215:                         push(@choices,$serverhomes->{$name});
 4216:                     }
 4217:                 }
 4218:             }
 4219:         }
 4220:     }
 4221:     return sort(@choices);
 4222: }
 4223: 
 4224: sub print_loadbalancing {
 4225:     my ($dom,$settings,$rowtotal) = @_;
 4226:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 4227:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 4228:     my $numinrow = 1;
 4229:     my $datatable;
 4230:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4231:     my (%currbalancer,%currtargets,%currrules,%existing);
 4232:     if (ref($settings) eq 'HASH') {
 4233:         %existing = %{$settings};
 4234:     }
 4235:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 4236:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 4237:                                   \%currtargets,\%currrules);
 4238:     } else {
 4239:         return;
 4240:     }
 4241:     my ($othertitle,$usertypes,$types) =
 4242:         &Apache::loncommon::sorted_inst_types($dom);
 4243:     my $rownum = 8;
 4244:     if (ref($types) eq 'ARRAY') {
 4245:         $rownum += scalar(@{$types});
 4246:     }
 4247:     my @css_class = ('LC_odd_row','LC_even_row');
 4248:     my $balnum = 0;
 4249:     my $islast;
 4250:     my (@toshow,$disabledtext);
 4251:     if (keys(%currbalancer) > 0) {
 4252:         @toshow = sort(keys(%currbalancer));
 4253:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 4254:             push(@toshow,'');
 4255:         }
 4256:     } else {
 4257:         @toshow = ('');
 4258:         $disabledtext = &mt('No existing load balancer');
 4259:     }
 4260:     foreach my $lonhost (@toshow) {
 4261:         if ($balnum == scalar(@toshow)-1) {
 4262:             $islast = 1;
 4263:         } else {
 4264:             $islast = 0;
 4265:         }
 4266:         my $cssidx = $balnum%2;
 4267:         my $targets_div_style = 'display: none';
 4268:         my $disabled_div_style = 'display: block';
 4269:         my $homedom_div_style = 'display: none';
 4270:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 4271:                       '<td rowspan="'.$rownum.'" valign="top">'.
 4272:                       '<p>';
 4273:         if ($lonhost eq '') {
 4274:             $datatable .= '<span class="LC_nobreak">';
 4275:             if (keys(%currbalancer) > 0) {
 4276:                 $datatable .= &mt('Add balancer:');
 4277:             } else {
 4278:                 $datatable .= &mt('Enable balancer:');
 4279:             }
 4280:             $datatable .= '&nbsp;'.
 4281:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 4282:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 4283:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 4284:                           '<option value="" selected="selected">'.&mt('None').
 4285:                           '</option>'."\n";
 4286:             foreach my $server (sort(keys(%servers))) {
 4287:                 next if ($currbalancer{$server});
 4288:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 4289:             }
 4290:             $datatable .=
 4291:                 '</select>'."\n".
 4292:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 4293:         } else {
 4294:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 4295:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 4296:                            &mt('Stop balancing').'</label>'.
 4297:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 4298:             $targets_div_style = 'display: block';
 4299:             $disabled_div_style = 'display: none';
 4300:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 4301:                 $homedom_div_style = 'display: block';
 4302:             }
 4303:         }
 4304:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 4305:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 4306:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 4307:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 4308:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 4309:         my @sparestypes = ('primary','default');
 4310:         my %typetitles = &sparestype_titles();
 4311:         my %hostherechecked = (
 4312:                                   no => ' checked="checked"',
 4313:                               );
 4314:         foreach my $sparetype (@sparestypes) {
 4315:             my $targettable;
 4316:             for (my $i=0; $i<$numspares; $i++) {
 4317:                 my $checked;
 4318:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 4319:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 4320:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 4321:                             $checked = ' checked="checked"';
 4322:                         }
 4323:                     }
 4324:                 }
 4325:                 my ($chkboxval,$disabled);
 4326:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 4327:                     $chkboxval = $spares[$i];
 4328:                 }
 4329:                 if (exists($currbalancer{$spares[$i]})) {
 4330:                     $disabled = ' disabled="disabled"';
 4331:                 }
 4332:                 $targettable .=
 4333:                     '<td><span class="LC_nobreak"><label>'.
 4334:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 4335:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 4336:                     '</span></label></span></td>';
 4337:                 my $rem = $i%($numinrow);
 4338:                 if ($rem == 0) {
 4339:                     if (($i > 0) && ($i < $numspares-1)) {
 4340:                         $targettable .= '</tr>';
 4341:                     }
 4342:                     if ($i < $numspares-1) {
 4343:                         $targettable .= '<tr>';
 4344:                     }
 4345:                 }
 4346:             }
 4347:             if ($targettable ne '') {
 4348:                 my $rem = $numspares%($numinrow);
 4349:                 my $colsleft = $numinrow - $rem;
 4350:                 if ($colsleft > 1 ) {
 4351:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4352:                                     '&nbsp;</td>';
 4353:                 } elsif ($colsleft == 1) {
 4354:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 4355:                 }
 4356:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 4357:                                '<table><tr>'.$targettable.'</tr></table><br />';
 4358:             }
 4359:             $hostherechecked{$sparetype} = '';
 4360:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 4361:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 4362:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 4363:                         $hostherechecked{$sparetype} = ' checked="checked"';
 4364:                         $hostherechecked{'no'} = '';
 4365:                     }
 4366:                 }
 4367:             }
 4368:         }
 4369:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 4370:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 4371:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 4372:         foreach my $sparetype (@sparestypes) {
 4373:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 4374:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 4375:                           '</i></label><br />';
 4376:         }
 4377:         $datatable .= '</div></td></tr>'.
 4378:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 4379:                                            $othertitle,$usertypes,$types,\%servers,
 4380:                                            \%currbalancer,$lonhost,
 4381:                                            $targets_div_style,$homedom_div_style,
 4382:                                            $css_class[$cssidx],$balnum,$islast);
 4383:         $$rowtotal += $rownum;
 4384:         $balnum ++;
 4385:     }
 4386:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 4387:     return $datatable;
 4388: }
 4389: 
 4390: sub get_loadbalancers_config {
 4391:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 4392:     return unless ((ref($servers) eq 'HASH') &&
 4393:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 4394:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 4395:     if (keys(%{$existing}) > 0) {
 4396:         my $oldlonhost;
 4397:         foreach my $key (sort(keys(%{$existing}))) {
 4398:             if ($key eq 'lonhost') {
 4399:                 $oldlonhost = $existing->{'lonhost'};
 4400:                 $currbalancer->{$oldlonhost} = 1;
 4401:             } elsif ($key eq 'targets') {
 4402:                 if ($oldlonhost) {
 4403:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 4404:                 }
 4405:             } elsif ($key eq 'rules') {
 4406:                 if ($oldlonhost) {
 4407:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 4408:                 }
 4409:             } elsif (ref($existing->{$key}) eq 'HASH') {
 4410:                 $currbalancer->{$key} = 1;
 4411:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 4412:                 $currrules->{$key} = $existing->{$key}{'rules'};
 4413:             }
 4414:         }
 4415:     } else {
 4416:         my ($balancerref,$targetsref) =
 4417:                 &Apache::lonnet::get_lonbalancer_config($servers);
 4418:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 4419:             foreach my $server (sort(keys(%{$balancerref}))) {
 4420:                 $currbalancer->{$server} = 1;
 4421:                 $currtargets->{$server} = $targetsref->{$server};
 4422:             }
 4423:         }
 4424:     }
 4425:     return;
 4426: }
 4427: 
 4428: sub loadbalancing_rules {
 4429:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 4430:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 4431:         $css_class,$balnum,$islast) = @_;
 4432:     my $output;
 4433:     my $num = 0;
 4434:     my ($alltypes,$othertypes,$titles) =
 4435:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 4436:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 4437:         foreach my $type (@{$alltypes}) {
 4438:             $num ++;
 4439:             my $current;
 4440:             if (ref($currrules) eq 'HASH') {
 4441:                 $current = $currrules->{$type};
 4442:             }
 4443:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 4444:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 4445:                     $current = '';
 4446:                 }
 4447:             }
 4448:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 4449:                                              $servers,$currbalancer,$lonhost,$dom,
 4450:                                              $targets_div_style,$homedom_div_style,
 4451:                                              $css_class,$balnum,$num,$islast);
 4452:         }
 4453:     }
 4454:     return $output;
 4455: }
 4456: 
 4457: sub loadbalancing_titles {
 4458:     my ($dom,$intdom,$usertypes,$types) = @_;
 4459:     my %othertypes = (
 4460:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 4461:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 4462:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 4463:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 4464:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 4465:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 4466:                      );
 4467:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 4468:     if (ref($types) eq 'ARRAY') {
 4469:         unshift(@alltypes,@{$types},'default');
 4470:     }
 4471:     my %titles;
 4472:     foreach my $type (@alltypes) {
 4473:         if ($type =~ /^_LC_/) {
 4474:             $titles{$type} = $othertypes{$type};
 4475:         } elsif ($type eq 'default') {
 4476:             $titles{$type} = &mt('All users from [_1]',$dom);
 4477:             if (ref($types) eq 'ARRAY') {
 4478:                 if (@{$types} > 0) {
 4479:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 4480:                 }
 4481:             }
 4482:         } elsif (ref($usertypes) eq 'HASH') {
 4483:             $titles{$type} = $usertypes->{$type};
 4484:         }
 4485:     }
 4486:     return (\@alltypes,\%othertypes,\%titles);
 4487: }
 4488: 
 4489: sub loadbalance_rule_row {
 4490:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 4491:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 4492:     my @rulenames;
 4493:     my %ruletitles = &offloadtype_text();
 4494:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 4495:         @rulenames = ('balancer','offloadedto','specific');
 4496:     } else {
 4497:         @rulenames = ('default','homeserver');
 4498:         if ($type eq '_LC_external') {
 4499:             push(@rulenames,'externalbalancer');
 4500:         } else {
 4501:             push(@rulenames,'specific');
 4502:         }
 4503:         push(@rulenames,'none');
 4504:     }
 4505:     my $style = $targets_div_style;
 4506:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 4507:         $style = $homedom_div_style;
 4508:     }
 4509:     my $space;
 4510:     if ($islast && $num == 1) {
 4511:         $space = '<div display="inline-block">&nbsp;</div>';
 4512:     }
 4513:     my $output =
 4514:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 4515:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 4516:         '<td valaign="top">'.$space.
 4517:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 4518:     for (my $i=0; $i<@rulenames; $i++) {
 4519:         my $rule = $rulenames[$i];
 4520:         my ($checked,$extra);
 4521:         if ($rulenames[$i] eq 'default') {
 4522:             $rule = '';
 4523:         }
 4524:         if ($rulenames[$i] eq 'specific') {
 4525:             if (ref($servers) eq 'HASH') {
 4526:                 my $default;
 4527:                 if (($current ne '') && (exists($servers->{$current}))) {
 4528:                     $checked = ' checked="checked"';
 4529:                 }
 4530:                 unless ($checked) {
 4531:                     $default = ' selected="selected"';
 4532:                 }
 4533:                 $extra =
 4534:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 4535:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 4536:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 4537:                     '<option value=""'.$default.'></option>'."\n";
 4538:                 foreach my $server (sort(keys(%{$servers}))) {
 4539:                     if (ref($currbalancer) eq 'HASH') {
 4540:                         next if (exists($currbalancer->{$server}));
 4541:                     }
 4542:                     my $selected;
 4543:                     if ($server eq $current) {
 4544:                         $selected = ' selected="selected"';
 4545:                     }
 4546:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 4547:                 }
 4548:                 $extra .= '</select>';
 4549:             }
 4550:         } elsif ($rule eq $current) {
 4551:             $checked = ' checked="checked"';
 4552:         }
 4553:         $output .= '<span class="LC_nobreak"><label>'.
 4554:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 4555:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 4556:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 4557:                    ')"'.$checked.' />&nbsp;';
 4558:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 4559:             $output .= $ruletitles{'particular'};
 4560:         } else {
 4561:             $output .= $ruletitles{$rulenames[$i]};
 4562:         }
 4563:         $output .= '</label>'.$extra.'</span><br />'."\n";
 4564:     }
 4565:     $output .= '</div></td></tr>'."\n";
 4566:     return $output;
 4567: }
 4568: 
 4569: sub offloadtype_text {
 4570:     my %ruletitles = &Apache::lonlocal::texthash (
 4571:            'default'          => 'Offloads to default destinations',
 4572:            'homeserver'       => "Offloads to user's home server",
 4573:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 4574:            'specific'         => 'Offloads to specific server',
 4575:            'none'             => 'No offload',
 4576:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 4577:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 4578:            'particular'       => 'Session hosted (after re-auth) on server:',
 4579:     );
 4580:     return %ruletitles;
 4581: }
 4582: 
 4583: sub sparestype_titles {
 4584:     my %typestitles = &Apache::lonlocal::texthash (
 4585:                           'primary' => 'primary',
 4586:                           'default' => 'default',
 4587:                       );
 4588:     return %typestitles;
 4589: }
 4590: 
 4591: sub contact_titles {
 4592:     my %titles = &Apache::lonlocal::texthash (
 4593:                    'supportemail' => 'Support E-mail address',
 4594:                    'adminemail'   => 'Default Server Admin E-mail address',
 4595:                    'errormail'    => 'Error reports to be e-mailed to',
 4596:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 4597:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 4598:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 4599:                    'requestsmail' => 'E-mail from course requests requiring approval',
 4600:                    'updatesmail'  => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 4601:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 4602:                  );
 4603:     my %short_titles = &Apache::lonlocal::texthash (
 4604:                            adminemail   => 'Admin E-mail address',
 4605:                            supportemail => 'Support E-mail',
 4606:                        );   
 4607:     return (\%titles,\%short_titles);
 4608: }
 4609: 
 4610: sub tool_titles {
 4611:     my %titles = &Apache::lonlocal::texthash (
 4612:                      aboutme    => 'Personal web page',
 4613:                      blog       => 'Blog',
 4614:                      webdav     => 'WebDAV',
 4615:                      portfolio  => 'Portfolio',
 4616:                      official   => 'Official courses (with institutional codes)',
 4617:                      unofficial => 'Unofficial courses',
 4618:                      community  => 'Communities',
 4619:                      textbook   => 'Textbook courses',
 4620:                  );
 4621:     return %titles;
 4622: }
 4623: 
 4624: sub courserequest_titles {
 4625:     my %titles = &Apache::lonlocal::texthash (
 4626:                                    official   => 'Official',
 4627:                                    unofficial => 'Unofficial',
 4628:                                    community  => 'Communities',
 4629:                                    textbook   => 'Textbook',
 4630:                                    norequest  => 'Not allowed',
 4631:                                    approval   => 'Approval by Dom. Coord.',
 4632:                                    validate   => 'With validation',
 4633:                                    autolimit  => 'Numerical limit',
 4634:                                    unlimited  => '(blank for unlimited)',
 4635:                  );
 4636:     return %titles;
 4637: }
 4638: 
 4639: sub authorrequest_titles {
 4640:     my %titles = &Apache::lonlocal::texthash (
 4641:                                    norequest  => 'Not allowed',
 4642:                                    approval   => 'Approval by Dom. Coord.',
 4643:                                    automatic  => 'Automatic approval',
 4644:                  );
 4645:     return %titles;
 4646: }
 4647: 
 4648: sub courserequest_conditions {
 4649:     my %conditions = &Apache::lonlocal::texthash (
 4650:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 4651:        validate   => '(Processing of request subject to institutional validation).',
 4652:                  );
 4653:     return %conditions;
 4654: }
 4655: 
 4656: 
 4657: sub print_usercreation {
 4658:     my ($position,$dom,$settings,$rowtotal) = @_;
 4659:     my $numinrow = 4;
 4660:     my $datatable;
 4661:     if ($position eq 'top') {
 4662:         $$rowtotal ++;
 4663:         my $rowcount = 0;
 4664:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 4665:         if (ref($rules) eq 'HASH') {
 4666:             if (keys(%{$rules}) > 0) {
 4667:                 $datatable .= &user_formats_row('username',$settings,$rules,
 4668:                                                 $ruleorder,$numinrow,$rowcount);
 4669:                 $$rowtotal ++;
 4670:                 $rowcount ++;
 4671:             }
 4672:         }
 4673:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 4674:         if (ref($idrules) eq 'HASH') {
 4675:             if (keys(%{$idrules}) > 0) {
 4676:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 4677:                                                 $idruleorder,$numinrow,$rowcount);
 4678:                 $$rowtotal ++;
 4679:                 $rowcount ++;
 4680:             }
 4681:         }
 4682:         if ($rowcount == 0) {
 4683:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 4684:             $$rowtotal ++;
 4685:             $rowcount ++;
 4686:         }
 4687:     } elsif ($position eq 'middle') {
 4688:         my @creators = ('author','course','requestcrs');
 4689:         my ($rules,$ruleorder) =
 4690:             &Apache::lonnet::inst_userrules($dom,'username');
 4691:         my %lt = &usercreation_types();
 4692:         my %checked;
 4693:         if (ref($settings) eq 'HASH') {
 4694:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 4695:                 foreach my $item (@creators) {
 4696:                     $checked{$item} = $settings->{'cancreate'}{$item};
 4697:                 }
 4698:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 4699:                 foreach my $item (@creators) {
 4700:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 4701:                         $checked{$item} = 'none';
 4702:                     }
 4703:                 }
 4704:             }
 4705:         }
 4706:         my $rownum = 0;
 4707:         foreach my $item (@creators) {
 4708:             $rownum ++;
 4709:             if ($checked{$item} eq '') {
 4710:                 $checked{$item} = 'any';
 4711:             }
 4712:             my $css_class;
 4713:             if ($rownum%2) {
 4714:                 $css_class = '';
 4715:             } else {
 4716:                 $css_class = ' class="LC_odd_row" ';
 4717:             }
 4718:             $datatable .= '<tr'.$css_class.'>'.
 4719:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 4720:                          '</span></td><td align="right">';
 4721:             my @options = ('any');
 4722:             if (ref($rules) eq 'HASH') {
 4723:                 if (keys(%{$rules}) > 0) {
 4724:                     push(@options,('official','unofficial'));
 4725:                 }
 4726:             }
 4727:             push(@options,'none');
 4728:             foreach my $option (@options) {
 4729:                 my $type = 'radio';
 4730:                 my $check = ' ';
 4731:                 if ($checked{$item} eq $option) {
 4732:                     $check = ' checked="checked" ';
 4733:                 } 
 4734:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4735:                               '<input type="'.$type.'" name="can_createuser_'.
 4736:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 4737:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 4738:             }
 4739:             $datatable .= '</td></tr>';
 4740:         }
 4741:     } else {
 4742:         my @contexts = ('author','course','domain');
 4743:         my @authtypes = ('int','krb4','krb5','loc');
 4744:         my %checked;
 4745:         if (ref($settings) eq 'HASH') {
 4746:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 4747:                 foreach my $item (@contexts) {
 4748:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 4749:                         foreach my $auth (@authtypes) {
 4750:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 4751:                                 $checked{$item}{$auth} = ' checked="checked" ';
 4752:                             }
 4753:                         }
 4754:                     }
 4755:                 }
 4756:             }
 4757:         } else {
 4758:             foreach my $item (@contexts) {
 4759:                 foreach my $auth (@authtypes) {
 4760:                     $checked{$item}{$auth} = ' checked="checked" ';
 4761:                 }
 4762:             }
 4763:         }
 4764:         my %title = &context_names();
 4765:         my %authname = &authtype_names();
 4766:         my $rownum = 0;
 4767:         my $css_class; 
 4768:         foreach my $item (@contexts) {
 4769:             if ($rownum%2) {
 4770:                 $css_class = '';
 4771:             } else {
 4772:                 $css_class = ' class="LC_odd_row" ';
 4773:             }
 4774:             $datatable .=   '<tr'.$css_class.'>'.
 4775:                             '<td>'.$title{$item}.
 4776:                             '</td><td class="LC_left_item">'.
 4777:                             '<span class="LC_nobreak">';
 4778:             foreach my $auth (@authtypes) {
 4779:                 $datatable .= '<label>'. 
 4780:                               '<input type="checkbox" name="'.$item.'_auth" '.
 4781:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 4782:                               $authname{$auth}.'</label>&nbsp;';
 4783:             }
 4784:             $datatable .= '</span></td></tr>';
 4785:             $rownum ++;
 4786:         }
 4787:         $$rowtotal += $rownum;
 4788:     }
 4789:     return $datatable;
 4790: }
 4791: 
 4792: sub print_selfcreation {
 4793:     my ($position,$dom,$settings,$rowtotal) = @_;
 4794:     my (@selfcreate,$createsettings,$processing,$datatable);
 4795:     if (ref($settings) eq 'HASH') {
 4796:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 4797:             $createsettings = $settings->{'cancreate'};
 4798:             if (ref($createsettings) eq 'HASH') {
 4799:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 4800:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 4801:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 4802:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 4803:                         @selfcreate = ('email','login','sso');
 4804:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 4805:                         @selfcreate = ($createsettings->{'selfcreate'});
 4806:                     }
 4807:                 }
 4808:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 4809:                     $processing = $createsettings->{'selfcreateprocessing'};
 4810:                 }
 4811:             }
 4812:         }
 4813:     }
 4814:     my %radiohash;
 4815:     my $numinrow = 4;
 4816:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 4817:     if ($position eq 'top') {
 4818:         my %choices = &Apache::lonlocal::texthash (
 4819:                                                       cancreate_login      => 'Institutional Login',
 4820:                                                       cancreate_sso        => 'Institutional Single Sign On',
 4821:                                                   );
 4822:         my @toggles = sort(keys(%choices));
 4823:         my %defaultchecked = (
 4824:                                'cancreate_login' => 'off',
 4825:                                'cancreate_sso'   => 'off',
 4826:                              );
 4827:         my ($onclick,$itemcount);
 4828:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 4829:                                                      \%choices,$itemcount,$onclick);
 4830:         $$rowtotal += $itemcount;
 4831:         
 4832:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4833: 
 4834:         if (ref($usertypes) eq 'HASH') {
 4835:             if (keys(%{$usertypes}) > 0) {
 4836:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 4837:                                              $dom,$numinrow,$othertitle,
 4838:                                              'statustocreate',$$rowtotal);
 4839:                 $$rowtotal ++;
 4840:             }
 4841:         }
 4842:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 4843:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4844:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 4845:         my $rem;
 4846:         my $numperrow = 2;
 4847:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 4848:         $datatable .= '<tr'.$css_class.'>'.
 4849:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 4850:                      '<td class="LC_left_item">'."\n".
 4851:                      '<table><tr><td>'."\n";
 4852:         for (my $i=0; $i<@fields; $i++) {
 4853:             $rem = $i%($numperrow);
 4854:             if ($rem == 0) {
 4855:                 if ($i > 0) {
 4856:                     $datatable .= '</tr>';
 4857:                 }
 4858:                 $datatable .= '<tr>';
 4859:             }
 4860:             my $currval;
 4861:             if (ref($createsettings) eq 'HASH') {
 4862:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 4863:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 4864:                 }
 4865:             }
 4866:             $datatable .= '<td class="LC_left_item">'.
 4867:                           '<span class="LC_nobreak">'.
 4868:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 4869:                           'value="'.$currval.'" size="10" />&nbsp;'.
 4870:                           $fieldtitles{$fields[$i]}.'</span></td>';
 4871:         }
 4872:         my $colsleft = $numperrow - $rem;
 4873:         if ($colsleft > 1 ) {
 4874:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4875:                          '&nbsp;</td>';
 4876:         } elsif ($colsleft == 1) {
 4877:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4878:         }
 4879:         $datatable .= '</tr></table></td></tr>';
 4880:         $$rowtotal ++;
 4881:     } elsif ($position eq 'middle') {
 4882:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 4883:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4884:         $usertypes->{'default'} = $othertitle;
 4885:         if (ref($types) eq 'ARRAY') {
 4886:             push(@{$types},'default');
 4887:             $usertypes->{'default'} = $othertitle;
 4888:             foreach my $status (@{$types}) {
 4889:                 $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 4890:                                                        $numinrow,$$rowtotal,$usertypes);
 4891:                 $$rowtotal ++;
 4892:             }
 4893:         }
 4894:     } else {
 4895:         my %choices = &Apache::lonlocal::texthash (
 4896:                                                       cancreate_email => 'E-mail address as username',
 4897:                                                   );
 4898:         my @toggles = sort(keys(%choices));
 4899:         my %defaultchecked = (
 4900:                                'cancreate_email' => 'off',
 4901:                              );
 4902:         my $itemcount = 0;
 4903:         my $display = 'none';
 4904:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 4905:             $display = 'block';
 4906:         }
 4907:         my $onclick = "toggleDisplay(this.form,'emailoptions');";
 4908:         my $additional = '<div id="emailoptions" style="display: '.$display.'">';
 4909:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 4910:         my $usertypes = {};
 4911:         my $order = [];
 4912:         if ((ref($domdefaults{'inststatustypes'}) eq 'HASH') && (ref($domdefaults{'inststatusguest'}) eq 'ARRAY')) {
 4913:             $usertypes = $domdefaults{'inststatustypes'};
 4914:             $order = $domdefaults{'inststatusguest'};
 4915:         }
 4916:         if (ref($order) eq 'ARRAY') {
 4917:             push(@{$order},'default');
 4918:             if (@{$order} > 1) {
 4919:                 $usertypes->{'default'} = &mt('Other users');
 4920:                 $additional .= '<table><tr>';
 4921:                 foreach my $status (@{$order}) {
 4922:                     $additional .= '<th>'.$usertypes->{$status}.'</th>';
 4923:                 }
 4924:                 $additional .= '</tr><tr>';
 4925:                 foreach my $status (@{$order}) {
 4926:                     $additional .= '<td>'.&email_as_username($rowtotal,$processing,$status).'</td>';
 4927:                 }
 4928:                 $additional .= '</tr></table>';
 4929:             } else {
 4930:                 $usertypes->{'default'} = &mt('All users');
 4931:                 $additional .= &email_as_username($rowtotal,$processing);
 4932:             }
 4933:         }
 4934:         $additional .= '</div>'."\n";
 4935: 
 4936:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 4937:                                                      \%choices,$$rowtotal,$onclick,$additional);
 4938:         $$rowtotal ++;
 4939:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal);
 4940:         $$rowtotal ++;
 4941:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 4942:         $numinrow = 1;
 4943:         if (ref($order) eq 'ARRAY') {
 4944:             foreach my $status (@{$order}) {
 4945:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 4946:                                                        $numinrow,$$rowtotal,$usertypes,$infofields,$infotitles);
 4947:                 $$rowtotal ++;
 4948:             }
 4949:         }
 4950:         my ($emailrules,$emailruleorder) =
 4951:             &Apache::lonnet::inst_userrules($dom,'email');
 4952:         if (ref($emailrules) eq 'HASH') {
 4953:             if (keys(%{$emailrules}) > 0) {
 4954:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 4955:                                                 $emailruleorder,$numinrow,$$rowtotal);
 4956:                 $$rowtotal ++;
 4957:             }
 4958:         }
 4959:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal);
 4960:     }
 4961:     return $datatable;
 4962: }
 4963: 
 4964: sub email_as_username {
 4965:     my ($rowtotal,$processing,$type) = @_;
 4966:     my %choices =
 4967:         &Apache::lonlocal::texthash (
 4968:                                       automatic => 'Automatic approval',
 4969:                                       approval  => 'Queued for approval',
 4970:                                     );
 4971:     my $output;
 4972:     foreach my $option ('automatic','approval') {
 4973:         my $checked;
 4974:         if (ref($processing) eq 'HASH') {
 4975:             if ($type eq '') {   
 4976:                 if (!exists($processing->{'default'})) {
 4977:                     if ($option eq 'automatic') {
 4978:                         $checked = ' checked="checked"';
 4979:                     }
 4980:                 } else {
 4981:                     if ($processing->{'default'} eq $option) {
 4982:                         $checked = ' checked="checked"';
 4983:                     }
 4984:                 }
 4985:             } else {
 4986:                 if (!exists($processing->{$type})) {
 4987:                     if ($option eq 'automatic') {
 4988:                         $checked = ' checked="checked"';
 4989:                     }
 4990:                 } else {
 4991:                     if ($processing->{$type} eq $option) {
 4992:                         $checked = ' checked="checked"';
 4993:                     }
 4994:                 }
 4995:             }
 4996:         } elsif ($option eq 'automatic') {
 4997:             $checked = ' checked="checked"'; 
 4998:         }
 4999:         my $name = 'cancreate_emailprocess';
 5000:         if (($type ne '') && ($type ne 'default')) {
 5001:             $name .= '_'.$type;
 5002:         }
 5003:         $output .= '<span class="LC_nobreak"><label>'.
 5004:                    '<input type="radio" name="'.$name.'"'.
 5005:                    $checked.' value="'.$option.'" />'.
 5006:                    $choices{$option}.'</label></span>';
 5007:         if ($type eq '') {
 5008:             $output .= '&nbsp;';
 5009:         } else {
 5010:             $output .= '<br />';
 5011:         }
 5012:     }
 5013:     $$rowtotal ++;
 5014:     return $output;
 5015: }
 5016: 
 5017: sub captcha_choice {
 5018:     my ($context,$settings,$itemcount) = @_;
 5019:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 5020:         $vertext,$currver); 
 5021:     my %lt = &captcha_phrases();
 5022:     $keyentry = 'hidden';
 5023:     if ($context eq 'cancreate') {
 5024:         $rowname = &mt('CAPTCHA validation');
 5025:     } elsif ($context eq 'login') {
 5026:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 5027:     }
 5028:     if (ref($settings) eq 'HASH') {
 5029:         if ($settings->{'captcha'}) {
 5030:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 5031:         } else {
 5032:             $checked{'original'} = ' checked="checked"';
 5033:         }
 5034:         if ($settings->{'captcha'} eq 'recaptcha') {
 5035:             $pubtext = $lt{'pub'};
 5036:             $privtext = $lt{'priv'};
 5037:             $keyentry = 'text';
 5038:             $vertext = $lt{'ver'};
 5039:             $currver = $settings->{'recaptchaversion'};
 5040:             if ($currver ne '2') {
 5041:                 $currver = 1;
 5042:             }
 5043:         }
 5044:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 5045:             $currpub = $settings->{'recaptchakeys'}{'public'};
 5046:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 5047:         }
 5048:     } else {
 5049:         $checked{'original'} = ' checked="checked"';
 5050:     }
 5051:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5052:     my $output = '<tr'.$css_class.'>'.
 5053:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 5054:                  '<table><tr><td>'."\n";
 5055:     foreach my $option ('original','recaptcha','notused') {
 5056:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 5057:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 5058:                    $lt{$option}.'</label></span>';
 5059:         unless ($option eq 'notused') {
 5060:             $output .= ('&nbsp;'x2)."\n";
 5061:         }
 5062:     }
 5063: #
 5064: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 5065: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 5066: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 5067: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 5068: #
 5069:     $output .= '</td></tr>'."\n".
 5070:                '<tr><td>'."\n".
 5071:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 5072:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 5073:                $currpub.'" size="40" /></span><br />'."\n".
 5074:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 5075:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 5076:                $currpriv.'" size="40" /></span><br />'.
 5077:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 5078:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 5079:                $currver.'" size="3" /></span><br />'.
 5080:                '</td></tr></table>'."\n".
 5081:                '</td></tr>';
 5082:     return $output;
 5083: }
 5084: 
 5085: sub user_formats_row {
 5086:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 5087:     my $output;
 5088:     my %text = (
 5089:                    'username' => 'new usernames',
 5090:                    'id'       => 'IDs',
 5091:                    'email'    => 'self-created accounts (e-mail)',
 5092:                );
 5093:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 5094:     $output = '<tr '.$css_class.'>'.
 5095:               '<td><span class="LC_nobreak">';
 5096:     if ($type eq 'email') {
 5097:         $output .= &mt("Formats disallowed for $text{$type}: ");
 5098:     } else {
 5099:         $output .= &mt("Format rules to check for $text{$type}: ");
 5100:     }
 5101:     $output .= '</span></td>'.
 5102:                '<td class="LC_left_item" colspan="2"><table>';
 5103:     my $rem;
 5104:     if (ref($ruleorder) eq 'ARRAY') {
 5105:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 5106:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 5107:                 my $rem = $i%($numinrow);
 5108:                 if ($rem == 0) {
 5109:                     if ($i > 0) {
 5110:                         $output .= '</tr>';
 5111:                     }
 5112:                     $output .= '<tr>';
 5113:                 }
 5114:                 my $check = ' ';
 5115:                 if (ref($settings) eq 'HASH') {
 5116:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 5117:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 5118:                             $check = ' checked="checked" ';
 5119:                         }
 5120:                     }
 5121:                 }
 5122:                 $output .= '<td class="LC_left_item">'.
 5123:                            '<span class="LC_nobreak"><label>'.
 5124:                            '<input type="checkbox" name="'.$type.'_rule" '.
 5125:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 5126:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 5127:             }
 5128:         }
 5129:         $rem = @{$ruleorder}%($numinrow);
 5130:     }
 5131:     my $colsleft = $numinrow - $rem;
 5132:     if ($colsleft > 1 ) {
 5133:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5134:                    '&nbsp;</td>';
 5135:     } elsif ($colsleft == 1) {
 5136:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 5137:     }
 5138:     $output .= '</tr></table></td></tr>';
 5139:     return $output;
 5140: }
 5141: 
 5142: sub usercreation_types {
 5143:     my %lt = &Apache::lonlocal::texthash (
 5144:                     author     => 'When adding a co-author',
 5145:                     course     => 'When adding a user to a course',
 5146:                     requestcrs => 'When requesting a course',
 5147:                     any        => 'Any',
 5148:                     official   => 'Institutional only ',
 5149:                     unofficial => 'Non-institutional only',
 5150:                     none       => 'None',
 5151:     );
 5152:     return %lt;
 5153: }
 5154: 
 5155: sub selfcreation_types {
 5156:     my %lt = &Apache::lonlocal::texthash (
 5157:                     selfcreate => 'User creates own account',
 5158:                     any        => 'Any',
 5159:                     official   => 'Institutional only ',
 5160:                     unofficial => 'Non-institutional only',
 5161:                     email      => 'E-mail address',
 5162:                     login      => 'Institutional Login',
 5163:                     sso        => 'SSO',
 5164:              );
 5165: }
 5166: 
 5167: sub authtype_names {
 5168:     my %lt = &Apache::lonlocal::texthash(
 5169:                       int    => 'Internal',
 5170:                       krb4   => 'Kerberos 4',
 5171:                       krb5   => 'Kerberos 5',
 5172:                       loc    => 'Local',
 5173:                   );
 5174:     return %lt;
 5175: }
 5176: 
 5177: sub context_names {
 5178:     my %context_title = &Apache::lonlocal::texthash(
 5179:        author => 'Creating users when an Author',
 5180:        course => 'Creating users when in a course',
 5181:        domain => 'Creating users when a Domain Coordinator',
 5182:     );
 5183:     return %context_title;
 5184: }
 5185: 
 5186: sub print_usermodification {
 5187:     my ($position,$dom,$settings,$rowtotal) = @_;
 5188:     my $numinrow = 4;
 5189:     my ($context,$datatable,$rowcount);
 5190:     if ($position eq 'top') {
 5191:         $rowcount = 0;
 5192:         $context = 'author'; 
 5193:         foreach my $role ('ca','aa') {
 5194:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 5195:                                                    $numinrow,$rowcount);
 5196:             $$rowtotal ++;
 5197:             $rowcount ++;
 5198:         }
 5199:     } elsif ($position eq 'bottom') {
 5200:         $context = 'course';
 5201:         $rowcount = 0;
 5202:         foreach my $role ('st','ep','ta','in','cr') {
 5203:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 5204:                                                    $numinrow,$rowcount);
 5205:             $$rowtotal ++;
 5206:             $rowcount ++;
 5207:         }
 5208:     }
 5209:     return $datatable;
 5210: }
 5211: 
 5212: sub print_defaults {
 5213:     my ($position,$dom,$settings,$rowtotal) = @_;
 5214:     my $rownum = 0;
 5215:     my ($datatable,$css_class);
 5216:     if ($position eq 'top') {
 5217:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 5218:                      'datelocale_def','portal_def');
 5219:         my %defaults;
 5220:         if (ref($settings) eq 'HASH') {
 5221:             %defaults = %{$settings};
 5222:         } else {
 5223:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 5224:             foreach my $item (@items) {
 5225:                 $defaults{$item} = $domdefaults{$item};
 5226:             }
 5227:         }
 5228:         my $titles = &defaults_titles($dom);
 5229:         foreach my $item (@items) {
 5230:             if ($rownum%2) {
 5231:                 $css_class = '';
 5232:             } else {
 5233:                 $css_class = ' class="LC_odd_row" ';
 5234:             }
 5235:             $datatable .= '<tr'.$css_class.'>'.
 5236:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 5237:                           '</span></td><td class="LC_right_item" colspan="3">';
 5238:             if ($item eq 'auth_def') {
 5239:                 my @authtypes = ('internal','krb4','krb5','localauth');
 5240:                 my %shortauth = (
 5241:                                  internal => 'int',
 5242:                                  krb4 => 'krb4',
 5243:                                  krb5 => 'krb5',
 5244:                                  localauth  => 'loc'
 5245:                                 );
 5246:                 my %authnames = &authtype_names();
 5247:                 foreach my $auth (@authtypes) {
 5248:                     my $checked = ' ';
 5249:                     if ($defaults{$item} eq $auth) {
 5250:                         $checked = ' checked="checked" ';
 5251:                     }
 5252:                     $datatable .= '<label><input type="radio" name="'.$item.
 5253:                                   '" value="'.$auth.'"'.$checked.'/>'.
 5254:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 5255:                 }
 5256:             } elsif ($item eq 'timezone_def') {
 5257:                 my $includeempty = 1;
 5258:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 5259:             } elsif ($item eq 'datelocale_def') {
 5260:                 my $includeempty = 1;
 5261:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 5262:             } elsif ($item eq 'lang_def') {
 5263:                 my $includeempty = 1;
 5264:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 5265:             } else {
 5266:                 my $size;
 5267:                 if ($item eq 'portal_def') {
 5268:                     $size = ' size="25"';
 5269:                 }
 5270:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 5271:                               $defaults{$item}.'"'.$size.' />';
 5272:             }
 5273:             $datatable .= '</td></tr>';
 5274:             $rownum ++;
 5275:         }
 5276:     } else {
 5277:         my (%defaults);
 5278:         if (ref($settings) eq 'HASH') {
 5279:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH') &&
 5280:                 (ref($settings->{'inststatusguest'}) eq 'ARRAY')) {
 5281:                 my $maxnum = @{$settings->{'inststatusorder'}};
 5282:                 for (my $i=0; $i<$maxnum; $i++) {
 5283:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 5284:                     my $item = $settings->{'inststatusorder'}->[$i];
 5285:                     my $title = $settings->{'inststatustypes'}->{$item};
 5286:                     my $guestok;
 5287:                     if (grep(/^\Q$item\E$/,@{$settings->{'inststatusguest'}})) {
 5288:                         $guestok = 1;
 5289:                     }
 5290:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 5291:                     $datatable .= '<tr'.$css_class.'>'.
 5292:                                   '<td><span class="LC_nobreak">'.
 5293:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 5294:                     for (my $k=0; $k<=$maxnum; $k++) {
 5295:                         my $vpos = $k+1;
 5296:                         my $selstr;
 5297:                         if ($k == $i) {
 5298:                             $selstr = ' selected="selected" ';
 5299:                         }
 5300:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5301:                     }
 5302:                     my ($checkedon,$checkedoff);
 5303:                     $checkedoff = ' checked="checked"';
 5304:                     if ($guestok) {
 5305:                         $checkedon = $checkedoff;
 5306:                         $checkedoff = ''; 
 5307:                     }
 5308:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 5309:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 5310:                                   &mt('delete').'</span></td>'.
 5311:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed:').
 5312:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 5313:                                   '</span></td>'.
 5314:                                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 5315:                                   '<label><input type="radio" value="1" name="inststatus_guest_'.$item.'"'.$checkedon.' />'.
 5316:                                   &mt('Yes').'</label>'.('&nbsp;'x2).
 5317:                                   '<label><input type="radio" value="0" name="inststatus_guest_'.$item.'"'.$checkedoff.' />'.
 5318:                                   &mt('No').'</label></span></td></tr>';
 5319:                 }
 5320:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 5321:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 5322:                 $datatable .= '<tr '.$css_class.'>'.
 5323:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 5324:                 for (my $k=0; $k<=$maxnum; $k++) {
 5325:                     my $vpos = $k+1;
 5326:                     my $selstr;
 5327:                     if ($k == $maxnum) {
 5328:                         $selstr = ' selected="selected" ';
 5329:                     }
 5330:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5331:                 }
 5332:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 5333:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 5334:                               '&nbsp;'.&mt('(new)').
 5335:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 5336:                               &mt('Name displayed:').
 5337:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 5338:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 5339:                               '<label><input type="radio" value="1" name="addinststatus_guest" />'.
 5340:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 5341:                               '<label><input type="radio" value="0" name="addinststatus_guest" />'.
 5342:                               &mt('No').'</label></span></td></tr>';
 5343:                               '</tr>'."\n";
 5344:                 $rownum ++;
 5345:             }
 5346:         }
 5347:     }
 5348:     $$rowtotal += $rownum;
 5349:     return $datatable;
 5350: }
 5351: 
 5352: sub get_languages_hash {
 5353:     my %langchoices;
 5354:     foreach my $id (&Apache::loncommon::languageids()) {
 5355:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 5356:         if ($code ne '') {
 5357:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 5358:         }
 5359:     }
 5360:     return %langchoices;
 5361: }
 5362: 
 5363: sub defaults_titles {
 5364:     my ($dom) = @_;
 5365:     my %titles = &Apache::lonlocal::texthash (
 5366:                    'auth_def'      => 'Default authentication type',
 5367:                    'auth_arg_def'  => 'Default authentication argument',
 5368:                    'lang_def'      => 'Default language',
 5369:                    'timezone_def'  => 'Default timezone',
 5370:                    'datelocale_def' => 'Default locale for dates',
 5371:                    'portal_def'     => 'Portal/Default URL',
 5372:                  );
 5373:     if ($dom) {
 5374:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 5375:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 5376:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 5377:         $protocol = 'http' if ($protocol ne 'https');
 5378:         if ($uint_dom) {
 5379:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 5380:                                          $uint_dom);
 5381:         }
 5382:     }
 5383:     return (\%titles);
 5384: }
 5385: 
 5386: sub print_scantronformat {
 5387:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 5388:     my $itemcount = 1;
 5389:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 5390:         %confhash);
 5391:     my $switchserver = &check_switchserver($dom,$confname);
 5392:     my %lt = &Apache::lonlocal::texthash (
 5393:                 default => 'Default bubblesheet format file error',
 5394:                 custom  => 'Custom bubblesheet format file error',
 5395:              );
 5396:     my %scantronfiles = (
 5397:         default => 'default.tab',
 5398:         custom => 'custom.tab',
 5399:     );
 5400:     foreach my $key (keys(%scantronfiles)) {
 5401:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 5402:                               .$scantronfiles{$key};
 5403:     }
 5404:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 5405:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 5406:         if (!$switchserver) {
 5407:             my $servadm = $r->dir_config('lonAdmEMail');
 5408:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 5409:             if ($configuserok eq 'ok') {
 5410:                 if ($author_ok eq 'ok') {
 5411:                     my %legacyfile = (
 5412:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 5413:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 5414:                     );
 5415:                     my %md5chk;
 5416:                     foreach my $type (keys(%legacyfile)) {
 5417:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 5418:                         chomp($md5chk{$type});
 5419:                     }
 5420:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 5421:                         foreach my $type (keys(%legacyfile)) {
 5422:                             ($scantronurls{$type},my $error) = 
 5423:                                 &legacy_scantronformat($r,$dom,$confname,
 5424:                                                  $type,$legacyfile{$type},
 5425:                                                  $scantronurls{$type},
 5426:                                                  $scantronfiles{$type});
 5427:                             if ($error ne '') {
 5428:                                 $error{$type} = $error;
 5429:                             }
 5430:                         }
 5431:                         if (keys(%error) == 0) {
 5432:                             $is_custom = 1;
 5433:                             $confhash{'scantron'}{'scantronformat'} = 
 5434:                                 $scantronurls{'custom'};
 5435:                             my $putresult = 
 5436:                                 &Apache::lonnet::put_dom('configuration',
 5437:                                                          \%confhash,$dom);
 5438:                             if ($putresult ne 'ok') {
 5439:                                 $error{'custom'} = 
 5440:                                     '<span class="LC_error">'.
 5441:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 5442:                             }
 5443:                         }
 5444:                     } else {
 5445:                         ($scantronurls{'default'},my $error) =
 5446:                             &legacy_scantronformat($r,$dom,$confname,
 5447:                                           'default',$legacyfile{'default'},
 5448:                                           $scantronurls{'default'},
 5449:                                           $scantronfiles{'default'});
 5450:                         if ($error eq '') {
 5451:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 5452:                             my $putresult =
 5453:                                 &Apache::lonnet::put_dom('configuration',
 5454:                                                          \%confhash,$dom);
 5455:                             if ($putresult ne 'ok') {
 5456:                                 $error{'default'} =
 5457:                                     '<span class="LC_error">'.
 5458:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 5459:                             }
 5460:                         } else {
 5461:                             $error{'default'} = $error;
 5462:                         }
 5463:                     }
 5464:                 }
 5465:             }
 5466:         } else {
 5467:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 5468:         }
 5469:     }
 5470:     if (ref($settings) eq 'HASH') {
 5471:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 5472:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 5473:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 5474:                 $scantronurl = '';
 5475:             } else {
 5476:                 $scantronurl = $settings->{'scantronformat'};
 5477:             }
 5478:             $is_custom = 1;
 5479:         } else {
 5480:             $scantronurl = $scantronurls{'default'};
 5481:         }
 5482:     } else {
 5483:         if ($is_custom) {
 5484:             $scantronurl = $scantronurls{'custom'};
 5485:         } else {
 5486:             $scantronurl = $scantronurls{'default'};
 5487:         }
 5488:     }
 5489:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5490:     $datatable .= '<tr'.$css_class.'>';
 5491:     if (!$is_custom) {
 5492:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 5493:                       '<span class="LC_nobreak">';
 5494:         if ($scantronurl) {
 5495:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 5496:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 5497:         } else {
 5498:             $datatable = &mt('File unavailable for display');
 5499:         }
 5500:         $datatable .= '</span></td>';
 5501:         if (keys(%error) == 0) { 
 5502:             $datatable .= '<td valign="bottom">';
 5503:             if (!$switchserver) {
 5504:                 $datatable .= &mt('Upload:').'<br />';
 5505:             }
 5506:         } else {
 5507:             my $errorstr;
 5508:             foreach my $key (sort(keys(%error))) {
 5509:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 5510:             }
 5511:             $datatable .= '<td>'.$errorstr;
 5512:         }
 5513:     } else {
 5514:         if (keys(%error) > 0) {
 5515:             my $errorstr;
 5516:             foreach my $key (sort(keys(%error))) {
 5517:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 5518:             } 
 5519:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 5520:         } elsif ($scantronurl) {
 5521:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 5522:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 5523:             $datatable .= '<td><span class="LC_nobreak">'.
 5524:                           $link.
 5525:                           '<label><input type="checkbox" name="scantronformat_del"'.
 5526:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 5527:                           '<td><span class="LC_nobreak">&nbsp;'.
 5528:                           &mt('Replace:').'</span><br />';
 5529:         }
 5530:     }
 5531:     if (keys(%error) == 0) {
 5532:         if ($switchserver) {
 5533:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 5534:         } else {
 5535:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 5536:                          '<input type="file" name="scantronformat" /></span>';
 5537:         }
 5538:     }
 5539:     $datatable .= '</td></tr>';
 5540:     $$rowtotal ++;
 5541:     return $datatable;
 5542: }
 5543: 
 5544: sub legacy_scantronformat {
 5545:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 5546:     my ($url,$error);
 5547:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 5548:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 5549:         (my $result,$url) =
 5550:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 5551:                          '','',$newfile);
 5552:         if ($result ne 'ok') {
 5553:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 5554:         }
 5555:     }
 5556:     return ($url,$error);
 5557: }
 5558: 
 5559: sub print_coursecategories {
 5560:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 5561:     my $datatable;
 5562:     if ($position eq 'top') {
 5563:         my (%checked);
 5564:         my @catitems = ('unauth','auth');
 5565:         my @cattypes = ('std','domonly','codesrch','none');
 5566:         $checked{'unauth'} = 'std';
 5567:         $checked{'auth'} = 'std';
 5568:         if (ref($settings) eq 'HASH') {
 5569:             foreach my $type (@cattypes) {
 5570:                 if ($type eq $settings->{'unauth'}) {
 5571:                     $checked{'unauth'} = $type;
 5572:                 }
 5573:                 if ($type eq $settings->{'auth'}) {
 5574:                     $checked{'auth'} = $type;
 5575:                 }
 5576:             }
 5577:         }
 5578:         my %lt = &Apache::lonlocal::texthash (
 5579:                                                unauth   => 'Catalog type for unauthenticated users',
 5580:                                                auth     => 'Catalog type for authenticated users',
 5581:                                                none     => 'No catalog',
 5582:                                                std      => 'Standard catalog',
 5583:                                                domonly  => 'Domain-only catalog',
 5584:                                                codesrch => "Code search form",
 5585:                                              );
 5586:        my $itemcount = 0;
 5587:        foreach my $item (@catitems) {
 5588:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 5589:            $datatable .= '<tr '.$css_class.'>'.
 5590:                          '<td>'.$lt{$item}.'</td>'.
 5591:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 5592:            foreach my $type (@cattypes) {
 5593:                my $ischecked;
 5594:                if ($checked{$item} eq $type) {
 5595:                    $ischecked=' checked="checked"';
 5596:                }
 5597:                $datatable .= '<label>'.
 5598:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 5599:                              ' />'.$lt{$type}.'</label>&nbsp;';
 5600:            }
 5601:            $datatable .= '</td></tr>';
 5602:            $itemcount ++;
 5603:         }
 5604:         $$rowtotal += $itemcount;
 5605:     } elsif ($position eq 'middle') {
 5606:         my $toggle_cats_crs = ' ';
 5607:         my $toggle_cats_dom = ' checked="checked" ';
 5608:         my $can_cat_crs = ' ';
 5609:         my $can_cat_dom = ' checked="checked" ';
 5610:         my $toggle_catscomm_comm = ' ';
 5611:         my $toggle_catscomm_dom = ' checked="checked" ';
 5612:         my $can_catcomm_comm = ' ';
 5613:         my $can_catcomm_dom = ' checked="checked" ';
 5614: 
 5615:         if (ref($settings) eq 'HASH') {
 5616:             if ($settings->{'togglecats'} eq 'crs') {
 5617:                 $toggle_cats_crs = $toggle_cats_dom;
 5618:                 $toggle_cats_dom = ' ';
 5619:             }
 5620:             if ($settings->{'categorize'} eq 'crs') {
 5621:                 $can_cat_crs = $can_cat_dom;
 5622:                 $can_cat_dom = ' ';
 5623:             }
 5624:             if ($settings->{'togglecatscomm'} eq 'comm') {
 5625:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 5626:                 $toggle_catscomm_dom = ' ';
 5627:             }
 5628:             if ($settings->{'categorizecomm'} eq 'comm') {
 5629:                 $can_catcomm_comm = $can_catcomm_dom;
 5630:                 $can_catcomm_dom = ' ';
 5631:             }
 5632:         }
 5633:         my %title = &Apache::lonlocal::texthash (
 5634:                      togglecats     => 'Show/Hide a course in catalog',
 5635:                      togglecatscomm => 'Show/Hide a community in catalog',
 5636:                      categorize     => 'Assign a category to a course',
 5637:                      categorizecomm => 'Assign a category to a community',
 5638:                     );
 5639:         my %level = &Apache::lonlocal::texthash (
 5640:                      dom  => 'Set in Domain',
 5641:                      crs  => 'Set in Course',
 5642:                      comm => 'Set in Community',
 5643:                     );
 5644:         $datatable = '<tr class="LC_odd_row">'.
 5645:                   '<td>'.$title{'togglecats'}.'</td>'.
 5646:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 5647:                   '<input type="radio" name="togglecats"'.
 5648:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 5649:                   '<label><input type="radio" name="togglecats"'.
 5650:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 5651:                   '</tr><tr>'.
 5652:                   '<td>'.$title{'categorize'}.'</td>'.
 5653:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 5654:                   '<label><input type="radio" name="categorize"'.
 5655:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 5656:                   '<label><input type="radio" name="categorize"'.
 5657:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 5658:                   '</tr><tr class="LC_odd_row">'.
 5659:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 5660:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 5661:                   '<input type="radio" name="togglecatscomm"'.
 5662:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 5663:                   '<label><input type="radio" name="togglecatscomm"'.
 5664:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 5665:                   '</tr><tr>'.
 5666:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 5667:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 5668:                   '<label><input type="radio" name="categorizecomm"'.
 5669:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 5670:                   '<label><input type="radio" name="categorizecomm"'.
 5671:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 5672:                   '</tr>';
 5673:         $$rowtotal += 4;
 5674:     } else {
 5675:         my $css_class;
 5676:         my $itemcount = 1;
 5677:         my $cathash; 
 5678:         if (ref($settings) eq 'HASH') {
 5679:             $cathash = $settings->{'cats'};
 5680:         }
 5681:         if (ref($cathash) eq 'HASH') {
 5682:             my (@cats,@trails,%allitems,%idx,@jsarray);
 5683:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 5684:                                                    \%allitems,\%idx,\@jsarray);
 5685:             my $maxdepth = scalar(@cats);
 5686:             my $colattrib = '';
 5687:             if ($maxdepth > 2) {
 5688:                 $colattrib = ' colspan="2" ';
 5689:             }
 5690:             my @path;
 5691:             if (@cats > 0) {
 5692:                 if (ref($cats[0]) eq 'ARRAY') {
 5693:                     my $numtop = @{$cats[0]};
 5694:                     my $maxnum = $numtop;
 5695:                     my %default_names = (
 5696:                           instcode    => &mt('Official courses'),
 5697:                           communities => &mt('Communities'),
 5698:                     );
 5699: 
 5700:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 5701:                         ($cathash->{'instcode::0'} eq '') ||
 5702:                         (!grep(/^communities$/,@{$cats[0]})) || 
 5703:                         ($cathash->{'communities::0'} eq '')) {
 5704:                         $maxnum ++;
 5705:                     }
 5706:                     my $lastidx;
 5707:                     for (my $i=0; $i<$numtop; $i++) {
 5708:                         my $parent = $cats[0][$i];
 5709:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5710:                         my $item = &escape($parent).'::0';
 5711:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 5712:                         $lastidx = $idx{$item};
 5713:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5714:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 5715:                         for (my $k=0; $k<=$maxnum; $k++) {
 5716:                             my $vpos = $k+1;
 5717:                             my $selstr;
 5718:                             if ($k == $i) {
 5719:                                 $selstr = ' selected="selected" ';
 5720:                             }
 5721:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5722:                         }
 5723:                         $datatable .= '</select></span></td><td>';
 5724:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 5725:                             $datatable .=  '<span class="LC_nobreak">'
 5726:                                            .$default_names{$parent}.'</span>';
 5727:                             if ($parent eq 'instcode') {
 5728:                                 $datatable .= '<br /><span class="LC_nobreak">('
 5729:                                               .&mt('with institutional codes')
 5730:                                               .')</span></td><td'.$colattrib.'>';
 5731:                             } else {
 5732:                                 $datatable .= '<table><tr><td>';
 5733:                             }
 5734:                             $datatable .= '<span class="LC_nobreak">'
 5735:                                           .'<label><input type="radio" name="'
 5736:                                           .$parent.'" value="1" checked="checked" />'
 5737:                                           .&mt('Display').'</label>';
 5738:                             if ($parent eq 'instcode') {
 5739:                                 $datatable .= '&nbsp;';
 5740:                             } else {
 5741:                                 $datatable .= '</span></td></tr><tr><td>'
 5742:                                               .'<span class="LC_nobreak">';
 5743:                             }
 5744:                             $datatable .= '<label><input type="radio" name="'
 5745:                                           .$parent.'" value="0" />'
 5746:                                           .&mt('Do not display').'</label></span>';
 5747:                             if ($parent eq 'communities') {
 5748:                                 $datatable .= '</td></tr></table>';
 5749:                             }
 5750:                             $datatable .= '</td>';
 5751:                         } else {
 5752:                             $datatable .= $parent
 5753:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 5754:                                           .'<input type="checkbox" name="deletecategory" '
 5755:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 5756:                         }
 5757:                         my $depth = 1;
 5758:                         push(@path,$parent);
 5759:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 5760:                         pop(@path);
 5761:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 5762:                         $itemcount ++;
 5763:                     }
 5764:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5765:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 5766:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 5767:                     for (my $k=0; $k<=$maxnum; $k++) {
 5768:                         my $vpos = $k+1;
 5769:                         my $selstr;
 5770:                         if ($k == $numtop) {
 5771:                             $selstr = ' selected="selected" ';
 5772:                         }
 5773:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5774:                     }
 5775:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 5776:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 5777:                                   .'</tr>'."\n";
 5778:                     $itemcount ++;
 5779:                     foreach my $default ('instcode','communities') {
 5780:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 5781:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5782:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 5783:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 5784:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 5785:                             for (my $k=0; $k<=$maxnum; $k++) {
 5786:                                 my $vpos = $k+1;
 5787:                                 my $selstr;
 5788:                                 if ($k == $maxnum) {
 5789:                                     $selstr = ' selected="selected" ';
 5790:                                 }
 5791:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5792:                             }
 5793:                             $datatable .= '</select></span></td>'.
 5794:                                           '<td><span class="LC_nobreak">'.
 5795:                                           $default_names{$default}.'</span>';
 5796:                             if ($default eq 'instcode') {
 5797:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 5798:                                               .&mt('with institutional codes').')</span>';
 5799:                             }
 5800:                             $datatable .= '</td>'
 5801:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 5802:                                           .&mt('Display').'</label>&nbsp;'
 5803:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 5804:                                           .&mt('Do not display').'</label></span></td></tr>';
 5805:                         }
 5806:                     }
 5807:                 }
 5808:             } else {
 5809:                 $datatable .= &initialize_categories($itemcount);
 5810:             }
 5811:         } else {
 5812:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td>'
 5813:                           .&initialize_categories($itemcount);
 5814:         }
 5815:         $$rowtotal += $itemcount;
 5816:     }
 5817:     return $datatable;
 5818: }
 5819: 
 5820: sub print_serverstatuses {
 5821:     my ($dom,$settings,$rowtotal) = @_;
 5822:     my $datatable;
 5823:     my @pages = &serverstatus_pages();
 5824:     my (%namedaccess,%machineaccess);
 5825:     foreach my $type (@pages) {
 5826:         $namedaccess{$type} = '';
 5827:         $machineaccess{$type}= '';
 5828:     }
 5829:     if (ref($settings) eq 'HASH') {
 5830:         foreach my $type (@pages) {
 5831:             if (exists($settings->{$type})) {
 5832:                 if (ref($settings->{$type}) eq 'HASH') {
 5833:                     foreach my $key (keys(%{$settings->{$type}})) {
 5834:                         if ($key eq 'namedusers') {
 5835:                             $namedaccess{$type} = $settings->{$type}->{$key};
 5836:                         } elsif ($key eq 'machines') {
 5837:                             $machineaccess{$type} = $settings->{$type}->{$key};
 5838:                         }
 5839:                     }
 5840:                 }
 5841:             }
 5842:         }
 5843:     }
 5844:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 5845:     my $rownum = 0;
 5846:     my $css_class;
 5847:     foreach my $type (@pages) {
 5848:         $rownum ++;
 5849:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 5850:         $datatable .= '<tr'.$css_class.'>'.
 5851:                       '<td><span class="LC_nobreak">'.
 5852:                       $titles->{$type}.'</span></td>'.
 5853:                       '<td class="LC_left_item">'.
 5854:                       '<input type="text" name="'.$type.'_namedusers" '.
 5855:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 5856:                       '<td class="LC_right_item">'.
 5857:                       '<span class="LC_nobreak">'.
 5858:                       '<input type="text" name="'.$type.'_machines" '.
 5859:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 5860:                       '</td></tr>'."\n";
 5861:     }
 5862:     $$rowtotal += $rownum;
 5863:     return $datatable;
 5864: }
 5865: 
 5866: sub serverstatus_pages {
 5867:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 5868:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 5869:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 5870:             'uniquecodes','diskusage','coursecatalog');
 5871: }
 5872: 
 5873: sub defaults_javascript {
 5874:     my ($settings) = @_;
 5875:     return unless (ref($settings) eq 'HASH');
 5876:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 5877:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 5878:         if ($maxnum eq '') {
 5879:             $maxnum = 0;
 5880:         }
 5881:         $maxnum ++;
 5882:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 5883:         return <<"ENDSCRIPT";
 5884: <script type="text/javascript">
 5885: // <![CDATA[
 5886: function reorderTypes(form,caller) {
 5887:     var changedVal;
 5888: $jstext 
 5889:     var newpos = 'addinststatus_pos';
 5890:     var current = new Array;
 5891:     var maxh = $maxnum;
 5892:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 5893:     var oldVal;
 5894:     if (caller == newpos) {
 5895:         changedVal = newitemVal;
 5896:     } else {
 5897:         var curritem = 'inststatus_pos_'+caller;
 5898:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 5899:         current[newitemVal] = newpos;
 5900:     }
 5901:     for (var i=0; i<inststatuses.length; i++) {
 5902:         if (inststatuses[i] != caller) {
 5903:             var elementName = 'inststatus_pos_'+inststatuses[i];
 5904:             if (form.elements[elementName]) {
 5905:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 5906:                 current[currVal] = elementName;
 5907:             }
 5908:         }
 5909:     }
 5910:     for (var j=0; j<maxh; j++) {
 5911:         if (current[j] == undefined) {
 5912:             oldVal = j;
 5913:         }
 5914:     }
 5915:     if (oldVal < changedVal) {
 5916:         for (var k=oldVal+1; k<=changedVal ; k++) {
 5917:            var elementName = current[k];
 5918:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 5919:         }
 5920:     } else {
 5921:         for (var k=changedVal; k<oldVal; k++) {
 5922:             var elementName = current[k];
 5923:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 5924:         }
 5925:     }
 5926:     return;
 5927: }
 5928: 
 5929: // ]]>
 5930: </script>
 5931: 
 5932: ENDSCRIPT
 5933:     }
 5934: }
 5935: 
 5936: sub coursecategories_javascript {
 5937:     my ($settings) = @_;
 5938:     my ($output,$jstext,$cathash);
 5939:     if (ref($settings) eq 'HASH') {
 5940:         $cathash = $settings->{'cats'};
 5941:     }
 5942:     if (ref($cathash) eq 'HASH') {
 5943:         my (@cats,@jsarray,%idx);
 5944:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 5945:         if (@jsarray > 0) {
 5946:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 5947:             for (my $i=0; $i<@jsarray; $i++) {
 5948:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 5949:                     my $catstr = join('","',@{$jsarray[$i]});
 5950:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 5951:                 }
 5952:             }
 5953:         }
 5954:     } else {
 5955:         $jstext  = '    var categories = Array(1);'."\n".
 5956:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 5957:     }
 5958:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 5959:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 5960:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
 5961:     &js_escape(\$instcode_reserved);
 5962:     &js_escape(\$communities_reserved);
 5963:     &js_escape(\$choose_again);
 5964:     $output = <<"ENDSCRIPT";
 5965: <script type="text/javascript">
 5966: // <![CDATA[
 5967: function reorderCats(form,parent,item,idx) {
 5968:     var changedVal;
 5969: $jstext
 5970:     var newpos = 'addcategory_pos';
 5971:     if (parent == '') {
 5972:         var has_instcode = 0;
 5973:         var maxtop = categories[idx].length;
 5974:         for (var j=0; j<maxtop; j++) {
 5975:             if (categories[idx][j] == 'instcode::0') {
 5976:                 has_instcode == 1;
 5977:             }
 5978:         }
 5979:         if (has_instcode == 0) {
 5980:             categories[idx][maxtop] = 'instcode_pos';
 5981:         }
 5982:     } else {
 5983:         newpos += '_'+parent;
 5984:     }
 5985:     var maxh = 1 + categories[idx].length;
 5986:     var current = new Array;
 5987:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 5988:     if (item == newpos) {
 5989:         changedVal = newitemVal;
 5990:     } else {
 5991:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 5992:         current[newitemVal] = newpos;
 5993:     }
 5994:     for (var i=0; i<categories[idx].length; i++) {
 5995:         var elementName = categories[idx][i];
 5996:         if (elementName != item) {
 5997:             if (form.elements[elementName]) {
 5998:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 5999:                 current[currVal] = elementName;
 6000:             }
 6001:         }
 6002:     }
 6003:     var oldVal;
 6004:     for (var j=0; j<maxh; j++) {
 6005:         if (current[j] == undefined) {
 6006:             oldVal = j;
 6007:         }
 6008:     }
 6009:     if (oldVal < changedVal) {
 6010:         for (var k=oldVal+1; k<=changedVal ; k++) {
 6011:            var elementName = current[k];
 6012:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 6013:         }
 6014:     } else {
 6015:         for (var k=changedVal; k<oldVal; k++) {
 6016:             var elementName = current[k];
 6017:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 6018:         }
 6019:     }
 6020:     return;
 6021: }
 6022: 
 6023: function categoryCheck(form) {
 6024:     if (form.elements['addcategory_name'].value == 'instcode') {
 6025:         alert('$instcode_reserved\\n$choose_again');
 6026:         return false;
 6027:     }
 6028:     if (form.elements['addcategory_name'].value == 'communities') {
 6029:         alert('$communities_reserved\\n$choose_again');
 6030:         return false;
 6031:     }
 6032:     return true;
 6033: }
 6034: 
 6035: // ]]>
 6036: </script>
 6037: 
 6038: ENDSCRIPT
 6039:     return $output;
 6040: }
 6041: 
 6042: sub initialize_categories {
 6043:     my ($itemcount) = @_;
 6044:     my ($datatable,$css_class,$chgstr);
 6045:     my %default_names = (
 6046:                       instcode    => 'Official courses (with institutional codes)',
 6047:                       communities => 'Communities',
 6048:                         );
 6049:     my $select0 = ' selected="selected"';
 6050:     my $select1 = '';
 6051:     foreach my $default ('instcode','communities') {
 6052:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6053:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 6054:         if ($default eq 'communities') {
 6055:             $select1 = $select0;
 6056:             $select0 = '';
 6057:         }
 6058:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6059:                      .'<select name="'.$default.'_pos">'
 6060:                      .'<option value="0"'.$select0.'>1</option>'
 6061:                      .'<option value="1"'.$select1.'>2</option>'
 6062:                      .'<option value="2">3</option></select>&nbsp;'
 6063:                      .$default_names{$default}
 6064:                      .'</span></td><td><span class="LC_nobreak">'
 6065:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 6066:                      .&mt('Display').'</label>&nbsp;<label>'
 6067:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 6068:                  .'</label></span></td></tr>';
 6069:         $itemcount ++;
 6070:     }
 6071:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6072:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 6073:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6074:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 6075:                   .'<option value="0">1</option>'
 6076:                   .'<option value="1">2</option>'
 6077:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 6078:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 6079:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 6080:     return $datatable;
 6081: }
 6082: 
 6083: sub build_category_rows {
 6084:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 6085:     my ($text,$name,$item,$chgstr);
 6086:     if (ref($cats) eq 'ARRAY') {
 6087:         my $maxdepth = scalar(@{$cats});
 6088:         if (ref($cats->[$depth]) eq 'HASH') {
 6089:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 6090:                 my $numchildren = @{$cats->[$depth]{$parent}};
 6091:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6092:                 $text .= '<td><table class="LC_data_table">';
 6093:                 my ($idxnum,$parent_name,$parent_item);
 6094:                 my $higher = $depth - 1;
 6095:                 if ($higher == 0) {
 6096:                     $parent_name = &escape($parent).'::'.$higher;
 6097:                 } else {
 6098:                     if (ref($path) eq 'ARRAY') {
 6099:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 6100:                     }
 6101:                 }
 6102:                 $parent_item = 'addcategory_pos_'.$parent_name;
 6103:                 for (my $j=0; $j<=$numchildren; $j++) {
 6104:                     if ($j < $numchildren) {
 6105:                         $name = $cats->[$depth]{$parent}[$j];
 6106:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 6107:                         $idxnum = $idx->{$item};
 6108:                     } else {
 6109:                         $name = $parent_name;
 6110:                         $item = $parent_item;
 6111:                     }
 6112:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 6113:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 6114:                     for (my $i=0; $i<=$numchildren; $i++) {
 6115:                         my $vpos = $i+1;
 6116:                         my $selstr;
 6117:                         if ($j == $i) {
 6118:                             $selstr = ' selected="selected" ';
 6119:                         }
 6120:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 6121:                     }
 6122:                     $text .= '</select>&nbsp;';
 6123:                     if ($j < $numchildren) {
 6124:                         my $deeper = $depth+1;
 6125:                         $text .= $name.'&nbsp;'
 6126:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 6127:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 6128:                         if(ref($path) eq 'ARRAY') {
 6129:                             push(@{$path},$name);
 6130:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 6131:                             pop(@{$path});
 6132:                         }
 6133:                     } else {
 6134:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 6135:                         if ($j == $numchildren) {
 6136:                             $text .= $name;
 6137:                         } else {
 6138:                             $text .= $item;
 6139:                         }
 6140:                         $text .= '" value="" />';
 6141:                     }
 6142:                     $text .= '</td></tr>';
 6143:                 }
 6144:                 $text .= '</table></td>';
 6145:             } else {
 6146:                 my $higher = $depth-1;
 6147:                 if ($higher == 0) {
 6148:                     $name = &escape($parent).'::'.$higher;
 6149:                 } else {
 6150:                     if (ref($path) eq 'ARRAY') {
 6151:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 6152:                     }
 6153:                 }
 6154:                 my $colspan;
 6155:                 if ($parent ne 'instcode') {
 6156:                     $colspan = $maxdepth - $depth - 1;
 6157:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 6158:                 }
 6159:             }
 6160:         }
 6161:     }
 6162:     return $text;
 6163: }
 6164: 
 6165: sub modifiable_userdata_row {
 6166:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref) = @_;
 6167:     my ($role,$rolename,$statustype);
 6168:     $role = $item;
 6169:     if ($context eq 'cancreate') {
 6170:         if ($item =~ /^emailusername_(.+)$/) {
 6171:             $statustype = $1;
 6172:             $role = 'emailusername';
 6173:             if (ref($usertypes) eq 'HASH') {
 6174:                 if ($usertypes->{$statustype}) {
 6175:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 6176:                 } else {
 6177:                     $rolename = &mt('Data provided by user');
 6178:                 }
 6179:             }
 6180:         }
 6181:     } elsif ($context eq 'selfcreate') {
 6182:         if (ref($usertypes) eq 'HASH') {
 6183:             $rolename = $usertypes->{$role};
 6184:         } else {
 6185:             $rolename = $role;
 6186:         }
 6187:     } else {
 6188:         if ($role eq 'cr') {
 6189:             $rolename = &mt('Custom role');
 6190:         } else {
 6191:             $rolename = &Apache::lonnet::plaintext($role);
 6192:         }
 6193:     }
 6194:     my (@fields,%fieldtitles);
 6195:     if (ref($fieldsref) eq 'ARRAY') {
 6196:         @fields = @{$fieldsref};
 6197:     } else {
 6198:         @fields = ('lastname','firstname','middlename','generation',
 6199:                    'permanentemail','id');
 6200:     }
 6201:     if ((ref($titlesref) eq 'HASH')) {
 6202:         %fieldtitles = %{$titlesref};
 6203:     } else {
 6204:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 6205:     }
 6206:     my $output;
 6207:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 6208:     $output = '<tr '.$css_class.'>'.
 6209:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 6210:               '<td class="LC_left_item" colspan="2"><table>';
 6211:     my $rem;
 6212:     my %checks;
 6213:     if (ref($settings) eq 'HASH') {
 6214:         if (ref($settings->{$context}) eq 'HASH') {
 6215:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 6216:                 my $hashref = $settings->{$context}->{$role};
 6217:                 if ($role eq 'emailusername') {
 6218:                     if ($statustype) {
 6219:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 6220:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 6221:                             if (ref($hashref) eq 'HASH') { 
 6222:                                 foreach my $field (@fields) {
 6223:                                     if ($hashref->{$field}) {
 6224:                                         $checks{$field} = $hashref->{$field};
 6225:                                     }
 6226:                                 }
 6227:                             }
 6228:                         }
 6229:                     }
 6230:                 } else {
 6231:                     if (ref($hashref) eq 'HASH') {
 6232:                         foreach my $field (@fields) {
 6233:                             if ($hashref->{$field}) {
 6234:                                 $checks{$field} = ' checked="checked" ';
 6235:                             }
 6236:                         }
 6237:                     }
 6238:                 }
 6239:             }
 6240:         }
 6241:     }
 6242:      
 6243:     for (my $i=0; $i<@fields; $i++) {
 6244:         my $rem = $i%($numinrow);
 6245:         if ($rem == 0) {
 6246:             if ($i > 0) {
 6247:                 $output .= '</tr>';
 6248:             }
 6249:             $output .= '<tr>';
 6250:         }
 6251:         my $check = ' ';
 6252:         unless ($role eq 'emailusername') {
 6253:             if (exists($checks{$fields[$i]})) {
 6254:                 $check = $checks{$fields[$i]}
 6255:             } else {
 6256:                 if ($role eq 'st') {
 6257:                     if (ref($settings) ne 'HASH') {
 6258:                         $check = ' checked="checked" '; 
 6259:                     }
 6260:                 }
 6261:             }
 6262:         }
 6263:         $output .= '<td class="LC_left_item">'.
 6264:                    '<span class="LC_nobreak">';
 6265:         if ($role eq 'emailusername') {
 6266:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 6267:                 $checks{$fields[$i]} = 'omit';
 6268:             }
 6269:             foreach my $option ('required','optional','omit') {
 6270:                 my $checked='';
 6271:                 if ($checks{$fields[$i]} eq $option) {
 6272:                     $checked='checked="checked" ';
 6273:                 }
 6274:                 $output .= '<label>'.
 6275:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 6276:                            &mt($option).'</label>'.('&nbsp;' x2);
 6277:             }
 6278:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 6279:         } else {
 6280:             $output .= '<label>'.
 6281:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 6282:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 6283:                        '</label>';
 6284:         }
 6285:         $output .= '</span></td>';
 6286:         $rem = @fields%($numinrow);
 6287:     }
 6288:     my $colsleft = $numinrow - $rem;
 6289:     if ($colsleft > 1 ) {
 6290:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 6291:                    '&nbsp;</td>';
 6292:     } elsif ($colsleft == 1) {
 6293:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 6294:     }
 6295:     $output .= '</tr></table></td></tr>';
 6296:     return $output;
 6297: }
 6298: 
 6299: sub insttypes_row {
 6300:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rownum) = @_;
 6301:     my %lt = &Apache::lonlocal::texthash (
 6302:                       cansearch => 'Users allowed to search',
 6303:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 6304:                       lockablenames => 'User preference to lock name',
 6305:              );
 6306:     my $showdom;
 6307:     if ($context eq 'cansearch') {
 6308:         $showdom = ' ('.$dom.')';
 6309:     }
 6310:     my $class = 'LC_left_item';
 6311:     if ($context eq 'statustocreate') {
 6312:         $class = 'LC_right_item';
 6313:     }
 6314:     my $css_class = ' class="LC_odd_row"';
 6315:     if ($rownum ne '') { 
 6316:         $css_class = ($rownum%2? ' class="LC_odd_row"':'');
 6317:     }
 6318:     my $output = '<tr'.$css_class.'>'.
 6319:                  '<td>'.$lt{$context}.$showdom.
 6320:                  '</td><td class="'.$class.'" colspan="2"><table>';
 6321:     my $rem;
 6322:     if (ref($types) eq 'ARRAY') {
 6323:         for (my $i=0; $i<@{$types}; $i++) {
 6324:             if (defined($usertypes->{$types->[$i]})) {
 6325:                 my $rem = $i%($numinrow);
 6326:                 if ($rem == 0) {
 6327:                     if ($i > 0) {
 6328:                         $output .= '</tr>';
 6329:                     }
 6330:                     $output .= '<tr>';
 6331:                 }
 6332:                 my $check = ' ';
 6333:                 if (ref($settings) eq 'HASH') {
 6334:                     if (ref($settings->{$context}) eq 'ARRAY') {
 6335:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 6336:                             $check = ' checked="checked" ';
 6337:                         }
 6338:                     } elsif ($context eq 'statustocreate') {
 6339:                         $check = ' checked="checked" ';
 6340:                     }
 6341:                 }
 6342:                 $output .= '<td class="LC_left_item">'.
 6343:                            '<span class="LC_nobreak"><label>'.
 6344:                            '<input type="checkbox" name="'.$context.'" '.
 6345:                            'value="'.$types->[$i].'"'.$check.'/>'.
 6346:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 6347:             }
 6348:         }
 6349:         $rem = @{$types}%($numinrow);
 6350:     }
 6351:     my $colsleft = $numinrow - $rem;
 6352:     if (($rem == 0) && (@{$types} > 0)) {
 6353:         $output .= '<tr>';
 6354:     }
 6355:     if ($colsleft > 1) {
 6356:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 6357:     } else {
 6358:         $output .= '<td class="LC_left_item">';
 6359:     }
 6360:     my $defcheck = ' ';
 6361:     if (ref($settings) eq 'HASH') {  
 6362:         if (ref($settings->{$context}) eq 'ARRAY') {
 6363:             if (grep(/^default$/,@{$settings->{$context}})) {
 6364:                 $defcheck = ' checked="checked" ';
 6365:             }
 6366:         } elsif ($context eq 'statustocreate') {
 6367:             $defcheck = ' checked="checked" ';
 6368:         }
 6369:     }
 6370:     $output .= '<span class="LC_nobreak"><label>'.
 6371:                '<input type="checkbox" name="'.$context.'" '.
 6372:                'value="default"'.$defcheck.'/>'.
 6373:                $othertitle.'</label></span></td>'.
 6374:                '</tr></table></td></tr>';
 6375:     return $output;
 6376: }
 6377: 
 6378: sub sorted_searchtitles {
 6379:     my %searchtitles = &Apache::lonlocal::texthash(
 6380:                          'uname' => 'username',
 6381:                          'lastname' => 'last name',
 6382:                          'lastfirst' => 'last name, first name',
 6383:                      );
 6384:     my @titleorder = ('uname','lastname','lastfirst');
 6385:     return (\%searchtitles,\@titleorder);
 6386: }
 6387: 
 6388: sub sorted_searchtypes {
 6389:     my %srchtypes_desc = (
 6390:                            exact    => 'is exact match',
 6391:                            contains => 'contains ..',
 6392:                            begins   => 'begins with ..',
 6393:                          );
 6394:     my @srchtypeorder = ('exact','begins','contains');
 6395:     return (\%srchtypes_desc,\@srchtypeorder);
 6396: }
 6397: 
 6398: sub usertype_update_row {
 6399:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 6400:     my $datatable;
 6401:     my $numinrow = 4;
 6402:     foreach my $type (@{$types}) {
 6403:         if (defined($usertypes->{$type})) {
 6404:             $$rownums ++;
 6405:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 6406:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 6407:                           '</td><td class="LC_left_item"><table>';
 6408:             for (my $i=0; $i<@{$fields}; $i++) {
 6409:                 my $rem = $i%($numinrow);
 6410:                 if ($rem == 0) {
 6411:                     if ($i > 0) {
 6412:                         $datatable .= '</tr>';
 6413:                     }
 6414:                     $datatable .= '<tr>';
 6415:                 }
 6416:                 my $check = ' ';
 6417:                 if (ref($settings) eq 'HASH') {
 6418:                     if (ref($settings->{'fields'}) eq 'HASH') {
 6419:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 6420:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 6421:                                 $check = ' checked="checked" ';
 6422:                             }
 6423:                         }
 6424:                     }
 6425:                 }
 6426: 
 6427:                 if ($i == @{$fields}-1) {
 6428:                     my $colsleft = $numinrow - $rem;
 6429:                     if ($colsleft > 1) {
 6430:                         $datatable .= '<td colspan="'.$colsleft.'">';
 6431:                     } else {
 6432:                         $datatable .= '<td>';
 6433:                     }
 6434:                 } else {
 6435:                     $datatable .= '<td>';
 6436:                 }
 6437:                 $datatable .= '<span class="LC_nobreak"><label>'.
 6438:                               '<input type="checkbox" name="updateable_'.$type.
 6439:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 6440:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 6441:             }
 6442:             $datatable .= '</tr></table></td></tr>';
 6443:         }
 6444:     }
 6445:     return $datatable;
 6446: }
 6447: 
 6448: sub modify_login {
 6449:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 6450:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 6451:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 6452:     %title = ( coursecatalog => 'Display course catalog',
 6453:                adminmail => 'Display administrator E-mail address',
 6454:                helpdesk  => 'Display "Contact Helpdesk" link',
 6455:                newuser => 'Link for visitors to create a user account',
 6456:                loginheader => 'Log-in box header');
 6457:     @offon = ('off','on');
 6458:     if (ref($domconfig{login}) eq 'HASH') {
 6459:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 6460:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 6461:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 6462:             }
 6463:         }
 6464:     }
 6465:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 6466:                                            \%domconfig,\%loginhash);
 6467:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 6468:     foreach my $item (@toggles) {
 6469:         $loginhash{login}{$item} = $env{'form.'.$item};
 6470:     }
 6471:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 6472:     if (ref($colchanges{'login'}) eq 'HASH') {  
 6473:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 6474:                                          \%loginhash);
 6475:     }
 6476: 
 6477:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 6478:     my %domservers = &Apache::lonnet::get_servers($dom);
 6479:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 6480:     if (keys(%servers) > 1) {
 6481:         foreach my $lonhost (keys(%servers)) {
 6482:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 6483:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 6484:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 6485:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 6486:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 6487:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 6488:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 6489:                         $changes{'loginvia'}{$lonhost} = 1;
 6490:                     } else {
 6491:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 6492:                         $changes{'loginvia'}{$lonhost} = 1;
 6493:                     }
 6494:                 } else {
 6495:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 6496:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 6497:                         $changes{'loginvia'}{$lonhost} = 1;
 6498:                     }
 6499:                 }
 6500:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 6501:                     foreach my $item (@loginvia_attribs) {
 6502:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 6503:                     }
 6504:                 } else {
 6505:                     foreach my $item (@loginvia_attribs) {
 6506:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 6507:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 6508:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 6509:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 6510:                                 $new = '/';
 6511:                             }
 6512:                         }
 6513:                         if (($item eq 'custompath') && 
 6514:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 6515:                             $new = '';
 6516:                         }
 6517:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 6518:                             $changes{'loginvia'}{$lonhost} = 1;
 6519:                         }
 6520:                         if ($item eq 'exempt') {
 6521:                             $new = &check_exempt_addresses($new);
 6522:                         }
 6523:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 6524:                     }
 6525:                 }
 6526:             } else {
 6527:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 6528:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 6529:                     $changes{'loginvia'}{$lonhost} = 1;
 6530:                     foreach my $item (@loginvia_attribs) {
 6531:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 6532:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 6533:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 6534:                                 $new = '/';
 6535:                             }
 6536:                         }
 6537:                         if (($item eq 'custompath') && 
 6538:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 6539:                             $new = '';
 6540:                         }
 6541:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 6542:                     }
 6543:                 }
 6544:             }
 6545:         }
 6546:     }
 6547: 
 6548:     my $servadm = $r->dir_config('lonAdmEMail');
 6549:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 6550:     if (ref($domconfig{'login'}) eq 'HASH') {
 6551:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 6552:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 6553:                 if ($lang eq 'nolang') {
 6554:                     push(@currlangs,$lang);
 6555:                 } elsif (defined($langchoices{$lang})) {
 6556:                     push(@currlangs,$lang);
 6557:                 } else {
 6558:                     next;
 6559:                 }
 6560:             }
 6561:         }
 6562:     }
 6563:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 6564:     if (@currlangs > 0) {
 6565:         foreach my $lang (@currlangs) {
 6566:             if (grep(/^\Q$lang\E$/,@delurls)) {
 6567:                 $changes{'helpurl'}{$lang} = 1;
 6568:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 6569:                 $changes{'helpurl'}{$lang} = 1;
 6570:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 6571:                 push(@newlangs,$lang);
 6572:             } else {
 6573:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 6574:             }
 6575:         }
 6576:     }
 6577:     unless (grep(/^nolang$/,@currlangs)) {
 6578:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 6579:             $changes{'helpurl'}{'nolang'} = 1;
 6580:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 6581:             push(@newlangs,'nolang');
 6582:         }
 6583:     }
 6584:     if ($env{'form.loginhelpurl_add_lang'}) {
 6585:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 6586:             ($env{'form.loginhelpurl_add_file.filename'})) {
 6587:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 6588:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 6589:         }
 6590:     }
 6591:     if ((@newlangs > 0) || ($addedfile)) {
 6592:         my $error;
 6593:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 6594:         if ($configuserok eq 'ok') {
 6595:             if ($switchserver) {
 6596:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 6597:             } elsif ($author_ok eq 'ok') {
 6598:                 my @allnew = @newlangs;
 6599:                 if ($addedfile ne '') {
 6600:                     push(@allnew,$addedfile);
 6601:                 }
 6602:                 foreach my $lang (@allnew) {
 6603:                     my $formelem = 'loginhelpurl_'.$lang;
 6604:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 6605:                         $formelem = 'loginhelpurl_add_file';
 6606:                     }
 6607:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 6608:                                                                "help/$lang",'','',$newfile{$lang});
 6609:                     if ($result eq 'ok') {
 6610:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 6611:                         $changes{'helpurl'}{$lang} = 1;
 6612:                     } else {
 6613:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 6614:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 6615:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 6616:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 6617: 
 6618:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 6619:                         }
 6620:                     }
 6621:                 }
 6622:             } else {
 6623:                 $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);
 6624:             }
 6625:         } else {
 6626:             $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);
 6627:         }
 6628:         if ($error) {
 6629:             &Apache::lonnet::logthis($error);
 6630:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6631:         }
 6632:     }
 6633: 
 6634:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
 6635:     if (ref($domconfig{'login'}) eq 'HASH') {
 6636:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
 6637:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
 6638:                 if ($domservers{$lonhost}) {
 6639:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 6640:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
 6641:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
 6642:                     }
 6643:                 }
 6644:             }
 6645:         }
 6646:     }
 6647:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
 6648:     foreach my $lonhost (sort(keys(%domservers))) {
 6649:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 6650:             $changes{'headtag'}{$lonhost} = 1;
 6651:         } else {
 6652:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
 6653:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
 6654:             }
 6655:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
 6656:                 push(@newhosts,$lonhost);
 6657:             } elsif ($currheadtagurls{$lonhost}) {
 6658:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
 6659:                 if ($currexempt{$lonhost}) {
 6660:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
 6661:                         $changes{'headtag'}{$lonhost} = 1;
 6662:                     }
 6663:                 } elsif ($possexempt{$lonhost}) {
 6664:                     $changes{'headtag'}{$lonhost} = 1;
 6665:                 }
 6666:                 if ($possexempt{$lonhost}) {
 6667:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 6668:                 }
 6669:             }
 6670:         }
 6671:     }
 6672:     if (@newhosts) {
 6673:         my $error;
 6674:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 6675:         if ($configuserok eq 'ok') {
 6676:             if ($switchserver) {
 6677:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
 6678:             } elsif ($author_ok eq 'ok') {
 6679:                 foreach my $lonhost (@newhosts) {
 6680:                     my $formelem = 'loginheadtag_'.$lonhost;
 6681:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 6682:                                                                           "login/headtag/$lonhost",'','',
 6683:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
 6684:                     if ($result eq 'ok') {
 6685:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
 6686:                         $changes{'headtag'}{$lonhost} = 1;
 6687:                         if ($possexempt{$lonhost}) {
 6688:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 6689:                         }
 6690:                     } else {
 6691:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
 6692:                                            $newheadtagurls{$lonhost},$result);
 6693:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 6694:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
 6695:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
 6696:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
 6697:                         }
 6698:                     }
 6699:                 }
 6700:             } else {
 6701:                 $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);
 6702:             }
 6703:         } else {
 6704:             $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);
 6705:         }
 6706:         if ($error) {
 6707:             &Apache::lonnet::logthis($error);
 6708:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6709:         }
 6710:     }
 6711: 
 6712:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 6713: 
 6714:     my $defaulthelpfile = '/adm/loginproblems.html';
 6715:     my $defaulttext = &mt('Default in use');
 6716: 
 6717:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 6718:                                              $dom);
 6719:     if ($putresult eq 'ok') {
 6720:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 6721:         my %defaultchecked = (
 6722:                     'coursecatalog' => 'on',
 6723:                     'helpdesk'      => 'on',
 6724:                     'adminmail'     => 'off',
 6725:                     'newuser'       => 'off',
 6726:         );
 6727:         if (ref($domconfig{'login'}) eq 'HASH') {
 6728:             foreach my $item (@toggles) {
 6729:                 if ($defaultchecked{$item} eq 'on') { 
 6730:                     if (($domconfig{'login'}{$item} eq '0') &&
 6731:                         ($env{'form.'.$item} eq '1')) {
 6732:                         $changes{$item} = 1;
 6733:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 6734:                               $domconfig{'login'}{$item} eq '1') &&
 6735:                              ($env{'form.'.$item} eq '0')) {
 6736:                         $changes{$item} = 1;
 6737:                     }
 6738:                 } elsif ($defaultchecked{$item} eq 'off') {
 6739:                     if (($domconfig{'login'}{$item} eq '1') &&
 6740:                         ($env{'form.'.$item} eq '0')) {
 6741:                         $changes{$item} = 1;
 6742:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 6743:                               $domconfig{'login'}{$item} eq '0') &&
 6744:                              ($env{'form.'.$item} eq '1')) {
 6745:                         $changes{$item} = 1;
 6746:                     }
 6747:                 }
 6748:             }
 6749:         }
 6750:         if (keys(%changes) > 0 || $colchgtext) {
 6751:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 6752:             if (ref($lastactref) eq 'HASH') {
 6753:                 $lastactref->{'domainconfig'} = 1;
 6754:             }
 6755:             $resulttext = &mt('Changes made:').'<ul>';
 6756:             foreach my $item (sort(keys(%changes))) {
 6757:                 if ($item eq 'loginvia') {
 6758:                     if (ref($changes{$item}) eq 'HASH') {
 6759:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 6760:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 6761:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 6762:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 6763:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 6764:                                     $protocol = 'http' if ($protocol ne 'https');
 6765:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 6766: 
 6767:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 6768:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 6769:                                     } else {
 6770:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 6771:                                     }
 6772:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 6773:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 6774:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 6775:                                     }
 6776:                                     $resulttext .= '</li>';
 6777:                                 } else {
 6778:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 6779:                                 }
 6780:                             } else {
 6781:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 6782:                             }
 6783:                         }
 6784:                         $resulttext .= '</ul></li>';
 6785:                     }
 6786:                 } elsif ($item eq 'helpurl') {
 6787:                     if (ref($changes{$item}) eq 'HASH') {
 6788:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 6789:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 6790:                                 my ($chg,$link);
 6791:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 6792:                                 if ($lang eq 'nolang') {
 6793:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 6794:                                 } else {
 6795:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 6796:                                 }
 6797:                                 $resulttext .= '<li>'.$chg.'</li>';
 6798:                             } else {
 6799:                                 my $chg;
 6800:                                 if ($lang eq 'nolang') {
 6801:                                     $chg = &mt('custom log-in help file for no preferred language');
 6802:                                 } else {
 6803:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 6804:                                 }
 6805:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 6806:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 6807:                                                       '?inhibitmenu=yes',$chg,600,500).
 6808:                                                '</li>';
 6809:                             }
 6810:                         }
 6811:                     }
 6812:                 } elsif ($item eq 'headtag') {
 6813:                     if (ref($changes{$item}) eq 'HASH') {
 6814:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 6815:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 6816:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
 6817:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 6818:                                 $resulttext .= '<li><a href="'.
 6819:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
 6820:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 6821:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
 6822:                                 if ($possexempt{$lonhost}) {
 6823:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
 6824:                                 } else {
 6825:                                     $resulttext .= &mt('included for any client IP');
 6826:                                 }
 6827:                                 $resulttext .= '</li>';
 6828:                             }
 6829:                         }
 6830:                     }
 6831:                 } elsif ($item eq 'captcha') {
 6832:                     if (ref($loginhash{'login'}) eq 'HASH') {
 6833:                         my $chgtxt;
 6834:                         if ($loginhash{'login'}{$item} eq 'notused') {
 6835:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 6836:                         } else {
 6837:                             my %captchas = &captcha_phrases();
 6838:                             if ($captchas{$loginhash{'login'}{$item}}) {
 6839:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 6840:                             } else {
 6841:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 6842:                             }
 6843:                         }
 6844:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 6845:                     }
 6846:                 } elsif ($item eq 'recaptchakeys') {
 6847:                     if (ref($loginhash{'login'}) eq 'HASH') {
 6848:                         my ($privkey,$pubkey);
 6849:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 6850:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 6851:                             $privkey = $loginhash{'login'}{$item}{'private'};
 6852:                         }
 6853:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 6854:                         if (!$pubkey) {
 6855:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 6856:                         } else {
 6857:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 6858:                         }
 6859:                         if (!$privkey) {
 6860:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 6861:                         } else {
 6862:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 6863:                         }
 6864:                         $chgtxt .= '</ul>';
 6865:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 6866:                     }
 6867:                 } elsif ($item eq 'recaptchaversion') {
 6868:                     if (ref($loginhash{'login'}) eq 'HASH') {
 6869:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
 6870:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
 6871:                                            '</li>';
 6872:                         }
 6873:                     }
 6874:                 } else {
 6875:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 6876:                 }
 6877:             }
 6878:             $resulttext .= $colchgtext.'</ul>';
 6879:         } else {
 6880:             $resulttext = &mt('No changes made to log-in page settings');
 6881:         }
 6882:     } else {
 6883:         $resulttext = '<span class="LC_error">'.
 6884: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6885:     }
 6886:     if ($errors) {
 6887:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 6888:                        $errors.'</ul>';
 6889:     }
 6890:     return $resulttext;
 6891: }
 6892: 
 6893: sub check_exempt_addresses {
 6894:     my ($iplist) = @_;
 6895:     $iplist =~ s/^\s+//;
 6896:     $iplist =~ s/\s+$//;
 6897:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
 6898:     my (@okips,$new);
 6899:     foreach my $ip (@poss_ips) {
 6900:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 6901:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 6902:                 push(@okips,$ip);
 6903:             }
 6904:         }
 6905:     }
 6906:     if (@okips > 0) {
 6907:         $new = join(',',@okips);
 6908:     } else {
 6909:         $new = '';
 6910:     }
 6911:     return $new;
 6912: }
 6913: 
 6914: sub color_font_choices {
 6915:     my %choices =
 6916:         &Apache::lonlocal::texthash (
 6917:             img => "Header",
 6918:             bgs => "Background colors",
 6919:             links => "Link colors",
 6920:             images => "Images",
 6921:             font => "Font color",
 6922:             fontmenu => "Font menu",
 6923:             pgbg => "Page",
 6924:             tabbg => "Header",
 6925:             sidebg => "Border",
 6926:             link => "Link",
 6927:             alink => "Active link",
 6928:             vlink => "Visited link",
 6929:         );
 6930:     return %choices;
 6931: }
 6932: 
 6933: sub modify_rolecolors {
 6934:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 6935:     my ($resulttext,%rolehash);
 6936:     $rolehash{'rolecolors'} = {};
 6937:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 6938:         if ($domconfig{'rolecolors'} eq '') {
 6939:             $domconfig{'rolecolors'} = {};
 6940:         }
 6941:     }
 6942:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 6943:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 6944:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 6945:                                              $dom);
 6946:     if ($putresult eq 'ok') {
 6947:         if (keys(%changes) > 0) {
 6948:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 6949:             if (ref($lastactref) eq 'HASH') {
 6950:                 $lastactref->{'domainconfig'} = 1;
 6951:             }
 6952:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 6953:                                              $rolehash{'rolecolors'});
 6954:         } else {
 6955:             $resulttext = &mt('No changes made to default color schemes');
 6956:         }
 6957:     } else {
 6958:         $resulttext = '<span class="LC_error">'.
 6959: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6960:     }
 6961:     if ($errors) {
 6962:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 6963:                        $errors.'</ul>';
 6964:     }
 6965:     return $resulttext;
 6966: }
 6967: 
 6968: sub modify_colors {
 6969:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 6970:     my (%changes,%choices);
 6971:     my @bgs;
 6972:     my @links = ('link','alink','vlink');
 6973:     my @logintext;
 6974:     my @images;
 6975:     my $servadm = $r->dir_config('lonAdmEMail');
 6976:     my $errors;
 6977:     my %defaults;
 6978:     foreach my $role (@{$roles}) {
 6979:         if ($role eq 'login') {
 6980:             %choices = &login_choices();
 6981:             @logintext = ('textcol','bgcol');
 6982:         } else {
 6983:             %choices = &color_font_choices();
 6984:         }
 6985:         if ($role eq 'login') {
 6986:             @images = ('img','logo','domlogo','login');
 6987:             @bgs = ('pgbg','mainbg','sidebg');
 6988:         } else {
 6989:             @images = ('img');
 6990:             @bgs = ('pgbg','tabbg','sidebg');
 6991:         }
 6992:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 6993:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 6994:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 6995:         }
 6996:         if ($role eq 'login') {
 6997:             foreach my $item (@logintext) {
 6998:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6999:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 7000:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 7001:                 }
 7002:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 7003:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 7004:                 }
 7005:             }
 7006:         } else {
 7007:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 7008:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 7009:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 7010:             }
 7011:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 7012:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 7013:             }
 7014:         }
 7015:         foreach my $item (@bgs) {
 7016:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 7017:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 7018:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 7019:             }
 7020:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 7021:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 7022:             }
 7023:         }
 7024:         foreach my $item (@links) {
 7025:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 7026:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 7027:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 7028:             }
 7029:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 7030:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 7031:             }
 7032:         }
 7033:         my ($configuserok,$author_ok,$switchserver) = 
 7034:             &config_check($dom,$confname,$servadm);
 7035:         my ($width,$height) = &thumb_dimensions();
 7036:         if (ref($domconfig->{$role}) ne 'HASH') {
 7037:             $domconfig->{$role} = {};
 7038:         }
 7039:         foreach my $img (@images) {
 7040:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 7041:                 if (defined($env{'form.login_showlogo_'.$img})) {
 7042:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 7043:                 } else { 
 7044:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 7045:                 }
 7046:             } 
 7047: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 7048: 		 && !defined($domconfig->{$role}{$img})
 7049: 		 && !$env{'form.'.$role.'_del_'.$img}
 7050: 		 && $env{'form.'.$role.'_import_'.$img}) {
 7051: 		# import the old configured image from the .tab setting
 7052: 		# if they haven't provided a new one 
 7053: 		$domconfig->{$role}{$img} = 
 7054: 		    $env{'form.'.$role.'_import_'.$img};
 7055: 	    }
 7056:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 7057:                 my $error;
 7058:                 if ($configuserok eq 'ok') {
 7059:                     if ($switchserver) {
 7060:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 7061:                     } else {
 7062:                         if ($author_ok eq 'ok') {
 7063:                             my ($result,$logourl) = 
 7064:                                 &publishlogo($r,'upload',$role.'_'.$img,
 7065:                                            $dom,$confname,$img,$width,$height);
 7066:                             if ($result eq 'ok') {
 7067:                                 $confhash->{$role}{$img} = $logourl;
 7068:                                 $changes{$role}{'images'}{$img} = 1;
 7069:                             } else {
 7070:                                 $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);
 7071:                             }
 7072:                         } else {
 7073:                             $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);
 7074:                         }
 7075:                     }
 7076:                 } else {
 7077:                     $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);
 7078:                 }
 7079:                 if ($error) {
 7080:                     &Apache::lonnet::logthis($error);
 7081:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7082:                 }
 7083:             } elsif ($domconfig->{$role}{$img} ne '') {
 7084:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 7085:                     my $error;
 7086:                     if ($configuserok eq 'ok') {
 7087: # is confname an author?
 7088:                         if ($switchserver eq '') {
 7089:                             if ($author_ok eq 'ok') {
 7090:                                 my ($result,$logourl) = 
 7091:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 7092:                                             $dom,$confname,$img,$width,$height);
 7093:                                 if ($result eq 'ok') {
 7094:                                     $confhash->{$role}{$img} = $logourl;
 7095: 				    $changes{$role}{'images'}{$img} = 1;
 7096:                                 }
 7097:                             }
 7098:                         }
 7099:                     }
 7100:                 }
 7101:             }
 7102:         }
 7103:         if (ref($domconfig) eq 'HASH') {
 7104:             if (ref($domconfig->{$role}) eq 'HASH') {
 7105:                 foreach my $img (@images) {
 7106:                     if ($domconfig->{$role}{$img} ne '') {
 7107:                         if ($env{'form.'.$role.'_del_'.$img}) {
 7108:                             $confhash->{$role}{$img} = '';
 7109:                             $changes{$role}{'images'}{$img} = 1;
 7110:                         } else {
 7111:                             if ($confhash->{$role}{$img} eq '') {
 7112:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 7113:                             }
 7114:                         }
 7115:                     } else {
 7116:                         if ($env{'form.'.$role.'_del_'.$img}) {
 7117:                             $confhash->{$role}{$img} = '';
 7118:                             $changes{$role}{'images'}{$img} = 1;
 7119:                         } 
 7120:                     }
 7121:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 7122:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 7123:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 7124:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 7125:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 7126:                             }
 7127:                         } else {
 7128:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 7129:                                 $changes{$role}{'showlogo'}{$img} = 1;
 7130:                             }
 7131:                         }
 7132:                     }
 7133:                 }
 7134:                 if ($domconfig->{$role}{'font'} ne '') {
 7135:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 7136:                         $changes{$role}{'font'} = 1;
 7137:                     }
 7138:                 } else {
 7139:                     if ($confhash->{$role}{'font'}) {
 7140:                         $changes{$role}{'font'} = 1;
 7141:                     }
 7142:                 }
 7143:                 if ($role ne 'login') {
 7144:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 7145:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 7146:                             $changes{$role}{'fontmenu'} = 1;
 7147:                         }
 7148:                     } else {
 7149:                         if ($confhash->{$role}{'fontmenu'}) {
 7150:                             $changes{$role}{'fontmenu'} = 1;
 7151:                         }
 7152:                     }
 7153:                 }
 7154:                 foreach my $item (@bgs) {
 7155:                     if ($domconfig->{$role}{$item} ne '') {
 7156:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 7157:                             $changes{$role}{'bgs'}{$item} = 1;
 7158:                         } 
 7159:                     } else {
 7160:                         if ($confhash->{$role}{$item}) {
 7161:                             $changes{$role}{'bgs'}{$item} = 1;
 7162:                         }
 7163:                     }
 7164:                 }
 7165:                 foreach my $item (@links) {
 7166:                     if ($domconfig->{$role}{$item} ne '') {
 7167:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 7168:                             $changes{$role}{'links'}{$item} = 1;
 7169:                         }
 7170:                     } else {
 7171:                         if ($confhash->{$role}{$item}) {
 7172:                             $changes{$role}{'links'}{$item} = 1;
 7173:                         }
 7174:                     }
 7175:                 }
 7176:                 foreach my $item (@logintext) {
 7177:                     if ($domconfig->{$role}{$item} ne '') {
 7178:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 7179:                             $changes{$role}{'logintext'}{$item} = 1;
 7180:                         }
 7181:                     } else {
 7182:                         if ($confhash->{$role}{$item}) {
 7183:                             $changes{$role}{'logintext'}{$item} = 1;
 7184:                         }
 7185:                     }
 7186:                 }
 7187:             } else {
 7188:                 &default_change_checker($role,\@images,\@links,\@bgs,
 7189:                                         \@logintext,$confhash,\%changes); 
 7190:             }
 7191:         } else {
 7192:             &default_change_checker($role,\@images,\@links,\@bgs,
 7193:                                     \@logintext,$confhash,\%changes); 
 7194:         }
 7195:     }
 7196:     return ($errors,%changes);
 7197: }
 7198: 
 7199: sub config_check {
 7200:     my ($dom,$confname,$servadm) = @_;
 7201:     my ($configuserok,$author_ok,$switchserver,%currroles);
 7202:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 7203:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 7204:                                                    $confname,$servadm);
 7205:     if ($configuserok eq 'ok') {
 7206:         $switchserver = &check_switchserver($dom,$confname);
 7207:         if ($switchserver eq '') {
 7208:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 7209:         }
 7210:     }
 7211:     return ($configuserok,$author_ok,$switchserver);
 7212: }
 7213: 
 7214: sub default_change_checker {
 7215:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 7216:     foreach my $item (@{$links}) {
 7217:         if ($confhash->{$role}{$item}) {
 7218:             $changes->{$role}{'links'}{$item} = 1;
 7219:         }
 7220:     }
 7221:     foreach my $item (@{$bgs}) {
 7222:         if ($confhash->{$role}{$item}) {
 7223:             $changes->{$role}{'bgs'}{$item} = 1;
 7224:         }
 7225:     }
 7226:     foreach my $item (@{$logintext}) {
 7227:         if ($confhash->{$role}{$item}) {
 7228:             $changes->{$role}{'logintext'}{$item} = 1;
 7229:         }
 7230:     }
 7231:     foreach my $img (@{$images}) {
 7232:         if ($env{'form.'.$role.'_del_'.$img}) {
 7233:             $confhash->{$role}{$img} = '';
 7234:             $changes->{$role}{'images'}{$img} = 1;
 7235:         }
 7236:         if ($role eq 'login') {
 7237:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 7238:                 $changes->{$role}{'showlogo'}{$img} = 1;
 7239:             }
 7240:         }
 7241:     }
 7242:     if ($confhash->{$role}{'font'}) {
 7243:         $changes->{$role}{'font'} = 1;
 7244:     }
 7245: }
 7246: 
 7247: sub display_colorchgs {
 7248:     my ($dom,$changes,$roles,$confhash) = @_;
 7249:     my (%choices,$resulttext);
 7250:     if (!grep(/^login$/,@{$roles})) {
 7251:         $resulttext = &mt('Changes made:').'<br />';
 7252:     }
 7253:     foreach my $role (@{$roles}) {
 7254:         if ($role eq 'login') {
 7255:             %choices = &login_choices();
 7256:         } else {
 7257:             %choices = &color_font_choices();
 7258:         }
 7259:         if (ref($changes->{$role}) eq 'HASH') {
 7260:             if ($role ne 'login') {
 7261:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 7262:             }
 7263:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 7264:                 if ($role ne 'login') {
 7265:                     $resulttext .= '<ul>';
 7266:                 }
 7267:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 7268:                     if ($role ne 'login') {
 7269:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 7270:                     }
 7271:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 7272:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 7273:                             if ($confhash->{$role}{$key}{$item}) {
 7274:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 7275:                             } else {
 7276:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 7277:                             }
 7278:                         } elsif ($confhash->{$role}{$item} eq '') {
 7279:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 7280:                         } else {
 7281:                             my $newitem = $confhash->{$role}{$item};
 7282:                             if ($key eq 'images') {
 7283:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 7284:                             }
 7285:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 7286:                         }
 7287:                     }
 7288:                     if ($role ne 'login') {
 7289:                         $resulttext .= '</ul></li>';
 7290:                     }
 7291:                 } else {
 7292:                     if ($confhash->{$role}{$key} eq '') {
 7293:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 7294:                     } else {
 7295:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 7296:                     }
 7297:                 }
 7298:                 if ($role ne 'login') {
 7299:                     $resulttext .= '</ul>';
 7300:                 }
 7301:             }
 7302:         }
 7303:     }
 7304:     return $resulttext;
 7305: }
 7306: 
 7307: sub thumb_dimensions {
 7308:     return ('200','50');
 7309: }
 7310: 
 7311: sub check_dimensions {
 7312:     my ($inputfile) = @_;
 7313:     my ($fullwidth,$fullheight);
 7314:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 7315:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 7316:             my $imageinfo = <PIPE>;
 7317:             if (!close(PIPE)) {
 7318:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 7319:             }
 7320:             chomp($imageinfo);
 7321:             my ($fullsize) = 
 7322:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 7323:             if ($fullsize) {
 7324:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 7325:             }
 7326:         }
 7327:     }
 7328:     return ($fullwidth,$fullheight);
 7329: }
 7330: 
 7331: sub check_configuser {
 7332:     my ($uhome,$dom,$confname,$servadm) = @_;
 7333:     my ($configuserok,%currroles);
 7334:     if ($uhome eq 'no_host') {
 7335:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 7336:         my $configpass = &LONCAPA::Enrollment::create_password();
 7337:         $configuserok = 
 7338:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 7339:                              $configpass,'','','','','',undef,$servadm);
 7340:     } else {
 7341:         $configuserok = 'ok';
 7342:         %currroles = 
 7343:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 7344:     }
 7345:     return ($configuserok,%currroles);
 7346: }
 7347: 
 7348: sub check_authorstatus {
 7349:     my ($dom,$confname,%currroles) = @_;
 7350:     my $author_ok;
 7351:     if (!$currroles{':'.$dom.':au'}) {
 7352:         my $start = time;
 7353:         my $end = 0;
 7354:         $author_ok = 
 7355:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 7356:                                         'au',$end,$start,'','','domconfig');
 7357:     } else {
 7358:         $author_ok = 'ok';
 7359:     }
 7360:     return $author_ok;
 7361: }
 7362: 
 7363: sub publishlogo {
 7364:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 7365:     my ($output,$fname,$logourl);
 7366:     if ($action eq 'upload') {
 7367:         $fname=$env{'form.'.$formname.'.filename'};
 7368:         chop($env{'form.'.$formname});
 7369:     } else {
 7370:         ($fname) = ($formname =~ /([^\/]+)$/);
 7371:     }
 7372:     if ($savefileas ne '') {
 7373:         $fname = $savefileas;
 7374:     }
 7375:     $fname=&Apache::lonnet::clean_filename($fname);
 7376: # See if there is anything left
 7377:     unless ($fname) { return ('error: no uploaded file'); }
 7378:     $fname="$subdir/$fname";
 7379:     my $docroot=$r->dir_config('lonDocRoot');
 7380:     my $filepath="$docroot/priv";
 7381:     my $relpath = "$dom/$confname";
 7382:     my ($fnamepath,$file,$fetchthumb);
 7383:     $file=$fname;
 7384:     if ($fname=~m|/|) {
 7385:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 7386:     }
 7387:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 7388:     my $count;
 7389:     for ($count=5;$count<=$#parts;$count++) {
 7390:         $filepath.="/$parts[$count]";
 7391:         if ((-e $filepath)!=1) {
 7392:             mkdir($filepath,02770);
 7393:         }
 7394:     }
 7395:     # Check for bad extension and disallow upload
 7396:     if ($file=~/\.(\w+)$/ &&
 7397:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 7398:         $output = 
 7399:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 7400:     } elsif ($file=~/\.(\w+)$/ &&
 7401:         !defined(&Apache::loncommon::fileembstyle($1))) {
 7402:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 7403:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 7404:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 7405:     } elsif (-d "$filepath/$file") {
 7406:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 7407:     } else {
 7408:         my $source = $filepath.'/'.$file;
 7409:         my $logfile;
 7410:         if (!open($logfile,">>$source".'.log')) {
 7411:             return (&mt('No write permission to Authoring Space'));
 7412:         }
 7413:         print $logfile
 7414: "\n================= Publish ".localtime()." ================\n".
 7415: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 7416: # Save the file
 7417:         if (!open(FH,'>'.$source)) {
 7418:             &Apache::lonnet::logthis('Failed to create '.$source);
 7419:             return (&mt('Failed to create file'));
 7420:         }
 7421:         if ($action eq 'upload') {
 7422:             if (!print FH ($env{'form.'.$formname})) {
 7423:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 7424:                 return (&mt('Failed to write file'));
 7425:             }
 7426:         } else {
 7427:             my $original = &Apache::lonnet::filelocation('',$formname);
 7428:             if(!copy($original,$source)) {
 7429:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 7430:                 return (&mt('Failed to write file'));
 7431:             }
 7432:         }
 7433:         close(FH);
 7434:         chmod(0660, $source); # Permissions to rw-rw---.
 7435: 
 7436:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 7437:         my $copyfile=$targetdir.'/'.$file;
 7438: 
 7439:         my @parts=split(/\//,$targetdir);
 7440:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 7441:         for (my $count=5;$count<=$#parts;$count++) {
 7442:             $path.="/$parts[$count]";
 7443:             if (!-e $path) {
 7444:                 print $logfile "\nCreating directory ".$path;
 7445:                 mkdir($path,02770);
 7446:             }
 7447:         }
 7448:         my $versionresult;
 7449:         if (-e $copyfile) {
 7450:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 7451:         } else {
 7452:             $versionresult = 'ok';
 7453:         }
 7454:         if ($versionresult eq 'ok') {
 7455:             if (copy($source,$copyfile)) {
 7456:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 7457:                 $output = 'ok';
 7458:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 7459:                 push(@{$modified_urls},[$copyfile,$source]);
 7460:                 my $metaoutput = 
 7461:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 7462:                 unless ($registered_cleanup) {
 7463:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 7464:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 7465:                     $registered_cleanup=1;
 7466:                 }
 7467:             } else {
 7468:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 7469:                 $output = &mt('Failed to copy file to RES space').", $!";
 7470:             }
 7471:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 7472:                 my $inputfile = $filepath.'/'.$file;
 7473:                 my $outfile = $filepath.'/'.'tn-'.$file;
 7474:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 7475:                 if ($fullwidth ne '' && $fullheight ne '') { 
 7476:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 7477:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 7478:                         system("convert -sample $thumbsize $inputfile $outfile");
 7479:                         chmod(0660, $filepath.'/tn-'.$file);
 7480:                         if (-e $outfile) {
 7481:                             my $copyfile=$targetdir.'/tn-'.$file;
 7482:                             if (copy($outfile,$copyfile)) {
 7483:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 7484:                                 my $thumb_metaoutput = 
 7485:                                     &write_metadata($dom,$confname,$formname,
 7486:                                                     $targetdir,'tn-'.$file,$logfile);
 7487:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 7488:                                 unless ($registered_cleanup) {
 7489:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 7490:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 7491:                                     $registered_cleanup=1;
 7492:                                 }
 7493:                             } else {
 7494:                                 print $logfile "\nUnable to write ".$copyfile.
 7495:                                                ':'.$!."\n";
 7496:                             }
 7497:                         }
 7498:                     }
 7499:                 }
 7500:             }
 7501:         } else {
 7502:             $output = $versionresult;
 7503:         }
 7504:     }
 7505:     return ($output,$logourl);
 7506: }
 7507: 
 7508: sub logo_versioning {
 7509:     my ($targetdir,$file,$logfile) = @_;
 7510:     my $target = $targetdir.'/'.$file;
 7511:     my ($maxversion,$fn,$extn,$output);
 7512:     $maxversion = 0;
 7513:     if ($file =~ /^(.+)\.(\w+)$/) {
 7514:         $fn=$1;
 7515:         $extn=$2;
 7516:     }
 7517:     opendir(DIR,$targetdir);
 7518:     while (my $filename=readdir(DIR)) {
 7519:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 7520:             $maxversion=($1>$maxversion)?$1:$maxversion;
 7521:         }
 7522:     }
 7523:     $maxversion++;
 7524:     print $logfile "\nCreating old version ".$maxversion."\n";
 7525:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 7526:     if (copy($target,$copyfile)) {
 7527:         print $logfile "Copied old target to ".$copyfile."\n";
 7528:         $copyfile=$copyfile.'.meta';
 7529:         if (copy($target.'.meta',$copyfile)) {
 7530:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 7531:             $output = 'ok';
 7532:         } else {
 7533:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 7534:             $output = &mt('Failed to copy old meta').", $!, ";
 7535:         }
 7536:     } else {
 7537:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 7538:         $output = &mt('Failed to copy old target').", $!, ";
 7539:     }
 7540:     return $output;
 7541: }
 7542: 
 7543: sub write_metadata {
 7544:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 7545:     my (%metadatafields,%metadatakeys,$output);
 7546:     $metadatafields{'title'}=$formname;
 7547:     $metadatafields{'creationdate'}=time;
 7548:     $metadatafields{'lastrevisiondate'}=time;
 7549:     $metadatafields{'copyright'}='public';
 7550:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 7551:                                          $env{'user.domain'};
 7552:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 7553:     $metadatafields{'domain'}=$dom;
 7554:     {
 7555:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 7556:         my $mfh;
 7557:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 7558:             foreach (sort(keys(%metadatafields))) {
 7559:                 unless ($_=~/\./) {
 7560:                     my $unikey=$_;
 7561:                     $unikey=~/^([A-Za-z]+)/;
 7562:                     my $tag=$1;
 7563:                     $tag=~tr/A-Z/a-z/;
 7564:                     print $mfh "\n\<$tag";
 7565:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 7566:                         my $value=$metadatafields{$unikey.'.'.$_};
 7567:                         $value=~s/\"/\'\'/g;
 7568:                         print $mfh ' '.$_.'="'.$value.'"';
 7569:                     }
 7570:                     print $mfh '>'.
 7571:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 7572:                             .'</'.$tag.'>';
 7573:                 }
 7574:             }
 7575:             $output = 'ok';
 7576:             print $logfile "\nWrote metadata";
 7577:             close($mfh);
 7578:         } else {
 7579:             print $logfile "\nFailed to open metadata file";
 7580:             $output = &mt('Could not write metadata');
 7581:         }
 7582:     }
 7583:     return $output;
 7584: }
 7585: 
 7586: sub notifysubscribed {
 7587:     foreach my $targetsource (@{$modified_urls}){
 7588:         next unless (ref($targetsource) eq 'ARRAY');
 7589:         my ($target,$source)=@{$targetsource};
 7590:         if ($source ne '') {
 7591:             if (open(my $logfh,'>>'.$source.'.log')) {
 7592:                 print $logfh "\nCleanup phase: Notifications\n";
 7593:                 my @subscribed=&subscribed_hosts($target);
 7594:                 foreach my $subhost (@subscribed) {
 7595:                     print $logfh "\nNotifying host ".$subhost.':';
 7596:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 7597:                     print $logfh $reply;
 7598:                 }
 7599:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 7600:                 foreach my $subhost (@subscribedmeta) {
 7601:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 7602:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 7603:                                                         $subhost);
 7604:                     print $logfh $reply;
 7605:                 }
 7606:                 print $logfh "\n============ Done ============\n";
 7607:                 close($logfh);
 7608:             }
 7609:         }
 7610:     }
 7611:     return OK;
 7612: }
 7613: 
 7614: sub subscribed_hosts {
 7615:     my ($target) = @_;
 7616:     my @subscribed;
 7617:     if (open(my $fh,"<$target.subscription")) {
 7618:         while (my $subline=<$fh>) {
 7619:             if ($subline =~ /^($match_lonid):/) {
 7620:                 my $host = $1;
 7621:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 7622:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 7623:                         push(@subscribed,$host);
 7624:                     }
 7625:                 }
 7626:             }
 7627:         }
 7628:     }
 7629:     return @subscribed;
 7630: }
 7631: 
 7632: sub check_switchserver {
 7633:     my ($dom,$confname) = @_;
 7634:     my ($allowed,$switchserver);
 7635:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 7636:     if ($home eq 'no_host') {
 7637:         $home = &Apache::lonnet::domain($dom,'primary');
 7638:     }
 7639:     my @ids=&Apache::lonnet::current_machine_ids();
 7640:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 7641:     if (!$allowed) {
 7642: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 7643:     }
 7644:     return $switchserver;
 7645: }
 7646: 
 7647: sub modify_quotas {
 7648:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 7649:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 7650:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 7651:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 7652:         $validationfieldsref);
 7653:     if ($action eq 'quotas') {
 7654:         $context = 'tools'; 
 7655:     } else {
 7656:         $context = $action;
 7657:     }
 7658:     if ($context eq 'requestcourses') {
 7659:         @usertools = ('official','unofficial','community','textbook');
 7660:         @options =('norequest','approval','validate','autolimit');
 7661:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 7662:         %titles = &courserequest_titles();
 7663:         $toolregexp = join('|',@usertools);
 7664:         %conditions = &courserequest_conditions();
 7665:         $confname = $dom.'-domainconfig';
 7666:         my $servadm = $r->dir_config('lonAdmEMail');
 7667:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 7668:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 7669:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 7670:     } elsif ($context eq 'requestauthor') {
 7671:         @usertools = ('author');
 7672:         %titles = &authorrequest_titles();
 7673:     } else {
 7674:         @usertools = ('aboutme','blog','webdav','portfolio');
 7675:         %titles = &tool_titles();
 7676:     }
 7677:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 7678:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7679:     foreach my $key (keys(%env)) {
 7680:         if ($context eq 'requestcourses') {
 7681:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 7682:                 my $item = $1;
 7683:                 my $type = $2;
 7684:                 if ($type =~ /^limit_(.+)/) {
 7685:                     $limithash{$item}{$1} = $env{$key};
 7686:                 } else {
 7687:                     $confhash{$item}{$type} = $env{$key};
 7688:                 }
 7689:             }
 7690:         } elsif ($context eq 'requestauthor') {
 7691:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 7692:                 $confhash{$1} = $env{$key};
 7693:             }
 7694:         } else {
 7695:             if ($key =~ /^form\.quota_(.+)$/) {
 7696:                 $confhash{'defaultquota'}{$1} = $env{$key};
 7697:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 7698:                 $confhash{'authorquota'}{$1} = $env{$key};
 7699:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 7700:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 7701:             }
 7702:         }
 7703:     }
 7704:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 7705:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 7706:         @approvalnotify = sort(@approvalnotify);
 7707:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 7708:         my @crstypes = ('official','unofficial','community','textbook');
 7709:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 7710:         foreach my $type (@hasuniquecode) {
 7711:             if (grep(/^\Q$type\E$/,@crstypes)) {
 7712:                 $confhash{'uniquecode'}{$type} = 1;
 7713:             }
 7714:         }
 7715:         my (%newbook,%allpos);
 7716:         if ($context eq 'requestcourses') {
 7717:             foreach my $type ('textbooks','templates') {
 7718:                 @{$allpos{$type}} = (); 
 7719:                 my $invalid;
 7720:                 if ($type eq 'textbooks') {
 7721:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
 7722:                 } else {
 7723:                     $invalid = &mt('Invalid LON-CAPA course for template');
 7724:                 }
 7725:                 if ($env{'form.'.$type.'_addbook'}) {
 7726:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
 7727:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
 7728:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
 7729:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
 7730:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 7731:                         } else {
 7732:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
 7733:                             my $position = $env{'form.'.$type.'_addbook_pos'};
 7734:                             $position =~ s/\D+//g;
 7735:                             if ($position ne '') {
 7736:                                 $allpos{$type}[$position] = $newbook{$type};
 7737:                             }
 7738:                         }
 7739:                     } else {
 7740:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 7741:                     }
 7742:                 }
 7743:             } 
 7744:         }
 7745:         if (ref($domconfig{$action}) eq 'HASH') {
 7746:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 7747:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 7748:                     $changes{'notify'}{'approval'} = 1;
 7749:                 }
 7750:             } else {
 7751:                 if ($confhash{'notify'}{'approval'}) {
 7752:                     $changes{'notify'}{'approval'} = 1;
 7753:                 }
 7754:             }
 7755:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 7756:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 7757:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 7758:                         unless ($confhash{'uniquecode'}{$crstype}) {
 7759:                             $changes{'uniquecode'} = 1;
 7760:                         }
 7761:                     }
 7762:                     unless ($changes{'uniquecode'}) {
 7763:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 7764:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 7765:                                 $changes{'uniquecode'} = 1;
 7766:                             }
 7767:                         }
 7768:                     }
 7769:                } else {
 7770:                    $changes{'uniquecode'} = 1;
 7771:                }
 7772:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 7773:                 $changes{'uniquecode'} = 1;
 7774:             }
 7775:             if ($context eq 'requestcourses') {
 7776:                 foreach my $type ('textbooks','templates') {
 7777:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
 7778:                         my %deletions;
 7779:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
 7780:                         if (@todelete) {
 7781:                             map { $deletions{$_} = 1; } @todelete;
 7782:                         }
 7783:                         my %imgdeletions;
 7784:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
 7785:                         if (@todeleteimages) {
 7786:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
 7787:                         }
 7788:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
 7789:                         for (my $i=0; $i<=$maxnum; $i++) {
 7790:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
 7791:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
 7792:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
 7793:                                 if ($deletions{$key}) {
 7794:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
 7795:                                         #FIXME need to obsolete item in RES space
 7796:                                     }
 7797:                                     next;
 7798:                                 } else {
 7799:                                     my $newpos = $env{'form.'.$itemid};
 7800:                                     $newpos =~ s/\D+//g;
 7801:                                     foreach my $item ('subject','title','publisher','author') {
 7802:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 7803:                                                  ($type eq 'templates'));
 7804:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
 7805:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
 7806:                                             $changes{$type}{$key} = 1;
 7807:                                         }
 7808:                                     }
 7809:                                     $allpos{$type}[$newpos] = $key;
 7810:                                 }
 7811:                                 if ($imgdeletions{$key}) {
 7812:                                     $changes{$type}{$key} = 1;
 7813:                                     #FIXME need to obsolete item in RES space
 7814:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
 7815:                                     my ($cdom,$cnum) = split(/_/,$key);
 7816:                                     my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
 7817:                                                                                   $cdom,$cnum,$type,$configuserok,
 7818:                                                                                   $switchserver,$author_ok);
 7819:                                     if ($imgurl) {
 7820:                                         $confhash{$type}{$key}{'image'} = $imgurl;
 7821:                                         $changes{$type}{$key} = 1; 
 7822:                                     }
 7823:                                     if ($error) {
 7824:                                         &Apache::lonnet::logthis($error);
 7825:                                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7826:                                     } 
 7827:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
 7828:                                     $confhash{$type}{$key}{'image'} = 
 7829:                                         $domconfig{$action}{$type}{$key}{'image'};
 7830:                                 }
 7831:                             }
 7832:                         }
 7833:                     }
 7834:                 }
 7835:             }
 7836:         } else {
 7837:             if ($confhash{'notify'}{'approval'}) {
 7838:                 $changes{'notify'}{'approval'} = 1;
 7839:             }
 7840:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 7841:                 $changes{'uniquecode'} = 1;
 7842:             }
 7843:         }
 7844:         if ($context eq 'requestcourses') {
 7845:             foreach my $type ('textbooks','templates') {
 7846:                 if ($newbook{$type}) {
 7847:                     $changes{$type}{$newbook{$type}} = 1;
 7848:                     foreach my $item ('subject','title','publisher','author') {
 7849:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 7850:                                  ($type eq 'template'));
 7851:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
 7852:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
 7853:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
 7854:                         }
 7855:                     }
 7856:                     if ($type eq 'textbooks') {
 7857:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
 7858:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
 7859:                             my ($imageurl,$error) =
 7860:                                 &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
 7861:                                                         $configuserok,$switchserver,$author_ok);
 7862:                             if ($imageurl) {
 7863:                                 $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
 7864:                             }
 7865:                             if ($error) {
 7866:                                 &Apache::lonnet::logthis($error);
 7867:                                 $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7868:                             }
 7869:                         }
 7870:                     }
 7871:                 }
 7872:                 if (@{$allpos{$type}} > 0) {
 7873:                     my $idx = 0;
 7874:                     foreach my $item (@{$allpos{$type}}) {
 7875:                         if ($item ne '') {
 7876:                             $confhash{$type}{$item}{'order'} = $idx;
 7877:                             if (ref($domconfig{$action}) eq 'HASH') {
 7878:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
 7879:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
 7880:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
 7881:                                             $changes{$type}{$item} = 1;
 7882:                                         }
 7883:                                     }
 7884:                                 }
 7885:                             }
 7886:                             $idx ++;
 7887:                         }
 7888:                     }
 7889:                 }
 7890:             }
 7891:             if (ref($validationitemsref) eq 'ARRAY') {
 7892:                 foreach my $item (@{$validationitemsref}) {
 7893:                     if ($item eq 'fields') {
 7894:                         my @changed;
 7895:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 7896:                         if (@{$confhash{'validation'}{$item}} > 0) {
 7897:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 7898:                         }
 7899:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 7900:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7901:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 7902:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 7903:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
 7904:                                 } else {
 7905:                                     @changed = @{$confhash{'validation'}{$item}};
 7906:                                 }
 7907:                             } else {
 7908:                                 @changed = @{$confhash{'validation'}{$item}};
 7909:                             }
 7910:                         } else {
 7911:                             @changed = @{$confhash{'validation'}{$item}};
 7912:                         }
 7913:                         if (@changed) {
 7914:                             if ($confhash{'validation'}{$item}) {
 7915:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 7916:                             } else {
 7917:                                 $changes{'validation'}{$item} = &mt('None');
 7918:                             }
 7919:                         }
 7920:                     } else {
 7921:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 7922:                         if ($item eq 'markup') {
 7923:                             if ($env{'form.requestcourses_validation_'.$item}) {
 7924:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 7925:                             }
 7926:                         }
 7927:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 7928:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7929:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 7930:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 7931:                                 }
 7932:                             } else {
 7933:                                 if ($confhash{'validation'}{$item} ne '') {
 7934:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 7935:                                 }
 7936:                             }
 7937:                         } else {
 7938:                             if ($confhash{'validation'}{$item} ne '') {
 7939:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 7940:                             }
 7941:                         }
 7942:                     }
 7943:                 }
 7944:             }
 7945:             if ($env{'form.validationdc'}) {
 7946:                 my $newval = $env{'form.validationdc'};
 7947:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
 7948:                 if (exists($domcoords{$newval})) {
 7949:                     $confhash{'validation'}{'dc'} = $newval;
 7950:                 }
 7951:             }
 7952:             if (ref($confhash{'validation'}) eq 'HASH') {
 7953:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 7954:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7955:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7956:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7957:                                 if ($confhash{'validation'}{'dc'} eq '') {
 7958:                                     $changes{'validation'}{'dc'} = &mt('None');
 7959:                                 } else {
 7960:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7961:                                 }
 7962:                             }
 7963:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
 7964:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7965:                         }
 7966:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 7967:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7968:                     }
 7969:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 7970:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7971:                 }
 7972:             } else {
 7973:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 7974:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7975:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7976:                             $changes{'validation'}{'dc'} = &mt('None');
 7977:                         }
 7978:                     }
 7979:                 }
 7980:             }
 7981:         }
 7982:     } else {
 7983:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 7984:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 7985:     }
 7986:     foreach my $item (@usertools) {
 7987:         foreach my $type (@{$types},'default','_LC_adv') {
 7988:             my $unset; 
 7989:             if ($context eq 'requestcourses') {
 7990:                 $unset = '0';
 7991:                 if ($type eq '_LC_adv') {
 7992:                     $unset = '';
 7993:                 }
 7994:                 if ($confhash{$item}{$type} eq 'autolimit') {
 7995:                     $confhash{$item}{$type} .= '=';
 7996:                     unless ($limithash{$item}{$type} =~ /\D/) {
 7997:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 7998:                     }
 7999:                 }
 8000:             } elsif ($context eq 'requestauthor') {
 8001:                 $unset = '0';
 8002:                 if ($type eq '_LC_adv') {
 8003:                     $unset = '';
 8004:                 }
 8005:             } else {
 8006:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 8007:                     $confhash{$item}{$type} = 1;
 8008:                 } else {
 8009:                     $confhash{$item}{$type} = 0;
 8010:                 }
 8011:             }
 8012:             if (ref($domconfig{$action}) eq 'HASH') {
 8013:                 if ($action eq 'requestauthor') {
 8014:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 8015:                         $changes{$type} = 1;
 8016:                     }
 8017:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 8018:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 8019:                         $changes{$item}{$type} = 1;
 8020:                     }
 8021:                 } else {
 8022:                     if ($context eq 'requestcourses') {
 8023:                         if ($confhash{$item}{$type} ne $unset) {
 8024:                             $changes{$item}{$type} = 1;
 8025:                         }
 8026:                     } else {
 8027:                         if (!$confhash{$item}{$type}) {
 8028:                             $changes{$item}{$type} = 1;
 8029:                         }
 8030:                     }
 8031:                 }
 8032:             } else {
 8033:                 if ($context eq 'requestcourses') {
 8034:                     if ($confhash{$item}{$type} ne $unset) {
 8035:                         $changes{$item}{$type} = 1;
 8036:                     }
 8037:                 } elsif ($context eq 'requestauthor') {
 8038:                     if ($confhash{$type} ne $unset) {
 8039:                         $changes{$type} = 1;
 8040:                     }
 8041:                 } else {
 8042:                     if (!$confhash{$item}{$type}) {
 8043:                         $changes{$item}{$type} = 1;
 8044:                     }
 8045:                 }
 8046:             }
 8047:         }
 8048:     }
 8049:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 8050:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 8051:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 8052:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 8053:                     if (exists($confhash{'defaultquota'}{$key})) {
 8054:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 8055:                             $changes{'defaultquota'}{$key} = 1;
 8056:                         }
 8057:                     } else {
 8058:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 8059:                     }
 8060:                 }
 8061:             } else {
 8062:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 8063:                     if (exists($confhash{'defaultquota'}{$key})) {
 8064:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 8065:                             $changes{'defaultquota'}{$key} = 1;
 8066:                         }
 8067:                     } else {
 8068:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 8069:                     }
 8070:                 }
 8071:             }
 8072:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 8073:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 8074:                     if (exists($confhash{'authorquota'}{$key})) {
 8075:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 8076:                             $changes{'authorquota'}{$key} = 1;
 8077:                         }
 8078:                     } else {
 8079:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 8080:                     }
 8081:                 }
 8082:             }
 8083:         }
 8084:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 8085:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 8086:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 8087:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 8088:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 8089:                             $changes{'defaultquota'}{$key} = 1;
 8090:                         }
 8091:                     } else {
 8092:                         if (!exists($domconfig{'quotas'}{$key})) {
 8093:                             $changes{'defaultquota'}{$key} = 1;
 8094:                         }
 8095:                     }
 8096:                 } else {
 8097:                     $changes{'defaultquota'}{$key} = 1;
 8098:                 }
 8099:             }
 8100:         }
 8101:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 8102:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 8103:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 8104:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 8105:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 8106:                             $changes{'authorquota'}{$key} = 1;
 8107:                         }
 8108:                     } else {
 8109:                         $changes{'authorquota'}{$key} = 1;
 8110:                     }
 8111:                 } else {
 8112:                     $changes{'authorquota'}{$key} = 1;
 8113:                 }
 8114:             }
 8115:         }
 8116:     }
 8117: 
 8118:     if ($context eq 'requestauthor') {
 8119:         $domdefaults{'requestauthor'} = \%confhash;
 8120:     } else {
 8121:         foreach my $key (keys(%confhash)) {
 8122:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
 8123:                 $domdefaults{$key} = $confhash{$key};
 8124:             }
 8125:         }
 8126:     }
 8127: 
 8128:     my %quotahash = (
 8129:                       $action => { %confhash }
 8130:                     );
 8131:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 8132:                                              $dom);
 8133:     if ($putresult eq 'ok') {
 8134:         if (keys(%changes) > 0) {
 8135:             my $cachetime = 24*60*60;
 8136:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 8137:             if (ref($lastactref) eq 'HASH') {
 8138:                 $lastactref->{'domdefaults'} = 1;
 8139:             }
 8140:             $resulttext = &mt('Changes made:').'<ul>';
 8141:             unless (($context eq 'requestcourses') ||
 8142:                     ($context eq 'requestauthor')) {
 8143:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 8144:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 8145:                     foreach my $type (@{$types},'default') {
 8146:                         if (defined($changes{'defaultquota'}{$type})) {
 8147:                             my $typetitle = $usertypes->{$type};
 8148:                             if ($type eq 'default') {
 8149:                                 $typetitle = $othertitle;
 8150:                             }
 8151:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 8152:                         }
 8153:                     }
 8154:                     $resulttext .= '</ul></li>';
 8155:                 }
 8156:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 8157:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 8158:                     foreach my $type (@{$types},'default') {
 8159:                         if (defined($changes{'authorquota'}{$type})) {
 8160:                             my $typetitle = $usertypes->{$type};
 8161:                             if ($type eq 'default') {
 8162:                                 $typetitle = $othertitle;
 8163:                             }
 8164:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 8165:                         }
 8166:                     }
 8167:                     $resulttext .= '</ul></li>';
 8168:                 }
 8169:             }
 8170:             my %newenv;
 8171:             foreach my $item (@usertools) {
 8172:                 my (%haschgs,%inconf);
 8173:                 if ($context eq 'requestauthor') {
 8174:                     %haschgs = %changes;
 8175:                     %inconf = %confhash;
 8176:                 } else {
 8177:                     if (ref($changes{$item}) eq 'HASH') {
 8178:                         %haschgs = %{$changes{$item}};
 8179:                     }
 8180:                     if (ref($confhash{$item}) eq 'HASH') {
 8181:                         %inconf = %{$confhash{$item}};
 8182:                     }
 8183:                 }
 8184:                 if (keys(%haschgs) > 0) {
 8185:                     my $newacc = 
 8186:                         &Apache::lonnet::usertools_access($env{'user.name'},
 8187:                                                           $env{'user.domain'},
 8188:                                                           $item,'reload',$context);
 8189:                     if (($context eq 'requestcourses') ||
 8190:                         ($context eq 'requestauthor')) {
 8191:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 8192:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 8193:                         }
 8194:                     } else {
 8195:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 8196:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 8197:                         }
 8198:                     }
 8199:                     unless ($context eq 'requestauthor') {
 8200:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 8201:                     }
 8202:                     foreach my $type (@{$types},'default','_LC_adv') {
 8203:                         if ($haschgs{$type}) {
 8204:                             my $typetitle = $usertypes->{$type};
 8205:                             if ($type eq 'default') {
 8206:                                 $typetitle = $othertitle;
 8207:                             } elsif ($type eq '_LC_adv') {
 8208:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 8209:                             }
 8210:                             if ($inconf{$type}) {
 8211:                                 if ($context eq 'requestcourses') {
 8212:                                     my $cond;
 8213:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 8214:                                         if ($1 eq '') {
 8215:                                             $cond = &mt('(Automatic processing of any request).');
 8216:                                         } else {
 8217:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 8218:                                         }
 8219:                                     } else { 
 8220:                                         $cond = $conditions{$inconf{$type}};
 8221:                                     }
 8222:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 8223:                                 } elsif ($context eq 'requestauthor') {
 8224:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 8225:                                                              $titles{$inconf{$type}},$typetitle);
 8226: 
 8227:                                 } else {
 8228:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 8229:                                 }
 8230:                             } else {
 8231:                                 if ($type eq '_LC_adv') {
 8232:                                     if ($inconf{$type} eq '0') {
 8233:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 8234:                                     } else { 
 8235:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 8236:                                     }
 8237:                                 } else {
 8238:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 8239:                                 }
 8240:                             }
 8241:                         }
 8242:                     }
 8243:                     unless ($context eq 'requestauthor') {
 8244:                         $resulttext .= '</ul></li>';
 8245:                     }
 8246:                 }
 8247:             }
 8248:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 8249:                 if (ref($changes{'notify'}) eq 'HASH') {
 8250:                     if ($changes{'notify'}{'approval'}) {
 8251:                         if (ref($confhash{'notify'}) eq 'HASH') {
 8252:                             if ($confhash{'notify'}{'approval'}) {
 8253:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 8254:                             } else {
 8255:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 8256:                             }
 8257:                         }
 8258:                     }
 8259:                 }
 8260:             }
 8261:             if ($action eq 'requestcourses') {
 8262:                 my @offon = ('off','on');
 8263:                 if ($changes{'uniquecode'}) {
 8264:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 8265:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 8266:                         $resulttext .= '<li>'.
 8267:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 8268:                                        '</li>';
 8269:                     } else {
 8270:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 8271:                                        '</li>';
 8272:                     }
 8273:                 }
 8274:                 foreach my $type ('textbooks','templates') {
 8275:                     if (ref($changes{$type}) eq 'HASH') {
 8276:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
 8277:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
 8278:                             my %coursehash = &Apache::lonnet::coursedescription($key);
 8279:                             my $coursetitle = $coursehash{'description'};
 8280:                             my $position = $confhash{$type}{$key}{'order'} + 1;
 8281:                             $resulttext .= '<li>';
 8282:                             foreach my $item ('subject','title','publisher','author') {
 8283:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
 8284:                                          ($type eq 'templates'));
 8285:                                 my $name = $item.':';
 8286:                                 $name =~ s/^(\w)/\U$1/;
 8287:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
 8288:                             }
 8289:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 8290:                             if ($type eq 'textbooks') {
 8291:                                 if ($confhash{$type}{$key}{'image'}) {
 8292:                                     $resulttext .= ' '.&mt('Image: [_1]',
 8293:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
 8294:                                                    ' alt="Textbook cover" />').'<br />';
 8295:                                 }
 8296:                             }
 8297:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 8298:                         }
 8299:                         $resulttext .= '</ul></li>';
 8300:                     }
 8301:                 }
 8302:                 if (ref($changes{'validation'}) eq 'HASH') {
 8303:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 8304:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 8305:                         foreach my $item (@{$validationitemsref}) {
 8306:                             if (exists($changes{'validation'}{$item})) {
 8307:                                 if ($item eq 'markup') {
 8308:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 8309:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 8310:                                 } else {
 8311:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 8312:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 8313:                                 }
 8314:                             }
 8315:                         }
 8316:                         if (exists($changes{'validation'}{'dc'})) {
 8317:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 8318:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 8319:                         }
 8320:                     }
 8321:                 }
 8322:             }
 8323:             $resulttext .= '</ul>';
 8324:             if (keys(%newenv)) {
 8325:                 &Apache::lonnet::appenv(\%newenv);
 8326:             }
 8327:         } else {
 8328:             if ($context eq 'requestcourses') {
 8329:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 8330:             } elsif ($context eq 'requestauthor') {
 8331:                 $resulttext = &mt('No changes made to rights to request author space.');
 8332:             } else {
 8333:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 8334:             }
 8335:         }
 8336:     } else {
 8337:         $resulttext = '<span class="LC_error">'.
 8338: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 8339:     }
 8340:     if ($errors) {
 8341:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 8342:                        '<ul>'.$errors.'</ul></p>';
 8343:     }
 8344:     return $resulttext;
 8345: }
 8346: 
 8347: sub process_textbook_image {
 8348:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
 8349:     my $filename = $env{'form.'.$caller.'.filename'};
 8350:     my ($error,$url);
 8351:     my ($width,$height) = (50,50);
 8352:     if ($configuserok eq 'ok') {
 8353:         if ($switchserver) {
 8354:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 8355:                          $switchserver);
 8356:         } elsif ($author_ok eq 'ok') {
 8357:             my ($result,$imageurl) =
 8358:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 8359:                              "$type/$dom/$cnum/cover",$width,$height);
 8360:             if ($result eq 'ok') {
 8361:                 $url = $imageurl;
 8362:             } else {
 8363:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 8364:             }
 8365:         } else {
 8366:             $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);
 8367:         }
 8368:     } else {
 8369:         $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);
 8370:     }
 8371:     return ($url,$error);
 8372: }
 8373: 
 8374: sub modify_autoenroll {
 8375:     my ($dom,$lastactref,%domconfig) = @_;
 8376:     my ($resulttext,%changes);
 8377:     my %currautoenroll;
 8378:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 8379:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 8380:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 8381:         }
 8382:     }
 8383:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 8384:     my %title = ( run => 'Auto-enrollment active',
 8385:                   sender => 'Sender for notification messages',
 8386:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
 8387:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
 8388:     my @offon = ('off','on');
 8389:     my $sender_uname = $env{'form.sender_uname'};
 8390:     my $sender_domain = $env{'form.sender_domain'};
 8391:     if ($sender_domain eq '') {
 8392:         $sender_uname = '';
 8393:     } elsif ($sender_uname eq '') {
 8394:         $sender_domain = '';
 8395:     }
 8396:     my $coowners = $env{'form.autoassign_coowners'};
 8397:     my $failsafe = $env{'form.autoenroll_failsafe'};
 8398:     $failsafe =~ s{^\s+|\s+$}{}g;
 8399:     if ($failsafe =~ /\D/) {
 8400:         undef($failsafe);
 8401:     }
 8402:     my %autoenrollhash =  (
 8403:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 8404:                                        'sender_uname' => $sender_uname,
 8405:                                        'sender_domain' => $sender_domain,
 8406:                                        'co-owners' => $coowners,
 8407:                                        'autofailsafe' => $failsafe,
 8408:                                 }
 8409:                      );
 8410:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 8411:                                              $dom);
 8412:     if ($putresult eq 'ok') {
 8413:         if (exists($currautoenroll{'run'})) {
 8414:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 8415:                  $changes{'run'} = 1;
 8416:              }
 8417:         } elsif ($autorun) {
 8418:             if ($env{'form.autoenroll_run'} ne '1') {
 8419:                  $changes{'run'} = 1;
 8420:             }
 8421:         }
 8422:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 8423:             $changes{'sender'} = 1;
 8424:         }
 8425:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 8426:             $changes{'sender'} = 1;
 8427:         }
 8428:         if ($currautoenroll{'co-owners'} ne '') {
 8429:             if ($currautoenroll{'co-owners'} ne $coowners) {
 8430:                 $changes{'coowners'} = 1;
 8431:             }
 8432:         } elsif ($coowners) {
 8433:             $changes{'coowners'} = 1;
 8434:         }
 8435:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
 8436:             $changes{'autofailsafe'} = 1;
 8437:         }
 8438:         if (keys(%changes) > 0) {
 8439:             $resulttext = &mt('Changes made:').'<ul>';
 8440:             if ($changes{'run'}) {
 8441:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 8442:             }
 8443:             if ($changes{'sender'}) {
 8444:                 if ($sender_uname eq '' || $sender_domain eq '') {
 8445:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 8446:                 } else {
 8447:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 8448:                 }
 8449:             }
 8450:             if ($changes{'coowners'}) {
 8451:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 8452:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 8453:                 if (ref($lastactref) eq 'HASH') {
 8454:                     $lastactref->{'domainconfig'} = 1;
 8455:                 }
 8456:             }
 8457:             if ($changes{'autofailsafe'}) {
 8458:                 if ($failsafe ne '') {
 8459:                     $resulttext .= '<li>'.&mt("$title{'failsafe'} set to [_1]",$failsafe).'</li>';
 8460:                 } else {
 8461:                     $resulttext .= '<li>'.&mt("$title{'failsafe'} deleted");
 8462:                 }
 8463:                 &Apache::lonnet::get_domain_defaults($dom,1);
 8464:                 if (ref($lastactref) eq 'HASH') {
 8465:                     $lastactref->{'domdefaults'} = 1;
 8466:                 }
 8467:             }
 8468:             $resulttext .= '</ul>';
 8469:         } else {
 8470:             $resulttext = &mt('No changes made to auto-enrollment settings');
 8471:         }
 8472:     } else {
 8473:         $resulttext = '<span class="LC_error">'.
 8474: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 8475:     }
 8476:     return $resulttext;
 8477: }
 8478: 
 8479: sub modify_autoupdate {
 8480:     my ($dom,%domconfig) = @_;
 8481:     my ($resulttext,%currautoupdate,%fields,%changes);
 8482:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 8483:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 8484:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 8485:         }
 8486:     }
 8487:     my @offon = ('off','on');
 8488:     my %title = &Apache::lonlocal::texthash (
 8489:                    run => 'Auto-update:',
 8490:                    classlists => 'Updates to user information in classlists?'
 8491:                 );
 8492:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8493:     my %fieldtitles = &Apache::lonlocal::texthash (
 8494:                         id => 'Student/Employee ID',
 8495:                         permanentemail => 'E-mail address',
 8496:                         lastname => 'Last Name',
 8497:                         firstname => 'First Name',
 8498:                         middlename => 'Middle Name',
 8499:                         generation => 'Generation',
 8500:                       );
 8501:     $othertitle = &mt('All users');
 8502:     if (keys(%{$usertypes}) >  0) {
 8503:         $othertitle = &mt('Other users');
 8504:     }
 8505:     foreach my $key (keys(%env)) {
 8506:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 8507:             my ($usertype,$item) = ($1,$2);
 8508:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 8509:                 if ($usertype eq 'default') {   
 8510:                     push(@{$fields{$1}},$2);
 8511:                 } elsif (ref($types) eq 'ARRAY') {
 8512:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 8513:                         push(@{$fields{$1}},$2);
 8514:                     }
 8515:                 }
 8516:             }
 8517:         }
 8518:     }
 8519:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 8520:     @lockablenames = sort(@lockablenames);
 8521:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 8522:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 8523:         if (@changed) {
 8524:             $changes{'lockablenames'} = 1;
 8525:         }
 8526:     } else {
 8527:         if (@lockablenames) {
 8528:             $changes{'lockablenames'} = 1;
 8529:         }
 8530:     }
 8531:     my %updatehash = (
 8532:                       autoupdate => { run => $env{'form.autoupdate_run'},
 8533:                                       classlists => $env{'form.classlists'},
 8534:                                       fields => {%fields},
 8535:                                       lockablenames => \@lockablenames,
 8536:                                     }
 8537:                      );
 8538:     foreach my $key (keys(%currautoupdate)) {
 8539:         if (($key eq 'run') || ($key eq 'classlists')) {
 8540:             if (exists($updatehash{autoupdate}{$key})) {
 8541:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 8542:                     $changes{$key} = 1;
 8543:                 }
 8544:             }
 8545:         } elsif ($key eq 'fields') {
 8546:             if (ref($currautoupdate{$key}) eq 'HASH') {
 8547:                 foreach my $item (@{$types},'default') {
 8548:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 8549:                         my $change = 0;
 8550:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 8551:                             if (!exists($fields{$item})) {
 8552:                                 $change = 1;
 8553:                                 last;
 8554:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 8555:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 8556:                                     $change = 1;
 8557:                                     last;
 8558:                                 }
 8559:                             }
 8560:                         }
 8561:                         if ($change) {
 8562:                             push(@{$changes{$key}},$item);
 8563:                         }
 8564:                     } 
 8565:                 }
 8566:             }
 8567:         } elsif ($key eq 'lockablenames') {
 8568:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 8569:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 8570:                 if (@changed) {
 8571:                     $changes{'lockablenames'} = 1;
 8572:                 }
 8573:             } else {
 8574:                 if (@lockablenames) {
 8575:                     $changes{'lockablenames'} = 1;
 8576:                 }
 8577:             }
 8578:         }
 8579:     }
 8580:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 8581:         if (@lockablenames) {
 8582:             $changes{'lockablenames'} = 1;
 8583:         }
 8584:     }
 8585:     foreach my $item (@{$types},'default') {
 8586:         if (defined($fields{$item})) {
 8587:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 8588:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 8589:                     my $change = 0;
 8590:                     if (ref($fields{$item}) eq 'ARRAY') {
 8591:                         foreach my $type (@{$fields{$item}}) {
 8592:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 8593:                                 $change = 1;
 8594:                                 last;
 8595:                             }
 8596:                         }
 8597:                     }
 8598:                     if ($change) {
 8599:                         push(@{$changes{'fields'}},$item);
 8600:                     }
 8601:                 } else {
 8602:                     push(@{$changes{'fields'}},$item);
 8603:                 }
 8604:             } else {
 8605:                 push(@{$changes{'fields'}},$item);
 8606:             }
 8607:         }
 8608:     }
 8609:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 8610:                                              $dom);
 8611:     if ($putresult eq 'ok') {
 8612:         if (keys(%changes) > 0) {
 8613:             $resulttext = &mt('Changes made:').'<ul>';
 8614:             foreach my $key (sort(keys(%changes))) {
 8615:                 if ($key eq 'lockablenames') {
 8616:                     $resulttext .= '<li>';
 8617:                     if (@lockablenames) {
 8618:                         $usertypes->{'default'} = $othertitle;
 8619:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 8620:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 8621:                     } else {
 8622:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 8623:                     }
 8624:                     $resulttext .= '</li>';
 8625:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 8626:                     foreach my $item (@{$changes{$key}}) {
 8627:                         my @newvalues;
 8628:                         foreach my $type (@{$fields{$item}}) {
 8629:                             push(@newvalues,$fieldtitles{$type});
 8630:                         }
 8631:                         my $newvaluestr;
 8632:                         if (@newvalues > 0) {
 8633:                             $newvaluestr = join(', ',@newvalues);
 8634:                         } else {
 8635:                             $newvaluestr = &mt('none');
 8636:                         }
 8637:                         if ($item eq 'default') {
 8638:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 8639:                         } else {
 8640:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 8641:                         }
 8642:                     }
 8643:                 } else {
 8644:                     my $newvalue;
 8645:                     if ($key eq 'run') {
 8646:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 8647:                     } else {
 8648:                         $newvalue = $offon[$env{'form.'.$key}];
 8649:                     }
 8650:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 8651:                 }
 8652:             }
 8653:             $resulttext .= '</ul>';
 8654:         } else {
 8655:             $resulttext = &mt('No changes made to autoupdates');
 8656:         }
 8657:     } else {
 8658:         $resulttext = '<span class="LC_error">'.
 8659: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 8660:     }
 8661:     return $resulttext;
 8662: }
 8663: 
 8664: sub modify_autocreate {
 8665:     my ($dom,%domconfig) = @_;
 8666:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 8667:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 8668:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 8669:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 8670:         }
 8671:     }
 8672:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 8673:                  req => 'Auto-creation of validated requests for official courses',
 8674:                  xmldc => 'Identity of course creator of courses from XML files',
 8675:                );
 8676:     my @types = ('xml','req');
 8677:     foreach my $item (@types) {
 8678:         $newvals{$item} = $env{'form.autocreate_'.$item};
 8679:         $newvals{$item} =~ s/\D//g;
 8680:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 8681:     }
 8682:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 8683:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
 8684:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 8685:         $newvals{'xmldc'} = '';
 8686:     } 
 8687:     %autocreatehash =  (
 8688:                         autocreate => { xml => $newvals{'xml'},
 8689:                                         req => $newvals{'req'},
 8690:                                       }
 8691:                        );
 8692:     if ($newvals{'xmldc'} ne '') {
 8693:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 8694:     }
 8695:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 8696:                                              $dom);
 8697:     if ($putresult eq 'ok') {
 8698:         my @items = @types;
 8699:         if ($newvals{'xml'}) {
 8700:             push(@items,'xmldc');
 8701:         }
 8702:         foreach my $item (@items) {
 8703:             if (exists($currautocreate{$item})) {
 8704:                 if ($currautocreate{$item} ne $newvals{$item}) {
 8705:                     $changes{$item} = 1;
 8706:                 }
 8707:             } elsif ($newvals{$item}) {
 8708:                 $changes{$item} = 1;
 8709:             }
 8710:         }
 8711:         if (keys(%changes) > 0) {
 8712:             my @offon = ('off','on'); 
 8713:             $resulttext = &mt('Changes made:').'<ul>';
 8714:             foreach my $item (@types) {
 8715:                 if ($changes{$item}) {
 8716:                     my $newtxt = $offon[$newvals{$item}];
 8717:                     $resulttext .= '<li>'.
 8718:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
 8719:                                        '<b>','</b>').
 8720:                                    '</li>';
 8721:                 }
 8722:             }
 8723:             if ($changes{'xmldc'}) {
 8724:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 8725:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 8726:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
 8727:             }
 8728:             $resulttext .= '</ul>';
 8729:         } else {
 8730:             $resulttext = &mt('No changes made to auto-creation settings');
 8731:         }
 8732:     } else {
 8733:         $resulttext = '<span class="LC_error">'.
 8734:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8735:     }
 8736:     return $resulttext;
 8737: }
 8738: 
 8739: sub modify_directorysrch {
 8740:     my ($dom,%domconfig) = @_;
 8741:     my ($resulttext,%changes);
 8742:     my %currdirsrch;
 8743:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 8744:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 8745:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 8746:         }
 8747:     }
 8748:     my %title = ( available => 'Institutional directory search available',
 8749:                   localonly => 'Other domains can search institution',
 8750:                   lcavailable => 'LON-CAPA directory search available',
 8751:                   lclocalonly => 'Other domains can search LON-CAPA domain',
 8752:                   searchby => 'Search types',
 8753:                   searchtypes => 'Search latitude');
 8754:     my @offon = ('off','on');
 8755:     my @otherdoms = ('Yes','No');
 8756: 
 8757:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 8758:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 8759:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 8760: 
 8761:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8762:     if (keys(%{$usertypes}) == 0) {
 8763:         @cansearch = ('default');
 8764:     } else {
 8765:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 8766:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 8767:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 8768:                     push(@{$changes{'cansearch'}},$type);
 8769:                 }
 8770:             }
 8771:             foreach my $type (@cansearch) {
 8772:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 8773:                     push(@{$changes{'cansearch'}},$type);
 8774:                 }
 8775:             }
 8776:         } else {
 8777:             push(@{$changes{'cansearch'}},@cansearch);
 8778:         }
 8779:     }
 8780: 
 8781:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 8782:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 8783:             if (!grep(/^\Q$by\E$/,@searchby)) {
 8784:                 push(@{$changes{'searchby'}},$by);
 8785:             }
 8786:         }
 8787:         foreach my $by (@searchby) {
 8788:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 8789:                 push(@{$changes{'searchby'}},$by);
 8790:             }
 8791:         }
 8792:     } else {
 8793:         push(@{$changes{'searchby'}},@searchby);
 8794:     }
 8795: 
 8796:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 8797:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 8798:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 8799:                 push(@{$changes{'searchtypes'}},$type);
 8800:             }
 8801:         }
 8802:         foreach my $type (@searchtypes) {
 8803:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 8804:                 push(@{$changes{'searchtypes'}},$type);
 8805:             }
 8806:         }
 8807:     } else {
 8808:         if (exists($currdirsrch{'searchtypes'})) {
 8809:             foreach my $type (@searchtypes) {  
 8810:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 8811:                     push(@{$changes{'searchtypes'}},$type);
 8812:                 }
 8813:             }
 8814:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 8815:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 8816:             }   
 8817:         } else {
 8818:             push(@{$changes{'searchtypes'}},@searchtypes); 
 8819:         }
 8820:     }
 8821: 
 8822:     my %dirsrch_hash =  (
 8823:             directorysrch => { available => $env{'form.dirsrch_available'},
 8824:                                cansearch => \@cansearch,
 8825:                                localonly => $env{'form.dirsrch_instlocalonly'},
 8826:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
 8827:                                lcavailable => $env{'form.dirsrch_domavailable'},
 8828:                                searchby => \@searchby,
 8829:                                searchtypes => \@searchtypes,
 8830:                              }
 8831:             );
 8832:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 8833:                                              $dom);
 8834:     if ($putresult eq 'ok') {
 8835:         if (exists($currdirsrch{'available'})) {
 8836:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 8837:                  $changes{'available'} = 1;
 8838:              }
 8839:         } else {
 8840:             if ($env{'form.dirsrch_available'} eq '1') {
 8841:                 $changes{'available'} = 1;
 8842:             }
 8843:         }
 8844:         if (exists($currdirsrch{'lcavailable'})) {
 8845:              if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
 8846:                  $changes{'lcavailable'} = 1;
 8847:              }
 8848:         } else {
 8849:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
 8850:                 $changes{'lcavailable'} = 1;
 8851:             }
 8852:         }
 8853:         if (exists($currdirsrch{'localonly'})) {
 8854:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
 8855:                 $changes{'localonly'} = 1;
 8856:             }
 8857:         } else {
 8858:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
 8859:                 $changes{'localonly'} = 1;
 8860:             }
 8861:         }
 8862:         if (exists($currdirsrch{'lclocalonly'})) {
 8863:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
 8864:                 $changes{'lclocalonly'} = 1;
 8865:             }
 8866:         } else {
 8867:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
 8868:                 $changes{'lclocalonly'} = 1;
 8869:             }
 8870:         }
 8871:         if (keys(%changes) > 0) {
 8872:             $resulttext = &mt('Changes made:').'<ul>';
 8873:             if ($changes{'available'}) {
 8874:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 8875:             }
 8876:             if ($changes{'lcavailable'}) {
 8877:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
 8878:             }
 8879:             if ($changes{'localonly'}) {
 8880:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
 8881:             }
 8882:             if ($changes{'lclocalonly'}) {
 8883:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
 8884:             }
 8885:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 8886:                 my $chgtext;
 8887:                 if (ref($usertypes) eq 'HASH') {
 8888:                     if (keys(%{$usertypes}) > 0) {
 8889:                         foreach my $type (@{$types}) {
 8890:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 8891:                                 $chgtext .= $usertypes->{$type}.'; ';
 8892:                             }
 8893:                         }
 8894:                         if (grep(/^default$/,@cansearch)) {
 8895:                             $chgtext .= $othertitle;
 8896:                         } else {
 8897:                             $chgtext =~ s/\; $//;
 8898:                         }
 8899:                         $resulttext .=
 8900:                             '<li>'.
 8901:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
 8902:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
 8903:                             '</li>';
 8904:                     }
 8905:                 }
 8906:             }
 8907:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 8908:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 8909:                 my $chgtext;
 8910:                 foreach my $type (@{$titleorder}) {
 8911:                     if (grep(/^\Q$type\E$/,@searchby)) {
 8912:                         if (defined($searchtitles->{$type})) {
 8913:                             $chgtext .= $searchtitles->{$type}.'; ';
 8914:                         }
 8915:                     }
 8916:                 }
 8917:                 $chgtext =~ s/\; $//;
 8918:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 8919:             }
 8920:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 8921:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 8922:                 my $chgtext;
 8923:                 foreach my $type (@{$srchtypeorder}) {
 8924:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 8925:                         if (defined($srchtypes_desc->{$type})) {
 8926:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 8927:                         }
 8928:                     }
 8929:                 }
 8930:                 $chgtext =~ s/\; $//;
 8931:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
 8932:             }
 8933:             $resulttext .= '</ul>';
 8934:         } else {
 8935:             $resulttext = &mt('No changes made to directory search settings');
 8936:         }
 8937:     } else {
 8938:         $resulttext = '<span class="LC_error">'.
 8939:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 8940:     }
 8941:     return $resulttext;
 8942: }
 8943: 
 8944: sub modify_contacts {
 8945:     my ($dom,$lastactref,%domconfig) = @_;
 8946:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 8947:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 8948:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 8949:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 8950:         }
 8951:     }
 8952:     my (%others,%to,%bcc);
 8953:     my @contacts = ('supportemail','adminemail');
 8954:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 8955:                     'requestsmail','updatesmail','idconflictsmail');
 8956:     my @toggles = ('reporterrors','reportupdates');
 8957:     foreach my $type (@mailings) {
 8958:         @{$newsetting{$type}} = 
 8959:             &Apache::loncommon::get_env_multiple('form.'.$type);
 8960:         foreach my $item (@contacts) {
 8961:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 8962:                 $contacts_hash{contacts}{$type}{$item} = 1;
 8963:             } else {
 8964:                 $contacts_hash{contacts}{$type}{$item} = 0;
 8965:             }
 8966:         }  
 8967:         $others{$type} = $env{'form.'.$type.'_others'};
 8968:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 8969:         if ($type eq 'helpdeskmail') {
 8970:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 8971:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 8972:         }
 8973:     }
 8974:     foreach my $item (@contacts) {
 8975:         $to{$item} = $env{'form.'.$item};
 8976:         $contacts_hash{'contacts'}{$item} = $to{$item};
 8977:     }
 8978:     foreach my $item (@toggles) {
 8979:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
 8980:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
 8981:         }
 8982:     }
 8983:     if (keys(%currsetting) > 0) {
 8984:         foreach my $item (@contacts) {
 8985:             if ($to{$item} ne $currsetting{$item}) {
 8986:                 $changes{$item} = 1;
 8987:             }
 8988:         }
 8989:         foreach my $type (@mailings) {
 8990:             foreach my $item (@contacts) {
 8991:                 if (ref($currsetting{$type}) eq 'HASH') {
 8992:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 8993:                         push(@{$changes{$type}},$item);
 8994:                     }
 8995:                 } else {
 8996:                     push(@{$changes{$type}},@{$newsetting{$type}});
 8997:                 }
 8998:             }
 8999:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 9000:                 push(@{$changes{$type}},'others');
 9001:             }
 9002:             if ($type eq 'helpdeskmail') {   
 9003:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 9004:                     push(@{$changes{$type}},'bcc'); 
 9005:                 }
 9006:             }
 9007:         }
 9008:     } else {
 9009:         my %default;
 9010:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 9011:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 9012:         $default{'errormail'} = 'adminemail';
 9013:         $default{'packagesmail'} = 'adminemail';
 9014:         $default{'helpdeskmail'} = 'supportemail';
 9015:         $default{'lonstatusmail'} = 'adminemail';
 9016:         $default{'requestsmail'} = 'adminemail';
 9017:         $default{'updatesmail'} = 'adminemail';
 9018:         foreach my $item (@contacts) {
 9019:            if ($to{$item} ne $default{$item}) {
 9020:               $changes{$item} = 1;
 9021:            }
 9022:         }
 9023:         foreach my $type (@mailings) {
 9024:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 9025:                
 9026:                 push(@{$changes{$type}},@{$newsetting{$type}});
 9027:             }
 9028:             if ($others{$type} ne '') {
 9029:                 push(@{$changes{$type}},'others');
 9030:             }
 9031:             if ($type eq 'helpdeskmail') {
 9032:                 if ($bcc{$type} ne '') {
 9033:                     push(@{$changes{$type}},'bcc');
 9034:                 }
 9035:             }
 9036:         }
 9037:     }
 9038:     foreach my $item (@toggles) {
 9039:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
 9040:             $changes{$item} = 1;
 9041:         } elsif ((!$env{'form.'.$item}) &&
 9042:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
 9043:             $changes{$item} = 1;
 9044:         }
 9045:     }
 9046:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 9047:                                              $dom);
 9048:     if ($putresult eq 'ok') {
 9049:         if (keys(%changes) > 0) {
 9050:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 9051:             if (ref($lastactref) eq 'HASH') {
 9052:                 $lastactref->{'domainconfig'} = 1;
 9053:             }
 9054:             my ($titles,$short_titles)  = &contact_titles();
 9055:             $resulttext = &mt('Changes made:').'<ul>';
 9056:             foreach my $item (@contacts) {
 9057:                 if ($changes{$item}) {
 9058:                     $resulttext .= '<li>'.$titles->{$item}.
 9059:                                     &mt(' set to: ').
 9060:                                     '<span class="LC_cusr_emph">'.
 9061:                                     $to{$item}.'</span></li>';
 9062:                 }
 9063:             }
 9064:             foreach my $type (@mailings) {
 9065:                 if (ref($changes{$type}) eq 'ARRAY') {
 9066:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 9067:                     my @text;
 9068:                     foreach my $item (@{$newsetting{$type}}) {
 9069:                         push(@text,$short_titles->{$item});
 9070:                     }
 9071:                     if ($others{$type} ne '') {
 9072:                         push(@text,$others{$type});
 9073:                     }
 9074:                     $resulttext .= '<span class="LC_cusr_emph">'.
 9075:                                    join(', ',@text).'</span>';
 9076:                     if ($type eq 'helpdeskmail') {
 9077:                         if ($bcc{$type} ne '') {
 9078:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 9079:                         }
 9080:                     }
 9081:                     $resulttext .= '</li>';
 9082:                 }
 9083:             }
 9084:             my @offon = ('off','on');
 9085:             if ($changes{'reporterrors'}) {
 9086:                 $resulttext .= '<li>'.
 9087:                                &mt('E-mail error reports to [_1] set to "'.
 9088:                                    $offon[$env{'form.reporterrors'}].'".',
 9089:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 9090:                                        &mt('LON-CAPA core group - MSU'),600,500)).
 9091:                                '</li>';
 9092:             }
 9093:             if ($changes{'reportupdates'}) {
 9094:                 $resulttext .= '<li>'.
 9095:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
 9096:                                     $offon[$env{'form.reportupdates'}].'".',
 9097:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 9098:                                         &mt('LON-CAPA core group - MSU'),600,500)).
 9099:                                 '</li>';
 9100:             }
 9101:             $resulttext .= '</ul>';
 9102:         } else {
 9103:             $resulttext = &mt('No changes made to contact information');
 9104:         }
 9105:     } else {
 9106:         $resulttext = '<span class="LC_error">'.
 9107:             &mt('An error occurred: [_1].',$putresult).'</span>';
 9108:     }
 9109:     return $resulttext;
 9110: }
 9111: 
 9112: sub modify_usercreation {
 9113:     my ($dom,%domconfig) = @_;
 9114:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
 9115:     my $warningmsg;
 9116:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 9117:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 9118:             if ($key eq 'cancreate') {
 9119:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 9120:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 9121:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 9122:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
 9123:                             ($item eq 'recaptchaversion')) {
 9124:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 9125:                         } else {
 9126:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 9127:                         }
 9128:                     }
 9129:                 }
 9130:             } elsif ($key eq 'email_rule') {
 9131:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 9132:             } else {
 9133:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 9134:             }
 9135:         }
 9136:     }
 9137:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 9138:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 9139:     my @contexts = ('author','course','requestcrs');
 9140:     foreach my $item(@contexts) {
 9141:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
 9142:     }
 9143:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 9144:         foreach my $item (@contexts) {
 9145:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 9146:                 push(@{$changes{'cancreate'}},$item);
 9147:             }
 9148:         }
 9149:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 9150:         foreach my $item (@contexts) {
 9151:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 9152:                 if ($cancreate{$item} ne 'any') {
 9153:                     push(@{$changes{'cancreate'}},$item);
 9154:                 }
 9155:             } else {
 9156:                 if ($cancreate{$item} ne 'none') {
 9157:                     push(@{$changes{'cancreate'}},$item);
 9158:                 }
 9159:             }
 9160:         }
 9161:     } else {
 9162:         foreach my $item (@contexts)  {
 9163:             push(@{$changes{'cancreate'}},$item);
 9164:         }
 9165:     }
 9166: 
 9167:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 9168:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 9169:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 9170:                 push(@{$changes{'username_rule'}},$type);
 9171:             }
 9172:         }
 9173:         foreach my $type (@username_rule) {
 9174:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 9175:                 push(@{$changes{'username_rule'}},$type);
 9176:             }
 9177:         }
 9178:     } else {
 9179:         push(@{$changes{'username_rule'}},@username_rule);
 9180:     }
 9181: 
 9182:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 9183:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 9184:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 9185:                 push(@{$changes{'id_rule'}},$type);
 9186:             }
 9187:         }
 9188:         foreach my $type (@id_rule) {
 9189:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 9190:                 push(@{$changes{'id_rule'}},$type);
 9191:             }
 9192:         }
 9193:     } else {
 9194:         push(@{$changes{'id_rule'}},@id_rule);
 9195:     }
 9196: 
 9197:     my @authen_contexts = ('author','course','domain');
 9198:     my @authtypes = ('int','krb4','krb5','loc');
 9199:     my %authhash;
 9200:     foreach my $item (@authen_contexts) {
 9201:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 9202:         foreach my $auth (@authtypes) {
 9203:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 9204:                 $authhash{$item}{$auth} = 1;
 9205:             } else {
 9206:                 $authhash{$item}{$auth} = 0;
 9207:             }
 9208:         }
 9209:     }
 9210:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 9211:         foreach my $item (@authen_contexts) {
 9212:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 9213:                 foreach my $auth (@authtypes) {
 9214:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 9215:                         push(@{$changes{'authtypes'}},$item);
 9216:                         last;
 9217:                     }
 9218:                 }
 9219:             }
 9220:         }
 9221:     } else {
 9222:         foreach my $item (@authen_contexts) {
 9223:             push(@{$changes{'authtypes'}},$item);
 9224:         }
 9225:     }
 9226: 
 9227:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
 9228:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
 9229:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
 9230:     $save_usercreate{'id_rule'} = \@id_rule;
 9231:     $save_usercreate{'username_rule'} = \@username_rule,
 9232:     $save_usercreate{'authtypes'} = \%authhash;
 9233: 
 9234:     my %usercreation_hash =  (
 9235:         usercreation     => \%save_usercreate,
 9236:     );
 9237: 
 9238:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 9239:                                              $dom);
 9240: 
 9241:     if ($putresult eq 'ok') {
 9242:         if (keys(%changes) > 0) {
 9243:             $resulttext = &mt('Changes made:').'<ul>';
 9244:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 9245:                 my %lt = &usercreation_types();
 9246:                 foreach my $type (@{$changes{'cancreate'}}) {
 9247:                     my $chgtext = $lt{$type}.', ';
 9248:                     if ($cancreate{$type} eq 'none') {
 9249:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 9250:                     } elsif ($cancreate{$type} eq 'any') {
 9251:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 9252:                     } elsif ($cancreate{$type} eq 'official') {
 9253:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 9254:                     } elsif ($cancreate{$type} eq 'unofficial') {
 9255:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 9256:                     }
 9257:                     $resulttext .= '<li>'.$chgtext.'</li>';
 9258:                 }
 9259:             }
 9260:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 9261:                 my ($rules,$ruleorder) = 
 9262:                     &Apache::lonnet::inst_userrules($dom,'username');
 9263:                 my $chgtext = '<ul>';
 9264:                 foreach my $type (@username_rule) {
 9265:                     if (ref($rules->{$type}) eq 'HASH') {
 9266:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 9267:                     }
 9268:                 }
 9269:                 $chgtext .= '</ul>';
 9270:                 if (@username_rule > 0) {
 9271:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 9272:                 } else {
 9273:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 9274:                 }
 9275:             }
 9276:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 9277:                 my ($idrules,$idruleorder) = 
 9278:                     &Apache::lonnet::inst_userrules($dom,'id');
 9279:                 my $chgtext = '<ul>';
 9280:                 foreach my $type (@id_rule) {
 9281:                     if (ref($idrules->{$type}) eq 'HASH') {
 9282:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 9283:                     }
 9284:                 }
 9285:                 $chgtext .= '</ul>';
 9286:                 if (@id_rule > 0) {
 9287:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 9288:                 } else {
 9289:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 9290:                 }
 9291:             }
 9292:             my %authname = &authtype_names();
 9293:             my %context_title = &context_names();
 9294:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 9295:                 my $chgtext = '<ul>';
 9296:                 foreach my $type (@{$changes{'authtypes'}}) {
 9297:                     my @allowed;
 9298:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 9299:                     foreach my $auth (@authtypes) {
 9300:                         if ($authhash{$type}{$auth}) {
 9301:                             push(@allowed,$authname{$auth});
 9302:                         }
 9303:                     }
 9304:                     if (@allowed > 0) {
 9305:                         $chgtext .= join(', ',@allowed).'</li>';
 9306:                     } else {
 9307:                         $chgtext .= &mt('none').'</li>';
 9308:                     }
 9309:                 }
 9310:                 $chgtext .= '</ul>';
 9311:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 9312:                 $resulttext .= '</li>';
 9313:             }
 9314:             $resulttext .= '</ul>';
 9315:         } else {
 9316:             $resulttext = &mt('No changes made to user creation settings');
 9317:         }
 9318:     } else {
 9319:         $resulttext = '<span class="LC_error">'.
 9320:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9321:     }
 9322:     if ($warningmsg ne '') {
 9323:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 9324:     }
 9325:     return $resulttext;
 9326: }
 9327: 
 9328: sub modify_selfcreation {
 9329:     my ($dom,%domconfig) = @_;
 9330:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%changes,%cancreate);
 9331:     my (%save_usercreate,%save_usermodify);
 9332:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9333:     if (ref($types) eq 'ARRAY') {
 9334:         $usertypes->{'default'} = $othertitle;
 9335:         push(@{$types},'default');
 9336:     }
 9337: #
 9338: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
 9339: #
 9340:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 9341:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 9342:             if ($key eq 'cancreate') {
 9343:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 9344:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 9345:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 9346:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') || 
 9347:                             ($item eq 'recaptchaversion') ||
 9348:                             ($item eq 'emailusername') || ($item eq 'notify') ||
 9349:                             ($item eq 'selfcreateprocessing') || ($item eq 'shibenv')) {
 9350:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 9351:                         } else {
 9352:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 9353:                         }
 9354:                     }
 9355:                 }
 9356:             } elsif ($key eq 'email_rule') {
 9357:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 9358:             } else {
 9359:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 9360:             }
 9361:         }
 9362:     }
 9363: #
 9364: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
 9365: #
 9366:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 9367:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 9368:             if ($key eq 'selfcreate') {
 9369:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
 9370:             } else {
 9371:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
 9372:             }
 9373:         }
 9374:     }
 9375: 
 9376:     my @contexts = ('selfcreate');
 9377:     @{$cancreate{'selfcreate'}} = ();
 9378:     %{$cancreate{'emailusername'}} = ();
 9379:     @{$cancreate{'statustocreate'}} = ();
 9380:     %{$cancreate{'selfcreateprocessing'}} = ();
 9381:     %{$cancreate{'shibenv'}} = ();
 9382:     my %selfcreatetypes = (
 9383:                              sso   => 'users authenticated by institutional single sign on',
 9384:                              login => 'users authenticated by institutional log-in',
 9385:                              email => 'users who provide a valid e-mail address for use as username',
 9386:                           );
 9387: #
 9388: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
 9389: # is permitted.
 9390: #
 9391: 
 9392:     my @statuses;
 9393:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 9394:         if (ref($domconfig{'inststatus'}{'inststatusguest'}) eq 'ARRAY') {
 9395:             @statuses = @{$domconfig{'inststatus'}{'inststatusguest'}};
 9396:         }
 9397:     }
 9398:     push(@statuses,'default');
 9399: 
 9400:     foreach my $item ('login','sso','email') {
 9401:         if ($item eq 'email') {
 9402:             if ($env{'form.cancreate_email'}) {
 9403:                 push(@{$cancreate{'selfcreate'}},'email');
 9404:                 push(@contexts,'selfcreateprocessing');
 9405:                 foreach my $type (@statuses) {
 9406:                     if ($type eq 'default') {
 9407:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess'};
 9408:                     } else { 
 9409:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
 9410:                     }
 9411:                 }
 9412:             }
 9413:         } else {
 9414:             if ($env{'form.cancreate_'.$item}) {
 9415:                 push(@{$cancreate{'selfcreate'}},$item);
 9416:             }
 9417:         }
 9418:     }
 9419:     my (@email_rule,%userinfo,%savecaptcha);
 9420:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 9421: #
 9422: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
 9423: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
 9424: #
 9425: 
 9426:     if ($env{'form.cancreate_email'}) {
 9427:         push(@contexts,'emailusername');
 9428:         if (ref($types) eq 'ARRAY') {
 9429:             foreach my $type (@{$types}) {
 9430:                 if (ref($infofields) eq 'ARRAY') {
 9431:                     foreach my $field (@{$infofields}) {
 9432:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
 9433:                             $cancreate{'emailusername'}{$type}{$field} = $1;
 9434:                         }
 9435:                     }
 9436:                 }
 9437:             }
 9438:         }
 9439: #
 9440: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
 9441: # queued requests for self-creation of account using e-mail address as username
 9442: #
 9443: 
 9444:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
 9445:         @approvalnotify = sort(@approvalnotify);
 9446:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
 9447:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 9448:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
 9449:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
 9450:                     push(@{$changes{'cancreate'}},'notify');
 9451:                 }
 9452:             } else {
 9453:                 if ($cancreate{'notify'}{'approval'}) {
 9454:                     push(@{$changes{'cancreate'}},'notify');
 9455:                 }
 9456:             }
 9457:         } elsif ($cancreate{'notify'}{'approval'}) {
 9458:             push(@{$changes{'cancreate'}},'notify');
 9459:         }
 9460: 
 9461: #
 9462: # Retrieve rules (if any) governing types of e-mail address which may be used as a username
 9463: #
 9464:         @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 9465:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
 9466:         if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 9467:             if (@{$curr_usercreation{'email_rule'}} > 0) {
 9468:                 foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 9469:                     if (!grep(/^\Q$type\E$/,@email_rule)) {
 9470:                         push(@{$changes{'email_rule'}},$type);
 9471:                     }
 9472:                 }
 9473:             }
 9474:             if (@email_rule > 0) {
 9475:                 foreach my $type (@email_rule) {
 9476:                     if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 9477:                         push(@{$changes{'email_rule'}},$type);
 9478:                     }
 9479:                 }
 9480:             }
 9481:         } elsif (@email_rule > 0) {
 9482:             push(@{$changes{'email_rule'}},@email_rule);
 9483:         }
 9484:     }
 9485: #  
 9486: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
 9487: # institutional log-in.
 9488: #
 9489:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
 9490:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9491:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
 9492:                ($domdefaults{'auth_def'} eq 'localauth'))) {
 9493:             $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.').' '.
 9494:                           &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.');
 9495:         }
 9496:     }
 9497:     my @fields = ('lastname','firstname','middlename','generation',
 9498:                   'permanentemail','id');
 9499:     my @shibfields = (@fields,'inststatus');
 9500:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9501: #
 9502: # Where usernames may created for institutional log-in and/or institutional single sign on:
 9503: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
 9504: # may self-create accounts 
 9505: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
 9506: # which the user may supply, if institutional data is unavailable.
 9507: #
 9508:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
 9509:         if (ref($types) eq 'ARRAY') {
 9510:             if (@{$types} > 1) {
 9511:                 @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
 9512:                 push(@contexts,'statustocreate');
 9513:             } else {
 9514:                 undef($cancreate{'statustocreate'});
 9515:             } 
 9516:             foreach my $type (@{$types}) {
 9517:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
 9518:                 foreach my $field (@fields) {
 9519:                     if (grep(/^\Q$field\E$/,@modifiable)) {
 9520:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
 9521:                     } else {
 9522:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
 9523:                     }
 9524:                 }
 9525:             }
 9526:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
 9527:                 foreach my $type (@{$types}) {
 9528:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
 9529:                         foreach my $field (@fields) {
 9530:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
 9531:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
 9532:                                 push(@{$changes{'selfcreate'}},$type);
 9533:                                 last;
 9534:                             }
 9535:                         }
 9536:                     }
 9537:                 }
 9538:             } else {
 9539:                 foreach my $type (@{$types}) {
 9540:                     push(@{$changes{'selfcreate'}},$type);
 9541:                 }
 9542:             }
 9543:         }
 9544:         foreach my $field (@shibfields) {
 9545:             if ($env{'form.shibenv_'.$field} ne '') {
 9546:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
 9547:             }
 9548:         }
 9549:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 9550:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
 9551:                 foreach my $field (@shibfields) {
 9552:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
 9553:                         push(@{$changes{'cancreate'}},'shibenv');
 9554:                     }
 9555:                 }
 9556:             } else {
 9557:                 foreach my $field (@shibfields) {
 9558:                     if ($env{'form.shibenv_'.$field}) {
 9559:                         push(@{$changes{'cancreate'}},'shibenv');
 9560:                         last;
 9561:                     }
 9562:                 }
 9563:             }
 9564:         }
 9565:     }
 9566:     foreach my $item (@contexts) {
 9567:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 9568:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 9569:                 if (ref($cancreate{$item}) eq 'ARRAY') {
 9570:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 9571:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9572:                             push(@{$changes{'cancreate'}},$item);
 9573:                         }
 9574:                     }
 9575:                 }
 9576:             }
 9577:             if (ref($cancreate{$item}) eq 'ARRAY') {
 9578:                 foreach my $type (@{$cancreate{$item}}) {
 9579:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 9580:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9581:                             push(@{$changes{'cancreate'}},$item);
 9582:                         }
 9583:                     }
 9584:                 }
 9585:             }
 9586:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
 9587:             if (ref($cancreate{$item}) eq 'HASH') {
 9588:                 foreach my $curr (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
 9589:                     if (ref($curr_usercreation{'cancreate'}{$item}{$curr}) eq 'HASH') {
 9590:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$curr}})) {
 9591:                             unless ($curr_usercreation{'cancreate'}{$item}{$curr}{$field} eq $cancreate{$item}{$curr}{$field}) {
 9592:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9593:                                     push(@{$changes{'cancreate'}},$item);
 9594:                                 }
 9595:                             }
 9596:                         }
 9597:                     } elsif ($item eq 'selfcreateprocessing') {
 9598:                         if ($cancreate{$item}{$curr} ne $curr_usercreation{'cancreate'}{$item}{$curr}) {
 9599:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9600:                                 push(@{$changes{'cancreate'}},$item);
 9601:                             }
 9602:                         }
 9603:                     } else {
 9604:                         if (!$cancreate{$item}{$curr}) {
 9605:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9606:                                 push(@{$changes{'cancreate'}},$item);
 9607:                             }
 9608:                         }
 9609:                     }
 9610:                 }
 9611:                 foreach my $field (keys(%{$cancreate{$item}})) {
 9612:                     if (ref($cancreate{$item}{$field}) eq 'HASH') {
 9613:                         foreach my $inner (keys(%{$cancreate{$item}{$field}})) {
 9614:                             if (ref($curr_usercreation{'cancreate'}{$item}{$field}) eq 'HASH') {
 9615:                                 unless ($curr_usercreation{'cancreate'}{$item}{$field}{$inner} eq $cancreate{$item}{$field}{$inner}) {
 9616:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9617:                                         push(@{$changes{'cancreate'}},$item);
 9618:                                     }
 9619:                                 }
 9620:                             } else {
 9621:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9622:                                     push(@{$changes{'cancreate'}},$item);
 9623:                                 }
 9624:                             }
 9625:                         }
 9626:                     } elsif ($item eq 'selfcreateprocessing') {
 9627:                         if ($cancreate{$item}{$field} ne $curr_usercreation{'cancreate'}{$item}{$field}) {
 9628:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9629:                                 push(@{$changes{'cancreate'}},$item);
 9630:                             }
 9631:                         }
 9632:                     } else {
 9633:                         if (!$curr_usercreation{'cancreate'}{$item}{$field}) {
 9634:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9635:                                 push(@{$changes{'cancreate'}},$item);
 9636:                             }
 9637:                         }
 9638:                     }
 9639:                 }
 9640:             }
 9641:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
 9642:             if (ref($cancreate{$item}) eq 'ARRAY') {
 9643:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
 9644:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9645:                         push(@{$changes{'cancreate'}},$item);
 9646:                     }
 9647:                 }
 9648:             } elsif (ref($cancreate{$item}) eq 'HASH') {
 9649:                 if (!$cancreate{$item}{$curr_usercreation{'cancreate'}{$item}}) {
 9650:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9651:                         push(@{$changes{'cancreate'}},$item);
 9652:                     }
 9653:                 }
 9654:             }
 9655:         } elsif ($item eq 'emailusername') {
 9656:             if (ref($cancreate{$item}) eq 'HASH') {
 9657:                 foreach my $type (keys(%{$cancreate{$item}})) {
 9658:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
 9659:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
 9660:                             if ($cancreate{$item}{$type}{$field}) {
 9661:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9662:                                     push(@{$changes{'cancreate'}},$item);
 9663:                                 }
 9664:                                 last;
 9665:                             }
 9666:                         }
 9667:                     }
 9668:                 }
 9669:             }
 9670:         }
 9671:     }
 9672: #
 9673: # Populate %save_usercreate hash with updates to self-creation configuration.
 9674: #
 9675:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
 9676:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
 9677:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
 9678:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
 9679:     if (ref($cancreate{'notify'}) eq 'HASH') {
 9680:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
 9681:     }
 9682:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
 9683:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
 9684:     }
 9685:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 9686:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
 9687:     }
 9688:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
 9689:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
 9690:     }
 9691:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
 9692:     $save_usercreate{'emailrule'} = \@email_rule;
 9693: 
 9694:     my %userconfig_hash = (
 9695:             usercreation     => \%save_usercreate,
 9696:             usermodification => \%save_usermodify,
 9697:     );
 9698:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
 9699:                                              $dom);
 9700: #
 9701: # Accumulate details of changes to domain cofiguration for self-creation of usernames in $resulttext
 9702: #
 9703:     if ($putresult eq 'ok') {
 9704:         if (keys(%changes) > 0) {
 9705:             $resulttext = &mt('Changes made:').'<ul>';
 9706:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 9707:                 my %lt = &selfcreation_types();
 9708:                 foreach my $type (@{$changes{'cancreate'}}) {
 9709:                     my $chgtext;
 9710:                     if ($type eq 'selfcreate') {
 9711:                         if (@{$cancreate{$type}} == 0) {
 9712:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
 9713:                         } else {
 9714:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
 9715:                                         '<ul>';
 9716:                             foreach my $case (@{$cancreate{$type}}) {
 9717:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 9718:                             }
 9719:                             $chgtext .= '</ul>';
 9720:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 9721:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 9722:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 9723:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 9724:                                             $chgtext .= '<br />'.
 9725:                                                         '<span class="LC_warning">'.
 9726:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 9727:                                                         '</span>';
 9728:                                         }
 9729:                                     }
 9730:                                 }
 9731:                             }
 9732:                         }
 9733:                     } elsif ($type eq 'shibenv') {
 9734:                         if (keys(%{$cancreate{$type}}) == 0) {
 9735:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information'); 
 9736:                         } else {
 9737:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
 9738:                                         '<ul>';
 9739:                             foreach my $field (@shibfields) {
 9740:                                 next if ($cancreate{$type}{$field} eq '');
 9741:                                 if ($field eq 'inststatus') {
 9742:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
 9743:                                 } else {
 9744:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
 9745:                                 }
 9746:                             }
 9747:                             $chgtext .= '</ul>';
 9748:                         }  
 9749:                     } elsif ($type eq 'statustocreate') {
 9750:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 9751:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 9752:                             if (@{$cancreate{'selfcreate'}} > 0) {
 9753:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 9754:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 9755:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 9756:                                         $chgtext .= '<br />'.
 9757:                                                     '<span class="LC_warning">'.
 9758:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 9759:                                                     '</span>';
 9760:                                     }
 9761:                                 } elsif (ref($usertypes) eq 'HASH') {
 9762:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 9763:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 9764:                                     } else {
 9765:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 9766:                                     }
 9767:                                     $chgtext .= '<ul>';
 9768:                                     foreach my $case (@{$cancreate{$type}}) {
 9769:                                         if ($case eq 'default') {
 9770:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 9771:                                         } else {
 9772:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 9773:                                         }
 9774:                                     }
 9775:                                     $chgtext .= '</ul>';
 9776:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 9777:                                         $chgtext .= '<br /><span class="LC_warning">'.
 9778:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
 9779:                                                     '</span>';
 9780:                                     }
 9781:                                 }
 9782:                             } else {
 9783:                                 if (@{$cancreate{$type}} == 0) {
 9784:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 9785:                                 } else {
 9786:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 9787:                                 }
 9788:                             }
 9789:                         }
 9790:                     } elsif ($type eq 'selfcreateprocessing') {
 9791:                         my %choices = &Apache::lonlocal::texthash (
 9792:                                                                     automatic => 'Automatic approval',
 9793:                                                                     approval  => 'Queued for approval',
 9794:                                                                   );
 9795:                         if (@statuses > 1) {
 9796:                             $chgtext .= &mt('Processing of requests to create account with e-mail address as username set as follows:'). 
 9797:                                         '<ul>';
 9798:                            foreach my $type (@statuses) {
 9799:                                if ($type eq 'default') {
 9800:                                    $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
 9801:                                } else {
 9802:                                    $chgtext .= '<li>'.$usertypes->{$type}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
 9803:                                }
 9804:                            }
 9805:                            $chgtext .= '</ul>';
 9806:                         } else {
 9807:                            $chgtext .= &mt('Processing of requests to create account with e-mail address as username set to: "[_1]"',
 9808:                                          $choices{$cancreate{'selfcreateprocessing'}{'default'}});
 9809:                         }
 9810:                     } elsif ($type eq 'captcha') {
 9811:                         if ($savecaptcha{$type} eq 'notused') {
 9812:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 9813:                         } else {
 9814:                             my %captchas = &captcha_phrases();
 9815:                             if ($captchas{$savecaptcha{$type}}) {
 9816:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
 9817:                             } else {
 9818:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
 9819:                             }
 9820:                         }
 9821:                     } elsif ($type eq 'recaptchakeys') {
 9822:                         my ($privkey,$pubkey);
 9823:                         if (ref($savecaptcha{$type}) eq 'HASH') {
 9824:                             $pubkey = $savecaptcha{$type}{'public'};
 9825:                             $privkey = $savecaptcha{$type}{'private'};
 9826:                         }
 9827:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 9828:                         if (!$pubkey) {
 9829:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 9830:                         } else {
 9831:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 9832:                         }
 9833:                         if (!$privkey) {
 9834:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 9835:                         } else {
 9836:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 9837:                         }
 9838:                         $chgtext .= '</ul>';
 9839:                     } elsif ($type eq 'recaptchaversion') {
 9840:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
 9841:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
 9842:                         }
 9843:                     } elsif ($type eq 'emailusername') {
 9844:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
 9845:                             if (ref($types) eq 'ARRAY') {
 9846:                                 foreach my $type (@{$types}) {
 9847:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
 9848:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
 9849:                                             $chgtext .= &mt('When self-creating account with e-mail as username, the following information will be provided by [_1]:',"'$usertypes->{$type}'").
 9850:                                                     '<ul>';
 9851:                                             foreach my $field (@{$infofields}) {
 9852:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
 9853:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
 9854:                                                 }
 9855:                                             }
 9856:                                             $chgtext .= '</ul>';
 9857:                                         } else {
 9858:                                             $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 />';
 9859:                                         }
 9860:                                     } else {
 9861:                                         $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 />';
 9862:                                     }
 9863:                                 }
 9864:                             }
 9865:                         }
 9866:                     } elsif ($type eq 'notify') {
 9867:                         $chgtext = &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
 9868:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
 9869:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
 9870:                                 if ($cancreate{'notify'}{'approval'}) {
 9871:                                     $chgtext = &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
 9872:                                 }
 9873:                             }
 9874:                         }
 9875:                     }
 9876:                     if ($chgtext) {
 9877:                         $resulttext .= '<li>'.$chgtext.'</li>';
 9878:                     }
 9879:                 }
 9880:             }
 9881:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 9882:                 my ($emailrules,$emailruleorder) =
 9883:                     &Apache::lonnet::inst_userrules($dom,'email');
 9884:                 my $chgtext = '<ul>';
 9885:                 foreach my $type (@email_rule) {
 9886:                     if (ref($emailrules->{$type}) eq 'HASH') {
 9887:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 9888:                     }
 9889:                 }
 9890:                 $chgtext .= '</ul>';
 9891:                 if (@email_rule > 0) {
 9892:                     $resulttext .= '<li>'.
 9893:                                    &mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').
 9894:                                        $chgtext.
 9895:                                    '</li>';
 9896:                 } else {
 9897:                     $resulttext .= '<li>'.
 9898:                                    &mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').
 9899:                                    '</li>';
 9900:                 }
 9901:             }
 9902:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
 9903:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
 9904:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9905:                 foreach my $type (@{$changes{'selfcreate'}}) {
 9906:                     my $typename = $type;
 9907:                     if (ref($usertypes) eq 'HASH') {
 9908:                         if ($usertypes->{$type} ne '') {
 9909:                             $typename = $usertypes->{$type};
 9910:                         }
 9911:                     }
 9912:                     my @modifiable;
 9913:                     $resulttext .= '<li>'.
 9914:                                     &mt('Self-creation of account by users with status: [_1]',
 9915:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
 9916:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
 9917:                     foreach my $field (@fields) {
 9918:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
 9919:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
 9920:                         }
 9921:                     }
 9922:                     if (@modifiable > 0) {
 9923:                         $resulttext .= join(', ',@modifiable);
 9924:                     } else {
 9925:                         $resulttext .= &mt('none');
 9926:                     }
 9927:                     $resulttext .= '</li>';
 9928:                 }
 9929:                 $resulttext .= '</ul></li>';
 9930:             }
 9931:             $resulttext .= '</ul>';
 9932:         } else {
 9933:             $resulttext = &mt('No changes made to self-creation settings');
 9934:         }
 9935:     } else {
 9936:         $resulttext = '<span class="LC_error">'.
 9937:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9938:     }
 9939:     if ($warningmsg ne '') {
 9940:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 9941:     }
 9942:     return $resulttext;
 9943: }
 9944: 
 9945: sub process_captcha {
 9946:     my ($container,$changes,$newsettings,$current) = @_;
 9947:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 9948:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 9949:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 9950:         $newsettings->{'captcha'} = 'original';
 9951:     }
 9952:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 9953:         if ($container eq 'cancreate') {
 9954:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 9955:                 push(@{$changes->{'cancreate'}},'captcha');
 9956:             } elsif (!defined($changes->{'cancreate'})) {
 9957:                 $changes->{'cancreate'} = ['captcha'];
 9958:             }
 9959:         } else {
 9960:             $changes->{'captcha'} = 1;
 9961:         }
 9962:     }
 9963:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
 9964:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 9965:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 9966:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 9967:         $newpub =~ s/[^\w\-]//g;
 9968:         $newpriv =~ s/[^\w\-]//g;
 9969:         $newsettings->{'recaptchakeys'} = {
 9970:                                              public  => $newpub,
 9971:                                              private => $newpriv,
 9972:                                           };
 9973:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
 9974:         $newversion =~ s/\D//g;
 9975:         if ($newversion ne '2') {
 9976:             $newversion = 1;
 9977:         }
 9978:         $newsettings->{'recaptchaversion'} = $newversion;
 9979:     }
 9980:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 9981:         $currpub = $current->{'recaptchakeys'}{'public'};
 9982:         $currpriv = $current->{'recaptchakeys'}{'private'};
 9983:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
 9984:             $newsettings->{'recaptchakeys'} = {
 9985:                                                  public  => '',
 9986:                                                  private => '',
 9987:                                               }
 9988:         }
 9989:     }
 9990:     if ($current->{'captcha'} eq 'recaptcha') {
 9991:         $currversion = $current->{'recaptchaversion'};
 9992:         if ($currversion ne '2') {
 9993:             $currversion = 1;
 9994:         }
 9995:     }
 9996:     if ($currversion ne $newversion) {
 9997:         if ($container eq 'cancreate') {
 9998:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 9999:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
10000:             } elsif (!defined($changes->{'cancreate'})) {
10001:                 $changes->{'cancreate'} = ['recaptchaversion'];
10002:             }
10003:         } else {
10004:             $changes->{'recaptchaversion'} = 1;
10005:         }
10006:     }
10007:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
10008:         if ($container eq 'cancreate') {
10009:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
10010:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
10011:             } elsif (!defined($changes->{'cancreate'})) {
10012:                 $changes->{'cancreate'} = ['recaptchakeys'];
10013:             }
10014:         } else {
10015:             $changes->{'recaptchakeys'} = 1;
10016:         }
10017:     }
10018:     return;
10019: }
10020: 
10021: sub modify_usermodification {
10022:     my ($dom,%domconfig) = @_;
10023:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
10024:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
10025:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
10026:             if ($key eq 'selfcreate') {
10027:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
10028:             } else {  
10029:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
10030:             }
10031:         }
10032:     }
10033:     my @contexts = ('author','course');
10034:     my %context_title = (
10035:                            author => 'In author context',
10036:                            course => 'In course context',
10037:                         );
10038:     my @fields = ('lastname','firstname','middlename','generation',
10039:                   'permanentemail','id');
10040:     my %roles = (
10041:                   author => ['ca','aa'],
10042:                   course => ['st','ep','ta','in','cr'],
10043:                 );
10044:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
10045:     foreach my $context (@contexts) {
10046:         foreach my $role (@{$roles{$context}}) {
10047:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
10048:             foreach my $item (@fields) {
10049:                 if (grep(/^\Q$item\E$/,@modifiable)) {
10050:                     $modifyhash{$context}{$role}{$item} = 1;
10051:                 } else {
10052:                     $modifyhash{$context}{$role}{$item} = 0;
10053:                 }
10054:             }
10055:         }
10056:         if (ref($curr_usermodification{$context}) eq 'HASH') {
10057:             foreach my $role (@{$roles{$context}}) {
10058:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
10059:                     foreach my $field (@fields) {
10060:                         if ($modifyhash{$context}{$role}{$field} ne 
10061:                                 $curr_usermodification{$context}{$role}{$field}) {
10062:                             push(@{$changes{$context}},$role);
10063:                             last;
10064:                         }
10065:                     }
10066:                 }
10067:             }
10068:         } else {
10069:             foreach my $context (@contexts) {
10070:                 foreach my $role (@{$roles{$context}}) {
10071:                     push(@{$changes{$context}},$role);
10072:                 }
10073:             }
10074:         }
10075:     }
10076:     my %usermodification_hash =  (
10077:                                    usermodification => \%modifyhash,
10078:                                  );
10079:     my $putresult = &Apache::lonnet::put_dom('configuration',
10080:                                              \%usermodification_hash,$dom);
10081:     if ($putresult eq 'ok') {
10082:         if (keys(%changes) > 0) {
10083:             $resulttext = &mt('Changes made: ').'<ul>';
10084:             foreach my $context (@contexts) {
10085:                 if (ref($changes{$context}) eq 'ARRAY') {
10086:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
10087:                     if (ref($changes{$context}) eq 'ARRAY') {
10088:                         foreach my $role (@{$changes{$context}}) {
10089:                             my $rolename;
10090:                             if ($role eq 'cr') {
10091:                                 $rolename = &mt('Custom');
10092:                             } else {
10093:                                 $rolename = &Apache::lonnet::plaintext($role);
10094:                             }
10095:                             my @modifiable;
10096:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
10097:                             foreach my $field (@fields) {
10098:                                 if ($modifyhash{$context}{$role}{$field}) {
10099:                                     push(@modifiable,$fieldtitles{$field});
10100:                                 }
10101:                             }
10102:                             if (@modifiable > 0) {
10103:                                 $resulttext .= join(', ',@modifiable);
10104:                             } else {
10105:                                 $resulttext .= &mt('none'); 
10106:                             }
10107:                             $resulttext .= '</li>';
10108:                         }
10109:                         $resulttext .= '</ul></li>';
10110:                     }
10111:                 }
10112:             }
10113:             $resulttext .= '</ul>';
10114:         } else {
10115:             $resulttext = &mt('No changes made to user modification settings');
10116:         }
10117:     } else {
10118:         $resulttext = '<span class="LC_error">'.
10119:             &mt('An error occurred: [_1]',$putresult).'</span>';
10120:     }
10121:     return $resulttext;
10122: }
10123: 
10124: sub modify_defaults {
10125:     my ($dom,$lastactref,%domconfig) = @_;
10126:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
10127:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10128:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
10129:     my @authtypes = ('internal','krb4','krb5','localauth');
10130:     foreach my $item (@items) {
10131:         $newvalues{$item} = $env{'form.'.$item};
10132:         if ($item eq 'auth_def') {
10133:             if ($newvalues{$item} ne '') {
10134:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
10135:                     push(@errors,$item);
10136:                 }
10137:             }
10138:         } elsif ($item eq 'lang_def') {
10139:             if ($newvalues{$item} ne '') {
10140:                 if ($newvalues{$item} =~ /^(\w+)/) {
10141:                     my $langcode = $1;
10142:                     if ($langcode ne 'x_chef') {
10143:                         if (code2language($langcode) eq '') {
10144:                             push(@errors,$item);
10145:                         }
10146:                     }
10147:                 } else {
10148:                     push(@errors,$item);
10149:                 }
10150:             }
10151:         } elsif ($item eq 'timezone_def') {
10152:             if ($newvalues{$item} ne '') {
10153:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
10154:                     push(@errors,$item);   
10155:                 }
10156:             }
10157:         } elsif ($item eq 'datelocale_def') {
10158:             if ($newvalues{$item} ne '') {
10159:                 my @datelocale_ids = DateTime::Locale->ids();
10160:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
10161:                     push(@errors,$item);
10162:                 }
10163:             }
10164:         } elsif ($item eq 'portal_def') {
10165:             if ($newvalues{$item} ne '') {
10166:                 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])\/?$/) {
10167:                     push(@errors,$item);
10168:                 }
10169:             }
10170:         }
10171:         if (grep(/^\Q$item\E$/,@errors)) {
10172:             $newvalues{$item} = $domdefaults{$item};
10173:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
10174:             $changes{$item} = 1;
10175:         }
10176:         $domdefaults{$item} = $newvalues{$item};
10177:     }
10178:     my %defaults_hash = (
10179:                          defaults => \%newvalues,
10180:                         );
10181:     my $title = &defaults_titles();
10182: 
10183:     my $currinststatus;
10184:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
10185:         $currinststatus = $domconfig{'inststatus'};
10186:     } else {
10187:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10188:         $currinststatus = {
10189:                              inststatustypes => $usertypes,
10190:                              inststatusorder => $types,
10191:                              inststatusguest => [],
10192:                           };
10193:     }
10194:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
10195:     my @allpos;
10196:     my %guests;
10197:     my %alltypes;
10198:     my ($currtitles,$currguests,$currorder);
10199:     if (ref($currinststatus) eq 'HASH') {
10200:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
10201:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
10202:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
10203:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
10204:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
10205:                     }
10206:                 }
10207:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
10208:                     my $position = $env{'form.inststatus_pos_'.$type};
10209:                     $position =~ s/\D+//g;
10210:                     $allpos[$position] = $type;
10211:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
10212:                     $alltypes{$type} =~ s/`//g;
10213:                     if ($env{'form.inststatus_guest_'.$type}) {
10214:                         $guests{$type} = 1;
10215:                     }
10216:                 }
10217:             }
10218:             if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
10219:                 $currguests = join(',',@{$currinststatus->{'inststatusguest'}});
10220:             }
10221:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
10222:             $currtitles =~ s/,$//;
10223:         }
10224:     }
10225:     if ($env{'form.addinststatus'}) {
10226:         my $newtype = $env{'form.addinststatus'};
10227:         $newtype =~ s/\W//g;
10228:         unless (exists($alltypes{$newtype})) {
10229:             if ($env{'form.addinststatus_guest'}) {
10230:                 $guests{$newtype} = 1;
10231:             }
10232:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
10233:             $alltypes{$newtype} =~ s/`//g; 
10234:             my $position = $env{'form.addinststatus_pos'};
10235:             $position =~ s/\D+//g;
10236:             if ($position ne '') {
10237:                 $allpos[$position] = $newtype;
10238:             }
10239:         }
10240:     }
10241:     my (@orderedstatus,@orderedguests);
10242:     foreach my $type (@allpos) {
10243:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
10244:             push(@orderedstatus,$type);
10245:             if ($guests{$type}) {
10246:                 push(@orderedguests,$type);
10247:             }
10248:         }
10249:     }
10250:     foreach my $type (keys(%alltypes)) {
10251:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
10252:             delete($alltypes{$type});
10253:         }
10254:     }
10255:     $defaults_hash{'inststatus'} = {
10256:                                      inststatustypes => \%alltypes,
10257:                                      inststatusorder => \@orderedstatus,
10258:                                      inststatusguest => \@orderedguests,
10259:                                    };
10260:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
10261:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
10262:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
10263:         }
10264:     }
10265:     if ($currorder ne join(',',@orderedstatus)) {
10266:         $changes{'inststatus'}{'inststatusorder'} = 1;
10267:     }
10268:     if ($currguests ne join(',',@orderedguests)) {
10269:         $changes{'inststatus'}{'inststatusguest'} = 1;
10270:     }
10271:     my $newtitles;
10272:     foreach my $item (@orderedstatus) {
10273:         $newtitles .= $alltypes{$item}.',';
10274:     }
10275:     $newtitles =~ s/,$//;
10276:     if ($currtitles ne $newtitles) {
10277:         $changes{'inststatus'}{'inststatustypes'} = 1;
10278:     }
10279:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
10280:                                              $dom);
10281:     if ($putresult eq 'ok') {
10282:         if (keys(%changes) > 0) {
10283:             $resulttext = &mt('Changes made:').'<ul>';
10284:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
10285:             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";
10286:             foreach my $item (sort(keys(%changes))) {
10287:                 if ($item eq 'inststatus') {
10288:                     if (ref($changes{'inststatus'}) eq 'HASH') {
10289:                         if (($changes{'inststatus'}{'inststatustypes'}) || $changes{'inststatus'}{'inststatusorder'}) {
10290:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
10291:                             foreach my $type (@orderedstatus) { 
10292:                                 $resulttext .= $alltypes{$type}.', ';
10293:                             }
10294:                             $resulttext =~ s/, $//;
10295:                             $resulttext .= '</li>';
10296:                         }
10297:                         if ($changes{'inststatus'}{'inststatusguest'}) {
10298:                             $resulttext .= '<li>'; 
10299:                             if (@orderedguests) {
10300:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to:').' ';
10301:                                 foreach my $type (@orderedguests) {
10302:                                     $resulttext .= $alltypes{$type}.', ';
10303:                                 }
10304:                                 $resulttext =~ s/, $//;
10305:                             } else {
10306:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to none.');
10307:                             }
10308:                             $resulttext .= '</li>';
10309:                         }
10310:                     }
10311:                 } else {
10312:                     my $value = $env{'form.'.$item};
10313:                     if ($value eq '') {
10314:                         $value = &mt('none');
10315:                     } elsif ($item eq 'auth_def') {
10316:                         my %authnames = &authtype_names();
10317:                         my %shortauth = (
10318:                                           internal   => 'int',
10319:                                           krb4       => 'krb4',
10320:                                           krb5       => 'krb5',
10321:                                           localauth  => 'loc',
10322:                         );
10323:                         $value = $authnames{$shortauth{$value}};
10324:                     }
10325:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
10326:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
10327:                 }
10328:             }
10329:             $resulttext .= '</ul>';
10330:             $mailmsgtext .= "\n";
10331:             my $cachetime = 24*60*60;
10332:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10333:             if (ref($lastactref) eq 'HASH') {
10334:                 $lastactref->{'domdefaults'} = 1;
10335:             }
10336:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
10337:                 my $notify = 1;
10338:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
10339:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
10340:                         $notify = 0;
10341:                     }
10342:                 }
10343:                 if ($notify) {
10344:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
10345:                                                "LON-CAPA Domain Settings Change - $dom",
10346:                                                $mailmsgtext);
10347:                 }
10348:             }
10349:         } else {
10350:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
10351:         }
10352:     } else {
10353:         $resulttext = '<span class="LC_error">'.
10354:             &mt('An error occurred: [_1]',$putresult).'</span>';
10355:     }
10356:     if (@errors > 0) {
10357:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
10358:         foreach my $item (@errors) {
10359:             $resulttext .= ' "'.$title->{$item}.'",';
10360:         }
10361:         $resulttext =~ s/,$//;
10362:     }
10363:     return $resulttext;
10364: }
10365: 
10366: sub modify_scantron {
10367:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
10368:     my ($resulttext,%confhash,%changes,$errors);
10369:     my $custom = 'custom.tab';
10370:     my $default = 'default.tab';
10371:     my $servadm = $r->dir_config('lonAdmEMail');
10372:     my ($configuserok,$author_ok,$switchserver) = 
10373:         &config_check($dom,$confname,$servadm);
10374:     if ($env{'form.scantronformat.filename'} ne '') {
10375:         my $error;
10376:         if ($configuserok eq 'ok') {
10377:             if ($switchserver) {
10378:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
10379:             } else {
10380:                 if ($author_ok eq 'ok') {
10381:                     my ($result,$scantronurl) =
10382:                         &publishlogo($r,'upload','scantronformat',$dom,
10383:                                      $confname,'scantron','','',$custom);
10384:                     if ($result eq 'ok') {
10385:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
10386:                         $changes{'scantronformat'} = 1;
10387:                     } else {
10388:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
10389:                     }
10390:                 } else {
10391:                     $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);
10392:                 }
10393:             }
10394:         } else {
10395:             $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);
10396:         }
10397:         if ($error) {
10398:             &Apache::lonnet::logthis($error);
10399:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10400:         }
10401:     }
10402:     if (ref($domconfig{'scantron'}) eq 'HASH') {
10403:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
10404:             if ($env{'form.scantronformat_del'}) {
10405:                 $confhash{'scantron'}{'scantronformat'} = '';
10406:                 $changes{'scantronformat'} = 1;
10407:             }
10408:         }
10409:     }
10410:     if (keys(%confhash) > 0) {
10411:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
10412:                                                  $dom);
10413:         if ($putresult eq 'ok') {
10414:             if (keys(%changes) > 0) {
10415:                 if (ref($confhash{'scantron'}) eq 'HASH') {
10416:                     $resulttext = &mt('Changes made:').'<ul>';
10417:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
10418:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
10419:                     } else {
10420:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
10421:                     }
10422:                     $resulttext .= '</ul>';
10423:                 } else {
10424:                     $resulttext = &mt('Changes made to bubblesheet format file.');
10425:                 }
10426:                 $resulttext .= '</ul>';
10427:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
10428:                 if (ref($lastactref) eq 'HASH') {
10429:                     $lastactref->{'domainconfig'} = 1;
10430:                 }
10431:             } else {
10432:                 $resulttext = &mt('No changes made to bubblesheet format file');
10433:             }
10434:         } else {
10435:             $resulttext = '<span class="LC_error">'.
10436:                 &mt('An error occurred: [_1]',$putresult).'</span>';
10437:         }
10438:     } else {
10439:         $resulttext = &mt('No changes made to bubblesheet format file'); 
10440:     }
10441:     if ($errors) {
10442:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
10443:                        $errors.'</ul>';
10444:     }
10445:     return $resulttext;
10446: }
10447: 
10448: sub modify_coursecategories {
10449:     my ($dom,$lastactref,%domconfig) = @_;
10450:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
10451:         $cathash);
10452:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
10453:     my @catitems = ('unauth','auth');
10454:     my @cattypes = ('std','domonly','codesrch','none');
10455:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
10456:         $cathash = $domconfig{'coursecategories'}{'cats'};
10457:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
10458:             $changes{'togglecats'} = 1;
10459:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
10460:         }
10461:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
10462:             $changes{'categorize'} = 1;
10463:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
10464:         }
10465:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
10466:             $changes{'togglecatscomm'} = 1;
10467:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
10468:         }
10469:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
10470:             $changes{'categorizecomm'} = 1;
10471:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
10472:         }
10473:         foreach my $item (@catitems) {
10474:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
10475:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
10476:                     $changes{$item} = 1;
10477:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
10478:                 }
10479:             }
10480:         }
10481:     } else {
10482:         $changes{'togglecats'} = 1;
10483:         $changes{'categorize'} = 1;
10484:         $changes{'togglecatscomm'} = 1;
10485:         $changes{'categorizecomm'} = 1;
10486:         $domconfig{'coursecategories'} = {
10487:                                              togglecats => $env{'form.togglecats'},
10488:                                              categorize => $env{'form.categorize'},
10489:                                              togglecatscomm => $env{'form.togglecatscomm'},
10490:                                              categorizecomm => $env{'form.categorizecomm'},
10491:                                          };
10492:         foreach my $item (@catitems) {
10493:             if ($env{'form.coursecat_'.$item} ne 'std') {
10494:                 $changes{$item} = 1;
10495:             }
10496:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
10497:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
10498:             }
10499:         }
10500:     }
10501:     if (ref($cathash) eq 'HASH') {
10502:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
10503:             push (@deletecategory,'instcode::0');
10504:         }
10505:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
10506:             push(@deletecategory,'communities::0');
10507:         }
10508:     }
10509:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
10510:     if (ref($cathash) eq 'HASH') {
10511:         if (@deletecategory > 0) {
10512:             #FIXME Need to remove category from all courses using a deleted category 
10513:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
10514:             foreach my $item (@deletecategory) {
10515:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
10516:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
10517:                     $deletions{$item} = 1;
10518:                     &recurse_cat_deletes($item,$cathash,\%deletions);
10519:                 }
10520:             }
10521:         }
10522:         foreach my $item (keys(%{$cathash})) {
10523:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
10524:             if ($cathash->{$item} ne $env{'form.'.$item}) {
10525:                 $reorderings{$item} = 1;
10526:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
10527:             }
10528:             if ($env{'form.addcategory_name_'.$item} ne '') {
10529:                 my $newcat = $env{'form.addcategory_name_'.$item};
10530:                 my $newdepth = $depth+1;
10531:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
10532:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
10533:                 $adds{$newitem} = 1; 
10534:             }
10535:             if ($env{'form.subcat_'.$item} ne '') {
10536:                 my $newcat = $env{'form.subcat_'.$item};
10537:                 my $newdepth = $depth+1;
10538:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
10539:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
10540:                 $adds{$newitem} = 1;
10541:             }
10542:         }
10543:     }
10544:     if ($env{'form.instcode'} eq '1') {
10545:         if (ref($cathash) eq 'HASH') {
10546:             my $newitem = 'instcode::0';
10547:             if ($cathash->{$newitem} eq '') {  
10548:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
10549:                 $adds{$newitem} = 1;
10550:             }
10551:         } else {
10552:             my $newitem = 'instcode::0';
10553:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
10554:             $adds{$newitem} = 1;
10555:         }
10556:     }
10557:     if ($env{'form.communities'} eq '1') {
10558:         if (ref($cathash) eq 'HASH') {
10559:             my $newitem = 'communities::0';
10560:             if ($cathash->{$newitem} eq '') {
10561:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
10562:                 $adds{$newitem} = 1;
10563:             }
10564:         } else {
10565:             my $newitem = 'communities::0';
10566:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
10567:             $adds{$newitem} = 1;
10568:         }
10569:     }
10570:     if ($env{'form.addcategory_name'} ne '') {
10571:         if (($env{'form.addcategory_name'} ne 'instcode') &&
10572:             ($env{'form.addcategory_name'} ne 'communities')) {
10573:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
10574:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
10575:             $adds{$newitem} = 1;
10576:         }
10577:     }
10578:     my $putresult;
10579:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
10580:         if (keys(%deletions) > 0) {
10581:             foreach my $key (keys(%deletions)) {
10582:                 if ($predelallitems{$key} ne '') {
10583:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
10584:                 }
10585:             }
10586:         }
10587:         my (@chkcats,@chktrails,%chkallitems);
10588:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
10589:         if (ref($chkcats[0]) eq 'ARRAY') {
10590:             my $depth = 0;
10591:             my $chg = 0;
10592:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
10593:                 my $name = $chkcats[0][$i];
10594:                 my $item;
10595:                 if ($name eq '') {
10596:                     $chg ++;
10597:                 } else {
10598:                     $item = &escape($name).'::0';
10599:                     if ($chg) {
10600:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
10601:                     }
10602:                     $depth ++; 
10603:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
10604:                     $depth --;
10605:                 }
10606:             }
10607:         }
10608:     }
10609:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
10610:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
10611:         if ($putresult eq 'ok') {
10612:             my %title = (
10613:                          togglecats     => 'Show/Hide a course in catalog',
10614:                          categorize     => 'Assign a category to a course',
10615:                          togglecatscomm => 'Show/Hide a community in catalog',
10616:                          categorizecomm => 'Assign a category to a community',
10617:                         );
10618:             my %level = (
10619:                          dom  => 'set in Domain ("Modify Course/Community")',
10620:                          crs  => 'set in Course ("Course Configuration")',
10621:                          comm => 'set in Community ("Community Configuration")',
10622:                          none     => 'No catalog',
10623:                          std      => 'Standard catalog',
10624:                          domonly  => 'Domain-only catalog',
10625:                          codesrch => 'Code search form',
10626:                         );
10627:             $resulttext = &mt('Changes made:').'<ul>';
10628:             if ($changes{'togglecats'}) {
10629:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
10630:             }
10631:             if ($changes{'categorize'}) {
10632:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
10633:             }
10634:             if ($changes{'togglecatscomm'}) {
10635:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
10636:             }
10637:             if ($changes{'categorizecomm'}) {
10638:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
10639:             }
10640:             if ($changes{'unauth'}) {
10641:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
10642:             }
10643:             if ($changes{'auth'}) {
10644:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
10645:             }
10646:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
10647:                 my $cathash;
10648:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
10649:                     $cathash = $domconfig{'coursecategories'}{'cats'};
10650:                 } else {
10651:                     $cathash = {};
10652:                 } 
10653:                 my (@cats,@trails,%allitems);
10654:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
10655:                 if (keys(%deletions) > 0) {
10656:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
10657:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
10658:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
10659:                     }
10660:                     $resulttext .= '</ul></li>';
10661:                 }
10662:                 if (keys(%reorderings) > 0) {
10663:                     my %sort_by_trail;
10664:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
10665:                     foreach my $key (keys(%reorderings)) {
10666:                         if ($allitems{$key} ne '') {
10667:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
10668:                         }
10669:                     }
10670:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
10671:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
10672:                     }
10673:                     $resulttext .= '</ul></li>';
10674:                 }
10675:                 if (keys(%adds) > 0) {
10676:                     my %sort_by_trail;
10677:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
10678:                     foreach my $key (keys(%adds)) {
10679:                         if ($allitems{$key} ne '') {
10680:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
10681:                         }
10682:                     }
10683:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
10684:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
10685:                     }
10686:                     $resulttext .= '</ul></li>';
10687:                 }
10688:             }
10689:             $resulttext .= '</ul>';
10690:             if ($changes{'unauth'} || $changes{'auth'}) {
10691:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
10692:                 if ($changes{'auth'}) {
10693:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
10694:                 }
10695:                 if ($changes{'unauth'}) {
10696:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
10697:                 }
10698:                 my $cachetime = 24*60*60;
10699:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10700:                 if (ref($lastactref) eq 'HASH') {
10701:                     $lastactref->{'domdefaults'} = 1;
10702:                 }
10703:             }
10704:         } else {
10705:             $resulttext = '<span class="LC_error">'.
10706:                           &mt('An error occurred: [_1]',$putresult).'</span>';
10707:         }
10708:     } else {
10709:         $resulttext = &mt('No changes made to course and community categories');
10710:     }
10711:     return $resulttext;
10712: }
10713: 
10714: sub modify_serverstatuses {
10715:     my ($dom,%domconfig) = @_;
10716:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
10717:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
10718:         %currserverstatus = %{$domconfig{'serverstatuses'}};
10719:     }
10720:     my @pages = &serverstatus_pages();
10721:     foreach my $type (@pages) {
10722:         $newserverstatus{$type}{'namedusers'} = '';
10723:         $newserverstatus{$type}{'machines'} = '';
10724:         if (defined($env{'form.'.$type.'_namedusers'})) {
10725:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
10726:             my @okusers;
10727:             foreach my $user (@users) {
10728:                 my ($uname,$udom) = split(/:/,$user);
10729:                 if (($udom =~ /^$match_domain$/) &&   
10730:                     (&Apache::lonnet::domain($udom)) &&
10731:                     ($uname =~ /^$match_username$/)) {
10732:                     if (!grep(/^\Q$user\E/,@okusers)) {
10733:                         push(@okusers,$user);
10734:                     }
10735:                 }
10736:             }
10737:             if (@okusers > 0) {
10738:                  @okusers = sort(@okusers);
10739:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
10740:             }
10741:         }
10742:         if (defined($env{'form.'.$type.'_machines'})) {
10743:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
10744:             my @okmachines;
10745:             foreach my $ip (@machines) {
10746:                 my @parts = split(/\./,$ip);
10747:                 next if (@parts < 4);
10748:                 my $badip = 0;
10749:                 for (my $i=0; $i<4; $i++) {
10750:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
10751:                         $badip = 1;
10752:                         last;
10753:                     }
10754:                 }
10755:                 if (!$badip) {
10756:                     push(@okmachines,$ip);     
10757:                 }
10758:             }
10759:             @okmachines = sort(@okmachines);
10760:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
10761:         }
10762:     }
10763:     my %serverstatushash =  (
10764:                                 serverstatuses => \%newserverstatus,
10765:                             );
10766:     foreach my $type (@pages) {
10767:         foreach my $setting ('namedusers','machines') {
10768:             my (@current,@new);
10769:             if (ref($currserverstatus{$type}) eq 'HASH') {
10770:                 if ($currserverstatus{$type}{$setting} ne '') { 
10771:                     @current = split(/,/,$currserverstatus{$type}{$setting});
10772:                 }
10773:             }
10774:             if ($newserverstatus{$type}{$setting} ne '') {
10775:                 @new = split(/,/,$newserverstatus{$type}{$setting});
10776:             }
10777:             if (@current > 0) {
10778:                 if (@new > 0) {
10779:                     foreach my $item (@current) {
10780:                         if (!grep(/^\Q$item\E$/,@new)) {
10781:                             $changes{$type}{$setting} = 1;
10782:                             last;
10783:                         }
10784:                     }
10785:                     foreach my $item (@new) {
10786:                         if (!grep(/^\Q$item\E$/,@current)) {
10787:                             $changes{$type}{$setting} = 1;
10788:                             last;
10789:                         }
10790:                     }
10791:                 } else {
10792:                     $changes{$type}{$setting} = 1;
10793:                 }
10794:             } elsif (@new > 0) {
10795:                 $changes{$type}{$setting} = 1;
10796:             }
10797:         }
10798:     }
10799:     if (keys(%changes) > 0) {
10800:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
10801:         my $putresult = &Apache::lonnet::put_dom('configuration',
10802:                                                  \%serverstatushash,$dom);
10803:         if ($putresult eq 'ok') {
10804:             $resulttext .= &mt('Changes made:').'<ul>';
10805:             foreach my $type (@pages) {
10806:                 if (ref($changes{$type}) eq 'HASH') {
10807:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
10808:                     if ($changes{$type}{'namedusers'}) {
10809:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
10810:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
10811:                         } else {
10812:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
10813:                         }
10814:                     }
10815:                     if ($changes{$type}{'machines'}) {
10816:                         if ($newserverstatus{$type}{'machines'} eq '') {
10817:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
10818:                         } else {
10819:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
10820:                         }
10821: 
10822:                     }
10823:                     $resulttext .= '</ul></li>';
10824:                 }
10825:             }
10826:             $resulttext .= '</ul>';
10827:         } else {
10828:             $resulttext = '<span class="LC_error">'.
10829:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
10830: 
10831:         }
10832:     } else {
10833:         $resulttext = &mt('No changes made to access to server status pages');
10834:     }
10835:     return $resulttext;
10836: }
10837: 
10838: sub modify_helpsettings {
10839:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
10840:     my ($resulttext,$errors,%changes,%helphash);
10841:     my %defaultchecked = ('submitbugs' => 'on');
10842:     my @offon = ('off','on');
10843:     my @toggles = ('submitbugs');
10844:     my %current = ('submitbugs' => '',
10845:                    'adhoc'      => {},
10846:                   );
10847:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
10848:         %current = %{$domconfig{'helpsettings'}};
10849:     }
10850:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10851:     foreach my $item (@toggles) {
10852:         if ($defaultchecked{$item} eq 'on') { 
10853:             if ($current{$item} eq '') {
10854:                 if ($env{'form.'.$item} eq '0') {
10855:                     $changes{$item} = 1;
10856:                 }
10857:             } elsif ($current{$item} ne $env{'form.'.$item}) {
10858:                 $changes{$item} = 1;
10859:             }
10860:         } elsif ($defaultchecked{$item} eq 'off') {
10861:             if ($current{$item} eq '') {
10862:                 if ($env{'form.'.$item} eq '1') {
10863:                     $changes{$item} = 1;
10864:                 }
10865:             } elsif ($current{$item} ne $env{'form.'.$item}) {
10866:                 $changes{$item} = 1;
10867:             }
10868:         }
10869:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
10870:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
10871:         }
10872:     }
10873:     my $maxnum = $env{'form.helproles_maxnum'};
10874:     my $confname = $dom.'-domainconfig';
10875:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
10876:     my (@allpos,%newsettings,%changedprivs,$newrole);
10877:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10878:     my @accesstypes = ('all','none','status','inc','exc');
10879:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh']);
10880:     my %lt = &Apache::lonlocal::texthash(
10881:                     s      => 'system',
10882:                     d      => 'domain',
10883:                     order  => 'Display order',
10884:                     access => 'Role usage',
10885:                     all    => 'All',
10886:                     none   => 'None',
10887:                     status => 'Determined based on institutional status',
10888:                     inc    => 'Include all, but exclude specific personnel',
10889:                     exc    => 'Exclude all, but include specific personnel',
10890:     );
10891:     for (my $num=0; $num<=$maxnum; $num++) {
10892:         my ($prefix,$identifier,$rolename,%curr);
10893:         if ($num == $maxnum) {
10894:             next unless ($env{'form.newcusthelp'} == $maxnum);
10895:             $identifier = 'custhelp'.$num;
10896:             $prefix = 'helproles_'.$num;
10897:             $rolename = $env{'form.custhelpname'.$num};
10898:             $rolename=~s/[^A-Za-z0-9]//gs;
10899:             next if ($rolename eq '');
10900:             next if (exists($existing{'rolesdef_'.$rolename}));
10901:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
10902:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
10903:                                                      $newprivs{'c'},$confname,$dom);
10904:             if ($result ne 'ok') {
10905:                 $errors .= '<li><span class="LC_error">'.
10906:                            &mt('An error occurred storing the new custom role: [_1]',
10907:                            $result).'</span></li>';
10908:                 next;
10909:             } else {
10910:                 $changedprivs{$rolename} = \%newprivs;
10911:                 $newrole = $rolename;
10912:             }
10913:         } else {
10914:             $prefix = 'helproles_'.$num;
10915:             $rolename = $env{'form.'.$prefix};
10916:             next if ($rolename eq '');
10917:             next unless (exists($existing{'rolesdef_'.$rolename}));
10918:             $identifier = 'custhelp'.$num;
10919:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
10920:             my %currprivs;
10921:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
10922:                 split(/\_/,$existing{'rolesdef_'.$rolename});
10923:             foreach my $level ('c','d','s') {
10924:                 if ($newprivs{$level} ne $currprivs{$level}) {
10925:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
10926:                                                              $newprivs{'c'},$confname,$dom);
10927:                     if ($result ne 'ok') {
10928:                         $errors .= '<li><span class="LC_error">'.
10929:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
10930:                                        $rolename,$result).'</span></li>';
10931:                     } else {
10932:                         $changedprivs{$rolename} = \%newprivs;
10933:                     }
10934:                     last;
10935:                 }
10936:             }
10937:             if (ref($current{'adhoc'}) eq 'HASH') {
10938:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
10939:                     %curr = %{$current{'adhoc'}{$rolename}};
10940:                 }
10941:             }
10942:         }
10943:         my $newpos = $env{'form.'.$prefix.'_pos'};
10944:         $newpos =~ s/\D+//g;
10945:         $allpos[$newpos] = $rolename;
10946:         my $newdesc = $env{'form.'.$prefix.'_desc'};
10947:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
10948:         if ($curr{'desc'}) {
10949:             if ($curr{'desc'} ne $newdesc) {
10950:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
10951:                 $newsettings{$rolename}{'desc'} = $newdesc;
10952:             }
10953:         } elsif ($newdesc ne '') {
10954:             $changes{'customrole'}{$rolename}{'desc'} = 1;
10955:             $newsettings{$rolename}{'desc'} = $newdesc;
10956:         }
10957:         my $access = $env{'form.'.$prefix.'_access'};
10958:         if (grep(/^\Q$access\E$/,@accesstypes)) {
10959:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
10960:             if ($access eq 'status') {
10961:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
10962:                 if (scalar(@statuses) == 0) {
10963:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
10964:                 } else {
10965:                     my (@shownstatus,$numtypes);
10966:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
10967:                     if (ref($types) eq 'ARRAY') {
10968:                         $numtypes = scalar(@{$types});
10969:                         foreach my $type (sort(@statuses)) {
10970:                             if ($type eq 'default') {
10971:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
10972:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
10973:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
10974:                                 push(@shownstatus,$usertypes->{$type});
10975:                             }
10976:                         }
10977:                     }
10978:                     if (grep(/^default$/,@statuses)) {
10979:                         push(@shownstatus,$othertitle);
10980:                     }
10981:                     if (scalar(@shownstatus) == 1+$numtypes) {
10982:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
10983:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
10984:                     } else {
10985:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
10986:                         if (ref($curr{'status'}) eq 'ARRAY') {
10987:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
10988:                             if (@diffs) {
10989:                                 $changes{'customrole'}{$rolename}{$access} = 1;
10990:                             }
10991:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
10992:                             $changes{'customrole'}{$rolename}{$access} = 1;
10993:                         }
10994:                     }
10995:                 }
10996:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
10997:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
10998:                 my @newspecstaff;
10999:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
11000:                 foreach my $person (sort(@personnel)) {
11001:                     if ($domhelpdesk{$person}) {
11002:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
11003:                     }
11004:                 }
11005:                 if (ref($curr{$access}) eq 'ARRAY') {
11006:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
11007:                     if (@diffs) {
11008:                         $changes{'customrole'}{$rolename}{$access} = 1;
11009:                     }
11010:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
11011:                     $changes{'customrole'}{$rolename}{$access} = 1;
11012:                 }
11013:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
11014:                     my ($uname,$udom) = split(/:/,$person);
11015:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
11016:                 }
11017:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
11018:             }
11019:         } else {
11020:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
11021:         }
11022:         unless ($curr{'access'} eq $access) {
11023:             $changes{'customrole'}{$rolename}{'access'} = 1;
11024:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
11025:         }
11026:     }
11027:     if (@allpos > 0) {
11028:         my $idx = 0;
11029:         foreach my $rolename (@allpos) {
11030:             if ($rolename ne '') {
11031:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
11032:                 if (ref($current{'adhoc'}) eq 'HASH') {
11033:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
11034:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
11035:                             $changes{'customrole'}{$rolename}{'order'} = 1;
11036:                             $newsettings{$rolename}{'order'} = $idx+1;
11037:                         }
11038:                     }
11039:                 }
11040:                 $idx ++;
11041:             }
11042:         }
11043:     }
11044:     my $putresult;
11045:     if (keys(%changes) > 0) {
11046:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
11047:         if ($putresult eq 'ok') {
11048:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
11049:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
11050:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
11051:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
11052:                 }
11053:             }
11054:             my $cachetime = 24*60*60;
11055:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
11056:             if (ref($lastactref) eq 'HASH') {
11057:                 $lastactref->{'domdefaults'} = 1;
11058:             }
11059:         } else {
11060:             $errors .= '<li><span class="LC_error">'.
11061:                        &mt('An error occurred storing the settings: [_1]',
11062:                            $putresult).'</span></li>';
11063:         }
11064:     }
11065:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
11066:         $resulttext = &mt('Changes made:').'<ul>';
11067:         my (%shownprivs,@levelorder);
11068:         @levelorder = ('c','d','s');
11069:         if ((keys(%changes)) && ($putresult eq 'ok')) {
11070:             foreach my $item (sort(keys(%changes))) {
11071:                 if ($item eq 'submitbugs') {
11072:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
11073:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
11074:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
11075:                 } elsif ($item eq 'customrole') {
11076:                     if (ref($changes{'customrole'}) eq 'HASH') {
11077:                         my @keyorder = ('order','desc','access','status','exc','inc');
11078:                         my %keytext = &Apache::lonlocal::texthash(
11079:                                                                    order  => 'Order',
11080:                                                                    desc   => 'Role description',
11081:                                                                    access => 'Role usage',
11082:                                                                    status => 'Allowed instituional types',
11083:                                                                    exc    => 'Allowed personnel',
11084:                                                                    inc    => 'Disallowed personnel',
11085:                         );
11086:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
11087:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
11088:                                 if ($role eq $newrole) {
11089:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
11090:                                                               $role).'<ul>';
11091:                                 } else {
11092:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
11093:                                                               $role).'<ul>';
11094:                                 }
11095:                                 foreach my $key (@keyorder) {
11096:                                     if ($changes{'customrole'}{$role}{$key}) {
11097:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
11098:                                                                   $keytext{$key},$newsettings{$role}{$key}).
11099:                                                        '</li>';
11100:                                     }
11101:                                 }
11102:                                 if (ref($changedprivs{$role}) eq 'HASH') {
11103:                                     $shownprivs{$role} = 1;
11104:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
11105:                                     foreach my $level (@levelorder) {
11106:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
11107:                                             next if ($item eq '');
11108:                                             my ($priv) = split(/\&/,$item,2);
11109:                                             if (&Apache::lonnet::plaintext($priv)) {
11110:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
11111:                                                 unless ($level eq 'c') {
11112:                                                     $resulttext .= ' ('.$lt{$level}.')';
11113:                                                 }
11114:                                                 $resulttext .= '</li>';
11115:                                             }
11116:                                         }
11117:                                     }
11118:                                     $resulttext .= '</ul>';
11119:                                 }
11120:                                 $resulttext .= '</ul></li>';
11121:                             }
11122:                         }
11123:                     }
11124:                 }
11125:             }
11126:         }
11127:         if (keys(%changedprivs)) {
11128:             foreach my $role (sort(keys(%changedprivs))) {
11129:                 unless ($shownprivs{$role}) {
11130:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
11131:                                               $role).'<ul>'.
11132:                                    '<li>'.&mt('Privileges set to :').'<ul>';
11133:                     foreach my $level (@levelorder) {
11134:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
11135:                             next if ($item eq '');
11136:                             my ($priv) = split(/\&/,$item,2);
11137:                             if (&Apache::lonnet::plaintext($priv)) {
11138:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
11139:                                 unless ($level eq 'c') {
11140:                                     $resulttext .= ' ('.$lt{$level}.')';
11141:                                 }
11142:                                 $resulttext .= '</li>';
11143:                             }
11144:                         }
11145:                     }
11146:                     $resulttext .= '</ul></li></ul></li>';
11147:                 }
11148:             }
11149:         }
11150:         $resulttext .= '</ul>';
11151:     } else {
11152:         $resulttext = &mt('No changes made to help settings');
11153:     }
11154:     if ($errors) {
11155:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
11156:                                     $errors.'</ul>';
11157:     }
11158:     return $resulttext;
11159: }
11160: 
11161: sub modify_coursedefaults {
11162:     my ($dom,$lastactref,%domconfig) = @_;
11163:     my ($resulttext,$errors,%changes,%defaultshash);
11164:     my %defaultchecked = (
11165:                            'uselcmath'       => 'on',
11166:                            'usejsme'         => 'on'
11167:                          );
11168:     my @toggles = ('uselcmath','usejsme');
11169:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
11170:                    'uploadquota_community','uploadquota_textbook','mysqltables_official',
11171:                    'mysqltables_unofficial','mysqltables_community','mysqltables_textbook');
11172:     my @types = ('official','unofficial','community','textbook');
11173:     my %staticdefaults = (
11174:                            anonsurvey_threshold => 10,
11175:                            uploadquota          => 500,
11176:                            postsubmit           => 60,
11177:                            mysqltables          => 172800,
11178:                          );
11179: 
11180:     $defaultshash{'coursedefaults'} = {};
11181: 
11182:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
11183:         if ($domconfig{'coursedefaults'} eq '') {
11184:             $domconfig{'coursedefaults'} = {};
11185:         }
11186:     }
11187: 
11188:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
11189:         foreach my $item (@toggles) {
11190:             if ($defaultchecked{$item} eq 'on') {
11191:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
11192:                     ($env{'form.'.$item} eq '0')) {
11193:                     $changes{$item} = 1;
11194:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
11195:                     $changes{$item} = 1;
11196:                 }
11197:             } elsif ($defaultchecked{$item} eq 'off') {
11198:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
11199:                     ($env{'form.'.$item} eq '1')) {
11200:                     $changes{$item} = 1;
11201:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
11202:                     $changes{$item} = 1;
11203:                 }
11204:             }
11205:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
11206:         }
11207:         foreach my $item (@numbers) {
11208:             my ($currdef,$newdef);
11209:             $newdef = $env{'form.'.$item};
11210:             if ($item eq 'anonsurvey_threshold') {
11211:                 $currdef = $domconfig{'coursedefaults'}{$item};
11212:                 $newdef =~ s/\D//g;
11213:                 if ($newdef eq '' || $newdef < 1) {
11214:                     $newdef = 1;
11215:                 }
11216:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
11217:             } else {
11218:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
11219:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
11220:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
11221:                 }
11222:                 $newdef =~ s/[^\w.\-]//g;
11223:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
11224:             }
11225:             if ($currdef ne $newdef) {
11226:                 my $staticdef;
11227:                 if ($item eq 'anonsurvey_threshold') {
11228:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
11229:                         $changes{$item} = 1;
11230:                     }
11231:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
11232:                     my $setting = $1;
11233:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
11234:                         $changes{$setting} = 1;
11235:                     }
11236:                 }
11237:             }
11238:         }
11239:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
11240:         my @currclonecode;
11241:         if (ref($currclone) eq 'HASH') {
11242:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
11243:                 @currclonecode = @{$currclone->{'instcode'}};
11244:             }
11245:         }
11246:         my $newclone;
11247:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
11248:             $newclone = $env{'form.canclone'};
11249:         }
11250:         if ($newclone eq 'instcode') {
11251:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
11252:             my (%codedefaults,@code_order,@clonecode);
11253:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
11254:                                                     \@code_order);
11255:             foreach my $item (@code_order) {
11256:                 if (grep(/^\Q$item\E$/,@newcodes)) {
11257:                     push(@clonecode,$item);
11258:                 }
11259:             }
11260:             if (@clonecode) {
11261:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
11262:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
11263:                 if (@diffs) {
11264:                     $changes{'canclone'} = 1;
11265:                 }
11266:             } else {
11267:                 $newclone eq '';
11268:             }
11269:         } elsif ($newclone ne '') {
11270:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
11271:         }
11272:         if ($newclone ne $currclone) {
11273:             $changes{'canclone'} = 1;
11274:         }
11275:         my %credits;
11276:         foreach my $type (@types) {
11277:             unless ($type eq 'community') {
11278:                 $credits{$type} = $env{'form.'.$type.'_credits'};
11279:                 $credits{$type} =~ s/[^\d.]+//g;
11280:             }
11281:         }
11282:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
11283:             ($env{'form.coursecredits'} eq '1')) {
11284:             $changes{'coursecredits'} = 1;
11285:             foreach my $type (keys(%credits)) {
11286:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
11287:             }
11288:         } else {
11289:             if ($env{'form.coursecredits'} eq '1') {
11290:                 foreach my $type (@types) {
11291:                     unless ($type eq 'community') {
11292:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
11293:                             $changes{'coursecredits'} = 1;
11294:                         }
11295:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
11296:                     }
11297:                 }
11298:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
11299:                 foreach my $type (@types) {
11300:                     unless ($type eq 'community') {
11301:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
11302:                             $changes{'coursecredits'} = 1;
11303:                             last;
11304:                         }
11305:                     }
11306:                 }
11307:             }
11308:         }
11309:         if ($env{'form.postsubmit'} eq '1') {
11310:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
11311:             my %currtimeout;
11312:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
11313:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
11314:                     $changes{'postsubmit'} = 1;
11315:                 }
11316:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
11317:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
11318:                 }
11319:             } else {
11320:                 $changes{'postsubmit'} = 1;
11321:             }
11322:             foreach my $type (@types) {
11323:                 my $timeout = $env{'form.'.$type.'_timeout'};
11324:                 $timeout =~ s/\D//g;
11325:                 if ($timeout == $staticdefaults{'postsubmit'}) {
11326:                     $timeout = '';
11327:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
11328:                     $timeout = '0';
11329:                 }
11330:                 unless ($timeout eq '') {
11331:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
11332:                 }
11333:                 if (exists($currtimeout{$type})) {
11334:                     if ($timeout ne $currtimeout{$type}) {
11335:                         $changes{'postsubmit'} = 1;
11336:                     }
11337:                 } elsif ($timeout ne '') {
11338:                     $changes{'postsubmit'} = 1;
11339:                 }
11340:             }
11341:         } else {
11342:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
11343:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
11344:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
11345:                     $changes{'postsubmit'} = 1;
11346:                 }
11347:             } else {
11348:                 $changes{'postsubmit'} = 1;
11349:             }
11350:         }
11351:     }
11352:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
11353:                                              $dom);
11354:     if ($putresult eq 'ok') {
11355:         if (keys(%changes) > 0) {
11356:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11357:             if (($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
11358:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
11359:                 ($changes{'canclone'}) || ($changes{'mysqltables'})) {
11360:                 foreach my $item ('uselcmath','usejsme') {
11361:                     if ($changes{$item}) {
11362:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
11363:                     }
11364:                 }
11365:                 if ($changes{'coursecredits'}) {
11366:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
11367:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
11368:                             $domdefaults{$type.'credits'} =
11369:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
11370:                         }
11371:                     }
11372:                 }
11373:                 if ($changes{'postsubmit'}) {
11374:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
11375:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
11376:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
11377:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
11378:                                 $domdefaults{$type.'postsubtimeout'} =
11379:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
11380:                             }
11381:                         }
11382:                     }
11383:                 }
11384:                 if ($changes{'uploadquota'}) {
11385:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
11386:                         foreach my $type (@types) {
11387:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
11388:                         }
11389:                     }
11390:                 }
11391:                 if ($changes{'canclone'}) {
11392:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
11393:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
11394:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
11395:                             if (@clonecodes) {
11396:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
11397:                             }
11398:                         }
11399:                     } else {
11400:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
11401:                     }
11402:                 }
11403:                 my $cachetime = 24*60*60;
11404:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
11405:                 if (ref($lastactref) eq 'HASH') {
11406:                     $lastactref->{'domdefaults'} = 1;
11407:                 }
11408:             }
11409:             $resulttext = &mt('Changes made:').'<ul>';
11410:             foreach my $item (sort(keys(%changes))) {
11411:                 if ($item eq 'uselcmath') {
11412:                     if ($env{'form.'.$item} eq '1') {
11413:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
11414:                     } else {
11415:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
11416:                     }
11417:                 } elsif ($item eq 'usejsme') {
11418:                     if ($env{'form.'.$item} eq '1') {
11419:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
11420:                     } else {
11421:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
11422:                     }
11423:                 } elsif ($item eq 'anonsurvey_threshold') {
11424:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
11425:                 } elsif ($item eq 'uploadquota') {
11426:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
11427:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
11428:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
11429:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
11430:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
11431: 
11432:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
11433:                                        '</ul>'.
11434:                                        '</li>';
11435:                     } else {
11436:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
11437:                     }
11438:                 } elsif ($item eq 'mysqltables') {
11439:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
11440:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
11441:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
11442:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
11443:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
11444:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
11445:                                        '</ul>'.
11446:                                        '</li>';
11447:                     } else {
11448:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
11449:                     }
11450:                 } elsif ($item eq 'postsubmit') {
11451:                     if ($domdefaults{'postsubmit'} eq 'off') {
11452:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
11453:                     } else {
11454:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
11455:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
11456:                             $resulttext .= &mt('durations:').'<ul>';
11457:                             foreach my $type (@types) {
11458:                                 $resulttext .= '<li>';
11459:                                 my $timeout;
11460:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
11461:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
11462:                                 }
11463:                                 my $display;
11464:                                 if ($timeout eq '0') {
11465:                                     $display = &mt('unlimited');
11466:                                 } elsif ($timeout eq '') {
11467:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
11468:                                 } else {
11469:                                     $display = &mt('[quant,_1,second]',$timeout);
11470:                                 }
11471:                                 if ($type eq 'community') {
11472:                                     $resulttext .= &mt('Communities');
11473:                                 } elsif ($type eq 'official') {
11474:                                     $resulttext .= &mt('Official courses');
11475:                                 } elsif ($type eq 'unofficial') {
11476:                                     $resulttext .= &mt('Unofficial courses');
11477:                                 } elsif ($type eq 'textbook') {
11478:                                     $resulttext .= &mt('Textbook courses');
11479:                                 }
11480:                                 $resulttext .= ' -- '.$display.'</li>';
11481:                             }
11482:                             $resulttext .= '</ul>';
11483:                         }
11484:                         $resulttext .= '</li>';
11485:                     }
11486:                 } elsif ($item eq 'coursecredits') {
11487:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
11488:                         if (($domdefaults{'officialcredits'} eq '') &&
11489:                             ($domdefaults{'unofficialcredits'} eq '') &&
11490:                             ($domdefaults{'textbookcredits'} eq '')) {
11491:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
11492:                         } else {
11493:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
11494:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
11495:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
11496:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
11497:                                            '</ul>'.
11498:                                            '</li>';
11499:                         }
11500:                     } else {
11501:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
11502:                     }
11503:                 } elsif ($item eq 'canclone') {
11504:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
11505:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
11506:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
11507:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
11508:                         }
11509:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
11510:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
11511:                     } else {
11512:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
11513:                     }
11514:                 }
11515:             }
11516:             $resulttext .= '</ul>';
11517:         } else {
11518:             $resulttext = &mt('No changes made to course defaults');
11519:         }
11520:     } else {
11521:         $resulttext = '<span class="LC_error">'.
11522:             &mt('An error occurred: [_1]',$putresult).'</span>';
11523:     }
11524:     return $resulttext;
11525: }
11526: 
11527: sub modify_selfenrollment {
11528:     my ($dom,$lastactref,%domconfig) = @_;
11529:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
11530:     my @types = ('official','unofficial','community','textbook');
11531:     my %titles = &tool_titles();
11532:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
11533:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
11534:     $ordered{'default'} = ['types','registered','approval','limit'];
11535: 
11536:     my (%roles,%shown,%toplevel);
11537:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
11538: 
11539:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
11540:         if ($domconfig{'selfenrollment'} eq '') {
11541:             $domconfig{'selfenrollment'} = {};
11542:         }
11543:     }
11544:     %toplevel = (
11545:                   admin      => 'Configuration Rights',
11546:                   default    => 'Default settings',
11547:                   validation => 'Validation of self-enrollment requests',
11548:                 );
11549:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
11550: 
11551:     if (ref($ordered{'admin'}) eq 'ARRAY') {
11552:         foreach my $item (@{$ordered{'admin'}}) {
11553:             foreach my $type (@types) {
11554:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
11555:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
11556:                 } else {
11557:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
11558:                 }
11559:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
11560:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
11561:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
11562:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
11563:                             push(@{$changes{'admin'}{$type}},$item);
11564:                         }
11565:                     } else {
11566:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
11567:                             push(@{$changes{'admin'}{$type}},$item);
11568:                         }
11569:                     }
11570:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
11571:                     push(@{$changes{'admin'}{$type}},$item);
11572:                 }
11573:             }
11574:         }
11575:     }
11576: 
11577:     foreach my $item (@{$ordered{'default'}}) {
11578:         foreach my $type (@types) {
11579:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
11580:             if ($item eq 'types') {
11581:                 unless (($value eq 'all') || ($value eq 'dom')) {
11582:                     $value = '';
11583:                 }
11584:             } elsif ($item eq 'registered') {
11585:                 unless ($value eq '1') {
11586:                     $value = 0;
11587:                 }
11588:             } elsif ($item eq 'approval') {
11589:                 unless ($value =~ /^[012]$/) {
11590:                     $value = 0;
11591:                 }
11592:             } else {
11593:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
11594:                     $value = 'none';
11595:                 }
11596:             }
11597:             $selfenrollhash{'default'}{$type}{$item} = $value;
11598:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
11599:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
11600:                     if ($selfenrollhash{'default'}{$type}{$item} ne
11601:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
11602:                          push(@{$changes{'default'}{$type}},$item);
11603:                     }
11604:                 } else {
11605:                     push(@{$changes{'default'}{$type}},$item);
11606:                 }
11607:             } else {
11608:                 push(@{$changes{'default'}{$type}},$item);
11609:             }
11610:             if ($item eq 'limit') {
11611:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
11612:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
11613:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
11614:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
11615:                     }
11616:                 } else {
11617:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
11618:                 }
11619:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
11620:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
11621:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
11622:                          push(@{$changes{'default'}{$type}},'cap');
11623:                     }
11624:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
11625:                     push(@{$changes{'default'}{$type}},'cap');
11626:                 }
11627:             }
11628:         }
11629:     }
11630: 
11631:     foreach my $item (@{$itemsref}) {
11632:         if ($item eq 'fields') {
11633:             my @changed;
11634:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
11635:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
11636:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
11637:             }
11638:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
11639:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
11640:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
11641:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
11642:                 } else {
11643:                     @changed = @{$selfenrollhash{'validation'}{$item}};
11644:                 }
11645:             } else {
11646:                 @changed = @{$selfenrollhash{'validation'}{$item}};
11647:             }
11648:             if (@changed) {
11649:                 if ($selfenrollhash{'validation'}{$item}) { 
11650:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
11651:                 } else {
11652:                     $changes{'validation'}{$item} = &mt('None');
11653:                 }
11654:             }
11655:         } else {
11656:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
11657:             if ($item eq 'markup') {
11658:                if ($env{'form.selfenroll_validation_'.$item}) {
11659:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
11660:                }
11661:             }
11662:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
11663:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
11664:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
11665:                 }
11666:             }
11667:         }
11668:     }
11669: 
11670:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
11671:                                              $dom);
11672:     if ($putresult eq 'ok') {
11673:         if (keys(%changes) > 0) {
11674:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11675:             $resulttext = &mt('Changes made:').'<ul>';
11676:             foreach my $key ('admin','default','validation') {
11677:                 if (ref($changes{$key}) eq 'HASH') {
11678:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
11679:                     if ($key eq 'validation') {
11680:                         foreach my $item (@{$itemsref}) {
11681:                             if (exists($changes{$key}{$item})) {
11682:                                 if ($item eq 'markup') {
11683:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
11684:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
11685:                                 } else {  
11686:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
11687:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
11688:                                 }
11689:                             }
11690:                         }
11691:                     } else {
11692:                         foreach my $type (@types) {
11693:                             if ($type eq 'community') {
11694:                                 $roles{'1'} = &mt('Community personnel');
11695:                             } else {
11696:                                 $roles{'1'} = &mt('Course personnel');
11697:                             }
11698:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
11699:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
11700:                                     if ($key eq 'admin') {
11701:                                         my @mgrdc = ();
11702:                                         if (ref($ordered{$key}) eq 'ARRAY') {
11703:                                             foreach my $item (@{$ordered{'admin'}}) {
11704:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
11705:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
11706:                                                         push(@mgrdc,$item);
11707:                                                     }
11708:                                                 }
11709:                                             }
11710:                                             if (@mgrdc) {
11711:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
11712:                                             } else {
11713:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
11714:                                             }
11715:                                         }
11716:                                     } else {
11717:                                         if (ref($ordered{$key}) eq 'ARRAY') {
11718:                                             foreach my $item (@{$ordered{$key}}) {
11719:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
11720:                                                     $domdefaults{$type.'selfenroll'.$item} =
11721:                                                         $selfenrollhash{$key}{$type}{$item};
11722:                                                 }
11723:                                             }
11724:                                         }
11725:                                     }
11726:                                 }
11727:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
11728:                                 foreach my $item (@{$ordered{$key}}) {
11729:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
11730:                                         $resulttext .= '<li>';
11731:                                         if ($key eq 'admin') {
11732:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
11733:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
11734:                                         } else {
11735:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
11736:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
11737:                                         }
11738:                                         $resulttext .= '</li>';
11739:                                     }
11740:                                 }
11741:                                 $resulttext .= '</ul></li>';
11742:                             }
11743:                         }
11744:                         $resulttext .= '</ul></li>'; 
11745:                     }
11746:                 }
11747:                 if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
11748:                     my $cachetime = 24*60*60;
11749:                     &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
11750:                     if (ref($lastactref) eq 'HASH') {
11751:                         $lastactref->{'domdefaults'} = 1;
11752:                     }
11753:                 }
11754:             }
11755:             $resulttext .= '</ul>';
11756:         } else {
11757:             $resulttext = &mt('No changes made to self-enrollment settings');
11758:         }
11759:     } else {
11760:         $resulttext = '<span class="LC_error">'.
11761:             &mt('An error occurred: [_1]',$putresult).'</span>';
11762:     }
11763:     return $resulttext;
11764: }
11765: 
11766: sub modify_usersessions {
11767:     my ($dom,$lastactref,%domconfig) = @_;
11768:     my @hostingtypes = ('version','excludedomain','includedomain');
11769:     my @offloadtypes = ('primary','default');
11770:     my %types = (
11771:                   remote => \@hostingtypes,
11772:                   hosted => \@hostingtypes,
11773:                   spares => \@offloadtypes,
11774:                 );
11775:     my @prefixes = ('remote','hosted','spares');
11776:     my @lcversions = &Apache::lonnet::all_loncaparevs();
11777:     my (%by_ip,%by_location,@intdoms);
11778:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
11779:     my @locations = sort(keys(%by_location));
11780:     my (%defaultshash,%changes);
11781:     foreach my $prefix (@prefixes) {
11782:         $defaultshash{'usersessions'}{$prefix} = {};
11783:     }
11784:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11785:     my $resulttext;
11786:     my %iphost = &Apache::lonnet::get_iphost();
11787:     foreach my $prefix (@prefixes) {
11788:         next if ($prefix eq 'spares');
11789:         foreach my $type (@{$types{$prefix}}) {
11790:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
11791:             if ($type eq 'version') {
11792:                 my $value = $env{'form.'.$prefix.'_'.$type};
11793:                 my $okvalue;
11794:                 if ($value ne '') {
11795:                     if (grep(/^\Q$value\E$/,@lcversions)) {
11796:                         $okvalue = $value;
11797:                     }
11798:                 }
11799:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
11800:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
11801:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
11802:                             if ($inuse == 0) {
11803:                                 $changes{$prefix}{$type} = 1;
11804:                             } else {
11805:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
11806:                                     $changes{$prefix}{$type} = 1;
11807:                                 }
11808:                                 if ($okvalue ne '') {
11809:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
11810:                                 } 
11811:                             }
11812:                         } else {
11813:                             if (($inuse == 1) && ($okvalue ne '')) {
11814:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
11815:                                 $changes{$prefix}{$type} = 1;
11816:                             }
11817:                         }
11818:                     } else {
11819:                         if (($inuse == 1) && ($okvalue ne '')) {
11820:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
11821:                             $changes{$prefix}{$type} = 1;
11822:                         }
11823:                     }
11824:                 } else {
11825:                     if (($inuse == 1) && ($okvalue ne '')) {
11826:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
11827:                         $changes{$prefix}{$type} = 1;
11828:                     }
11829:                 }
11830:             } else {
11831:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
11832:                 my @okvals;
11833:                 foreach my $val (@vals) {
11834:                     if ($val =~ /:/) {
11835:                         my @items = split(/:/,$val);
11836:                         foreach my $item (@items) {
11837:                             if (ref($by_location{$item}) eq 'ARRAY') {
11838:                                 push(@okvals,$item);
11839:                             }
11840:                         }
11841:                     } else {
11842:                         if (ref($by_location{$val}) eq 'ARRAY') {
11843:                             push(@okvals,$val);
11844:                         }
11845:                     }
11846:                 }
11847:                 @okvals = sort(@okvals);
11848:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
11849:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
11850:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
11851:                             if ($inuse == 0) {
11852:                                 $changes{$prefix}{$type} = 1; 
11853:                             } else {
11854:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
11855:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
11856:                                 if (@changed > 0) {
11857:                                     $changes{$prefix}{$type} = 1;
11858:                                 }
11859:                             }
11860:                         } else {
11861:                             if ($inuse == 1) {
11862:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
11863:                                 $changes{$prefix}{$type} = 1;
11864:                             }
11865:                         } 
11866:                     } else {
11867:                         if ($inuse == 1) {
11868:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
11869:                             $changes{$prefix}{$type} = 1;
11870:                         }
11871:                     }
11872:                 } else {
11873:                     if ($inuse == 1) {
11874:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
11875:                         $changes{$prefix}{$type} = 1;
11876:                     }
11877:                 }
11878:             }
11879:         }
11880:     }
11881: 
11882:     my @alldoms = &Apache::lonnet::all_domains();
11883:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
11884:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
11885:     my $savespares;
11886: 
11887:     foreach my $lonhost (sort(keys(%servers))) {
11888:         my $serverhomeID =
11889:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
11890:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
11891:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
11892:         my %spareschg;
11893:         foreach my $type (@{$types{'spares'}}) {
11894:             my @okspares;
11895:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
11896:             foreach my $server (@checked) {
11897:                 if (&Apache::lonnet::hostname($server) ne '') {
11898:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
11899:                         unless (grep(/^\Q$server\E$/,@okspares)) {
11900:                             push(@okspares,$server);
11901:                         }
11902:                     }
11903:                 }
11904:             }
11905:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
11906:             my $newspare;
11907:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
11908:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
11909:                     $newspare = $new;
11910:                 }
11911:             }
11912:             my @spares;
11913:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
11914:                 @spares = sort(@okspares,$newspare);
11915:             } else {
11916:                 @spares = sort(@okspares);
11917:             }
11918:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
11919:             if (ref($spareid{$lonhost}) eq 'HASH') {
11920:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
11921:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
11922:                     if (@diffs > 0) {
11923:                         $spareschg{$type} = 1;
11924:                     }
11925:                 }
11926:             }
11927:         }
11928:         if (keys(%spareschg) > 0) {
11929:             $changes{'spares'}{$lonhost} = \%spareschg;
11930:         }
11931:     }
11932:     $defaultshash{'usersessions'}{'offloadnow'} = {};
11933:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
11934:     my @okoffload;
11935:     if (@offloadnow) {
11936:         foreach my $server (@offloadnow) {
11937:             if (&Apache::lonnet::hostname($server) ne '') {
11938:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
11939:                     push(@okoffload,$server);
11940:                 }
11941:             }
11942:         }
11943:         if (@okoffload) {
11944:             foreach my $lonhost (@okoffload) {
11945:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
11946:             }
11947:         }
11948:     }
11949:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
11950:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
11951:             if (ref($changes{'spares'}) eq 'HASH') {
11952:                 if (keys(%{$changes{'spares'}}) > 0) {
11953:                     $savespares = 1;
11954:                 }
11955:             }
11956:         } else {
11957:             $savespares = 1;
11958:         }
11959:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
11960:             foreach my $lonhost (keys(%{$domconfig{'usersessions'}{'offloadnow'}})) {
11961:                 unless ($defaultshash{'usersessions'}{'offloadnow'}{$lonhost}) {
11962:                     $changes{'offloadnow'} = 1;
11963:                     last;
11964:                 }
11965:             }
11966:             unless ($changes{'offloadnow'}) {
11967:                 foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{'offloadnow'}})) {
11968:                     unless ($domconfig{'usersessions'}{'offloadnow'}{$lonhost}) {
11969:                         $changes{'offloadnow'} = 1;
11970:                         last;
11971:                     }
11972:                 }
11973:             }
11974:         } elsif (@okoffload) {
11975:             $changes{'offloadnow'} = 1;
11976:         }
11977:     } elsif (@okoffload) {
11978:         $changes{'offloadnow'} = 1;
11979:     }
11980:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
11981:     if ((keys(%changes) > 0) || ($savespares)) {
11982:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
11983:                                                  $dom);
11984:         if ($putresult eq 'ok') {
11985:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
11986:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
11987:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
11988:                 }
11989:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
11990:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
11991:                 }
11992:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
11993:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
11994:                 }
11995:             }
11996:             my $cachetime = 24*60*60;
11997:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
11998:             if (ref($lastactref) eq 'HASH') {
11999:                 $lastactref->{'domdefaults'} = 1;
12000:             }
12001:             if (keys(%changes) > 0) {
12002:                 my %lt = &usersession_titles();
12003:                 $resulttext = &mt('Changes made:').'<ul>';
12004:                 foreach my $prefix (@prefixes) {
12005:                     if (ref($changes{$prefix}) eq 'HASH') {
12006:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
12007:                         if ($prefix eq 'spares') {
12008:                             if (ref($changes{$prefix}) eq 'HASH') {
12009:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
12010:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
12011:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
12012:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
12013:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
12014:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
12015:                                         foreach my $type (@{$types{$prefix}}) {
12016:                                             if ($changes{$prefix}{$lonhost}{$type}) {
12017:                                                 my $offloadto = &mt('None');
12018:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
12019:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
12020:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
12021:                                                     }
12022:                                                 }
12023:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
12024:                                             }
12025:                                         }
12026:                                     }
12027:                                     $resulttext .= '</li>';
12028:                                 }
12029:                             }
12030:                         } else {
12031:                             foreach my $type (@{$types{$prefix}}) {
12032:                                 if (defined($changes{$prefix}{$type})) {
12033:                                     my $newvalue;
12034:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
12035:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
12036:                                             if ($type eq 'version') {
12037:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
12038:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
12039:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
12040:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
12041:                                                 }
12042:                                             }
12043:                                         }
12044:                                     }
12045:                                     if ($newvalue eq '') {
12046:                                         if ($type eq 'version') {
12047:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
12048:                                         } else {
12049:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
12050:                                         }
12051:                                     } else {
12052:                                         if ($type eq 'version') {
12053:                                             $newvalue .= ' '.&mt('(or later)'); 
12054:                                         }
12055:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
12056:                                     }
12057:                                 }
12058:                             }
12059:                         }
12060:                         $resulttext .= '</ul>';
12061:                     }
12062:                 }
12063:                 if ($changes{'offloadnow'}) {
12064:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
12065:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
12066:                             $resulttext .= '<li>'.&mt('Switch active users on next access, for server(s):').'<ul>';
12067:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
12068:                                 $resulttext .= '<li>'.$lonhost.'</li>';
12069:                             }
12070:                             $resulttext .= '</ul>';
12071:                         } else {
12072:                             $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.');
12073:                         }
12074:                     } else {
12075:                         $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.').'</li>';
12076:                     }
12077:                 }
12078:                 $resulttext .= '</ul>';
12079:             } else {
12080:                 $resulttext = $nochgmsg;
12081:             }
12082:         } else {
12083:             $resulttext = '<span class="LC_error">'.
12084:                           &mt('An error occurred: [_1]',$putresult).'</span>';
12085:         }
12086:     } else {
12087:         $resulttext = $nochgmsg;
12088:     }
12089:     return $resulttext;
12090: }
12091: 
12092: sub modify_loadbalancing {
12093:     my ($dom,%domconfig) = @_;
12094:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
12095:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
12096:     my ($othertitle,$usertypes,$types) =
12097:         &Apache::loncommon::sorted_inst_types($dom);
12098:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
12099:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
12100:     my @sparestypes = ('primary','default');
12101:     my %typetitles = &sparestype_titles();
12102:     my $resulttext;
12103:     my (%currbalancer,%currtargets,%currrules,%existing);
12104:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
12105:         %existing = %{$domconfig{'loadbalancing'}};
12106:     }
12107:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
12108:                               \%currtargets,\%currrules);
12109:     my ($saveloadbalancing,%defaultshash,%changes);
12110:     my ($alltypes,$othertypes,$titles) =
12111:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
12112:     my %ruletitles = &offloadtype_text();
12113:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
12114:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
12115:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
12116:         if ($balancer eq '') {
12117:             next;
12118:         }
12119:         if (!exists($servers{$balancer})) {
12120:             if (exists($currbalancer{$balancer})) {
12121:                 push(@{$changes{'delete'}},$balancer);
12122:             }
12123:             next;
12124:         }
12125:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
12126:             push(@{$changes{'delete'}},$balancer);
12127:             next;
12128:         }
12129:         if (!exists($currbalancer{$balancer})) {
12130:             push(@{$changes{'add'}},$balancer);
12131:         }
12132:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
12133:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
12134:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
12135:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
12136:             $saveloadbalancing = 1;
12137:         }
12138:         foreach my $sparetype (@sparestypes) {
12139:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
12140:             my @offloadto;
12141:             foreach my $target (@targets) {
12142:                 if (($servers{$target}) && ($target ne $balancer)) {
12143:                     if ($sparetype eq 'default') {
12144:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
12145:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
12146:                         }
12147:                     }
12148:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
12149:                         push(@offloadto,$target);
12150:                     }
12151:                 }
12152:             }
12153:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
12154:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
12155:                     push(@offloadto,$balancer);
12156:                 }
12157:             }
12158:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
12159:         }
12160:         if (ref($currtargets{$balancer}) eq 'HASH') {
12161:             foreach my $sparetype (@sparestypes) {
12162:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
12163:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
12164:                     if (@targetdiffs > 0) {
12165:                         $changes{'curr'}{$balancer}{'targets'} = 1;
12166:                     }
12167:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
12168:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
12169:                         $changes{'curr'}{$balancer}{'targets'} = 1;
12170:                     }
12171:                 }
12172:             }
12173:         } else {
12174:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
12175:                 foreach my $sparetype (@sparestypes) {
12176:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
12177:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
12178:                             $changes{'curr'}{$balancer}{'targets'} = 1;
12179:                         }
12180:                     }
12181:                 }
12182:             }
12183:         }
12184:         my $ishomedom;
12185:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
12186:             $ishomedom = 1;
12187:         }
12188:         if (ref($alltypes) eq 'ARRAY') {
12189:             foreach my $type (@{$alltypes}) {
12190:                 my $rule;
12191:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
12192:                          (!$ishomedom)) {
12193:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
12194:                 }
12195:                 if ($rule eq 'specific') {
12196:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
12197:                     if (exists($servers{$specifiedhost})) {
12198:                         $rule = $specifiedhost;
12199:                     }
12200:                 }
12201:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
12202:                 if (ref($currrules{$balancer}) eq 'HASH') {
12203:                     if ($rule ne $currrules{$balancer}{$type}) {
12204:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
12205:                     }
12206:                 } elsif ($rule ne '') {
12207:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
12208:                 }
12209:             }
12210:         }
12211:     }
12212:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
12213:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
12214:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
12215:             $defaultshash{'loadbalancing'} = {};
12216:         }
12217:         my $putresult = &Apache::lonnet::put_dom('configuration',
12218:                                                  \%defaultshash,$dom);
12219:         if ($putresult eq 'ok') {
12220:             if (keys(%changes) > 0) {
12221:                 my %toupdate;
12222:                 if (ref($changes{'delete'}) eq 'ARRAY') {
12223:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
12224:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
12225:                         $toupdate{$balancer} = 1;
12226:                     }
12227:                 }
12228:                 if (ref($changes{'add'}) eq 'ARRAY') {
12229:                     foreach my $balancer (sort(@{$changes{'add'}})) {
12230:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
12231:                         $toupdate{$balancer} = 1;
12232:                     }
12233:                 }
12234:                 if (ref($changes{'curr'}) eq 'HASH') {
12235:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
12236:                         $toupdate{$balancer} = 1;
12237:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
12238:                             if ($changes{'curr'}{$balancer}{'targets'}) {
12239:                                 my %offloadstr;
12240:                                 foreach my $sparetype (@sparestypes) {
12241:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
12242:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
12243:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
12244:                                         }
12245:                                     }
12246:                                 }
12247:                                 if (keys(%offloadstr) == 0) {
12248:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
12249:                                 } else {
12250:                                     my $showoffload;
12251:                                     foreach my $sparetype (@sparestypes) {
12252:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
12253:                                         if (defined($offloadstr{$sparetype})) {
12254:                                             $showoffload .= $offloadstr{$sparetype};
12255:                                         } else {
12256:                                             $showoffload .= &mt('None');
12257:                                         }
12258:                                         $showoffload .= ('&nbsp;'x3);
12259:                                     }
12260:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
12261:                                 }
12262:                             }
12263:                         }
12264:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
12265:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
12266:                                 foreach my $type (@{$alltypes}) {
12267:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
12268:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
12269:                                         my $balancetext;
12270:                                         if ($rule eq '') {
12271:                                             $balancetext =  $ruletitles{'default'};
12272:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
12273:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
12274:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
12275:                                                 foreach my $sparetype (@sparestypes) {
12276:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
12277:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
12278:                                                     }
12279:                                                 }
12280:                                                 foreach my $item (@{$alltypes}) {
12281:                                                     next if ($item =~  /^_LC_ipchange/);
12282:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
12283:                                                     if ($hasrule eq 'homeserver') {
12284:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
12285:                                                     } else {
12286:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
12287:                                                             if ($servers{$hasrule}) {
12288:                                                                 $toupdate{$hasrule} = 1;
12289:                                                             }
12290:                                                         }
12291:                                                     }
12292:                                                 }
12293:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
12294:                                                     $balancetext =  $ruletitles{$rule};
12295:                                                 } else {
12296:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
12297:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
12298:                                                     if ($receiver) {
12299:                                                         $toupdate{$receiver};
12300:                                                     }
12301:                                                 }
12302:                                             } else {
12303:                                                 $balancetext =  $ruletitles{$rule};
12304:                                             }
12305:                                         } else {
12306:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
12307:                                         }
12308:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
12309:                                     }
12310:                                 }
12311:                             }
12312:                         }
12313:                         if (keys(%toupdate)) {
12314:                             my %thismachine;
12315:                             my $updatedhere;
12316:                             my $cachetime = 60*60*24;
12317:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
12318:                             foreach my $lonhost (keys(%toupdate)) {
12319:                                 if ($thismachine{$lonhost}) {
12320:                                     unless ($updatedhere) {
12321:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
12322:                                                                       $defaultshash{'loadbalancing'},
12323:                                                                       $cachetime);
12324:                                         $updatedhere = 1;
12325:                                     }
12326:                                 } else {
12327:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
12328:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
12329:                                 }
12330:                             }
12331:                         }
12332:                     }
12333:                 }
12334:                 if ($resulttext ne '') {
12335:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
12336:                 } else {
12337:                     $resulttext = $nochgmsg;
12338:                 }
12339:             } else {
12340:                 $resulttext = $nochgmsg;
12341:             }
12342:         } else {
12343:             $resulttext = '<span class="LC_error">'.
12344:                           &mt('An error occurred: [_1]',$putresult).'</span>';
12345:         }
12346:     } else {
12347:         $resulttext = $nochgmsg;
12348:     }
12349:     return $resulttext;
12350: }
12351: 
12352: sub recurse_check {
12353:     my ($chkcats,$categories,$depth,$name) = @_;
12354:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
12355:         my $chg = 0;
12356:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
12357:             my $category = $chkcats->[$depth]{$name}[$j];
12358:             my $item;
12359:             if ($category eq '') {
12360:                 $chg ++;
12361:             } else {
12362:                 my $deeper = $depth + 1;
12363:                 $item = &escape($category).':'.&escape($name).':'.$depth;
12364:                 if ($chg) {
12365:                     $categories->{$item} -= $chg;
12366:                 }
12367:                 &recurse_check($chkcats,$categories,$deeper,$category);
12368:                 $deeper --;
12369:             }
12370:         }
12371:     }
12372:     return;
12373: }
12374: 
12375: sub recurse_cat_deletes {
12376:     my ($item,$coursecategories,$deletions) = @_;
12377:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
12378:     my $subdepth = $depth + 1;
12379:     if (ref($coursecategories) eq 'HASH') {
12380:         foreach my $subitem (keys(%{$coursecategories})) {
12381:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
12382:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
12383:                 delete($coursecategories->{$subitem});
12384:                 $deletions->{$subitem} = 1;
12385:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
12386:             }
12387:         }
12388:     }
12389:     return;
12390: }
12391: 
12392: sub active_dc_picker {
12393:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
12394:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
12395:     my @domcoord = keys(%domcoords);
12396:     if (keys(%currhash)) {
12397:         foreach my $dc (keys(%currhash)) {
12398:             unless (exists($domcoords{$dc})) {
12399:                 push(@domcoord,$dc);
12400:             }
12401:         }
12402:     }
12403:     @domcoord = sort(@domcoord);
12404:     my $numdcs = scalar(@domcoord);
12405:     my $rows = 0;
12406:     my $table;
12407:     if ($numdcs > 1) {
12408:         $table = '<table>';
12409:         for (my $i=0; $i<@domcoord; $i++) {
12410:             my $rem = $i%($numinrow);
12411:             if ($rem == 0) {
12412:                 if ($i > 0) {
12413:                     $table .= '</tr>';
12414:                 }
12415:                 $table .= '<tr>';
12416:                 $rows ++;
12417:             }
12418:             my $check = '';
12419:             if ($inputtype eq 'radio') {
12420:                 if (keys(%currhash) == 0) {
12421:                     if (!$i) {
12422:                         $check = ' checked="checked"';
12423:                     }
12424:                 } elsif (exists($currhash{$domcoord[$i]})) {
12425:                     $check = ' checked="checked"';
12426:                 }
12427:             } else {
12428:                 if (exists($currhash{$domcoord[$i]})) {
12429:                     $check = ' checked="checked"';
12430:                 }
12431:             }
12432:             if ($i == @domcoord - 1) {
12433:                 my $colsleft = $numinrow - $rem;
12434:                 if ($colsleft > 1) {
12435:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
12436:                 } else {
12437:                     $table .= '<td class="LC_left_item">';
12438:                 }
12439:             } else {
12440:                 $table .= '<td class="LC_left_item">';
12441:             }
12442:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
12443:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
12444:             $table .= '<span class="LC_nobreak"><label>'.
12445:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
12446:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
12447:             if ($user ne $dcname.':'.$dcdom) {
12448:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
12449:             }
12450:             $table .= '</label></span></td>';
12451:         }
12452:         $table .= '</tr></table>';
12453:     } elsif ($numdcs == 1) {
12454:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
12455:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
12456:         if ($inputtype eq 'radio') {
12457:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
12458:             if ($user ne $dcname.':'.$dcdom) {
12459:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
12460:             }
12461:         } else {
12462:             my $check;
12463:             if (exists($currhash{$domcoord[0]})) {
12464:                 $check = ' checked="checked"';
12465:             }
12466:             $table = '<span class="LC_nobreak"><label>'.
12467:                      '<input type="checkbox" name="'.$name.'" '.
12468:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
12469:             if ($user ne $dcname.':'.$dcdom) {
12470:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
12471:             }
12472:             $table .= '</label></span>';
12473:             $rows ++;
12474:         }
12475:     }
12476:     return ($numdcs,$table,$rows);
12477: }
12478: 
12479: sub usersession_titles {
12480:     return &Apache::lonlocal::texthash(
12481:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
12482:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
12483:                spares => 'Servers offloaded to, when busy',
12484:                version => 'LON-CAPA version requirement',
12485:                excludedomain => 'Allow all, but exclude specific domains',
12486:                includedomain => 'Deny all, but include specific domains',
12487:                primary => 'Primary (checked first)',
12488:                default => 'Default',
12489:            );
12490: }
12491: 
12492: sub id_for_thisdom {
12493:     my (%servers) = @_;
12494:     my %altids;
12495:     foreach my $server (keys(%servers)) {
12496:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
12497:         if ($serverhome ne $server) {
12498:             $altids{$serverhome} = $server;
12499:         }
12500:     }
12501:     return %altids;
12502: }
12503: 
12504: sub count_servers {
12505:     my ($currbalancer,%servers) = @_;
12506:     my (@spares,$numspares);
12507:     foreach my $lonhost (sort(keys(%servers))) {
12508:         next if ($currbalancer eq $lonhost);
12509:         push(@spares,$lonhost);
12510:     }
12511:     if ($currbalancer) {
12512:         $numspares = scalar(@spares);
12513:     } else {
12514:         $numspares = scalar(@spares) - 1;
12515:     }
12516:     return ($numspares,@spares);
12517: }
12518: 
12519: sub lonbalance_targets_js {
12520:     my ($dom,$types,$servers,$settings) = @_;
12521:     my $select = &mt('Select');
12522:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
12523:     if (ref($servers) eq 'HASH') {
12524:         $alltargets = join("','",sort(keys(%{$servers})));
12525:         my @homedoms;
12526:         foreach my $server (sort(keys(%{$servers}))) {
12527:             if (&Apache::lonnet::host_domain($server) eq $dom) {
12528:                 push(@homedoms,'1');
12529:             } else {
12530:                 push(@homedoms,'0');
12531:             }
12532:         }
12533:         $allishome = join("','",@homedoms);
12534:     }
12535:     if (ref($types) eq 'ARRAY') {
12536:         if (@{$types} > 0) {
12537:             @alltypes = @{$types};
12538:         }
12539:     }
12540:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
12541:     $allinsttypes = join("','",@alltypes);
12542:     my (%currbalancer,%currtargets,%currrules,%existing);
12543:     if (ref($settings) eq 'HASH') {
12544:         %existing = %{$settings};
12545:     }
12546:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
12547:                               \%currtargets,\%currrules);
12548:     my $balancers = join("','",sort(keys(%currbalancer)));
12549:     return <<"END";
12550: 
12551: <script type="text/javascript">
12552: // <![CDATA[
12553: 
12554: currBalancers = new Array('$balancers');
12555: 
12556: function toggleTargets(balnum) {
12557:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
12558:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
12559:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
12560:     var prevbalancer = prevhostitem.value;
12561:     var baltotal = document.getElementById('loadbalancing_total').value;
12562:     prevhostitem.value = balancer;
12563:     if (prevbalancer != '') {
12564:         var prevIdx = currBalancers.indexOf(prevbalancer);
12565:         if (prevIdx != -1) {
12566:             currBalancers.splice(prevIdx,1);
12567:         }
12568:     }
12569:     if (balancer == '') {
12570:         hideSpares(balnum);
12571:     } else {
12572:         var currIdx = currBalancers.indexOf(balancer);
12573:         if (currIdx == -1) {
12574:             currBalancers.push(balancer);
12575:         }
12576:         var homedoms = new Array('$allishome');
12577:         var ishomedom = homedoms[lonhostitem.selectedIndex];
12578:         showSpares(balancer,ishomedom,balnum);
12579:     }
12580:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
12581:     return;
12582: }
12583: 
12584: function showSpares(balancer,ishomedom,balnum) {
12585:     var alltargets = new Array('$alltargets');
12586:     var insttypes = new Array('$allinsttypes');
12587:     var offloadtypes = new Array('primary','default');
12588: 
12589:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
12590:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
12591:  
12592:     for (var i=0; i<offloadtypes.length; i++) {
12593:         var count = 0;
12594:         for (var j=0; j<alltargets.length; j++) {
12595:             if (alltargets[j] != balancer) {
12596:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
12597:                 item.value = alltargets[j];
12598:                 item.style.textAlign='left';
12599:                 item.style.textFace='normal';
12600:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
12601:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
12602:                     item.disabled = '';
12603:                 } else {
12604:                     item.disabled = 'disabled';
12605:                     item.checked = false;
12606:                 }
12607:                 count ++;
12608:             }
12609:         }
12610:     }
12611:     for (var k=0; k<insttypes.length; k++) {
12612:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
12613:             if (ishomedom == 1) {
12614:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
12615:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
12616:             } else {
12617:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
12618:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
12619:             }
12620:         } else {
12621:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
12622:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
12623:         }
12624:         if ((insttypes[k] != '_LC_external') && 
12625:             ((insttypes[k] != '_LC_internetdom') ||
12626:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
12627:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
12628:             item.options.length = 0;
12629:             item.options[0] = new Option("","",true,true);
12630:             var idx = 0;
12631:             for (var m=0; m<alltargets.length; m++) {
12632:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
12633:                     idx ++;
12634:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
12635:                 }
12636:             }
12637:         }
12638:     }
12639:     return;
12640: }
12641: 
12642: function hideSpares(balnum) {
12643:     var alltargets = new Array('$alltargets');
12644:     var insttypes = new Array('$allinsttypes');
12645:     var offloadtypes = new Array('primary','default');
12646: 
12647:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
12648:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
12649: 
12650:     var total = alltargets.length - 1;
12651:     for (var i=0; i<offloadtypes; i++) {
12652:         for (var j=0; j<total; j++) {
12653:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
12654:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
12655:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
12656:         }
12657:     }
12658:     for (var k=0; k<insttypes.length; k++) {
12659:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
12660:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
12661:         if (insttypes[k] != '_LC_external') {
12662:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
12663:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
12664:         }
12665:     }
12666:     return;
12667: }
12668: 
12669: function checkOffloads(item,balnum,type) {
12670:     var alltargets = new Array('$alltargets');
12671:     var offloadtypes = new Array('primary','default');
12672:     if (item.checked) {
12673:         var total = alltargets.length - 1;
12674:         var other;
12675:         if (type == offloadtypes[0]) {
12676:             other = offloadtypes[1];
12677:         } else {
12678:             other = offloadtypes[0];
12679:         }
12680:         for (var i=0; i<total; i++) {
12681:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
12682:             if (server == item.value) {
12683:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
12684:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
12685:                 }
12686:             }
12687:         }
12688:     }
12689:     return;
12690: }
12691: 
12692: function singleServerToggle(balnum,type) {
12693:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
12694:     if (offloadtoSelIdx == 0) {
12695:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
12696:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
12697: 
12698:     } else {
12699:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
12700:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
12701:     }
12702:     return;
12703: }
12704: 
12705: function balanceruleChange(formname,balnum,type) {
12706:     if (type == '_LC_external') {
12707:         return;
12708:     }
12709:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
12710:     for (var i=0; i<typesRules.length; i++) {
12711:         if (formname.elements[typesRules[i]].checked) {
12712:             if (formname.elements[typesRules[i]].value != 'specific') {
12713:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
12714:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
12715:             } else {
12716:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
12717:             }
12718:         }
12719:     }
12720:     return;
12721: }
12722: 
12723: function balancerDeleteChange(balnum) {
12724:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
12725:     var baltotal = document.getElementById('loadbalancing_total').value;
12726:     var addtarget;
12727:     var removetarget;
12728:     var action = 'delete';
12729:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
12730:         var lonhost = hostitem.value;
12731:         var currIdx = currBalancers.indexOf(lonhost);
12732:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
12733:             if (currIdx != -1) {
12734:                 currBalancers.splice(currIdx,1);
12735:             }
12736:             addtarget = lonhost;
12737:         } else {
12738:             if (currIdx == -1) {
12739:                 currBalancers.push(lonhost);
12740:             }
12741:             removetarget = lonhost;
12742:             action = 'undelete';
12743:         }
12744:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
12745:     }
12746:     return;
12747: }
12748: 
12749: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
12750:     if (baltotal > 1) {
12751:         var offloadtypes = new Array('primary','default');
12752:         var alltargets = new Array('$alltargets');
12753:         var insttypes = new Array('$allinsttypes');
12754:         for (var i=0; i<baltotal; i++) {
12755:             if (i != balnum) {
12756:                 for (var j=0; j<offloadtypes.length; j++) {
12757:                     var total = alltargets.length - 1;
12758:                     for (var k=0; k<total; k++) {
12759:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
12760:                         var server = serveritem.value;
12761:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
12762:                             if (server == addtarget) {
12763:                                 serveritem.disabled = '';
12764:                             }
12765:                         }
12766:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
12767:                             if (server == removetarget) {
12768:                                 serveritem.disabled = 'disabled';
12769:                                 serveritem.checked = false;
12770:                             }
12771:                         }
12772:                     }
12773:                 }
12774:                 for (var j=0; j<insttypes.length; j++) {
12775:                     if (insttypes[j] != '_LC_external') {
12776:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
12777:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
12778:                             var currSel = singleserver.selectedIndex;
12779:                             var currVal = singleserver.options[currSel].value;
12780:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
12781:                                 var numoptions = singleserver.options.length;
12782:                                 var needsnew = 1;
12783:                                 for (var k=0; k<numoptions; k++) {
12784:                                     if (singleserver.options[k] == addtarget) {
12785:                                         needsnew = 0;
12786:                                         break;
12787:                                     }
12788:                                 }
12789:                                 if (needsnew == 1) {
12790:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
12791:                                 }
12792:                             }
12793:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
12794:                                 singleserver.options.length = 0;
12795:                                 if ((currVal) && (currVal != removetarget)) {
12796:                                     singleserver.options[0] = new Option("","",false,false);
12797:                                 } else {
12798:                                     singleserver.options[0] = new Option("","",true,true);
12799:                                 }
12800:                                 var idx = 0;
12801:                                 for (var m=0; m<alltargets.length; m++) {
12802:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
12803:                                         idx ++;
12804:                                         if (currVal == alltargets[m]) {
12805:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
12806:                                         } else {
12807:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
12808:                                         }
12809:                                     }
12810:                                 }
12811:                             }
12812:                         }
12813:                     }
12814:                 }
12815:             }
12816:         }
12817:     }
12818:     return;
12819: }
12820: 
12821: // ]]>
12822: </script>
12823: 
12824: END
12825: }
12826: 
12827: sub new_spares_js {
12828:     my @sparestypes = ('primary','default');
12829:     my $types = join("','",@sparestypes);
12830:     my $select = &mt('Select');
12831:     return <<"END";
12832: 
12833: <script type="text/javascript">
12834: // <![CDATA[
12835: 
12836: function updateNewSpares(formname,lonhost) {
12837:     var types = new Array('$types');
12838:     var include = new Array();
12839:     var exclude = new Array();
12840:     for (var i=0; i<types.length; i++) {
12841:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
12842:         for (var j=0; j<spareboxes.length; j++) {
12843:             if (formname.elements[spareboxes[j]].checked) {
12844:                 exclude.push(formname.elements[spareboxes[j]].value);
12845:             } else {
12846:                 include.push(formname.elements[spareboxes[j]].value);
12847:             }
12848:         }
12849:     }
12850:     for (var i=0; i<types.length; i++) {
12851:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
12852:         var selIdx = newSpare.selectedIndex;
12853:         var currnew = newSpare.options[selIdx].value;
12854:         var okSpares = new Array();
12855:         for (var j=0; j<newSpare.options.length; j++) {
12856:             var possible = newSpare.options[j].value;
12857:             if (possible != '') {
12858:                 if (exclude.indexOf(possible) == -1) {
12859:                     okSpares.push(possible);
12860:                 } else {
12861:                     if (currnew == possible) {
12862:                         selIdx = 0;
12863:                     }
12864:                 }
12865:             }
12866:         }
12867:         for (var k=0; k<include.length; k++) {
12868:             if (okSpares.indexOf(include[k]) == -1) {
12869:                 okSpares.push(include[k]);
12870:             }
12871:         }
12872:         okSpares.sort();
12873:         newSpare.options.length = 0;
12874:         if (selIdx == 0) {
12875:             newSpare.options[0] = new Option("$select","",true,true);
12876:         } else {
12877:             newSpare.options[0] = new Option("$select","",false,false);
12878:         }
12879:         for (var m=0; m<okSpares.length; m++) {
12880:             var idx = m+1;
12881:             var selThis = 0;
12882:             if (selIdx != 0) {
12883:                 if (okSpares[m] == currnew) {
12884:                     selThis = 1;
12885:                 }
12886:             }
12887:             if (selThis == 1) {
12888:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
12889:             } else {
12890:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
12891:             }
12892:         }
12893:     }
12894:     return;
12895: }
12896: 
12897: function checkNewSpares(lonhost,type) {
12898:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
12899:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
12900:     if (chosen != '') { 
12901:         var othertype;
12902:         var othernewSpare;
12903:         if (type == 'primary') {
12904:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
12905:         }
12906:         if (type == 'default') {
12907:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
12908:         }
12909:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
12910:             othernewSpare.selectedIndex = 0;
12911:         }
12912:     }
12913:     return;
12914: }
12915: 
12916: // ]]>
12917: </script>
12918: 
12919: END
12920: 
12921: }
12922: 
12923: sub common_domprefs_js {
12924:     return <<"END";
12925: 
12926: <script type="text/javascript">
12927: // <![CDATA[
12928: 
12929: function getIndicesByName(formname,item) {
12930:     var group = new Array();
12931:     for (var i=0;i<formname.elements.length;i++) {
12932:         if (formname.elements[i].name == item) {
12933:             group.push(formname.elements[i].id);
12934:         }
12935:     }
12936:     return group;
12937: }
12938: 
12939: // ]]>
12940: </script>
12941: 
12942: END
12943: 
12944: }
12945: 
12946: sub recaptcha_js {
12947:     my %lt = &captcha_phrases();
12948:     return <<"END";
12949: 
12950: <script type="text/javascript">
12951: // <![CDATA[
12952: 
12953: function updateCaptcha(caller,context) {
12954:     var privitem;
12955:     var pubitem;
12956:     var privtext;
12957:     var pubtext;
12958:     var versionitem;
12959:     var versiontext;
12960:     if (document.getElementById(context+'_recaptchapub')) {
12961:         pubitem = document.getElementById(context+'_recaptchapub');
12962:     } else {
12963:         return;
12964:     }
12965:     if (document.getElementById(context+'_recaptchapriv')) {
12966:         privitem = document.getElementById(context+'_recaptchapriv');
12967:     } else {
12968:         return;
12969:     }
12970:     if (document.getElementById(context+'_recaptchapubtxt')) {
12971:         pubtext = document.getElementById(context+'_recaptchapubtxt');
12972:     } else {
12973:         return;
12974:     }
12975:     if (document.getElementById(context+'_recaptchaprivtxt')) {
12976:         privtext = document.getElementById(context+'_recaptchaprivtxt');
12977:     } else {
12978:         return;
12979:     }
12980:     if (document.getElementById(context+'_recaptchaversion')) {
12981:         versionitem = document.getElementById(context+'_recaptchaversion');
12982:     } else {
12983:         return;
12984:     }
12985:     if (document.getElementById(context+'_recaptchavertxt')) {
12986:         versiontext = document.getElementById(context+'_recaptchavertxt');
12987:     } else {
12988:         return;
12989:     }
12990:     if (caller.checked) {
12991:         if (caller.value == 'recaptcha') {
12992:             pubitem.type = 'text';
12993:             privitem.type = 'text';
12994:             pubitem.size = '40';
12995:             privitem.size = '40';
12996:             pubtext.innerHTML = "$lt{'pub'}";
12997:             privtext.innerHTML = "$lt{'priv'}";
12998:             versionitem.type = 'text';
12999:             versionitem.size = '3';
13000:             versiontext.innerHTML = "$lt{'ver'}";
13001:         } else {
13002:             pubitem.type = 'hidden';
13003:             privitem.type = 'hidden';
13004:             versionitem.type = 'hidden';
13005:             pubtext.innerHTML = '';
13006:             privtext.innerHTML = '';
13007:             versiontext.innerHTML = '';
13008:         }
13009:     }
13010:     return;
13011: }
13012: 
13013: // ]]>
13014: </script>
13015: 
13016: END
13017: 
13018: }
13019: 
13020: sub toggle_display_js {
13021:     return <<"END";
13022: 
13023: <script type="text/javascript">
13024: // <![CDATA[
13025: 
13026: function toggleDisplay(domForm,caller) {
13027:     if (document.getElementById(caller)) {
13028:         var divitem = document.getElementById(caller);
13029:         var optionsElement = domForm.coursecredits;
13030:         var checkval = 1;
13031:         var dispval = 'block';
13032:         if (caller == 'emailoptions') {
13033:             optionsElement = domForm.cancreate_email; 
13034:         }
13035:         if (caller == 'studentsubmission') {
13036:             optionsElement = domForm.postsubmit;
13037:         }
13038:         if (caller == 'cloneinstcode') {
13039:             optionsElement = domForm.canclone;
13040:             checkval = 'instcode';
13041:         }
13042:         if (optionsElement.length) {
13043:             var currval;
13044:             for (var i=0; i<optionsElement.length; i++) {
13045:                 if (optionsElement[i].checked) {
13046:                    currval = optionsElement[i].value;
13047:                 }
13048:             }
13049:             if (currval == checkval) {
13050:                 divitem.style.display = dispval;
13051:             } else {
13052:                 divitem.style.display = 'none';
13053:             }
13054:         }
13055:     }
13056:     return;
13057: }
13058: 
13059: // ]]>
13060: </script>
13061: 
13062: END
13063: 
13064: }
13065: 
13066: sub captcha_phrases {
13067:     return &Apache::lonlocal::texthash (
13068:                  priv => 'Private key',
13069:                  pub  => 'Public key',
13070:                  original  => 'original (CAPTCHA)',
13071:                  recaptcha => 'successor (ReCAPTCHA)',
13072:                  notused   => 'unused',
13073:                  ver => 'ReCAPTCHA version (1 or 2)',
13074:     );
13075: }
13076: 
13077: sub devalidate_remote_domconfs {
13078:     my ($dom,$cachekeys) = @_;
13079:     return unless (ref($cachekeys) eq 'HASH');
13080:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
13081:     my %thismachine;
13082:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
13083:     my @posscached = ('domainconfig','domdefaults');
13084:     if (keys(%servers)) {
13085:         foreach my $server (keys(%servers)) {
13086:             next if ($thismachine{$server});
13087:             my @cached;
13088:             foreach my $name (@posscached) {
13089:                 if ($cachekeys->{$name}) {
13090:                     push(@cached,&escape($name).':'.&escape($dom));
13091:                 }
13092:             }
13093:             if (@cached) {
13094:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
13095:             }
13096:         }
13097:     }
13098:     return;
13099: }
13100: 
13101: 1;

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