File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.80: download - view: text, annotated - select for diffs
Mon Mar 13 21:20:18 2017 UTC (7 years, 2 months ago) by raeburn
Branches: version_2_11_X
Diff to branchpoint 1.160: preferred, unified
- For 2.11
  - Backport 1.294

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.160.6.80 2017/03/13 21:20:18 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: #
   29: ###############################################################
   30: ##############################################################
   31: 
   32: =pod
   33: 
   34: =head1 NAME
   35: 
   36: Apache::domainprefs.pm
   37: 
   38: =head1 SYNOPSIS
   39: 
   40: Handles configuration of a LON-CAPA domain.  
   41: 
   42: This is part of the LearningOnline Network with CAPA project
   43: described at http://www.lon-capa.org.
   44: 
   45: 
   46: =head1 OVERVIEW
   47: 
   48: Each institution using LON-CAPA will typically have a single domain designated 
   49: for use by individuals affiliated with the institution.  Accordingly, each domain
   50: may define a default set of logos and a color scheme which can be used to "brand"
   51: the LON-CAPA instance. In addition, an institution will typically have a language
   52: and timezone which are used for the majority of courses.
   53: 
   54: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   55: host of other domain-wide settings which determine the types of functionality
   56: available to users and courses in the domain.
   57: 
   58: There is also a mechanism to configure cataloging of courses in the domain, and
   59: controls on the operation of automated processes which govern such things as
   60: roster updates, user directory updates and processing of course requests.
   61: 
   62: The domain coordination manual which is built dynamically on install/update of 
   63: LON-CAPA from the relevant help items provides more information about domain 
   64: configuration.
   65: 
   66: Most of the domain settings are stored in the configuration.db GDBM file which is
   67: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   68: where $dom is the domain.  The configuration.db stores settings in a number of 
   69: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   70: the domain as files (e.g., image files for logos etc., or plain text files for
   71: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   72: session hosted on the primary library server in the domain, as these files are 
   73: stored in author space belonging to a special $dom-domainconfig user.   
   74: 
   75: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   76: the current settings, and provides an interface to make modifications.
   77: 
   78: =head1 SUBROUTINES
   79: 
   80: =over
   81: 
   82: =item print_quotas()
   83: 
   84: Inputs: 4 
   85: 
   86: $dom,$settings,$rowtotal,$action.
   87: 
   88: $dom is the domain, $settings is a reference to a hash of current settings for
   89: the current context, $rowtotal is a reference to the scalar used to record the 
   90: number of rows displayed on the page, and $action is the context (quotas, 
   91: requestcourses or requestauthor).
   92: 
   93: The print_quotas routine was orginally created to display/store information
   94: about default quota sizes for portfolio spaces for the different types of 
   95: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   96: but is now also used to manage availability of user tools: 
   97: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   98: used by course owners to request creation of a course, and to display/store
   99: default quota sizes for Authoring Spaces.
  100: 
  101: Outputs: 1
  102: 
  103: $datatable  - HTML containing form elements which allow settings to be changed. 
  104: 
  105: In the case of course requests, radio buttons are displayed for each institutional
  106: affiliate type (and also default, and _LC_adv) for each of the course types 
  107: (official, unofficial, community, and textbook).  In each case the radio buttons 
  108: allow the selection of one of four values:
  109: 
  110: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  111: which have the following effects:
  112: 
  113: 0
  114: 
  115: =over
  116: 
  117: - course requests are not allowed for this course types/affiliation
  118: 
  119: =back
  120: 
  121: approval 
  122: 
  123: =over 
  124: 
  125: - course requests must be approved by a Doman Coordinator in the 
  126: course's domain
  127: 
  128: =back
  129: 
  130: validate 
  131: 
  132: =over
  133: 
  134: - an institutional validation (e.g., check requestor is instructor
  135: of record) needs to be passed before the course will be created.  The required
  136: validation is in localenroll.pm on the primary library server for the course 
  137: domain.
  138: 
  139: =back
  140: 
  141: autolimit 
  142: 
  143: =over
  144:  
  145: - course requests will be processed automatically up to a limit of
  146: N requests for the course type for the particular requestor.
  147: If N is undefined, there is no limit to the number of course requests
  148: which a course owner may submit and have processed automatically. 
  149: 
  150: =back
  151: 
  152: =item modify_quotas() 
  153: 
  154: =back
  155: 
  156: =cut
  157: 
  158: package Apache::domainprefs;
  159: 
  160: use strict;
  161: use Apache::Constants qw(:common :http);
  162: use Apache::lonnet;
  163: use Apache::loncommon();
  164: use Apache::lonhtmlcommon();
  165: use Apache::lonlocal;
  166: use Apache::lonmsg();
  167: use Apache::lonconfigsettings;
  168: use Apache::lonuserutils();
  169: use Apache::loncoursequeueadmin();
  170: use LONCAPA qw(:DEFAULT :match);
  171: use LONCAPA::Enrollment;
  172: use LONCAPA::lonauthcgi();
  173: use File::Copy;
  174: use Locale::Language;
  175: use DateTime::TimeZone;
  176: use DateTime::Locale;
  177: 
  178: my $registered_cleanup;
  179: my $modified_urls;
  180: 
  181: sub handler {
  182:     my $r=shift;
  183:     if ($r->header_only) {
  184:         &Apache::loncommon::content_type($r,'text/html');
  185:         $r->send_http_header;
  186:         return OK;
  187:     }
  188: 
  189:     my $context = 'domain';
  190:     my $dom = $env{'request.role.domain'};
  191:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  192:     if (&Apache::lonnet::allowed('mau',$dom)) {
  193:         &Apache::loncommon::content_type($r,'text/html');
  194:         $r->send_http_header;
  195:     } else {
  196:         $env{'user.error.msg'}=
  197:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  198:         return HTTP_NOT_ACCEPTABLE;
  199:     }
  200: 
  201:     $registered_cleanup=0;
  202:     @{$modified_urls}=();
  203: 
  204:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  205:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  206:                                             ['phase','actions']);
  207:     my $phase = 'pickactions';
  208:     if ( exists($env{'form.phase'}) ) {
  209:         $phase = $env{'form.phase'};
  210:     }
  211:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  212:     my %domconfig =
  213:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  214:                 'quotas','autoenroll','autoupdate','autocreate',
  215:                 'directorysrch','usercreation','usermodification',
  216:                 'contacts','defaults','scantron','coursecategories',
  217:                 'serverstatuses','requestcourses','helpsettings',
  218:                 'coursedefaults','usersessions','loadbalancing',
  219:                 'requestauthor','selfenrollment','inststatus'],$dom);
  220:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  221:                        'autoupdate','autocreate','directorysrch','contacts',
  222:                        'usercreation','selfcreation','usermodification','scantron',
  223:                        'requestcourses','requestauthor','coursecategories',
  224:                        'serverstatuses','helpsettings','coursedefaults',
  225:                        'selfenrollment','usersessions');
  226:     my %existing;
  227:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  228:         %existing = %{$domconfig{'loadbalancing'}};
  229:     }
  230:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  231:         push(@prefs_order,'loadbalancing');
  232:     }
  233:     my %prefs = (
  234:         'rolecolors' =>
  235:                    { text => 'Default color schemes',
  236:                      help => 'Domain_Configuration_Color_Schemes',
  237:                      header => [{col1 => 'Student Settings',
  238:                                  col2 => '',},
  239:                                 {col1 => 'Coordinator Settings',
  240:                                  col2 => '',},
  241:                                 {col1 => 'Author Settings',
  242:                                  col2 => '',},
  243:                                 {col1 => 'Administrator Settings',
  244:                                  col2 => '',}],
  245:                       print => \&print_rolecolors,
  246:                       modify => \&modify_rolecolors,
  247:                     },
  248:         'login' =>
  249:                     { text => 'Log-in page options',
  250:                       help => 'Domain_Configuration_Login_Page',
  251:                       header => [{col1 => 'Log-in Page Items',
  252:                                   col2 => '',},
  253:                                  {col1 => 'Log-in Help',
  254:                                   col2 => 'Value'},
  255:                                  {col1 => 'Custom HTML in document head',
  256:                                   col2 => 'Value'}],
  257:                       print => \&print_login,
  258:                       modify => \&modify_login,
  259:                     },
  260:         'defaults' => 
  261:                     { text => 'Default authentication/language/timezone/portal/types',
  262:                       help => 'Domain_Configuration_LangTZAuth',
  263:                       header => [{col1 => 'Setting',
  264:                                   col2 => 'Value'},
  265:                                  {col1 => 'Internal Authentication',
  266:                                   col2 => 'Value'},
  267:                                  {col1 => 'Institutional user types',
  268:                                   col2 => 'Assignable to e-mail usernames'}],
  269:                       print => \&print_defaults,
  270:                       modify => \&modify_defaults,
  271:                     },
  272:         'quotas' => 
  273:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  274:                       help => 'Domain_Configuration_Quotas',
  275:                       header => [{col1 => 'User affiliation',
  276:                                   col2 => 'Available tools',
  277:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  278:                       print => \&print_quotas,
  279:                       modify => \&modify_quotas,
  280:                     },
  281:         'autoenroll' =>
  282:                    { text => 'Auto-enrollment settings',
  283:                      help => 'Domain_Configuration_Auto_Enrollment',
  284:                      header => [{col1 => 'Configuration setting',
  285:                                  col2 => 'Value(s)'}],
  286:                      print => \&print_autoenroll,
  287:                      modify => \&modify_autoenroll,
  288:                    },
  289:         'autoupdate' => 
  290:                    { text => 'Auto-update settings',
  291:                      help => 'Domain_Configuration_Auto_Updates',
  292:                      header => [{col1 => 'Setting',
  293:                                  col2 => 'Value',},
  294:                                 {col1 => 'Setting',
  295:                                  col2 => 'Affiliation'},
  296:                                 {col1 => 'User population',
  297:                                  col2 => 'Updatable user data'}],
  298:                      print => \&print_autoupdate,
  299:                      modify => \&modify_autoupdate,
  300:                   },
  301:         'autocreate' => 
  302:                   { text => 'Auto-course creation settings',
  303:                      help => 'Domain_Configuration_Auto_Creation',
  304:                      header => [{col1 => 'Configuration Setting',
  305:                                  col2 => 'Value',}],
  306:                      print => \&print_autocreate,
  307:                      modify => \&modify_autocreate,
  308:                   },
  309:         'directorysrch' => 
  310:                   { text => 'Directory searches',
  311:                     help => 'Domain_Configuration_InstDirectory_Search',
  312:                     header => [{col1 => 'Institutional Directory Setting',
  313:                                 col2 => 'Value',},
  314:                                {col1 => 'LON-CAPA Directory Setting',
  315:                                 col2 => 'Value',}],
  316:                     print => \&print_directorysrch,
  317:                     modify => \&modify_directorysrch,
  318:                   },
  319:         'contacts' =>
  320:                   { text => 'E-mail addresses and helpform',
  321:                     help => 'Domain_Configuration_Contact_Info',
  322:                     header => [{col1 => 'Default e-mail addresses',
  323:                                 col2 => 'Value',},
  324:                                {col1 => 'Recipient(s) for notifications',
  325:                                 col2 => 'Value',},
  326:                                {col1 => 'Ask helpdesk form settings',
  327:                                 col2 => 'Value',},],
  328:                     print => \&print_contacts,
  329:                     modify => \&modify_contacts,
  330:                   },
  331:         'usercreation' => 
  332:                   { text => 'User creation',
  333:                     help => 'Domain_Configuration_User_Creation',
  334:                     header => [{col1 => 'Format rule type',
  335:                                 col2 => 'Format rules in force'},
  336:                                {col1 => 'User account creation',
  337:                                 col2 => 'Usernames which may be created',},
  338:                                {col1 => 'Context',
  339:                                 col2 => 'Assignable authentication types'}],
  340:                     print => \&print_usercreation,
  341:                     modify => \&modify_usercreation,
  342:                   },
  343:         'selfcreation' => 
  344:                   { text => 'Users self-creating accounts',
  345:                     help => 'Domain_Configuration_Self_Creation', 
  346:                     header => [{col1 => 'Self-creation with institutional username',
  347:                                 col2 => 'Enabled?'},
  348:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  349:                                 col2 => 'Information user can enter'},
  350:                                {col1 => 'Self-creation with e-mail as username',
  351:                                 col2 => 'Settings'}],
  352:                     print => \&print_selfcreation,
  353:                     modify => \&modify_selfcreation,
  354:                   },
  355:         'usermodification' =>
  356:                   { text => 'User modification',
  357:                     help => 'Domain_Configuration_User_Modification',
  358:                     header => [{col1 => 'Target user has role',
  359:                                 col2 => 'User information updatable in author context'},
  360:                                {col1 => 'Target user has role',
  361:                                 col2 => 'User information updatable in course context'}],
  362:                     print => \&print_usermodification,
  363:                     modify => \&modify_usermodification,
  364:                   },
  365:         'scantron' =>
  366:                   { text => 'Bubblesheet format file',
  367:                     help => 'Domain_Configuration_Scantron_Format',
  368:                     header => [ {col1 => 'Item',
  369:                                  col2 => '',
  370:                               }],
  371:                     print => \&print_scantron,
  372:                     modify => \&modify_scantron,
  373:                   },
  374:         'requestcourses' => 
  375:                  {text => 'Request creation of courses',
  376:                   help => 'Domain_Configuration_Request_Courses',
  377:                   header => [{col1 => 'User affiliation',
  378:                               col2 => 'Availability/Processing of requests',},
  379:                              {col1 => 'Setting',
  380:                               col2 => 'Value'},
  381:                              {col1 => 'Available textbooks',
  382:                               col2 => ''},
  383:                              {col1 => 'Available templates',
  384:                               col2 => ''},
  385:                              {col1 => 'Validation (not official courses)',
  386:                               col2 => 'Value'},],
  387:                   print => \&print_quotas,
  388:                   modify => \&modify_quotas,
  389:                  },
  390:         'requestauthor' =>
  391:                  {text => 'Request Authoring Space',
  392:                   help => 'Domain_Configuration_Request_Author',
  393:                   header => [{col1 => 'User affiliation',
  394:                               col2 => 'Availability/Processing of requests',},
  395:                              {col1 => 'Setting',
  396:                               col2 => 'Value'}],
  397:                   print => \&print_quotas,
  398:                   modify => \&modify_quotas,
  399:                  },
  400:         'coursecategories' =>
  401:                   { text => 'Cataloging of courses/communities',
  402:                     help => 'Domain_Configuration_Cataloging_Courses',
  403:                     header => [{col1 => 'Catalog type/availability',
  404:                                 col2 => '',},
  405:                                {col1 => 'Category settings for standard catalog',
  406:                                 col2 => '',},
  407:                                {col1 => 'Categories',
  408:                                 col2 => '',
  409:                                }],
  410:                     print => \&print_coursecategories,
  411:                     modify => \&modify_coursecategories,
  412:                   },
  413:         'serverstatuses' =>
  414:                  {text   => 'Access to server status pages',
  415:                   help   => 'Domain_Configuration_Server_Status',
  416:                   header => [{col1 => 'Status Page',
  417:                               col2 => 'Other named users',
  418:                               col3 => 'Specific IPs',
  419:                             }],
  420:                   print => \&print_serverstatuses,
  421:                   modify => \&modify_serverstatuses,
  422:                  },
  423:         'helpsettings' =>
  424:                  {text   => 'Support settings',
  425:                   help   => 'Domain_Configuration_Help_Settings',
  426:                   header => [{col1 => 'Help Page Settings (logged-in users)',
  427:                               col2 => 'Value'},
  428:                              {col1 => 'Helpdesk Roles',
  429:                               col2 => 'Settings'},],
  430:                   print  => \&print_helpsettings,
  431:                   modify => \&modify_helpsettings,
  432:                  },
  433:         'coursedefaults' => 
  434:                  {text => 'Course/Community defaults',
  435:                   help => 'Domain_Configuration_Course_Defaults',
  436:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  437:                               col2 => 'Value',},
  438:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  439:                               col2 => 'Value',},],
  440:                   print => \&print_coursedefaults,
  441:                   modify => \&modify_coursedefaults,
  442:                  },
  443:         'selfenrollment' => 
  444:                  {text   => 'Self-enrollment in Course/Community',
  445:                   help   => 'Domain_Configuration_Selfenrollment',
  446:                   header => [{col1 => 'Configuration Rights',
  447:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  448:                              {col1 => 'Defaults',
  449:                               col2 => 'Value'},
  450:                              {col1 => 'Self-enrollment validation (optional)',
  451:                               col2 => 'Value'},],
  452:                   print => \&print_selfenrollment,
  453:                   modify => \&modify_selfenrollment,
  454:                  },
  455:         'usersessions' =>
  456:                  {text  => 'User session hosting/offloading',
  457:                   help  => 'Domain_Configuration_User_Sessions',
  458:                   header => [{col1 => 'Domain server',
  459:                               col2 => 'Servers to offload sessions to when busy'},
  460:                              {col1 => 'Hosting of users from other domains',
  461:                               col2 => 'Rules'},
  462:                              {col1 => "Hosting domain's own users elsewhere",
  463:                               col2 => 'Rules'}],
  464:                   print => \&print_usersessions,
  465:                   modify => \&modify_usersessions,
  466:                  },
  467:         'loadbalancing' =>
  468:                  {text  => 'Dedicated Load Balancer(s)',
  469:                   help  => 'Domain_Configuration_Load_Balancing',
  470:                   header => [{col1 => 'Balancers',
  471:                               col2 => 'Default destinations',
  472:                               col3 => 'User affiliation',
  473:                               col4 => 'Overrides'},
  474:                             ],
  475:                   print => \&print_loadbalancing,
  476:                   modify => \&modify_loadbalancing,
  477:                  },
  478:     );
  479:     if (keys(%servers) > 1) {
  480:         $prefs{'login'}  = { text   => 'Log-in page options',
  481:                              help   => 'Domain_Configuration_Login_Page',
  482:                             header => [{col1 => 'Log-in Service',
  483:                                         col2 => 'Server Setting',},
  484:                                        {col1 => 'Log-in Page Items',
  485:                                         col2 => ''},
  486:                                        {col1 => 'Log-in Help',
  487:                                         col2 => 'Value'},
  488:                                        {col1 => 'Custom HTML in document head',
  489:                                         col2 => 'Value'}],
  490:                             print => \&print_login,
  491:                             modify => \&modify_login,
  492:                            };
  493:     }
  494: 
  495:     my @roles = ('student','coordinator','author','admin');
  496:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  497:     &Apache::lonhtmlcommon::add_breadcrumb
  498:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  499:       text=>"Settings to display/modify"});
  500:     my $confname = $dom.'-domainconfig';
  501: 
  502:     if ($phase eq 'process') {
  503:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  504:                                                               \%prefs,\%domconfig,$confname,\@roles);
  505:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  506:             $r->rflush();
  507:             &devalidate_remote_domconfs($dom,$result);
  508:         }
  509:     } elsif ($phase eq 'display') {
  510:         my $js = &recaptcha_js().
  511:                  &toggle_display_js();
  512:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  513:             my ($othertitle,$usertypes,$types) =
  514:                 &Apache::loncommon::sorted_inst_types($dom);
  515:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  516:                                           $domconfig{'loadbalancing'}).
  517:                    &new_spares_js().
  518:                    &common_domprefs_js().
  519:                    &Apache::loncommon::javascript_array_indexof();
  520:         }
  521:         if (grep(/^requestcourses$/,@actions)) {
  522:             my $javascript_validations;
  523:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  524:             $js .= <<END;
  525: <script type="text/javascript">
  526: $javascript_validations
  527: </script>
  528: $coursebrowserjs
  529: END
  530:         }
  531:         if (grep(/^contacts$/,@actions)) {
  532:             $js .= &contacts_javascript();
  533:         }
  534:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  535:     } else {
  536: # check if domconfig user exists for the domain.
  537:         my $servadm = $r->dir_config('lonAdmEMail');
  538:         my ($configuserok,$author_ok,$switchserver) =
  539:             &config_check($dom,$confname,$servadm);
  540:         unless ($configuserok eq 'ok') {
  541:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  542:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  543:                           $confname).
  544:                       '<br />'
  545:             );
  546:             if ($switchserver) {
  547:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  548:                           '<br />'.
  549:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  550:                           '<br />'.
  551:                           &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).
  552:                           '<br />'.
  553:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  554:                 );
  555:             } else {
  556:                 $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.').
  557:                           '<br />'.
  558:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  559:                 );
  560:             }
  561:             $r->print(&Apache::loncommon::end_page());
  562:             return OK;
  563:         }
  564:         if (keys(%domconfig) == 0) {
  565:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  566:             my @ids=&Apache::lonnet::current_machine_ids();
  567:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  568:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  569:                 my @loginimages = ('img','logo','domlogo','login');
  570:                 my $custom_img_count = 0;
  571:                 foreach my $img (@loginimages) {
  572:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  573:                         $custom_img_count ++;
  574:                     }
  575:                 }
  576:                 foreach my $role (@roles) {
  577:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  578:                         $custom_img_count ++;
  579:                     }
  580:                 }
  581:                 if ($custom_img_count > 0) {
  582:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  583:                     my $switch_server = &check_switchserver($dom,$confname);
  584:                     $r->print(
  585:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  586:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  587:     &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 />'.
  588:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  589:                     if ($switch_server) {
  590:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  591:                     }
  592:                     $r->print(&Apache::loncommon::end_page());
  593:                     return OK;
  594:                 }
  595:             }
  596:         }
  597:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  598:     }
  599:     return OK;
  600: }
  601: 
  602: sub process_changes {
  603:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  604:     my %domconfig;
  605:     if (ref($values) eq 'HASH') {
  606:         %domconfig = %{$values};
  607:     }
  608:     my $output;
  609:     if ($action eq 'login') {
  610:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  611:     } elsif ($action eq 'rolecolors') {
  612:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  613:                                      $lastactref,%domconfig);
  614:     } elsif ($action eq 'quotas') {
  615:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  616:     } elsif ($action eq 'autoenroll') {
  617:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  618:     } elsif ($action eq 'autoupdate') {
  619:         $output = &modify_autoupdate($dom,%domconfig);
  620:     } elsif ($action eq 'autocreate') {
  621:         $output = &modify_autocreate($dom,%domconfig);
  622:     } elsif ($action eq 'directorysrch') {
  623:         $output = &modify_directorysrch($dom,%domconfig);
  624:     } elsif ($action eq 'usercreation') {
  625:         $output = &modify_usercreation($dom,%domconfig);
  626:     } elsif ($action eq 'selfcreation') {
  627:         $output = &modify_selfcreation($dom,%domconfig);
  628:     } elsif ($action eq 'usermodification') {
  629:         $output = &modify_usermodification($dom,%domconfig);
  630:     } elsif ($action eq 'contacts') {
  631:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  632:     } elsif ($action eq 'defaults') {
  633:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  634:     } elsif ($action eq 'scantron') {
  635:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  636:     } elsif ($action eq 'coursecategories') {
  637:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  638:     } elsif ($action eq 'serverstatuses') {
  639:         $output = &modify_serverstatuses($dom,%domconfig);
  640:     } elsif ($action eq 'requestcourses') {
  641:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  642:     } elsif ($action eq 'requestauthor') {
  643:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  644:     } elsif ($action eq 'helpsettings') {
  645:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  646:     } elsif ($action eq 'coursedefaults') {
  647:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  648:     } elsif ($action eq 'selfenrollment') {
  649:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  650:     } elsif ($action eq 'usersessions') {
  651:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  652:     } elsif ($action eq 'loadbalancing') {
  653:         $output = &modify_loadbalancing($dom,%domconfig);
  654:     }
  655:     return $output;
  656: }
  657: 
  658: sub print_config_box {
  659:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  660:     my $rowtotal = 0;
  661:     my $output;
  662:     if ($action eq 'coursecategories') {
  663:         $output = &coursecategories_javascript($settings);
  664:     } elsif ($action eq 'defaults') {
  665:         $output = &defaults_javascript($settings); 
  666:     } elsif ($action eq 'helpsettings') {
  667:         my (%privs,%levelscurrent);
  668:         my %full=();
  669:         my %levels=(
  670:                      course => {},
  671:                      domain => {},
  672:                      system => {},
  673:                    );
  674:         my $context = 'domain';
  675:         my $crstype = 'Course';
  676:         my $formname = 'display';
  677:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  678:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  679:         $output =
  680:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full,
  681:                                                       \@templateroles);
  682:     }
  683:     $output .=
  684:          '<table class="LC_nested_outer">
  685:           <tr>
  686:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  687:            &mt($item->{text}).'&nbsp;'.
  688:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  689:           '</tr>';
  690:     $rowtotal ++;
  691:     my $numheaders = 1;
  692:     if (ref($item->{'header'}) eq 'ARRAY') {
  693:         $numheaders = scalar(@{$item->{'header'}});
  694:     }
  695:     if ($numheaders > 1) {
  696:         my $colspan = '';
  697:         my $rightcolspan = '';
  698:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  699:             ($action eq 'directorysrch') ||
  700:             (($action eq 'login') && ($numheaders < 4))) {
  701:             $colspan = ' colspan="2"';
  702:         }
  703:         if ($action eq 'usersessions') {
  704:             $rightcolspan = ' colspan="3"'; 
  705:         }
  706:         $output .= '
  707:           <tr>
  708:            <td>
  709:             <table class="LC_nested">
  710:              <tr class="LC_info_row">
  711:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  712:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  713:              </tr>';
  714:         $rowtotal ++;
  715:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  716:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  717:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'directorysrch') ||
  718:             ($action eq 'helpsettings') || ($action eq 'contacts')) {
  719:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  720:         } elsif ($action eq 'coursecategories') {
  721:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  722:         } elsif ($action eq 'login') {
  723:             if ($numheaders == 4) {
  724:                 $colspan = ' colspan="2"';
  725:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  726:             } else {
  727:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  728:             }
  729:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  730:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  731:         } elsif ($action eq 'rolecolors') {
  732:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  733:         }
  734:         $output .= '
  735:            </table>
  736:           </td>
  737:          </tr>
  738:          <tr>
  739:            <td>
  740:             <table class="LC_nested">
  741:              <tr class="LC_info_row">
  742:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  743:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  744:              </tr>';
  745:             $rowtotal ++;
  746:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  747:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  748:             ($action eq 'usersessions') || ($action eq 'coursecategories') ||
  749:             ($action eq 'contacts') || ($action eq 'defaults')) {
  750:             if ($action eq 'coursecategories') {
  751:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  752:                 $colspan = ' colspan="2"';
  753:             } else {
  754:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  755:             }
  756:             $output .= '
  757:            </table>
  758:           </td>
  759:          </tr>
  760:          <tr>
  761:            <td>
  762:             <table class="LC_nested">
  763:              <tr class="LC_info_row">
  764:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  765:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  766:              </tr>'."\n";
  767:             if ($action eq 'coursecategories') {
  768:                 $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  769:             } else {
  770:                 $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  771:             }
  772:             $rowtotal ++;
  773:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  774:                  ($action eq 'defaults') || ($action eq 'directorysrch') ||
  775:                  ($action eq 'helpsettings')) {
  776:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  777:         } elsif ($action eq 'login') {
  778:             if ($numheaders == 4) {
  779:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  780:            </table>
  781:           </td>
  782:          </tr>
  783:          <tr>
  784:            <td>
  785:             <table class="LC_nested">
  786:              <tr class="LC_info_row">
  787:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  788:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  789:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  790:                 $rowtotal ++;
  791:             } else {
  792:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  793:             }
  794:             $output .= '
  795:            </table>
  796:           </td>
  797:          </tr>
  798:          <tr>
  799:            <td>
  800:             <table class="LC_nested">
  801:              <tr class="LC_info_row">';
  802:             if ($numheaders == 4) {
  803:                 $output .= '
  804:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  805:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  806:              </tr>';
  807:             } else {
  808:                 $output .= '
  809:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  810:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  811:              </tr>';
  812:             }
  813:             $rowtotal ++;
  814:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal);
  815:         } elsif ($action eq 'requestcourses') {
  816:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  817:             $rowtotal ++;
  818:             $output .= &print_studentcode($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'}->[2]->{'col1'}).'</td>
  827:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  828:                        &textbookcourses_javascript($settings).
  829:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
  830:             </table>
  831:            </td>
  832:           </tr>
  833:          <tr>
  834:            <td>
  835:             <table class="LC_nested">
  836:              <tr class="LC_info_row">
  837:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  838:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
  839:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
  840:             </table>
  841:            </td>
  842:           </tr>
  843:           <tr>
  844:            <td>
  845:             <table class="LC_nested">
  846:              <tr class="LC_info_row">
  847:               <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
  848:               <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
  849:              </tr>'.
  850:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
  851:         } elsif ($action eq 'requestauthor') {
  852:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  853:             $rowtotal ++;
  854:         } elsif ($action eq 'rolecolors') {
  855:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  856:            </table>
  857:           </td>
  858:          </tr>
  859:          <tr>
  860:            <td>
  861:             <table class="LC_nested">
  862:              <tr class="LC_info_row">
  863:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  864:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  865:               <td class="LC_right_item" valign="top">'.
  866:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  867:              </tr>'.
  868:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  869:            </table>
  870:           </td>
  871:          </tr>
  872:          <tr>
  873:            <td>
  874:             <table class="LC_nested">
  875:              <tr class="LC_info_row">
  876:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  877:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  878:              </tr>'.
  879:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  880:             $rowtotal += 2;
  881:         }
  882:     } else {
  883:         $output .= '
  884:           <tr>
  885:            <td>
  886:             <table class="LC_nested">
  887:              <tr class="LC_info_row">';
  888:         if ($action eq 'login') {
  889:             $output .= '  
  890:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  891:         } elsif ($action eq 'serverstatuses') {
  892:             $output .= '
  893:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  894:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  895: 
  896:         } else {
  897:             $output .= '
  898:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  899:         }
  900:         if (defined($item->{'header'}->[0]->{'col3'})) {
  901:             $output .= '<td class="LC_left_item" valign="top">'.
  902:                        &mt($item->{'header'}->[0]->{'col2'});
  903:             if ($action eq 'serverstatuses') {
  904:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  905:             } 
  906:         } else {
  907:             $output .= '<td class="LC_right_item" valign="top">'.
  908:                        &mt($item->{'header'}->[0]->{'col2'});
  909:         }
  910:         $output .= '</td>';
  911:         if ($item->{'header'}->[0]->{'col3'}) {
  912:             if (defined($item->{'header'}->[0]->{'col4'})) {
  913:                 $output .= '<td class="LC_left_item" valign="top">'.
  914:                             &mt($item->{'header'}->[0]->{'col3'});
  915:             } else {
  916:                 $output .= '<td class="LC_right_item" valign="top">'.
  917:                            &mt($item->{'header'}->[0]->{'col3'});
  918:             }
  919:             if ($action eq 'serverstatuses') {
  920:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  921:             }
  922:             $output .= '</td>';
  923:         }
  924:         if ($item->{'header'}->[0]->{'col4'}) {
  925:             $output .= '<td class="LC_right_item" valign="top">'.
  926:                        &mt($item->{'header'}->[0]->{'col4'});
  927:         }
  928:         $output .= '</tr>';
  929:         $rowtotal ++;
  930:         if ($action eq 'quotas') {
  931:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  932:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
  933:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing')) {
  934:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
  935:         } elsif ($action eq 'scantron') {
  936:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  937:         }
  938:     }
  939:     $output .= '
  940:    </table>
  941:   </td>
  942:  </tr>
  943: </table><br />';
  944:     return ($output,$rowtotal);
  945: }
  946: 
  947: sub print_login {
  948:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  949:     my ($css_class,$datatable);
  950:     my %choices = &login_choices();
  951: 
  952:     if ($caller eq 'service') {
  953:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  954:         my $choice = $choices{'disallowlogin'};
  955:         $css_class = ' class="LC_odd_row"';
  956:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  957:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  958:                       '<th>'.$choices{'server'}.'</th>'.
  959:                       '<th>'.$choices{'serverpath'}.'</th>'.
  960:                       '<th>'.$choices{'custompath'}.'</th>'.
  961:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  962:         my %disallowed;
  963:         if (ref($settings) eq 'HASH') {
  964:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  965:                %disallowed = %{$settings->{'loginvia'}};
  966:             }
  967:         }
  968:         foreach my $lonhost (sort(keys(%servers))) {
  969:             my $direct = 'selected="selected"';
  970:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  971:                 if ($disallowed{$lonhost}{'server'} ne '') {
  972:                     $direct = '';
  973:                 }
  974:             }
  975:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  976:                           '<td><select name="'.$lonhost.'_server">'.
  977:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  978:                           '</option>';
  979:             foreach my $hostid (sort(keys(%servers))) {
  980:                 next if ($servers{$hostid} eq $servers{$lonhost});
  981:                 my $selected = '';
  982:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  983:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  984:                         $selected = 'selected="selected"';
  985:                     }
  986:                 }
  987:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  988:                               $servers{$hostid}.'</option>';
  989:             }
  990:             $datatable .= '</select></td>'.
  991:                           '<td><select name="'.$lonhost.'_serverpath">';
  992:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  993:                 my $pathname = $path;
  994:                 if ($path eq 'custom') {
  995:                     $pathname = &mt('Custom Path').' ->';
  996:                 }
  997:                 my $selected = '';
  998:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  999:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1000:                         $selected = 'selected="selected"';
 1001:                     }
 1002:                 } elsif ($path eq '') {
 1003:                     $selected = 'selected="selected"';
 1004:                 }
 1005:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1006:             }
 1007:             $datatable .= '</select></td>';
 1008:             my ($custom,$exempt);
 1009:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1010:                 $custom = $disallowed{$lonhost}{'custompath'};
 1011:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1012:             }
 1013:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1014:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1015:                           '</tr>';
 1016:         }
 1017:         $datatable .= '</table></td></tr>';
 1018:         return $datatable;
 1019:     } elsif ($caller eq 'page') {
 1020:         my %defaultchecked = ( 
 1021:                                'coursecatalog' => 'on',
 1022:                                'helpdesk'      => 'on',
 1023:                                'adminmail'     => 'off',
 1024:                                'newuser'       => 'off',
 1025:                              );
 1026:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1027:         my (%checkedon,%checkedoff);
 1028:         foreach my $item (@toggles) {
 1029:             if ($defaultchecked{$item} eq 'on') { 
 1030:                 $checkedon{$item} = ' checked="checked" ';
 1031:                 $checkedoff{$item} = ' ';
 1032:             } elsif ($defaultchecked{$item} eq 'off') {
 1033:                 $checkedoff{$item} = ' checked="checked" ';
 1034:                 $checkedon{$item} = ' ';
 1035:             }
 1036:         }
 1037:         my @images = ('img','logo','domlogo','login');
 1038:         my @logintext = ('textcol','bgcol');
 1039:         my @bgs = ('pgbg','mainbg','sidebg');
 1040:         my @links = ('link','alink','vlink');
 1041:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1042:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1043:         my (%is_custom,%designs);
 1044:         my %defaults = (
 1045:                        font => $defaultdesign{'login.font'},
 1046:                        );
 1047:         foreach my $item (@images) {
 1048:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1049:             $defaults{'showlogo'}{$item} = 1;
 1050:         }
 1051:         foreach my $item (@bgs) {
 1052:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1053:         }
 1054:         foreach my $item (@logintext) {
 1055:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1056:         }
 1057:         foreach my $item (@links) {
 1058:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1059:         }
 1060:         if (ref($settings) eq 'HASH') {
 1061:             foreach my $item (@toggles) {
 1062:                 if ($settings->{$item} eq '1') {
 1063:                     $checkedon{$item} =  ' checked="checked" ';
 1064:                     $checkedoff{$item} = ' ';
 1065:                 } elsif ($settings->{$item} eq '0') {
 1066:                     $checkedoff{$item} =  ' checked="checked" ';
 1067:                     $checkedon{$item} = ' ';
 1068:                 }
 1069:             }
 1070:             foreach my $item (@images) {
 1071:                 if (defined($settings->{$item})) {
 1072:                     $designs{$item} = $settings->{$item};
 1073:                     $is_custom{$item} = 1;
 1074:                 }
 1075:                 if (defined($settings->{'showlogo'}{$item})) {
 1076:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1077:                 }
 1078:             }
 1079:             foreach my $item (@logintext) {
 1080:                 if ($settings->{$item} ne '') {
 1081:                     $designs{'logintext'}{$item} = $settings->{$item};
 1082:                     $is_custom{$item} = 1;
 1083:                 }
 1084:             }
 1085:             if ($settings->{'font'} ne '') {
 1086:                 $designs{'font'} = $settings->{'font'};
 1087:                 $is_custom{'font'} = 1;
 1088:             }
 1089:             foreach my $item (@bgs) {
 1090:                 if ($settings->{$item} ne '') {
 1091:                     $designs{'bgs'}{$item} = $settings->{$item};
 1092:                     $is_custom{$item} = 1;
 1093:                 }
 1094:             }
 1095:             foreach my $item (@links) {
 1096:                 if ($settings->{$item} ne '') {
 1097:                     $designs{'links'}{$item} = $settings->{$item};
 1098:                     $is_custom{$item} = 1;
 1099:                 }
 1100:             }
 1101:         } else {
 1102:             if ($designhash{$dom.'.login.font'} ne '') {
 1103:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1104:                 $is_custom{'font'} = 1;
 1105:             }
 1106:             foreach my $item (@images) {
 1107:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1108:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1109:                     $is_custom{$item} = 1;
 1110:                 }
 1111:             }
 1112:             foreach my $item (@bgs) {
 1113:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1114:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1115:                     $is_custom{$item} = 1;
 1116:                 }
 1117:             }
 1118:             foreach my $item (@links) {
 1119:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1120:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1121:                     $is_custom{$item} = 1;
 1122:                 }
 1123:             }
 1124:         }
 1125:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1126:                                                       logo => 'Institution Logo',
 1127:                                                       domlogo => 'Domain Logo',
 1128:                                                       login => 'Login box');
 1129:         my $itemcount = 1;
 1130:         foreach my $item (@toggles) {
 1131:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1132:             $datatable .=  
 1133:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1134:                 '</td><td>'.
 1135:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1136:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1137:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1138:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1139:                 '</tr>';
 1140:             $itemcount ++;
 1141:         }
 1142:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1143:         $datatable .= '</tr></table></td></tr>';
 1144:     } elsif ($caller eq 'help') {
 1145:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1146:         my $switchserver = &check_switchserver($dom,$confname);
 1147:         my $itemcount = 1;
 1148:         $defaulturl = '/adm/loginproblems.html';
 1149:         $defaulttype = 'default';
 1150:         %lt = &Apache::lonlocal::texthash (
 1151:                      del     => 'Delete?',
 1152:                      rep     => 'Replace:',
 1153:                      upl     => 'Upload:',
 1154:                      default => 'Default',
 1155:                      custom  => 'Custom',
 1156:                                              );
 1157:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1158:         my @currlangs;
 1159:         if (ref($settings) eq 'HASH') {
 1160:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1161:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1162:                     next if ($settings->{'helpurl'}{$key} eq '');
 1163:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1164:                     $type{$key} = 'custom';
 1165:                     unless ($key eq 'nolang') {
 1166:                         push(@currlangs,$key);
 1167:                     }
 1168:                 }
 1169:             } elsif ($settings->{'helpurl'} ne '') {
 1170:                 $type{'nolang'} = 'custom';
 1171:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1172:             }
 1173:         }
 1174:         foreach my $lang ('nolang',sort(@currlangs)) {
 1175:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1176:             $datatable .= '<tr'.$css_class.'>';
 1177:             if ($url{$lang} eq '') {
 1178:                 $url{$lang} = $defaulturl;
 1179:             }
 1180:             if ($type{$lang} eq '') {
 1181:                 $type{$lang} = $defaulttype;
 1182:             }
 1183:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1184:             if ($lang eq 'nolang') {
 1185:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1186:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1187:             } else {
 1188:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1189:                                   $langchoices{$lang},
 1190:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1191:             }
 1192:             $datatable .= '</span></td>'."\n".
 1193:                           '<td class="LC_left_item">';
 1194:             if ($type{$lang} eq 'custom') {
 1195:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1196:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1197:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1198:             } else {
 1199:                 $datatable .= $lt{'upl'};
 1200:             }
 1201:             $datatable .='<br />';
 1202:             if ($switchserver) {
 1203:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1204:             } else {
 1205:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1206:             }
 1207:             $datatable .= '</td></tr>';
 1208:             $itemcount ++;
 1209:         }
 1210:         my @addlangs;
 1211:         foreach my $lang (sort(keys(%langchoices))) {
 1212:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1213:             push(@addlangs,$lang);
 1214:         }
 1215:         if (@addlangs > 0) {
 1216:             my %toadd;
 1217:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1218:             $toadd{''} = &mt('Select');
 1219:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1220:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1221:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1222:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1223:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1224:             if ($switchserver) {
 1225:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1226:             } else {
 1227:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1228:             }
 1229:             $datatable .= '</td></tr>';
 1230:             $itemcount ++;
 1231:         }
 1232:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1233:     } elsif ($caller eq 'headtag') {
 1234:         my %domservers = &Apache::lonnet::get_servers($dom);
 1235:         my $choice = $choices{'headtag'};
 1236:         $css_class = ' class="LC_odd_row"';
 1237:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1238:                       '<td align="left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1239:                       '<th>'.$choices{'current'}.'</th>'.
 1240:                       '<th>'.$choices{'action'}.'</th>'.
 1241:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1242:         my (%currurls,%currexempt);
 1243:         if (ref($settings) eq 'HASH') {
 1244:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1245:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1246:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1247:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1248:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1249:                     }
 1250:                 }
 1251:             }
 1252:         }
 1253:         my %lt = &Apache::lonlocal::texthash(
 1254:                                                del  => 'Delete?',
 1255:                                                rep  => 'Replace:',
 1256:                                                upl  => 'Upload:',
 1257:                                                curr => 'View contents',
 1258:                                                none => 'None',
 1259:         );
 1260:         my $switchserver = &check_switchserver($dom,$confname);
 1261:         foreach my $lonhost (sort(keys(%domservers))) {
 1262:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1263:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1264:             if ($currurls{$lonhost}) {
 1265:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1266:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1267:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1268:                               '">'.$lt{'curr'}.'</a></td>'.
 1269:                               '<td><span class="LC_nobreak"><label>'.
 1270:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1271:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1272:             } else {
 1273:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1274:             }
 1275:             $datatable .='<br />';
 1276:             if ($switchserver) {
 1277:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1278:             } else {
 1279:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1280:             }
 1281:             $datatable .= '</td><td><input type="textbox" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1282:         }
 1283:         $datatable .= '</table></td></tr>';
 1284:     }
 1285:     return $datatable;
 1286: }
 1287: 
 1288: sub login_choices {
 1289:     my %choices =
 1290:         &Apache::lonlocal::texthash (
 1291:             coursecatalog => 'Display Course/Community Catalog link?',
 1292:             adminmail     => "Display Administrator's E-mail Address?",
 1293:             helpdesk      => 'Display "Contact Helpdesk" link',
 1294:             disallowlogin => "Login page requests redirected",
 1295:             hostid        => "Server",
 1296:             server        => "Redirect to:",
 1297:             serverpath    => "Path",
 1298:             custompath    => "Custom", 
 1299:             exempt        => "Exempt IP(s)",
 1300:             directlogin   => "No redirect",
 1301:             newuser       => "Link to create a user account",
 1302:             img           => "Header",
 1303:             logo          => "Main Logo",
 1304:             domlogo       => "Domain Logo",
 1305:             login         => "Log-in Header", 
 1306:             textcol       => "Text color",
 1307:             bgcol         => "Box color",
 1308:             bgs           => "Background colors",
 1309:             links         => "Link colors",
 1310:             font          => "Font color",
 1311:             pgbg          => "Header",
 1312:             mainbg        => "Page",
 1313:             sidebg        => "Login box",
 1314:             link          => "Link",
 1315:             alink         => "Active link",
 1316:             vlink         => "Visited link",
 1317:             headtag       => "Custom markup",
 1318:             action        => "Action",
 1319:             current       => "Current",
 1320:         );
 1321:     return %choices;
 1322: }
 1323: 
 1324: sub print_rolecolors {
 1325:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1326:     my %choices = &color_font_choices();
 1327:     my @bgs = ('pgbg','tabbg','sidebg');
 1328:     my @links = ('link','alink','vlink');
 1329:     my @images = ('img');
 1330:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1331:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1332:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1333:     my (%is_custom,%designs);
 1334:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1335:     if (ref($settings) eq 'HASH') {
 1336:         if (ref($settings->{$role}) eq 'HASH') {
 1337:             if ($settings->{$role}->{'img'} ne '') {
 1338:                 $designs{'img'} = $settings->{$role}->{'img'};
 1339:                 $is_custom{'img'} = 1;
 1340:             }
 1341:             if ($settings->{$role}->{'font'} ne '') {
 1342:                 $designs{'font'} = $settings->{$role}->{'font'};
 1343:                 $is_custom{'font'} = 1;
 1344:             }
 1345:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1346:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1347:                 $is_custom{'fontmenu'} = 1;
 1348:             }
 1349:             foreach my $item (@bgs) {
 1350:                 if ($settings->{$role}->{$item} ne '') {
 1351:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1352:                     $is_custom{$item} = 1;
 1353:                 }
 1354:             }
 1355:             foreach my $item (@links) {
 1356:                 if ($settings->{$role}->{$item} ne '') {
 1357:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1358:                     $is_custom{$item} = 1;
 1359:                 }
 1360:             }
 1361:         }
 1362:     } else {
 1363:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1364:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1365:             $is_custom{'img'} = 1;
 1366:         }
 1367:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1368:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1369:             $is_custom{'fontmenu'} = 1; 
 1370:         }
 1371:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1372:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1373:             $is_custom{'font'} = 1;
 1374:         }
 1375:         foreach my $item (@bgs) {
 1376:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1377:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1378:                 $is_custom{$item} = 1;
 1379:             
 1380:             }
 1381:         }
 1382:         foreach my $item (@links) {
 1383:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1384:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1385:                 $is_custom{$item} = 1;
 1386:             }
 1387:         }
 1388:     }
 1389:     my $itemcount = 1;
 1390:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1391:     $datatable .= '</tr></table></td></tr>';
 1392:     return $datatable;
 1393: }
 1394: 
 1395: sub role_defaults {
 1396:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1397:     my %defaults;
 1398:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1399:         return %defaults;
 1400:     }
 1401:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1402:     if ($role eq 'login') {
 1403:         %defaults = (
 1404:                        font => $defaultdesign{$role.'.font'},
 1405:                     );
 1406:         if (ref($logintext) eq 'ARRAY') {
 1407:             foreach my $item (@{$logintext}) {
 1408:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1409:             }
 1410:         }
 1411:         foreach my $item (@{$images}) {
 1412:             $defaults{'showlogo'}{$item} = 1;
 1413:         }
 1414:     } else {
 1415:         %defaults = (
 1416:                        img => $defaultdesign{$role.'.img'},
 1417:                        font => $defaultdesign{$role.'.font'},
 1418:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1419:                     );
 1420:     }
 1421:     foreach my $item (@{$bgs}) {
 1422:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1423:     }
 1424:     foreach my $item (@{$links}) {
 1425:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1426:     }
 1427:     foreach my $item (@{$images}) {
 1428:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1429:     }
 1430:     return %defaults;
 1431: }
 1432: 
 1433: sub display_color_options {
 1434:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1435:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1436:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1437:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1438:     my $datatable = '<tr'.$css_class.'>'.
 1439:         '<td>'.$choices->{'font'}.'</td>';
 1440:     if (!$is_custom->{'font'}) {
 1441:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1442:     } else {
 1443:         $datatable .= '<td>&nbsp;</td>';
 1444:     }
 1445:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1446: 
 1447:     $datatable .= '<td><span class="LC_nobreak">'.
 1448:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1449:                   ' value="'.$current_color.'" />&nbsp;'.
 1450:                   '&nbsp;</td></tr>';
 1451:     unless ($role eq 'login') { 
 1452:         $datatable .= '<tr'.$css_class.'>'.
 1453:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1454:         if (!$is_custom->{'fontmenu'}) {
 1455:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1456:         } else {
 1457:             $datatable .= '<td>&nbsp;</td>';
 1458:         }
 1459: 	$current_color = $designs->{'fontmenu'} ?
 1460: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1461:         $datatable .= '<td><span class="LC_nobreak">'.
 1462:                       '<input class="colorchooser" type="text" size="10" name="'
 1463: 		      .$role.'_fontmenu"'.
 1464:                       ' value="'.$current_color.'" />&nbsp;'.
 1465:                       '&nbsp;</td></tr>';
 1466:     }
 1467:     my $switchserver = &check_switchserver($dom,$confname);
 1468:     foreach my $img (@{$images}) {
 1469: 	$itemcount ++;
 1470:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1471:         $datatable .= '<tr'.$css_class.'>'.
 1472:                       '<td>'.$choices->{$img};
 1473:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1474:         if ($role eq 'login') {
 1475:             if ($img eq 'login') {
 1476:                 $login_hdr_pick =
 1477:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1478:                 $logincolors =
 1479:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1480:                                        $designs,$defaults);
 1481:             } elsif ($img ne 'domlogo') {
 1482:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1483:             }
 1484:         }
 1485:         $datatable .= '</td>';
 1486:         if ($designs->{$img} ne '') {
 1487:             $imgfile = $designs->{$img};
 1488: 	    $img_import = ($imgfile =~ m{^/adm/});
 1489:         } else {
 1490:             $imgfile = $defaults->{$img};
 1491:         }
 1492:         if ($imgfile) {
 1493:             my ($showfile,$fullsize);
 1494:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1495:                 my $urldir = $1;
 1496:                 my $filename = $2;
 1497:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1498:                 if (@info) {
 1499:                     my $thumbfile = 'tn-'.$filename;
 1500:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1501:                     if (@thumb) {
 1502:                         $showfile = $urldir.'/'.$thumbfile;
 1503:                     } else {
 1504:                         $showfile = $imgfile;
 1505:                     }
 1506:                 } else {
 1507:                     $showfile = '';
 1508:                 }
 1509:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1510:                 $showfile = $imgfile;
 1511:                 my $imgdir = $1;
 1512:                 my $filename = $2;
 1513:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1514:                     $showfile = "/$imgdir/tn-".$filename;
 1515:                 } else {
 1516:                     my $input = $londocroot.$imgfile;
 1517:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1518:                     if (!-e $output) {
 1519:                         my ($width,$height) = &thumb_dimensions();
 1520:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1521:                         if ($fullwidth ne '' && $fullheight ne '') {
 1522:                             if ($fullwidth > $width && $fullheight > $height) { 
 1523:                                 my $size = $width.'x'.$height;
 1524:                                 system("convert -sample $size $input $output");
 1525:                                 $showfile = "/$imgdir/tn-".$filename;
 1526:                             }
 1527:                         }
 1528:                     }
 1529:                 }
 1530:             }
 1531:             if ($showfile) {
 1532:                 if ($showfile =~ m{^/(adm|res)/}) {
 1533:                     if ($showfile =~ m{^/res/}) {
 1534:                         my $local_showfile =
 1535:                             &Apache::lonnet::filelocation('',$showfile);
 1536:                         &Apache::lonnet::repcopy($local_showfile);
 1537:                     }
 1538:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1539:                 }
 1540:                 if ($imgfile) {
 1541:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1542:                         if ($imgfile =~ m{^/res/}) {
 1543:                             my $local_imgfile =
 1544:                                 &Apache::lonnet::filelocation('',$imgfile);
 1545:                             &Apache::lonnet::repcopy($local_imgfile);
 1546:                         }
 1547:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1548:                     } else {
 1549:                         $fullsize = $imgfile;
 1550:                     }
 1551:                 }
 1552:                 $datatable .= '<td>';
 1553:                 if ($img eq 'login') {
 1554:                     $datatable .= $login_hdr_pick;
 1555:                 } 
 1556:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1557:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1558:             } else {
 1559:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1560:                               &mt('Upload:').'<br />';
 1561:             }
 1562:         } else {
 1563:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1564:                           &mt('Upload:').'<br />';
 1565:         }
 1566:         if ($switchserver) {
 1567:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1568:         } else {
 1569:             if ($img ne 'login') { # suppress file selection for Log-in header
 1570:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1571:             }
 1572:         }
 1573:         $datatable .= '</td></tr>';
 1574:     }
 1575:     $itemcount ++;
 1576:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1577:     $datatable .= '<tr'.$css_class.'>'.
 1578:                   '<td>'.$choices->{'bgs'}.'</td>';
 1579:     my $bgs_def;
 1580:     foreach my $item (@{$bgs}) {
 1581:         if (!$is_custom->{$item}) {
 1582:             $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>';
 1583:         }
 1584:     }
 1585:     if ($bgs_def) {
 1586:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1587:     } else {
 1588:         $datatable .= '<td>&nbsp;</td>';
 1589:     }
 1590:     $datatable .= '<td class="LC_right_item">'.
 1591:                   '<table border="0"><tr>';
 1592: 
 1593:     foreach my $item (@{$bgs}) {
 1594:         $datatable .= '<td align="center">'.$choices->{$item};
 1595: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1596:         if ($designs->{'bgs'}{$item}) {
 1597:             $datatable .= '&nbsp;';
 1598:         }
 1599:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1600:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1601:     }
 1602:     $datatable .= '</tr></table></td></tr>';
 1603:     $itemcount ++;
 1604:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1605:     $datatable .= '<tr'.$css_class.'>'.
 1606:                   '<td>'.$choices->{'links'}.'</td>';
 1607:     my $links_def;
 1608:     foreach my $item (@{$links}) {
 1609:         if (!$is_custom->{$item}) {
 1610:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1611:         }
 1612:     }
 1613:     if ($links_def) {
 1614:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1615:     } else {
 1616:         $datatable .= '<td>&nbsp;</td>';
 1617:     }
 1618:     $datatable .= '<td class="LC_right_item">'.
 1619:                   '<table border="0"><tr>';
 1620:     foreach my $item (@{$links}) {
 1621: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1622:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1623:         if ($designs->{'links'}{$item}) {
 1624:             $datatable.='&nbsp;';
 1625:         }
 1626:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1627:                       '" /></td>';
 1628:     }
 1629:     $$rowtotal += $itemcount;
 1630:     return $datatable;
 1631: }
 1632: 
 1633: sub logo_display_options {
 1634:     my ($img,$defaults,$designs) = @_;
 1635:     my $checkedon;
 1636:     if (ref($defaults) eq 'HASH') {
 1637:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1638:             if ($defaults->{'showlogo'}{$img}) {
 1639:                 $checkedon = 'checked="checked" ';     
 1640:             }
 1641:         } 
 1642:     }
 1643:     if (ref($designs) eq 'HASH') {
 1644:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1645:             if (defined($designs->{'showlogo'}{$img})) {
 1646:                 if ($designs->{'showlogo'}{$img} == 0) {
 1647:                     $checkedon = '';
 1648:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1649:                     $checkedon = 'checked="checked" ';
 1650:                 }
 1651:             }
 1652:         }
 1653:     }
 1654:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1655:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1656:            &mt('show').'</label>'."\n";
 1657: }
 1658: 
 1659: sub login_header_options  {
 1660:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1661:     my $output = '';
 1662:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1663:         $output .= &mt('Text default(s):').'<br />';
 1664:         if (!$is_custom->{'textcol'}) {
 1665:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1666:                        '&nbsp;&nbsp;&nbsp;';
 1667:         }
 1668:         if (!$is_custom->{'bgcol'}) {
 1669:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1670:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1671:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1672:         }
 1673:         $output .= '<br />';
 1674:     }
 1675:     $output .='<br />';
 1676:     return $output;
 1677: }
 1678: 
 1679: sub login_text_colors {
 1680:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1681:     my $color_menu = '<table border="0"><tr>';
 1682:     foreach my $item (@{$logintext}) {
 1683:         $color_menu .= '<td align="center">'.$choices->{$item};
 1684:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1685:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1686:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1687:     }
 1688:     $color_menu .= '</tr></table><br />';
 1689:     return $color_menu;
 1690: }
 1691: 
 1692: sub image_changes {
 1693:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1694:     my $output;
 1695:     if ($img eq 'login') {
 1696:             # suppress image for Log-in header
 1697:     } elsif (!$is_custom) {
 1698:         if ($img ne 'domlogo') {
 1699:             $output .= &mt('Default image:').'<br />';
 1700:         } else {
 1701:             $output .= &mt('Default in use:').'<br />';
 1702:         }
 1703:     }
 1704:     if ($img eq 'login') { # suppress image for Log-in header
 1705:         $output .= '<td>'.$logincolors;
 1706:     } else {
 1707:         if ($img_import) {
 1708:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1709:         }
 1710:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1711:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1712:         if ($is_custom) {
 1713:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1714:                        '<input type="checkbox" name="'.
 1715:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1716:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1717:         } else {
 1718:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1719:         }
 1720:     }
 1721:     return $output;
 1722: }
 1723: 
 1724: sub print_quotas {
 1725:     my ($dom,$settings,$rowtotal,$action) = @_;
 1726:     my $context;
 1727:     if ($action eq 'quotas') {
 1728:         $context = 'tools';
 1729:     } else {
 1730:         $context = $action;
 1731:     }
 1732:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1733:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1734:     my $typecount = 0;
 1735:     my ($css_class,%titles);
 1736:     if ($context eq 'requestcourses') {
 1737:         @usertools = ('official','unofficial','community','textbook');
 1738:         @options =('norequest','approval','validate','autolimit');
 1739:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1740:         %titles = &courserequest_titles();
 1741:     } elsif ($context eq 'requestauthor') {
 1742:         @usertools = ('author');
 1743:         @options = ('norequest','approval','automatic');
 1744:         %titles = &authorrequest_titles();
 1745:     } else {
 1746:         @usertools = ('aboutme','blog','webdav','portfolio');
 1747:         %titles = &tool_titles();
 1748:     }
 1749:     if (ref($types) eq 'ARRAY') {
 1750:         foreach my $type (@{$types}) {
 1751:             my ($currdefquota,$currauthorquota);
 1752:             unless (($context eq 'requestcourses') ||
 1753:                     ($context eq 'requestauthor')) {
 1754:                 if (ref($settings) eq 'HASH') {
 1755:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1756:                         $currdefquota = $settings->{defaultquota}->{$type};
 1757:                     } else {
 1758:                         $currdefquota = $settings->{$type};
 1759:                     }
 1760:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1761:                         $currauthorquota = $settings->{authorquota}->{$type};
 1762:                     }
 1763:                 }
 1764:             }
 1765:             if (defined($usertypes->{$type})) {
 1766:                 $typecount ++;
 1767:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1768:                 $datatable .= '<tr'.$css_class.'>'.
 1769:                               '<td>'.$usertypes->{$type}.'</td>'.
 1770:                               '<td class="LC_left_item">';
 1771:                 if ($context eq 'requestcourses') {
 1772:                     $datatable .= '<table><tr>';
 1773:                 }
 1774:                 my %cell;  
 1775:                 foreach my $item (@usertools) {
 1776:                     if ($context eq 'requestcourses') {
 1777:                         my ($curroption,$currlimit);
 1778:                         if (ref($settings) eq 'HASH') {
 1779:                             if (ref($settings->{$item}) eq 'HASH') {
 1780:                                 $curroption = $settings->{$item}->{$type};
 1781:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1782:                                     $currlimit = $1; 
 1783:                                 }
 1784:                             }
 1785:                         }
 1786:                         if (!$curroption) {
 1787:                             $curroption = 'norequest';
 1788:                         }
 1789:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1790:                         foreach my $option (@options) {
 1791:                             my $val = $option;
 1792:                             if ($option eq 'norequest') {
 1793:                                 $val = 0;  
 1794:                             }
 1795:                             if ($option eq 'validate') {
 1796:                                 my $canvalidate = 0;
 1797:                                 if (ref($validations{$item}) eq 'HASH') { 
 1798:                                     if ($validations{$item}{$type}) {
 1799:                                         $canvalidate = 1;
 1800:                                     }
 1801:                                 }
 1802:                                 next if (!$canvalidate);
 1803:                             }
 1804:                             my $checked = '';
 1805:                             if ($option eq $curroption) {
 1806:                                 $checked = ' checked="checked"';
 1807:                             } elsif ($option eq 'autolimit') {
 1808:                                 if ($curroption =~ /^autolimit/) {
 1809:                                     $checked = ' checked="checked"';
 1810:                                 }                       
 1811:                             } 
 1812:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1813:                                   '<input type="radio" name="crsreq_'.$item.
 1814:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1815:                                   $titles{$option}.'</label>';
 1816:                             if ($option eq 'autolimit') {
 1817:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1818:                                                 $item.'_limit_'.$type.'" size="1" '.
 1819:                                                 'value="'.$currlimit.'" />';
 1820:                             }
 1821:                             $cell{$item} .= '</span> ';
 1822:                             if ($option eq 'autolimit') {
 1823:                                 $cell{$item} .= $titles{'unlimited'};
 1824:                             }
 1825:                         }
 1826:                     } elsif ($context eq 'requestauthor') {
 1827:                         my $curroption;
 1828:                         if (ref($settings) eq 'HASH') {
 1829:                             $curroption = $settings->{$type};
 1830:                         }
 1831:                         if (!$curroption) {
 1832:                             $curroption = 'norequest';
 1833:                         }
 1834:                         foreach my $option (@options) {
 1835:                             my $val = $option;
 1836:                             if ($option eq 'norequest') {
 1837:                                 $val = 0;
 1838:                             }
 1839:                             my $checked = '';
 1840:                             if ($option eq $curroption) {
 1841:                                 $checked = ' checked="checked"';
 1842:                             }
 1843:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1844:                                   '<input type="radio" name="authorreq_'.$type.
 1845:                                   '" value="'.$val.'"'.$checked.' />'.
 1846:                                   $titles{$option}.'</label></span>&nbsp; ';
 1847:                         }
 1848:                     } else {
 1849:                         my $checked = 'checked="checked" ';
 1850:                         if (ref($settings) eq 'HASH') {
 1851:                             if (ref($settings->{$item}) eq 'HASH') {
 1852:                                 if ($settings->{$item}->{$type} == 0) {
 1853:                                     $checked = '';
 1854:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1855:                                     $checked =  'checked="checked" ';
 1856:                                 }
 1857:                             }
 1858:                         }
 1859:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1860:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1861:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1862:                                       '</label></span>&nbsp; ';
 1863:                     }
 1864:                 }
 1865:                 if ($context eq 'requestcourses') {
 1866:                     $datatable .= '</tr><tr>';
 1867:                     foreach my $item (@usertools) {
 1868:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1869:                     }
 1870:                     $datatable .= '</tr></table>';
 1871:                 }
 1872:                 $datatable .= '</td>';
 1873:                 unless (($context eq 'requestcourses') ||
 1874:                         ($context eq 'requestauthor')) {
 1875:                     $datatable .= 
 1876:                               '<td class="LC_right_item">'.
 1877:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1878:                               '<input type="text" name="quota_'.$type.
 1879:                               '" value="'.$currdefquota.
 1880:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1881:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1882:                               '<input type="text" name="authorquota_'.$type.
 1883:                               '" value="'.$currauthorquota.
 1884:                               '" size="5" /></span></td>';
 1885:                 }
 1886:                 $datatable .= '</tr>';
 1887:             }
 1888:         }
 1889:     }
 1890:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1891:         $defaultquota = '20';
 1892:         $authorquota = '500';
 1893:         if (ref($settings) eq 'HASH') {
 1894:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1895:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1896:             } elsif (defined($settings->{'default'})) {
 1897:                 $defaultquota = $settings->{'default'};
 1898:             }
 1899:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1900:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1901:             }
 1902:         }
 1903:     }
 1904:     $typecount ++;
 1905:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1906:     $datatable .= '<tr'.$css_class.'>'.
 1907:                   '<td>'.$othertitle.'</td>'.
 1908:                   '<td class="LC_left_item">';
 1909:     if ($context eq 'requestcourses') {
 1910:         $datatable .= '<table><tr>';
 1911:     }
 1912:     my %defcell;
 1913:     foreach my $item (@usertools) {
 1914:         if ($context eq 'requestcourses') {
 1915:             my ($curroption,$currlimit);
 1916:             if (ref($settings) eq 'HASH') {
 1917:                 if (ref($settings->{$item}) eq 'HASH') {
 1918:                     $curroption = $settings->{$item}->{'default'};
 1919:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1920:                         $currlimit = $1;
 1921:                     }
 1922:                 }
 1923:             }
 1924:             if (!$curroption) {
 1925:                 $curroption = 'norequest';
 1926:             }
 1927:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1928:             foreach my $option (@options) {
 1929:                 my $val = $option;
 1930:                 if ($option eq 'norequest') {
 1931:                     $val = 0;
 1932:                 }
 1933:                 if ($option eq 'validate') {
 1934:                     my $canvalidate = 0;
 1935:                     if (ref($validations{$item}) eq 'HASH') {
 1936:                         if ($validations{$item}{'default'}) {
 1937:                             $canvalidate = 1;
 1938:                         }
 1939:                     }
 1940:                     next if (!$canvalidate);
 1941:                 }
 1942:                 my $checked = '';
 1943:                 if ($option eq $curroption) {
 1944:                     $checked = ' checked="checked"';
 1945:                 } elsif ($option eq 'autolimit') {
 1946:                     if ($curroption =~ /^autolimit/) {
 1947:                         $checked = ' checked="checked"';
 1948:                     }
 1949:                 }
 1950:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1951:                                   '<input type="radio" name="crsreq_'.$item.
 1952:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1953:                                   $titles{$option}.'</label>';
 1954:                 if ($option eq 'autolimit') {
 1955:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1956:                                        $item.'_limit_default" size="1" '.
 1957:                                        'value="'.$currlimit.'" />';
 1958:                 }
 1959:                 $defcell{$item} .= '</span> ';
 1960:                 if ($option eq 'autolimit') {
 1961:                     $defcell{$item} .= $titles{'unlimited'};
 1962:                 }
 1963:             }
 1964:         } elsif ($context eq 'requestauthor') {
 1965:             my $curroption;
 1966:             if (ref($settings) eq 'HASH') {
 1967:                 $curroption = $settings->{'default'};
 1968:             }
 1969:             if (!$curroption) {
 1970:                 $curroption = 'norequest';
 1971:             }
 1972:             foreach my $option (@options) {
 1973:                 my $val = $option;
 1974:                 if ($option eq 'norequest') {
 1975:                     $val = 0;
 1976:                 }
 1977:                 my $checked = '';
 1978:                 if ($option eq $curroption) {
 1979:                     $checked = ' checked="checked"';
 1980:                 }
 1981:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1982:                               '<input type="radio" name="authorreq_default"'.
 1983:                               ' value="'.$val.'"'.$checked.' />'.
 1984:                               $titles{$option}.'</label></span>&nbsp; ';
 1985:             }
 1986:         } else {
 1987:             my $checked = 'checked="checked" ';
 1988:             if (ref($settings) eq 'HASH') {
 1989:                 if (ref($settings->{$item}) eq 'HASH') {
 1990:                     if ($settings->{$item}->{'default'} == 0) {
 1991:                         $checked = '';
 1992:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1993:                         $checked = 'checked="checked" ';
 1994:                     }
 1995:                 }
 1996:             }
 1997:             $datatable .= '<span class="LC_nobreak"><label>'.
 1998:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1999:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2000:                           '</label></span>&nbsp; ';
 2001:         }
 2002:     }
 2003:     if ($context eq 'requestcourses') {
 2004:         $datatable .= '</tr><tr>';
 2005:         foreach my $item (@usertools) {
 2006:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2007:         }
 2008:         $datatable .= '</tr></table>';
 2009:     }
 2010:     $datatable .= '</td>';
 2011:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2012:         $datatable .= '<td class="LC_right_item">'.
 2013:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2014:                       '<input type="text" name="defaultquota" value="'.
 2015:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2016:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2017:                       '<input type="text" name="authorquota" value="'.
 2018:                       $authorquota.'" size="5" /></span></td>';
 2019:     }
 2020:     $datatable .= '</tr>';
 2021:     $typecount ++;
 2022:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2023:     $datatable .= '<tr'.$css_class.'>'.
 2024:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2025:     if ($context eq 'requestcourses') {
 2026:         $datatable .= &mt('(overrides affiliation, if set)').
 2027:                       '</td>'.
 2028:                       '<td class="LC_left_item">'.
 2029:                       '<table><tr>';
 2030:     } else {
 2031:         $datatable .= &mt('(overrides affiliation, if checked)').
 2032:                       '</td>'.
 2033:                       '<td class="LC_left_item" colspan="2">'.
 2034:                       '<br />';
 2035:     }
 2036:     my %advcell;
 2037:     foreach my $item (@usertools) {
 2038:         if ($context eq 'requestcourses') {
 2039:             my ($curroption,$currlimit);
 2040:             if (ref($settings) eq 'HASH') {
 2041:                 if (ref($settings->{$item}) eq 'HASH') {
 2042:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2043:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2044:                         $currlimit = $1;
 2045:                     }
 2046:                 }
 2047:             }
 2048:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2049:             my $checked = '';
 2050:             if ($curroption eq '') {
 2051:                 $checked = ' checked="checked"';
 2052:             }
 2053:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2054:                                '<input type="radio" name="crsreq_'.$item.
 2055:                                '__LC_adv" value=""'.$checked.' />'.
 2056:                                &mt('No override set').'</label></span>&nbsp; ';
 2057:             foreach my $option (@options) {
 2058:                 my $val = $option;
 2059:                 if ($option eq 'norequest') {
 2060:                     $val = 0;
 2061:                 }
 2062:                 if ($option eq 'validate') {
 2063:                     my $canvalidate = 0;
 2064:                     if (ref($validations{$item}) eq 'HASH') {
 2065:                         if ($validations{$item}{'_LC_adv'}) {
 2066:                             $canvalidate = 1;
 2067:                         }
 2068:                     }
 2069:                     next if (!$canvalidate);
 2070:                 }
 2071:                 my $checked = '';
 2072:                 if ($val eq $curroption) {
 2073:                     $checked = ' checked="checked"';
 2074:                 } elsif ($option eq 'autolimit') {
 2075:                     if ($curroption =~ /^autolimit/) {
 2076:                         $checked = ' checked="checked"';
 2077:                     }
 2078:                 }
 2079:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2080:                                   '<input type="radio" name="crsreq_'.$item.
 2081:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2082:                                   $titles{$option}.'</label>';
 2083:                 if ($option eq 'autolimit') {
 2084:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2085:                                        $item.'_limit__LC_adv" size="1" '.
 2086:                                        'value="'.$currlimit.'" />';
 2087:                 }
 2088:                 $advcell{$item} .= '</span> ';
 2089:                 if ($option eq 'autolimit') {
 2090:                     $advcell{$item} .= $titles{'unlimited'};
 2091:                 }
 2092:             }
 2093:         } elsif ($context eq 'requestauthor') {
 2094:             my $curroption;
 2095:             if (ref($settings) eq 'HASH') {
 2096:                 $curroption = $settings->{'_LC_adv'};
 2097:             }
 2098:             my $checked = '';
 2099:             if ($curroption eq '') {
 2100:                 $checked = ' checked="checked"';
 2101:             }
 2102:             $datatable .= '<span class="LC_nobreak"><label>'.
 2103:                           '<input type="radio" name="authorreq__LC_adv"'.
 2104:                           ' value=""'.$checked.' />'.
 2105:                           &mt('No override set').'</label></span>&nbsp; ';
 2106:             foreach my $option (@options) {
 2107:                 my $val = $option;
 2108:                 if ($option eq 'norequest') {
 2109:                     $val = 0;
 2110:                 }
 2111:                 my $checked = '';
 2112:                 if ($val eq $curroption) {
 2113:                     $checked = ' checked="checked"';
 2114:                 }
 2115:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2116:                               '<input type="radio" name="authorreq__LC_adv"'.
 2117:                               ' value="'.$val.'"'.$checked.' />'.
 2118:                               $titles{$option}.'</label></span>&nbsp; ';
 2119:             }
 2120:         } else {
 2121:             my $checked = 'checked="checked" ';
 2122:             if (ref($settings) eq 'HASH') {
 2123:                 if (ref($settings->{$item}) eq 'HASH') {
 2124:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2125:                         $checked = '';
 2126:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2127:                         $checked = 'checked="checked" ';
 2128:                     }
 2129:                 }
 2130:             }
 2131:             $datatable .= '<span class="LC_nobreak"><label>'.
 2132:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2133:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2134:                           '</label></span>&nbsp; ';
 2135:         }
 2136:     }
 2137:     if ($context eq 'requestcourses') {
 2138:         $datatable .= '</tr><tr>';
 2139:         foreach my $item (@usertools) {
 2140:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2141:         }
 2142:         $datatable .= '</tr></table>';
 2143:     }
 2144:     $datatable .= '</td></tr>';
 2145:     $$rowtotal += $typecount;
 2146:     return $datatable;
 2147: }
 2148: 
 2149: sub print_requestmail {
 2150:     my ($dom,$action,$settings,$rowtotal) = @_;
 2151:     my ($now,$datatable,%currapp);
 2152:     $now = time;
 2153:     if (ref($settings) eq 'HASH') {
 2154:         if (ref($settings->{'notify'}) eq 'HASH') {
 2155:             if ($settings->{'notify'}{'approval'} ne '') {
 2156:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2157:             }
 2158:         }
 2159:     }
 2160:     my $numinrow = 2;
 2161:     my $css_class;
 2162:     $css_class = ($$rowtotal%2? ' class="LC_odd_row"':'');
 2163:     my $text;
 2164:     if ($action eq 'requestcourses') {
 2165:         $text = &mt('Receive notification of course requests requiring approval');
 2166:     } elsif ($action eq 'requestauthor') {
 2167:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2168:     } else {
 2169:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2170:     }
 2171:     $datatable = '<tr'.$css_class.'>'.
 2172:                  ' <td>'.$text.'</td>'.
 2173:                  ' <td class="LC_left_item">';
 2174:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2175:                                                  $action.'notifyapproval',%currapp);
 2176:     if ($numdc > 0) {
 2177:         $datatable .= $table;
 2178:     } else {
 2179:         $datatable .= &mt('There are no active Domain Coordinators');
 2180:     }
 2181:     $datatable .='</td></tr>';
 2182:     return $datatable;
 2183: }
 2184: 
 2185: sub print_studentcode {
 2186:     my ($settings,$rowtotal) = @_;
 2187:     my $rownum = 0; 
 2188:     my ($output,%current);
 2189:     my @crstypes = ('official','unofficial','community','textbook');
 2190:     if (ref($settings) eq 'HASH') {
 2191:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2192:             foreach my $type (@crstypes) {
 2193:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2194:             }
 2195:         }
 2196:     }
 2197:     $output .= '<tr>'.
 2198:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2199:                '<td class="LC_left_item">';
 2200:     foreach my $type (@crstypes) {
 2201:         my $check = ' ';
 2202:         if ($current{$type}) {
 2203:             $check = ' checked="checked" ';
 2204:         }
 2205:         $output .= '<span class="LC_nobreak"><label>'.
 2206:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2207:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2208:     }
 2209:     $output .= '</td></tr>';
 2210:     $$rowtotal ++;
 2211:     return $output;
 2212: }
 2213: 
 2214: sub print_textbookcourses {
 2215:     my ($dom,$type,$settings,$rowtotal) = @_;
 2216:     my $rownum = 0;
 2217:     my $css_class;
 2218:     my $itemcount = 1;
 2219:     my $maxnum = 0;
 2220:     my $bookshash;
 2221:     if (ref($settings) eq 'HASH') {
 2222:         $bookshash = $settings->{$type};
 2223:     }
 2224:     my %ordered;
 2225:     if (ref($bookshash) eq 'HASH') {
 2226:         foreach my $item (keys(%{$bookshash})) {
 2227:             if (ref($bookshash->{$item}) eq 'HASH') {
 2228:                 my $num = $bookshash->{$item}{'order'};
 2229:                 $ordered{$num} = $item;
 2230:             }
 2231:         }
 2232:     }
 2233:     my $confname = $dom.'-domainconfig';
 2234:     my $switchserver = &check_switchserver($dom,$confname);
 2235:     my $maxnum = scalar(keys(%ordered));
 2236:     my $datatable;
 2237:     if (keys(%ordered)) {
 2238:         my @items = sort { $a <=> $b } keys(%ordered);
 2239:         for (my $i=0; $i<@items; $i++) {
 2240:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2241:             my $key = $ordered{$items[$i]};
 2242:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2243:             my $coursetitle = $coursehash{'description'};
 2244:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2245:             if (ref($bookshash->{$key}) eq 'HASH') {
 2246:                 $subject = $bookshash->{$key}->{'subject'};
 2247:                 $title = $bookshash->{$key}->{'title'};
 2248:                 if ($type eq 'textbooks') {
 2249:                     $publisher = $bookshash->{$key}->{'publisher'};
 2250:                     $author = $bookshash->{$key}->{'author'};
 2251:                     $image = $bookshash->{$key}->{'image'};
 2252:                     if ($image ne '') {
 2253:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2254:                         my $imagethumb = "$path/tn-".$imagefile;
 2255:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2256:                     }
 2257:                 }
 2258:             }
 2259:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2260:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2261:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2262:             for (my $k=0; $k<=$maxnum; $k++) {
 2263:                 my $vpos = $k+1;
 2264:                 my $selstr;
 2265:                 if ($k == $i) {
 2266:                     $selstr = ' selected="selected" ';
 2267:                 }
 2268:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2269:             }
 2270:             $datatable .= '</select>'.('&nbsp;'x2).
 2271:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2272:                 &mt('Delete?').'</label></span></td>'.
 2273:                 '<td colspan="2">'.
 2274:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2275:                 ('&nbsp;'x2).
 2276:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2277:             if ($type eq 'textbooks') {
 2278:                 $datatable .= ('&nbsp;'x2).
 2279:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2280:                               ('&nbsp;'x2).
 2281:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2282:                               ('&nbsp;'x2).
 2283:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2284:                 if ($image) {
 2285:                     $datatable .= '<span class="LC_nobreak">'.
 2286:                                   $imgsrc.
 2287:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2288:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2289:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2290:                 }
 2291:                 if ($switchserver) {
 2292:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2293:                 } else {
 2294:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2295:                 }
 2296:             }
 2297:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2298:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2299:                           $coursetitle.'</span></td></tr>'."\n";
 2300:             $itemcount ++;
 2301:         }
 2302:     }
 2303:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2304:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2305:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2306:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2307:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2308:     for (my $k=0; $k<$maxnum+1; $k++) {
 2309:         my $vpos = $k+1;
 2310:         my $selstr;
 2311:         if ($k == $maxnum) {
 2312:             $selstr = ' selected="selected" ';
 2313:         }
 2314:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2315:     }
 2316:     $datatable .= '</select>&nbsp;'."\n".
 2317:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2318:                   '<td colspan="2">'.
 2319:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2320:                   ('&nbsp;'x2).
 2321:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2322:                   ('&nbsp;'x2);
 2323:     if ($type eq 'textbooks') {
 2324:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2325:                       ('&nbsp;'x2).
 2326:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2327:                       ('&nbsp;'x2).
 2328:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2329:         if ($switchserver) {
 2330:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2331:         } else {
 2332:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2333:         }
 2334:     }
 2335:     $datatable .= '</span>'."\n".
 2336:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2337:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2338:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2339:                   &Apache::loncommon::selectcourse_link
 2340:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course');
 2341:                   '</span></td>'."\n".
 2342:                   '</tr>'."\n";
 2343:     $itemcount ++;
 2344:     return $datatable;
 2345: }
 2346: 
 2347: sub textbookcourses_javascript {
 2348:     my ($settings) = @_;
 2349:     return unless(ref($settings) eq 'HASH');
 2350:     my (%ordered,%total,%jstext);
 2351:     foreach my $type ('textbooks','templates') {
 2352:         $total{$type} = 0;
 2353:         if (ref($settings->{$type}) eq 'HASH') {
 2354:             foreach my $item (keys(%{$settings->{$type}})) {
 2355:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2356:                     my $num = $settings->{$type}->{$item}{'order'};
 2357:                     $ordered{$type}{$num} = $item;
 2358:                 }
 2359:             }
 2360:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2361:         }
 2362:         my @jsarray = ();
 2363:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2364:             push(@jsarray,$ordered{$type}{$item});
 2365:         }
 2366:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2367:     }
 2368:     return <<"ENDSCRIPT";
 2369: <script type="text/javascript">
 2370: // <![CDATA[
 2371: function reorderBooks(form,item,caller) {
 2372:     var changedVal;
 2373: $jstext{'textbooks'};
 2374: $jstext{'templates'};
 2375:     var newpos;
 2376:     var maxh;
 2377:     if (caller == 'textbooks') {  
 2378:         newpos = 'textbooks_addbook_pos';
 2379:         maxh = 1 + $total{'textbooks'};
 2380:     } else {
 2381:         newpos = 'templates_addbook_pos';
 2382:         maxh = 1 + $total{'templates'};
 2383:     }
 2384:     var current = new Array;
 2385:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2386:     if (item == newpos) {
 2387:         changedVal = newitemVal;
 2388:     } else {
 2389:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2390:         current[newitemVal] = newpos;
 2391:     }
 2392:     if (caller == 'textbooks') {
 2393:         for (var i=0; i<textbooks.length; i++) {
 2394:             var elementName = 'textbooks_'+textbooks[i];
 2395:             if (elementName != item) {
 2396:                 if (form.elements[elementName]) {
 2397:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2398:                     current[currVal] = elementName;
 2399:                 }
 2400:             }
 2401:         }
 2402:     }
 2403:     if (caller == 'templates') {
 2404:         for (var i=0; i<templates.length; i++) {
 2405:             var elementName = 'templates_'+templates[i];
 2406:             if (elementName != item) {
 2407:                 if (form.elements[elementName]) {
 2408:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2409:                     current[currVal] = elementName;
 2410:                 }
 2411:             }
 2412:         }
 2413:     }
 2414:     var oldVal;
 2415:     for (var j=0; j<maxh; j++) {
 2416:         if (current[j] == undefined) {
 2417:             oldVal = j;
 2418:         }
 2419:     }
 2420:     if (oldVal < changedVal) {
 2421:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2422:            var elementName = current[k];
 2423:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2424:         }
 2425:     } else {
 2426:         for (var k=changedVal; k<oldVal; k++) {
 2427:             var elementName = current[k];
 2428:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2429:         }
 2430:     }
 2431:     return;
 2432: }
 2433: 
 2434: // ]]>
 2435: </script>
 2436: 
 2437: ENDSCRIPT
 2438: }
 2439: 
 2440: sub print_autoenroll {
 2441:     my ($dom,$settings,$rowtotal) = @_;
 2442:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2443:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,$failsafe);
 2444:     if (ref($settings) eq 'HASH') {
 2445:         if (exists($settings->{'run'})) {
 2446:             if ($settings->{'run'} eq '0') {
 2447:                 $runoff = ' checked="checked" ';
 2448:                 $runon = ' ';
 2449:             } else {
 2450:                 $runon = ' checked="checked" ';
 2451:                 $runoff = ' ';
 2452:             }
 2453:         } else {
 2454:             if ($autorun) {
 2455:                 $runon = ' checked="checked" ';
 2456:                 $runoff = ' ';
 2457:             } else {
 2458:                 $runoff = ' checked="checked" ';
 2459:                 $runon = ' ';
 2460:             }
 2461:         }
 2462:         if (exists($settings->{'co-owners'})) {
 2463:             if ($settings->{'co-owners'} eq '0') {
 2464:                 $coownersoff = ' checked="checked" ';
 2465:                 $coownerson = ' ';
 2466:             } else {
 2467:                 $coownerson = ' checked="checked" ';
 2468:                 $coownersoff = ' ';
 2469:             }
 2470:         } else {
 2471:             $coownersoff = ' checked="checked" ';
 2472:             $coownerson = ' ';
 2473:         }
 2474:         if (exists($settings->{'sender_domain'})) {
 2475:             $defdom = $settings->{'sender_domain'};
 2476:         }
 2477:         if (exists($settings->{'autofailsafe'})) {
 2478:             $failsafe = $settings->{'autofailsafe'};
 2479:         }
 2480:     } else {
 2481:         if ($autorun) {
 2482:             $runon = ' checked="checked" ';
 2483:             $runoff = ' ';
 2484:         } else {
 2485:             $runoff = ' checked="checked" ';
 2486:             $runon = ' ';
 2487:         }
 2488:     }
 2489:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2490:     my $notif_sender;
 2491:     if (ref($settings) eq 'HASH') {
 2492:         $notif_sender = $settings->{'sender_uname'};
 2493:     }
 2494:     my $datatable='<tr class="LC_odd_row">'.
 2495:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2496:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2497:                   '<input type="radio" name="autoenroll_run"'.
 2498:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2499:                   '<label><input type="radio" name="autoenroll_run"'.
 2500:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2501:                   '</tr><tr>'.
 2502:                   '<td>'.&mt('Notification messages - sender').
 2503:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2504:                   &mt('username').':&nbsp;'.
 2505:                   '<input type="text" name="sender_uname" value="'.
 2506:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2507:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2508:                   '<tr class="LC_odd_row">'.
 2509:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2510:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2511:                   '<input type="radio" name="autoassign_coowners"'.
 2512:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2513:                   '<label><input type="radio" name="autoassign_coowners"'.
 2514:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2515:                   '</tr><tr>'.
 2516:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 2517:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2518:                   '<input type="text" name="autoenroll_failsafe"'.
 2519:                   ' value="'.$failsafe.'" size="4" /></td></tr>';
 2520:     $$rowtotal += 4;
 2521:     return $datatable;
 2522: }
 2523: 
 2524: sub print_autoupdate {
 2525:     my ($position,$dom,$settings,$rowtotal) = @_;
 2526:     my $datatable;
 2527:     if ($position eq 'top') {
 2528:         my $updateon = ' ';
 2529:         my $updateoff = ' checked="checked" ';
 2530:         my $classlistson = ' ';
 2531:         my $classlistsoff = ' checked="checked" ';
 2532:         if (ref($settings) eq 'HASH') {
 2533:             if ($settings->{'run'} eq '1') {
 2534:                 $updateon = $updateoff;
 2535:                 $updateoff = ' ';
 2536:             }
 2537:             if ($settings->{'classlists'} eq '1') {
 2538:                 $classlistson = $classlistsoff;
 2539:                 $classlistsoff = ' ';
 2540:             }
 2541:         }
 2542:         my %title = (
 2543:                    run => 'Auto-update active?',
 2544:                    classlists => 'Update information in classlists?',
 2545:                     );
 2546:         $datatable = '<tr class="LC_odd_row">'. 
 2547:                   '<td>'.&mt($title{'run'}).'</td>'.
 2548:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2549:                   '<input type="radio" name="autoupdate_run"'.
 2550:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2551:                   '<label><input type="radio" name="autoupdate_run"'.
 2552:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2553:                   '</tr><tr>'.
 2554:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2555:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2556:                   '<label><input type="radio" name="classlists"'.
 2557:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2558:                   '<label><input type="radio" name="classlists"'.
 2559:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2560:                   '</tr>';
 2561:         $$rowtotal += 2;
 2562:     } elsif ($position eq 'middle') {
 2563:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2564:         my $numinrow = 3;
 2565:         my $locknamesettings;
 2566:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2567:                                      $dom,$numinrow,$othertitle,
 2568:                                     'lockablenames');
 2569:         $$rowtotal ++;
 2570:     } else {
 2571:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2572:         my @fields = ('lastname','firstname','middlename','generation',
 2573:                       'permanentemail','id');
 2574:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2575:         my $numrows = 0;
 2576:         if (ref($types) eq 'ARRAY') {
 2577:             if (@{$types} > 0) {
 2578:                 $datatable = 
 2579:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2580:                                          \@fields,$types,\$numrows);
 2581:                     $$rowtotal += @{$types}; 
 2582:             }
 2583:         }
 2584:         $datatable .= 
 2585:             &usertype_update_row($settings,{'default' => $othertitle},
 2586:                                  \%fieldtitles,\@fields,['default'],
 2587:                                  \$numrows);
 2588:         $$rowtotal ++;     
 2589:     }
 2590:     return $datatable;
 2591: }
 2592: 
 2593: sub print_autocreate {
 2594:     my ($dom,$settings,$rowtotal) = @_;
 2595:     my (%createon,%createoff,%currhash);
 2596:     my @types = ('xml','req');
 2597:     if (ref($settings) eq 'HASH') {
 2598:         foreach my $item (@types) {
 2599:             $createoff{$item} = ' checked="checked" ';
 2600:             $createon{$item} = ' ';
 2601:             if (exists($settings->{$item})) {
 2602:                 if ($settings->{$item}) {
 2603:                     $createon{$item} = ' checked="checked" ';
 2604:                     $createoff{$item} = ' ';
 2605:                 }
 2606:             }
 2607:         }
 2608:         if ($settings->{'xmldc'} ne '') {
 2609:             $currhash{$settings->{'xmldc'}} = 1;
 2610:         }
 2611:     } else {
 2612:         foreach my $item (@types) {
 2613:             $createoff{$item} = ' checked="checked" ';
 2614:             $createon{$item} = ' ';
 2615:         }
 2616:     }
 2617:     $$rowtotal += 2;
 2618:     my $numinrow = 2;
 2619:     my $datatable='<tr class="LC_odd_row">'.
 2620:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2621:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2622:                   '<input type="radio" name="autocreate_xml"'.
 2623:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2624:                   '<label><input type="radio" name="autocreate_xml"'.
 2625:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2626:                   '</td></tr><tr>'.
 2627:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2628:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2629:                   '<input type="radio" name="autocreate_req"'.
 2630:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2631:                   '<label><input type="radio" name="autocreate_req"'.
 2632:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2633:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2634:                                                    'autocreate_xmldc',%currhash);
 2635:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 2636:     if ($numdc > 1) {
 2637:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 2638:                       '</td><td class="LC_left_item">';
 2639:     } else {
 2640:         $datatable .= &mt('Course creation processed as:').
 2641:                       '</td><td class="LC_right_item">';
 2642:     }
 2643:     $datatable .= $dctable.'</td></tr>';
 2644:     $$rowtotal += $rows;
 2645:     return $datatable;
 2646: }
 2647: 
 2648: sub print_directorysrch {
 2649:     my ($position,$dom,$settings,$rowtotal) = @_;
 2650:     my $datatable;
 2651:     if ($position eq 'top') {
 2652:         my $instsrchon = ' ';
 2653:         my $instsrchoff = ' checked="checked" ';
 2654:         my ($exacton,$containson,$beginson);
 2655:         my $instlocalon = ' ';
 2656:         my $instlocaloff = ' checked="checked" ';
 2657:         if (ref($settings) eq 'HASH') {
 2658:             if ($settings->{'available'} eq '1') {
 2659:                 $instsrchon = $instsrchoff;
 2660:                 $instsrchoff = ' ';
 2661:             }
 2662:             if ($settings->{'localonly'} eq '1') {
 2663:                 $instlocalon = $instlocaloff;
 2664:                 $instlocaloff = ' ';
 2665:             }
 2666:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2667:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 2668:                     if ($type eq 'exact') {
 2669:                         $exacton = ' checked="checked" ';
 2670:                     } elsif ($type eq 'contains') {
 2671:                         $containson = ' checked="checked" ';
 2672:                     } elsif ($type eq 'begins') {
 2673:                         $beginson = ' checked="checked" ';
 2674:                     }
 2675:                 }
 2676:             } else {
 2677:                 if ($settings->{'searchtypes'} eq 'exact') {
 2678:                     $exacton = ' checked="checked" ';
 2679:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 2680:                     $containson = ' checked="checked" ';
 2681:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 2682:                     $exacton = ' checked="checked" ';
 2683:                     $containson = ' checked="checked" ';
 2684:                 }
 2685:             }
 2686:         }
 2687:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2688:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2689: 
 2690:         my $numinrow = 4;
 2691:         my $cansrchrow = 0;
 2692:         $datatable='<tr class="LC_odd_row">'.
 2693:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 2694:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2695:                    '<input type="radio" name="dirsrch_available"'.
 2696:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2697:                    '<label><input type="radio" name="dirsrch_available"'.
 2698:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2699:                    '</tr><tr>'.
 2700:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 2701:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2702:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 2703:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2704:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 2705:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2706:                    '</tr>';
 2707:         $$rowtotal += 2;
 2708:         if (ref($usertypes) eq 'HASH') {
 2709:             if (keys(%{$usertypes}) > 0) {
 2710:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2711:                                              $numinrow,$othertitle,'cansearch');
 2712:                 $cansrchrow = 1;
 2713:             }
 2714:         }
 2715:         if ($cansrchrow) {
 2716:             $$rowtotal ++;
 2717:             $datatable .= '<tr>';
 2718:         } else {
 2719:             $datatable .= '<tr class="LC_odd_row">';
 2720:         }
 2721:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2722:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2723:         foreach my $title (@{$titleorder}) {
 2724:             if (defined($searchtitles->{$title})) {
 2725:                 my $check = ' ';
 2726:                 if (ref($settings) eq 'HASH') {
 2727:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2728:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2729:                             $check = ' checked="checked" ';
 2730:                         }
 2731:                     }
 2732:                 }
 2733:                 $datatable .= '<td class="LC_left_item">'.
 2734:                               '<span class="LC_nobreak"><label>'.
 2735:                               '<input type="checkbox" name="searchby" '.
 2736:                               'value="'.$title.'"'.$check.'/>'.
 2737:                               $searchtitles->{$title}.'</label></span></td>';
 2738:             }
 2739:         }
 2740:         $datatable .= '</tr></table></td></tr>';
 2741:         $$rowtotal ++;
 2742:         if ($cansrchrow) {
 2743:             $datatable .= '<tr class="LC_odd_row">';
 2744:         } else {
 2745:             $datatable .= '<tr>';
 2746:         }
 2747:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2748:                       '<td class="LC_left_item" colspan="2">'.
 2749:                       '<span class="LC_nobreak"><label>'.
 2750:                       '<input type="checkbox" name="searchtypes" '.
 2751:                       $exacton.' value="exact" />'.&mt('Exact match').
 2752:                       '</label>&nbsp;'.
 2753:                       '<label><input type="checkbox" name="searchtypes" '.
 2754:                       $beginson.' value="begins" />'.&mt('Begins with').
 2755:                       '</label>&nbsp;'.
 2756:                       '<label><input type="checkbox" name="searchtypes" '.
 2757:                       $containson.' value="contains" />'.&mt('Contains').
 2758:                       '</label></span></td></tr>';
 2759:         $$rowtotal ++;
 2760:     } else {
 2761:         my $domsrchon = ' checked="checked" ';
 2762:         my $domsrchoff = ' ';
 2763:         my $domlocalon = ' ';
 2764:         my $domlocaloff = ' checked="checked" ';
 2765:         if (ref($settings) eq 'HASH') {
 2766:             if ($settings->{'lclocalonly'} eq '1') {
 2767:                 $domlocalon = $domlocaloff;
 2768:                 $domlocaloff = ' ';
 2769:             }
 2770:             if ($settings->{'lcavailable'} eq '0') {
 2771:                 $domsrchoff = $domsrchon;
 2772:                 $domsrchon = ' ';
 2773:             }
 2774:         }
 2775:         $datatable='<tr class="LC_odd_row">'.
 2776:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 2777:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2778:                       '<input type="radio" name="dirsrch_domavailable"'.
 2779:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2780:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 2781:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2782:                       '</tr><tr>'.
 2783:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 2784:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2785:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 2786:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2787:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 2788:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2789:                       '</tr>';
 2790:         $$rowtotal += 2;
 2791:     }
 2792:     return $datatable;
 2793: }
 2794: 
 2795: sub print_contacts {
 2796:     my ($position,$dom,$settings,$rowtotal) = @_;
 2797:     my $datatable;
 2798:     my @contacts = ('adminemail','supportemail');
 2799:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 2800:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings);
 2801:     if ($position eq 'top') {
 2802:         if (ref($settings) eq 'HASH') {
 2803:             foreach my $item (@contacts) {
 2804:                 if (exists($settings->{$item})) {
 2805:                     $to{$item} = $settings->{$item};
 2806:                 }
 2807:             }
 2808:         }
 2809:     } elsif ($position eq 'middle') {
 2810:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 2811:                      'updatesmail','idconflictsmail');
 2812:         foreach my $type (@mailings) {
 2813:             $otheremails{$type} = '';
 2814:         }
 2815:     } else {
 2816:         @mailings = ('helpdeskmail','otherdomsmail');
 2817:         foreach my $type (@mailings) {
 2818:             $otheremails{$type} = '';
 2819:         }
 2820:         $bccemails{'helpdeskmail'} = '';
 2821:         $bccemails{'otherdomsmail'} = '';
 2822:         $includestr{'helpdeskmail'} = '';
 2823:         $includestr{'otherdomsmail'} = '';
 2824:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 2825:     }
 2826:     if (ref($settings) eq 'HASH') {
 2827:         unless ($position eq 'top') {
 2828:             foreach my $type (@mailings) {
 2829:                 if (exists($settings->{$type})) {
 2830:                     if (ref($settings->{$type}) eq 'HASH') {
 2831:                         foreach my $item (@contacts) {
 2832:                             if ($settings->{$type}{$item}) {
 2833:                                 $checked{$type}{$item} = ' checked="checked" ';
 2834:                             }
 2835:                         }
 2836:                         $otheremails{$type} = $settings->{$type}{'others'};
 2837:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 2838:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 2839:                             if ($settings->{$type}{'include'} ne '') {
 2840:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 2841:                                 $includestr{$type} = &unescape($includestr{$type});
 2842:                             }
 2843:                         }
 2844:                     }
 2845:                 } elsif ($type eq 'lonstatusmail') {
 2846:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2847:                 }
 2848:             }
 2849:         }
 2850:         if ($position eq 'bottom') {
 2851:             foreach my $type (@mailings) {
 2852:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 2853:                 if ($settings->{$type}{'include'} ne '') {
 2854:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 2855:                     $includestr{$type} = &unescape($includestr{$type});
 2856:                 }
 2857:             }
 2858:             if (ref($settings->{'helpform'}) eq 'HASH') {
 2859:                 if (ref($fields) eq 'ARRAY') {
 2860:                     foreach my $field (@{$fields}) {
 2861:                         $currfield{$field} = $settings->{'helpform'}{$field};
 2862:                     }
 2863:                 }
 2864:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 2865:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 2866:                 } else {
 2867:                     $maxsize = '1.0';
 2868:                 }
 2869:             } else {
 2870:                 if (ref($fields) eq 'ARRAY') {
 2871:                     foreach my $field (@{$fields}) {
 2872:                         $currfield{$field} = 'yes';
 2873:                     }
 2874:                 }
 2875:                 $maxsize = '1.0';
 2876:             }
 2877:         }
 2878:     } else {
 2879:         if ($position eq 'top') {
 2880:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2881:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2882:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2883:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2884:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2885:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2886:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2887:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 2888:         } elsif ($position eq 'bottom') {
 2889:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2890:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 2891:             if (ref($fields) eq 'ARRAY') {
 2892:                 foreach my $field (@{$fields}) {
 2893:                     $currfield{$field} = 'yes';
 2894:                 }
 2895:             }
 2896:             $maxsize = '1.0';
 2897:         }
 2898:     }
 2899:     my ($titles,$short_titles) = &contact_titles();
 2900:     my $rownum = 0;
 2901:     my $css_class;
 2902:     if ($position eq 'top') {
 2903:         foreach my $item (@contacts) {
 2904:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 2905:             $datatable .= '<tr'.$css_class.'>'. 
 2906:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 2907:                           '</span></td><td class="LC_right_item">'.
 2908:                           '<input type="text" name="'.$item.'" value="'.
 2909:                           $to{$item}.'" /></td></tr>';
 2910:             $rownum ++;
 2911:         }
 2912:     } else {
 2913:         foreach my $type (@mailings) {
 2914:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 2915:             $datatable .= '<tr'.$css_class.'>'.
 2916:                           '<td><span class="LC_nobreak">'.
 2917:                           $titles->{$type}.': </span></td>'.
 2918:                           '<td class="LC_left_item">';
 2919:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 2920:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 2921:             }
 2922:             $datatable .= '<span class="LC_nobreak">';
 2923:             foreach my $item (@contacts) {
 2924:                 $datatable .= '<label>'.
 2925:                               '<input type="checkbox" name="'.$type.'"'.
 2926:                               $checked{$type}{$item}.
 2927:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 2928:                               '</label>&nbsp;';
 2929:             }
 2930:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2931:                           '<input type="text" name="'.$type.'_others" '.
 2932:                           'value="'.$otheremails{$type}.'"  />';
 2933:             my %locchecked;
 2934:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 2935:                 foreach my $loc ('s','b') {
 2936:                     if ($includeloc{$type} eq $loc) {
 2937:                         $locchecked{$loc} = ' checked="checked"';
 2938:                         last;
 2939:                     }
 2940:                 }
 2941:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2942:                               '<input type="text" name="'.$type.'_bcc" '.
 2943:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 2944:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 2945:                               &mt('Text automatically added to e-mail:').' '.
 2946:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br >'.
 2947:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 2948:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 2949:                               ('&nbsp;'x2).
 2950:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 2951:                               '</span></fieldset>';
 2952:             }
 2953:             $datatable .= '</td></tr>'."\n";
 2954:             $rownum ++;
 2955:         }
 2956:     }
 2957:     if ($position eq 'middle') {
 2958:         my %choices;
 2959:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 2960:                                        &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2961:                                        &mt('LON-CAPA core group - MSU'),600,500));
 2962:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 2963:                                         &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2964:                                         &mt('LON-CAPA core group - MSU'),600,500));
 2965:         my @toggles = ('reporterrors','reportupdates');
 2966:         my %defaultchecked = ('reporterrors'  => 'on',
 2967:                               'reportupdates' => 'on');
 2968:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2969:                                                    \%choices,$rownum);
 2970:         $datatable .= $reports;
 2971:     } elsif ($position eq 'bottom') {
 2972:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2973:         $datatable .= '<tr'.$css_class.'>'.
 2974:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 2975:                       &mt('(e-mail, subject, and description always shown)').
 2976:                       '</td><td class="LC_left_item">';
 2977:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 2978:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 2979:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 2980:             foreach my $field (@{$fields}) {
 2981:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 2982:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 2983:                     $datatable .= ' '.&mt('(logged-in users)');
 2984:                 }
 2985:                 $datatable .='</td><td>';
 2986:                 my $clickaction;
 2987:                 if ($field eq 'screenshot') {
 2988:                     $clickaction = ' onclick="screenshotSize(this);"';
 2989:                 }
 2990:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 2991:                     foreach my $option (@{$possoptions->{$field}}) {
 2992:                         my $checked;
 2993:                         if ($currfield{$field} eq $option) {
 2994:                             $checked = ' checked="checked"';
 2995:                         }
 2996:                         $datatable .= '<span class="LC_nobreak"><label>'.
 2997:                                       '<input type="radio" name="helpform_'.$field.'" '.
 2998:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 2999:                                       '</label></span>'.('&nbsp;'x2);
 3000:                     }
 3001:                 }
 3002:                 if ($field eq 'screenshot') {
 3003:                     my $display;
 3004:                     if ($currfield{$field} eq 'no') {
 3005:                         $display = ' style="display:none"';
 3006:                     }
 3007:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.' />'.
 3008:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 3009:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 3010:                 }
 3011:                 $datatable .= '</td></tr>';
 3012:             }
 3013:             $datatable .= '</table>';
 3014:         }
 3015:         $datatable .= '</td></tr>'."\n";
 3016:         $rownum ++;
 3017:     }
 3018:     $$rowtotal += $rownum;
 3019:     return $datatable;
 3020: }
 3021: 
 3022: sub contacts_javascript {
 3023:     return <<"ENDSCRIPT";
 3024: 
 3025: <script type="text/javascript">
 3026: // <![CDATA[
 3027: 
 3028: function screenshotSize(field) {
 3029:     if (document.getElementById('help_screenshotsize')) {
 3030:         if (field.value == 'no') {
 3031:             document.getElementById('help_screenshotsize').style.display="none";
 3032:         } else {
 3033:             document.getElementById('help_screenshotsize').style.display="";
 3034:         }
 3035:     }
 3036:     return;
 3037: }
 3038: 
 3039: // ]]>
 3040: </script>
 3041: 
 3042: ENDSCRIPT
 3043: }
 3044: 
 3045: sub print_helpsettings {
 3046:     my ($position,$dom,$settings,$rowtotal) = @_;
 3047:     my $confname = $dom.'-domainconfig';
 3048:     my $formname = 'display';
 3049:     my ($datatable,$itemcount);
 3050:     if ($position eq 'top') {
 3051:         $itemcount = 1;
 3052:         my (%choices,%defaultchecked,@toggles);
 3053:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 3054:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 3055:                                      &mt('LON-CAPA bug tracker'),600,500));
 3056:         %defaultchecked = ('submitbugs' => 'on');
 3057:         @toggles = ('submitbugs');
 3058:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3059:                                                      \%choices,$itemcount);
 3060:         $$rowtotal ++;
 3061:     } else {
 3062:         my $css_class;
 3063:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 3064:         my (%customroles,%ordered,%current);
 3065:         if (ref($settings->{'adhoc'}) eq 'HASH') {
 3066:             %current = %{$settings->{'adhoc'}};
 3067:         }
 3068:         my $count = 0;
 3069:         foreach my $key (sort(keys(%existing))) {
 3070:             if ($key=~/^rolesdef\_(\w+)$/) {
 3071:                 my $rolename = $1;
 3072:                 my (%privs,$order);
 3073:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 3074:                 $customroles{$rolename} = \%privs;
 3075:                 if (ref($current{$rolename}) eq 'HASH') {
 3076:                     $order = $current{$rolename}{'order'};
 3077:                 }
 3078:                 if ($order eq '') {
 3079:                     $order = $count;
 3080:                 }
 3081:                 $ordered{$order} = $rolename;
 3082:                 $count++;
 3083:             }
 3084:         }
 3085:         my $maxnum = scalar(keys(%ordered));
 3086:         my @roles_by_num = ();
 3087:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3088:             push(@roles_by_num,$item);
 3089:         }
 3090:         my $context = 'domprefs';
 3091:         my $crstype = 'Course';
 3092:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3093:         my @accesstypes = ('all','dh','da','none');
 3094:         my ($numstatustypes,@jsarray);
 3095:         if (ref($types) eq 'ARRAY') {
 3096:             if (@{$types} > 0) {
 3097:                 $numstatustypes = scalar(@{$types});
 3098:                 push(@accesstypes,'status');
 3099:                 @jsarray = ('bystatus');
 3100:             }
 3101:         }
 3102:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh'.'da']);
 3103:         if (keys(%domhelpdesk)) {
 3104:             push(@accesstypes,('inc','exc'));
 3105:             push(@jsarray,('notinc','notexc'));
 3106:         }
 3107:         my $hiddenstr = join("','",@jsarray);
 3108:         $datatable .= &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname);
 3109:         my $context = 'domprefs';
 3110:         my $crstype = 'Course';
 3111:         my $prefix = 'helproles_';
 3112:         my $add_class = 'LC_hidden';
 3113:         foreach my $num (@roles_by_num) {
 3114:             my $role = $ordered{$num};
 3115:             my ($desc,$access,@statuses);
 3116:             if (ref($current{$role}) eq 'HASH') {
 3117:                 $desc = $current{$role}{'desc'};
 3118:                 $access = $current{$role}{'access'};
 3119:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 3120:                     @statuses = @{$current{$role}{'insttypes'}};
 3121:                 }
 3122:             }
 3123:             if ($desc eq '') {
 3124:                 $desc = $role;
 3125:             }
 3126:             my $identifier = 'custhelp'.$num;
 3127:             my %full=();
 3128:             my %levels= (
 3129:                          course => {},
 3130:                          domain => {},
 3131:                          system => {},
 3132:                         );
 3133:             my %levelscurrent=(
 3134:                                course => {},
 3135:                                domain => {},
 3136:                                system => {},
 3137:                               );
 3138:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 3139:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3140:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3141:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 3142:             $datatable .= '<tr '.$css_class.'><td valign="top"><b>'.$role.'</b><br />'.
 3143:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 3144:             for (my $k=0; $k<=$maxnum; $k++) {
 3145:                 my $vpos = $k+1;
 3146:                 my $selstr;
 3147:                 if ($k == $num) {
 3148:                     $selstr = ' selected="selected" ';
 3149:                 }
 3150:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3151:             }
 3152:             $datatable .= '</select>'.('&nbsp;'x2).
 3153:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 3154:                           '</td>'.
 3155:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3156:                           &mt('Name shown to users:').
 3157:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 3158:                           '</fieldset>'.
 3159:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 3160:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 3161:                           '<fieldset>'.
 3162:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 3163:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 3164:                                                                    \%levelscurrent,$identifier,
 3165:                                                                    'LC_hidden',$prefix.$num.'_privs').
 3166:                           '</fieldset></td>';
 3167:             $itemcount ++;
 3168:         }
 3169:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3170:         my $newcust = 'custhelp'.$count;
 3171:         my (%privs,%levelscurrent);
 3172:         my %full=();
 3173:         my %levels= (
 3174:                      course => {},
 3175:                      domain => {},
 3176:                      system => {},
 3177:                     );
 3178:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 3179:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3180:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 3181:         $datatable .= '<tr '.$css_class.'><td valign="top"><span class="LC_nobreak"><label>'.
 3182:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 3183:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 3184:         for (my $k=0; $k<$maxnum+1; $k++) {
 3185:             my $vpos = $k+1;
 3186:             my $selstr;
 3187:             if ($k == $maxnum) {
 3188:                 $selstr = ' selected="selected" ';
 3189:             }
 3190:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3191:         }
 3192:         $datatable .= '</select>&nbsp;'."\n".
 3193:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 3194:                       '</label></span></td>'.
 3195:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3196:                       '<span class="LC_nobreak">'.
 3197:                       &mt('Internal name:').
 3198:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 3199:                       '</span>'.('&nbsp;'x4).
 3200:                       '<span class="LC_nobreak">'.
 3201:                       &mt('Name shown to users:').
 3202:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 3203:                       '</span></fieldset>'.
 3204:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 3205:                                              $usertypes,$types,\%domhelpdesk).
 3206:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 3207:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 3208:                                                                 \@templateroles,$newcust).
 3209:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 3210:                                                                \%levelscurrent,$newcust).
 3211:                       '</fieldset></td></tr>';
 3212:         $count ++;
 3213:         $$rowtotal += $count;
 3214:     }
 3215:     return $datatable;
 3216: }
 3217: 
 3218: sub adhocbutton {
 3219:     my ($prefix,$num,$field,$visibility) = @_;
 3220:     my %lt = &Apache::lonlocal::texthash(
 3221:                                           show => 'Show details',
 3222:                                           hide => 'Hide details',
 3223:                                         );
 3224:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 3225:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 3226:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 3227:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 3228: }
 3229: 
 3230: sub helpsettings_javascript {
 3231:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 3232:     return unless(ref($roles_by_num) eq 'ARRAY');
 3233:     my %html_js_lt = &Apache::lonlocal::texthash(
 3234:                                           show => 'Show details',
 3235:                                           hide => 'Hide details',
 3236:                                         );
 3237:     &html_escape(\%html_js_lt);
 3238:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 3239:     return <<"ENDSCRIPT";
 3240: <script type="text/javascript">
 3241: // <![CDATA[
 3242: 
 3243: function reorderHelpRoles(form,item) {
 3244:     var changedVal;
 3245: $jstext
 3246:     var newpos = 'helproles_${total}_pos';
 3247:     var maxh = 1 + $total;
 3248:     var current = new Array();
 3249:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3250:     if (item == newpos) {
 3251:         changedVal = newitemVal;
 3252:     } else {
 3253:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3254:         current[newitemVal] = newpos;
 3255:     }
 3256:     for (var i=0; i<helproles.length; i++) {
 3257:         var elementName = 'helproles_'+helproles[i]+'_pos';
 3258:         if (elementName != item) {
 3259:             if (form.elements[elementName]) {
 3260:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3261:                 current[currVal] = elementName;
 3262:             }
 3263:         }
 3264:     }
 3265:     var oldVal;
 3266:     for (var j=0; j<maxh; j++) {
 3267:         if (current[j] == undefined) {
 3268:             oldVal = j;
 3269:         }
 3270:     }
 3271:     if (oldVal < changedVal) {
 3272:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3273:            var elementName = current[k];
 3274:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3275:         }
 3276:     } else {
 3277:         for (var k=changedVal; k<oldVal; k++) {
 3278:             var elementName = current[k];
 3279:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3280:         }
 3281:     }
 3282:     return;
 3283: }
 3284: 
 3285: function helpdeskAccess(num) {
 3286:     var curraccess = null;
 3287:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 3288:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 3289:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 3290:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 3291:             }
 3292:         }
 3293:     }
 3294:     var shown = Array();
 3295:     var hidden = Array();
 3296:     if (curraccess == 'none') {
 3297:         hidden = Array('$hiddenstr');
 3298:     } else {
 3299:         if (curraccess == 'status') {
 3300:             shown = Array('bystatus');
 3301:             hidden = Array('notinc','notexc');
 3302:         } else {
 3303:             if (curraccess == 'exc') {
 3304:                 shown = Array('notexc');
 3305:                 hidden = Array('notinc','bystatus');
 3306:             }
 3307:             if (curraccess == 'inc') {
 3308:                 shown = Array('notinc');
 3309:                 hidden = Array('notexc','bystatus');
 3310:             }
 3311:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 3312:                 hidden = Array('notinc','notexc','bystatus');
 3313:             }
 3314:         }
 3315:     }
 3316:     if (hidden.length > 0) {
 3317:         for (var i=0; i<hidden.length; i++) {
 3318:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 3319:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 3320:             }
 3321:         }
 3322:     }
 3323:     if (shown.length > 0) {
 3324:         for (var i=0; i<shown.length; i++) {
 3325:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 3326:                 if (shown[i] == 'privs') {
 3327:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 3328:                 } else {
 3329:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 3330:                 }
 3331:             }
 3332:         }
 3333:     }
 3334:     return;
 3335: }
 3336: 
 3337: function toggleHelpdeskItem(num,field) {
 3338:     if (document.getElementById('helproles_'+num+'_'+field)) {
 3339:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 3340:             document.getElementById('helproles_'+num+'_'+field).className =
 3341:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 3342:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3343:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 3344:             }
 3345:         } else {
 3346:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 3347:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3348:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 3349:             }
 3350:         }
 3351:     }
 3352:     return;
 3353: }
 3354: 
 3355: // ]]>
 3356: </script>
 3357: 
 3358: ENDSCRIPT
 3359: }
 3360: 
 3361: sub helpdeskroles_access {
 3362:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 3363:         $usertypes,$types,$domhelpdesk) = @_;
 3364:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 3365:     my %lt = &Apache::lonlocal::texthash(
 3366:                     'rou'    => 'Role usage',
 3367:                     'whi'    => 'Which helpdesk personnel may use this role?',
 3368:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 3369:                     'dh'     => 'All with domain helpdesk role',
 3370:                     'da'     => 'All with domain helpdesk assistant role',
 3371:                     'none'   => 'None',
 3372:                     'status' => 'Determined based on institutional status',
 3373:                     'inc'    => 'Include all, but exclude specific personnel',
 3374:                     'exc'    => 'Exclude all, but include specific personnel',
 3375:                   );
 3376:     my %usecheck = (
 3377:                      all => ' checked="checked"',
 3378:                    );
 3379:     my %displaydiv = (
 3380:                       status => 'none',
 3381:                       inc    => 'none',
 3382:                       exc    => 'none',
 3383:                       priv   => 'block',
 3384:                      );
 3385:     my $output;
 3386:     if (ref($current) eq 'HASH') {
 3387:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 3388:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 3389:                 $usecheck{$current->{access}} = $usecheck{'all'};
 3390:                 delete($usecheck{'all'});
 3391:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 3392:                     my $access = $1;
 3393:                     $displaydiv{$access} = 'inline';
 3394:                 } elsif ($current->{access} eq 'none') {
 3395:                     $displaydiv{'priv'} = 'none';
 3396:                 }
 3397:             }
 3398:         }
 3399:     }
 3400:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 3401:               '<p>'.$lt{'whi'}.'</p>';
 3402:     foreach my $access (@{$accesstypes}) {
 3403:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 3404:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 3405:                    $lt{$access}.'</label>';
 3406:         if ($access eq 'status') {
 3407:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 3408:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 3409:                                                                  $othertitle,$usertypes,$types).
 3410:                        '</div>';
 3411:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 3412:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 3413:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3414:                        '</div>';
 3415:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 3416:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 3417:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3418:                        '</div>';
 3419:         }
 3420:         $output .= '</p>';
 3421:     }
 3422:     $output .= '</fieldset>';
 3423:     return $output;
 3424: }
 3425: 
 3426: sub radiobutton_prefs {
 3427:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 3428:         $additional,$align) = @_;
 3429:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 3430:                    (ref($choices) eq 'HASH'));
 3431: 
 3432:     my (%checkedon,%checkedoff,$datatable,$css_class);
 3433: 
 3434:     foreach my $item (@{$toggles}) {
 3435:         if ($defaultchecked->{$item} eq 'on') {
 3436:             $checkedon{$item} = ' checked="checked" ';
 3437:             $checkedoff{$item} = ' ';
 3438:         } elsif ($defaultchecked->{$item} eq 'off') {
 3439:             $checkedoff{$item} = ' checked="checked" ';
 3440:             $checkedon{$item} = ' ';
 3441:         }
 3442:     }
 3443:     if (ref($settings) eq 'HASH') {
 3444:         foreach my $item (@{$toggles}) {
 3445:             if ($settings->{$item} eq '1') {
 3446:                 $checkedon{$item} =  ' checked="checked" ';
 3447:                 $checkedoff{$item} = ' ';
 3448:             } elsif ($settings->{$item} eq '0') {
 3449:                 $checkedoff{$item} =  ' checked="checked" ';
 3450:                 $checkedon{$item} = ' ';
 3451:             }
 3452:         }
 3453:     }
 3454:     if ($onclick) {
 3455:         $onclick = ' onclick="'.$onclick.'"';
 3456:     }
 3457:     foreach my $item (@{$toggles}) {
 3458:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3459:         $datatable .=
 3460:             '<tr'.$css_class.'><td valign="top">'.
 3461:             '<span class="LC_nobreak">'.$choices->{$item}.
 3462:             '</span></td>';
 3463:         if ($align eq 'left') {
 3464:             $datatable .= '<td class="LC_left_item">';
 3465:         } else {
 3466:             $datatable .= '<td class="LC_right_item">';
 3467:         }
 3468:         $datatable .=
 3469:             '<span class="LC_nobreak">'.
 3470:             '<label><input type="radio" name="'.
 3471:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 3472:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 3473:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 3474:             '</span>'.$additional.
 3475:             '</td>'.
 3476:             '</tr>';
 3477:         $itemcount ++;
 3478:     }
 3479:     return ($datatable,$itemcount);
 3480: }
 3481: 
 3482: sub print_coursedefaults {
 3483:     my ($position,$dom,$settings,$rowtotal) = @_;
 3484:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 3485:     my $itemcount = 1;
 3486:     my %choices =  &Apache::lonlocal::texthash (
 3487:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 3488:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 3489:         coursecredits        => 'Credits can be specified for courses',
 3490:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 3491:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 3492:         postsubmit           => 'Disable submit button/keypress following student submission',
 3493:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 3494:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 3495:     );
 3496:     my %staticdefaults = (
 3497:                            anonsurvey_threshold => 10,
 3498:                            uploadquota          => 500,
 3499:                            postsubmit           => 60,
 3500:                            mysqltables          => 172800,
 3501:                          );
 3502:     if ($position eq 'top') {
 3503:         %defaultchecked = (
 3504:                             'uselcmath'       => 'on',
 3505:                             'usejsme'         => 'on',
 3506:                             'canclone'        => 'none',
 3507:                           );
 3508:         @toggles = ('uselcmath','usejsme');
 3509:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3510:                                                      \%choices,$itemcount);
 3511:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3512:         $datatable .=
 3513:             '<tr'.$css_class.'><td valign="top">'.
 3514:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 3515:             '</span></td><td class="LC_left_item">';
 3516:         my $currcanclone = 'none';
 3517:         my $onclick;
 3518:         my @cloneoptions = ('none','domain');
 3519:         my %clonetitles = (
 3520:                              none     => 'No additional course requesters',
 3521:                              domain   => "Any course requester in course's domain",
 3522:                              instcode => 'Course requests for official courses ...',
 3523:                           );
 3524:         my (%codedefaults,@code_order,@posscodes);
 3525:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 3526:                                                     \@code_order) eq 'ok') {
 3527:             if (@code_order > 0) {
 3528:                 push(@cloneoptions,'instcode');
 3529:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 3530:             }
 3531:         }
 3532:         if (ref($settings) eq 'HASH') {
 3533:             if ($settings->{'canclone'}) {
 3534:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 3535:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 3536:                         if (@code_order > 0) {
 3537:                             $currcanclone = 'instcode';
 3538:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 3539:                         }
 3540:                     }
 3541:                 } elsif ($settings->{'canclone'} eq 'domain') {
 3542:                     $currcanclone = $settings->{'canclone'};
 3543:                 }
 3544:             }
 3545:         }
 3546:         foreach my $option (@cloneoptions) {
 3547:             my ($checked,$additional);
 3548:             if ($currcanclone eq $option) {
 3549:                 $checked = ' checked="checked"';
 3550:             }
 3551:             if ($option eq 'instcode') {
 3552:                 if (@code_order) {
 3553:                     my $show = 'none';
 3554:                     if ($checked) {
 3555:                         $show = 'block';
 3556:                     }
 3557:                     $additional = '<div id="cloneinstcode" style="display:'.$show.'" />'.
 3558:                                   &mt('Institutional codes for new and cloned course have identical:').
 3559:                                   '<br />';
 3560:                     foreach my $item (@code_order) {
 3561:                         my $codechk;
 3562:                         if ($checked) {
 3563:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 3564:                                 $codechk = ' checked="checked"';
 3565:                             }
 3566:                         }
 3567:                         $additional .= '<label>'.
 3568:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 3569:                                        $item.'</label>';
 3570:                     }
 3571:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 3572:                 }
 3573:             }
 3574:             $datatable .=
 3575:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 3576:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 3577:                 '</label>&nbsp;'.$additional.'</span><br />';
 3578:         }
 3579:         $datatable .= '</td>'.
 3580:                       '</tr>';
 3581:         $itemcount ++;
 3582:     } else {
 3583:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3584:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 3585:         my $currusecredits = 0;
 3586:         my $postsubmitclient = 1;
 3587:         my @types = ('official','unofficial','community','textbook');
 3588:         if (ref($settings) eq 'HASH') {
 3589:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 3590:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 3591:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 3592:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 3593:                 }
 3594:             }
 3595:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 3596:                 foreach my $type (@types) {
 3597:                     next if ($type eq 'community');
 3598:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 3599:                     if ($defcredits{$type} ne '') {
 3600:                         $currusecredits = 1;
 3601:                     }
 3602:                 }
 3603:             }
 3604:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 3605:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 3606:                     $postsubmitclient = 0;
 3607:                     foreach my $type (@types) {
 3608:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3609:                     }
 3610:                 } else {
 3611:                     foreach my $type (@types) {
 3612:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 3613:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 3614:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 3615:                             } else {
 3616:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3617:                             }
 3618:                         } else {
 3619:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3620:                         }
 3621:                     }
 3622:                 }
 3623:             } else {
 3624:                 foreach my $type (@types) {
 3625:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3626:                 }
 3627:             }
 3628:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 3629:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 3630:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 3631:                 }
 3632:             } else {
 3633:                 foreach my $type (@types) {
 3634:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 3635:                 }
 3636:             }
 3637:         } else {
 3638:             foreach my $type (@types) {
 3639:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3640:             }
 3641:         }
 3642:         if (!$currdefresponder) {
 3643:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 3644:         } elsif ($currdefresponder < 1) {
 3645:             $currdefresponder = 1;
 3646:         }
 3647:         foreach my $type (@types) {
 3648:             if ($curruploadquota{$type} eq '') {
 3649:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 3650:             }
 3651:         }
 3652:         $datatable .=
 3653:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3654:                 $choices{'anonsurvey_threshold'}.
 3655:                 '</span></td>'.
 3656:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 3657:                 '<input type="text" name="anonsurvey_threshold"'.
 3658:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 3659:                 '</td></tr>'."\n";
 3660:         $itemcount ++;
 3661:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3662:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3663:                       $choices{'uploadquota'}.
 3664:                       '</span></td>'.
 3665:                       '<td align="right" class="LC_right_item">'.
 3666:                       '<table><tr>';
 3667:         foreach my $type (@types) {
 3668:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 3669:                            '<input type="text" name="uploadquota_'.$type.'"'.
 3670:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 3671:         }
 3672:         $datatable .= '</tr></table></td></tr>'."\n";
 3673:         $itemcount ++;
 3674:         my $onclick = "toggleDisplay(this.form,'credits');";
 3675:         my $display = 'none';
 3676:         if ($currusecredits) {
 3677:             $display = 'block';
 3678:         }
 3679:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 3680:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 3681:         foreach my $type (@types) {
 3682:             next if ($type eq 'community');
 3683:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 3684:                            '<input type="text" name="'.$type.'_credits"'.
 3685:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 3686:         }
 3687:         $additional .= '</tr></table></div>'."\n";
 3688:         %defaultchecked = ('coursecredits' => 'off');
 3689:         @toggles = ('coursecredits');
 3690:         my $current = {
 3691:                         'coursecredits' => $currusecredits,
 3692:                       };
 3693:         (my $table,$itemcount) =
 3694:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 3695:                                \%choices,$itemcount,$onclick,$additional,'left');
 3696:         $datatable .= $table;
 3697:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 3698:         my $display = 'none';
 3699:         if ($postsubmitclient) {
 3700:             $display = 'block';
 3701:         }
 3702:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 3703:                       &mt('Number of seconds submit is disabled').'<br />'.
 3704:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 3705:                       '<table><tr>';
 3706:         foreach my $type (@types) {
 3707:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 3708:                            '<input type="text" name="'.$type.'_timeout" value="'.
 3709:                            $deftimeout{$type}.'" size="5" /></td>';
 3710:         }
 3711:         $additional .= '</tr></table></div>'."\n";
 3712:         %defaultchecked = ('postsubmit' => 'on');
 3713:         @toggles = ('postsubmit');
 3714:         $current = {
 3715:                        'postsubmit' => $postsubmitclient,
 3716:                    };
 3717:         ($table,$itemcount) =
 3718:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 3719:                                \%choices,$itemcount,$onclick,$additional,'left');
 3720:         $datatable .= $table;
 3721:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3722:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3723:                       $choices{'mysqltables'}.
 3724:                       '</span></td>'.
 3725:                       '<td align="right" class="LC_right_item">'.
 3726:                       '<table><tr>';
 3727:         foreach my $type (@types) {
 3728:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 3729:                            '<input type="text" name="mysqltables_'.$type.'"'.
 3730:                            ' value="'.$currmysql{$type}.'" size="5" /></td>';
 3731:         }
 3732:         $datatable .= '</tr></table></td></tr>'."\n";
 3733:         $itemcount ++;
 3734: 
 3735:     }
 3736:     $$rowtotal += $itemcount;
 3737:     return $datatable;
 3738: }
 3739: 
 3740: sub print_selfenrollment {
 3741:     my ($position,$dom,$settings,$rowtotal) = @_;
 3742:     my ($css_class,$datatable);
 3743:     my $itemcount = 1;
 3744:     my @types = ('official','unofficial','community','textbook');
 3745:     if (($position eq 'top') || ($position eq 'middle')) {
 3746:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 3747:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 3748:         my @rows;
 3749:         my $key;
 3750:         if ($position eq 'top') {
 3751:             $key = 'admin'; 
 3752:             if (ref($rowsref) eq 'ARRAY') {
 3753:                 @rows = @{$rowsref};
 3754:             }
 3755:         } elsif ($position eq 'middle') {
 3756:             $key = 'default';
 3757:             @rows = ('types','registered','approval','limit');
 3758:         }
 3759:         foreach my $row (@rows) {
 3760:             if (defined($titlesref->{$row})) {
 3761:                 $itemcount ++;
 3762:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3763:                 $datatable .= '<tr'.$css_class.'>'.
 3764:                               '<td>'.$titlesref->{$row}.'</td>'.
 3765:                               '<td class="LC_left_item">'.
 3766:                               '<table><tr>';
 3767:                 my (%current,%currentcap);
 3768:                 if (ref($settings) eq 'HASH') {
 3769:                     if (ref($settings->{$key}) eq 'HASH') {
 3770:                         foreach my $type (@types) {
 3771:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 3772:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 3773:                             }
 3774:                             if (($row eq 'limit') && ($key eq 'default')) {
 3775:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 3776:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 3777:                                 }
 3778:                             }
 3779:                         }
 3780:                     }
 3781:                 }
 3782:                 my %roles = (
 3783:                              '0' => &Apache::lonnet::plaintext('dc'),
 3784:                             ); 
 3785:             
 3786:                 foreach my $type (@types) {
 3787:                     unless (($row eq 'registered') && ($key eq 'default')) {
 3788:                         $datatable .= '<th>'.&mt($type).'</th>';
 3789:                     }
 3790:                 }
 3791:                 unless (($row eq 'registered') && ($key eq 'default')) {
 3792:                     $datatable .= '</tr><tr>';
 3793:                 }
 3794:                 foreach my $type (@types) {
 3795:                     if ($type eq 'community') {
 3796:                         $roles{'1'} = &mt('Community personnel');
 3797:                     } else {
 3798:                         $roles{'1'} = &mt('Course personnel');
 3799:                     }
 3800:                     $datatable .= '<td style="vertical-align: top">';
 3801:                     if ($position eq 'top') {
 3802:                         my %checked;
 3803:                         if ($current{$type} eq '0') {
 3804:                             $checked{'0'} = ' checked="checked"';
 3805:                         } else {
 3806:                             $checked{'1'} = ' checked="checked"';
 3807:                         }
 3808:                         foreach my $role ('1','0') {
 3809:                             $datatable .= '<span class="LC_nobreak"><label>'.
 3810:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 3811:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 3812:                                           $roles{$role}.'</label></span> ';
 3813:                         }
 3814:                     } else {
 3815:                         if ($row eq 'types') {
 3816:                             my %checked;
 3817:                             if ($current{$type} =~ /^(all|dom)$/) {
 3818:                                 $checked{$1} = ' checked="checked"';
 3819:                             } else {
 3820:                                 $checked{''} = ' checked="checked"';
 3821:                             }
 3822:                             foreach my $val ('','dom','all') {
 3823:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3824:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3825:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3826:                             }
 3827:                         } elsif ($row eq 'registered') {
 3828:                             my %checked;
 3829:                             if ($current{$type} eq '1') {
 3830:                                 $checked{'1'} = ' checked="checked"';
 3831:                             } else {
 3832:                                 $checked{'0'} = ' checked="checked"';
 3833:                             }
 3834:                             foreach my $val ('0','1') {
 3835:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3836:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3837:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3838:                             }
 3839:                         } elsif ($row eq 'approval') {
 3840:                             my %checked;
 3841:                             if ($current{$type} =~ /^([12])$/) {
 3842:                                 $checked{$1} = ' checked="checked"';
 3843:                             } else {
 3844:                                 $checked{'0'} = ' checked="checked"';
 3845:                             }
 3846:                             for my $val (0..2) {
 3847:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3848:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3849:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3850:                             }
 3851:                         } elsif ($row eq 'limit') {
 3852:                             my %checked;
 3853:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 3854:                                 $checked{$1} = ' checked="checked"';
 3855:                             } else {
 3856:                                 $checked{'none'} = ' checked="checked"';
 3857:                             }
 3858:                             my $cap;
 3859:                             if ($currentcap{$type} =~ /^\d+$/) {
 3860:                                 $cap = $currentcap{$type};
 3861:                             }
 3862:                             foreach my $val ('none','allstudents','selfenrolled') {
 3863:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3864:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3865:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3866:                             }
 3867:                             $datatable .= '<br />'.
 3868:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 3869:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 3870:                                           '</span>'; 
 3871:                         }
 3872:                     }
 3873:                     $datatable .= '</td>';
 3874:                 }
 3875:                 $datatable .= '</tr>';
 3876:             }
 3877:             $datatable .= '</table></td></tr>';
 3878:         }
 3879:     } elsif ($position eq 'bottom') {
 3880:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 3881:     }
 3882:     $$rowtotal += $itemcount;
 3883:     return $datatable;
 3884: }
 3885: 
 3886: sub print_validation_rows {
 3887:     my ($caller,$dom,$settings,$rowtotal) = @_;
 3888:     my ($itemsref,$namesref,$fieldsref);
 3889:     if ($caller eq 'selfenroll') { 
 3890:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 3891:     } elsif ($caller eq 'requestcourses') {
 3892:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 3893:     }
 3894:     my %currvalidation;
 3895:     if (ref($settings) eq 'HASH') {
 3896:         if (ref($settings->{'validation'}) eq 'HASH') {
 3897:             %currvalidation = %{$settings->{'validation'}};
 3898:         }
 3899:     }
 3900:     my $datatable;
 3901:     my $itemcount = 0;
 3902:     foreach my $item (@{$itemsref}) {
 3903:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3904:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3905:                       $namesref->{$item}.
 3906:                       '</span></td>'.
 3907:                       '<td class="LC_left_item">';
 3908:         if (($item eq 'url') || ($item eq 'button')) {
 3909:             $datatable .= '<span class="LC_nobreak">'.
 3910:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 3911:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 3912:         } elsif ($item eq 'fields') {
 3913:             my @currfields;
 3914:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 3915:                 @currfields = @{$currvalidation{$item}};
 3916:             }
 3917:             foreach my $field (@{$fieldsref}) {
 3918:                 my $check = '';
 3919:                 if (grep(/^\Q$field\E$/,@currfields)) {
 3920:                     $check = ' checked="checked"';
 3921:                 }
 3922:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3923:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 3924:                               ' value="'.$field.'"'.$check.' />'.$field.
 3925:                               '</label></span> ';
 3926:             }
 3927:         } elsif ($item eq 'markup') {
 3928:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5" wrap="soft">'.
 3929:                            $currvalidation{$item}.
 3930:                               '</textarea>';
 3931:         }
 3932:         $datatable .= '</td></tr>'."\n";
 3933:         if (ref($rowtotal)) {
 3934:             $itemcount ++;
 3935:         }
 3936:     }
 3937:     if ($caller eq 'requestcourses') {
 3938:         my %currhash;
 3939:         if (ref($settings) eq 'HASH') {
 3940:             if (ref($settings->{'validation'}) eq 'HASH') {
 3941:                 if ($settings->{'validation'}{'dc'} ne '') {
 3942:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 3943:                 }
 3944:             }
 3945:         }
 3946:         my $numinrow = 2;
 3947:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 3948:                                                        'validationdc',%currhash);
 3949:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3950:         $datatable .= '</td></tr><tr'.$css_class.'><td>';
 3951:         if ($numdc > 1) {
 3952:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 3953:         } else {
 3954:             $datatable .=  &mt('Course creation processed as: ');
 3955:         }
 3956:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 3957:         $itemcount ++;
 3958:     }
 3959:     if (ref($rowtotal)) {
 3960:         $$rowtotal += $itemcount;
 3961:     }
 3962:     return $datatable;
 3963: }
 3964: 
 3965: sub print_usersessions {
 3966:     my ($position,$dom,$settings,$rowtotal) = @_;
 3967:     my ($css_class,$datatable,%checked,%choices);
 3968:     my (%by_ip,%by_location,@intdoms);
 3969:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 3970: 
 3971:     my @alldoms = &Apache::lonnet::all_domains();
 3972:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 3973:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3974:     my %altids = &id_for_thisdom(%servers);
 3975:     my $itemcount = 1;
 3976:     if ($position eq 'top') {
 3977:         if (keys(%serverhomes) > 1) {
 3978:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 3979:             my $curroffloadnow;
 3980:             if (ref($settings) eq 'HASH') {
 3981:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 3982:                     $curroffloadnow = $settings->{'offloadnow'};
 3983:                 }
 3984:             }
 3985:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$curroffloadnow,$rowtotal);
 3986:         } else {
 3987:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3988:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 3989:         }
 3990:     } else {
 3991:         if (keys(%by_location) == 0) {
 3992:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3993:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 3994:         } else {
 3995:             my %lt = &usersession_titles();
 3996:             my $numinrow = 5;
 3997:             my $prefix;
 3998:             my @types;
 3999:             if ($position eq 'bottom') {
 4000:                 $prefix = 'remote';
 4001:                 @types = ('version','excludedomain','includedomain');
 4002:             } else {
 4003:                 $prefix = 'hosted';
 4004:                 @types = ('excludedomain','includedomain');
 4005:             }
 4006:             my (%current,%checkedon,%checkedoff);
 4007:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 4008:             my @locations = sort(keys(%by_location));
 4009:             foreach my $type (@types) {
 4010:                 $checkedon{$type} = '';
 4011:                 $checkedoff{$type} = ' checked="checked"';
 4012:             }
 4013:             if (ref($settings) eq 'HASH') {
 4014:                 if (ref($settings->{$prefix}) eq 'HASH') {
 4015:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 4016:                         $current{$key} = $settings->{$prefix}{$key};
 4017:                         if ($key eq 'version') {
 4018:                             if ($current{$key} ne '') {
 4019:                                 $checkedon{$key} = ' checked="checked"';
 4020:                                 $checkedoff{$key} = '';
 4021:                             }
 4022:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 4023:                             $checkedon{$key} = ' checked="checked"';
 4024:                             $checkedoff{$key} = '';
 4025:                         }
 4026:                     }
 4027:                 }
 4028:             }
 4029:             foreach my $type (@types) {
 4030:                 next if ($type ne 'version' && !@locations);
 4031:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4032:                 $datatable .= '<tr'.$css_class.'>
 4033:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 4034:                                <span class="LC_nobreak">&nbsp;
 4035:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 4036:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 4037:                 if ($type eq 'version') {
 4038:                     my $selector = '<select name="'.$prefix.'_version">';
 4039:                     foreach my $version (@lcversions) {
 4040:                         my $selected = '';
 4041:                         if ($current{'version'} eq $version) {
 4042:                             $selected = ' selected="selected"';
 4043:                         }
 4044:                         $selector .= ' <option value="'.$version.'"'.
 4045:                                      $selected.'>'.$version.'</option>';
 4046:                     }
 4047:                     $selector .= '</select> ';
 4048:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 4049:                 } else {
 4050:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 4051:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 4052:                                  ' />'.('&nbsp;'x2).
 4053:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 4054:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 4055:                                  "\n".
 4056:                                  '</div><div><table>';
 4057:                     my $rem;
 4058:                     for (my $i=0; $i<@locations; $i++) {
 4059:                         my ($showloc,$value,$checkedtype);
 4060:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 4061:                             my $ip = $by_location{$locations[$i]}->[0];
 4062:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 4063:                                  $value = join(':',@{$by_ip{$ip}});
 4064:                                 $showloc = join(', ',@{$by_ip{$ip}});
 4065:                                 if (ref($current{$type}) eq 'ARRAY') {
 4066:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 4067:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 4068:                                             $checkedtype = ' checked="checked"';
 4069:                                             last;
 4070:                                         }
 4071:                                     }
 4072:                                 }
 4073:                             }
 4074:                         }
 4075:                         $rem = $i%($numinrow);
 4076:                         if ($rem == 0) {
 4077:                             if ($i > 0) {
 4078:                                 $datatable .= '</tr>';
 4079:                             }
 4080:                             $datatable .= '<tr>';
 4081:                         }
 4082:                         $datatable .= '<td class="LC_left_item">'.
 4083:                                       '<span class="LC_nobreak"><label>'.
 4084:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 4085:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 4086:                                       '</label></span></td>';
 4087:                     }
 4088:                     $rem = @locations%($numinrow);
 4089:                     my $colsleft = $numinrow - $rem;
 4090:                     if ($colsleft > 1 ) {
 4091:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4092:                                       '&nbsp;</td>';
 4093:                     } elsif ($colsleft == 1) {
 4094:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4095:                     }
 4096:                     $datatable .= '</tr></table>';
 4097:                 }
 4098:                 $datatable .= '</td></tr>';
 4099:                 $itemcount ++;
 4100:             }
 4101:         }
 4102:     }
 4103:     $$rowtotal += $itemcount;
 4104:     return $datatable;
 4105: }
 4106: 
 4107: sub build_location_hashes {
 4108:     my ($intdoms,$by_ip,$by_location) = @_;
 4109:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 4110:                   (ref($by_location) eq 'HASH')); 
 4111:     my %iphost = &Apache::lonnet::get_iphost();
 4112:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 4113:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 4114:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 4115:         foreach my $id (@{$iphost{$primary_ip}}) {
 4116:             my $intdom = &Apache::lonnet::internet_dom($id);
 4117:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 4118:                 push(@{$intdoms},$intdom);
 4119:             }
 4120:         }
 4121:     }
 4122:     foreach my $ip (keys(%iphost)) {
 4123:         if (ref($iphost{$ip}) eq 'ARRAY') {
 4124:             foreach my $id (@{$iphost{$ip}}) {
 4125:                 my $location = &Apache::lonnet::internet_dom($id);
 4126:                 if ($location) {
 4127:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 4128:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4129:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 4130:                             push(@{$by_ip->{$ip}},$location);
 4131:                         }
 4132:                     } else {
 4133:                         $by_ip->{$ip} = [$location];
 4134:                     }
 4135:                 }
 4136:             }
 4137:         }
 4138:     }
 4139:     foreach my $ip (sort(keys(%{$by_ip}))) {
 4140:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4141:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 4142:             my $first = $by_ip->{$ip}->[0];
 4143:             if (ref($by_location->{$first}) eq 'ARRAY') {
 4144:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 4145:                     push(@{$by_location->{$first}},$ip);
 4146:                 }
 4147:             } else {
 4148:                 $by_location->{$first} = [$ip];
 4149:             }
 4150:         }
 4151:     }
 4152:     return;
 4153: }
 4154: 
 4155: sub current_offloads_to {
 4156:     my ($dom,$settings,$servers) = @_;
 4157:     my (%spareid,%otherdomconfigs);
 4158:     if (ref($servers) eq 'HASH') {
 4159:         foreach my $lonhost (sort(keys(%{$servers}))) {
 4160:             my $gotspares;
 4161:             if (ref($settings) eq 'HASH') {
 4162:                 if (ref($settings->{'spares'}) eq 'HASH') {
 4163:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 4164:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 4165:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 4166:                         $gotspares = 1;
 4167:                     }
 4168:                 }
 4169:             }
 4170:             unless ($gotspares) {
 4171:                 my $gotspares;
 4172:                 my $serverhomeID =
 4173:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 4174:                 my $serverhomedom =
 4175:                     &Apache::lonnet::host_domain($serverhomeID);
 4176:                 if ($serverhomedom ne $dom) {
 4177:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 4178:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4179:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4180:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4181:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4182:                                 $gotspares = 1;
 4183:                             }
 4184:                         }
 4185:                     } else {
 4186:                         $otherdomconfigs{$serverhomedom} =
 4187:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 4188:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 4189:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4190:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4191:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 4192:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4193:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4194:                                         $gotspares = 1;
 4195:                                     }
 4196:                                 }
 4197:                             }
 4198:                         }
 4199:                     }
 4200:                 }
 4201:             }
 4202:             unless ($gotspares) {
 4203:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4204:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4205:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4206:                } else {
 4207:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 4208:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 4209:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4210:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4211:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4212:                     } else {
 4213:                         my %what = (
 4214:                              spareid => 1,
 4215:                         );
 4216:                         my ($result,$returnhash) = 
 4217:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 4218:                         if ($result eq 'ok') { 
 4219:                             if (ref($returnhash) eq 'HASH') {
 4220:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 4221:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 4222:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 4223:                                 }
 4224:                             }
 4225:                         }
 4226:                     }
 4227:                 }
 4228:             }
 4229:         }
 4230:     }
 4231:     return %spareid;
 4232: }
 4233: 
 4234: sub spares_row {
 4235:     my ($dom,$servers,$spareid,$serverhomes,$altids,$curroffloadnow,$rowtotal) = @_;
 4236:     my $css_class;
 4237:     my $numinrow = 4;
 4238:     my $itemcount = 1;
 4239:     my $datatable;
 4240:     my %typetitles = &sparestype_titles();
 4241:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 4242:         foreach my $server (sort(keys(%{$servers}))) {
 4243:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 4244:             my ($othercontrol,$serverdom);
 4245:             if ($serverhome ne $server) {
 4246:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 4247:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 4248:             } else {
 4249:                 $serverdom = &Apache::lonnet::host_domain($server);
 4250:                 if ($serverdom ne $dom) {
 4251:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 4252:                 }
 4253:             }
 4254:             next unless (ref($spareid->{$server}) eq 'HASH');
 4255:             my $checkednow;
 4256:             if (ref($curroffloadnow) eq 'HASH') {
 4257:                 if ($curroffloadnow->{$server}) {
 4258:                     $checkednow = ' checked="checked"';
 4259:                 }
 4260:             }
 4261:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4262:             $datatable .= '<tr'.$css_class.'>
 4263:                            <td rowspan="2">
 4264:                             <span class="LC_nobreak">'.
 4265:                           &mt('[_1] when busy, offloads to:'
 4266:                               ,'<b>'.$server.'</b>').'</span><br />'.
 4267:                           '<span class="LC_nobreak">'."\n".
 4268:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 4269:                           '&nbsp;'.&mt('Switch active users on next access').'</label></span>'.
 4270:                           "\n";
 4271:             my (%current,%canselect);
 4272:             my @choices = 
 4273:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 4274:             foreach my $type ('primary','default') {
 4275:                 if (ref($spareid->{$server}) eq 'HASH') {
 4276:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 4277:                         my @spares = @{$spareid->{$server}{$type}};
 4278:                         if (@spares > 0) {
 4279:                             if ($othercontrol) {
 4280:                                 $current{$type} = join(', ',@spares);
 4281:                             } else {
 4282:                                 $current{$type} .= '<table>';
 4283:                                 my $numspares = scalar(@spares);
 4284:                                 for (my $i=0;  $i<@spares; $i++) {
 4285:                                     my $rem = $i%($numinrow);
 4286:                                     if ($rem == 0) {
 4287:                                         if ($i > 0) {
 4288:                                             $current{$type} .= '</tr>';
 4289:                                         }
 4290:                                         $current{$type} .= '<tr>';
 4291:                                     }
 4292:                                     $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;'.
 4293:                                                        $spareid->{$server}{$type}[$i].
 4294:                                                        '</label></td>'."\n";
 4295:                                 }
 4296:                                 my $rem = @spares%($numinrow);
 4297:                                 my $colsleft = $numinrow - $rem;
 4298:                                 if ($colsleft > 1 ) {
 4299:                                     $current{$type} .= '<td colspan="'.$colsleft.
 4300:                                                        '" class="LC_left_item">'.
 4301:                                                        '&nbsp;</td>';
 4302:                                 } elsif ($colsleft == 1) {
 4303:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 4304:                                 }
 4305:                                 $current{$type} .= '</tr></table>';
 4306:                             }
 4307:                         }
 4308:                     }
 4309:                     if ($current{$type} eq '') {
 4310:                         $current{$type} = &mt('None specified');
 4311:                     }
 4312:                     if ($othercontrol) {
 4313:                         if ($type eq 'primary') {
 4314:                             $canselect{$type} = $othercontrol;
 4315:                         }
 4316:                     } else {
 4317:                         $canselect{$type} = 
 4318:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 4319:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 4320:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 4321:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 4322:                         if (@choices > 0) {
 4323:                             foreach my $lonhost (@choices) {
 4324:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 4325:                             }
 4326:                         }
 4327:                         $canselect{$type} .= '</select>'."\n";
 4328:                     }
 4329:                 } else {
 4330:                     $current{$type} = &mt('Could not be determined');
 4331:                     if ($type eq 'primary') {
 4332:                         $canselect{$type} =  $othercontrol;
 4333:                     }
 4334:                 }
 4335:                 if ($type eq 'default') {
 4336:                     $datatable .= '<tr'.$css_class.'>';
 4337:                 }
 4338:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 4339:                               '<td>'.$current{$type}.'</td>'."\n".
 4340:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 4341:             }
 4342:             $itemcount ++;
 4343:         }
 4344:     }
 4345:     $$rowtotal += $itemcount;
 4346:     return $datatable;
 4347: }
 4348: 
 4349: sub possible_newspares {
 4350:     my ($server,$currspares,$serverhomes,$altids) = @_;
 4351:     my $serverhostname = &Apache::lonnet::hostname($server);
 4352:     my %excluded;
 4353:     if ($serverhostname ne '') {
 4354:         %excluded = (
 4355:                        $serverhostname => 1,
 4356:                     );
 4357:     }
 4358:     if (ref($currspares) eq 'HASH') {
 4359:         foreach my $type (keys(%{$currspares})) {
 4360:             if (ref($currspares->{$type}) eq 'ARRAY') {
 4361:                 if (@{$currspares->{$type}} > 0) {
 4362:                     foreach my $curr (@{$currspares->{$type}}) {
 4363:                         my $hostname = &Apache::lonnet::hostname($curr);
 4364:                         $excluded{$hostname} = 1;
 4365:                     }
 4366:                 }
 4367:             }
 4368:         }
 4369:     }
 4370:     my @choices;
 4371:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 4372:         if (keys(%{$serverhomes}) > 1) {
 4373:             foreach my $name (sort(keys(%{$serverhomes}))) {
 4374:                 unless ($excluded{$name}) {
 4375:                     if (exists($altids->{$serverhomes->{$name}})) {
 4376:                         push(@choices,$altids->{$serverhomes->{$name}});
 4377:                     } else {
 4378:                         push(@choices,$serverhomes->{$name});
 4379:                     }
 4380:                 }
 4381:             }
 4382:         }
 4383:     }
 4384:     return sort(@choices);
 4385: }
 4386: 
 4387: sub print_loadbalancing {
 4388:     my ($dom,$settings,$rowtotal) = @_;
 4389:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 4390:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 4391:     my $numinrow = 1;
 4392:     my $datatable;
 4393:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4394:     my (%currbalancer,%currtargets,%currrules,%existing);
 4395:     if (ref($settings) eq 'HASH') {
 4396:         %existing = %{$settings};
 4397:     }
 4398:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 4399:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 4400:                                   \%currtargets,\%currrules);
 4401:     } else {
 4402:         return;
 4403:     }
 4404:     my ($othertitle,$usertypes,$types) =
 4405:         &Apache::loncommon::sorted_inst_types($dom);
 4406:     my $rownum = 8;
 4407:     if (ref($types) eq 'ARRAY') {
 4408:         $rownum += scalar(@{$types});
 4409:     }
 4410:     my @css_class = ('LC_odd_row','LC_even_row');
 4411:     my $balnum = 0;
 4412:     my $islast;
 4413:     my (@toshow,$disabledtext);
 4414:     if (keys(%currbalancer) > 0) {
 4415:         @toshow = sort(keys(%currbalancer));
 4416:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 4417:             push(@toshow,'');
 4418:         }
 4419:     } else {
 4420:         @toshow = ('');
 4421:         $disabledtext = &mt('No existing load balancer');
 4422:     }
 4423:     foreach my $lonhost (@toshow) {
 4424:         if ($balnum == scalar(@toshow)-1) {
 4425:             $islast = 1;
 4426:         } else {
 4427:             $islast = 0;
 4428:         }
 4429:         my $cssidx = $balnum%2;
 4430:         my $targets_div_style = 'display: none';
 4431:         my $disabled_div_style = 'display: block';
 4432:         my $homedom_div_style = 'display: none';
 4433:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 4434:                       '<td rowspan="'.$rownum.'" valign="top">'.
 4435:                       '<p>';
 4436:         if ($lonhost eq '') {
 4437:             $datatable .= '<span class="LC_nobreak">';
 4438:             if (keys(%currbalancer) > 0) {
 4439:                 $datatable .= &mt('Add balancer:');
 4440:             } else {
 4441:                 $datatable .= &mt('Enable balancer:');
 4442:             }
 4443:             $datatable .= '&nbsp;'.
 4444:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 4445:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 4446:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 4447:                           '<option value="" selected="selected">'.&mt('None').
 4448:                           '</option>'."\n";
 4449:             foreach my $server (sort(keys(%servers))) {
 4450:                 next if ($currbalancer{$server});
 4451:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 4452:             }
 4453:             $datatable .=
 4454:                 '</select>'."\n".
 4455:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 4456:         } else {
 4457:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 4458:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 4459:                            &mt('Stop balancing').'</label>'.
 4460:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 4461:             $targets_div_style = 'display: block';
 4462:             $disabled_div_style = 'display: none';
 4463:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 4464:                 $homedom_div_style = 'display: block';
 4465:             }
 4466:         }
 4467:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 4468:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 4469:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 4470:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 4471:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 4472:         my @sparestypes = ('primary','default');
 4473:         my %typetitles = &sparestype_titles();
 4474:         my %hostherechecked = (
 4475:                                   no => ' checked="checked"',
 4476:                               );
 4477:         foreach my $sparetype (@sparestypes) {
 4478:             my $targettable;
 4479:             for (my $i=0; $i<$numspares; $i++) {
 4480:                 my $checked;
 4481:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 4482:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 4483:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 4484:                             $checked = ' checked="checked"';
 4485:                         }
 4486:                     }
 4487:                 }
 4488:                 my ($chkboxval,$disabled);
 4489:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 4490:                     $chkboxval = $spares[$i];
 4491:                 }
 4492:                 if (exists($currbalancer{$spares[$i]})) {
 4493:                     $disabled = ' disabled="disabled"';
 4494:                 }
 4495:                 $targettable .=
 4496:                     '<td><span class="LC_nobreak"><label>'.
 4497:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 4498:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 4499:                     '</span></label></span></td>';
 4500:                 my $rem = $i%($numinrow);
 4501:                 if ($rem == 0) {
 4502:                     if (($i > 0) && ($i < $numspares-1)) {
 4503:                         $targettable .= '</tr>';
 4504:                     }
 4505:                     if ($i < $numspares-1) {
 4506:                         $targettable .= '<tr>';
 4507:                     }
 4508:                 }
 4509:             }
 4510:             if ($targettable ne '') {
 4511:                 my $rem = $numspares%($numinrow);
 4512:                 my $colsleft = $numinrow - $rem;
 4513:                 if ($colsleft > 1 ) {
 4514:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4515:                                     '&nbsp;</td>';
 4516:                 } elsif ($colsleft == 1) {
 4517:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 4518:                 }
 4519:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 4520:                                '<table><tr>'.$targettable.'</tr></table><br />';
 4521:             }
 4522:             $hostherechecked{$sparetype} = '';
 4523:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 4524:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 4525:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 4526:                         $hostherechecked{$sparetype} = ' checked="checked"';
 4527:                         $hostherechecked{'no'} = '';
 4528:                     }
 4529:                 }
 4530:             }
 4531:         }
 4532:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 4533:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 4534:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 4535:         foreach my $sparetype (@sparestypes) {
 4536:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 4537:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 4538:                           '</i></label><br />';
 4539:         }
 4540:         $datatable .= '</div></td></tr>'.
 4541:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 4542:                                            $othertitle,$usertypes,$types,\%servers,
 4543:                                            \%currbalancer,$lonhost,
 4544:                                            $targets_div_style,$homedom_div_style,
 4545:                                            $css_class[$cssidx],$balnum,$islast);
 4546:         $$rowtotal += $rownum;
 4547:         $balnum ++;
 4548:     }
 4549:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 4550:     return $datatable;
 4551: }
 4552: 
 4553: sub get_loadbalancers_config {
 4554:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 4555:     return unless ((ref($servers) eq 'HASH') &&
 4556:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 4557:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 4558:     if (keys(%{$existing}) > 0) {
 4559:         my $oldlonhost;
 4560:         foreach my $key (sort(keys(%{$existing}))) {
 4561:             if ($key eq 'lonhost') {
 4562:                 $oldlonhost = $existing->{'lonhost'};
 4563:                 $currbalancer->{$oldlonhost} = 1;
 4564:             } elsif ($key eq 'targets') {
 4565:                 if ($oldlonhost) {
 4566:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 4567:                 }
 4568:             } elsif ($key eq 'rules') {
 4569:                 if ($oldlonhost) {
 4570:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 4571:                 }
 4572:             } elsif (ref($existing->{$key}) eq 'HASH') {
 4573:                 $currbalancer->{$key} = 1;
 4574:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 4575:                 $currrules->{$key} = $existing->{$key}{'rules'};
 4576:             }
 4577:         }
 4578:     } else {
 4579:         my ($balancerref,$targetsref) =
 4580:                 &Apache::lonnet::get_lonbalancer_config($servers);
 4581:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 4582:             foreach my $server (sort(keys(%{$balancerref}))) {
 4583:                 $currbalancer->{$server} = 1;
 4584:                 $currtargets->{$server} = $targetsref->{$server};
 4585:             }
 4586:         }
 4587:     }
 4588:     return;
 4589: }
 4590: 
 4591: sub loadbalancing_rules {
 4592:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 4593:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 4594:         $css_class,$balnum,$islast) = @_;
 4595:     my $output;
 4596:     my $num = 0;
 4597:     my ($alltypes,$othertypes,$titles) =
 4598:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 4599:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 4600:         foreach my $type (@{$alltypes}) {
 4601:             $num ++;
 4602:             my $current;
 4603:             if (ref($currrules) eq 'HASH') {
 4604:                 $current = $currrules->{$type};
 4605:             }
 4606:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 4607:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 4608:                     $current = '';
 4609:                 }
 4610:             }
 4611:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 4612:                                              $servers,$currbalancer,$lonhost,$dom,
 4613:                                              $targets_div_style,$homedom_div_style,
 4614:                                              $css_class,$balnum,$num,$islast);
 4615:         }
 4616:     }
 4617:     return $output;
 4618: }
 4619: 
 4620: sub loadbalancing_titles {
 4621:     my ($dom,$intdom,$usertypes,$types) = @_;
 4622:     my %othertypes = (
 4623:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 4624:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 4625:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 4626:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 4627:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 4628:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 4629:                      );
 4630:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 4631:     if (ref($types) eq 'ARRAY') {
 4632:         unshift(@alltypes,@{$types},'default');
 4633:     }
 4634:     my %titles;
 4635:     foreach my $type (@alltypes) {
 4636:         if ($type =~ /^_LC_/) {
 4637:             $titles{$type} = $othertypes{$type};
 4638:         } elsif ($type eq 'default') {
 4639:             $titles{$type} = &mt('All users from [_1]',$dom);
 4640:             if (ref($types) eq 'ARRAY') {
 4641:                 if (@{$types} > 0) {
 4642:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 4643:                 }
 4644:             }
 4645:         } elsif (ref($usertypes) eq 'HASH') {
 4646:             $titles{$type} = $usertypes->{$type};
 4647:         }
 4648:     }
 4649:     return (\@alltypes,\%othertypes,\%titles);
 4650: }
 4651: 
 4652: sub loadbalance_rule_row {
 4653:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 4654:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 4655:     my @rulenames;
 4656:     my %ruletitles = &offloadtype_text();
 4657:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 4658:         @rulenames = ('balancer','offloadedto','specific');
 4659:     } else {
 4660:         @rulenames = ('default','homeserver');
 4661:         if ($type eq '_LC_external') {
 4662:             push(@rulenames,'externalbalancer');
 4663:         } else {
 4664:             push(@rulenames,'specific');
 4665:         }
 4666:         push(@rulenames,'none');
 4667:     }
 4668:     my $style = $targets_div_style;
 4669:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 4670:         $style = $homedom_div_style;
 4671:     }
 4672:     my $space;
 4673:     if ($islast && $num == 1) {
 4674:         $space = '<div display="inline-block">&nbsp;</div>';
 4675:     }
 4676:     my $output =
 4677:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 4678:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 4679:         '<td valaign="top">'.$space.
 4680:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 4681:     for (my $i=0; $i<@rulenames; $i++) {
 4682:         my $rule = $rulenames[$i];
 4683:         my ($checked,$extra);
 4684:         if ($rulenames[$i] eq 'default') {
 4685:             $rule = '';
 4686:         }
 4687:         if ($rulenames[$i] eq 'specific') {
 4688:             if (ref($servers) eq 'HASH') {
 4689:                 my $default;
 4690:                 if (($current ne '') && (exists($servers->{$current}))) {
 4691:                     $checked = ' checked="checked"';
 4692:                 }
 4693:                 unless ($checked) {
 4694:                     $default = ' selected="selected"';
 4695:                 }
 4696:                 $extra =
 4697:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 4698:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 4699:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 4700:                     '<option value=""'.$default.'></option>'."\n";
 4701:                 foreach my $server (sort(keys(%{$servers}))) {
 4702:                     if (ref($currbalancer) eq 'HASH') {
 4703:                         next if (exists($currbalancer->{$server}));
 4704:                     }
 4705:                     my $selected;
 4706:                     if ($server eq $current) {
 4707:                         $selected = ' selected="selected"';
 4708:                     }
 4709:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 4710:                 }
 4711:                 $extra .= '</select>';
 4712:             }
 4713:         } elsif ($rule eq $current) {
 4714:             $checked = ' checked="checked"';
 4715:         }
 4716:         $output .= '<span class="LC_nobreak"><label>'.
 4717:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 4718:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 4719:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 4720:                    ')"'.$checked.' />&nbsp;';
 4721:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 4722:             $output .= $ruletitles{'particular'};
 4723:         } else {
 4724:             $output .= $ruletitles{$rulenames[$i]};
 4725:         }
 4726:         $output .= '</label>'.$extra.'</span><br />'."\n";
 4727:     }
 4728:     $output .= '</div></td></tr>'."\n";
 4729:     return $output;
 4730: }
 4731: 
 4732: sub offloadtype_text {
 4733:     my %ruletitles = &Apache::lonlocal::texthash (
 4734:            'default'          => 'Offloads to default destinations',
 4735:            'homeserver'       => "Offloads to user's home server",
 4736:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 4737:            'specific'         => 'Offloads to specific server',
 4738:            'none'             => 'No offload',
 4739:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 4740:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 4741:            'particular'       => 'Session hosted (after re-auth) on server:',
 4742:     );
 4743:     return %ruletitles;
 4744: }
 4745: 
 4746: sub sparestype_titles {
 4747:     my %typestitles = &Apache::lonlocal::texthash (
 4748:                           'primary' => 'primary',
 4749:                           'default' => 'default',
 4750:                       );
 4751:     return %typestitles;
 4752: }
 4753: 
 4754: sub contact_titles {
 4755:     my %titles = &Apache::lonlocal::texthash (
 4756:                    'supportemail'    => 'Support E-mail address',
 4757:                    'adminemail'      => 'Default Server Admin E-mail address',
 4758:                    'errormail'       => 'Error reports to be e-mailed to',
 4759:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 4760:                    'helpdeskmail'    => "Helpdesk requests for this domain's users",
 4761:                    'otherdomsmail'   => 'Helpdesk requests for other (unconfigured) domains',
 4762:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 4763:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 4764:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 4765:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 4766:                  );
 4767:     my %short_titles = &Apache::lonlocal::texthash (
 4768:                            adminemail   => 'Admin E-mail address',
 4769:                            supportemail => 'Support E-mail',
 4770:                        );   
 4771:     return (\%titles,\%short_titles);
 4772: }
 4773: 
 4774: sub helpform_fields {
 4775:     my %titles =  &Apache::lonlocal::texthash (
 4776:                        'username'   => 'Name',
 4777:                        'user'       => 'Username/domain',
 4778:                        'phone'      => 'Phone',
 4779:                        'cc'         => 'Cc e-mail',
 4780:                        'course'     => 'Course Details',
 4781:                        'section'    => 'Sections',
 4782:                        'screenshot' => 'File upload',
 4783:     );
 4784:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 4785:     my %possoptions = (
 4786:                         username     => ['yes','no','req'],
 4787:                         phone        => ['yes','no','req'],
 4788:                         user         => ['yes','no'],
 4789:                         cc           => ['yes','no'],
 4790:                         course       => ['yes','no'],
 4791:                         section      => ['yes','no'],
 4792:                         screenshot   => ['yes','no'],
 4793:                       );
 4794:     my %fieldoptions = &Apache::lonlocal::texthash (
 4795:                          'yes'  => 'Optional',
 4796:                          'req'  => 'Required',
 4797:                          'no'   => "Not shown",
 4798:     );
 4799:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 4800: }
 4801: 
 4802: sub tool_titles {
 4803:     my %titles = &Apache::lonlocal::texthash (
 4804:                      aboutme    => 'Personal web page',
 4805:                      blog       => 'Blog',
 4806:                      webdav     => 'WebDAV',
 4807:                      portfolio  => 'Portfolio',
 4808:                      official   => 'Official courses (with institutional codes)',
 4809:                      unofficial => 'Unofficial courses',
 4810:                      community  => 'Communities',
 4811:                      textbook   => 'Textbook courses',
 4812:                  );
 4813:     return %titles;
 4814: }
 4815: 
 4816: sub courserequest_titles {
 4817:     my %titles = &Apache::lonlocal::texthash (
 4818:                                    official   => 'Official',
 4819:                                    unofficial => 'Unofficial',
 4820:                                    community  => 'Communities',
 4821:                                    textbook   => 'Textbook',
 4822:                                    norequest  => 'Not allowed',
 4823:                                    approval   => 'Approval by Dom. Coord.',
 4824:                                    validate   => 'With validation',
 4825:                                    autolimit  => 'Numerical limit',
 4826:                                    unlimited  => '(blank for unlimited)',
 4827:                  );
 4828:     return %titles;
 4829: }
 4830: 
 4831: sub authorrequest_titles {
 4832:     my %titles = &Apache::lonlocal::texthash (
 4833:                                    norequest  => 'Not allowed',
 4834:                                    approval   => 'Approval by Dom. Coord.',
 4835:                                    automatic  => 'Automatic approval',
 4836:                  );
 4837:     return %titles;
 4838: }
 4839: 
 4840: sub courserequest_conditions {
 4841:     my %conditions = &Apache::lonlocal::texthash (
 4842:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 4843:        validate   => '(Processing of request subject to institutional validation).',
 4844:                  );
 4845:     return %conditions;
 4846: }
 4847: 
 4848: 
 4849: sub print_usercreation {
 4850:     my ($position,$dom,$settings,$rowtotal) = @_;
 4851:     my $numinrow = 4;
 4852:     my $datatable;
 4853:     if ($position eq 'top') {
 4854:         $$rowtotal ++;
 4855:         my $rowcount = 0;
 4856:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 4857:         if (ref($rules) eq 'HASH') {
 4858:             if (keys(%{$rules}) > 0) {
 4859:                 $datatable .= &user_formats_row('username',$settings,$rules,
 4860:                                                 $ruleorder,$numinrow,$rowcount);
 4861:                 $$rowtotal ++;
 4862:                 $rowcount ++;
 4863:             }
 4864:         }
 4865:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 4866:         if (ref($idrules) eq 'HASH') {
 4867:             if (keys(%{$idrules}) > 0) {
 4868:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 4869:                                                 $idruleorder,$numinrow,$rowcount);
 4870:                 $$rowtotal ++;
 4871:                 $rowcount ++;
 4872:             }
 4873:         }
 4874:         if ($rowcount == 0) {
 4875:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 4876:             $$rowtotal ++;
 4877:             $rowcount ++;
 4878:         }
 4879:     } elsif ($position eq 'middle') {
 4880:         my @creators = ('author','course','requestcrs');
 4881:         my ($rules,$ruleorder) =
 4882:             &Apache::lonnet::inst_userrules($dom,'username');
 4883:         my %lt = &usercreation_types();
 4884:         my %checked;
 4885:         if (ref($settings) eq 'HASH') {
 4886:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 4887:                 foreach my $item (@creators) {
 4888:                     $checked{$item} = $settings->{'cancreate'}{$item};
 4889:                 }
 4890:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 4891:                 foreach my $item (@creators) {
 4892:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 4893:                         $checked{$item} = 'none';
 4894:                     }
 4895:                 }
 4896:             }
 4897:         }
 4898:         my $rownum = 0;
 4899:         foreach my $item (@creators) {
 4900:             $rownum ++;
 4901:             if ($checked{$item} eq '') {
 4902:                 $checked{$item} = 'any';
 4903:             }
 4904:             my $css_class;
 4905:             if ($rownum%2) {
 4906:                 $css_class = '';
 4907:             } else {
 4908:                 $css_class = ' class="LC_odd_row" ';
 4909:             }
 4910:             $datatable .= '<tr'.$css_class.'>'.
 4911:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 4912:                          '</span></td><td align="right">';
 4913:             my @options = ('any');
 4914:             if (ref($rules) eq 'HASH') {
 4915:                 if (keys(%{$rules}) > 0) {
 4916:                     push(@options,('official','unofficial'));
 4917:                 }
 4918:             }
 4919:             push(@options,'none');
 4920:             foreach my $option (@options) {
 4921:                 my $type = 'radio';
 4922:                 my $check = ' ';
 4923:                 if ($checked{$item} eq $option) {
 4924:                     $check = ' checked="checked" ';
 4925:                 } 
 4926:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4927:                               '<input type="'.$type.'" name="can_createuser_'.
 4928:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 4929:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 4930:             }
 4931:             $datatable .= '</td></tr>';
 4932:         }
 4933:     } else {
 4934:         my @contexts = ('author','course','domain');
 4935:         my @authtypes = ('int','krb4','krb5','loc');
 4936:         my %checked;
 4937:         if (ref($settings) eq 'HASH') {
 4938:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 4939:                 foreach my $item (@contexts) {
 4940:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 4941:                         foreach my $auth (@authtypes) {
 4942:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 4943:                                 $checked{$item}{$auth} = ' checked="checked" ';
 4944:                             }
 4945:                         }
 4946:                     }
 4947:                 }
 4948:             }
 4949:         } else {
 4950:             foreach my $item (@contexts) {
 4951:                 foreach my $auth (@authtypes) {
 4952:                     $checked{$item}{$auth} = ' checked="checked" ';
 4953:                 }
 4954:             }
 4955:         }
 4956:         my %title = &context_names();
 4957:         my %authname = &authtype_names();
 4958:         my $rownum = 0;
 4959:         my $css_class; 
 4960:         foreach my $item (@contexts) {
 4961:             if ($rownum%2) {
 4962:                 $css_class = '';
 4963:             } else {
 4964:                 $css_class = ' class="LC_odd_row" ';
 4965:             }
 4966:             $datatable .=   '<tr'.$css_class.'>'.
 4967:                             '<td>'.$title{$item}.
 4968:                             '</td><td class="LC_left_item">'.
 4969:                             '<span class="LC_nobreak">';
 4970:             foreach my $auth (@authtypes) {
 4971:                 $datatable .= '<label>'. 
 4972:                               '<input type="checkbox" name="'.$item.'_auth" '.
 4973:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 4974:                               $authname{$auth}.'</label>&nbsp;';
 4975:             }
 4976:             $datatable .= '</span></td></tr>';
 4977:             $rownum ++;
 4978:         }
 4979:         $$rowtotal += $rownum;
 4980:     }
 4981:     return $datatable;
 4982: }
 4983: 
 4984: sub print_selfcreation {
 4985:     my ($position,$dom,$settings,$rowtotal) = @_;
 4986:     my (@selfcreate,$createsettings,$processing,$datatable);
 4987:     if (ref($settings) eq 'HASH') {
 4988:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 4989:             $createsettings = $settings->{'cancreate'};
 4990:             if (ref($createsettings) eq 'HASH') {
 4991:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 4992:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 4993:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 4994:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 4995:                         @selfcreate = ('email','login','sso');
 4996:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 4997:                         @selfcreate = ($createsettings->{'selfcreate'});
 4998:                     }
 4999:                 }
 5000:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 5001:                     $processing = $createsettings->{'selfcreateprocessing'};
 5002:                 }
 5003:             }
 5004:         }
 5005:     }
 5006:     my %radiohash;
 5007:     my $numinrow = 4;
 5008:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 5009:     if ($position eq 'top') {
 5010:         my %choices = &Apache::lonlocal::texthash (
 5011:                                                       cancreate_login      => 'Institutional Login',
 5012:                                                       cancreate_sso        => 'Institutional Single Sign On',
 5013:                                                   );
 5014:         my @toggles = sort(keys(%choices));
 5015:         my %defaultchecked = (
 5016:                                'cancreate_login' => 'off',
 5017:                                'cancreate_sso'   => 'off',
 5018:                              );
 5019:         my ($onclick,$itemcount);
 5020:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5021:                                                      \%choices,$itemcount,$onclick);
 5022:         $$rowtotal += $itemcount;
 5023:         
 5024:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5025: 
 5026:         if (ref($usertypes) eq 'HASH') {
 5027:             if (keys(%{$usertypes}) > 0) {
 5028:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 5029:                                              $dom,$numinrow,$othertitle,
 5030:                                              'statustocreate',$$rowtotal);
 5031:                 $$rowtotal ++;
 5032:             }
 5033:         }
 5034:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 5035:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5036:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 5037:         my $rem;
 5038:         my $numperrow = 2;
 5039:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 5040:         $datatable .= '<tr'.$css_class.'>'.
 5041:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 5042:                      '<td class="LC_left_item">'."\n".
 5043:                      '<table><tr><td>'."\n";
 5044:         for (my $i=0; $i<@fields; $i++) {
 5045:             $rem = $i%($numperrow);
 5046:             if ($rem == 0) {
 5047:                 if ($i > 0) {
 5048:                     $datatable .= '</tr>';
 5049:                 }
 5050:                 $datatable .= '<tr>';
 5051:             }
 5052:             my $currval;
 5053:             if (ref($createsettings) eq 'HASH') {
 5054:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 5055:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 5056:                 }
 5057:             }
 5058:             $datatable .= '<td class="LC_left_item">'.
 5059:                           '<span class="LC_nobreak">'.
 5060:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 5061:                           'value="'.$currval.'" size="10" />&nbsp;'.
 5062:                           $fieldtitles{$fields[$i]}.'</span></td>';
 5063:         }
 5064:         my $colsleft = $numperrow - $rem;
 5065:         if ($colsleft > 1 ) {
 5066:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5067:                          '&nbsp;</td>';
 5068:         } elsif ($colsleft == 1) {
 5069:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 5070:         }
 5071:         $datatable .= '</tr></table></td></tr>';
 5072:         $$rowtotal ++;
 5073:     } elsif ($position eq 'middle') {
 5074:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 5075:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5076:         $usertypes->{'default'} = $othertitle;
 5077:         if (ref($types) eq 'ARRAY') {
 5078:             push(@{$types},'default');
 5079:             $usertypes->{'default'} = $othertitle;
 5080:             foreach my $status (@{$types}) {
 5081:                 $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 5082:                                                        $numinrow,$$rowtotal,$usertypes);
 5083:                 $$rowtotal ++;
 5084:             }
 5085:         }
 5086:     } else {
 5087:         my %choices = &Apache::lonlocal::texthash (
 5088:                                                       cancreate_email => 'E-mail address as username',
 5089:                                                   );
 5090:         my @toggles = sort(keys(%choices));
 5091:         my %defaultchecked = (
 5092:                                'cancreate_email' => 'off',
 5093:                              );
 5094:         my $itemcount = 0;
 5095:         my $display = 'none';
 5096:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 5097:             $display = 'block';
 5098:         }
 5099:         my $onclick = "toggleDisplay(this.form,'emailoptions');";
 5100:         my $additional = '<div id="emailoptions" style="display: '.$display.'">';
 5101:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5102:         my $usertypes = {};
 5103:         my $order = [];
 5104:         if ((ref($domdefaults{'inststatustypes'}) eq 'HASH') && (ref($domdefaults{'inststatusguest'}) eq 'ARRAY')) {
 5105:             $usertypes = $domdefaults{'inststatustypes'};
 5106:             $order = $domdefaults{'inststatusguest'};
 5107:         }
 5108:         if (ref($order) eq 'ARRAY') {
 5109:             push(@{$order},'default');
 5110:             if (@{$order} > 1) {
 5111:                 $usertypes->{'default'} = &mt('Other users');
 5112:                 $additional .= '<table><tr>';
 5113:                 foreach my $status (@{$order}) {
 5114:                     $additional .= '<th>'.$usertypes->{$status}.'</th>';
 5115:                 }
 5116:                 $additional .= '</tr><tr>';
 5117:                 foreach my $status (@{$order}) {
 5118:                     $additional .= '<td>'.&email_as_username($rowtotal,$processing,$status).'</td>';
 5119:                 }
 5120:                 $additional .= '</tr></table>';
 5121:             } else {
 5122:                 $usertypes->{'default'} = &mt('All users');
 5123:                 $additional .= &email_as_username($rowtotal,$processing);
 5124:             }
 5125:         }
 5126:         $additional .= '</div>'."\n";
 5127: 
 5128:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5129:                                                      \%choices,$$rowtotal,$onclick,$additional);
 5130:         $$rowtotal ++;
 5131:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal);
 5132:         $$rowtotal ++;
 5133:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 5134:         $numinrow = 1;
 5135:         if (ref($order) eq 'ARRAY') {
 5136:             foreach my $status (@{$order}) {
 5137:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 5138:                                                        $numinrow,$$rowtotal,$usertypes,$infofields,$infotitles);
 5139:                 $$rowtotal ++;
 5140:             }
 5141:         }
 5142:         my ($emailrules,$emailruleorder) =
 5143:             &Apache::lonnet::inst_userrules($dom,'email');
 5144:         if (ref($emailrules) eq 'HASH') {
 5145:             if (keys(%{$emailrules}) > 0) {
 5146:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 5147:                                                 $emailruleorder,$numinrow,$$rowtotal);
 5148:                 $$rowtotal ++;
 5149:             }
 5150:         }
 5151:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal);
 5152:     }
 5153:     return $datatable;
 5154: }
 5155: 
 5156: sub email_as_username {
 5157:     my ($rowtotal,$processing,$type) = @_;
 5158:     my %choices =
 5159:         &Apache::lonlocal::texthash (
 5160:                                       automatic => 'Automatic approval',
 5161:                                       approval  => 'Queued for approval',
 5162:                                     );
 5163:     my $output;
 5164:     foreach my $option ('automatic','approval') {
 5165:         my $checked;
 5166:         if (ref($processing) eq 'HASH') {
 5167:             if ($type eq '') {   
 5168:                 if (!exists($processing->{'default'})) {
 5169:                     if ($option eq 'automatic') {
 5170:                         $checked = ' checked="checked"';
 5171:                     }
 5172:                 } else {
 5173:                     if ($processing->{'default'} eq $option) {
 5174:                         $checked = ' checked="checked"';
 5175:                     }
 5176:                 }
 5177:             } else {
 5178:                 if (!exists($processing->{$type})) {
 5179:                     if ($option eq 'automatic') {
 5180:                         $checked = ' checked="checked"';
 5181:                     }
 5182:                 } else {
 5183:                     if ($processing->{$type} eq $option) {
 5184:                         $checked = ' checked="checked"';
 5185:                     }
 5186:                 }
 5187:             }
 5188:         } elsif ($option eq 'automatic') {
 5189:             $checked = ' checked="checked"'; 
 5190:         }
 5191:         my $name = 'cancreate_emailprocess';
 5192:         if (($type ne '') && ($type ne 'default')) {
 5193:             $name .= '_'.$type;
 5194:         }
 5195:         $output .= '<span class="LC_nobreak"><label>'.
 5196:                    '<input type="radio" name="'.$name.'"'.
 5197:                    $checked.' value="'.$option.'" />'.
 5198:                    $choices{$option}.'</label></span>';
 5199:         if ($type eq '') {
 5200:             $output .= '&nbsp;';
 5201:         } else {
 5202:             $output .= '<br />';
 5203:         }
 5204:     }
 5205:     $$rowtotal ++;
 5206:     return $output;
 5207: }
 5208: 
 5209: sub captcha_choice {
 5210:     my ($context,$settings,$itemcount) = @_;
 5211:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 5212:         $vertext,$currver); 
 5213:     my %lt = &captcha_phrases();
 5214:     $keyentry = 'hidden';
 5215:     if ($context eq 'cancreate') {
 5216:         $rowname = &mt('CAPTCHA validation');
 5217:     } elsif ($context eq 'login') {
 5218:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 5219:     }
 5220:     if (ref($settings) eq 'HASH') {
 5221:         if ($settings->{'captcha'}) {
 5222:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 5223:         } else {
 5224:             $checked{'original'} = ' checked="checked"';
 5225:         }
 5226:         if ($settings->{'captcha'} eq 'recaptcha') {
 5227:             $pubtext = $lt{'pub'};
 5228:             $privtext = $lt{'priv'};
 5229:             $keyentry = 'text';
 5230:             $vertext = $lt{'ver'};
 5231:             $currver = $settings->{'recaptchaversion'};
 5232:             if ($currver ne '2') {
 5233:                 $currver = 1;
 5234:             }
 5235:         }
 5236:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 5237:             $currpub = $settings->{'recaptchakeys'}{'public'};
 5238:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 5239:         }
 5240:     } else {
 5241:         $checked{'original'} = ' checked="checked"';
 5242:     }
 5243:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5244:     my $output = '<tr'.$css_class.'>'.
 5245:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 5246:                  '<table><tr><td>'."\n";
 5247:     foreach my $option ('original','recaptcha','notused') {
 5248:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 5249:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 5250:                    $lt{$option}.'</label></span>';
 5251:         unless ($option eq 'notused') {
 5252:             $output .= ('&nbsp;'x2)."\n";
 5253:         }
 5254:     }
 5255: #
 5256: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 5257: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 5258: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 5259: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 5260: #
 5261:     $output .= '</td></tr>'."\n".
 5262:                '<tr><td>'."\n".
 5263:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 5264:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 5265:                $currpub.'" size="40" /></span><br />'."\n".
 5266:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 5267:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 5268:                $currpriv.'" size="40" /></span><br />'.
 5269:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 5270:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 5271:                $currver.'" size="3" /></span><br />'.
 5272:                '</td></tr></table>'."\n".
 5273:                '</td></tr>';
 5274:     return $output;
 5275: }
 5276: 
 5277: sub user_formats_row {
 5278:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 5279:     my $output;
 5280:     my %text = (
 5281:                    'username' => 'new usernames',
 5282:                    'id'       => 'IDs',
 5283:                    'email'    => 'self-created accounts (e-mail)',
 5284:                );
 5285:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 5286:     $output = '<tr '.$css_class.'>'.
 5287:               '<td><span class="LC_nobreak">';
 5288:     if ($type eq 'email') {
 5289:         $output .= &mt("Formats disallowed for $text{$type}: ");
 5290:     } else {
 5291:         $output .= &mt("Format rules to check for $text{$type}: ");
 5292:     }
 5293:     $output .= '</span></td>'.
 5294:                '<td class="LC_left_item" colspan="2"><table>';
 5295:     my $rem;
 5296:     if (ref($ruleorder) eq 'ARRAY') {
 5297:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 5298:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 5299:                 my $rem = $i%($numinrow);
 5300:                 if ($rem == 0) {
 5301:                     if ($i > 0) {
 5302:                         $output .= '</tr>';
 5303:                     }
 5304:                     $output .= '<tr>';
 5305:                 }
 5306:                 my $check = ' ';
 5307:                 if (ref($settings) eq 'HASH') {
 5308:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 5309:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 5310:                             $check = ' checked="checked" ';
 5311:                         }
 5312:                     }
 5313:                 }
 5314:                 $output .= '<td class="LC_left_item">'.
 5315:                            '<span class="LC_nobreak"><label>'.
 5316:                            '<input type="checkbox" name="'.$type.'_rule" '.
 5317:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 5318:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 5319:             }
 5320:         }
 5321:         $rem = @{$ruleorder}%($numinrow);
 5322:     }
 5323:     my $colsleft = $numinrow - $rem;
 5324:     if ($colsleft > 1 ) {
 5325:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5326:                    '&nbsp;</td>';
 5327:     } elsif ($colsleft == 1) {
 5328:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 5329:     }
 5330:     $output .= '</tr></table></td></tr>';
 5331:     return $output;
 5332: }
 5333: 
 5334: sub usercreation_types {
 5335:     my %lt = &Apache::lonlocal::texthash (
 5336:                     author     => 'When adding a co-author',
 5337:                     course     => 'When adding a user to a course',
 5338:                     requestcrs => 'When requesting a course',
 5339:                     any        => 'Any',
 5340:                     official   => 'Institutional only ',
 5341:                     unofficial => 'Non-institutional only',
 5342:                     none       => 'None',
 5343:     );
 5344:     return %lt;
 5345: }
 5346: 
 5347: sub selfcreation_types {
 5348:     my %lt = &Apache::lonlocal::texthash (
 5349:                     selfcreate => 'User creates own account',
 5350:                     any        => 'Any',
 5351:                     official   => 'Institutional only ',
 5352:                     unofficial => 'Non-institutional only',
 5353:                     email      => 'E-mail address',
 5354:                     login      => 'Institutional Login',
 5355:                     sso        => 'SSO',
 5356:              );
 5357: }
 5358: 
 5359: sub authtype_names {
 5360:     my %lt = &Apache::lonlocal::texthash(
 5361:                       int    => 'Internal',
 5362:                       krb4   => 'Kerberos 4',
 5363:                       krb5   => 'Kerberos 5',
 5364:                       loc    => 'Local',
 5365:                   );
 5366:     return %lt;
 5367: }
 5368: 
 5369: sub context_names {
 5370:     my %context_title = &Apache::lonlocal::texthash(
 5371:        author => 'Creating users when an Author',
 5372:        course => 'Creating users when in a course',
 5373:        domain => 'Creating users when a Domain Coordinator',
 5374:     );
 5375:     return %context_title;
 5376: }
 5377: 
 5378: sub print_usermodification {
 5379:     my ($position,$dom,$settings,$rowtotal) = @_;
 5380:     my $numinrow = 4;
 5381:     my ($context,$datatable,$rowcount);
 5382:     if ($position eq 'top') {
 5383:         $rowcount = 0;
 5384:         $context = 'author'; 
 5385:         foreach my $role ('ca','aa') {
 5386:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 5387:                                                    $numinrow,$rowcount);
 5388:             $$rowtotal ++;
 5389:             $rowcount ++;
 5390:         }
 5391:     } elsif ($position eq 'bottom') {
 5392:         $context = 'course';
 5393:         $rowcount = 0;
 5394:         foreach my $role ('st','ep','ta','in','cr') {
 5395:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 5396:                                                    $numinrow,$rowcount);
 5397:             $$rowtotal ++;
 5398:             $rowcount ++;
 5399:         }
 5400:     }
 5401:     return $datatable;
 5402: }
 5403: 
 5404: sub print_defaults {
 5405:     my ($position,$dom,$settings,$rowtotal) = @_;
 5406:     my $rownum = 0;
 5407:     my ($datatable,$css_class,$titles);
 5408:     unless ($position eq 'bottom') {
 5409:         $titles = &defaults_titles($dom);
 5410:     }
 5411:     if ($position eq 'top') {
 5412:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 5413:                      'datelocale_def','portal_def');
 5414:         my %defaults;
 5415:         if (ref($settings) eq 'HASH') {
 5416:             %defaults = %{$settings};
 5417:         } else {
 5418:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 5419:             foreach my $item (@items) {
 5420:                 $defaults{$item} = $domdefaults{$item};
 5421:             }
 5422:         }
 5423:         foreach my $item (@items) {
 5424:             if ($rownum%2) {
 5425:                 $css_class = '';
 5426:             } else {
 5427:                 $css_class = ' class="LC_odd_row" ';
 5428:             }
 5429:             $datatable .= '<tr'.$css_class.'>'.
 5430:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 5431:                           '</span></td><td class="LC_right_item" colspan="3">';
 5432:             if ($item eq 'auth_def') {
 5433:                 my @authtypes = ('internal','krb4','krb5','localauth');
 5434:                 my %shortauth = (
 5435:                                  internal => 'int',
 5436:                                  krb4 => 'krb4',
 5437:                                  krb5 => 'krb5',
 5438:                                  localauth  => 'loc'
 5439:                                 );
 5440:                 my %authnames = &authtype_names();
 5441:                 foreach my $auth (@authtypes) {
 5442:                     my $checked = ' ';
 5443:                     if ($defaults{$item} eq $auth) {
 5444:                         $checked = ' checked="checked" ';
 5445:                     }
 5446:                     $datatable .= '<label><input type="radio" name="'.$item.
 5447:                                   '" value="'.$auth.'"'.$checked.'/>'.
 5448:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 5449:                 }
 5450:             } elsif ($item eq 'timezone_def') {
 5451:                 my $includeempty = 1;
 5452:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 5453:             } elsif ($item eq 'datelocale_def') {
 5454:                 my $includeempty = 1;
 5455:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 5456:             } elsif ($item eq 'lang_def') {
 5457:                 my $includeempty = 1;
 5458:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 5459:             } else {
 5460:                 my $size;
 5461:                 if ($item eq 'portal_def') {
 5462:                     $size = ' size="25"';
 5463:                 }
 5464:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 5465:                               $defaults{$item}.'"'.$size.' />';
 5466:             }
 5467:             $datatable .= '</td></tr>';
 5468:             $rownum ++;
 5469:         }
 5470:     } elsif ($position eq 'middle') {
 5471:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 5472:         my %defaults;
 5473:         if (ref($settings) eq 'HASH') {
 5474:             %defaults = %{$settings};
 5475:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 5476:                 $defaults{'intauth_cost'} = 10;
 5477:             }
 5478:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 5479:                 $defaults{'intauth_check'} = 0;
 5480:             }
 5481:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 5482:                 $defaults{'intauth_switch'} = 0;
 5483:             }
 5484:         } else {
 5485:             %defaults = (
 5486:                           'intauth_cost'   => 10,
 5487:                           'intauth_check'  => 0,
 5488:                           'intauth_switch' => 0,
 5489:                         );
 5490:         }
 5491:         foreach my $item (@items) {
 5492:             if ($rownum%2) {
 5493:                 $css_class = '';
 5494:             } else {
 5495:                 $css_class = ' class="LC_odd_row" ';
 5496:             }
 5497:             $datatable .= '<tr'.$css_class.'>'.
 5498:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 5499:                           '</span></td><td class="LC_left_item" colspan="3">';
 5500:             if ($item eq 'intauth_switch') {
 5501:                 my @options = (0,1,2);
 5502:                 my %optiondesc = &Apache::lonlocal::texthash (
 5503:                                    0 => 'No',
 5504:                                    1 => 'Yes',
 5505:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 5506:                                  );
 5507:                 $datatable .= '<table width="100%">';
 5508:                 foreach my $option (@options) {
 5509:                     my $checked = ' ';
 5510:                     if ($defaults{$item} eq $option) {
 5511:                         $checked = ' checked="checked"';
 5512:                     }
 5513:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 5514:                                   '<label><input type="radio" name="'.$item.
 5515:                                   '" value="'.$option.'"'.$checked.' />'.
 5516:                                   $optiondesc{$option}.'</label></span></td></tr>';
 5517:                 }
 5518:                 $datatable .= '</table>';
 5519:             } elsif ($item eq 'intauth_check') {
 5520:                 my @options = (0,1,2);
 5521:                 my %optiondesc = &Apache::lonlocal::texthash (
 5522:                                    0 => 'No',
 5523:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 5524:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 5525:                                  );
 5526:                 $datatable .= '<table wisth="100%">';
 5527:                 foreach my $option (@options) {
 5528:                     my $checked = ' ';
 5529:                     my $onclick;
 5530:                     if ($defaults{$item} eq $option) {
 5531:                         $checked = ' checked="checked"';
 5532:                     }
 5533:                     if ($option == 2) {
 5534:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 5535:                     }
 5536:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 5537:                                   '<label><input type="radio" name="'.$item.
 5538:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 5539:                                   $optiondesc{$option}.'</label></span></td></tr>';
 5540:                 }
 5541:                 $datatable .= '</table>';
 5542:             } else {
 5543:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 5544:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />';
 5545:             }
 5546:             $datatable .= '</td></tr>';
 5547:             $rownum ++;
 5548:         }
 5549:     } else {
 5550:         my %defaults;
 5551:         if (ref($settings) eq 'HASH') {
 5552:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH') &&
 5553:                 (ref($settings->{'inststatusguest'}) eq 'ARRAY')) {
 5554:                 my $maxnum = @{$settings->{'inststatusorder'}};
 5555:                 for (my $i=0; $i<$maxnum; $i++) {
 5556:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 5557:                     my $item = $settings->{'inststatusorder'}->[$i];
 5558:                     my $title = $settings->{'inststatustypes'}->{$item};
 5559:                     my $guestok;
 5560:                     if (grep(/^\Q$item\E$/,@{$settings->{'inststatusguest'}})) {
 5561:                         $guestok = 1;
 5562:                     }
 5563:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 5564:                     $datatable .= '<tr'.$css_class.'>'.
 5565:                                   '<td><span class="LC_nobreak">'.
 5566:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 5567:                     for (my $k=0; $k<=$maxnum; $k++) {
 5568:                         my $vpos = $k+1;
 5569:                         my $selstr;
 5570:                         if ($k == $i) {
 5571:                             $selstr = ' selected="selected" ';
 5572:                         }
 5573:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5574:                     }
 5575:                     my ($checkedon,$checkedoff);
 5576:                     $checkedoff = ' checked="checked"';
 5577:                     if ($guestok) {
 5578:                         $checkedon = $checkedoff;
 5579:                         $checkedoff = ''; 
 5580:                     }
 5581:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 5582:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 5583:                                   &mt('delete').'</span></td>'.
 5584:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed:').
 5585:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 5586:                                   '</span></td>'.
 5587:                                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 5588:                                   '<label><input type="radio" value="1" name="inststatus_guest_'.$item.'"'.$checkedon.' />'.
 5589:                                   &mt('Yes').'</label>'.('&nbsp;'x2).
 5590:                                   '<label><input type="radio" value="0" name="inststatus_guest_'.$item.'"'.$checkedoff.' />'.
 5591:                                   &mt('No').'</label></span></td></tr>';
 5592:                 }
 5593:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 5594:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 5595:                 $datatable .= '<tr '.$css_class.'>'.
 5596:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 5597:                 for (my $k=0; $k<=$maxnum; $k++) {
 5598:                     my $vpos = $k+1;
 5599:                     my $selstr;
 5600:                     if ($k == $maxnum) {
 5601:                         $selstr = ' selected="selected" ';
 5602:                     }
 5603:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5604:                 }
 5605:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 5606:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 5607:                               '&nbsp;'.&mt('(new)').
 5608:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 5609:                               &mt('Name displayed:').
 5610:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 5611:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 5612:                               '<label><input type="radio" value="1" name="addinststatus_guest" />'.
 5613:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 5614:                               '<label><input type="radio" value="0" name="addinststatus_guest" />'.
 5615:                               &mt('No').'</label></span></td></tr>';
 5616:                               '</tr>'."\n";
 5617:                 $rownum ++;
 5618:             }
 5619:         }
 5620:     }
 5621:     $$rowtotal += $rownum;
 5622:     return $datatable;
 5623: }
 5624: 
 5625: sub get_languages_hash {
 5626:     my %langchoices;
 5627:     foreach my $id (&Apache::loncommon::languageids()) {
 5628:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 5629:         if ($code ne '') {
 5630:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 5631:         }
 5632:     }
 5633:     return %langchoices;
 5634: }
 5635: 
 5636: sub defaults_titles {
 5637:     my ($dom) = @_;
 5638:     my %titles = &Apache::lonlocal::texthash (
 5639:                    'auth_def'      => 'Default authentication type',
 5640:                    'auth_arg_def'  => 'Default authentication argument',
 5641:                    'lang_def'      => 'Default language',
 5642:                    'timezone_def'  => 'Default timezone',
 5643:                    'datelocale_def' => 'Default locale for dates',
 5644:                    'portal_def'     => 'Portal/Default URL',
 5645:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
 5646:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
 5647:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
 5648:                  );
 5649:     if ($dom) {
 5650:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 5651:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 5652:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 5653:         $protocol = 'http' if ($protocol ne 'https');
 5654:         if ($uint_dom) {
 5655:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 5656:                                          $uint_dom);
 5657:         }
 5658:     }
 5659:     return (\%titles);
 5660: }
 5661: 
 5662: sub print_scantronformat {
 5663:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 5664:     my $itemcount = 1;
 5665:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 5666:         %confhash);
 5667:     my $switchserver = &check_switchserver($dom,$confname);
 5668:     my %lt = &Apache::lonlocal::texthash (
 5669:                 default => 'Default bubblesheet format file error',
 5670:                 custom  => 'Custom bubblesheet format file error',
 5671:              );
 5672:     my %scantronfiles = (
 5673:         default => 'default.tab',
 5674:         custom => 'custom.tab',
 5675:     );
 5676:     foreach my $key (keys(%scantronfiles)) {
 5677:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 5678:                               .$scantronfiles{$key};
 5679:     }
 5680:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 5681:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 5682:         if (!$switchserver) {
 5683:             my $servadm = $r->dir_config('lonAdmEMail');
 5684:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 5685:             if ($configuserok eq 'ok') {
 5686:                 if ($author_ok eq 'ok') {
 5687:                     my %legacyfile = (
 5688:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 5689:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 5690:                     );
 5691:                     my %md5chk;
 5692:                     foreach my $type (keys(%legacyfile)) {
 5693:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 5694:                         chomp($md5chk{$type});
 5695:                     }
 5696:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 5697:                         foreach my $type (keys(%legacyfile)) {
 5698:                             ($scantronurls{$type},my $error) = 
 5699:                                 &legacy_scantronformat($r,$dom,$confname,
 5700:                                                  $type,$legacyfile{$type},
 5701:                                                  $scantronurls{$type},
 5702:                                                  $scantronfiles{$type});
 5703:                             if ($error ne '') {
 5704:                                 $error{$type} = $error;
 5705:                             }
 5706:                         }
 5707:                         if (keys(%error) == 0) {
 5708:                             $is_custom = 1;
 5709:                             $confhash{'scantron'}{'scantronformat'} = 
 5710:                                 $scantronurls{'custom'};
 5711:                             my $putresult = 
 5712:                                 &Apache::lonnet::put_dom('configuration',
 5713:                                                          \%confhash,$dom);
 5714:                             if ($putresult ne 'ok') {
 5715:                                 $error{'custom'} = 
 5716:                                     '<span class="LC_error">'.
 5717:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 5718:                             }
 5719:                         }
 5720:                     } else {
 5721:                         ($scantronurls{'default'},my $error) =
 5722:                             &legacy_scantronformat($r,$dom,$confname,
 5723:                                           'default',$legacyfile{'default'},
 5724:                                           $scantronurls{'default'},
 5725:                                           $scantronfiles{'default'});
 5726:                         if ($error eq '') {
 5727:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 5728:                             my $putresult =
 5729:                                 &Apache::lonnet::put_dom('configuration',
 5730:                                                          \%confhash,$dom);
 5731:                             if ($putresult ne 'ok') {
 5732:                                 $error{'default'} =
 5733:                                     '<span class="LC_error">'.
 5734:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 5735:                             }
 5736:                         } else {
 5737:                             $error{'default'} = $error;
 5738:                         }
 5739:                     }
 5740:                 }
 5741:             }
 5742:         } else {
 5743:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 5744:         }
 5745:     }
 5746:     if (ref($settings) eq 'HASH') {
 5747:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 5748:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 5749:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 5750:                 $scantronurl = '';
 5751:             } else {
 5752:                 $scantronurl = $settings->{'scantronformat'};
 5753:             }
 5754:             $is_custom = 1;
 5755:         } else {
 5756:             $scantronurl = $scantronurls{'default'};
 5757:         }
 5758:     } else {
 5759:         if ($is_custom) {
 5760:             $scantronurl = $scantronurls{'custom'};
 5761:         } else {
 5762:             $scantronurl = $scantronurls{'default'};
 5763:         }
 5764:     }
 5765:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5766:     $datatable .= '<tr'.$css_class.'>';
 5767:     if (!$is_custom) {
 5768:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 5769:                       '<span class="LC_nobreak">';
 5770:         if ($scantronurl) {
 5771:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 5772:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 5773:         } else {
 5774:             $datatable = &mt('File unavailable for display');
 5775:         }
 5776:         $datatable .= '</span></td>';
 5777:         if (keys(%error) == 0) { 
 5778:             $datatable .= '<td valign="bottom">';
 5779:             if (!$switchserver) {
 5780:                 $datatable .= &mt('Upload:').'<br />';
 5781:             }
 5782:         } else {
 5783:             my $errorstr;
 5784:             foreach my $key (sort(keys(%error))) {
 5785:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 5786:             }
 5787:             $datatable .= '<td>'.$errorstr;
 5788:         }
 5789:     } else {
 5790:         if (keys(%error) > 0) {
 5791:             my $errorstr;
 5792:             foreach my $key (sort(keys(%error))) {
 5793:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 5794:             } 
 5795:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 5796:         } elsif ($scantronurl) {
 5797:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 5798:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 5799:             $datatable .= '<td><span class="LC_nobreak">'.
 5800:                           $link.
 5801:                           '<label><input type="checkbox" name="scantronformat_del"'.
 5802:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 5803:                           '<td><span class="LC_nobreak">&nbsp;'.
 5804:                           &mt('Replace:').'</span><br />';
 5805:         }
 5806:     }
 5807:     if (keys(%error) == 0) {
 5808:         if ($switchserver) {
 5809:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 5810:         } else {
 5811:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 5812:                          '<input type="file" name="scantronformat" /></span>';
 5813:         }
 5814:     }
 5815:     $datatable .= '</td></tr>';
 5816:     $$rowtotal ++;
 5817:     return $datatable;
 5818: }
 5819: 
 5820: sub legacy_scantronformat {
 5821:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 5822:     my ($url,$error);
 5823:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 5824:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 5825:         (my $result,$url) =
 5826:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 5827:                          '','',$newfile);
 5828:         if ($result ne 'ok') {
 5829:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 5830:         }
 5831:     }
 5832:     return ($url,$error);
 5833: }
 5834: 
 5835: sub print_coursecategories {
 5836:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 5837:     my $datatable;
 5838:     if ($position eq 'top') {
 5839:         my (%checked);
 5840:         my @catitems = ('unauth','auth');
 5841:         my @cattypes = ('std','domonly','codesrch','none');
 5842:         $checked{'unauth'} = 'std';
 5843:         $checked{'auth'} = 'std';
 5844:         if (ref($settings) eq 'HASH') {
 5845:             foreach my $type (@cattypes) {
 5846:                 if ($type eq $settings->{'unauth'}) {
 5847:                     $checked{'unauth'} = $type;
 5848:                 }
 5849:                 if ($type eq $settings->{'auth'}) {
 5850:                     $checked{'auth'} = $type;
 5851:                 }
 5852:             }
 5853:         }
 5854:         my %lt = &Apache::lonlocal::texthash (
 5855:                                                unauth   => 'Catalog type for unauthenticated users',
 5856:                                                auth     => 'Catalog type for authenticated users',
 5857:                                                none     => 'No catalog',
 5858:                                                std      => 'Standard catalog',
 5859:                                                domonly  => 'Domain-only catalog',
 5860:                                                codesrch => "Code search form",
 5861:                                              );
 5862:        my $itemcount = 0;
 5863:        foreach my $item (@catitems) {
 5864:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 5865:            $datatable .= '<tr '.$css_class.'>'.
 5866:                          '<td>'.$lt{$item}.'</td>'.
 5867:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 5868:            foreach my $type (@cattypes) {
 5869:                my $ischecked;
 5870:                if ($checked{$item} eq $type) {
 5871:                    $ischecked=' checked="checked"';
 5872:                }
 5873:                $datatable .= '<label>'.
 5874:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 5875:                              ' />'.$lt{$type}.'</label>&nbsp;';
 5876:            }
 5877:            $datatable .= '</td></tr>';
 5878:            $itemcount ++;
 5879:         }
 5880:         $$rowtotal += $itemcount;
 5881:     } elsif ($position eq 'middle') {
 5882:         my $toggle_cats_crs = ' ';
 5883:         my $toggle_cats_dom = ' checked="checked" ';
 5884:         my $can_cat_crs = ' ';
 5885:         my $can_cat_dom = ' checked="checked" ';
 5886:         my $toggle_catscomm_comm = ' ';
 5887:         my $toggle_catscomm_dom = ' checked="checked" ';
 5888:         my $can_catcomm_comm = ' ';
 5889:         my $can_catcomm_dom = ' checked="checked" ';
 5890: 
 5891:         if (ref($settings) eq 'HASH') {
 5892:             if ($settings->{'togglecats'} eq 'crs') {
 5893:                 $toggle_cats_crs = $toggle_cats_dom;
 5894:                 $toggle_cats_dom = ' ';
 5895:             }
 5896:             if ($settings->{'categorize'} eq 'crs') {
 5897:                 $can_cat_crs = $can_cat_dom;
 5898:                 $can_cat_dom = ' ';
 5899:             }
 5900:             if ($settings->{'togglecatscomm'} eq 'comm') {
 5901:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 5902:                 $toggle_catscomm_dom = ' ';
 5903:             }
 5904:             if ($settings->{'categorizecomm'} eq 'comm') {
 5905:                 $can_catcomm_comm = $can_catcomm_dom;
 5906:                 $can_catcomm_dom = ' ';
 5907:             }
 5908:         }
 5909:         my %title = &Apache::lonlocal::texthash (
 5910:                      togglecats     => 'Show/Hide a course in catalog',
 5911:                      togglecatscomm => 'Show/Hide a community in catalog',
 5912:                      categorize     => 'Assign a category to a course',
 5913:                      categorizecomm => 'Assign a category to a community',
 5914:                     );
 5915:         my %level = &Apache::lonlocal::texthash (
 5916:                      dom  => 'Set in Domain',
 5917:                      crs  => 'Set in Course',
 5918:                      comm => 'Set in Community',
 5919:                     );
 5920:         $datatable = '<tr class="LC_odd_row">'.
 5921:                   '<td>'.$title{'togglecats'}.'</td>'.
 5922:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 5923:                   '<input type="radio" name="togglecats"'.
 5924:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 5925:                   '<label><input type="radio" name="togglecats"'.
 5926:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 5927:                   '</tr><tr>'.
 5928:                   '<td>'.$title{'categorize'}.'</td>'.
 5929:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 5930:                   '<label><input type="radio" name="categorize"'.
 5931:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 5932:                   '<label><input type="radio" name="categorize"'.
 5933:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 5934:                   '</tr><tr class="LC_odd_row">'.
 5935:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 5936:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 5937:                   '<input type="radio" name="togglecatscomm"'.
 5938:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 5939:                   '<label><input type="radio" name="togglecatscomm"'.
 5940:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 5941:                   '</tr><tr>'.
 5942:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 5943:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 5944:                   '<label><input type="radio" name="categorizecomm"'.
 5945:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 5946:                   '<label><input type="radio" name="categorizecomm"'.
 5947:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 5948:                   '</tr>';
 5949:         $$rowtotal += 4;
 5950:     } else {
 5951:         my $css_class;
 5952:         my $itemcount = 1;
 5953:         my $cathash; 
 5954:         if (ref($settings) eq 'HASH') {
 5955:             $cathash = $settings->{'cats'};
 5956:         }
 5957:         if (ref($cathash) eq 'HASH') {
 5958:             my (@cats,@trails,%allitems,%idx,@jsarray);
 5959:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 5960:                                                    \%allitems,\%idx,\@jsarray);
 5961:             my $maxdepth = scalar(@cats);
 5962:             my $colattrib = '';
 5963:             if ($maxdepth > 2) {
 5964:                 $colattrib = ' colspan="2" ';
 5965:             }
 5966:             my @path;
 5967:             if (@cats > 0) {
 5968:                 if (ref($cats[0]) eq 'ARRAY') {
 5969:                     my $numtop = @{$cats[0]};
 5970:                     my $maxnum = $numtop;
 5971:                     my %default_names = (
 5972:                           instcode    => &mt('Official courses'),
 5973:                           communities => &mt('Communities'),
 5974:                     );
 5975: 
 5976:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 5977:                         ($cathash->{'instcode::0'} eq '') ||
 5978:                         (!grep(/^communities$/,@{$cats[0]})) || 
 5979:                         ($cathash->{'communities::0'} eq '')) {
 5980:                         $maxnum ++;
 5981:                     }
 5982:                     my $lastidx;
 5983:                     for (my $i=0; $i<$numtop; $i++) {
 5984:                         my $parent = $cats[0][$i];
 5985:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5986:                         my $item = &escape($parent).'::0';
 5987:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 5988:                         $lastidx = $idx{$item};
 5989:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5990:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 5991:                         for (my $k=0; $k<=$maxnum; $k++) {
 5992:                             my $vpos = $k+1;
 5993:                             my $selstr;
 5994:                             if ($k == $i) {
 5995:                                 $selstr = ' selected="selected" ';
 5996:                             }
 5997:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5998:                         }
 5999:                         $datatable .= '</select></span></td><td>';
 6000:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 6001:                             $datatable .=  '<span class="LC_nobreak">'
 6002:                                            .$default_names{$parent}.'</span>';
 6003:                             if ($parent eq 'instcode') {
 6004:                                 $datatable .= '<br /><span class="LC_nobreak">('
 6005:                                               .&mt('with institutional codes')
 6006:                                               .')</span></td><td'.$colattrib.'>';
 6007:                             } else {
 6008:                                 $datatable .= '<table><tr><td>';
 6009:                             }
 6010:                             $datatable .= '<span class="LC_nobreak">'
 6011:                                           .'<label><input type="radio" name="'
 6012:                                           .$parent.'" value="1" checked="checked" />'
 6013:                                           .&mt('Display').'</label>';
 6014:                             if ($parent eq 'instcode') {
 6015:                                 $datatable .= '&nbsp;';
 6016:                             } else {
 6017:                                 $datatable .= '</span></td></tr><tr><td>'
 6018:                                               .'<span class="LC_nobreak">';
 6019:                             }
 6020:                             $datatable .= '<label><input type="radio" name="'
 6021:                                           .$parent.'" value="0" />'
 6022:                                           .&mt('Do not display').'</label></span>';
 6023:                             if ($parent eq 'communities') {
 6024:                                 $datatable .= '</td></tr></table>';
 6025:                             }
 6026:                             $datatable .= '</td>';
 6027:                         } else {
 6028:                             $datatable .= $parent
 6029:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 6030:                                           .'<input type="checkbox" name="deletecategory" '
 6031:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 6032:                         }
 6033:                         my $depth = 1;
 6034:                         push(@path,$parent);
 6035:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 6036:                         pop(@path);
 6037:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 6038:                         $itemcount ++;
 6039:                     }
 6040:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6041:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 6042:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 6043:                     for (my $k=0; $k<=$maxnum; $k++) {
 6044:                         my $vpos = $k+1;
 6045:                         my $selstr;
 6046:                         if ($k == $numtop) {
 6047:                             $selstr = ' selected="selected" ';
 6048:                         }
 6049:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6050:                     }
 6051:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 6052:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 6053:                                   .'</tr>'."\n";
 6054:                     $itemcount ++;
 6055:                     foreach my $default ('instcode','communities') {
 6056:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 6057:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6058:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 6059:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 6060:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 6061:                             for (my $k=0; $k<=$maxnum; $k++) {
 6062:                                 my $vpos = $k+1;
 6063:                                 my $selstr;
 6064:                                 if ($k == $maxnum) {
 6065:                                     $selstr = ' selected="selected" ';
 6066:                                 }
 6067:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6068:                             }
 6069:                             $datatable .= '</select></span></td>'.
 6070:                                           '<td><span class="LC_nobreak">'.
 6071:                                           $default_names{$default}.'</span>';
 6072:                             if ($default eq 'instcode') {
 6073:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 6074:                                               .&mt('with institutional codes').')</span>';
 6075:                             }
 6076:                             $datatable .= '</td>'
 6077:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 6078:                                           .&mt('Display').'</label>&nbsp;'
 6079:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 6080:                                           .&mt('Do not display').'</label></span></td></tr>';
 6081:                         }
 6082:                     }
 6083:                 }
 6084:             } else {
 6085:                 $datatable .= &initialize_categories($itemcount);
 6086:             }
 6087:         } else {
 6088:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td>'
 6089:                           .&initialize_categories($itemcount);
 6090:         }
 6091:         $$rowtotal += $itemcount;
 6092:     }
 6093:     return $datatable;
 6094: }
 6095: 
 6096: sub print_serverstatuses {
 6097:     my ($dom,$settings,$rowtotal) = @_;
 6098:     my $datatable;
 6099:     my @pages = &serverstatus_pages();
 6100:     my (%namedaccess,%machineaccess);
 6101:     foreach my $type (@pages) {
 6102:         $namedaccess{$type} = '';
 6103:         $machineaccess{$type}= '';
 6104:     }
 6105:     if (ref($settings) eq 'HASH') {
 6106:         foreach my $type (@pages) {
 6107:             if (exists($settings->{$type})) {
 6108:                 if (ref($settings->{$type}) eq 'HASH') {
 6109:                     foreach my $key (keys(%{$settings->{$type}})) {
 6110:                         if ($key eq 'namedusers') {
 6111:                             $namedaccess{$type} = $settings->{$type}->{$key};
 6112:                         } elsif ($key eq 'machines') {
 6113:                             $machineaccess{$type} = $settings->{$type}->{$key};
 6114:                         }
 6115:                     }
 6116:                 }
 6117:             }
 6118:         }
 6119:     }
 6120:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 6121:     my $rownum = 0;
 6122:     my $css_class;
 6123:     foreach my $type (@pages) {
 6124:         $rownum ++;
 6125:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 6126:         $datatable .= '<tr'.$css_class.'>'.
 6127:                       '<td><span class="LC_nobreak">'.
 6128:                       $titles->{$type}.'</span></td>'.
 6129:                       '<td class="LC_left_item">'.
 6130:                       '<input type="text" name="'.$type.'_namedusers" '.
 6131:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 6132:                       '<td class="LC_right_item">'.
 6133:                       '<span class="LC_nobreak">'.
 6134:                       '<input type="text" name="'.$type.'_machines" '.
 6135:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 6136:                       '</td></tr>'."\n";
 6137:     }
 6138:     $$rowtotal += $rownum;
 6139:     return $datatable;
 6140: }
 6141: 
 6142: sub serverstatus_pages {
 6143:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 6144:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 6145:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 6146:             'uniquecodes','diskusage','coursecatalog');
 6147: }
 6148: 
 6149: sub defaults_javascript {
 6150:     my ($settings) = @_;
 6151:     my $intauthcheck = &mt('Warning: disallowing login for an authenticated user if the stored cost is less than the default will require a password reset by/for the user.');
 6152:     my $intauthcost = &mt('Warning: bcrypt encryption cost for internal authentication must be an integer.');
 6153:     &js_escape(\$intauthcheck);
 6154:     &js_escape(\$intauthcost);
 6155:     my $intauthjs = <<"ENDSCRIPT";
 6156: 
 6157: function warnIntAuth(field) {
 6158:     if (field.name == 'intauth_check') {
 6159:         if (field.value == '2') {
 6160:             alert('$intauthcheck');
 6161:         }
 6162:     }
 6163:     if (field.name == 'intauth_cost') {
 6164:         field.value.replace(/\s/g,'');
 6165:         if (field.value != '') {
 6166:             var regexdigit=/^\\d+\$/;
 6167:             if (!regexdigit.test(field.value)) {
 6168:                 alert('$intauthcost');
 6169:             }
 6170:         }
 6171:     }
 6172:     return;
 6173: }
 6174: 
 6175: ENDSCRIPT
 6176: 
 6177:     if (ref($settings) ne 'HASH') {
 6178:         return &Apache::lonhtmlcommon::scripttag($intauthjs);
 6179:     }
 6180:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 6181:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 6182:         if ($maxnum eq '') {
 6183:             $maxnum = 0;
 6184:         }
 6185:         $maxnum ++;
 6186:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 6187:         return <<"ENDSCRIPT";
 6188: <script type="text/javascript">
 6189: // <![CDATA[
 6190: function reorderTypes(form,caller) {
 6191:     var changedVal;
 6192: $jstext 
 6193:     var newpos = 'addinststatus_pos';
 6194:     var current = new Array;
 6195:     var maxh = $maxnum;
 6196:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 6197:     var oldVal;
 6198:     if (caller == newpos) {
 6199:         changedVal = newitemVal;
 6200:     } else {
 6201:         var curritem = 'inststatus_pos_'+caller;
 6202:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 6203:         current[newitemVal] = newpos;
 6204:     }
 6205:     for (var i=0; i<inststatuses.length; i++) {
 6206:         if (inststatuses[i] != caller) {
 6207:             var elementName = 'inststatus_pos_'+inststatuses[i];
 6208:             if (form.elements[elementName]) {
 6209:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 6210:                 current[currVal] = elementName;
 6211:             }
 6212:         }
 6213:     }
 6214:     for (var j=0; j<maxh; j++) {
 6215:         if (current[j] == undefined) {
 6216:             oldVal = j;
 6217:         }
 6218:     }
 6219:     if (oldVal < changedVal) {
 6220:         for (var k=oldVal+1; k<=changedVal ; k++) {
 6221:            var elementName = current[k];
 6222:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 6223:         }
 6224:     } else {
 6225:         for (var k=changedVal; k<oldVal; k++) {
 6226:             var elementName = current[k];
 6227:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 6228:         }
 6229:     }
 6230:     return;
 6231: }
 6232: 
 6233: $intauthjs
 6234: 
 6235: // ]]>
 6236: </script>
 6237: 
 6238: ENDSCRIPT
 6239:     } else {
 6240:         return &Apache::lonhtmlcommon::scripttag($intauthjs);
 6241:     }
 6242: }
 6243: 
 6244: sub coursecategories_javascript {
 6245:     my ($settings) = @_;
 6246:     my ($output,$jstext,$cathash);
 6247:     if (ref($settings) eq 'HASH') {
 6248:         $cathash = $settings->{'cats'};
 6249:     }
 6250:     if (ref($cathash) eq 'HASH') {
 6251:         my (@cats,@jsarray,%idx);
 6252:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 6253:         if (@jsarray > 0) {
 6254:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 6255:             for (my $i=0; $i<@jsarray; $i++) {
 6256:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 6257:                     my $catstr = join('","',@{$jsarray[$i]});
 6258:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 6259:                 }
 6260:             }
 6261:         }
 6262:     } else {
 6263:         $jstext  = '    var categories = Array(1);'."\n".
 6264:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 6265:     }
 6266:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 6267:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 6268:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
 6269:     &js_escape(\$instcode_reserved);
 6270:     &js_escape(\$communities_reserved);
 6271:     &js_escape(\$choose_again);
 6272:     $output = <<"ENDSCRIPT";
 6273: <script type="text/javascript">
 6274: // <![CDATA[
 6275: function reorderCats(form,parent,item,idx) {
 6276:     var changedVal;
 6277: $jstext
 6278:     var newpos = 'addcategory_pos';
 6279:     if (parent == '') {
 6280:         var has_instcode = 0;
 6281:         var maxtop = categories[idx].length;
 6282:         for (var j=0; j<maxtop; j++) {
 6283:             if (categories[idx][j] == 'instcode::0') {
 6284:                 has_instcode == 1;
 6285:             }
 6286:         }
 6287:         if (has_instcode == 0) {
 6288:             categories[idx][maxtop] = 'instcode_pos';
 6289:         }
 6290:     } else {
 6291:         newpos += '_'+parent;
 6292:     }
 6293:     var maxh = 1 + categories[idx].length;
 6294:     var current = new Array;
 6295:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 6296:     if (item == newpos) {
 6297:         changedVal = newitemVal;
 6298:     } else {
 6299:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 6300:         current[newitemVal] = newpos;
 6301:     }
 6302:     for (var i=0; i<categories[idx].length; i++) {
 6303:         var elementName = categories[idx][i];
 6304:         if (elementName != item) {
 6305:             if (form.elements[elementName]) {
 6306:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 6307:                 current[currVal] = elementName;
 6308:             }
 6309:         }
 6310:     }
 6311:     var oldVal;
 6312:     for (var j=0; j<maxh; j++) {
 6313:         if (current[j] == undefined) {
 6314:             oldVal = j;
 6315:         }
 6316:     }
 6317:     if (oldVal < changedVal) {
 6318:         for (var k=oldVal+1; k<=changedVal ; k++) {
 6319:            var elementName = current[k];
 6320:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 6321:         }
 6322:     } else {
 6323:         for (var k=changedVal; k<oldVal; k++) {
 6324:             var elementName = current[k];
 6325:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 6326:         }
 6327:     }
 6328:     return;
 6329: }
 6330: 
 6331: function categoryCheck(form) {
 6332:     if (form.elements['addcategory_name'].value == 'instcode') {
 6333:         alert('$instcode_reserved\\n$choose_again');
 6334:         return false;
 6335:     }
 6336:     if (form.elements['addcategory_name'].value == 'communities') {
 6337:         alert('$communities_reserved\\n$choose_again');
 6338:         return false;
 6339:     }
 6340:     return true;
 6341: }
 6342: 
 6343: // ]]>
 6344: </script>
 6345: 
 6346: ENDSCRIPT
 6347:     return $output;
 6348: }
 6349: 
 6350: sub initialize_categories {
 6351:     my ($itemcount) = @_;
 6352:     my ($datatable,$css_class,$chgstr);
 6353:     my %default_names = (
 6354:                       instcode    => 'Official courses (with institutional codes)',
 6355:                       communities => 'Communities',
 6356:                         );
 6357:     my $select0 = ' selected="selected"';
 6358:     my $select1 = '';
 6359:     foreach my $default ('instcode','communities') {
 6360:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6361:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 6362:         if ($default eq 'communities') {
 6363:             $select1 = $select0;
 6364:             $select0 = '';
 6365:         }
 6366:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6367:                      .'<select name="'.$default.'_pos">'
 6368:                      .'<option value="0"'.$select0.'>1</option>'
 6369:                      .'<option value="1"'.$select1.'>2</option>'
 6370:                      .'<option value="2">3</option></select>&nbsp;'
 6371:                      .$default_names{$default}
 6372:                      .'</span></td><td><span class="LC_nobreak">'
 6373:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 6374:                      .&mt('Display').'</label>&nbsp;<label>'
 6375:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 6376:                  .'</label></span></td></tr>';
 6377:         $itemcount ++;
 6378:     }
 6379:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6380:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 6381:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6382:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 6383:                   .'<option value="0">1</option>'
 6384:                   .'<option value="1">2</option>'
 6385:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 6386:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 6387:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 6388:     return $datatable;
 6389: }
 6390: 
 6391: sub build_category_rows {
 6392:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 6393:     my ($text,$name,$item,$chgstr);
 6394:     if (ref($cats) eq 'ARRAY') {
 6395:         my $maxdepth = scalar(@{$cats});
 6396:         if (ref($cats->[$depth]) eq 'HASH') {
 6397:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 6398:                 my $numchildren = @{$cats->[$depth]{$parent}};
 6399:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6400:                 $text .= '<td><table class="LC_data_table">';
 6401:                 my ($idxnum,$parent_name,$parent_item);
 6402:                 my $higher = $depth - 1;
 6403:                 if ($higher == 0) {
 6404:                     $parent_name = &escape($parent).'::'.$higher;
 6405:                 } else {
 6406:                     if (ref($path) eq 'ARRAY') {
 6407:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 6408:                     }
 6409:                 }
 6410:                 $parent_item = 'addcategory_pos_'.$parent_name;
 6411:                 for (my $j=0; $j<=$numchildren; $j++) {
 6412:                     if ($j < $numchildren) {
 6413:                         $name = $cats->[$depth]{$parent}[$j];
 6414:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 6415:                         $idxnum = $idx->{$item};
 6416:                     } else {
 6417:                         $name = $parent_name;
 6418:                         $item = $parent_item;
 6419:                     }
 6420:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 6421:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 6422:                     for (my $i=0; $i<=$numchildren; $i++) {
 6423:                         my $vpos = $i+1;
 6424:                         my $selstr;
 6425:                         if ($j == $i) {
 6426:                             $selstr = ' selected="selected" ';
 6427:                         }
 6428:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 6429:                     }
 6430:                     $text .= '</select>&nbsp;';
 6431:                     if ($j < $numchildren) {
 6432:                         my $deeper = $depth+1;
 6433:                         $text .= $name.'&nbsp;'
 6434:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 6435:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 6436:                         if(ref($path) eq 'ARRAY') {
 6437:                             push(@{$path},$name);
 6438:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 6439:                             pop(@{$path});
 6440:                         }
 6441:                     } else {
 6442:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 6443:                         if ($j == $numchildren) {
 6444:                             $text .= $name;
 6445:                         } else {
 6446:                             $text .= $item;
 6447:                         }
 6448:                         $text .= '" value="" />';
 6449:                     }
 6450:                     $text .= '</td></tr>';
 6451:                 }
 6452:                 $text .= '</table></td>';
 6453:             } else {
 6454:                 my $higher = $depth-1;
 6455:                 if ($higher == 0) {
 6456:                     $name = &escape($parent).'::'.$higher;
 6457:                 } else {
 6458:                     if (ref($path) eq 'ARRAY') {
 6459:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 6460:                     }
 6461:                 }
 6462:                 my $colspan;
 6463:                 if ($parent ne 'instcode') {
 6464:                     $colspan = $maxdepth - $depth - 1;
 6465:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 6466:                 }
 6467:             }
 6468:         }
 6469:     }
 6470:     return $text;
 6471: }
 6472: 
 6473: sub modifiable_userdata_row {
 6474:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref) = @_;
 6475:     my ($role,$rolename,$statustype);
 6476:     $role = $item;
 6477:     if ($context eq 'cancreate') {
 6478:         if ($item =~ /^emailusername_(.+)$/) {
 6479:             $statustype = $1;
 6480:             $role = 'emailusername';
 6481:             if (ref($usertypes) eq 'HASH') {
 6482:                 if ($usertypes->{$statustype}) {
 6483:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 6484:                 } else {
 6485:                     $rolename = &mt('Data provided by user');
 6486:                 }
 6487:             }
 6488:         }
 6489:     } elsif ($context eq 'selfcreate') {
 6490:         if (ref($usertypes) eq 'HASH') {
 6491:             $rolename = $usertypes->{$role};
 6492:         } else {
 6493:             $rolename = $role;
 6494:         }
 6495:     } else {
 6496:         if ($role eq 'cr') {
 6497:             $rolename = &mt('Custom role');
 6498:         } else {
 6499:             $rolename = &Apache::lonnet::plaintext($role);
 6500:         }
 6501:     }
 6502:     my (@fields,%fieldtitles);
 6503:     if (ref($fieldsref) eq 'ARRAY') {
 6504:         @fields = @{$fieldsref};
 6505:     } else {
 6506:         @fields = ('lastname','firstname','middlename','generation',
 6507:                    'permanentemail','id');
 6508:     }
 6509:     if ((ref($titlesref) eq 'HASH')) {
 6510:         %fieldtitles = %{$titlesref};
 6511:     } else {
 6512:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 6513:     }
 6514:     my $output;
 6515:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 6516:     $output = '<tr '.$css_class.'>'.
 6517:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 6518:               '<td class="LC_left_item" colspan="2"><table>';
 6519:     my $rem;
 6520:     my %checks;
 6521:     if (ref($settings) eq 'HASH') {
 6522:         if (ref($settings->{$context}) eq 'HASH') {
 6523:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 6524:                 my $hashref = $settings->{$context}->{$role};
 6525:                 if ($role eq 'emailusername') {
 6526:                     if ($statustype) {
 6527:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 6528:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 6529:                             if (ref($hashref) eq 'HASH') { 
 6530:                                 foreach my $field (@fields) {
 6531:                                     if ($hashref->{$field}) {
 6532:                                         $checks{$field} = $hashref->{$field};
 6533:                                     }
 6534:                                 }
 6535:                             }
 6536:                         }
 6537:                     }
 6538:                 } else {
 6539:                     if (ref($hashref) eq 'HASH') {
 6540:                         foreach my $field (@fields) {
 6541:                             if ($hashref->{$field}) {
 6542:                                 $checks{$field} = ' checked="checked" ';
 6543:                             }
 6544:                         }
 6545:                     }
 6546:                 }
 6547:             }
 6548:         }
 6549:     }
 6550:      
 6551:     for (my $i=0; $i<@fields; $i++) {
 6552:         my $rem = $i%($numinrow);
 6553:         if ($rem == 0) {
 6554:             if ($i > 0) {
 6555:                 $output .= '</tr>';
 6556:             }
 6557:             $output .= '<tr>';
 6558:         }
 6559:         my $check = ' ';
 6560:         unless ($role eq 'emailusername') {
 6561:             if (exists($checks{$fields[$i]})) {
 6562:                 $check = $checks{$fields[$i]}
 6563:             } else {
 6564:                 if ($role eq 'st') {
 6565:                     if (ref($settings) ne 'HASH') {
 6566:                         $check = ' checked="checked" '; 
 6567:                     }
 6568:                 }
 6569:             }
 6570:         }
 6571:         $output .= '<td class="LC_left_item">'.
 6572:                    '<span class="LC_nobreak">';
 6573:         if ($role eq 'emailusername') {
 6574:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 6575:                 $checks{$fields[$i]} = 'omit';
 6576:             }
 6577:             foreach my $option ('required','optional','omit') {
 6578:                 my $checked='';
 6579:                 if ($checks{$fields[$i]} eq $option) {
 6580:                     $checked='checked="checked" ';
 6581:                 }
 6582:                 $output .= '<label>'.
 6583:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 6584:                            &mt($option).'</label>'.('&nbsp;' x2);
 6585:             }
 6586:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 6587:         } else {
 6588:             $output .= '<label>'.
 6589:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 6590:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 6591:                        '</label>';
 6592:         }
 6593:         $output .= '</span></td>';
 6594:         $rem = @fields%($numinrow);
 6595:     }
 6596:     my $colsleft = $numinrow - $rem;
 6597:     if ($colsleft > 1 ) {
 6598:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 6599:                    '&nbsp;</td>';
 6600:     } elsif ($colsleft == 1) {
 6601:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 6602:     }
 6603:     $output .= '</tr></table></td></tr>';
 6604:     return $output;
 6605: }
 6606: 
 6607: sub insttypes_row {
 6608:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rownum) = @_;
 6609:     my %lt = &Apache::lonlocal::texthash (
 6610:                       cansearch => 'Users allowed to search',
 6611:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 6612:                       lockablenames => 'User preference to lock name',
 6613:              );
 6614:     my $showdom;
 6615:     if ($context eq 'cansearch') {
 6616:         $showdom = ' ('.$dom.')';
 6617:     }
 6618:     my $class = 'LC_left_item';
 6619:     if ($context eq 'statustocreate') {
 6620:         $class = 'LC_right_item';
 6621:     }
 6622:     my $css_class = ' class="LC_odd_row"';
 6623:     if ($rownum ne '') { 
 6624:         $css_class = ($rownum%2? ' class="LC_odd_row"':'');
 6625:     }
 6626:     my $output = '<tr'.$css_class.'>'.
 6627:                  '<td>'.$lt{$context}.$showdom.
 6628:                  '</td><td class="'.$class.'" colspan="2"><table>';
 6629:     my $rem;
 6630:     if (ref($types) eq 'ARRAY') {
 6631:         for (my $i=0; $i<@{$types}; $i++) {
 6632:             if (defined($usertypes->{$types->[$i]})) {
 6633:                 my $rem = $i%($numinrow);
 6634:                 if ($rem == 0) {
 6635:                     if ($i > 0) {
 6636:                         $output .= '</tr>';
 6637:                     }
 6638:                     $output .= '<tr>';
 6639:                 }
 6640:                 my $check = ' ';
 6641:                 if (ref($settings) eq 'HASH') {
 6642:                     if (ref($settings->{$context}) eq 'ARRAY') {
 6643:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 6644:                             $check = ' checked="checked" ';
 6645:                         }
 6646:                     } elsif ($context eq 'statustocreate') {
 6647:                         $check = ' checked="checked" ';
 6648:                     }
 6649:                 }
 6650:                 $output .= '<td class="LC_left_item">'.
 6651:                            '<span class="LC_nobreak"><label>'.
 6652:                            '<input type="checkbox" name="'.$context.'" '.
 6653:                            'value="'.$types->[$i].'"'.$check.'/>'.
 6654:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 6655:             }
 6656:         }
 6657:         $rem = @{$types}%($numinrow);
 6658:     }
 6659:     my $colsleft = $numinrow - $rem;
 6660:     if (($rem == 0) && (@{$types} > 0)) {
 6661:         $output .= '<tr>';
 6662:     }
 6663:     if ($colsleft > 1) {
 6664:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 6665:     } else {
 6666:         $output .= '<td class="LC_left_item">';
 6667:     }
 6668:     my $defcheck = ' ';
 6669:     if (ref($settings) eq 'HASH') {  
 6670:         if (ref($settings->{$context}) eq 'ARRAY') {
 6671:             if (grep(/^default$/,@{$settings->{$context}})) {
 6672:                 $defcheck = ' checked="checked" ';
 6673:             }
 6674:         } elsif ($context eq 'statustocreate') {
 6675:             $defcheck = ' checked="checked" ';
 6676:         }
 6677:     }
 6678:     $output .= '<span class="LC_nobreak"><label>'.
 6679:                '<input type="checkbox" name="'.$context.'" '.
 6680:                'value="default"'.$defcheck.'/>'.
 6681:                $othertitle.'</label></span></td>'.
 6682:                '</tr></table></td></tr>';
 6683:     return $output;
 6684: }
 6685: 
 6686: sub sorted_searchtitles {
 6687:     my %searchtitles = &Apache::lonlocal::texthash(
 6688:                          'uname' => 'username',
 6689:                          'lastname' => 'last name',
 6690:                          'lastfirst' => 'last name, first name',
 6691:                      );
 6692:     my @titleorder = ('uname','lastname','lastfirst');
 6693:     return (\%searchtitles,\@titleorder);
 6694: }
 6695: 
 6696: sub sorted_searchtypes {
 6697:     my %srchtypes_desc = (
 6698:                            exact    => 'is exact match',
 6699:                            contains => 'contains ..',
 6700:                            begins   => 'begins with ..',
 6701:                          );
 6702:     my @srchtypeorder = ('exact','begins','contains');
 6703:     return (\%srchtypes_desc,\@srchtypeorder);
 6704: }
 6705: 
 6706: sub usertype_update_row {
 6707:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 6708:     my $datatable;
 6709:     my $numinrow = 4;
 6710:     foreach my $type (@{$types}) {
 6711:         if (defined($usertypes->{$type})) {
 6712:             $$rownums ++;
 6713:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 6714:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 6715:                           '</td><td class="LC_left_item"><table>';
 6716:             for (my $i=0; $i<@{$fields}; $i++) {
 6717:                 my $rem = $i%($numinrow);
 6718:                 if ($rem == 0) {
 6719:                     if ($i > 0) {
 6720:                         $datatable .= '</tr>';
 6721:                     }
 6722:                     $datatable .= '<tr>';
 6723:                 }
 6724:                 my $check = ' ';
 6725:                 if (ref($settings) eq 'HASH') {
 6726:                     if (ref($settings->{'fields'}) eq 'HASH') {
 6727:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 6728:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 6729:                                 $check = ' checked="checked" ';
 6730:                             }
 6731:                         }
 6732:                     }
 6733:                 }
 6734: 
 6735:                 if ($i == @{$fields}-1) {
 6736:                     my $colsleft = $numinrow - $rem;
 6737:                     if ($colsleft > 1) {
 6738:                         $datatable .= '<td colspan="'.$colsleft.'">';
 6739:                     } else {
 6740:                         $datatable .= '<td>';
 6741:                     }
 6742:                 } else {
 6743:                     $datatable .= '<td>';
 6744:                 }
 6745:                 $datatable .= '<span class="LC_nobreak"><label>'.
 6746:                               '<input type="checkbox" name="updateable_'.$type.
 6747:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 6748:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 6749:             }
 6750:             $datatable .= '</tr></table></td></tr>';
 6751:         }
 6752:     }
 6753:     return $datatable;
 6754: }
 6755: 
 6756: sub modify_login {
 6757:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 6758:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 6759:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 6760:     %title = ( coursecatalog => 'Display course catalog',
 6761:                adminmail => 'Display administrator E-mail address',
 6762:                helpdesk  => 'Display "Contact Helpdesk" link',
 6763:                newuser => 'Link for visitors to create a user account',
 6764:                loginheader => 'Log-in box header');
 6765:     @offon = ('off','on');
 6766:     if (ref($domconfig{login}) eq 'HASH') {
 6767:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 6768:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 6769:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 6770:             }
 6771:         }
 6772:     }
 6773:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 6774:                                            \%domconfig,\%loginhash);
 6775:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 6776:     foreach my $item (@toggles) {
 6777:         $loginhash{login}{$item} = $env{'form.'.$item};
 6778:     }
 6779:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 6780:     if (ref($colchanges{'login'}) eq 'HASH') {  
 6781:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 6782:                                          \%loginhash);
 6783:     }
 6784: 
 6785:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 6786:     my %domservers = &Apache::lonnet::get_servers($dom);
 6787:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 6788:     if (keys(%servers) > 1) {
 6789:         foreach my $lonhost (keys(%servers)) {
 6790:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 6791:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 6792:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 6793:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 6794:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 6795:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 6796:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 6797:                         $changes{'loginvia'}{$lonhost} = 1;
 6798:                     } else {
 6799:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 6800:                         $changes{'loginvia'}{$lonhost} = 1;
 6801:                     }
 6802:                 } else {
 6803:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 6804:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 6805:                         $changes{'loginvia'}{$lonhost} = 1;
 6806:                     }
 6807:                 }
 6808:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 6809:                     foreach my $item (@loginvia_attribs) {
 6810:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 6811:                     }
 6812:                 } else {
 6813:                     foreach my $item (@loginvia_attribs) {
 6814:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 6815:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 6816:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 6817:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 6818:                                 $new = '/';
 6819:                             }
 6820:                         }
 6821:                         if (($item eq 'custompath') && 
 6822:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 6823:                             $new = '';
 6824:                         }
 6825:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 6826:                             $changes{'loginvia'}{$lonhost} = 1;
 6827:                         }
 6828:                         if ($item eq 'exempt') {
 6829:                             $new = &check_exempt_addresses($new);
 6830:                         }
 6831:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 6832:                     }
 6833:                 }
 6834:             } else {
 6835:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 6836:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 6837:                     $changes{'loginvia'}{$lonhost} = 1;
 6838:                     foreach my $item (@loginvia_attribs) {
 6839:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 6840:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 6841:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 6842:                                 $new = '/';
 6843:                             }
 6844:                         }
 6845:                         if (($item eq 'custompath') && 
 6846:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 6847:                             $new = '';
 6848:                         }
 6849:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 6850:                     }
 6851:                 }
 6852:             }
 6853:         }
 6854:     }
 6855: 
 6856:     my $servadm = $r->dir_config('lonAdmEMail');
 6857:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 6858:     if (ref($domconfig{'login'}) eq 'HASH') {
 6859:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 6860:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 6861:                 if ($lang eq 'nolang') {
 6862:                     push(@currlangs,$lang);
 6863:                 } elsif (defined($langchoices{$lang})) {
 6864:                     push(@currlangs,$lang);
 6865:                 } else {
 6866:                     next;
 6867:                 }
 6868:             }
 6869:         }
 6870:     }
 6871:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 6872:     if (@currlangs > 0) {
 6873:         foreach my $lang (@currlangs) {
 6874:             if (grep(/^\Q$lang\E$/,@delurls)) {
 6875:                 $changes{'helpurl'}{$lang} = 1;
 6876:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 6877:                 $changes{'helpurl'}{$lang} = 1;
 6878:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 6879:                 push(@newlangs,$lang);
 6880:             } else {
 6881:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 6882:             }
 6883:         }
 6884:     }
 6885:     unless (grep(/^nolang$/,@currlangs)) {
 6886:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 6887:             $changes{'helpurl'}{'nolang'} = 1;
 6888:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 6889:             push(@newlangs,'nolang');
 6890:         }
 6891:     }
 6892:     if ($env{'form.loginhelpurl_add_lang'}) {
 6893:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 6894:             ($env{'form.loginhelpurl_add_file.filename'})) {
 6895:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 6896:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 6897:         }
 6898:     }
 6899:     if ((@newlangs > 0) || ($addedfile)) {
 6900:         my $error;
 6901:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 6902:         if ($configuserok eq 'ok') {
 6903:             if ($switchserver) {
 6904:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 6905:             } elsif ($author_ok eq 'ok') {
 6906:                 my @allnew = @newlangs;
 6907:                 if ($addedfile ne '') {
 6908:                     push(@allnew,$addedfile);
 6909:                 }
 6910:                 foreach my $lang (@allnew) {
 6911:                     my $formelem = 'loginhelpurl_'.$lang;
 6912:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 6913:                         $formelem = 'loginhelpurl_add_file';
 6914:                     }
 6915:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 6916:                                                                "help/$lang",'','',$newfile{$lang});
 6917:                     if ($result eq 'ok') {
 6918:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 6919:                         $changes{'helpurl'}{$lang} = 1;
 6920:                     } else {
 6921:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 6922:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 6923:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 6924:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 6925:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 6926:                         }
 6927:                     }
 6928:                 }
 6929:             } else {
 6930:                 $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);
 6931:             }
 6932:         } else {
 6933:             $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);
 6934:         }
 6935:         if ($error) {
 6936:             &Apache::lonnet::logthis($error);
 6937:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6938:         }
 6939:     }
 6940: 
 6941:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
 6942:     if (ref($domconfig{'login'}) eq 'HASH') {
 6943:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
 6944:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
 6945:                 if ($domservers{$lonhost}) {
 6946:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 6947:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
 6948:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
 6949:                     }
 6950:                 }
 6951:             }
 6952:         }
 6953:     }
 6954:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
 6955:     foreach my $lonhost (sort(keys(%domservers))) {
 6956:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 6957:             $changes{'headtag'}{$lonhost} = 1;
 6958:         } else {
 6959:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
 6960:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
 6961:             }
 6962:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
 6963:                 push(@newhosts,$lonhost);
 6964:             } elsif ($currheadtagurls{$lonhost}) {
 6965:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
 6966:                 if ($currexempt{$lonhost}) {
 6967:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
 6968:                         $changes{'headtag'}{$lonhost} = 1;
 6969:                     }
 6970:                 } elsif ($possexempt{$lonhost}) {
 6971:                     $changes{'headtag'}{$lonhost} = 1;
 6972:                 }
 6973:                 if ($possexempt{$lonhost}) {
 6974:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 6975:                 }
 6976:             }
 6977:         }
 6978:     }
 6979:     if (@newhosts) {
 6980:         my $error;
 6981:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 6982:         if ($configuserok eq 'ok') {
 6983:             if ($switchserver) {
 6984:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
 6985:             } elsif ($author_ok eq 'ok') {
 6986:                 foreach my $lonhost (@newhosts) {
 6987:                     my $formelem = 'loginheadtag_'.$lonhost;
 6988:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 6989:                                                                           "login/headtag/$lonhost",'','',
 6990:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
 6991:                     if ($result eq 'ok') {
 6992:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
 6993:                         $changes{'headtag'}{$lonhost} = 1;
 6994:                         if ($possexempt{$lonhost}) {
 6995:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 6996:                         }
 6997:                     } else {
 6998:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
 6999:                                            $newheadtagurls{$lonhost},$result);
 7000:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 7001:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
 7002:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
 7003:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
 7004:                         }
 7005:                     }
 7006:                 }
 7007:             } else {
 7008:                 $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);
 7009:             }
 7010:         } else {
 7011:             $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);
 7012:         }
 7013:         if ($error) {
 7014:             &Apache::lonnet::logthis($error);
 7015:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7016:         }
 7017:     }
 7018:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 7019: 
 7020:     my $defaulthelpfile = '/adm/loginproblems.html';
 7021:     my $defaulttext = &mt('Default in use');
 7022: 
 7023:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 7024:                                              $dom);
 7025:     if ($putresult eq 'ok') {
 7026:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 7027:         my %defaultchecked = (
 7028:                     'coursecatalog' => 'on',
 7029:                     'helpdesk'      => 'on',
 7030:                     'adminmail'     => 'off',
 7031:                     'newuser'       => 'off',
 7032:         );
 7033:         if (ref($domconfig{'login'}) eq 'HASH') {
 7034:             foreach my $item (@toggles) {
 7035:                 if ($defaultchecked{$item} eq 'on') { 
 7036:                     if (($domconfig{'login'}{$item} eq '0') &&
 7037:                         ($env{'form.'.$item} eq '1')) {
 7038:                         $changes{$item} = 1;
 7039:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 7040:                               $domconfig{'login'}{$item} eq '1') &&
 7041:                              ($env{'form.'.$item} eq '0')) {
 7042:                         $changes{$item} = 1;
 7043:                     }
 7044:                 } elsif ($defaultchecked{$item} eq 'off') {
 7045:                     if (($domconfig{'login'}{$item} eq '1') &&
 7046:                         ($env{'form.'.$item} eq '0')) {
 7047:                         $changes{$item} = 1;
 7048:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 7049:                               $domconfig{'login'}{$item} eq '0') &&
 7050:                              ($env{'form.'.$item} eq '1')) {
 7051:                         $changes{$item} = 1;
 7052:                     }
 7053:                 }
 7054:             }
 7055:         }
 7056:         if (keys(%changes) > 0 || $colchgtext) {
 7057:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 7058:             if (ref($lastactref) eq 'HASH') {
 7059:                 $lastactref->{'domainconfig'} = 1;
 7060:             }
 7061:             $resulttext = &mt('Changes made:').'<ul>';
 7062:             foreach my $item (sort(keys(%changes))) {
 7063:                 if ($item eq 'loginvia') {
 7064:                     if (ref($changes{$item}) eq 'HASH') {
 7065:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 7066:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 7067:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 7068:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 7069:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 7070:                                     $protocol = 'http' if ($protocol ne 'https');
 7071:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 7072: 
 7073:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 7074:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 7075:                                     } else {
 7076:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 7077:                                     }
 7078:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 7079:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 7080:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 7081:                                     }
 7082:                                     $resulttext .= '</li>';
 7083:                                 } else {
 7084:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 7085:                                 }
 7086:                             } else {
 7087:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 7088:                             }
 7089:                         }
 7090:                         $resulttext .= '</ul></li>';
 7091:                     }
 7092:                 } elsif ($item eq 'helpurl') {
 7093:                     if (ref($changes{$item}) eq 'HASH') {
 7094:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 7095:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 7096:                                 my ($chg,$link);
 7097:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 7098:                                 if ($lang eq 'nolang') {
 7099:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 7100:                                 } else {
 7101:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 7102:                                 }
 7103:                                 $resulttext .= '<li>'.$chg.'</li>';
 7104:                             } else {
 7105:                                 my $chg;
 7106:                                 if ($lang eq 'nolang') {
 7107:                                     $chg = &mt('custom log-in help file for no preferred language');
 7108:                                 } else {
 7109:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 7110:                                 }
 7111:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 7112:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 7113:                                                       '?inhibitmenu=yes',$chg,600,500).
 7114:                                                '</li>';
 7115:                             }
 7116:                         }
 7117:                     }
 7118:                 } elsif ($item eq 'headtag') {
 7119:                     if (ref($changes{$item}) eq 'HASH') {
 7120:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 7121:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 7122:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
 7123:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 7124:                                 $resulttext .= '<li><a href="'.
 7125:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
 7126:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 7127:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
 7128:                                 if ($possexempt{$lonhost}) {
 7129:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
 7130:                                 } else {
 7131:                                     $resulttext .= &mt('included for any client IP');
 7132:                                 }
 7133:                                 $resulttext .= '</li>';
 7134:                             }
 7135:                         }
 7136:                     }
 7137:                 } elsif ($item eq 'captcha') {
 7138:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7139:                         my $chgtxt;
 7140:                         if ($loginhash{'login'}{$item} eq 'notused') {
 7141:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 7142:                         } else {
 7143:                             my %captchas = &captcha_phrases();
 7144:                             if ($captchas{$loginhash{'login'}{$item}}) {
 7145:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 7146:                             } else {
 7147:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 7148:                             }
 7149:                         }
 7150:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 7151:                     }
 7152:                 } elsif ($item eq 'recaptchakeys') {
 7153:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7154:                         my ($privkey,$pubkey);
 7155:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 7156:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 7157:                             $privkey = $loginhash{'login'}{$item}{'private'};
 7158:                         }
 7159:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 7160:                         if (!$pubkey) {
 7161:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 7162:                         } else {
 7163:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 7164:                         }
 7165:                         if (!$privkey) {
 7166:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 7167:                         } else {
 7168:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 7169:                         }
 7170:                         $chgtxt .= '</ul>';
 7171:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 7172:                     }
 7173:                 } elsif ($item eq 'recaptchaversion') {
 7174:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7175:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
 7176:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
 7177:                                            '</li>';
 7178:                         }
 7179:                     }
 7180:                 } else {
 7181:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 7182:                 }
 7183:             }
 7184:             $resulttext .= $colchgtext.'</ul>';
 7185:         } else {
 7186:             $resulttext = &mt('No changes made to log-in page settings');
 7187:         }
 7188:     } else {
 7189:         $resulttext = '<span class="LC_error">'.
 7190: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7191:     }
 7192:     if ($errors) {
 7193:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 7194:                        $errors.'</ul>';
 7195:     }
 7196:     return $resulttext;
 7197: }
 7198: 
 7199: sub check_exempt_addresses {
 7200:     my ($iplist) = @_;
 7201:     $iplist =~ s/^\s+//;
 7202:     $iplist =~ s/\s+$//;
 7203:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
 7204:     my (@okips,$new);
 7205:     foreach my $ip (@poss_ips) {
 7206:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 7207:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 7208:                 push(@okips,$ip);
 7209:             }
 7210:         }
 7211:     }
 7212:     if (@okips > 0) {
 7213:         $new = join(',',@okips);
 7214:     } else {
 7215:         $new = '';
 7216:     }
 7217:     return $new;
 7218: }
 7219: 
 7220: sub color_font_choices {
 7221:     my %choices =
 7222:         &Apache::lonlocal::texthash (
 7223:             img => "Header",
 7224:             bgs => "Background colors",
 7225:             links => "Link colors",
 7226:             images => "Images",
 7227:             font => "Font color",
 7228:             fontmenu => "Font menu",
 7229:             pgbg => "Page",
 7230:             tabbg => "Header",
 7231:             sidebg => "Border",
 7232:             link => "Link",
 7233:             alink => "Active link",
 7234:             vlink => "Visited link",
 7235:         );
 7236:     return %choices;
 7237: }
 7238: 
 7239: sub modify_rolecolors {
 7240:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 7241:     my ($resulttext,%rolehash);
 7242:     $rolehash{'rolecolors'} = {};
 7243:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 7244:         if ($domconfig{'rolecolors'} eq '') {
 7245:             $domconfig{'rolecolors'} = {};
 7246:         }
 7247:     }
 7248:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 7249:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 7250:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 7251:                                              $dom);
 7252:     if ($putresult eq 'ok') {
 7253:         if (keys(%changes) > 0) {
 7254:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 7255:             if (ref($lastactref) eq 'HASH') {
 7256:                 $lastactref->{'domainconfig'} = 1;
 7257:             }
 7258:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 7259:                                              $rolehash{'rolecolors'});
 7260:         } else {
 7261:             $resulttext = &mt('No changes made to default color schemes');
 7262:         }
 7263:     } else {
 7264:         $resulttext = '<span class="LC_error">'.
 7265: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7266:     }
 7267:     if ($errors) {
 7268:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 7269:                        $errors.'</ul>';
 7270:     }
 7271:     return $resulttext;
 7272: }
 7273: 
 7274: sub modify_colors {
 7275:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 7276:     my (%changes,%choices);
 7277:     my @bgs;
 7278:     my @links = ('link','alink','vlink');
 7279:     my @logintext;
 7280:     my @images;
 7281:     my $servadm = $r->dir_config('lonAdmEMail');
 7282:     my $errors;
 7283:     my %defaults;
 7284:     foreach my $role (@{$roles}) {
 7285:         if ($role eq 'login') {
 7286:             %choices = &login_choices();
 7287:             @logintext = ('textcol','bgcol');
 7288:         } else {
 7289:             %choices = &color_font_choices();
 7290:         }
 7291:         if ($role eq 'login') {
 7292:             @images = ('img','logo','domlogo','login');
 7293:             @bgs = ('pgbg','mainbg','sidebg');
 7294:         } else {
 7295:             @images = ('img');
 7296:             @bgs = ('pgbg','tabbg','sidebg');
 7297:         }
 7298:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 7299:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 7300:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 7301:         }
 7302:         if ($role eq 'login') {
 7303:             foreach my $item (@logintext) {
 7304:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 7305:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 7306:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 7307:                 }
 7308:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 7309:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 7310:                 }
 7311:             }
 7312:         } else {
 7313:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 7314:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 7315:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 7316:             }
 7317:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 7318:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 7319:             }
 7320:         }
 7321:         foreach my $item (@bgs) {
 7322:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 7323:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 7324:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 7325:             }
 7326:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 7327:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 7328:             }
 7329:         }
 7330:         foreach my $item (@links) {
 7331:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 7332:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 7333:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 7334:             }
 7335:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 7336:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 7337:             }
 7338:         }
 7339:         my ($configuserok,$author_ok,$switchserver) = 
 7340:             &config_check($dom,$confname,$servadm);
 7341:         my ($width,$height) = &thumb_dimensions();
 7342:         if (ref($domconfig->{$role}) ne 'HASH') {
 7343:             $domconfig->{$role} = {};
 7344:         }
 7345:         foreach my $img (@images) {
 7346:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 7347:                 if (defined($env{'form.login_showlogo_'.$img})) {
 7348:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 7349:                 } else { 
 7350:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 7351:                 }
 7352:             } 
 7353: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 7354: 		 && !defined($domconfig->{$role}{$img})
 7355: 		 && !$env{'form.'.$role.'_del_'.$img}
 7356: 		 && $env{'form.'.$role.'_import_'.$img}) {
 7357: 		# import the old configured image from the .tab setting
 7358: 		# if they haven't provided a new one 
 7359: 		$domconfig->{$role}{$img} = 
 7360: 		    $env{'form.'.$role.'_import_'.$img};
 7361: 	    }
 7362:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 7363:                 my $error;
 7364:                 if ($configuserok eq 'ok') {
 7365:                     if ($switchserver) {
 7366:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 7367:                     } else {
 7368:                         if ($author_ok eq 'ok') {
 7369:                             my ($result,$logourl) = 
 7370:                                 &publishlogo($r,'upload',$role.'_'.$img,
 7371:                                            $dom,$confname,$img,$width,$height);
 7372:                             if ($result eq 'ok') {
 7373:                                 $confhash->{$role}{$img} = $logourl;
 7374:                                 $changes{$role}{'images'}{$img} = 1;
 7375:                             } else {
 7376:                                 $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);
 7377:                             }
 7378:                         } else {
 7379:                             $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);
 7380:                         }
 7381:                     }
 7382:                 } else {
 7383:                     $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);
 7384:                 }
 7385:                 if ($error) {
 7386:                     &Apache::lonnet::logthis($error);
 7387:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7388:                 }
 7389:             } elsif ($domconfig->{$role}{$img} ne '') {
 7390:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 7391:                     my $error;
 7392:                     if ($configuserok eq 'ok') {
 7393: # is confname an author?
 7394:                         if ($switchserver eq '') {
 7395:                             if ($author_ok eq 'ok') {
 7396:                                 my ($result,$logourl) = 
 7397:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 7398:                                             $dom,$confname,$img,$width,$height);
 7399:                                 if ($result eq 'ok') {
 7400:                                     $confhash->{$role}{$img} = $logourl;
 7401: 				    $changes{$role}{'images'}{$img} = 1;
 7402:                                 }
 7403:                             }
 7404:                         }
 7405:                     }
 7406:                 }
 7407:             }
 7408:         }
 7409:         if (ref($domconfig) eq 'HASH') {
 7410:             if (ref($domconfig->{$role}) eq 'HASH') {
 7411:                 foreach my $img (@images) {
 7412:                     if ($domconfig->{$role}{$img} ne '') {
 7413:                         if ($env{'form.'.$role.'_del_'.$img}) {
 7414:                             $confhash->{$role}{$img} = '';
 7415:                             $changes{$role}{'images'}{$img} = 1;
 7416:                         } else {
 7417:                             if ($confhash->{$role}{$img} eq '') {
 7418:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 7419:                             }
 7420:                         }
 7421:                     } else {
 7422:                         if ($env{'form.'.$role.'_del_'.$img}) {
 7423:                             $confhash->{$role}{$img} = '';
 7424:                             $changes{$role}{'images'}{$img} = 1;
 7425:                         } 
 7426:                     }
 7427:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 7428:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 7429:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 7430:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 7431:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 7432:                             }
 7433:                         } else {
 7434:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 7435:                                 $changes{$role}{'showlogo'}{$img} = 1;
 7436:                             }
 7437:                         }
 7438:                     }
 7439:                 }
 7440:                 if ($domconfig->{$role}{'font'} ne '') {
 7441:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 7442:                         $changes{$role}{'font'} = 1;
 7443:                     }
 7444:                 } else {
 7445:                     if ($confhash->{$role}{'font'}) {
 7446:                         $changes{$role}{'font'} = 1;
 7447:                     }
 7448:                 }
 7449:                 if ($role ne 'login') {
 7450:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 7451:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 7452:                             $changes{$role}{'fontmenu'} = 1;
 7453:                         }
 7454:                     } else {
 7455:                         if ($confhash->{$role}{'fontmenu'}) {
 7456:                             $changes{$role}{'fontmenu'} = 1;
 7457:                         }
 7458:                     }
 7459:                 }
 7460:                 foreach my $item (@bgs) {
 7461:                     if ($domconfig->{$role}{$item} ne '') {
 7462:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 7463:                             $changes{$role}{'bgs'}{$item} = 1;
 7464:                         } 
 7465:                     } else {
 7466:                         if ($confhash->{$role}{$item}) {
 7467:                             $changes{$role}{'bgs'}{$item} = 1;
 7468:                         }
 7469:                     }
 7470:                 }
 7471:                 foreach my $item (@links) {
 7472:                     if ($domconfig->{$role}{$item} ne '') {
 7473:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 7474:                             $changes{$role}{'links'}{$item} = 1;
 7475:                         }
 7476:                     } else {
 7477:                         if ($confhash->{$role}{$item}) {
 7478:                             $changes{$role}{'links'}{$item} = 1;
 7479:                         }
 7480:                     }
 7481:                 }
 7482:                 foreach my $item (@logintext) {
 7483:                     if ($domconfig->{$role}{$item} ne '') {
 7484:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 7485:                             $changes{$role}{'logintext'}{$item} = 1;
 7486:                         }
 7487:                     } else {
 7488:                         if ($confhash->{$role}{$item}) {
 7489:                             $changes{$role}{'logintext'}{$item} = 1;
 7490:                         }
 7491:                     }
 7492:                 }
 7493:             } else {
 7494:                 &default_change_checker($role,\@images,\@links,\@bgs,
 7495:                                         \@logintext,$confhash,\%changes); 
 7496:             }
 7497:         } else {
 7498:             &default_change_checker($role,\@images,\@links,\@bgs,
 7499:                                     \@logintext,$confhash,\%changes); 
 7500:         }
 7501:     }
 7502:     return ($errors,%changes);
 7503: }
 7504: 
 7505: sub config_check {
 7506:     my ($dom,$confname,$servadm) = @_;
 7507:     my ($configuserok,$author_ok,$switchserver,%currroles);
 7508:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 7509:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 7510:                                                    $confname,$servadm);
 7511:     if ($configuserok eq 'ok') {
 7512:         $switchserver = &check_switchserver($dom,$confname);
 7513:         if ($switchserver eq '') {
 7514:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 7515:         }
 7516:     }
 7517:     return ($configuserok,$author_ok,$switchserver);
 7518: }
 7519: 
 7520: sub default_change_checker {
 7521:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 7522:     foreach my $item (@{$links}) {
 7523:         if ($confhash->{$role}{$item}) {
 7524:             $changes->{$role}{'links'}{$item} = 1;
 7525:         }
 7526:     }
 7527:     foreach my $item (@{$bgs}) {
 7528:         if ($confhash->{$role}{$item}) {
 7529:             $changes->{$role}{'bgs'}{$item} = 1;
 7530:         }
 7531:     }
 7532:     foreach my $item (@{$logintext}) {
 7533:         if ($confhash->{$role}{$item}) {
 7534:             $changes->{$role}{'logintext'}{$item} = 1;
 7535:         }
 7536:     }
 7537:     foreach my $img (@{$images}) {
 7538:         if ($env{'form.'.$role.'_del_'.$img}) {
 7539:             $confhash->{$role}{$img} = '';
 7540:             $changes->{$role}{'images'}{$img} = 1;
 7541:         }
 7542:         if ($role eq 'login') {
 7543:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 7544:                 $changes->{$role}{'showlogo'}{$img} = 1;
 7545:             }
 7546:         }
 7547:     }
 7548:     if ($confhash->{$role}{'font'}) {
 7549:         $changes->{$role}{'font'} = 1;
 7550:     }
 7551: }
 7552: 
 7553: sub display_colorchgs {
 7554:     my ($dom,$changes,$roles,$confhash) = @_;
 7555:     my (%choices,$resulttext);
 7556:     if (!grep(/^login$/,@{$roles})) {
 7557:         $resulttext = &mt('Changes made:').'<br />';
 7558:     }
 7559:     foreach my $role (@{$roles}) {
 7560:         if ($role eq 'login') {
 7561:             %choices = &login_choices();
 7562:         } else {
 7563:             %choices = &color_font_choices();
 7564:         }
 7565:         if (ref($changes->{$role}) eq 'HASH') {
 7566:             if ($role ne 'login') {
 7567:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 7568:             }
 7569:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 7570:                 if ($role ne 'login') {
 7571:                     $resulttext .= '<ul>';
 7572:                 }
 7573:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 7574:                     if ($role ne 'login') {
 7575:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 7576:                     }
 7577:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 7578:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 7579:                             if ($confhash->{$role}{$key}{$item}) {
 7580:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 7581:                             } else {
 7582:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 7583:                             }
 7584:                         } elsif ($confhash->{$role}{$item} eq '') {
 7585:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 7586:                         } else {
 7587:                             my $newitem = $confhash->{$role}{$item};
 7588:                             if ($key eq 'images') {
 7589:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 7590:                             }
 7591:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 7592:                         }
 7593:                     }
 7594:                     if ($role ne 'login') {
 7595:                         $resulttext .= '</ul></li>';
 7596:                     }
 7597:                 } else {
 7598:                     if ($confhash->{$role}{$key} eq '') {
 7599:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 7600:                     } else {
 7601:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 7602:                     }
 7603:                 }
 7604:                 if ($role ne 'login') {
 7605:                     $resulttext .= '</ul>';
 7606:                 }
 7607:             }
 7608:         }
 7609:     }
 7610:     return $resulttext;
 7611: }
 7612: 
 7613: sub thumb_dimensions {
 7614:     return ('200','50');
 7615: }
 7616: 
 7617: sub check_dimensions {
 7618:     my ($inputfile) = @_;
 7619:     my ($fullwidth,$fullheight);
 7620:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 7621:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 7622:             my $imageinfo = <PIPE>;
 7623:             if (!close(PIPE)) {
 7624:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 7625:             }
 7626:             chomp($imageinfo);
 7627:             my ($fullsize) = 
 7628:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 7629:             if ($fullsize) {
 7630:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 7631:             }
 7632:         }
 7633:     }
 7634:     return ($fullwidth,$fullheight);
 7635: }
 7636: 
 7637: sub check_configuser {
 7638:     my ($uhome,$dom,$confname,$servadm) = @_;
 7639:     my ($configuserok,%currroles);
 7640:     if ($uhome eq 'no_host') {
 7641:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 7642:         my $configpass = &LONCAPA::Enrollment::create_password();
 7643:         $configuserok = 
 7644:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 7645:                              $configpass,'','','','','',undef,$servadm);
 7646:     } else {
 7647:         $configuserok = 'ok';
 7648:         %currroles = 
 7649:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 7650:     }
 7651:     return ($configuserok,%currroles);
 7652: }
 7653: 
 7654: sub check_authorstatus {
 7655:     my ($dom,$confname,%currroles) = @_;
 7656:     my $author_ok;
 7657:     if (!$currroles{':'.$dom.':au'}) {
 7658:         my $start = time;
 7659:         my $end = 0;
 7660:         $author_ok = 
 7661:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 7662:                                         'au',$end,$start,'','','domconfig');
 7663:     } else {
 7664:         $author_ok = 'ok';
 7665:     }
 7666:     return $author_ok;
 7667: }
 7668: 
 7669: sub publishlogo {
 7670:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 7671:     my ($output,$fname,$logourl);
 7672:     if ($action eq 'upload') {
 7673:         $fname=$env{'form.'.$formname.'.filename'};
 7674:         chop($env{'form.'.$formname});
 7675:     } else {
 7676:         ($fname) = ($formname =~ /([^\/]+)$/);
 7677:     }
 7678:     if ($savefileas ne '') {
 7679:         $fname = $savefileas;
 7680:     }
 7681:     $fname=&Apache::lonnet::clean_filename($fname);
 7682: # See if there is anything left
 7683:     unless ($fname) { return ('error: no uploaded file'); }
 7684:     $fname="$subdir/$fname";
 7685:     my $docroot=$r->dir_config('lonDocRoot');
 7686:     my $filepath="$docroot/priv";
 7687:     my $relpath = "$dom/$confname";
 7688:     my ($fnamepath,$file,$fetchthumb);
 7689:     $file=$fname;
 7690:     if ($fname=~m|/|) {
 7691:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 7692:     }
 7693:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 7694:     my $count;
 7695:     for ($count=5;$count<=$#parts;$count++) {
 7696:         $filepath.="/$parts[$count]";
 7697:         if ((-e $filepath)!=1) {
 7698:             mkdir($filepath,02770);
 7699:         }
 7700:     }
 7701:     # Check for bad extension and disallow upload
 7702:     if ($file=~/\.(\w+)$/ &&
 7703:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 7704:         $output = 
 7705:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 7706:     } elsif ($file=~/\.(\w+)$/ &&
 7707:         !defined(&Apache::loncommon::fileembstyle($1))) {
 7708:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 7709:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 7710:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 7711:     } elsif (-d "$filepath/$file") {
 7712:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 7713:     } else {
 7714:         my $source = $filepath.'/'.$file;
 7715:         my $logfile;
 7716:         if (!open($logfile,">>$source".'.log')) {
 7717:             return (&mt('No write permission to Authoring Space'));
 7718:         }
 7719:         print $logfile
 7720: "\n================= Publish ".localtime()." ================\n".
 7721: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 7722: # Save the file
 7723:         if (!open(FH,'>'.$source)) {
 7724:             &Apache::lonnet::logthis('Failed to create '.$source);
 7725:             return (&mt('Failed to create file'));
 7726:         }
 7727:         if ($action eq 'upload') {
 7728:             if (!print FH ($env{'form.'.$formname})) {
 7729:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 7730:                 return (&mt('Failed to write file'));
 7731:             }
 7732:         } else {
 7733:             my $original = &Apache::lonnet::filelocation('',$formname);
 7734:             if(!copy($original,$source)) {
 7735:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 7736:                 return (&mt('Failed to write file'));
 7737:             }
 7738:         }
 7739:         close(FH);
 7740:         chmod(0660, $source); # Permissions to rw-rw---.
 7741: 
 7742:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 7743:         my $copyfile=$targetdir.'/'.$file;
 7744: 
 7745:         my @parts=split(/\//,$targetdir);
 7746:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 7747:         for (my $count=5;$count<=$#parts;$count++) {
 7748:             $path.="/$parts[$count]";
 7749:             if (!-e $path) {
 7750:                 print $logfile "\nCreating directory ".$path;
 7751:                 mkdir($path,02770);
 7752:             }
 7753:         }
 7754:         my $versionresult;
 7755:         if (-e $copyfile) {
 7756:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 7757:         } else {
 7758:             $versionresult = 'ok';
 7759:         }
 7760:         if ($versionresult eq 'ok') {
 7761:             if (copy($source,$copyfile)) {
 7762:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 7763:                 $output = 'ok';
 7764:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 7765:                 push(@{$modified_urls},[$copyfile,$source]);
 7766:                 my $metaoutput = 
 7767:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 7768:                 unless ($registered_cleanup) {
 7769:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 7770:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 7771:                     $registered_cleanup=1;
 7772:                 }
 7773:             } else {
 7774:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 7775:                 $output = &mt('Failed to copy file to RES space').", $!";
 7776:             }
 7777:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 7778:                 my $inputfile = $filepath.'/'.$file;
 7779:                 my $outfile = $filepath.'/'.'tn-'.$file;
 7780:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 7781:                 if ($fullwidth ne '' && $fullheight ne '') { 
 7782:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 7783:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 7784:                         system("convert -sample $thumbsize $inputfile $outfile");
 7785:                         chmod(0660, $filepath.'/tn-'.$file);
 7786:                         if (-e $outfile) {
 7787:                             my $copyfile=$targetdir.'/tn-'.$file;
 7788:                             if (copy($outfile,$copyfile)) {
 7789:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 7790:                                 my $thumb_metaoutput = 
 7791:                                     &write_metadata($dom,$confname,$formname,
 7792:                                                     $targetdir,'tn-'.$file,$logfile);
 7793:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 7794:                                 unless ($registered_cleanup) {
 7795:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 7796:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 7797:                                     $registered_cleanup=1;
 7798:                                 }
 7799:                             } else {
 7800:                                 print $logfile "\nUnable to write ".$copyfile.
 7801:                                                ':'.$!."\n";
 7802:                             }
 7803:                         }
 7804:                     }
 7805:                 }
 7806:             }
 7807:         } else {
 7808:             $output = $versionresult;
 7809:         }
 7810:     }
 7811:     return ($output,$logourl);
 7812: }
 7813: 
 7814: sub logo_versioning {
 7815:     my ($targetdir,$file,$logfile) = @_;
 7816:     my $target = $targetdir.'/'.$file;
 7817:     my ($maxversion,$fn,$extn,$output);
 7818:     $maxversion = 0;
 7819:     if ($file =~ /^(.+)\.(\w+)$/) {
 7820:         $fn=$1;
 7821:         $extn=$2;
 7822:     }
 7823:     opendir(DIR,$targetdir);
 7824:     while (my $filename=readdir(DIR)) {
 7825:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 7826:             $maxversion=($1>$maxversion)?$1:$maxversion;
 7827:         }
 7828:     }
 7829:     $maxversion++;
 7830:     print $logfile "\nCreating old version ".$maxversion."\n";
 7831:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 7832:     if (copy($target,$copyfile)) {
 7833:         print $logfile "Copied old target to ".$copyfile."\n";
 7834:         $copyfile=$copyfile.'.meta';
 7835:         if (copy($target.'.meta',$copyfile)) {
 7836:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 7837:             $output = 'ok';
 7838:         } else {
 7839:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 7840:             $output = &mt('Failed to copy old meta').", $!, ";
 7841:         }
 7842:     } else {
 7843:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 7844:         $output = &mt('Failed to copy old target').", $!, ";
 7845:     }
 7846:     return $output;
 7847: }
 7848: 
 7849: sub write_metadata {
 7850:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 7851:     my (%metadatafields,%metadatakeys,$output);
 7852:     $metadatafields{'title'}=$formname;
 7853:     $metadatafields{'creationdate'}=time;
 7854:     $metadatafields{'lastrevisiondate'}=time;
 7855:     $metadatafields{'copyright'}='public';
 7856:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 7857:                                          $env{'user.domain'};
 7858:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 7859:     $metadatafields{'domain'}=$dom;
 7860:     {
 7861:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 7862:         my $mfh;
 7863:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 7864:             foreach (sort(keys(%metadatafields))) {
 7865:                 unless ($_=~/\./) {
 7866:                     my $unikey=$_;
 7867:                     $unikey=~/^([A-Za-z]+)/;
 7868:                     my $tag=$1;
 7869:                     $tag=~tr/A-Z/a-z/;
 7870:                     print $mfh "\n\<$tag";
 7871:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 7872:                         my $value=$metadatafields{$unikey.'.'.$_};
 7873:                         $value=~s/\"/\'\'/g;
 7874:                         print $mfh ' '.$_.'="'.$value.'"';
 7875:                     }
 7876:                     print $mfh '>'.
 7877:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 7878:                             .'</'.$tag.'>';
 7879:                 }
 7880:             }
 7881:             $output = 'ok';
 7882:             print $logfile "\nWrote metadata";
 7883:             close($mfh);
 7884:         } else {
 7885:             print $logfile "\nFailed to open metadata file";
 7886:             $output = &mt('Could not write metadata');
 7887:         }
 7888:     }
 7889:     return $output;
 7890: }
 7891: 
 7892: sub notifysubscribed {
 7893:     foreach my $targetsource (@{$modified_urls}){
 7894:         next unless (ref($targetsource) eq 'ARRAY');
 7895:         my ($target,$source)=@{$targetsource};
 7896:         if ($source ne '') {
 7897:             if (open(my $logfh,'>>'.$source.'.log')) {
 7898:                 print $logfh "\nCleanup phase: Notifications\n";
 7899:                 my @subscribed=&subscribed_hosts($target);
 7900:                 foreach my $subhost (@subscribed) {
 7901:                     print $logfh "\nNotifying host ".$subhost.':';
 7902:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 7903:                     print $logfh $reply;
 7904:                 }
 7905:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 7906:                 foreach my $subhost (@subscribedmeta) {
 7907:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 7908:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 7909:                                                         $subhost);
 7910:                     print $logfh $reply;
 7911:                 }
 7912:                 print $logfh "\n============ Done ============\n";
 7913:                 close($logfh);
 7914:             }
 7915:         }
 7916:     }
 7917:     return OK;
 7918: }
 7919: 
 7920: sub subscribed_hosts {
 7921:     my ($target) = @_;
 7922:     my @subscribed;
 7923:     if (open(my $fh,"<$target.subscription")) {
 7924:         while (my $subline=<$fh>) {
 7925:             if ($subline =~ /^($match_lonid):/) {
 7926:                 my $host = $1;
 7927:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 7928:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 7929:                         push(@subscribed,$host);
 7930:                     }
 7931:                 }
 7932:             }
 7933:         }
 7934:     }
 7935:     return @subscribed;
 7936: }
 7937: 
 7938: sub check_switchserver {
 7939:     my ($dom,$confname) = @_;
 7940:     my ($allowed,$switchserver);
 7941:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 7942:     if ($home eq 'no_host') {
 7943:         $home = &Apache::lonnet::domain($dom,'primary');
 7944:     }
 7945:     my @ids=&Apache::lonnet::current_machine_ids();
 7946:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 7947:     if (!$allowed) {
 7948: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 7949:     }
 7950:     return $switchserver;
 7951: }
 7952: 
 7953: sub modify_quotas {
 7954:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 7955:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 7956:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 7957:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 7958:         $validationfieldsref);
 7959:     if ($action eq 'quotas') {
 7960:         $context = 'tools'; 
 7961:     } else {
 7962:         $context = $action;
 7963:     }
 7964:     if ($context eq 'requestcourses') {
 7965:         @usertools = ('official','unofficial','community','textbook');
 7966:         @options =('norequest','approval','validate','autolimit');
 7967:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 7968:         %titles = &courserequest_titles();
 7969:         $toolregexp = join('|',@usertools);
 7970:         %conditions = &courserequest_conditions();
 7971:         $confname = $dom.'-domainconfig';
 7972:         my $servadm = $r->dir_config('lonAdmEMail');
 7973:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 7974:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 7975:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 7976:     } elsif ($context eq 'requestauthor') {
 7977:         @usertools = ('author');
 7978:         %titles = &authorrequest_titles();
 7979:     } else {
 7980:         @usertools = ('aboutme','blog','webdav','portfolio');
 7981:         %titles = &tool_titles();
 7982:     }
 7983:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 7984:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7985:     foreach my $key (keys(%env)) {
 7986:         if ($context eq 'requestcourses') {
 7987:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 7988:                 my $item = $1;
 7989:                 my $type = $2;
 7990:                 if ($type =~ /^limit_(.+)/) {
 7991:                     $limithash{$item}{$1} = $env{$key};
 7992:                 } else {
 7993:                     $confhash{$item}{$type} = $env{$key};
 7994:                 }
 7995:             }
 7996:         } elsif ($context eq 'requestauthor') {
 7997:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 7998:                 $confhash{$1} = $env{$key};
 7999:             }
 8000:         } else {
 8001:             if ($key =~ /^form\.quota_(.+)$/) {
 8002:                 $confhash{'defaultquota'}{$1} = $env{$key};
 8003:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 8004:                 $confhash{'authorquota'}{$1} = $env{$key};
 8005:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 8006:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 8007:             }
 8008:         }
 8009:     }
 8010:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 8011:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 8012:         @approvalnotify = sort(@approvalnotify);
 8013:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 8014:         my @crstypes = ('official','unofficial','community','textbook');
 8015:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 8016:         foreach my $type (@hasuniquecode) {
 8017:             if (grep(/^\Q$type\E$/,@crstypes)) {
 8018:                 $confhash{'uniquecode'}{$type} = 1;
 8019:             }
 8020:         }
 8021:         my (%newbook,%allpos);
 8022:         if ($context eq 'requestcourses') {
 8023:             foreach my $type ('textbooks','templates') {
 8024:                 @{$allpos{$type}} = (); 
 8025:                 my $invalid;
 8026:                 if ($type eq 'textbooks') {
 8027:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
 8028:                 } else {
 8029:                     $invalid = &mt('Invalid LON-CAPA course for template');
 8030:                 }
 8031:                 if ($env{'form.'.$type.'_addbook'}) {
 8032:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
 8033:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
 8034:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
 8035:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
 8036:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 8037:                         } else {
 8038:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
 8039:                             my $position = $env{'form.'.$type.'_addbook_pos'};
 8040:                             $position =~ s/\D+//g;
 8041:                             if ($position ne '') {
 8042:                                 $allpos{$type}[$position] = $newbook{$type};
 8043:                             }
 8044:                         }
 8045:                     } else {
 8046:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 8047:                     }
 8048:                 }
 8049:             } 
 8050:         }
 8051:         if (ref($domconfig{$action}) eq 'HASH') {
 8052:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 8053:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 8054:                     $changes{'notify'}{'approval'} = 1;
 8055:                 }
 8056:             } else {
 8057:                 if ($confhash{'notify'}{'approval'}) {
 8058:                     $changes{'notify'}{'approval'} = 1;
 8059:                 }
 8060:             }
 8061:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 8062:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 8063:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 8064:                         unless ($confhash{'uniquecode'}{$crstype}) {
 8065:                             $changes{'uniquecode'} = 1;
 8066:                         }
 8067:                     }
 8068:                     unless ($changes{'uniquecode'}) {
 8069:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 8070:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 8071:                                 $changes{'uniquecode'} = 1;
 8072:                             }
 8073:                         }
 8074:                     }
 8075:                } else {
 8076:                    $changes{'uniquecode'} = 1;
 8077:                }
 8078:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 8079:                 $changes{'uniquecode'} = 1;
 8080:             }
 8081:             if ($context eq 'requestcourses') {
 8082:                 foreach my $type ('textbooks','templates') {
 8083:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
 8084:                         my %deletions;
 8085:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
 8086:                         if (@todelete) {
 8087:                             map { $deletions{$_} = 1; } @todelete;
 8088:                         }
 8089:                         my %imgdeletions;
 8090:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
 8091:                         if (@todeleteimages) {
 8092:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
 8093:                         }
 8094:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
 8095:                         for (my $i=0; $i<=$maxnum; $i++) {
 8096:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
 8097:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
 8098:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
 8099:                                 if ($deletions{$key}) {
 8100:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
 8101:                                         #FIXME need to obsolete item in RES space
 8102:                                     }
 8103:                                     next;
 8104:                                 } else {
 8105:                                     my $newpos = $env{'form.'.$itemid};
 8106:                                     $newpos =~ s/\D+//g;
 8107:                                     foreach my $item ('subject','title','publisher','author') {
 8108:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 8109:                                                  ($type eq 'templates'));
 8110:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
 8111:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
 8112:                                             $changes{$type}{$key} = 1;
 8113:                                         }
 8114:                                     }
 8115:                                     $allpos{$type}[$newpos] = $key;
 8116:                                 }
 8117:                                 if ($imgdeletions{$key}) {
 8118:                                     $changes{$type}{$key} = 1;
 8119:                                     #FIXME need to obsolete item in RES space
 8120:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
 8121:                                     my ($cdom,$cnum) = split(/_/,$key);
 8122:                                     my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
 8123:                                                                                   $cdom,$cnum,$type,$configuserok,
 8124:                                                                                   $switchserver,$author_ok);
 8125:                                     if ($imgurl) {
 8126:                                         $confhash{$type}{$key}{'image'} = $imgurl;
 8127:                                         $changes{$type}{$key} = 1; 
 8128:                                     }
 8129:                                     if ($error) {
 8130:                                         &Apache::lonnet::logthis($error);
 8131:                                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8132:                                     } 
 8133:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
 8134:                                     $confhash{$type}{$key}{'image'} = 
 8135:                                         $domconfig{$action}{$type}{$key}{'image'};
 8136:                                 }
 8137:                             }
 8138:                         }
 8139:                     }
 8140:                 }
 8141:             }
 8142:         } else {
 8143:             if ($confhash{'notify'}{'approval'}) {
 8144:                 $changes{'notify'}{'approval'} = 1;
 8145:             }
 8146:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 8147:                 $changes{'uniquecode'} = 1;
 8148:             }
 8149:         }
 8150:         if ($context eq 'requestcourses') {
 8151:             foreach my $type ('textbooks','templates') {
 8152:                 if ($newbook{$type}) {
 8153:                     $changes{$type}{$newbook{$type}} = 1;
 8154:                     foreach my $item ('subject','title','publisher','author') {
 8155:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 8156:                                  ($type eq 'template'));
 8157:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
 8158:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
 8159:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
 8160:                         }
 8161:                     }
 8162:                     if ($type eq 'textbooks') {
 8163:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
 8164:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
 8165:                             my ($imageurl,$error) =
 8166:                                 &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
 8167:                                                         $configuserok,$switchserver,$author_ok);
 8168:                             if ($imageurl) {
 8169:                                 $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
 8170:                             }
 8171:                             if ($error) {
 8172:                                 &Apache::lonnet::logthis($error);
 8173:                                 $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8174:                             }
 8175:                         }
 8176:                     }
 8177:                 }
 8178:                 if (@{$allpos{$type}} > 0) {
 8179:                     my $idx = 0;
 8180:                     foreach my $item (@{$allpos{$type}}) {
 8181:                         if ($item ne '') {
 8182:                             $confhash{$type}{$item}{'order'} = $idx;
 8183:                             if (ref($domconfig{$action}) eq 'HASH') {
 8184:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
 8185:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
 8186:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
 8187:                                             $changes{$type}{$item} = 1;
 8188:                                         }
 8189:                                     }
 8190:                                 }
 8191:                             }
 8192:                             $idx ++;
 8193:                         }
 8194:                     }
 8195:                 }
 8196:             }
 8197:             if (ref($validationitemsref) eq 'ARRAY') {
 8198:                 foreach my $item (@{$validationitemsref}) {
 8199:                     if ($item eq 'fields') {
 8200:                         my @changed;
 8201:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 8202:                         if (@{$confhash{'validation'}{$item}} > 0) {
 8203:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 8204:                         }
 8205:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8206:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8207:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 8208:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 8209:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
 8210:                                 } else {
 8211:                                     @changed = @{$confhash{'validation'}{$item}};
 8212:                                 }
 8213:                             } else {
 8214:                                 @changed = @{$confhash{'validation'}{$item}};
 8215:                             }
 8216:                         } else {
 8217:                             @changed = @{$confhash{'validation'}{$item}};
 8218:                         }
 8219:                         if (@changed) {
 8220:                             if ($confhash{'validation'}{$item}) {
 8221:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 8222:                             } else {
 8223:                                 $changes{'validation'}{$item} = &mt('None');
 8224:                             }
 8225:                         }
 8226:                     } else {
 8227:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 8228:                         if ($item eq 'markup') {
 8229:                             if ($env{'form.requestcourses_validation_'.$item}) {
 8230:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 8231:                             }
 8232:                         }
 8233:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8234:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8235:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 8236:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 8237:                                 }
 8238:                             } else {
 8239:                                 if ($confhash{'validation'}{$item} ne '') {
 8240:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 8241:                                 }
 8242:                             }
 8243:                         } else {
 8244:                             if ($confhash{'validation'}{$item} ne '') {
 8245:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 8246:                             }
 8247:                         }
 8248:                     }
 8249:                 }
 8250:             }
 8251:             if ($env{'form.validationdc'}) {
 8252:                 my $newval = $env{'form.validationdc'};
 8253:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
 8254:                 if (exists($domcoords{$newval})) {
 8255:                     $confhash{'validation'}{'dc'} = $newval;
 8256:                 }
 8257:             }
 8258:             if (ref($confhash{'validation'}) eq 'HASH') {
 8259:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8260:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8261:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 8262:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 8263:                                 if ($confhash{'validation'}{'dc'} eq '') {
 8264:                                     $changes{'validation'}{'dc'} = &mt('None');
 8265:                                 } else {
 8266:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8267:                                 }
 8268:                             }
 8269:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
 8270:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8271:                         }
 8272:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 8273:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8274:                     }
 8275:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 8276:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8277:                 }
 8278:             } else {
 8279:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8280:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8281:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 8282:                             $changes{'validation'}{'dc'} = &mt('None');
 8283:                         }
 8284:                     }
 8285:                 }
 8286:             }
 8287:         }
 8288:     } else {
 8289:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 8290:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 8291:     }
 8292:     foreach my $item (@usertools) {
 8293:         foreach my $type (@{$types},'default','_LC_adv') {
 8294:             my $unset; 
 8295:             if ($context eq 'requestcourses') {
 8296:                 $unset = '0';
 8297:                 if ($type eq '_LC_adv') {
 8298:                     $unset = '';
 8299:                 }
 8300:                 if ($confhash{$item}{$type} eq 'autolimit') {
 8301:                     $confhash{$item}{$type} .= '=';
 8302:                     unless ($limithash{$item}{$type} =~ /\D/) {
 8303:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 8304:                     }
 8305:                 }
 8306:             } elsif ($context eq 'requestauthor') {
 8307:                 $unset = '0';
 8308:                 if ($type eq '_LC_adv') {
 8309:                     $unset = '';
 8310:                 }
 8311:             } else {
 8312:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 8313:                     $confhash{$item}{$type} = 1;
 8314:                 } else {
 8315:                     $confhash{$item}{$type} = 0;
 8316:                 }
 8317:             }
 8318:             if (ref($domconfig{$action}) eq 'HASH') {
 8319:                 if ($action eq 'requestauthor') {
 8320:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 8321:                         $changes{$type} = 1;
 8322:                     }
 8323:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 8324:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 8325:                         $changes{$item}{$type} = 1;
 8326:                     }
 8327:                 } else {
 8328:                     if ($context eq 'requestcourses') {
 8329:                         if ($confhash{$item}{$type} ne $unset) {
 8330:                             $changes{$item}{$type} = 1;
 8331:                         }
 8332:                     } else {
 8333:                         if (!$confhash{$item}{$type}) {
 8334:                             $changes{$item}{$type} = 1;
 8335:                         }
 8336:                     }
 8337:                 }
 8338:             } else {
 8339:                 if ($context eq 'requestcourses') {
 8340:                     if ($confhash{$item}{$type} ne $unset) {
 8341:                         $changes{$item}{$type} = 1;
 8342:                     }
 8343:                 } elsif ($context eq 'requestauthor') {
 8344:                     if ($confhash{$type} ne $unset) {
 8345:                         $changes{$type} = 1;
 8346:                     }
 8347:                 } else {
 8348:                     if (!$confhash{$item}{$type}) {
 8349:                         $changes{$item}{$type} = 1;
 8350:                     }
 8351:                 }
 8352:             }
 8353:         }
 8354:     }
 8355:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 8356:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 8357:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 8358:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 8359:                     if (exists($confhash{'defaultquota'}{$key})) {
 8360:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 8361:                             $changes{'defaultquota'}{$key} = 1;
 8362:                         }
 8363:                     } else {
 8364:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 8365:                     }
 8366:                 }
 8367:             } else {
 8368:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 8369:                     if (exists($confhash{'defaultquota'}{$key})) {
 8370:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 8371:                             $changes{'defaultquota'}{$key} = 1;
 8372:                         }
 8373:                     } else {
 8374:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 8375:                     }
 8376:                 }
 8377:             }
 8378:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 8379:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 8380:                     if (exists($confhash{'authorquota'}{$key})) {
 8381:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 8382:                             $changes{'authorquota'}{$key} = 1;
 8383:                         }
 8384:                     } else {
 8385:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 8386:                     }
 8387:                 }
 8388:             }
 8389:         }
 8390:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 8391:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 8392:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 8393:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 8394:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 8395:                             $changes{'defaultquota'}{$key} = 1;
 8396:                         }
 8397:                     } else {
 8398:                         if (!exists($domconfig{'quotas'}{$key})) {
 8399:                             $changes{'defaultquota'}{$key} = 1;
 8400:                         }
 8401:                     }
 8402:                 } else {
 8403:                     $changes{'defaultquota'}{$key} = 1;
 8404:                 }
 8405:             }
 8406:         }
 8407:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 8408:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 8409:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 8410:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 8411:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 8412:                             $changes{'authorquota'}{$key} = 1;
 8413:                         }
 8414:                     } else {
 8415:                         $changes{'authorquota'}{$key} = 1;
 8416:                     }
 8417:                 } else {
 8418:                     $changes{'authorquota'}{$key} = 1;
 8419:                 }
 8420:             }
 8421:         }
 8422:     }
 8423: 
 8424:     if ($context eq 'requestauthor') {
 8425:         $domdefaults{'requestauthor'} = \%confhash;
 8426:     } else {
 8427:         foreach my $key (keys(%confhash)) {
 8428:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
 8429:                 $domdefaults{$key} = $confhash{$key};
 8430:             }
 8431:         }
 8432:     }
 8433: 
 8434:     my %quotahash = (
 8435:                       $action => { %confhash }
 8436:                     );
 8437:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 8438:                                              $dom);
 8439:     if ($putresult eq 'ok') {
 8440:         if (keys(%changes) > 0) {
 8441:             my $cachetime = 24*60*60;
 8442:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 8443:             if (ref($lastactref) eq 'HASH') {
 8444:                 $lastactref->{'domdefaults'} = 1;
 8445:             }
 8446:             $resulttext = &mt('Changes made:').'<ul>';
 8447:             unless (($context eq 'requestcourses') ||
 8448:                     ($context eq 'requestauthor')) {
 8449:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 8450:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 8451:                     foreach my $type (@{$types},'default') {
 8452:                         if (defined($changes{'defaultquota'}{$type})) {
 8453:                             my $typetitle = $usertypes->{$type};
 8454:                             if ($type eq 'default') {
 8455:                                 $typetitle = $othertitle;
 8456:                             }
 8457:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 8458:                         }
 8459:                     }
 8460:                     $resulttext .= '</ul></li>';
 8461:                 }
 8462:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 8463:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 8464:                     foreach my $type (@{$types},'default') {
 8465:                         if (defined($changes{'authorquota'}{$type})) {
 8466:                             my $typetitle = $usertypes->{$type};
 8467:                             if ($type eq 'default') {
 8468:                                 $typetitle = $othertitle;
 8469:                             }
 8470:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 8471:                         }
 8472:                     }
 8473:                     $resulttext .= '</ul></li>';
 8474:                 }
 8475:             }
 8476:             my %newenv;
 8477:             foreach my $item (@usertools) {
 8478:                 my (%haschgs,%inconf);
 8479:                 if ($context eq 'requestauthor') {
 8480:                     %haschgs = %changes;
 8481:                     %inconf = %confhash;
 8482:                 } else {
 8483:                     if (ref($changes{$item}) eq 'HASH') {
 8484:                         %haschgs = %{$changes{$item}};
 8485:                     }
 8486:                     if (ref($confhash{$item}) eq 'HASH') {
 8487:                         %inconf = %{$confhash{$item}};
 8488:                     }
 8489:                 }
 8490:                 if (keys(%haschgs) > 0) {
 8491:                     my $newacc = 
 8492:                         &Apache::lonnet::usertools_access($env{'user.name'},
 8493:                                                           $env{'user.domain'},
 8494:                                                           $item,'reload',$context);
 8495:                     if (($context eq 'requestcourses') ||
 8496:                         ($context eq 'requestauthor')) {
 8497:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 8498:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 8499:                         }
 8500:                     } else {
 8501:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 8502:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 8503:                         }
 8504:                     }
 8505:                     unless ($context eq 'requestauthor') {
 8506:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 8507:                     }
 8508:                     foreach my $type (@{$types},'default','_LC_adv') {
 8509:                         if ($haschgs{$type}) {
 8510:                             my $typetitle = $usertypes->{$type};
 8511:                             if ($type eq 'default') {
 8512:                                 $typetitle = $othertitle;
 8513:                             } elsif ($type eq '_LC_adv') {
 8514:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 8515:                             }
 8516:                             if ($inconf{$type}) {
 8517:                                 if ($context eq 'requestcourses') {
 8518:                                     my $cond;
 8519:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 8520:                                         if ($1 eq '') {
 8521:                                             $cond = &mt('(Automatic processing of any request).');
 8522:                                         } else {
 8523:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 8524:                                         }
 8525:                                     } else { 
 8526:                                         $cond = $conditions{$inconf{$type}};
 8527:                                     }
 8528:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 8529:                                 } elsif ($context eq 'requestauthor') {
 8530:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 8531:                                                              $titles{$inconf{$type}},$typetitle);
 8532: 
 8533:                                 } else {
 8534:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 8535:                                 }
 8536:                             } else {
 8537:                                 if ($type eq '_LC_adv') {
 8538:                                     if ($inconf{$type} eq '0') {
 8539:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 8540:                                     } else { 
 8541:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 8542:                                     }
 8543:                                 } else {
 8544:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 8545:                                 }
 8546:                             }
 8547:                         }
 8548:                     }
 8549:                     unless ($context eq 'requestauthor') {
 8550:                         $resulttext .= '</ul></li>';
 8551:                     }
 8552:                 }
 8553:             }
 8554:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 8555:                 if (ref($changes{'notify'}) eq 'HASH') {
 8556:                     if ($changes{'notify'}{'approval'}) {
 8557:                         if (ref($confhash{'notify'}) eq 'HASH') {
 8558:                             if ($confhash{'notify'}{'approval'}) {
 8559:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 8560:                             } else {
 8561:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 8562:                             }
 8563:                         }
 8564:                     }
 8565:                 }
 8566:             }
 8567:             if ($action eq 'requestcourses') {
 8568:                 my @offon = ('off','on');
 8569:                 if ($changes{'uniquecode'}) {
 8570:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 8571:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 8572:                         $resulttext .= '<li>'.
 8573:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 8574:                                        '</li>';
 8575:                     } else {
 8576:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 8577:                                        '</li>';
 8578:                     }
 8579:                 }
 8580:                 foreach my $type ('textbooks','templates') {
 8581:                     if (ref($changes{$type}) eq 'HASH') {
 8582:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
 8583:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
 8584:                             my %coursehash = &Apache::lonnet::coursedescription($key);
 8585:                             my $coursetitle = $coursehash{'description'};
 8586:                             my $position = $confhash{$type}{$key}{'order'} + 1;
 8587:                             $resulttext .= '<li>';
 8588:                             foreach my $item ('subject','title','publisher','author') {
 8589:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
 8590:                                          ($type eq 'templates'));
 8591:                                 my $name = $item.':';
 8592:                                 $name =~ s/^(\w)/\U$1/;
 8593:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
 8594:                             }
 8595:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 8596:                             if ($type eq 'textbooks') {
 8597:                                 if ($confhash{$type}{$key}{'image'}) {
 8598:                                     $resulttext .= ' '.&mt('Image: [_1]',
 8599:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
 8600:                                                    ' alt="Textbook cover" />').'<br />';
 8601:                                 }
 8602:                             }
 8603:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 8604:                         }
 8605:                         $resulttext .= '</ul></li>';
 8606:                     }
 8607:                 }
 8608:                 if (ref($changes{'validation'}) eq 'HASH') {
 8609:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 8610:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 8611:                         foreach my $item (@{$validationitemsref}) {
 8612:                             if (exists($changes{'validation'}{$item})) {
 8613:                                 if ($item eq 'markup') {
 8614:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 8615:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 8616:                                 } else {
 8617:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 8618:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 8619:                                 }
 8620:                             }
 8621:                         }
 8622:                         if (exists($changes{'validation'}{'dc'})) {
 8623:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 8624:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 8625:                         }
 8626:                     }
 8627:                 }
 8628:             }
 8629:             $resulttext .= '</ul>';
 8630:             if (keys(%newenv)) {
 8631:                 &Apache::lonnet::appenv(\%newenv);
 8632:             }
 8633:         } else {
 8634:             if ($context eq 'requestcourses') {
 8635:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 8636:             } elsif ($context eq 'requestauthor') {
 8637:                 $resulttext = &mt('No changes made to rights to request author space.');
 8638:             } else {
 8639:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 8640:             }
 8641:         }
 8642:     } else {
 8643:         $resulttext = '<span class="LC_error">'.
 8644: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 8645:     }
 8646:     if ($errors) {
 8647:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 8648:                        '<ul>'.$errors.'</ul></p>';
 8649:     }
 8650:     return $resulttext;
 8651: }
 8652: 
 8653: sub process_textbook_image {
 8654:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
 8655:     my $filename = $env{'form.'.$caller.'.filename'};
 8656:     my ($error,$url);
 8657:     my ($width,$height) = (50,50);
 8658:     if ($configuserok eq 'ok') {
 8659:         if ($switchserver) {
 8660:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 8661:                          $switchserver);
 8662:         } elsif ($author_ok eq 'ok') {
 8663:             my ($result,$imageurl) =
 8664:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 8665:                              "$type/$dom/$cnum/cover",$width,$height);
 8666:             if ($result eq 'ok') {
 8667:                 $url = $imageurl;
 8668:             } else {
 8669:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 8670:             }
 8671:         } else {
 8672:             $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);
 8673:         }
 8674:     } else {
 8675:         $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);
 8676:     }
 8677:     return ($url,$error);
 8678: }
 8679: 
 8680: sub modify_autoenroll {
 8681:     my ($dom,$lastactref,%domconfig) = @_;
 8682:     my ($resulttext,%changes);
 8683:     my %currautoenroll;
 8684:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 8685:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 8686:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 8687:         }
 8688:     }
 8689:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 8690:     my %title = ( run => 'Auto-enrollment active',
 8691:                   sender => 'Sender for notification messages',
 8692:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
 8693:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
 8694:     my @offon = ('off','on');
 8695:     my $sender_uname = $env{'form.sender_uname'};
 8696:     my $sender_domain = $env{'form.sender_domain'};
 8697:     if ($sender_domain eq '') {
 8698:         $sender_uname = '';
 8699:     } elsif ($sender_uname eq '') {
 8700:         $sender_domain = '';
 8701:     }
 8702:     my $coowners = $env{'form.autoassign_coowners'};
 8703:     my $failsafe = $env{'form.autoenroll_failsafe'};
 8704:     $failsafe =~ s{^\s+|\s+$}{}g;
 8705:     if ($failsafe =~ /\D/) {
 8706:         undef($failsafe);
 8707:     }
 8708:     my %autoenrollhash =  (
 8709:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 8710:                                        'sender_uname' => $sender_uname,
 8711:                                        'sender_domain' => $sender_domain,
 8712:                                        'co-owners' => $coowners,
 8713:                                        'autofailsafe' => $failsafe,
 8714:                                 }
 8715:                      );
 8716:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 8717:                                              $dom);
 8718:     if ($putresult eq 'ok') {
 8719:         if (exists($currautoenroll{'run'})) {
 8720:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 8721:                  $changes{'run'} = 1;
 8722:              }
 8723:         } elsif ($autorun) {
 8724:             if ($env{'form.autoenroll_run'} ne '1') {
 8725:                  $changes{'run'} = 1;
 8726:             }
 8727:         }
 8728:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 8729:             $changes{'sender'} = 1;
 8730:         }
 8731:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 8732:             $changes{'sender'} = 1;
 8733:         }
 8734:         if ($currautoenroll{'co-owners'} ne '') {
 8735:             if ($currautoenroll{'co-owners'} ne $coowners) {
 8736:                 $changes{'coowners'} = 1;
 8737:             }
 8738:         } elsif ($coowners) {
 8739:             $changes{'coowners'} = 1;
 8740:         }
 8741:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
 8742:             $changes{'autofailsafe'} = 1;
 8743:         }
 8744:         if (keys(%changes) > 0) {
 8745:             $resulttext = &mt('Changes made:').'<ul>';
 8746:             if ($changes{'run'}) {
 8747:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 8748:             }
 8749:             if ($changes{'sender'}) {
 8750:                 if ($sender_uname eq '' || $sender_domain eq '') {
 8751:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 8752:                 } else {
 8753:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 8754:                 }
 8755:             }
 8756:             if ($changes{'coowners'}) {
 8757:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 8758:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 8759:                 if (ref($lastactref) eq 'HASH') {
 8760:                     $lastactref->{'domainconfig'} = 1;
 8761:                 }
 8762:             }
 8763:             if ($changes{'autofailsafe'}) {
 8764:                 if ($failsafe ne '') {
 8765:                     $resulttext .= '<li>'.&mt("$title{'failsafe'} set to [_1]",$failsafe).'</li>';
 8766:                 } else {
 8767:                     $resulttext .= '<li>'.&mt("$title{'failsafe'} deleted");
 8768:                 }
 8769:                 &Apache::lonnet::get_domain_defaults($dom,1);
 8770:                 if (ref($lastactref) eq 'HASH') {
 8771:                     $lastactref->{'domdefaults'} = 1;
 8772:                 }
 8773:             }
 8774:             $resulttext .= '</ul>';
 8775:         } else {
 8776:             $resulttext = &mt('No changes made to auto-enrollment settings');
 8777:         }
 8778:     } else {
 8779:         $resulttext = '<span class="LC_error">'.
 8780: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 8781:     }
 8782:     return $resulttext;
 8783: }
 8784: 
 8785: sub modify_autoupdate {
 8786:     my ($dom,%domconfig) = @_;
 8787:     my ($resulttext,%currautoupdate,%fields,%changes);
 8788:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 8789:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 8790:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 8791:         }
 8792:     }
 8793:     my @offon = ('off','on');
 8794:     my %title = &Apache::lonlocal::texthash (
 8795:                    run => 'Auto-update:',
 8796:                    classlists => 'Updates to user information in classlists?'
 8797:                 );
 8798:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8799:     my %fieldtitles = &Apache::lonlocal::texthash (
 8800:                         id => 'Student/Employee ID',
 8801:                         permanentemail => 'E-mail address',
 8802:                         lastname => 'Last Name',
 8803:                         firstname => 'First Name',
 8804:                         middlename => 'Middle Name',
 8805:                         generation => 'Generation',
 8806:                       );
 8807:     $othertitle = &mt('All users');
 8808:     if (keys(%{$usertypes}) >  0) {
 8809:         $othertitle = &mt('Other users');
 8810:     }
 8811:     foreach my $key (keys(%env)) {
 8812:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 8813:             my ($usertype,$item) = ($1,$2);
 8814:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 8815:                 if ($usertype eq 'default') {   
 8816:                     push(@{$fields{$1}},$2);
 8817:                 } elsif (ref($types) eq 'ARRAY') {
 8818:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 8819:                         push(@{$fields{$1}},$2);
 8820:                     }
 8821:                 }
 8822:             }
 8823:         }
 8824:     }
 8825:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 8826:     @lockablenames = sort(@lockablenames);
 8827:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 8828:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 8829:         if (@changed) {
 8830:             $changes{'lockablenames'} = 1;
 8831:         }
 8832:     } else {
 8833:         if (@lockablenames) {
 8834:             $changes{'lockablenames'} = 1;
 8835:         }
 8836:     }
 8837:     my %updatehash = (
 8838:                       autoupdate => { run => $env{'form.autoupdate_run'},
 8839:                                       classlists => $env{'form.classlists'},
 8840:                                       fields => {%fields},
 8841:                                       lockablenames => \@lockablenames,
 8842:                                     }
 8843:                      );
 8844:     foreach my $key (keys(%currautoupdate)) {
 8845:         if (($key eq 'run') || ($key eq 'classlists')) {
 8846:             if (exists($updatehash{autoupdate}{$key})) {
 8847:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 8848:                     $changes{$key} = 1;
 8849:                 }
 8850:             }
 8851:         } elsif ($key eq 'fields') {
 8852:             if (ref($currautoupdate{$key}) eq 'HASH') {
 8853:                 foreach my $item (@{$types},'default') {
 8854:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 8855:                         my $change = 0;
 8856:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 8857:                             if (!exists($fields{$item})) {
 8858:                                 $change = 1;
 8859:                                 last;
 8860:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 8861:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 8862:                                     $change = 1;
 8863:                                     last;
 8864:                                 }
 8865:                             }
 8866:                         }
 8867:                         if ($change) {
 8868:                             push(@{$changes{$key}},$item);
 8869:                         }
 8870:                     } 
 8871:                 }
 8872:             }
 8873:         } elsif ($key eq 'lockablenames') {
 8874:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 8875:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 8876:                 if (@changed) {
 8877:                     $changes{'lockablenames'} = 1;
 8878:                 }
 8879:             } else {
 8880:                 if (@lockablenames) {
 8881:                     $changes{'lockablenames'} = 1;
 8882:                 }
 8883:             }
 8884:         }
 8885:     }
 8886:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 8887:         if (@lockablenames) {
 8888:             $changes{'lockablenames'} = 1;
 8889:         }
 8890:     }
 8891:     foreach my $item (@{$types},'default') {
 8892:         if (defined($fields{$item})) {
 8893:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 8894:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 8895:                     my $change = 0;
 8896:                     if (ref($fields{$item}) eq 'ARRAY') {
 8897:                         foreach my $type (@{$fields{$item}}) {
 8898:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 8899:                                 $change = 1;
 8900:                                 last;
 8901:                             }
 8902:                         }
 8903:                     }
 8904:                     if ($change) {
 8905:                         push(@{$changes{'fields'}},$item);
 8906:                     }
 8907:                 } else {
 8908:                     push(@{$changes{'fields'}},$item);
 8909:                 }
 8910:             } else {
 8911:                 push(@{$changes{'fields'}},$item);
 8912:             }
 8913:         }
 8914:     }
 8915:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 8916:                                              $dom);
 8917:     if ($putresult eq 'ok') {
 8918:         if (keys(%changes) > 0) {
 8919:             $resulttext = &mt('Changes made:').'<ul>';
 8920:             foreach my $key (sort(keys(%changes))) {
 8921:                 if ($key eq 'lockablenames') {
 8922:                     $resulttext .= '<li>';
 8923:                     if (@lockablenames) {
 8924:                         $usertypes->{'default'} = $othertitle;
 8925:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 8926:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 8927:                     } else {
 8928:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 8929:                     }
 8930:                     $resulttext .= '</li>';
 8931:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 8932:                     foreach my $item (@{$changes{$key}}) {
 8933:                         my @newvalues;
 8934:                         foreach my $type (@{$fields{$item}}) {
 8935:                             push(@newvalues,$fieldtitles{$type});
 8936:                         }
 8937:                         my $newvaluestr;
 8938:                         if (@newvalues > 0) {
 8939:                             $newvaluestr = join(', ',@newvalues);
 8940:                         } else {
 8941:                             $newvaluestr = &mt('none');
 8942:                         }
 8943:                         if ($item eq 'default') {
 8944:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 8945:                         } else {
 8946:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 8947:                         }
 8948:                     }
 8949:                 } else {
 8950:                     my $newvalue;
 8951:                     if ($key eq 'run') {
 8952:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 8953:                     } else {
 8954:                         $newvalue = $offon[$env{'form.'.$key}];
 8955:                     }
 8956:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 8957:                 }
 8958:             }
 8959:             $resulttext .= '</ul>';
 8960:         } else {
 8961:             $resulttext = &mt('No changes made to autoupdates');
 8962:         }
 8963:     } else {
 8964:         $resulttext = '<span class="LC_error">'.
 8965: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 8966:     }
 8967:     return $resulttext;
 8968: }
 8969: 
 8970: sub modify_autocreate {
 8971:     my ($dom,%domconfig) = @_;
 8972:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 8973:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 8974:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 8975:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 8976:         }
 8977:     }
 8978:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 8979:                  req => 'Auto-creation of validated requests for official courses',
 8980:                  xmldc => 'Identity of course creator of courses from XML files',
 8981:                );
 8982:     my @types = ('xml','req');
 8983:     foreach my $item (@types) {
 8984:         $newvals{$item} = $env{'form.autocreate_'.$item};
 8985:         $newvals{$item} =~ s/\D//g;
 8986:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 8987:     }
 8988:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 8989:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
 8990:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 8991:         $newvals{'xmldc'} = '';
 8992:     } 
 8993:     %autocreatehash =  (
 8994:                         autocreate => { xml => $newvals{'xml'},
 8995:                                         req => $newvals{'req'},
 8996:                                       }
 8997:                        );
 8998:     if ($newvals{'xmldc'} ne '') {
 8999:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 9000:     }
 9001:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 9002:                                              $dom);
 9003:     if ($putresult eq 'ok') {
 9004:         my @items = @types;
 9005:         if ($newvals{'xml'}) {
 9006:             push(@items,'xmldc');
 9007:         }
 9008:         foreach my $item (@items) {
 9009:             if (exists($currautocreate{$item})) {
 9010:                 if ($currautocreate{$item} ne $newvals{$item}) {
 9011:                     $changes{$item} = 1;
 9012:                 }
 9013:             } elsif ($newvals{$item}) {
 9014:                 $changes{$item} = 1;
 9015:             }
 9016:         }
 9017:         if (keys(%changes) > 0) {
 9018:             my @offon = ('off','on'); 
 9019:             $resulttext = &mt('Changes made:').'<ul>';
 9020:             foreach my $item (@types) {
 9021:                 if ($changes{$item}) {
 9022:                     my $newtxt = $offon[$newvals{$item}];
 9023:                     $resulttext .= '<li>'.
 9024:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
 9025:                                        '<b>','</b>').
 9026:                                    '</li>';
 9027:                 }
 9028:             }
 9029:             if ($changes{'xmldc'}) {
 9030:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 9031:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 9032:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
 9033:             }
 9034:             $resulttext .= '</ul>';
 9035:         } else {
 9036:             $resulttext = &mt('No changes made to auto-creation settings');
 9037:         }
 9038:     } else {
 9039:         $resulttext = '<span class="LC_error">'.
 9040:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9041:     }
 9042:     return $resulttext;
 9043: }
 9044: 
 9045: sub modify_directorysrch {
 9046:     my ($dom,%domconfig) = @_;
 9047:     my ($resulttext,%changes);
 9048:     my %currdirsrch;
 9049:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 9050:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 9051:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 9052:         }
 9053:     }
 9054:     my %title = ( available => 'Institutional directory search available',
 9055:                   localonly => 'Other domains can search institution',
 9056:                   lcavailable => 'LON-CAPA directory search available',
 9057:                   lclocalonly => 'Other domains can search LON-CAPA domain',
 9058:                   searchby => 'Search types',
 9059:                   searchtypes => 'Search latitude');
 9060:     my @offon = ('off','on');
 9061:     my @otherdoms = ('Yes','No');
 9062: 
 9063:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 9064:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 9065:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 9066: 
 9067:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9068:     if (keys(%{$usertypes}) == 0) {
 9069:         @cansearch = ('default');
 9070:     } else {
 9071:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 9072:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 9073:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 9074:                     push(@{$changes{'cansearch'}},$type);
 9075:                 }
 9076:             }
 9077:             foreach my $type (@cansearch) {
 9078:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 9079:                     push(@{$changes{'cansearch'}},$type);
 9080:                 }
 9081:             }
 9082:         } else {
 9083:             push(@{$changes{'cansearch'}},@cansearch);
 9084:         }
 9085:     }
 9086: 
 9087:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 9088:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 9089:             if (!grep(/^\Q$by\E$/,@searchby)) {
 9090:                 push(@{$changes{'searchby'}},$by);
 9091:             }
 9092:         }
 9093:         foreach my $by (@searchby) {
 9094:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 9095:                 push(@{$changes{'searchby'}},$by);
 9096:             }
 9097:         }
 9098:     } else {
 9099:         push(@{$changes{'searchby'}},@searchby);
 9100:     }
 9101: 
 9102:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 9103:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 9104:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 9105:                 push(@{$changes{'searchtypes'}},$type);
 9106:             }
 9107:         }
 9108:         foreach my $type (@searchtypes) {
 9109:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 9110:                 push(@{$changes{'searchtypes'}},$type);
 9111:             }
 9112:         }
 9113:     } else {
 9114:         if (exists($currdirsrch{'searchtypes'})) {
 9115:             foreach my $type (@searchtypes) {  
 9116:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 9117:                     push(@{$changes{'searchtypes'}},$type);
 9118:                 }
 9119:             }
 9120:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 9121:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 9122:             }   
 9123:         } else {
 9124:             push(@{$changes{'searchtypes'}},@searchtypes); 
 9125:         }
 9126:     }
 9127: 
 9128:     my %dirsrch_hash =  (
 9129:             directorysrch => { available => $env{'form.dirsrch_available'},
 9130:                                cansearch => \@cansearch,
 9131:                                localonly => $env{'form.dirsrch_instlocalonly'},
 9132:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
 9133:                                lcavailable => $env{'form.dirsrch_domavailable'},
 9134:                                searchby => \@searchby,
 9135:                                searchtypes => \@searchtypes,
 9136:                              }
 9137:             );
 9138:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 9139:                                              $dom);
 9140:     if ($putresult eq 'ok') {
 9141:         if (exists($currdirsrch{'available'})) {
 9142:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 9143:                  $changes{'available'} = 1;
 9144:              }
 9145:         } else {
 9146:             if ($env{'form.dirsrch_available'} eq '1') {
 9147:                 $changes{'available'} = 1;
 9148:             }
 9149:         }
 9150:         if (exists($currdirsrch{'lcavailable'})) {
 9151:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
 9152:                 $changes{'lcavailable'} = 1;
 9153:             }
 9154:         } else {
 9155:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
 9156:                 $changes{'lcavailable'} = 1;
 9157:             }
 9158:         }
 9159:         if (exists($currdirsrch{'localonly'})) {
 9160:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
 9161:                 $changes{'localonly'} = 1;
 9162:             }
 9163:         } else {
 9164:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
 9165:                 $changes{'localonly'} = 1;
 9166:             }
 9167:         }
 9168:         if (exists($currdirsrch{'lclocalonly'})) {
 9169:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
 9170:                 $changes{'lclocalonly'} = 1;
 9171:             }
 9172:         } else {
 9173:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
 9174:                 $changes{'lclocalonly'} = 1;
 9175:             }
 9176:         }
 9177:         if (keys(%changes) > 0) {
 9178:             $resulttext = &mt('Changes made:').'<ul>';
 9179:             if ($changes{'available'}) {
 9180:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 9181:             }
 9182:             if ($changes{'lcavailable'}) {
 9183:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
 9184:             }
 9185:             if ($changes{'localonly'}) {
 9186:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
 9187:             }
 9188:             if ($changes{'lclocalonly'}) {
 9189:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
 9190:             }
 9191:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 9192:                 my $chgtext;
 9193:                 if (ref($usertypes) eq 'HASH') {
 9194:                     if (keys(%{$usertypes}) > 0) {
 9195:                         foreach my $type (@{$types}) {
 9196:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 9197:                                 $chgtext .= $usertypes->{$type}.'; ';
 9198:                             }
 9199:                         }
 9200:                         if (grep(/^default$/,@cansearch)) {
 9201:                             $chgtext .= $othertitle;
 9202:                         } else {
 9203:                             $chgtext =~ s/\; $//;
 9204:                         }
 9205:                         $resulttext .=
 9206:                             '<li>'.
 9207:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
 9208:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
 9209:                             '</li>';
 9210:                     }
 9211:                 }
 9212:             }
 9213:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 9214:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 9215:                 my $chgtext;
 9216:                 foreach my $type (@{$titleorder}) {
 9217:                     if (grep(/^\Q$type\E$/,@searchby)) {
 9218:                         if (defined($searchtitles->{$type})) {
 9219:                             $chgtext .= $searchtitles->{$type}.'; ';
 9220:                         }
 9221:                     }
 9222:                 }
 9223:                 $chgtext =~ s/\; $//;
 9224:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 9225:             }
 9226:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 9227:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 9228:                 my $chgtext;
 9229:                 foreach my $type (@{$srchtypeorder}) {
 9230:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 9231:                         if (defined($srchtypes_desc->{$type})) {
 9232:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 9233:                         }
 9234:                     }
 9235:                 }
 9236:                 $chgtext =~ s/\; $//;
 9237:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
 9238:             }
 9239:             $resulttext .= '</ul>';
 9240:         } else {
 9241:             $resulttext = &mt('No changes made to directory search settings');
 9242:         }
 9243:     } else {
 9244:         $resulttext = '<span class="LC_error">'.
 9245:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 9246:     }
 9247:     return $resulttext;
 9248: }
 9249: 
 9250: sub modify_contacts {
 9251:     my ($dom,$lastactref,%domconfig) = @_;
 9252:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 9253:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 9254:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 9255:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 9256:         }
 9257:     }
 9258:     my (%others,%to,%bcc,%includestr,%includeloc);
 9259:     my @contacts = ('supportemail','adminemail');
 9260:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
 9261:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail');
 9262:     my @toggles = ('reporterrors','reportupdates');
 9263:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 9264:     foreach my $type (@mailings) {
 9265:         @{$newsetting{$type}} = 
 9266:             &Apache::loncommon::get_env_multiple('form.'.$type);
 9267:         foreach my $item (@contacts) {
 9268:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 9269:                 $contacts_hash{contacts}{$type}{$item} = 1;
 9270:             } else {
 9271:                 $contacts_hash{contacts}{$type}{$item} = 0;
 9272:             }
 9273:         }
 9274:         $others{$type} = $env{'form.'.$type.'_others'};
 9275:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 9276:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 9277:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 9278:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 9279:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
 9280:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
 9281:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
 9282:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
 9283:             }
 9284:         }
 9285:     }
 9286:     foreach my $item (@contacts) {
 9287:         $to{$item} = $env{'form.'.$item};
 9288:         $contacts_hash{'contacts'}{$item} = $to{$item};
 9289:     }
 9290:     foreach my $item (@toggles) {
 9291:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
 9292:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
 9293:         }
 9294:     }
 9295:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
 9296:         foreach my $field (@{$fields}) {
 9297:             if (ref($possoptions->{$field}) eq 'ARRAY') {
 9298:                 my $value = $env{'form.helpform_'.$field};
 9299:                 $value =~ s/^\s+|\s+$//g;
 9300:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
 9301:                     $contacts_hash{contacts}{'helpform'}{$field} = $value;
 9302:                     if ($field eq 'screenshot') {
 9303:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
 9304:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
 9305:                             $contacts_hash{contacts}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
 9306:                         }
 9307:                     }
 9308:                 }
 9309:             }
 9310:         }
 9311:     }
 9312:     if (keys(%currsetting) > 0) {
 9313:         foreach my $item (@contacts) {
 9314:             if ($to{$item} ne $currsetting{$item}) {
 9315:                 $changes{$item} = 1;
 9316:             }
 9317:         }
 9318:         foreach my $type (@mailings) {
 9319:             foreach my $item (@contacts) {
 9320:                 if (ref($currsetting{$type}) eq 'HASH') {
 9321:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 9322:                         push(@{$changes{$type}},$item);
 9323:                     }
 9324:                 } else {
 9325:                     push(@{$changes{$type}},@{$newsetting{$type}});
 9326:                 }
 9327:             }
 9328:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 9329:                 push(@{$changes{$type}},'others');
 9330:             }
 9331:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 9332:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 9333:                     push(@{$changes{$type}},'bcc'); 
 9334:                 }
 9335:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
 9336:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
 9337:                     push(@{$changes{$type}},'include');
 9338:                 }
 9339:             }
 9340:         }
 9341:         if (ref($fields) eq 'ARRAY') {
 9342:             if (ref($currsetting{'helpform'}) eq 'HASH') {
 9343:                 foreach my $field (@{$fields}) {
 9344:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
 9345:                         push(@{$changes{'helpform'}},$field);
 9346:                     }
 9347:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
 9348:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
 9349:                             push(@{$changes{'helpform'}},'maxsize');
 9350:                         }
 9351:                     }
 9352:                 }
 9353:             } else {
 9354:                 foreach my $field (@{$fields}) {
 9355:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
 9356:                         push(@{$changes{'helpform'}},$field);
 9357:                     }
 9358:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
 9359:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
 9360:                             push(@{$changes{'helpform'}},'maxsize');
 9361:                         }
 9362:                     }
 9363:                 }
 9364:             }
 9365:         }
 9366:     } else {
 9367:         my %default;
 9368:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 9369:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 9370:         $default{'errormail'} = 'adminemail';
 9371:         $default{'packagesmail'} = 'adminemail';
 9372:         $default{'helpdeskmail'} = 'supportemail';
 9373:         $default{'otherdomsmail'} = 'supportemail';
 9374:         $default{'lonstatusmail'} = 'adminemail';
 9375:         $default{'requestsmail'} = 'adminemail';
 9376:         $default{'updatesmail'} = 'adminemail';
 9377:         foreach my $item (@contacts) {
 9378:            if ($to{$item} ne $default{$item}) {
 9379:                $changes{$item} = 1;
 9380:            }
 9381:         }
 9382:         foreach my $type (@mailings) {
 9383:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 9384:                 push(@{$changes{$type}},@{$newsetting{$type}});
 9385:             }
 9386:             if ($others{$type} ne '') {
 9387:                 push(@{$changes{$type}},'others');
 9388:             }
 9389:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 9390:                 if ($bcc{$type} ne '') {
 9391:                     push(@{$changes{$type}},'bcc');
 9392:                 }
 9393:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
 9394:                     push(@{$changes{$type}},'include');
 9395:                 }
 9396:             }
 9397:         }
 9398:         if (ref($fields) eq 'ARRAY') {
 9399:             foreach my $field (@{$fields}) {
 9400:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
 9401:                     push(@{$changes{'helpform'}},$field);
 9402:                 }
 9403:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
 9404:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
 9405:                         push(@{$changes{'helpform'}},'maxsize');
 9406:                     }
 9407:                 }
 9408:             }
 9409:         }
 9410:     }
 9411:     foreach my $item (@toggles) {
 9412:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
 9413:             $changes{$item} = 1;
 9414:         } elsif ((!$env{'form.'.$item}) &&
 9415:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
 9416:             $changes{$item} = 1;
 9417:         }
 9418:     }
 9419:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 9420:                                              $dom);
 9421:     if ($putresult eq 'ok') {
 9422:         if (keys(%changes) > 0) {
 9423:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 9424:             if (ref($lastactref) eq 'HASH') {
 9425:                 $lastactref->{'domainconfig'} = 1;
 9426:             }
 9427:             my ($titles,$short_titles)  = &contact_titles();
 9428:             $resulttext = &mt('Changes made:').'<ul>';
 9429:             foreach my $item (@contacts) {
 9430:                 if ($changes{$item}) {
 9431:                     $resulttext .= '<li>'.$titles->{$item}.
 9432:                                     &mt(' set to: ').
 9433:                                     '<span class="LC_cusr_emph">'.
 9434:                                     $to{$item}.'</span></li>';
 9435:                 }
 9436:             }
 9437:             foreach my $type (@mailings) {
 9438:                 if (ref($changes{$type}) eq 'ARRAY') {
 9439:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 9440:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
 9441:                     } else {
 9442:                         $resulttext .= '<li>'.$titles->{$type}.': ';
 9443:                     }
 9444:                     my @text;
 9445:                     foreach my $item (@{$newsetting{$type}}) {
 9446:                         push(@text,$short_titles->{$item});
 9447:                     }
 9448:                     if ($others{$type} ne '') {
 9449:                         push(@text,$others{$type});
 9450:                     }
 9451:                     if (@text) {
 9452:                         $resulttext .= '<span class="LC_cusr_emph">'.
 9453:                                        join(', ',@text).'</span>';
 9454:                     }
 9455:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 9456:                         if ($bcc{$type} ne '') {
 9457:                             my $bcctext;
 9458:                             if (@text) {
 9459:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
 9460:                             } else {
 9461:                                 $bcctext = '(Bcc)';
 9462:                             }
 9463:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 9464:                         } elsif (!@text) {
 9465:                             $resulttext .= &mt('No one');
 9466:                         }
 9467:                         if ($includestr{$type} ne '') {
 9468:                             if ($includeloc{$type} eq 'b') {
 9469:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
 9470:                             } elsif ($includeloc{$type} eq 's') {
 9471:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
 9472:                             }
 9473:                         }
 9474:                     } elsif (!@text) {
 9475:                         $resulttext .= &mt('No recipients');
 9476:                     }
 9477:                     $resulttext .= '</li>';
 9478:                 }
 9479:             }
 9480:             my @offon = ('off','on');
 9481:             if ($changes{'reporterrors'}) {
 9482:                 $resulttext .= '<li>'.
 9483:                                &mt('E-mail error reports to [_1] set to "'.
 9484:                                    $offon[$env{'form.reporterrors'}].'".',
 9485:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 9486:                                        &mt('LON-CAPA core group - MSU'),600,500)).
 9487:                                '</li>';
 9488:             }
 9489:             if ($changes{'reportupdates'}) {
 9490:                 $resulttext .= '<li>'.
 9491:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
 9492:                                     $offon[$env{'form.reportupdates'}].'".',
 9493:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 9494:                                         &mt('LON-CAPA core group - MSU'),600,500)).
 9495:                                 '</li>';
 9496:             }
 9497:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
 9498:                 my (@optional,@required,@unused,$maxsizechg);
 9499:                 foreach my $field (@{$changes{'helpform'}}) {
 9500:                     if ($field eq 'maxsize') {
 9501:                         $maxsizechg = 1;
 9502:                         next;
 9503:                     }
 9504:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
 9505:                         push(@optional,$field);
 9506:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
 9507:                         push(@unused,$field);
 9508:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
 9509:                         push(@required,$field);
 9510:                     }
 9511:                 }
 9512:                 if (@optional) {
 9513:                     $resulttext .= '<li>'.
 9514:                                    &mt('Help form fields changed to "Optional": [_1].',
 9515:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
 9516:                                    '</li>';
 9517:                 }
 9518:                 if (@required) {
 9519:                     $resulttext .= '<li>'.
 9520:                                    &mt('Help form fields changed to "Required": [_1].',
 9521:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
 9522:                                    '</li>';
 9523:                 }
 9524:                 if (@unused) {
 9525:                     $resulttext .= '<li>'.
 9526:                                    &mt('Help form fields changed to "Not shown": [_1].',
 9527:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
 9528:                                    '</li>';
 9529:                 }
 9530:                 if ($maxsizechg) {
 9531:                     $resulttext .= '<li>'.
 9532:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
 9533:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
 9534:                                    '</li>';
 9535: 
 9536:                 }
 9537:             }
 9538:             $resulttext .= '</ul>';
 9539:         } else {
 9540:             $resulttext = &mt('No changes made to contacts and form settings');
 9541:         }
 9542:     } else {
 9543:         $resulttext = '<span class="LC_error">'.
 9544:             &mt('An error occurred: [_1].',$putresult).'</span>';
 9545:     }
 9546:     return $resulttext;
 9547: }
 9548: 
 9549: sub modify_usercreation {
 9550:     my ($dom,%domconfig) = @_;
 9551:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
 9552:     my $warningmsg;
 9553:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 9554:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 9555:             if ($key eq 'cancreate') {
 9556:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 9557:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 9558:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 9559:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
 9560:                             ($item eq 'recaptchaversion')) {
 9561:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 9562:                         } else {
 9563:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 9564:                         }
 9565:                     }
 9566:                 }
 9567:             } elsif ($key eq 'email_rule') {
 9568:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 9569:             } else {
 9570:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 9571:             }
 9572:         }
 9573:     }
 9574:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 9575:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 9576:     my @contexts = ('author','course','requestcrs');
 9577:     foreach my $item(@contexts) {
 9578:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
 9579:     }
 9580:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 9581:         foreach my $item (@contexts) {
 9582:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 9583:                 push(@{$changes{'cancreate'}},$item);
 9584:             }
 9585:         }
 9586:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 9587:         foreach my $item (@contexts) {
 9588:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 9589:                 if ($cancreate{$item} ne 'any') {
 9590:                     push(@{$changes{'cancreate'}},$item);
 9591:                 }
 9592:             } else {
 9593:                 if ($cancreate{$item} ne 'none') {
 9594:                     push(@{$changes{'cancreate'}},$item);
 9595:                 }
 9596:             }
 9597:         }
 9598:     } else {
 9599:         foreach my $item (@contexts)  {
 9600:             push(@{$changes{'cancreate'}},$item);
 9601:         }
 9602:     }
 9603: 
 9604:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 9605:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 9606:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 9607:                 push(@{$changes{'username_rule'}},$type);
 9608:             }
 9609:         }
 9610:         foreach my $type (@username_rule) {
 9611:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 9612:                 push(@{$changes{'username_rule'}},$type);
 9613:             }
 9614:         }
 9615:     } else {
 9616:         push(@{$changes{'username_rule'}},@username_rule);
 9617:     }
 9618: 
 9619:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 9620:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 9621:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 9622:                 push(@{$changes{'id_rule'}},$type);
 9623:             }
 9624:         }
 9625:         foreach my $type (@id_rule) {
 9626:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 9627:                 push(@{$changes{'id_rule'}},$type);
 9628:             }
 9629:         }
 9630:     } else {
 9631:         push(@{$changes{'id_rule'}},@id_rule);
 9632:     }
 9633: 
 9634:     my @authen_contexts = ('author','course','domain');
 9635:     my @authtypes = ('int','krb4','krb5','loc');
 9636:     my %authhash;
 9637:     foreach my $item (@authen_contexts) {
 9638:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 9639:         foreach my $auth (@authtypes) {
 9640:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 9641:                 $authhash{$item}{$auth} = 1;
 9642:             } else {
 9643:                 $authhash{$item}{$auth} = 0;
 9644:             }
 9645:         }
 9646:     }
 9647:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 9648:         foreach my $item (@authen_contexts) {
 9649:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 9650:                 foreach my $auth (@authtypes) {
 9651:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 9652:                         push(@{$changes{'authtypes'}},$item);
 9653:                         last;
 9654:                     }
 9655:                 }
 9656:             }
 9657:         }
 9658:     } else {
 9659:         foreach my $item (@authen_contexts) {
 9660:             push(@{$changes{'authtypes'}},$item);
 9661:         }
 9662:     }
 9663: 
 9664:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
 9665:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
 9666:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
 9667:     $save_usercreate{'id_rule'} = \@id_rule;
 9668:     $save_usercreate{'username_rule'} = \@username_rule,
 9669:     $save_usercreate{'authtypes'} = \%authhash;
 9670: 
 9671:     my %usercreation_hash =  (
 9672:         usercreation     => \%save_usercreate,
 9673:     );
 9674: 
 9675:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 9676:                                              $dom);
 9677: 
 9678:     if ($putresult eq 'ok') {
 9679:         if (keys(%changes) > 0) {
 9680:             $resulttext = &mt('Changes made:').'<ul>';
 9681:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 9682:                 my %lt = &usercreation_types();
 9683:                 foreach my $type (@{$changes{'cancreate'}}) {
 9684:                     my $chgtext = $lt{$type}.', ';
 9685:                     if ($cancreate{$type} eq 'none') {
 9686:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 9687:                     } elsif ($cancreate{$type} eq 'any') {
 9688:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 9689:                     } elsif ($cancreate{$type} eq 'official') {
 9690:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 9691:                     } elsif ($cancreate{$type} eq 'unofficial') {
 9692:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 9693:                     }
 9694:                     $resulttext .= '<li>'.$chgtext.'</li>';
 9695:                 }
 9696:             }
 9697:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 9698:                 my ($rules,$ruleorder) = 
 9699:                     &Apache::lonnet::inst_userrules($dom,'username');
 9700:                 my $chgtext = '<ul>';
 9701:                 foreach my $type (@username_rule) {
 9702:                     if (ref($rules->{$type}) eq 'HASH') {
 9703:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 9704:                     }
 9705:                 }
 9706:                 $chgtext .= '</ul>';
 9707:                 if (@username_rule > 0) {
 9708:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 9709:                 } else {
 9710:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 9711:                 }
 9712:             }
 9713:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 9714:                 my ($idrules,$idruleorder) = 
 9715:                     &Apache::lonnet::inst_userrules($dom,'id');
 9716:                 my $chgtext = '<ul>';
 9717:                 foreach my $type (@id_rule) {
 9718:                     if (ref($idrules->{$type}) eq 'HASH') {
 9719:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 9720:                     }
 9721:                 }
 9722:                 $chgtext .= '</ul>';
 9723:                 if (@id_rule > 0) {
 9724:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 9725:                 } else {
 9726:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 9727:                 }
 9728:             }
 9729:             my %authname = &authtype_names();
 9730:             my %context_title = &context_names();
 9731:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 9732:                 my $chgtext = '<ul>';
 9733:                 foreach my $type (@{$changes{'authtypes'}}) {
 9734:                     my @allowed;
 9735:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 9736:                     foreach my $auth (@authtypes) {
 9737:                         if ($authhash{$type}{$auth}) {
 9738:                             push(@allowed,$authname{$auth});
 9739:                         }
 9740:                     }
 9741:                     if (@allowed > 0) {
 9742:                         $chgtext .= join(', ',@allowed).'</li>';
 9743:                     } else {
 9744:                         $chgtext .= &mt('none').'</li>';
 9745:                     }
 9746:                 }
 9747:                 $chgtext .= '</ul>';
 9748:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 9749:                 $resulttext .= '</li>';
 9750:             }
 9751:             $resulttext .= '</ul>';
 9752:         } else {
 9753:             $resulttext = &mt('No changes made to user creation settings');
 9754:         }
 9755:     } else {
 9756:         $resulttext = '<span class="LC_error">'.
 9757:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9758:     }
 9759:     if ($warningmsg ne '') {
 9760:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 9761:     }
 9762:     return $resulttext;
 9763: }
 9764: 
 9765: sub modify_selfcreation {
 9766:     my ($dom,%domconfig) = @_;
 9767:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%changes,%cancreate);
 9768:     my (%save_usercreate,%save_usermodify);
 9769:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9770:     if (ref($types) eq 'ARRAY') {
 9771:         $usertypes->{'default'} = $othertitle;
 9772:         push(@{$types},'default');
 9773:     }
 9774: #
 9775: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
 9776: #
 9777:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 9778:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 9779:             if ($key eq 'cancreate') {
 9780:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 9781:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 9782:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 9783:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') || 
 9784:                             ($item eq 'recaptchaversion') ||
 9785:                             ($item eq 'emailusername') || ($item eq 'notify') ||
 9786:                             ($item eq 'selfcreateprocessing') || ($item eq 'shibenv')) {
 9787:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 9788:                         } else {
 9789:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 9790:                         }
 9791:                     }
 9792:                 }
 9793:             } elsif ($key eq 'email_rule') {
 9794:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 9795:             } else {
 9796:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 9797:             }
 9798:         }
 9799:     }
 9800: #
 9801: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
 9802: #
 9803:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 9804:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 9805:             if ($key eq 'selfcreate') {
 9806:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
 9807:             } else {
 9808:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
 9809:             }
 9810:         }
 9811:     }
 9812: 
 9813:     my @contexts = ('selfcreate');
 9814:     @{$cancreate{'selfcreate'}} = ();
 9815:     %{$cancreate{'emailusername'}} = ();
 9816:     @{$cancreate{'statustocreate'}} = ();
 9817:     %{$cancreate{'selfcreateprocessing'}} = ();
 9818:     %{$cancreate{'shibenv'}} = ();
 9819:     my %selfcreatetypes = (
 9820:                              sso   => 'users authenticated by institutional single sign on',
 9821:                              login => 'users authenticated by institutional log-in',
 9822:                              email => 'users who provide a valid e-mail address for use as username',
 9823:                           );
 9824: #
 9825: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
 9826: # is permitted.
 9827: #
 9828: 
 9829:     my @statuses;
 9830:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 9831:         if (ref($domconfig{'inststatus'}{'inststatusguest'}) eq 'ARRAY') {
 9832:             @statuses = @{$domconfig{'inststatus'}{'inststatusguest'}};
 9833:         }
 9834:     }
 9835:     push(@statuses,'default');
 9836: 
 9837:     foreach my $item ('login','sso','email') {
 9838:         if ($item eq 'email') {
 9839:             if ($env{'form.cancreate_email'}) {
 9840:                 push(@{$cancreate{'selfcreate'}},'email');
 9841:                 push(@contexts,'selfcreateprocessing');
 9842:                 foreach my $type (@statuses) {
 9843:                     if ($type eq 'default') {
 9844:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess'};
 9845:                     } else { 
 9846:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
 9847:                     }
 9848:                 }
 9849:             }
 9850:         } else {
 9851:             if ($env{'form.cancreate_'.$item}) {
 9852:                 push(@{$cancreate{'selfcreate'}},$item);
 9853:             }
 9854:         }
 9855:     }
 9856:     my (@email_rule,%userinfo,%savecaptcha);
 9857:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 9858: #
 9859: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
 9860: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
 9861: #
 9862: 
 9863:     if ($env{'form.cancreate_email'}) {
 9864:         push(@contexts,'emailusername');
 9865:         if (ref($types) eq 'ARRAY') {
 9866:             foreach my $type (@{$types}) {
 9867:                 if (ref($infofields) eq 'ARRAY') {
 9868:                     foreach my $field (@{$infofields}) {
 9869:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
 9870:                             $cancreate{'emailusername'}{$type}{$field} = $1;
 9871:                         }
 9872:                     }
 9873:                 }
 9874:             }
 9875:         }
 9876: #
 9877: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
 9878: # queued requests for self-creation of account using e-mail address as username
 9879: #
 9880: 
 9881:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
 9882:         @approvalnotify = sort(@approvalnotify);
 9883:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
 9884:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 9885:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
 9886:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
 9887:                     push(@{$changes{'cancreate'}},'notify');
 9888:                 }
 9889:             } else {
 9890:                 if ($cancreate{'notify'}{'approval'}) {
 9891:                     push(@{$changes{'cancreate'}},'notify');
 9892:                 }
 9893:             }
 9894:         } elsif ($cancreate{'notify'}{'approval'}) {
 9895:             push(@{$changes{'cancreate'}},'notify');
 9896:         }
 9897: 
 9898: #
 9899: # Retrieve rules (if any) governing types of e-mail address which may be used as a username
 9900: #
 9901:         @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 9902:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
 9903:         if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 9904:             if (@{$curr_usercreation{'email_rule'}} > 0) {
 9905:                 foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 9906:                     if (!grep(/^\Q$type\E$/,@email_rule)) {
 9907:                         push(@{$changes{'email_rule'}},$type);
 9908:                     }
 9909:                 }
 9910:             }
 9911:             if (@email_rule > 0) {
 9912:                 foreach my $type (@email_rule) {
 9913:                     if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 9914:                         push(@{$changes{'email_rule'}},$type);
 9915:                     }
 9916:                 }
 9917:             }
 9918:         } elsif (@email_rule > 0) {
 9919:             push(@{$changes{'email_rule'}},@email_rule);
 9920:         }
 9921:     }
 9922: #  
 9923: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
 9924: # institutional log-in.
 9925: #
 9926:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
 9927:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9928:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
 9929:                ($domdefaults{'auth_def'} eq 'localauth'))) {
 9930:             $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.').' '.
 9931:                           &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.');
 9932:         }
 9933:     }
 9934:     my @fields = ('lastname','firstname','middlename','generation',
 9935:                   'permanentemail','id');
 9936:     my @shibfields = (@fields,'inststatus');
 9937:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9938: #
 9939: # Where usernames may created for institutional log-in and/or institutional single sign on:
 9940: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
 9941: # may self-create accounts 
 9942: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
 9943: # which the user may supply, if institutional data is unavailable.
 9944: #
 9945:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
 9946:         if (ref($types) eq 'ARRAY') {
 9947:             if (@{$types} > 1) {
 9948:                 @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
 9949:                 push(@contexts,'statustocreate');
 9950:             } else {
 9951:                 undef($cancreate{'statustocreate'});
 9952:             } 
 9953:             foreach my $type (@{$types}) {
 9954:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
 9955:                 foreach my $field (@fields) {
 9956:                     if (grep(/^\Q$field\E$/,@modifiable)) {
 9957:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
 9958:                     } else {
 9959:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
 9960:                     }
 9961:                 }
 9962:             }
 9963:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
 9964:                 foreach my $type (@{$types}) {
 9965:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
 9966:                         foreach my $field (@fields) {
 9967:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
 9968:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
 9969:                                 push(@{$changes{'selfcreate'}},$type);
 9970:                                 last;
 9971:                             }
 9972:                         }
 9973:                     }
 9974:                 }
 9975:             } else {
 9976:                 foreach my $type (@{$types}) {
 9977:                     push(@{$changes{'selfcreate'}},$type);
 9978:                 }
 9979:             }
 9980:         }
 9981:         foreach my $field (@shibfields) {
 9982:             if ($env{'form.shibenv_'.$field} ne '') {
 9983:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
 9984:             }
 9985:         }
 9986:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 9987:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
 9988:                 foreach my $field (@shibfields) {
 9989:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
 9990:                         push(@{$changes{'cancreate'}},'shibenv');
 9991:                     }
 9992:                 }
 9993:             } else {
 9994:                 foreach my $field (@shibfields) {
 9995:                     if ($env{'form.shibenv_'.$field}) {
 9996:                         push(@{$changes{'cancreate'}},'shibenv');
 9997:                         last;
 9998:                     }
 9999:                 }
10000:             }
10001:         }
10002:     }
10003:     foreach my $item (@contexts) {
10004:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
10005:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
10006:                 if (ref($cancreate{$item}) eq 'ARRAY') {
10007:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
10008:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10009:                             push(@{$changes{'cancreate'}},$item);
10010:                         }
10011:                     }
10012:                 }
10013:             }
10014:             if (ref($cancreate{$item}) eq 'ARRAY') {
10015:                 foreach my $type (@{$cancreate{$item}}) {
10016:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
10017:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10018:                             push(@{$changes{'cancreate'}},$item);
10019:                         }
10020:                     }
10021:                 }
10022:             }
10023:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
10024:             if (ref($cancreate{$item}) eq 'HASH') {
10025:                 foreach my $curr (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
10026:                     if (ref($curr_usercreation{'cancreate'}{$item}{$curr}) eq 'HASH') {
10027:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$curr}})) {
10028:                             unless ($curr_usercreation{'cancreate'}{$item}{$curr}{$field} eq $cancreate{$item}{$curr}{$field}) {
10029:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10030:                                     push(@{$changes{'cancreate'}},$item);
10031:                                 }
10032:                             }
10033:                         }
10034:                     } elsif ($item eq 'selfcreateprocessing') {
10035:                         if ($cancreate{$item}{$curr} ne $curr_usercreation{'cancreate'}{$item}{$curr}) {
10036:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10037:                                 push(@{$changes{'cancreate'}},$item);
10038:                             }
10039:                         }
10040:                     } else {
10041:                         if (!$cancreate{$item}{$curr}) {
10042:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10043:                                 push(@{$changes{'cancreate'}},$item);
10044:                             }
10045:                         }
10046:                     }
10047:                 }
10048:                 foreach my $field (keys(%{$cancreate{$item}})) {
10049:                     if (ref($cancreate{$item}{$field}) eq 'HASH') {
10050:                         foreach my $inner (keys(%{$cancreate{$item}{$field}})) {
10051:                             if (ref($curr_usercreation{'cancreate'}{$item}{$field}) eq 'HASH') {
10052:                                 unless ($curr_usercreation{'cancreate'}{$item}{$field}{$inner} eq $cancreate{$item}{$field}{$inner}) {
10053:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10054:                                         push(@{$changes{'cancreate'}},$item);
10055:                                     }
10056:                                 }
10057:                             } else {
10058:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10059:                                     push(@{$changes{'cancreate'}},$item);
10060:                                 }
10061:                             }
10062:                         }
10063:                     } elsif ($item eq 'selfcreateprocessing') {
10064:                         if ($cancreate{$item}{$field} ne $curr_usercreation{'cancreate'}{$item}{$field}) {
10065:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10066:                                 push(@{$changes{'cancreate'}},$item);
10067:                             }
10068:                         }
10069:                     } else {
10070:                         if (!$curr_usercreation{'cancreate'}{$item}{$field}) {
10071:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10072:                                 push(@{$changes{'cancreate'}},$item);
10073:                             }
10074:                         }
10075:                     }
10076:                 }
10077:             }
10078:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
10079:             if (ref($cancreate{$item}) eq 'ARRAY') {
10080:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
10081:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10082:                         push(@{$changes{'cancreate'}},$item);
10083:                     }
10084:                 }
10085:             } elsif (ref($cancreate{$item}) eq 'HASH') {
10086:                 if (!$cancreate{$item}{$curr_usercreation{'cancreate'}{$item}}) {
10087:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10088:                         push(@{$changes{'cancreate'}},$item);
10089:                     }
10090:                 }
10091:             }
10092:         } elsif ($item eq 'emailusername') {
10093:             if (ref($cancreate{$item}) eq 'HASH') {
10094:                 foreach my $type (keys(%{$cancreate{$item}})) {
10095:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
10096:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
10097:                             if ($cancreate{$item}{$type}{$field}) {
10098:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10099:                                     push(@{$changes{'cancreate'}},$item);
10100:                                 }
10101:                                 last;
10102:                             }
10103:                         }
10104:                     }
10105:                 }
10106:             }
10107:         }
10108:     }
10109: #
10110: # Populate %save_usercreate hash with updates to self-creation configuration.
10111: #
10112:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
10113:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
10114:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
10115:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
10116:     if (ref($cancreate{'notify'}) eq 'HASH') {
10117:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
10118:     }
10119:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
10120:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
10121:     }
10122:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
10123:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
10124:     }
10125:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
10126:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
10127:     }
10128:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
10129:     $save_usercreate{'emailrule'} = \@email_rule;
10130: 
10131:     my %userconfig_hash = (
10132:             usercreation     => \%save_usercreate,
10133:             usermodification => \%save_usermodify,
10134:     );
10135:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
10136:                                              $dom);
10137: #
10138: # Accumulate details of changes to domain cofiguration for self-creation of usernames in $resulttext
10139: #
10140:     if ($putresult eq 'ok') {
10141:         if (keys(%changes) > 0) {
10142:             $resulttext = &mt('Changes made:').'<ul>';
10143:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
10144:                 my %lt = &selfcreation_types();
10145:                 foreach my $type (@{$changes{'cancreate'}}) {
10146:                     my $chgtext;
10147:                     if ($type eq 'selfcreate') {
10148:                         if (@{$cancreate{$type}} == 0) {
10149:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
10150:                         } else {
10151:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
10152:                                         '<ul>';
10153:                             foreach my $case (@{$cancreate{$type}}) {
10154:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
10155:                             }
10156:                             $chgtext .= '</ul>';
10157:                             if (ref($cancreate{$type}) eq 'ARRAY') {
10158:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
10159:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
10160:                                         if (@{$cancreate{'statustocreate'}} == 0) {
10161:                                             $chgtext .= '<br />'.
10162:                                                         '<span class="LC_warning">'.
10163:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
10164:                                                         '</span>';
10165:                                         }
10166:                                     }
10167:                                 }
10168:                             }
10169:                         }
10170:                     } elsif ($type eq 'shibenv') {
10171:                         if (keys(%{$cancreate{$type}}) == 0) {
10172:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information'); 
10173:                         } else {
10174:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
10175:                                         '<ul>';
10176:                             foreach my $field (@shibfields) {
10177:                                 next if ($cancreate{$type}{$field} eq '');
10178:                                 if ($field eq 'inststatus') {
10179:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
10180:                                 } else {
10181:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
10182:                                 }
10183:                             }
10184:                             $chgtext .= '</ul>';
10185:                         }  
10186:                     } elsif ($type eq 'statustocreate') {
10187:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
10188:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
10189:                             if (@{$cancreate{'selfcreate'}} > 0) {
10190:                                 if (@{$cancreate{'statustocreate'}} == 0) {
10191:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
10192:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
10193:                                         $chgtext .= '<br />'.
10194:                                                     '<span class="LC_warning">'.
10195:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
10196:                                                     '</span>';
10197:                                     }
10198:                                 } elsif (ref($usertypes) eq 'HASH') {
10199:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
10200:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
10201:                                     } else {
10202:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
10203:                                     }
10204:                                     $chgtext .= '<ul>';
10205:                                     foreach my $case (@{$cancreate{$type}}) {
10206:                                         if ($case eq 'default') {
10207:                                             $chgtext .= '<li>'.$othertitle.'</li>';
10208:                                         } else {
10209:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
10210:                                         }
10211:                                     }
10212:                                     $chgtext .= '</ul>';
10213:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
10214:                                         $chgtext .= '<br /><span class="LC_warning">'.
10215:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
10216:                                                     '</span>';
10217:                                     }
10218:                                 }
10219:                             } else {
10220:                                 if (@{$cancreate{$type}} == 0) {
10221:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
10222:                                 } else {
10223:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
10224:                                 }
10225:                             }
10226:                         }
10227:                     } elsif ($type eq 'selfcreateprocessing') {
10228:                         my %choices = &Apache::lonlocal::texthash (
10229:                                                                     automatic => 'Automatic approval',
10230:                                                                     approval  => 'Queued for approval',
10231:                                                                   );
10232:                         if (@statuses > 1) {
10233:                             $chgtext .= &mt('Processing of requests to create account with e-mail address as username set as follows:'). 
10234:                                         '<ul>';
10235:                            foreach my $type (@statuses) {
10236:                                if ($type eq 'default') {
10237:                                    $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
10238:                                } else {
10239:                                    $chgtext .= '<li>'.$usertypes->{$type}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
10240:                                }
10241:                            }
10242:                            $chgtext .= '</ul>';
10243:                         } else {
10244:                            $chgtext .= &mt('Processing of requests to create account with e-mail address as username set to: "[_1]"',
10245:                                          $choices{$cancreate{'selfcreateprocessing'}{'default'}});
10246:                         }
10247:                     } elsif ($type eq 'captcha') {
10248:                         if ($savecaptcha{$type} eq 'notused') {
10249:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
10250:                         } else {
10251:                             my %captchas = &captcha_phrases();
10252:                             if ($captchas{$savecaptcha{$type}}) {
10253:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
10254:                             } else {
10255:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
10256:                             }
10257:                         }
10258:                     } elsif ($type eq 'recaptchakeys') {
10259:                         my ($privkey,$pubkey);
10260:                         if (ref($savecaptcha{$type}) eq 'HASH') {
10261:                             $pubkey = $savecaptcha{$type}{'public'};
10262:                             $privkey = $savecaptcha{$type}{'private'};
10263:                         }
10264:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
10265:                         if (!$pubkey) {
10266:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
10267:                         } else {
10268:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
10269:                         }
10270:                         if (!$privkey) {
10271:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
10272:                         } else {
10273:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
10274:                         }
10275:                         $chgtext .= '</ul>';
10276:                     } elsif ($type eq 'recaptchaversion') {
10277:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
10278:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
10279:                         }
10280:                     } elsif ($type eq 'emailusername') {
10281:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
10282:                             if (ref($types) eq 'ARRAY') {
10283:                                 foreach my $type (@{$types}) {
10284:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
10285:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
10286:                                             $chgtext .= &mt('When self-creating account with e-mail as username, the following information will be provided by [_1]:',"'$usertypes->{$type}'").
10287:                                                     '<ul>';
10288:                                             foreach my $field (@{$infofields}) {
10289:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
10290:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
10291:                                                 }
10292:                                             }
10293:                                             $chgtext .= '</ul>';
10294:                                         } else {
10295:                                             $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 />';
10296:                                         }
10297:                                     } else {
10298:                                         $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 />';
10299:                                     }
10300:                                 }
10301:                             }
10302:                         }
10303:                     } elsif ($type eq 'notify') {
10304:                         $chgtext = &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
10305:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
10306:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
10307:                                 if ($cancreate{'notify'}{'approval'}) {
10308:                                     $chgtext = &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
10309:                                 }
10310:                             }
10311:                         }
10312:                     }
10313:                     if ($chgtext) {
10314:                         $resulttext .= '<li>'.$chgtext.'</li>';
10315:                     }
10316:                 }
10317:             }
10318:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
10319:                 my ($emailrules,$emailruleorder) =
10320:                     &Apache::lonnet::inst_userrules($dom,'email');
10321:                 my $chgtext = '<ul>';
10322:                 foreach my $type (@email_rule) {
10323:                     if (ref($emailrules->{$type}) eq 'HASH') {
10324:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
10325:                     }
10326:                 }
10327:                 $chgtext .= '</ul>';
10328:                 if (@email_rule > 0) {
10329:                     $resulttext .= '<li>'.
10330:                                    &mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').
10331:                                        $chgtext.
10332:                                    '</li>';
10333:                 } else {
10334:                     $resulttext .= '<li>'.
10335:                                    &mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').
10336:                                    '</li>';
10337:                 }
10338:             }
10339:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
10340:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
10341:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
10342:                 foreach my $type (@{$changes{'selfcreate'}}) {
10343:                     my $typename = $type;
10344:                     if (ref($usertypes) eq 'HASH') {
10345:                         if ($usertypes->{$type} ne '') {
10346:                             $typename = $usertypes->{$type};
10347:                         }
10348:                     }
10349:                     my @modifiable;
10350:                     $resulttext .= '<li>'.
10351:                                     &mt('Self-creation of account by users with status: [_1]',
10352:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
10353:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
10354:                     foreach my $field (@fields) {
10355:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
10356:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
10357:                         }
10358:                     }
10359:                     if (@modifiable > 0) {
10360:                         $resulttext .= join(', ',@modifiable);
10361:                     } else {
10362:                         $resulttext .= &mt('none');
10363:                     }
10364:                     $resulttext .= '</li>';
10365:                 }
10366:                 $resulttext .= '</ul></li>';
10367:             }
10368:             $resulttext .= '</ul>';
10369:         } else {
10370:             $resulttext = &mt('No changes made to self-creation settings');
10371:         }
10372:     } else {
10373:         $resulttext = '<span class="LC_error">'.
10374:             &mt('An error occurred: [_1]',$putresult).'</span>';
10375:     }
10376:     if ($warningmsg ne '') {
10377:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
10378:     }
10379:     return $resulttext;
10380: }
10381: 
10382: sub process_captcha {
10383:     my ($container,$changes,$newsettings,$current) = @_;
10384:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
10385:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
10386:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
10387:         $newsettings->{'captcha'} = 'original';
10388:     }
10389:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
10390:         if ($container eq 'cancreate') {
10391:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
10392:                 push(@{$changes->{'cancreate'}},'captcha');
10393:             } elsif (!defined($changes->{'cancreate'})) {
10394:                 $changes->{'cancreate'} = ['captcha'];
10395:             }
10396:         } else {
10397:             $changes->{'captcha'} = 1;
10398:         }
10399:     }
10400:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
10401:     if ($newsettings->{'captcha'} eq 'recaptcha') {
10402:         $newpub = $env{'form.'.$container.'_recaptchapub'};
10403:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
10404:         $newpub =~ s/[^\w\-]//g;
10405:         $newpriv =~ s/[^\w\-]//g;
10406:         $newsettings->{'recaptchakeys'} = {
10407:                                              public  => $newpub,
10408:                                              private => $newpriv,
10409:                                           };
10410:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
10411:         $newversion =~ s/\D//g;
10412:         if ($newversion ne '2') {
10413:             $newversion = 1;
10414:         }
10415:         $newsettings->{'recaptchaversion'} = $newversion;
10416:     }
10417:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
10418:         $currpub = $current->{'recaptchakeys'}{'public'};
10419:         $currpriv = $current->{'recaptchakeys'}{'private'};
10420:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
10421:             $newsettings->{'recaptchakeys'} = {
10422:                                                  public  => '',
10423:                                                  private => '',
10424:                                               }
10425:         }
10426:     }
10427:     if ($current->{'captcha'} eq 'recaptcha') {
10428:         $currversion = $current->{'recaptchaversion'};
10429:         if ($currversion ne '2') {
10430:             $currversion = 1;
10431:         }
10432:     }
10433:     if ($currversion ne $newversion) {
10434:         if ($container eq 'cancreate') {
10435:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
10436:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
10437:             } elsif (!defined($changes->{'cancreate'})) {
10438:                 $changes->{'cancreate'} = ['recaptchaversion'];
10439:             }
10440:         } else {
10441:             $changes->{'recaptchaversion'} = 1;
10442:         }
10443:     }
10444:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
10445:         if ($container eq 'cancreate') {
10446:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
10447:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
10448:             } elsif (!defined($changes->{'cancreate'})) {
10449:                 $changes->{'cancreate'} = ['recaptchakeys'];
10450:             }
10451:         } else {
10452:             $changes->{'recaptchakeys'} = 1;
10453:         }
10454:     }
10455:     return;
10456: }
10457: 
10458: sub modify_usermodification {
10459:     my ($dom,%domconfig) = @_;
10460:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
10461:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
10462:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
10463:             if ($key eq 'selfcreate') {
10464:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
10465:             } else {  
10466:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
10467:             }
10468:         }
10469:     }
10470:     my @contexts = ('author','course');
10471:     my %context_title = (
10472:                            author => 'In author context',
10473:                            course => 'In course context',
10474:                         );
10475:     my @fields = ('lastname','firstname','middlename','generation',
10476:                   'permanentemail','id');
10477:     my %roles = (
10478:                   author => ['ca','aa'],
10479:                   course => ['st','ep','ta','in','cr'],
10480:                 );
10481:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
10482:     foreach my $context (@contexts) {
10483:         foreach my $role (@{$roles{$context}}) {
10484:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
10485:             foreach my $item (@fields) {
10486:                 if (grep(/^\Q$item\E$/,@modifiable)) {
10487:                     $modifyhash{$context}{$role}{$item} = 1;
10488:                 } else {
10489:                     $modifyhash{$context}{$role}{$item} = 0;
10490:                 }
10491:             }
10492:         }
10493:         if (ref($curr_usermodification{$context}) eq 'HASH') {
10494:             foreach my $role (@{$roles{$context}}) {
10495:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
10496:                     foreach my $field (@fields) {
10497:                         if ($modifyhash{$context}{$role}{$field} ne 
10498:                                 $curr_usermodification{$context}{$role}{$field}) {
10499:                             push(@{$changes{$context}},$role);
10500:                             last;
10501:                         }
10502:                     }
10503:                 }
10504:             }
10505:         } else {
10506:             foreach my $context (@contexts) {
10507:                 foreach my $role (@{$roles{$context}}) {
10508:                     push(@{$changes{$context}},$role);
10509:                 }
10510:             }
10511:         }
10512:     }
10513:     my %usermodification_hash =  (
10514:                                    usermodification => \%modifyhash,
10515:                                  );
10516:     my $putresult = &Apache::lonnet::put_dom('configuration',
10517:                                              \%usermodification_hash,$dom);
10518:     if ($putresult eq 'ok') {
10519:         if (keys(%changes) > 0) {
10520:             $resulttext = &mt('Changes made: ').'<ul>';
10521:             foreach my $context (@contexts) {
10522:                 if (ref($changes{$context}) eq 'ARRAY') {
10523:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
10524:                     if (ref($changes{$context}) eq 'ARRAY') {
10525:                         foreach my $role (@{$changes{$context}}) {
10526:                             my $rolename;
10527:                             if ($role eq 'cr') {
10528:                                 $rolename = &mt('Custom');
10529:                             } else {
10530:                                 $rolename = &Apache::lonnet::plaintext($role);
10531:                             }
10532:                             my @modifiable;
10533:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
10534:                             foreach my $field (@fields) {
10535:                                 if ($modifyhash{$context}{$role}{$field}) {
10536:                                     push(@modifiable,$fieldtitles{$field});
10537:                                 }
10538:                             }
10539:                             if (@modifiable > 0) {
10540:                                 $resulttext .= join(', ',@modifiable);
10541:                             } else {
10542:                                 $resulttext .= &mt('none'); 
10543:                             }
10544:                             $resulttext .= '</li>';
10545:                         }
10546:                         $resulttext .= '</ul></li>';
10547:                     }
10548:                 }
10549:             }
10550:             $resulttext .= '</ul>';
10551:         } else {
10552:             $resulttext = &mt('No changes made to user modification settings');
10553:         }
10554:     } else {
10555:         $resulttext = '<span class="LC_error">'.
10556:             &mt('An error occurred: [_1]',$putresult).'</span>';
10557:     }
10558:     return $resulttext;
10559: }
10560: 
10561: sub modify_defaults {
10562:     my ($dom,$lastactref,%domconfig) = @_;
10563:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
10564:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10565:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
10566:                  'portal_def','intauth_cost','intauth_check','intauth_switch');
10567:     my @authtypes = ('internal','krb4','krb5','localauth');
10568:     foreach my $item (@items) {
10569:         $newvalues{$item} = $env{'form.'.$item};
10570:         if ($item eq 'auth_def') {
10571:             if ($newvalues{$item} ne '') {
10572:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
10573:                     push(@errors,$item);
10574:                 }
10575:             }
10576:         } elsif ($item eq 'lang_def') {
10577:             if ($newvalues{$item} ne '') {
10578:                 if ($newvalues{$item} =~ /^(\w+)/) {
10579:                     my $langcode = $1;
10580:                     if ($langcode ne 'x_chef') {
10581:                         if (code2language($langcode) eq '') {
10582:                             push(@errors,$item);
10583:                         }
10584:                     }
10585:                 } else {
10586:                     push(@errors,$item);
10587:                 }
10588:             }
10589:         } elsif ($item eq 'timezone_def') {
10590:             if ($newvalues{$item} ne '') {
10591:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
10592:                     push(@errors,$item);   
10593:                 }
10594:             }
10595:         } elsif ($item eq 'datelocale_def') {
10596:             if ($newvalues{$item} ne '') {
10597:                 my @datelocale_ids = DateTime::Locale->ids();
10598:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
10599:                     push(@errors,$item);
10600:                 }
10601:             }
10602:         } elsif ($item eq 'portal_def') {
10603:             if ($newvalues{$item} ne '') {
10604:                 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])\/?$/) {
10605:                     push(@errors,$item);
10606:                 }
10607:             }
10608:         } elsif ($item eq 'intauth_cost') {
10609:             if ($newvalues{$item} ne '') {
10610:                 if ($newvalues{$item} =~ /\D/) {
10611:                     push(@errors,$item);
10612:                 }
10613:             }
10614:         } elsif ($item eq 'intauth_check') {
10615:             if ($newvalues{$item} ne '') {
10616:                 unless ($newvalues{$item} =~ /^(0|1|2)$/) {
10617:                     push(@errors,$item);
10618:                 }
10619:             }
10620:         } elsif ($item eq 'intauth_switch') {
10621:             if ($newvalues{$item} ne '') {
10622:                 unless ($newvalues{$item} =~ /^(0|1|2)$/) {
10623:                     push(@errors,$item);
10624:                 }
10625:             }
10626:         }
10627:         if (grep(/^\Q$item\E$/,@errors)) {
10628:             $newvalues{$item} = $domdefaults{$item};
10629:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
10630:             $changes{$item} = 1;
10631:         }
10632:         $domdefaults{$item} = $newvalues{$item};
10633:     }
10634:     my %defaults_hash = (
10635:                          defaults => \%newvalues,
10636:                         );
10637:     my $title = &defaults_titles();
10638: 
10639:     my $currinststatus;
10640:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
10641:         $currinststatus = $domconfig{'inststatus'};
10642:     } else {
10643:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10644:         $currinststatus = {
10645:                              inststatustypes => $usertypes,
10646:                              inststatusorder => $types,
10647:                              inststatusguest => [],
10648:                           };
10649:     }
10650:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
10651:     my @allpos;
10652:     my %guests;
10653:     my %alltypes;
10654:     my ($currtitles,$currguests,$currorder);
10655:     if (ref($currinststatus) eq 'HASH') {
10656:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
10657:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
10658:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
10659:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
10660:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
10661:                     }
10662:                 }
10663:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
10664:                     my $position = $env{'form.inststatus_pos_'.$type};
10665:                     $position =~ s/\D+//g;
10666:                     $allpos[$position] = $type;
10667:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
10668:                     $alltypes{$type} =~ s/`//g;
10669:                     if ($env{'form.inststatus_guest_'.$type}) {
10670:                         $guests{$type} = 1;
10671:                     }
10672:                 }
10673:             }
10674:             if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
10675:                 $currguests = join(',',@{$currinststatus->{'inststatusguest'}});
10676:             }
10677:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
10678:             $currtitles =~ s/,$//;
10679:         }
10680:     }
10681:     if ($env{'form.addinststatus'}) {
10682:         my $newtype = $env{'form.addinststatus'};
10683:         $newtype =~ s/\W//g;
10684:         unless (exists($alltypes{$newtype})) {
10685:             if ($env{'form.addinststatus_guest'}) {
10686:                 $guests{$newtype} = 1;
10687:             }
10688:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
10689:             $alltypes{$newtype} =~ s/`//g; 
10690:             my $position = $env{'form.addinststatus_pos'};
10691:             $position =~ s/\D+//g;
10692:             if ($position ne '') {
10693:                 $allpos[$position] = $newtype;
10694:             }
10695:         }
10696:     }
10697:     my (@orderedstatus,@orderedguests);
10698:     foreach my $type (@allpos) {
10699:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
10700:             push(@orderedstatus,$type);
10701:             if ($guests{$type}) {
10702:                 push(@orderedguests,$type);
10703:             }
10704:         }
10705:     }
10706:     foreach my $type (keys(%alltypes)) {
10707:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
10708:             delete($alltypes{$type});
10709:         }
10710:     }
10711:     $defaults_hash{'inststatus'} = {
10712:                                      inststatustypes => \%alltypes,
10713:                                      inststatusorder => \@orderedstatus,
10714:                                      inststatusguest => \@orderedguests,
10715:                                    };
10716:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
10717:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
10718:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
10719:         }
10720:     }
10721:     if ($currorder ne join(',',@orderedstatus)) {
10722:         $changes{'inststatus'}{'inststatusorder'} = 1;
10723:     }
10724:     if ($currguests ne join(',',@orderedguests)) {
10725:         $changes{'inststatus'}{'inststatusguest'} = 1;
10726:     }
10727:     my $newtitles;
10728:     foreach my $item (@orderedstatus) {
10729:         $newtitles .= $alltypes{$item}.',';
10730:     }
10731:     $newtitles =~ s/,$//;
10732:     if ($currtitles ne $newtitles) {
10733:         $changes{'inststatus'}{'inststatustypes'} = 1;
10734:     }
10735:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
10736:                                              $dom);
10737:     if ($putresult eq 'ok') {
10738:         if (keys(%changes) > 0) {
10739:             $resulttext = &mt('Changes made:').'<ul>';
10740:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
10741:             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";
10742:             foreach my $item (sort(keys(%changes))) {
10743:                 if ($item eq 'inststatus') {
10744:                     if (ref($changes{'inststatus'}) eq 'HASH') {
10745:                         if (($changes{'inststatus'}{'inststatustypes'}) || $changes{'inststatus'}{'inststatusorder'}) {
10746:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
10747:                             foreach my $type (@orderedstatus) { 
10748:                                 $resulttext .= $alltypes{$type}.', ';
10749:                             }
10750:                             $resulttext =~ s/, $//;
10751:                             $resulttext .= '</li>';
10752:                         }
10753:                         if ($changes{'inststatus'}{'inststatusguest'}) {
10754:                             $resulttext .= '<li>'; 
10755:                             if (@orderedguests) {
10756:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to:').' ';
10757:                                 foreach my $type (@orderedguests) {
10758:                                     $resulttext .= $alltypes{$type}.', ';
10759:                                 }
10760:                                 $resulttext =~ s/, $//;
10761:                             } else {
10762:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to none.');
10763:                             }
10764:                             $resulttext .= '</li>';
10765:                         }
10766:                     }
10767:                 } else {
10768:                     my $value = $env{'form.'.$item};
10769:                     if ($value eq '') {
10770:                         $value = &mt('none');
10771:                     } elsif ($item eq 'auth_def') {
10772:                         my %authnames = &authtype_names();
10773:                         my %shortauth = (
10774:                                           internal   => 'int',
10775:                                           krb4       => 'krb4',
10776:                                           krb5       => 'krb5',
10777:                                           localauth  => 'loc',
10778:                         );
10779:                         $value = $authnames{$shortauth{$value}};
10780:                     } elsif ($item eq 'intauth_switch') {
10781:                         my %optiondesc = &Apache::lonlocal::texthash (
10782:                                             0 => 'No',
10783:                                             1 => 'Yes',
10784:                                             2 => 'Yes, and copy existing passwd file to passwd.bak file',
10785:                                          );
10786:                         if ($value =~ /^(0|1|2)$/) {
10787:                             $value = $optiondesc{$value};
10788:                         } else {
10789:                             $value = &mt('none -- defaults to No');
10790:                         }
10791:                     } elsif ($item eq 'intauth_check') {
10792:                         my %optiondesc = &Apache::lonlocal::texthash (
10793:                                              0 => 'No',
10794:                                              1 => 'Yes, allow login then update passwd file using default cost (if higher)',
10795:                                              2 => 'Yes, disallow login if stored cost is less than domain default',
10796:                                          );
10797:                         if ($value =~ /^(0|1|2)$/) {
10798:                             $value = $optiondesc{$value};
10799:                         } else {
10800:                             $value = &mt('none -- defaults to No');
10801:                         }
10802:                     }
10803:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
10804:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
10805:                 }
10806:             }
10807:             $resulttext .= '</ul>';
10808:             $mailmsgtext .= "\n";
10809:             my $cachetime = 24*60*60;
10810:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10811:             if (ref($lastactref) eq 'HASH') {
10812:                 $lastactref->{'domdefaults'} = 1;
10813:             }
10814:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
10815:                 my $notify = 1;
10816:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
10817:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
10818:                         $notify = 0;
10819:                     }
10820:                 }
10821:                 if ($notify) {
10822:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
10823:                                                "LON-CAPA Domain Settings Change - $dom",
10824:                                                $mailmsgtext);
10825:                 }
10826:             }
10827:         } else {
10828:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
10829:         }
10830:     } else {
10831:         $resulttext = '<span class="LC_error">'.
10832:             &mt('An error occurred: [_1]',$putresult).'</span>';
10833:     }
10834:     if (@errors > 0) {
10835:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
10836:         foreach my $item (@errors) {
10837:             $resulttext .= ' "'.$title->{$item}.'",';
10838:         }
10839:         $resulttext =~ s/,$//;
10840:     }
10841:     return $resulttext;
10842: }
10843: 
10844: sub modify_scantron {
10845:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
10846:     my ($resulttext,%confhash,%changes,$errors);
10847:     my $custom = 'custom.tab';
10848:     my $default = 'default.tab';
10849:     my $servadm = $r->dir_config('lonAdmEMail');
10850:     my ($configuserok,$author_ok,$switchserver) = 
10851:         &config_check($dom,$confname,$servadm);
10852:     if ($env{'form.scantronformat.filename'} ne '') {
10853:         my $error;
10854:         if ($configuserok eq 'ok') {
10855:             if ($switchserver) {
10856:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
10857:             } else {
10858:                 if ($author_ok eq 'ok') {
10859:                     my ($result,$scantronurl) =
10860:                         &publishlogo($r,'upload','scantronformat',$dom,
10861:                                      $confname,'scantron','','',$custom);
10862:                     if ($result eq 'ok') {
10863:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
10864:                         $changes{'scantronformat'} = 1;
10865:                     } else {
10866:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
10867:                     }
10868:                 } else {
10869:                     $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);
10870:                 }
10871:             }
10872:         } else {
10873:             $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);
10874:         }
10875:         if ($error) {
10876:             &Apache::lonnet::logthis($error);
10877:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10878:         }
10879:     }
10880:     if (ref($domconfig{'scantron'}) eq 'HASH') {
10881:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
10882:             if ($env{'form.scantronformat_del'}) {
10883:                 $confhash{'scantron'}{'scantronformat'} = '';
10884:                 $changes{'scantronformat'} = 1;
10885:             }
10886:         }
10887:     }
10888:     if (keys(%confhash) > 0) {
10889:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
10890:                                                  $dom);
10891:         if ($putresult eq 'ok') {
10892:             if (keys(%changes) > 0) {
10893:                 if (ref($confhash{'scantron'}) eq 'HASH') {
10894:                     $resulttext = &mt('Changes made:').'<ul>';
10895:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
10896:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
10897:                     } else {
10898:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
10899:                     }
10900:                     $resulttext .= '</ul>';
10901:                 } else {
10902:                     $resulttext = &mt('Changes made to bubblesheet format file.');
10903:                 }
10904:                 $resulttext .= '</ul>';
10905:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
10906:                 if (ref($lastactref) eq 'HASH') {
10907:                     $lastactref->{'domainconfig'} = 1;
10908:                 }
10909:             } else {
10910:                 $resulttext = &mt('No changes made to bubblesheet format file');
10911:             }
10912:         } else {
10913:             $resulttext = '<span class="LC_error">'.
10914:                 &mt('An error occurred: [_1]',$putresult).'</span>';
10915:         }
10916:     } else {
10917:         $resulttext = &mt('No changes made to bubblesheet format file'); 
10918:     }
10919:     if ($errors) {
10920:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
10921:                        $errors.'</ul>';
10922:     }
10923:     return $resulttext;
10924: }
10925: 
10926: sub modify_coursecategories {
10927:     my ($dom,$lastactref,%domconfig) = @_;
10928:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
10929:         $cathash);
10930:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
10931:     my @catitems = ('unauth','auth');
10932:     my @cattypes = ('std','domonly','codesrch','none');
10933:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
10934:         $cathash = $domconfig{'coursecategories'}{'cats'};
10935:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
10936:             $changes{'togglecats'} = 1;
10937:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
10938:         }
10939:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
10940:             $changes{'categorize'} = 1;
10941:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
10942:         }
10943:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
10944:             $changes{'togglecatscomm'} = 1;
10945:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
10946:         }
10947:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
10948:             $changes{'categorizecomm'} = 1;
10949:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
10950:         }
10951:         foreach my $item (@catitems) {
10952:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
10953:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
10954:                     $changes{$item} = 1;
10955:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
10956:                 }
10957:             }
10958:         }
10959:     } else {
10960:         $changes{'togglecats'} = 1;
10961:         $changes{'categorize'} = 1;
10962:         $changes{'togglecatscomm'} = 1;
10963:         $changes{'categorizecomm'} = 1;
10964:         $domconfig{'coursecategories'} = {
10965:                                              togglecats => $env{'form.togglecats'},
10966:                                              categorize => $env{'form.categorize'},
10967:                                              togglecatscomm => $env{'form.togglecatscomm'},
10968:                                              categorizecomm => $env{'form.categorizecomm'},
10969:                                          };
10970:         foreach my $item (@catitems) {
10971:             if ($env{'form.coursecat_'.$item} ne 'std') {
10972:                 $changes{$item} = 1;
10973:             }
10974:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
10975:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
10976:             }
10977:         }
10978:     }
10979:     if (ref($cathash) eq 'HASH') {
10980:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
10981:             push (@deletecategory,'instcode::0');
10982:         }
10983:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
10984:             push(@deletecategory,'communities::0');
10985:         }
10986:     }
10987:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
10988:     if (ref($cathash) eq 'HASH') {
10989:         if (@deletecategory > 0) {
10990:             #FIXME Need to remove category from all courses using a deleted category 
10991:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
10992:             foreach my $item (@deletecategory) {
10993:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
10994:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
10995:                     $deletions{$item} = 1;
10996:                     &recurse_cat_deletes($item,$cathash,\%deletions);
10997:                 }
10998:             }
10999:         }
11000:         foreach my $item (keys(%{$cathash})) {
11001:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
11002:             if ($cathash->{$item} ne $env{'form.'.$item}) {
11003:                 $reorderings{$item} = 1;
11004:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
11005:             }
11006:             if ($env{'form.addcategory_name_'.$item} ne '') {
11007:                 my $newcat = $env{'form.addcategory_name_'.$item};
11008:                 my $newdepth = $depth+1;
11009:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
11010:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
11011:                 $adds{$newitem} = 1; 
11012:             }
11013:             if ($env{'form.subcat_'.$item} ne '') {
11014:                 my $newcat = $env{'form.subcat_'.$item};
11015:                 my $newdepth = $depth+1;
11016:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
11017:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
11018:                 $adds{$newitem} = 1;
11019:             }
11020:         }
11021:     }
11022:     if ($env{'form.instcode'} eq '1') {
11023:         if (ref($cathash) eq 'HASH') {
11024:             my $newitem = 'instcode::0';
11025:             if ($cathash->{$newitem} eq '') {  
11026:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
11027:                 $adds{$newitem} = 1;
11028:             }
11029:         } else {
11030:             my $newitem = 'instcode::0';
11031:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
11032:             $adds{$newitem} = 1;
11033:         }
11034:     }
11035:     if ($env{'form.communities'} eq '1') {
11036:         if (ref($cathash) eq 'HASH') {
11037:             my $newitem = 'communities::0';
11038:             if ($cathash->{$newitem} eq '') {
11039:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
11040:                 $adds{$newitem} = 1;
11041:             }
11042:         } else {
11043:             my $newitem = 'communities::0';
11044:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
11045:             $adds{$newitem} = 1;
11046:         }
11047:     }
11048:     if ($env{'form.addcategory_name'} ne '') {
11049:         if (($env{'form.addcategory_name'} ne 'instcode') &&
11050:             ($env{'form.addcategory_name'} ne 'communities')) {
11051:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
11052:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
11053:             $adds{$newitem} = 1;
11054:         }
11055:     }
11056:     my $putresult;
11057:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
11058:         if (keys(%deletions) > 0) {
11059:             foreach my $key (keys(%deletions)) {
11060:                 if ($predelallitems{$key} ne '') {
11061:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
11062:                 }
11063:             }
11064:         }
11065:         my (@chkcats,@chktrails,%chkallitems);
11066:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
11067:         if (ref($chkcats[0]) eq 'ARRAY') {
11068:             my $depth = 0;
11069:             my $chg = 0;
11070:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
11071:                 my $name = $chkcats[0][$i];
11072:                 my $item;
11073:                 if ($name eq '') {
11074:                     $chg ++;
11075:                 } else {
11076:                     $item = &escape($name).'::0';
11077:                     if ($chg) {
11078:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
11079:                     }
11080:                     $depth ++; 
11081:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
11082:                     $depth --;
11083:                 }
11084:             }
11085:         }
11086:     }
11087:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
11088:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
11089:         if ($putresult eq 'ok') {
11090:             my %title = (
11091:                          togglecats     => 'Show/Hide a course in catalog',
11092:                          categorize     => 'Assign a category to a course',
11093:                          togglecatscomm => 'Show/Hide a community in catalog',
11094:                          categorizecomm => 'Assign a category to a community',
11095:                         );
11096:             my %level = (
11097:                          dom  => 'set in Domain ("Modify Course/Community")',
11098:                          crs  => 'set in Course ("Course Configuration")',
11099:                          comm => 'set in Community ("Community Configuration")',
11100:                          none     => 'No catalog',
11101:                          std      => 'Standard catalog',
11102:                          domonly  => 'Domain-only catalog',
11103:                          codesrch => 'Code search form',
11104:                         );
11105:             $resulttext = &mt('Changes made:').'<ul>';
11106:             if ($changes{'togglecats'}) {
11107:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
11108:             }
11109:             if ($changes{'categorize'}) {
11110:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
11111:             }
11112:             if ($changes{'togglecatscomm'}) {
11113:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
11114:             }
11115:             if ($changes{'categorizecomm'}) {
11116:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
11117:             }
11118:             if ($changes{'unauth'}) {
11119:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
11120:             }
11121:             if ($changes{'auth'}) {
11122:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
11123:             }
11124:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
11125:                 my $cathash;
11126:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
11127:                     $cathash = $domconfig{'coursecategories'}{'cats'};
11128:                 } else {
11129:                     $cathash = {};
11130:                 } 
11131:                 my (@cats,@trails,%allitems);
11132:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
11133:                 if (keys(%deletions) > 0) {
11134:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
11135:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
11136:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
11137:                     }
11138:                     $resulttext .= '</ul></li>';
11139:                 }
11140:                 if (keys(%reorderings) > 0) {
11141:                     my %sort_by_trail;
11142:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
11143:                     foreach my $key (keys(%reorderings)) {
11144:                         if ($allitems{$key} ne '') {
11145:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
11146:                         }
11147:                     }
11148:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
11149:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
11150:                     }
11151:                     $resulttext .= '</ul></li>';
11152:                 }
11153:                 if (keys(%adds) > 0) {
11154:                     my %sort_by_trail;
11155:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
11156:                     foreach my $key (keys(%adds)) {
11157:                         if ($allitems{$key} ne '') {
11158:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
11159:                         }
11160:                     }
11161:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
11162:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
11163:                     }
11164:                     $resulttext .= '</ul></li>';
11165:                 }
11166:             }
11167:             $resulttext .= '</ul>';
11168:             if ($changes{'unauth'} || $changes{'auth'}) {
11169:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
11170:                 if ($changes{'auth'}) {
11171:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
11172:                 }
11173:                 if ($changes{'unauth'}) {
11174:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
11175:                 }
11176:                 my $cachetime = 24*60*60;
11177:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
11178:                 if (ref($lastactref) eq 'HASH') {
11179:                     $lastactref->{'domdefaults'} = 1;
11180:                 }
11181:             }
11182:         } else {
11183:             $resulttext = '<span class="LC_error">'.
11184:                           &mt('An error occurred: [_1]',$putresult).'</span>';
11185:         }
11186:     } else {
11187:         $resulttext = &mt('No changes made to course and community categories');
11188:     }
11189:     return $resulttext;
11190: }
11191: 
11192: sub modify_serverstatuses {
11193:     my ($dom,%domconfig) = @_;
11194:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
11195:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
11196:         %currserverstatus = %{$domconfig{'serverstatuses'}};
11197:     }
11198:     my @pages = &serverstatus_pages();
11199:     foreach my $type (@pages) {
11200:         $newserverstatus{$type}{'namedusers'} = '';
11201:         $newserverstatus{$type}{'machines'} = '';
11202:         if (defined($env{'form.'.$type.'_namedusers'})) {
11203:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
11204:             my @okusers;
11205:             foreach my $user (@users) {
11206:                 my ($uname,$udom) = split(/:/,$user);
11207:                 if (($udom =~ /^$match_domain$/) &&   
11208:                     (&Apache::lonnet::domain($udom)) &&
11209:                     ($uname =~ /^$match_username$/)) {
11210:                     if (!grep(/^\Q$user\E/,@okusers)) {
11211:                         push(@okusers,$user);
11212:                     }
11213:                 }
11214:             }
11215:             if (@okusers > 0) {
11216:                  @okusers = sort(@okusers);
11217:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
11218:             }
11219:         }
11220:         if (defined($env{'form.'.$type.'_machines'})) {
11221:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
11222:             my @okmachines;
11223:             foreach my $ip (@machines) {
11224:                 my @parts = split(/\./,$ip);
11225:                 next if (@parts < 4);
11226:                 my $badip = 0;
11227:                 for (my $i=0; $i<4; $i++) {
11228:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
11229:                         $badip = 1;
11230:                         last;
11231:                     }
11232:                 }
11233:                 if (!$badip) {
11234:                     push(@okmachines,$ip);     
11235:                 }
11236:             }
11237:             @okmachines = sort(@okmachines);
11238:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
11239:         }
11240:     }
11241:     my %serverstatushash =  (
11242:                                 serverstatuses => \%newserverstatus,
11243:                             );
11244:     foreach my $type (@pages) {
11245:         foreach my $setting ('namedusers','machines') {
11246:             my (@current,@new);
11247:             if (ref($currserverstatus{$type}) eq 'HASH') {
11248:                 if ($currserverstatus{$type}{$setting} ne '') { 
11249:                     @current = split(/,/,$currserverstatus{$type}{$setting});
11250:                 }
11251:             }
11252:             if ($newserverstatus{$type}{$setting} ne '') {
11253:                 @new = split(/,/,$newserverstatus{$type}{$setting});
11254:             }
11255:             if (@current > 0) {
11256:                 if (@new > 0) {
11257:                     foreach my $item (@current) {
11258:                         if (!grep(/^\Q$item\E$/,@new)) {
11259:                             $changes{$type}{$setting} = 1;
11260:                             last;
11261:                         }
11262:                     }
11263:                     foreach my $item (@new) {
11264:                         if (!grep(/^\Q$item\E$/,@current)) {
11265:                             $changes{$type}{$setting} = 1;
11266:                             last;
11267:                         }
11268:                     }
11269:                 } else {
11270:                     $changes{$type}{$setting} = 1;
11271:                 }
11272:             } elsif (@new > 0) {
11273:                 $changes{$type}{$setting} = 1;
11274:             }
11275:         }
11276:     }
11277:     if (keys(%changes) > 0) {
11278:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
11279:         my $putresult = &Apache::lonnet::put_dom('configuration',
11280:                                                  \%serverstatushash,$dom);
11281:         if ($putresult eq 'ok') {
11282:             $resulttext .= &mt('Changes made:').'<ul>';
11283:             foreach my $type (@pages) {
11284:                 if (ref($changes{$type}) eq 'HASH') {
11285:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
11286:                     if ($changes{$type}{'namedusers'}) {
11287:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
11288:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
11289:                         } else {
11290:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
11291:                         }
11292:                     }
11293:                     if ($changes{$type}{'machines'}) {
11294:                         if ($newserverstatus{$type}{'machines'} eq '') {
11295:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
11296:                         } else {
11297:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
11298:                         }
11299: 
11300:                     }
11301:                     $resulttext .= '</ul></li>';
11302:                 }
11303:             }
11304:             $resulttext .= '</ul>';
11305:         } else {
11306:             $resulttext = '<span class="LC_error">'.
11307:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
11308: 
11309:         }
11310:     } else {
11311:         $resulttext = &mt('No changes made to access to server status pages');
11312:     }
11313:     return $resulttext;
11314: }
11315: 
11316: sub modify_helpsettings {
11317:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
11318:     my ($resulttext,$errors,%changes,%helphash);
11319:     my %defaultchecked = ('submitbugs' => 'on');
11320:     my @offon = ('off','on');
11321:     my @toggles = ('submitbugs');
11322:     my %current = ('submitbugs' => '',
11323:                    'adhoc'      => {},
11324:                   );
11325:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
11326:         %current = %{$domconfig{'helpsettings'}};
11327:     }
11328:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11329:     foreach my $item (@toggles) {
11330:         if ($defaultchecked{$item} eq 'on') { 
11331:             if ($current{$item} eq '') {
11332:                 if ($env{'form.'.$item} eq '0') {
11333:                     $changes{$item} = 1;
11334:                 }
11335:             } elsif ($current{$item} ne $env{'form.'.$item}) {
11336:                 $changes{$item} = 1;
11337:             }
11338:         } elsif ($defaultchecked{$item} eq 'off') {
11339:             if ($current{$item} eq '') {
11340:                 if ($env{'form.'.$item} eq '1') {
11341:                     $changes{$item} = 1;
11342:                 }
11343:             } elsif ($current{$item} ne $env{'form.'.$item}) {
11344:                 $changes{$item} = 1;
11345:             }
11346:         }
11347:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
11348:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
11349:         }
11350:     }
11351:     my $maxnum = $env{'form.helproles_maxnum'};
11352:     my $confname = $dom.'-domainconfig';
11353:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
11354:     my (@allpos,%newsettings,%changedprivs,$newrole);
11355:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
11356:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
11357:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
11358:     my %lt = &Apache::lonlocal::texthash(
11359:                     s      => 'system',
11360:                     d      => 'domain',
11361:                     order  => 'Display order',
11362:                     access => 'Role usage',
11363:                     all    => 'All with domain helpdesk or helpdesk assistant role',
11364:                     dh     => 'All with domain helpdesk role',
11365:                     da     => 'All with domain helpdesk assistant role',
11366:                     none   => 'None',
11367:                     status => 'Determined based on institutional status',
11368:                     inc    => 'Include all, but exclude specific personnel',
11369:                     exc    => 'Exclude all, but include specific personnel',
11370:     );
11371:     for (my $num=0; $num<=$maxnum; $num++) {
11372:         my ($prefix,$identifier,$rolename,%curr);
11373:         if ($num == $maxnum) {
11374:             next unless ($env{'form.newcusthelp'} == $maxnum);
11375:             $identifier = 'custhelp'.$num;
11376:             $prefix = 'helproles_'.$num;
11377:             $rolename = $env{'form.custhelpname'.$num};
11378:             $rolename=~s/[^A-Za-z0-9]//gs;
11379:             next if ($rolename eq '');
11380:             next if (exists($existing{'rolesdef_'.$rolename}));
11381:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
11382:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
11383:                                                      $newprivs{'c'},$confname,$dom);
11384:             if ($result ne 'ok') {
11385:                 $errors .= '<li><span class="LC_error">'.
11386:                            &mt('An error occurred storing the new custom role: [_1]',
11387:                            $result).'</span></li>';
11388:                 next;
11389:             } else {
11390:                 $changedprivs{$rolename} = \%newprivs;
11391:                 $newrole = $rolename;
11392:             }
11393:         } else {
11394:             $prefix = 'helproles_'.$num;
11395:             $rolename = $env{'form.'.$prefix};
11396:             next if ($rolename eq '');
11397:             next unless (exists($existing{'rolesdef_'.$rolename}));
11398:             $identifier = 'custhelp'.$num;
11399:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
11400:             my %currprivs;
11401:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
11402:                 split(/\_/,$existing{'rolesdef_'.$rolename});
11403:             foreach my $level ('c','d','s') {
11404:                 if ($newprivs{$level} ne $currprivs{$level}) {
11405:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
11406:                                                              $newprivs{'c'},$confname,$dom);
11407:                     if ($result ne 'ok') {
11408:                         $errors .= '<li><span class="LC_error">'.
11409:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
11410:                                        $rolename,$result).'</span></li>';
11411:                     } else {
11412:                         $changedprivs{$rolename} = \%newprivs;
11413:                     }
11414:                     last;
11415:                 }
11416:             }
11417:             if (ref($current{'adhoc'}) eq 'HASH') {
11418:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
11419:                     %curr = %{$current{'adhoc'}{$rolename}};
11420:                 }
11421:             }
11422:         }
11423:         my $newpos = $env{'form.'.$prefix.'_pos'};
11424:         $newpos =~ s/\D+//g;
11425:         $allpos[$newpos] = $rolename;
11426:         my $newdesc = $env{'form.'.$prefix.'_desc'};
11427:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
11428:         if ($curr{'desc'}) {
11429:             if ($curr{'desc'} ne $newdesc) {
11430:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
11431:                 $newsettings{$rolename}{'desc'} = $newdesc;
11432:             }
11433:         } elsif ($newdesc ne '') {
11434:             $changes{'customrole'}{$rolename}{'desc'} = 1;
11435:             $newsettings{$rolename}{'desc'} = $newdesc;
11436:         }
11437:         my $access = $env{'form.'.$prefix.'_access'};
11438:         if (grep(/^\Q$access\E$/,@accesstypes)) {
11439:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
11440:             if ($access eq 'status') {
11441:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
11442:                 if (scalar(@statuses) == 0) {
11443:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
11444:                 } else {
11445:                     my (@shownstatus,$numtypes);
11446:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
11447:                     if (ref($types) eq 'ARRAY') {
11448:                         $numtypes = scalar(@{$types});
11449:                         foreach my $type (sort(@statuses)) {
11450:                             if ($type eq 'default') {
11451:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
11452:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
11453:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
11454:                                 push(@shownstatus,$usertypes->{$type});
11455:                             }
11456:                         }
11457:                     }
11458:                     if (grep(/^default$/,@statuses)) {
11459:                         push(@shownstatus,$othertitle);
11460:                     }
11461:                     if (scalar(@shownstatus) == 1+$numtypes) {
11462:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
11463:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
11464:                     } else {
11465:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
11466:                         if (ref($curr{'status'}) eq 'ARRAY') {
11467:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
11468:                             if (@diffs) {
11469:                                 $changes{'customrole'}{$rolename}{$access} = 1;
11470:                             }
11471:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
11472:                             $changes{'customrole'}{$rolename}{$access} = 1;
11473:                         }
11474:                     }
11475:                 }
11476:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
11477:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
11478:                 my @newspecstaff;
11479:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
11480:                 foreach my $person (sort(@personnel)) {
11481:                     if ($domhelpdesk{$person}) {
11482:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
11483:                     }
11484:                 }
11485:                 if (ref($curr{$access}) eq 'ARRAY') {
11486:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
11487:                     if (@diffs) {
11488:                         $changes{'customrole'}{$rolename}{$access} = 1;
11489:                     }
11490:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
11491:                     $changes{'customrole'}{$rolename}{$access} = 1;
11492:                 }
11493:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
11494:                     my ($uname,$udom) = split(/:/,$person);
11495:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
11496:                 }
11497:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
11498:             }
11499:         } else {
11500:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
11501:         }
11502:         unless ($curr{'access'} eq $access) {
11503:             $changes{'customrole'}{$rolename}{'access'} = 1;
11504:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
11505:         }
11506:     }
11507:     if (@allpos > 0) {
11508:         my $idx = 0;
11509:         foreach my $rolename (@allpos) {
11510:             if ($rolename ne '') {
11511:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
11512:                 if (ref($current{'adhoc'}) eq 'HASH') {
11513:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
11514:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
11515:                             $changes{'customrole'}{$rolename}{'order'} = 1;
11516:                             $newsettings{$rolename}{'order'} = $idx+1;
11517:                         }
11518:                     }
11519:                 }
11520:                 $idx ++;
11521:             }
11522:         }
11523:     }
11524:     my $putresult;
11525:     if (keys(%changes) > 0) {
11526:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
11527:         if ($putresult eq 'ok') {
11528:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
11529:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
11530:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
11531:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
11532:                 }
11533:             }
11534:             my $cachetime = 24*60*60;
11535:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
11536:             if (ref($lastactref) eq 'HASH') {
11537:                 $lastactref->{'domdefaults'} = 1;
11538:             }
11539:         } else {
11540:             $errors .= '<li><span class="LC_error">'.
11541:                        &mt('An error occurred storing the settings: [_1]',
11542:                            $putresult).'</span></li>';
11543:         }
11544:     }
11545:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
11546:         $resulttext = &mt('Changes made:').'<ul>';
11547:         my (%shownprivs,@levelorder);
11548:         @levelorder = ('c','d','s');
11549:         if ((keys(%changes)) && ($putresult eq 'ok')) {
11550:             foreach my $item (sort(keys(%changes))) {
11551:                 if ($item eq 'submitbugs') {
11552:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
11553:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
11554:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
11555:                 } elsif ($item eq 'customrole') {
11556:                     if (ref($changes{'customrole'}) eq 'HASH') {
11557:                         my @keyorder = ('order','desc','access','status','exc','inc');
11558:                         my %keytext = &Apache::lonlocal::texthash(
11559:                                                                    order  => 'Order',
11560:                                                                    desc   => 'Role description',
11561:                                                                    access => 'Role usage',
11562:                                                                    status => 'Allowed instituional types',
11563:                                                                    exc    => 'Allowed personnel',
11564:                                                                    inc    => 'Disallowed personnel',
11565:                         );
11566:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
11567:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
11568:                                 if ($role eq $newrole) {
11569:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
11570:                                                               $role).'<ul>';
11571:                                 } else {
11572:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
11573:                                                               $role).'<ul>';
11574:                                 }
11575:                                 foreach my $key (@keyorder) {
11576:                                     if ($changes{'customrole'}{$role}{$key}) {
11577:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
11578:                                                                   $keytext{$key},$newsettings{$role}{$key}).
11579:                                                        '</li>';
11580:                                     }
11581:                                 }
11582:                                 if (ref($changedprivs{$role}) eq 'HASH') {
11583:                                     $shownprivs{$role} = 1;
11584:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
11585:                                     foreach my $level (@levelorder) {
11586:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
11587:                                             next if ($item eq '');
11588:                                             my ($priv) = split(/\&/,$item,2);
11589:                                             if (&Apache::lonnet::plaintext($priv)) {
11590:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
11591:                                                 unless ($level eq 'c') {
11592:                                                     $resulttext .= ' ('.$lt{$level}.')';
11593:                                                 }
11594:                                                 $resulttext .= '</li>';
11595:                                             }
11596:                                         }
11597:                                     }
11598:                                     $resulttext .= '</ul>';
11599:                                 }
11600:                                 $resulttext .= '</ul></li>';
11601:                             }
11602:                         }
11603:                     }
11604:                 }
11605:             }
11606:         }
11607:         if (keys(%changedprivs)) {
11608:             foreach my $role (sort(keys(%changedprivs))) {
11609:                 unless ($shownprivs{$role}) {
11610:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
11611:                                               $role).'<ul>'.
11612:                                    '<li>'.&mt('Privileges set to :').'<ul>';
11613:                     foreach my $level (@levelorder) {
11614:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
11615:                             next if ($item eq '');
11616:                             my ($priv) = split(/\&/,$item,2);
11617:                             if (&Apache::lonnet::plaintext($priv)) {
11618:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
11619:                                 unless ($level eq 'c') {
11620:                                     $resulttext .= ' ('.$lt{$level}.')';
11621:                                 }
11622:                                 $resulttext .= '</li>';
11623:                             }
11624:                         }
11625:                     }
11626:                     $resulttext .= '</ul></li></ul></li>';
11627:                 }
11628:             }
11629:         }
11630:         $resulttext .= '</ul>';
11631:     } else {
11632:         $resulttext = &mt('No changes made to help settings');
11633:     }
11634:     if ($errors) {
11635:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
11636:                                     $errors.'</ul>';
11637:     }
11638:     return $resulttext;
11639: }
11640: 
11641: sub modify_coursedefaults {
11642:     my ($dom,$lastactref,%domconfig) = @_;
11643:     my ($resulttext,$errors,%changes,%defaultshash);
11644:     my %defaultchecked = (
11645:                            'uselcmath'       => 'on',
11646:                            'usejsme'         => 'on'
11647:                          );
11648:     my @toggles = ('uselcmath','usejsme');
11649:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
11650:                    'uploadquota_community','uploadquota_textbook','mysqltables_official',
11651:                    'mysqltables_unofficial','mysqltables_community','mysqltables_textbook');
11652:     my @types = ('official','unofficial','community','textbook');
11653:     my %staticdefaults = (
11654:                            anonsurvey_threshold => 10,
11655:                            uploadquota          => 500,
11656:                            postsubmit           => 60,
11657:                            mysqltables          => 172800,
11658:                          );
11659: 
11660:     $defaultshash{'coursedefaults'} = {};
11661: 
11662:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
11663:         if ($domconfig{'coursedefaults'} eq '') {
11664:             $domconfig{'coursedefaults'} = {};
11665:         }
11666:     }
11667: 
11668:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
11669:         foreach my $item (@toggles) {
11670:             if ($defaultchecked{$item} eq 'on') {
11671:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
11672:                     ($env{'form.'.$item} eq '0')) {
11673:                     $changes{$item} = 1;
11674:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
11675:                     $changes{$item} = 1;
11676:                 }
11677:             } elsif ($defaultchecked{$item} eq 'off') {
11678:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
11679:                     ($env{'form.'.$item} eq '1')) {
11680:                     $changes{$item} = 1;
11681:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
11682:                     $changes{$item} = 1;
11683:                 }
11684:             }
11685:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
11686:         }
11687:         foreach my $item (@numbers) {
11688:             my ($currdef,$newdef);
11689:             $newdef = $env{'form.'.$item};
11690:             if ($item eq 'anonsurvey_threshold') {
11691:                 $currdef = $domconfig{'coursedefaults'}{$item};
11692:                 $newdef =~ s/\D//g;
11693:                 if ($newdef eq '' || $newdef < 1) {
11694:                     $newdef = 1;
11695:                 }
11696:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
11697:             } else {
11698:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
11699:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
11700:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
11701:                 }
11702:                 $newdef =~ s/[^\w.\-]//g;
11703:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
11704:             }
11705:             if ($currdef ne $newdef) {
11706:                 my $staticdef;
11707:                 if ($item eq 'anonsurvey_threshold') {
11708:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
11709:                         $changes{$item} = 1;
11710:                     }
11711:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
11712:                     my $setting = $1;
11713:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
11714:                         $changes{$setting} = 1;
11715:                     }
11716:                 }
11717:             }
11718:         }
11719:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
11720:         my @currclonecode;
11721:         if (ref($currclone) eq 'HASH') {
11722:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
11723:                 @currclonecode = @{$currclone->{'instcode'}};
11724:             }
11725:         }
11726:         my $newclone;
11727:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
11728:             $newclone = $env{'form.canclone'};
11729:         }
11730:         if ($newclone eq 'instcode') {
11731:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
11732:             my (%codedefaults,@code_order,@clonecode);
11733:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
11734:                                                     \@code_order);
11735:             foreach my $item (@code_order) {
11736:                 if (grep(/^\Q$item\E$/,@newcodes)) {
11737:                     push(@clonecode,$item);
11738:                 }
11739:             }
11740:             if (@clonecode) {
11741:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
11742:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
11743:                 if (@diffs) {
11744:                     $changes{'canclone'} = 1;
11745:                 }
11746:             } else {
11747:                 $newclone eq '';
11748:             }
11749:         } elsif ($newclone ne '') {
11750:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
11751:         }
11752:         if ($newclone ne $currclone) {
11753:             $changes{'canclone'} = 1;
11754:         }
11755:         my %credits;
11756:         foreach my $type (@types) {
11757:             unless ($type eq 'community') {
11758:                 $credits{$type} = $env{'form.'.$type.'_credits'};
11759:                 $credits{$type} =~ s/[^\d.]+//g;
11760:             }
11761:         }
11762:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
11763:             ($env{'form.coursecredits'} eq '1')) {
11764:             $changes{'coursecredits'} = 1;
11765:             foreach my $type (keys(%credits)) {
11766:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
11767:             }
11768:         } else {
11769:             if ($env{'form.coursecredits'} eq '1') {
11770:                 foreach my $type (@types) {
11771:                     unless ($type eq 'community') {
11772:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
11773:                             $changes{'coursecredits'} = 1;
11774:                         }
11775:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
11776:                     }
11777:                 }
11778:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
11779:                 foreach my $type (@types) {
11780:                     unless ($type eq 'community') {
11781:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
11782:                             $changes{'coursecredits'} = 1;
11783:                             last;
11784:                         }
11785:                     }
11786:                 }
11787:             }
11788:         }
11789:         if ($env{'form.postsubmit'} eq '1') {
11790:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
11791:             my %currtimeout;
11792:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
11793:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
11794:                     $changes{'postsubmit'} = 1;
11795:                 }
11796:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
11797:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
11798:                 }
11799:             } else {
11800:                 $changes{'postsubmit'} = 1;
11801:             }
11802:             foreach my $type (@types) {
11803:                 my $timeout = $env{'form.'.$type.'_timeout'};
11804:                 $timeout =~ s/\D//g;
11805:                 if ($timeout == $staticdefaults{'postsubmit'}) {
11806:                     $timeout = '';
11807:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
11808:                     $timeout = '0';
11809:                 }
11810:                 unless ($timeout eq '') {
11811:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
11812:                 }
11813:                 if (exists($currtimeout{$type})) {
11814:                     if ($timeout ne $currtimeout{$type}) {
11815:                         $changes{'postsubmit'} = 1;
11816:                     }
11817:                 } elsif ($timeout ne '') {
11818:                     $changes{'postsubmit'} = 1;
11819:                 }
11820:             }
11821:         } else {
11822:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
11823:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
11824:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
11825:                     $changes{'postsubmit'} = 1;
11826:                 }
11827:             } else {
11828:                 $changes{'postsubmit'} = 1;
11829:             }
11830:         }
11831:     }
11832:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
11833:                                              $dom);
11834:     if ($putresult eq 'ok') {
11835:         if (keys(%changes) > 0) {
11836:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11837:             if (($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
11838:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
11839:                 ($changes{'canclone'}) || ($changes{'mysqltables'})) {
11840:                 foreach my $item ('uselcmath','usejsme') {
11841:                     if ($changes{$item}) {
11842:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
11843:                     }
11844:                 }
11845:                 if ($changes{'coursecredits'}) {
11846:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
11847:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
11848:                             $domdefaults{$type.'credits'} =
11849:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
11850:                         }
11851:                     }
11852:                 }
11853:                 if ($changes{'postsubmit'}) {
11854:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
11855:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
11856:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
11857:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
11858:                                 $domdefaults{$type.'postsubtimeout'} =
11859:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
11860:                             }
11861:                         }
11862:                     }
11863:                 }
11864:                 if ($changes{'uploadquota'}) {
11865:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
11866:                         foreach my $type (@types) {
11867:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
11868:                         }
11869:                     }
11870:                 }
11871:                 if ($changes{'canclone'}) {
11872:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
11873:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
11874:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
11875:                             if (@clonecodes) {
11876:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
11877:                             }
11878:                         }
11879:                     } else {
11880:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
11881:                     }
11882:                 }
11883:                 my $cachetime = 24*60*60;
11884:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
11885:                 if (ref($lastactref) eq 'HASH') {
11886:                     $lastactref->{'domdefaults'} = 1;
11887:                 }
11888:             }
11889:             $resulttext = &mt('Changes made:').'<ul>';
11890:             foreach my $item (sort(keys(%changes))) {
11891:                 if ($item eq 'uselcmath') {
11892:                     if ($env{'form.'.$item} eq '1') {
11893:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
11894:                     } else {
11895:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
11896:                     }
11897:                 } elsif ($item eq 'usejsme') {
11898:                     if ($env{'form.'.$item} eq '1') {
11899:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
11900:                     } else {
11901:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
11902:                     }
11903:                 } elsif ($item eq 'anonsurvey_threshold') {
11904:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
11905:                 } elsif ($item eq 'uploadquota') {
11906:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
11907:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
11908:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
11909:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
11910:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
11911: 
11912:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
11913:                                        '</ul>'.
11914:                                        '</li>';
11915:                     } else {
11916:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
11917:                     }
11918:                 } elsif ($item eq 'mysqltables') {
11919:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
11920:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
11921:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
11922:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
11923:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
11924:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
11925:                                        '</ul>'.
11926:                                        '</li>';
11927:                     } else {
11928:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
11929:                     }
11930:                 } elsif ($item eq 'postsubmit') {
11931:                     if ($domdefaults{'postsubmit'} eq 'off') {
11932:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
11933:                     } else {
11934:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
11935:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
11936:                             $resulttext .= &mt('durations:').'<ul>';
11937:                             foreach my $type (@types) {
11938:                                 $resulttext .= '<li>';
11939:                                 my $timeout;
11940:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
11941:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
11942:                                 }
11943:                                 my $display;
11944:                                 if ($timeout eq '0') {
11945:                                     $display = &mt('unlimited');
11946:                                 } elsif ($timeout eq '') {
11947:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
11948:                                 } else {
11949:                                     $display = &mt('[quant,_1,second]',$timeout);
11950:                                 }
11951:                                 if ($type eq 'community') {
11952:                                     $resulttext .= &mt('Communities');
11953:                                 } elsif ($type eq 'official') {
11954:                                     $resulttext .= &mt('Official courses');
11955:                                 } elsif ($type eq 'unofficial') {
11956:                                     $resulttext .= &mt('Unofficial courses');
11957:                                 } elsif ($type eq 'textbook') {
11958:                                     $resulttext .= &mt('Textbook courses');
11959:                                 }
11960:                                 $resulttext .= ' -- '.$display.'</li>';
11961:                             }
11962:                             $resulttext .= '</ul>';
11963:                         }
11964:                         $resulttext .= '</li>';
11965:                     }
11966:                 } elsif ($item eq 'coursecredits') {
11967:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
11968:                         if (($domdefaults{'officialcredits'} eq '') &&
11969:                             ($domdefaults{'unofficialcredits'} eq '') &&
11970:                             ($domdefaults{'textbookcredits'} eq '')) {
11971:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
11972:                         } else {
11973:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
11974:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
11975:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
11976:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
11977:                                            '</ul>'.
11978:                                            '</li>';
11979:                         }
11980:                     } else {
11981:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
11982:                     }
11983:                 } elsif ($item eq 'canclone') {
11984:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
11985:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
11986:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
11987:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
11988:                         }
11989:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
11990:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
11991:                     } else {
11992:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
11993:                     }
11994:                 }
11995:             }
11996:             $resulttext .= '</ul>';
11997:         } else {
11998:             $resulttext = &mt('No changes made to course defaults');
11999:         }
12000:     } else {
12001:         $resulttext = '<span class="LC_error">'.
12002:             &mt('An error occurred: [_1]',$putresult).'</span>';
12003:     }
12004:     return $resulttext;
12005: }
12006: 
12007: sub modify_selfenrollment {
12008:     my ($dom,$lastactref,%domconfig) = @_;
12009:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
12010:     my @types = ('official','unofficial','community','textbook');
12011:     my %titles = &tool_titles();
12012:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
12013:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
12014:     $ordered{'default'} = ['types','registered','approval','limit'];
12015: 
12016:     my (%roles,%shown,%toplevel);
12017:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
12018: 
12019:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
12020:         if ($domconfig{'selfenrollment'} eq '') {
12021:             $domconfig{'selfenrollment'} = {};
12022:         }
12023:     }
12024:     %toplevel = (
12025:                   admin      => 'Configuration Rights',
12026:                   default    => 'Default settings',
12027:                   validation => 'Validation of self-enrollment requests',
12028:                 );
12029:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
12030: 
12031:     if (ref($ordered{'admin'}) eq 'ARRAY') {
12032:         foreach my $item (@{$ordered{'admin'}}) {
12033:             foreach my $type (@types) {
12034:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
12035:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
12036:                 } else {
12037:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
12038:                 }
12039:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
12040:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
12041:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
12042:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
12043:                             push(@{$changes{'admin'}{$type}},$item);
12044:                         }
12045:                     } else {
12046:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
12047:                             push(@{$changes{'admin'}{$type}},$item);
12048:                         }
12049:                     }
12050:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
12051:                     push(@{$changes{'admin'}{$type}},$item);
12052:                 }
12053:             }
12054:         }
12055:     }
12056: 
12057:     foreach my $item (@{$ordered{'default'}}) {
12058:         foreach my $type (@types) {
12059:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
12060:             if ($item eq 'types') {
12061:                 unless (($value eq 'all') || ($value eq 'dom')) {
12062:                     $value = '';
12063:                 }
12064:             } elsif ($item eq 'registered') {
12065:                 unless ($value eq '1') {
12066:                     $value = 0;
12067:                 }
12068:             } elsif ($item eq 'approval') {
12069:                 unless ($value =~ /^[012]$/) {
12070:                     $value = 0;
12071:                 }
12072:             } else {
12073:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
12074:                     $value = 'none';
12075:                 }
12076:             }
12077:             $selfenrollhash{'default'}{$type}{$item} = $value;
12078:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
12079:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
12080:                     if ($selfenrollhash{'default'}{$type}{$item} ne
12081:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
12082:                          push(@{$changes{'default'}{$type}},$item);
12083:                     }
12084:                 } else {
12085:                     push(@{$changes{'default'}{$type}},$item);
12086:                 }
12087:             } else {
12088:                 push(@{$changes{'default'}{$type}},$item);
12089:             }
12090:             if ($item eq 'limit') {
12091:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
12092:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
12093:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
12094:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
12095:                     }
12096:                 } else {
12097:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
12098:                 }
12099:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
12100:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
12101:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
12102:                          push(@{$changes{'default'}{$type}},'cap');
12103:                     }
12104:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
12105:                     push(@{$changes{'default'}{$type}},'cap');
12106:                 }
12107:             }
12108:         }
12109:     }
12110: 
12111:     foreach my $item (@{$itemsref}) {
12112:         if ($item eq 'fields') {
12113:             my @changed;
12114:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
12115:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
12116:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
12117:             }
12118:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
12119:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
12120:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
12121:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
12122:                 } else {
12123:                     @changed = @{$selfenrollhash{'validation'}{$item}};
12124:                 }
12125:             } else {
12126:                 @changed = @{$selfenrollhash{'validation'}{$item}};
12127:             }
12128:             if (@changed) {
12129:                 if ($selfenrollhash{'validation'}{$item}) { 
12130:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
12131:                 } else {
12132:                     $changes{'validation'}{$item} = &mt('None');
12133:                 }
12134:             }
12135:         } else {
12136:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
12137:             if ($item eq 'markup') {
12138:                if ($env{'form.selfenroll_validation_'.$item}) {
12139:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
12140:                }
12141:             }
12142:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
12143:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
12144:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
12145:                 }
12146:             }
12147:         }
12148:     }
12149: 
12150:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
12151:                                              $dom);
12152:     if ($putresult eq 'ok') {
12153:         if (keys(%changes) > 0) {
12154:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12155:             $resulttext = &mt('Changes made:').'<ul>';
12156:             foreach my $key ('admin','default','validation') {
12157:                 if (ref($changes{$key}) eq 'HASH') {
12158:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
12159:                     if ($key eq 'validation') {
12160:                         foreach my $item (@{$itemsref}) {
12161:                             if (exists($changes{$key}{$item})) {
12162:                                 if ($item eq 'markup') {
12163:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
12164:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
12165:                                 } else {  
12166:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
12167:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
12168:                                 }
12169:                             }
12170:                         }
12171:                     } else {
12172:                         foreach my $type (@types) {
12173:                             if ($type eq 'community') {
12174:                                 $roles{'1'} = &mt('Community personnel');
12175:                             } else {
12176:                                 $roles{'1'} = &mt('Course personnel');
12177:                             }
12178:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
12179:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
12180:                                     if ($key eq 'admin') {
12181:                                         my @mgrdc = ();
12182:                                         if (ref($ordered{$key}) eq 'ARRAY') {
12183:                                             foreach my $item (@{$ordered{'admin'}}) {
12184:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
12185:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
12186:                                                         push(@mgrdc,$item);
12187:                                                     }
12188:                                                 }
12189:                                             }
12190:                                             if (@mgrdc) {
12191:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
12192:                                             } else {
12193:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
12194:                                             }
12195:                                         }
12196:                                     } else {
12197:                                         if (ref($ordered{$key}) eq 'ARRAY') {
12198:                                             foreach my $item (@{$ordered{$key}}) {
12199:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
12200:                                                     $domdefaults{$type.'selfenroll'.$item} =
12201:                                                         $selfenrollhash{$key}{$type}{$item};
12202:                                                 }
12203:                                             }
12204:                                         }
12205:                                     }
12206:                                 }
12207:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
12208:                                 foreach my $item (@{$ordered{$key}}) {
12209:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
12210:                                         $resulttext .= '<li>';
12211:                                         if ($key eq 'admin') {
12212:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
12213:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
12214:                                         } else {
12215:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
12216:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
12217:                                         }
12218:                                         $resulttext .= '</li>';
12219:                                     }
12220:                                 }
12221:                                 $resulttext .= '</ul></li>';
12222:                             }
12223:                         }
12224:                         $resulttext .= '</ul></li>'; 
12225:                     }
12226:                 }
12227:                 if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
12228:                     my $cachetime = 24*60*60;
12229:                     &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12230:                     if (ref($lastactref) eq 'HASH') {
12231:                         $lastactref->{'domdefaults'} = 1;
12232:                     }
12233:                 }
12234:             }
12235:             $resulttext .= '</ul>';
12236:         } else {
12237:             $resulttext = &mt('No changes made to self-enrollment settings');
12238:         }
12239:     } else {
12240:         $resulttext = '<span class="LC_error">'.
12241:             &mt('An error occurred: [_1]',$putresult).'</span>';
12242:     }
12243:     return $resulttext;
12244: }
12245: 
12246: sub modify_usersessions {
12247:     my ($dom,$lastactref,%domconfig) = @_;
12248:     my @hostingtypes = ('version','excludedomain','includedomain');
12249:     my @offloadtypes = ('primary','default');
12250:     my %types = (
12251:                   remote => \@hostingtypes,
12252:                   hosted => \@hostingtypes,
12253:                   spares => \@offloadtypes,
12254:                 );
12255:     my @prefixes = ('remote','hosted','spares');
12256:     my @lcversions = &Apache::lonnet::all_loncaparevs();
12257:     my (%by_ip,%by_location,@intdoms);
12258:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
12259:     my @locations = sort(keys(%by_location));
12260:     my (%defaultshash,%changes);
12261:     foreach my $prefix (@prefixes) {
12262:         $defaultshash{'usersessions'}{$prefix} = {};
12263:     }
12264:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12265:     my $resulttext;
12266:     my %iphost = &Apache::lonnet::get_iphost();
12267:     foreach my $prefix (@prefixes) {
12268:         next if ($prefix eq 'spares');
12269:         foreach my $type (@{$types{$prefix}}) {
12270:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
12271:             if ($type eq 'version') {
12272:                 my $value = $env{'form.'.$prefix.'_'.$type};
12273:                 my $okvalue;
12274:                 if ($value ne '') {
12275:                     if (grep(/^\Q$value\E$/,@lcversions)) {
12276:                         $okvalue = $value;
12277:                     }
12278:                 }
12279:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
12280:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
12281:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
12282:                             if ($inuse == 0) {
12283:                                 $changes{$prefix}{$type} = 1;
12284:                             } else {
12285:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
12286:                                     $changes{$prefix}{$type} = 1;
12287:                                 }
12288:                                 if ($okvalue ne '') {
12289:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
12290:                                 } 
12291:                             }
12292:                         } else {
12293:                             if (($inuse == 1) && ($okvalue ne '')) {
12294:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
12295:                                 $changes{$prefix}{$type} = 1;
12296:                             }
12297:                         }
12298:                     } else {
12299:                         if (($inuse == 1) && ($okvalue ne '')) {
12300:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
12301:                             $changes{$prefix}{$type} = 1;
12302:                         }
12303:                     }
12304:                 } else {
12305:                     if (($inuse == 1) && ($okvalue ne '')) {
12306:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
12307:                         $changes{$prefix}{$type} = 1;
12308:                     }
12309:                 }
12310:             } else {
12311:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
12312:                 my @okvals;
12313:                 foreach my $val (@vals) {
12314:                     if ($val =~ /:/) {
12315:                         my @items = split(/:/,$val);
12316:                         foreach my $item (@items) {
12317:                             if (ref($by_location{$item}) eq 'ARRAY') {
12318:                                 push(@okvals,$item);
12319:                             }
12320:                         }
12321:                     } else {
12322:                         if (ref($by_location{$val}) eq 'ARRAY') {
12323:                             push(@okvals,$val);
12324:                         }
12325:                     }
12326:                 }
12327:                 @okvals = sort(@okvals);
12328:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
12329:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
12330:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
12331:                             if ($inuse == 0) {
12332:                                 $changes{$prefix}{$type} = 1; 
12333:                             } else {
12334:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
12335:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
12336:                                 if (@changed > 0) {
12337:                                     $changes{$prefix}{$type} = 1;
12338:                                 }
12339:                             }
12340:                         } else {
12341:                             if ($inuse == 1) {
12342:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
12343:                                 $changes{$prefix}{$type} = 1;
12344:                             }
12345:                         } 
12346:                     } else {
12347:                         if ($inuse == 1) {
12348:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
12349:                             $changes{$prefix}{$type} = 1;
12350:                         }
12351:                     }
12352:                 } else {
12353:                     if ($inuse == 1) {
12354:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
12355:                         $changes{$prefix}{$type} = 1;
12356:                     }
12357:                 }
12358:             }
12359:         }
12360:     }
12361: 
12362:     my @alldoms = &Apache::lonnet::all_domains();
12363:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
12364:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
12365:     my $savespares;
12366: 
12367:     foreach my $lonhost (sort(keys(%servers))) {
12368:         my $serverhomeID =
12369:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
12370:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
12371:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
12372:         my %spareschg;
12373:         foreach my $type (@{$types{'spares'}}) {
12374:             my @okspares;
12375:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
12376:             foreach my $server (@checked) {
12377:                 if (&Apache::lonnet::hostname($server) ne '') {
12378:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
12379:                         unless (grep(/^\Q$server\E$/,@okspares)) {
12380:                             push(@okspares,$server);
12381:                         }
12382:                     }
12383:                 }
12384:             }
12385:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
12386:             my $newspare;
12387:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
12388:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
12389:                     $newspare = $new;
12390:                 }
12391:             }
12392:             my @spares;
12393:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
12394:                 @spares = sort(@okspares,$newspare);
12395:             } else {
12396:                 @spares = sort(@okspares);
12397:             }
12398:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
12399:             if (ref($spareid{$lonhost}) eq 'HASH') {
12400:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
12401:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
12402:                     if (@diffs > 0) {
12403:                         $spareschg{$type} = 1;
12404:                     }
12405:                 }
12406:             }
12407:         }
12408:         if (keys(%spareschg) > 0) {
12409:             $changes{'spares'}{$lonhost} = \%spareschg;
12410:         }
12411:     }
12412:     $defaultshash{'usersessions'}{'offloadnow'} = {};
12413:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
12414:     my @okoffload;
12415:     if (@offloadnow) {
12416:         foreach my $server (@offloadnow) {
12417:             if (&Apache::lonnet::hostname($server) ne '') {
12418:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
12419:                     push(@okoffload,$server);
12420:                 }
12421:             }
12422:         }
12423:         if (@okoffload) {
12424:             foreach my $lonhost (@okoffload) {
12425:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
12426:             }
12427:         }
12428:     }
12429:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
12430:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
12431:             if (ref($changes{'spares'}) eq 'HASH') {
12432:                 if (keys(%{$changes{'spares'}}) > 0) {
12433:                     $savespares = 1;
12434:                 }
12435:             }
12436:         } else {
12437:             $savespares = 1;
12438:         }
12439:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
12440:             foreach my $lonhost (keys(%{$domconfig{'usersessions'}{'offloadnow'}})) {
12441:                 unless ($defaultshash{'usersessions'}{'offloadnow'}{$lonhost}) {
12442:                     $changes{'offloadnow'} = 1;
12443:                     last;
12444:                 }
12445:             }
12446:             unless ($changes{'offloadnow'}) {
12447:                 foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{'offloadnow'}})) {
12448:                     unless ($domconfig{'usersessions'}{'offloadnow'}{$lonhost}) {
12449:                         $changes{'offloadnow'} = 1;
12450:                         last;
12451:                     }
12452:                 }
12453:             }
12454:         } elsif (@okoffload) {
12455:             $changes{'offloadnow'} = 1;
12456:         }
12457:     } elsif (@okoffload) {
12458:         $changes{'offloadnow'} = 1;
12459:     }
12460:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
12461:     if ((keys(%changes) > 0) || ($savespares)) {
12462:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
12463:                                                  $dom);
12464:         if ($putresult eq 'ok') {
12465:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
12466:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
12467:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
12468:                 }
12469:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
12470:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
12471:                 }
12472:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
12473:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
12474:                 }
12475:             }
12476:             my $cachetime = 24*60*60;
12477:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12478:             if (ref($lastactref) eq 'HASH') {
12479:                 $lastactref->{'domdefaults'} = 1;
12480:             }
12481:             if (keys(%changes) > 0) {
12482:                 my %lt = &usersession_titles();
12483:                 $resulttext = &mt('Changes made:').'<ul>';
12484:                 foreach my $prefix (@prefixes) {
12485:                     if (ref($changes{$prefix}) eq 'HASH') {
12486:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
12487:                         if ($prefix eq 'spares') {
12488:                             if (ref($changes{$prefix}) eq 'HASH') {
12489:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
12490:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
12491:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
12492:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
12493:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
12494:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
12495:                                         foreach my $type (@{$types{$prefix}}) {
12496:                                             if ($changes{$prefix}{$lonhost}{$type}) {
12497:                                                 my $offloadto = &mt('None');
12498:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
12499:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
12500:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
12501:                                                     }
12502:                                                 }
12503:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
12504:                                             }
12505:                                         }
12506:                                     }
12507:                                     $resulttext .= '</li>';
12508:                                 }
12509:                             }
12510:                         } else {
12511:                             foreach my $type (@{$types{$prefix}}) {
12512:                                 if (defined($changes{$prefix}{$type})) {
12513:                                     my $newvalue;
12514:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
12515:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
12516:                                             if ($type eq 'version') {
12517:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
12518:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
12519:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
12520:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
12521:                                                 }
12522:                                             }
12523:                                         }
12524:                                     }
12525:                                     if ($newvalue eq '') {
12526:                                         if ($type eq 'version') {
12527:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
12528:                                         } else {
12529:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
12530:                                         }
12531:                                     } else {
12532:                                         if ($type eq 'version') {
12533:                                             $newvalue .= ' '.&mt('(or later)'); 
12534:                                         }
12535:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
12536:                                     }
12537:                                 }
12538:                             }
12539:                         }
12540:                         $resulttext .= '</ul>';
12541:                     }
12542:                 }
12543:                 if ($changes{'offloadnow'}) {
12544:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
12545:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
12546:                             $resulttext .= '<li>'.&mt('Switch active users on next access, for server(s):').'<ul>';
12547:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
12548:                                 $resulttext .= '<li>'.$lonhost.'</li>';
12549:                             }
12550:                             $resulttext .= '</ul>';
12551:                         } else {
12552:                             $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.');
12553:                         }
12554:                     } else {
12555:                         $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.').'</li>';
12556:                     }
12557:                 }
12558:                 $resulttext .= '</ul>';
12559:             } else {
12560:                 $resulttext = $nochgmsg;
12561:             }
12562:         } else {
12563:             $resulttext = '<span class="LC_error">'.
12564:                           &mt('An error occurred: [_1]',$putresult).'</span>';
12565:         }
12566:     } else {
12567:         $resulttext = $nochgmsg;
12568:     }
12569:     return $resulttext;
12570: }
12571: 
12572: sub modify_loadbalancing {
12573:     my ($dom,%domconfig) = @_;
12574:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
12575:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
12576:     my ($othertitle,$usertypes,$types) =
12577:         &Apache::loncommon::sorted_inst_types($dom);
12578:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
12579:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
12580:     my @sparestypes = ('primary','default');
12581:     my %typetitles = &sparestype_titles();
12582:     my $resulttext;
12583:     my (%currbalancer,%currtargets,%currrules,%existing);
12584:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
12585:         %existing = %{$domconfig{'loadbalancing'}};
12586:     }
12587:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
12588:                               \%currtargets,\%currrules);
12589:     my ($saveloadbalancing,%defaultshash,%changes);
12590:     my ($alltypes,$othertypes,$titles) =
12591:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
12592:     my %ruletitles = &offloadtype_text();
12593:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
12594:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
12595:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
12596:         if ($balancer eq '') {
12597:             next;
12598:         }
12599:         if (!exists($servers{$balancer})) {
12600:             if (exists($currbalancer{$balancer})) {
12601:                 push(@{$changes{'delete'}},$balancer);
12602:             }
12603:             next;
12604:         }
12605:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
12606:             push(@{$changes{'delete'}},$balancer);
12607:             next;
12608:         }
12609:         if (!exists($currbalancer{$balancer})) {
12610:             push(@{$changes{'add'}},$balancer);
12611:         }
12612:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
12613:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
12614:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
12615:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
12616:             $saveloadbalancing = 1;
12617:         }
12618:         foreach my $sparetype (@sparestypes) {
12619:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
12620:             my @offloadto;
12621:             foreach my $target (@targets) {
12622:                 if (($servers{$target}) && ($target ne $balancer)) {
12623:                     if ($sparetype eq 'default') {
12624:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
12625:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
12626:                         }
12627:                     }
12628:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
12629:                         push(@offloadto,$target);
12630:                     }
12631:                 }
12632:             }
12633:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
12634:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
12635:                     push(@offloadto,$balancer);
12636:                 }
12637:             }
12638:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
12639:         }
12640:         if (ref($currtargets{$balancer}) eq 'HASH') {
12641:             foreach my $sparetype (@sparestypes) {
12642:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
12643:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
12644:                     if (@targetdiffs > 0) {
12645:                         $changes{'curr'}{$balancer}{'targets'} = 1;
12646:                     }
12647:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
12648:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
12649:                         $changes{'curr'}{$balancer}{'targets'} = 1;
12650:                     }
12651:                 }
12652:             }
12653:         } else {
12654:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
12655:                 foreach my $sparetype (@sparestypes) {
12656:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
12657:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
12658:                             $changes{'curr'}{$balancer}{'targets'} = 1;
12659:                         }
12660:                     }
12661:                 }
12662:             }
12663:         }
12664:         my $ishomedom;
12665:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
12666:             $ishomedom = 1;
12667:         }
12668:         if (ref($alltypes) eq 'ARRAY') {
12669:             foreach my $type (@{$alltypes}) {
12670:                 my $rule;
12671:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
12672:                          (!$ishomedom)) {
12673:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
12674:                 }
12675:                 if ($rule eq 'specific') {
12676:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
12677:                     if (exists($servers{$specifiedhost})) {
12678:                         $rule = $specifiedhost;
12679:                     }
12680:                 }
12681:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
12682:                 if (ref($currrules{$balancer}) eq 'HASH') {
12683:                     if ($rule ne $currrules{$balancer}{$type}) {
12684:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
12685:                     }
12686:                 } elsif ($rule ne '') {
12687:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
12688:                 }
12689:             }
12690:         }
12691:     }
12692:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
12693:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
12694:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
12695:             $defaultshash{'loadbalancing'} = {};
12696:         }
12697:         my $putresult = &Apache::lonnet::put_dom('configuration',
12698:                                                  \%defaultshash,$dom);
12699:         if ($putresult eq 'ok') {
12700:             if (keys(%changes) > 0) {
12701:                 my %toupdate;
12702:                 if (ref($changes{'delete'}) eq 'ARRAY') {
12703:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
12704:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
12705:                         $toupdate{$balancer} = 1;
12706:                     }
12707:                 }
12708:                 if (ref($changes{'add'}) eq 'ARRAY') {
12709:                     foreach my $balancer (sort(@{$changes{'add'}})) {
12710:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
12711:                         $toupdate{$balancer} = 1;
12712:                     }
12713:                 }
12714:                 if (ref($changes{'curr'}) eq 'HASH') {
12715:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
12716:                         $toupdate{$balancer} = 1;
12717:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
12718:                             if ($changes{'curr'}{$balancer}{'targets'}) {
12719:                                 my %offloadstr;
12720:                                 foreach my $sparetype (@sparestypes) {
12721:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
12722:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
12723:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
12724:                                         }
12725:                                     }
12726:                                 }
12727:                                 if (keys(%offloadstr) == 0) {
12728:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
12729:                                 } else {
12730:                                     my $showoffload;
12731:                                     foreach my $sparetype (@sparestypes) {
12732:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
12733:                                         if (defined($offloadstr{$sparetype})) {
12734:                                             $showoffload .= $offloadstr{$sparetype};
12735:                                         } else {
12736:                                             $showoffload .= &mt('None');
12737:                                         }
12738:                                         $showoffload .= ('&nbsp;'x3);
12739:                                     }
12740:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
12741:                                 }
12742:                             }
12743:                         }
12744:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
12745:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
12746:                                 foreach my $type (@{$alltypes}) {
12747:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
12748:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
12749:                                         my $balancetext;
12750:                                         if ($rule eq '') {
12751:                                             $balancetext =  $ruletitles{'default'};
12752:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
12753:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
12754:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
12755:                                                 foreach my $sparetype (@sparestypes) {
12756:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
12757:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
12758:                                                     }
12759:                                                 }
12760:                                                 foreach my $item (@{$alltypes}) {
12761:                                                     next if ($item =~  /^_LC_ipchange/);
12762:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
12763:                                                     if ($hasrule eq 'homeserver') {
12764:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
12765:                                                     } else {
12766:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
12767:                                                             if ($servers{$hasrule}) {
12768:                                                                 $toupdate{$hasrule} = 1;
12769:                                                             }
12770:                                                         }
12771:                                                     }
12772:                                                 }
12773:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
12774:                                                     $balancetext =  $ruletitles{$rule};
12775:                                                 } else {
12776:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
12777:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
12778:                                                     if ($receiver) {
12779:                                                         $toupdate{$receiver};
12780:                                                     }
12781:                                                 }
12782:                                             } else {
12783:                                                 $balancetext =  $ruletitles{$rule};
12784:                                             }
12785:                                         } else {
12786:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
12787:                                         }
12788:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
12789:                                     }
12790:                                 }
12791:                             }
12792:                         }
12793:                         if (keys(%toupdate)) {
12794:                             my %thismachine;
12795:                             my $updatedhere;
12796:                             my $cachetime = 60*60*24;
12797:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
12798:                             foreach my $lonhost (keys(%toupdate)) {
12799:                                 if ($thismachine{$lonhost}) {
12800:                                     unless ($updatedhere) {
12801:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
12802:                                                                       $defaultshash{'loadbalancing'},
12803:                                                                       $cachetime);
12804:                                         $updatedhere = 1;
12805:                                     }
12806:                                 } else {
12807:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
12808:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
12809:                                 }
12810:                             }
12811:                         }
12812:                     }
12813:                 }
12814:                 if ($resulttext ne '') {
12815:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
12816:                 } else {
12817:                     $resulttext = $nochgmsg;
12818:                 }
12819:             } else {
12820:                 $resulttext = $nochgmsg;
12821:             }
12822:         } else {
12823:             $resulttext = '<span class="LC_error">'.
12824:                           &mt('An error occurred: [_1]',$putresult).'</span>';
12825:         }
12826:     } else {
12827:         $resulttext = $nochgmsg;
12828:     }
12829:     return $resulttext;
12830: }
12831: 
12832: sub recurse_check {
12833:     my ($chkcats,$categories,$depth,$name) = @_;
12834:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
12835:         my $chg = 0;
12836:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
12837:             my $category = $chkcats->[$depth]{$name}[$j];
12838:             my $item;
12839:             if ($category eq '') {
12840:                 $chg ++;
12841:             } else {
12842:                 my $deeper = $depth + 1;
12843:                 $item = &escape($category).':'.&escape($name).':'.$depth;
12844:                 if ($chg) {
12845:                     $categories->{$item} -= $chg;
12846:                 }
12847:                 &recurse_check($chkcats,$categories,$deeper,$category);
12848:                 $deeper --;
12849:             }
12850:         }
12851:     }
12852:     return;
12853: }
12854: 
12855: sub recurse_cat_deletes {
12856:     my ($item,$coursecategories,$deletions) = @_;
12857:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
12858:     my $subdepth = $depth + 1;
12859:     if (ref($coursecategories) eq 'HASH') {
12860:         foreach my $subitem (keys(%{$coursecategories})) {
12861:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
12862:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
12863:                 delete($coursecategories->{$subitem});
12864:                 $deletions->{$subitem} = 1;
12865:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
12866:             }
12867:         }
12868:     }
12869:     return;
12870: }
12871: 
12872: sub active_dc_picker {
12873:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
12874:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
12875:     my @domcoord = keys(%domcoords);
12876:     if (keys(%currhash)) {
12877:         foreach my $dc (keys(%currhash)) {
12878:             unless (exists($domcoords{$dc})) {
12879:                 push(@domcoord,$dc);
12880:             }
12881:         }
12882:     }
12883:     @domcoord = sort(@domcoord);
12884:     my $numdcs = scalar(@domcoord);
12885:     my $rows = 0;
12886:     my $table;
12887:     if ($numdcs > 1) {
12888:         $table = '<table>';
12889:         for (my $i=0; $i<@domcoord; $i++) {
12890:             my $rem = $i%($numinrow);
12891:             if ($rem == 0) {
12892:                 if ($i > 0) {
12893:                     $table .= '</tr>';
12894:                 }
12895:                 $table .= '<tr>';
12896:                 $rows ++;
12897:             }
12898:             my $check = '';
12899:             if ($inputtype eq 'radio') {
12900:                 if (keys(%currhash) == 0) {
12901:                     if (!$i) {
12902:                         $check = ' checked="checked"';
12903:                     }
12904:                 } elsif (exists($currhash{$domcoord[$i]})) {
12905:                     $check = ' checked="checked"';
12906:                 }
12907:             } else {
12908:                 if (exists($currhash{$domcoord[$i]})) {
12909:                     $check = ' checked="checked"';
12910:                 }
12911:             }
12912:             if ($i == @domcoord - 1) {
12913:                 my $colsleft = $numinrow - $rem;
12914:                 if ($colsleft > 1) {
12915:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
12916:                 } else {
12917:                     $table .= '<td class="LC_left_item">';
12918:                 }
12919:             } else {
12920:                 $table .= '<td class="LC_left_item">';
12921:             }
12922:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
12923:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
12924:             $table .= '<span class="LC_nobreak"><label>'.
12925:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
12926:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
12927:             if ($user ne $dcname.':'.$dcdom) {
12928:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
12929:             }
12930:             $table .= '</label></span></td>';
12931:         }
12932:         $table .= '</tr></table>';
12933:     } elsif ($numdcs == 1) {
12934:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
12935:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
12936:         if ($inputtype eq 'radio') {
12937:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
12938:             if ($user ne $dcname.':'.$dcdom) {
12939:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
12940:             }
12941:         } else {
12942:             my $check;
12943:             if (exists($currhash{$domcoord[0]})) {
12944:                 $check = ' checked="checked"';
12945:             }
12946:             $table = '<span class="LC_nobreak"><label>'.
12947:                      '<input type="checkbox" name="'.$name.'" '.
12948:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
12949:             if ($user ne $dcname.':'.$dcdom) {
12950:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
12951:             }
12952:             $table .= '</label></span>';
12953:             $rows ++;
12954:         }
12955:     }
12956:     return ($numdcs,$table,$rows);
12957: }
12958: 
12959: sub usersession_titles {
12960:     return &Apache::lonlocal::texthash(
12961:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
12962:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
12963:                spares => 'Servers offloaded to, when busy',
12964:                version => 'LON-CAPA version requirement',
12965:                excludedomain => 'Allow all, but exclude specific domains',
12966:                includedomain => 'Deny all, but include specific domains',
12967:                primary => 'Primary (checked first)',
12968:                default => 'Default',
12969:            );
12970: }
12971: 
12972: sub id_for_thisdom {
12973:     my (%servers) = @_;
12974:     my %altids;
12975:     foreach my $server (keys(%servers)) {
12976:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
12977:         if ($serverhome ne $server) {
12978:             $altids{$serverhome} = $server;
12979:         }
12980:     }
12981:     return %altids;
12982: }
12983: 
12984: sub count_servers {
12985:     my ($currbalancer,%servers) = @_;
12986:     my (@spares,$numspares);
12987:     foreach my $lonhost (sort(keys(%servers))) {
12988:         next if ($currbalancer eq $lonhost);
12989:         push(@spares,$lonhost);
12990:     }
12991:     if ($currbalancer) {
12992:         $numspares = scalar(@spares);
12993:     } else {
12994:         $numspares = scalar(@spares) - 1;
12995:     }
12996:     return ($numspares,@spares);
12997: }
12998: 
12999: sub lonbalance_targets_js {
13000:     my ($dom,$types,$servers,$settings) = @_;
13001:     my $select = &mt('Select');
13002:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
13003:     if (ref($servers) eq 'HASH') {
13004:         $alltargets = join("','",sort(keys(%{$servers})));
13005:         my @homedoms;
13006:         foreach my $server (sort(keys(%{$servers}))) {
13007:             if (&Apache::lonnet::host_domain($server) eq $dom) {
13008:                 push(@homedoms,'1');
13009:             } else {
13010:                 push(@homedoms,'0');
13011:             }
13012:         }
13013:         $allishome = join("','",@homedoms);
13014:     }
13015:     if (ref($types) eq 'ARRAY') {
13016:         if (@{$types} > 0) {
13017:             @alltypes = @{$types};
13018:         }
13019:     }
13020:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
13021:     $allinsttypes = join("','",@alltypes);
13022:     my (%currbalancer,%currtargets,%currrules,%existing);
13023:     if (ref($settings) eq 'HASH') {
13024:         %existing = %{$settings};
13025:     }
13026:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
13027:                               \%currtargets,\%currrules);
13028:     my $balancers = join("','",sort(keys(%currbalancer)));
13029:     return <<"END";
13030: 
13031: <script type="text/javascript">
13032: // <![CDATA[
13033: 
13034: currBalancers = new Array('$balancers');
13035: 
13036: function toggleTargets(balnum) {
13037:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
13038:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
13039:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
13040:     var prevbalancer = prevhostitem.value;
13041:     var baltotal = document.getElementById('loadbalancing_total').value;
13042:     prevhostitem.value = balancer;
13043:     if (prevbalancer != '') {
13044:         var prevIdx = currBalancers.indexOf(prevbalancer);
13045:         if (prevIdx != -1) {
13046:             currBalancers.splice(prevIdx,1);
13047:         }
13048:     }
13049:     if (balancer == '') {
13050:         hideSpares(balnum);
13051:     } else {
13052:         var currIdx = currBalancers.indexOf(balancer);
13053:         if (currIdx == -1) {
13054:             currBalancers.push(balancer);
13055:         }
13056:         var homedoms = new Array('$allishome');
13057:         var ishomedom = homedoms[lonhostitem.selectedIndex];
13058:         showSpares(balancer,ishomedom,balnum);
13059:     }
13060:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
13061:     return;
13062: }
13063: 
13064: function showSpares(balancer,ishomedom,balnum) {
13065:     var alltargets = new Array('$alltargets');
13066:     var insttypes = new Array('$allinsttypes');
13067:     var offloadtypes = new Array('primary','default');
13068: 
13069:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
13070:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
13071:  
13072:     for (var i=0; i<offloadtypes.length; i++) {
13073:         var count = 0;
13074:         for (var j=0; j<alltargets.length; j++) {
13075:             if (alltargets[j] != balancer) {
13076:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
13077:                 item.value = alltargets[j];
13078:                 item.style.textAlign='left';
13079:                 item.style.textFace='normal';
13080:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
13081:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
13082:                     item.disabled = '';
13083:                 } else {
13084:                     item.disabled = 'disabled';
13085:                     item.checked = false;
13086:                 }
13087:                 count ++;
13088:             }
13089:         }
13090:     }
13091:     for (var k=0; k<insttypes.length; k++) {
13092:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
13093:             if (ishomedom == 1) {
13094:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
13095:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
13096:             } else {
13097:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
13098:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
13099:             }
13100:         } else {
13101:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
13102:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
13103:         }
13104:         if ((insttypes[k] != '_LC_external') && 
13105:             ((insttypes[k] != '_LC_internetdom') ||
13106:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
13107:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
13108:             item.options.length = 0;
13109:             item.options[0] = new Option("","",true,true);
13110:             var idx = 0;
13111:             for (var m=0; m<alltargets.length; m++) {
13112:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
13113:                     idx ++;
13114:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
13115:                 }
13116:             }
13117:         }
13118:     }
13119:     return;
13120: }
13121: 
13122: function hideSpares(balnum) {
13123:     var alltargets = new Array('$alltargets');
13124:     var insttypes = new Array('$allinsttypes');
13125:     var offloadtypes = new Array('primary','default');
13126: 
13127:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
13128:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
13129: 
13130:     var total = alltargets.length - 1;
13131:     for (var i=0; i<offloadtypes; i++) {
13132:         for (var j=0; j<total; j++) {
13133:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
13134:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
13135:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
13136:         }
13137:     }
13138:     for (var k=0; k<insttypes.length; k++) {
13139:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
13140:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
13141:         if (insttypes[k] != '_LC_external') {
13142:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
13143:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
13144:         }
13145:     }
13146:     return;
13147: }
13148: 
13149: function checkOffloads(item,balnum,type) {
13150:     var alltargets = new Array('$alltargets');
13151:     var offloadtypes = new Array('primary','default');
13152:     if (item.checked) {
13153:         var total = alltargets.length - 1;
13154:         var other;
13155:         if (type == offloadtypes[0]) {
13156:             other = offloadtypes[1];
13157:         } else {
13158:             other = offloadtypes[0];
13159:         }
13160:         for (var i=0; i<total; i++) {
13161:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
13162:             if (server == item.value) {
13163:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
13164:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
13165:                 }
13166:             }
13167:         }
13168:     }
13169:     return;
13170: }
13171: 
13172: function singleServerToggle(balnum,type) {
13173:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
13174:     if (offloadtoSelIdx == 0) {
13175:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
13176:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
13177: 
13178:     } else {
13179:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
13180:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
13181:     }
13182:     return;
13183: }
13184: 
13185: function balanceruleChange(formname,balnum,type) {
13186:     if (type == '_LC_external') {
13187:         return;
13188:     }
13189:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
13190:     for (var i=0; i<typesRules.length; i++) {
13191:         if (formname.elements[typesRules[i]].checked) {
13192:             if (formname.elements[typesRules[i]].value != 'specific') {
13193:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
13194:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
13195:             } else {
13196:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
13197:             }
13198:         }
13199:     }
13200:     return;
13201: }
13202: 
13203: function balancerDeleteChange(balnum) {
13204:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
13205:     var baltotal = document.getElementById('loadbalancing_total').value;
13206:     var addtarget;
13207:     var removetarget;
13208:     var action = 'delete';
13209:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
13210:         var lonhost = hostitem.value;
13211:         var currIdx = currBalancers.indexOf(lonhost);
13212:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
13213:             if (currIdx != -1) {
13214:                 currBalancers.splice(currIdx,1);
13215:             }
13216:             addtarget = lonhost;
13217:         } else {
13218:             if (currIdx == -1) {
13219:                 currBalancers.push(lonhost);
13220:             }
13221:             removetarget = lonhost;
13222:             action = 'undelete';
13223:         }
13224:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
13225:     }
13226:     return;
13227: }
13228: 
13229: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
13230:     if (baltotal > 1) {
13231:         var offloadtypes = new Array('primary','default');
13232:         var alltargets = new Array('$alltargets');
13233:         var insttypes = new Array('$allinsttypes');
13234:         for (var i=0; i<baltotal; i++) {
13235:             if (i != balnum) {
13236:                 for (var j=0; j<offloadtypes.length; j++) {
13237:                     var total = alltargets.length - 1;
13238:                     for (var k=0; k<total; k++) {
13239:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
13240:                         var server = serveritem.value;
13241:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
13242:                             if (server == addtarget) {
13243:                                 serveritem.disabled = '';
13244:                             }
13245:                         }
13246:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
13247:                             if (server == removetarget) {
13248:                                 serveritem.disabled = 'disabled';
13249:                                 serveritem.checked = false;
13250:                             }
13251:                         }
13252:                     }
13253:                 }
13254:                 for (var j=0; j<insttypes.length; j++) {
13255:                     if (insttypes[j] != '_LC_external') {
13256:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
13257:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
13258:                             var currSel = singleserver.selectedIndex;
13259:                             var currVal = singleserver.options[currSel].value;
13260:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
13261:                                 var numoptions = singleserver.options.length;
13262:                                 var needsnew = 1;
13263:                                 for (var k=0; k<numoptions; k++) {
13264:                                     if (singleserver.options[k] == addtarget) {
13265:                                         needsnew = 0;
13266:                                         break;
13267:                                     }
13268:                                 }
13269:                                 if (needsnew == 1) {
13270:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
13271:                                 }
13272:                             }
13273:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
13274:                                 singleserver.options.length = 0;
13275:                                 if ((currVal) && (currVal != removetarget)) {
13276:                                     singleserver.options[0] = new Option("","",false,false);
13277:                                 } else {
13278:                                     singleserver.options[0] = new Option("","",true,true);
13279:                                 }
13280:                                 var idx = 0;
13281:                                 for (var m=0; m<alltargets.length; m++) {
13282:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
13283:                                         idx ++;
13284:                                         if (currVal == alltargets[m]) {
13285:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
13286:                                         } else {
13287:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
13288:                                         }
13289:                                     }
13290:                                 }
13291:                             }
13292:                         }
13293:                     }
13294:                 }
13295:             }
13296:         }
13297:     }
13298:     return;
13299: }
13300: 
13301: // ]]>
13302: </script>
13303: 
13304: END
13305: }
13306: 
13307: sub new_spares_js {
13308:     my @sparestypes = ('primary','default');
13309:     my $types = join("','",@sparestypes);
13310:     my $select = &mt('Select');
13311:     return <<"END";
13312: 
13313: <script type="text/javascript">
13314: // <![CDATA[
13315: 
13316: function updateNewSpares(formname,lonhost) {
13317:     var types = new Array('$types');
13318:     var include = new Array();
13319:     var exclude = new Array();
13320:     for (var i=0; i<types.length; i++) {
13321:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
13322:         for (var j=0; j<spareboxes.length; j++) {
13323:             if (formname.elements[spareboxes[j]].checked) {
13324:                 exclude.push(formname.elements[spareboxes[j]].value);
13325:             } else {
13326:                 include.push(formname.elements[spareboxes[j]].value);
13327:             }
13328:         }
13329:     }
13330:     for (var i=0; i<types.length; i++) {
13331:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
13332:         var selIdx = newSpare.selectedIndex;
13333:         var currnew = newSpare.options[selIdx].value;
13334:         var okSpares = new Array();
13335:         for (var j=0; j<newSpare.options.length; j++) {
13336:             var possible = newSpare.options[j].value;
13337:             if (possible != '') {
13338:                 if (exclude.indexOf(possible) == -1) {
13339:                     okSpares.push(possible);
13340:                 } else {
13341:                     if (currnew == possible) {
13342:                         selIdx = 0;
13343:                     }
13344:                 }
13345:             }
13346:         }
13347:         for (var k=0; k<include.length; k++) {
13348:             if (okSpares.indexOf(include[k]) == -1) {
13349:                 okSpares.push(include[k]);
13350:             }
13351:         }
13352:         okSpares.sort();
13353:         newSpare.options.length = 0;
13354:         if (selIdx == 0) {
13355:             newSpare.options[0] = new Option("$select","",true,true);
13356:         } else {
13357:             newSpare.options[0] = new Option("$select","",false,false);
13358:         }
13359:         for (var m=0; m<okSpares.length; m++) {
13360:             var idx = m+1;
13361:             var selThis = 0;
13362:             if (selIdx != 0) {
13363:                 if (okSpares[m] == currnew) {
13364:                     selThis = 1;
13365:                 }
13366:             }
13367:             if (selThis == 1) {
13368:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
13369:             } else {
13370:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
13371:             }
13372:         }
13373:     }
13374:     return;
13375: }
13376: 
13377: function checkNewSpares(lonhost,type) {
13378:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
13379:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
13380:     if (chosen != '') { 
13381:         var othertype;
13382:         var othernewSpare;
13383:         if (type == 'primary') {
13384:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
13385:         }
13386:         if (type == 'default') {
13387:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
13388:         }
13389:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
13390:             othernewSpare.selectedIndex = 0;
13391:         }
13392:     }
13393:     return;
13394: }
13395: 
13396: // ]]>
13397: </script>
13398: 
13399: END
13400: 
13401: }
13402: 
13403: sub common_domprefs_js {
13404:     return <<"END";
13405: 
13406: <script type="text/javascript">
13407: // <![CDATA[
13408: 
13409: function getIndicesByName(formname,item) {
13410:     var group = new Array();
13411:     for (var i=0;i<formname.elements.length;i++) {
13412:         if (formname.elements[i].name == item) {
13413:             group.push(formname.elements[i].id);
13414:         }
13415:     }
13416:     return group;
13417: }
13418: 
13419: // ]]>
13420: </script>
13421: 
13422: END
13423: 
13424: }
13425: 
13426: sub recaptcha_js {
13427:     my %lt = &captcha_phrases();
13428:     return <<"END";
13429: 
13430: <script type="text/javascript">
13431: // <![CDATA[
13432: 
13433: function updateCaptcha(caller,context) {
13434:     var privitem;
13435:     var pubitem;
13436:     var privtext;
13437:     var pubtext;
13438:     var versionitem;
13439:     var versiontext;
13440:     if (document.getElementById(context+'_recaptchapub')) {
13441:         pubitem = document.getElementById(context+'_recaptchapub');
13442:     } else {
13443:         return;
13444:     }
13445:     if (document.getElementById(context+'_recaptchapriv')) {
13446:         privitem = document.getElementById(context+'_recaptchapriv');
13447:     } else {
13448:         return;
13449:     }
13450:     if (document.getElementById(context+'_recaptchapubtxt')) {
13451:         pubtext = document.getElementById(context+'_recaptchapubtxt');
13452:     } else {
13453:         return;
13454:     }
13455:     if (document.getElementById(context+'_recaptchaprivtxt')) {
13456:         privtext = document.getElementById(context+'_recaptchaprivtxt');
13457:     } else {
13458:         return;
13459:     }
13460:     if (document.getElementById(context+'_recaptchaversion')) {
13461:         versionitem = document.getElementById(context+'_recaptchaversion');
13462:     } else {
13463:         return;
13464:     }
13465:     if (document.getElementById(context+'_recaptchavertxt')) {
13466:         versiontext = document.getElementById(context+'_recaptchavertxt');
13467:     } else {
13468:         return;
13469:     }
13470:     if (caller.checked) {
13471:         if (caller.value == 'recaptcha') {
13472:             pubitem.type = 'text';
13473:             privitem.type = 'text';
13474:             pubitem.size = '40';
13475:             privitem.size = '40';
13476:             pubtext.innerHTML = "$lt{'pub'}";
13477:             privtext.innerHTML = "$lt{'priv'}";
13478:             versionitem.type = 'text';
13479:             versionitem.size = '3';
13480:             versiontext.innerHTML = "$lt{'ver'}";
13481:         } else {
13482:             pubitem.type = 'hidden';
13483:             privitem.type = 'hidden';
13484:             versionitem.type = 'hidden';
13485:             pubtext.innerHTML = '';
13486:             privtext.innerHTML = '';
13487:             versiontext.innerHTML = '';
13488:         }
13489:     }
13490:     return;
13491: }
13492: 
13493: // ]]>
13494: </script>
13495: 
13496: END
13497: 
13498: }
13499: 
13500: sub toggle_display_js {
13501:     return <<"END";
13502: 
13503: <script type="text/javascript">
13504: // <![CDATA[
13505: 
13506: function toggleDisplay(domForm,caller) {
13507:     if (document.getElementById(caller)) {
13508:         var divitem = document.getElementById(caller);
13509:         var optionsElement = domForm.coursecredits;
13510:         var checkval = 1;
13511:         var dispval = 'block';
13512:         if (caller == 'emailoptions') {
13513:             optionsElement = domForm.cancreate_email; 
13514:         }
13515:         if (caller == 'studentsubmission') {
13516:             optionsElement = domForm.postsubmit;
13517:         }
13518:         if (caller == 'cloneinstcode') {
13519:             optionsElement = domForm.canclone;
13520:             checkval = 'instcode';
13521:         }
13522:         if (optionsElement.length) {
13523:             var currval;
13524:             for (var i=0; i<optionsElement.length; i++) {
13525:                 if (optionsElement[i].checked) {
13526:                    currval = optionsElement[i].value;
13527:                 }
13528:             }
13529:             if (currval == checkval) {
13530:                 divitem.style.display = dispval;
13531:             } else {
13532:                 divitem.style.display = 'none';
13533:             }
13534:         }
13535:     }
13536:     return;
13537: }
13538: 
13539: // ]]>
13540: </script>
13541: 
13542: END
13543: 
13544: }
13545: 
13546: sub captcha_phrases {
13547:     return &Apache::lonlocal::texthash (
13548:                  priv => 'Private key',
13549:                  pub  => 'Public key',
13550:                  original  => 'original (CAPTCHA)',
13551:                  recaptcha => 'successor (ReCAPTCHA)',
13552:                  notused   => 'unused',
13553:                  ver => 'ReCAPTCHA version (1 or 2)',
13554:     );
13555: }
13556: 
13557: sub devalidate_remote_domconfs {
13558:     my ($dom,$cachekeys) = @_;
13559:     return unless (ref($cachekeys) eq 'HASH');
13560:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
13561:     my %thismachine;
13562:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
13563:     my @posscached = ('domainconfig','domdefaults');
13564:     if (keys(%servers)) {
13565:         foreach my $server (keys(%servers)) {
13566:             next if ($thismachine{$server});
13567:             my @cached;
13568:             foreach my $name (@posscached) {
13569:                 if ($cachekeys->{$name}) {
13570:                     push(@cached,&escape($name).':'.&escape($dom));
13571:                 }
13572:             }
13573:             if (@cached) {
13574:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
13575:             }
13576:         }
13577:     }
13578:     return;
13579: }
13580: 
13581: 1;

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