File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.102.2.3: download - view: text, annotated - select for diffs
Wed Feb 12 23:06:16 2020 UTC (4 years, 4 months ago) by raeburn
Branches: version_2_11_3_msu
- For 2.11.3 (modified)
  Include changes in 1.160.6.104

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.160.6.102.2.3 2020/02/12 23:06:16 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',
  220:                 'passwords','ltitools'],$dom);
  221:     if (ref($domconfig{'ltitools'}) eq 'HASH') {
  222:         my %encconfig =
  223:             &Apache::lonnet::get_dom('encconfig',['ltitools'],$dom);
  224:         if (ref($encconfig{'ltitools'}) eq 'HASH') {
  225:             foreach my $id (keys(%{$domconfig{'ltitools'}})) {
  226:                 if (ref($domconfig{'ltitools'}{$id}) eq 'HASH') {
  227:                     foreach my $item ('key','secret') {
  228:                         $domconfig{'ltitools'}{$id}{$item} = $encconfig{'ltitools'}{$id}{$item};
  229:                     }
  230:                 }
  231:             }
  232:         }
  233:     }
  234:     my @prefs_order = ('rolecolors','login','defaults','passwords','quotas','autoenroll',
  235:                        'autoupdate','autocreate','directorysrch','contacts',
  236:                        'usercreation','selfcreation','usermodification','scantron',
  237:                        'requestcourses','requestauthor','coursecategories',
  238:                        'serverstatuses','helpsettings','coursedefaults',
  239:                        'ltitools','selfenrollment','usersessions');
  240:     my %existing;
  241:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  242:         %existing = %{$domconfig{'loadbalancing'}};
  243:     }
  244:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  245:         push(@prefs_order,'loadbalancing');
  246:     }
  247:     my %prefs = (
  248:         'rolecolors' =>
  249:                    { text => 'Default color schemes',
  250:                      help => 'Domain_Configuration_Color_Schemes',
  251:                      header => [{col1 => 'Student Settings',
  252:                                  col2 => '',},
  253:                                 {col1 => 'Coordinator Settings',
  254:                                  col2 => '',},
  255:                                 {col1 => 'Author Settings',
  256:                                  col2 => '',},
  257:                                 {col1 => 'Administrator Settings',
  258:                                  col2 => '',}],
  259:                       print => \&print_rolecolors,
  260:                       modify => \&modify_rolecolors,
  261:                     },
  262:         'login' =>
  263:                     { text => 'Log-in page options',
  264:                       help => 'Domain_Configuration_Login_Page',
  265:                       header => [{col1 => 'Log-in Page Items',
  266:                                   col2 => '',},
  267:                                  {col1 => 'Log-in Help',
  268:                                   col2 => 'Value'},
  269:                                  {col1 => 'Custom HTML in document head',
  270:                                   col2 => 'Value'}],
  271:                       print => \&print_login,
  272:                       modify => \&modify_login,
  273:                     },
  274:         'defaults' => 
  275:                     { text => 'Default authentication/language/timezone/portal/types',
  276:                       help => 'Domain_Configuration_LangTZAuth',
  277:                       header => [{col1 => 'Setting',
  278:                                   col2 => 'Value'},
  279:                                  {col1 => 'Institutional user types',
  280:                                   col2 => 'Name displayed'}],
  281:                       print => \&print_defaults,
  282:                       modify => \&modify_defaults,
  283:                     },
  284:         'passwords' =>
  285:                     { text => 'Passwords (Internal authentication)',
  286:                       help => 'Domain_Configuration_Passwords',
  287:                       header => [{col1 => 'Resetting Forgotten Password',
  288:                                   col2 => 'Settings'},
  289:                                  {col1 => 'Encryption of Stored Passwords (Internal Auth)',
  290:                                   col2 => 'Settings'},
  291:                                  {col1 => 'Rules for LON-CAPA Passwords',
  292:                                   col2 => 'Settings'},
  293:                                  {col1 => 'Course Owner Changing Student Passwords',
  294:                                   col2 => 'Settings'}],
  295:                       print => \&print_passwords,
  296:                       modify => \&modify_passwords,
  297:                     },
  298:         'quotas' => 
  299:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  300:                       help => 'Domain_Configuration_Quotas',
  301:                       header => [{col1 => 'User affiliation',
  302:                                   col2 => 'Available tools',
  303:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  304:                       print => \&print_quotas,
  305:                       modify => \&modify_quotas,
  306:                     },
  307:         'autoenroll' =>
  308:                    { text => 'Auto-enrollment settings',
  309:                      help => 'Domain_Configuration_Auto_Enrollment',
  310:                      header => [{col1 => 'Configuration setting',
  311:                                  col2 => 'Value(s)'}],
  312:                      print => \&print_autoenroll,
  313:                      modify => \&modify_autoenroll,
  314:                    },
  315:         'autoupdate' => 
  316:                    { text => 'Auto-update settings',
  317:                      help => 'Domain_Configuration_Auto_Updates',
  318:                      header => [{col1 => 'Setting',
  319:                                  col2 => 'Value',},
  320:                                 {col1 => 'Setting',
  321:                                  col2 => 'Affiliation'},
  322:                                 {col1 => 'User population',
  323:                                  col2 => 'Updatable user data'}],
  324:                      print => \&print_autoupdate,
  325:                      modify => \&modify_autoupdate,
  326:                   },
  327:         'autocreate' => 
  328:                   { text => 'Auto-course creation settings',
  329:                      help => 'Domain_Configuration_Auto_Creation',
  330:                      header => [{col1 => 'Configuration Setting',
  331:                                  col2 => 'Value',}],
  332:                      print => \&print_autocreate,
  333:                      modify => \&modify_autocreate,
  334:                   },
  335:         'directorysrch' => 
  336:                   { text => 'Directory searches',
  337:                     help => 'Domain_Configuration_InstDirectory_Search',
  338:                     header => [{col1 => 'Institutional Directory Setting',
  339:                                 col2 => 'Value',},
  340:                                {col1 => 'LON-CAPA Directory Setting',
  341:                                 col2 => 'Value',}],
  342:                     print => \&print_directorysrch,
  343:                     modify => \&modify_directorysrch,
  344:                   },
  345:         'contacts' =>
  346:                   { text => 'E-mail addresses and helpform',
  347:                     help => 'Domain_Configuration_Contact_Info',
  348:                     header => [{col1 => 'Default e-mail addresses',
  349:                                 col2 => 'Value',},
  350:                                {col1 => 'Recipient(s) for notifications',
  351:                                 col2 => 'Value',},
  352:                                {col1 => 'Ask helpdesk form settings',
  353:                                 col2 => 'Value',},],
  354:                     print => \&print_contacts,
  355:                     modify => \&modify_contacts,
  356:                   },
  357:         'usercreation' => 
  358:                   { text => 'User creation',
  359:                     help => 'Domain_Configuration_User_Creation',
  360:                     header => [{col1 => 'Format rule type',
  361:                                 col2 => 'Format rules in force'},
  362:                                {col1 => 'User account creation',
  363:                                 col2 => 'Usernames which may be created',},
  364:                                {col1 => 'Context',
  365:                                 col2 => 'Assignable authentication types'}],
  366:                     print => \&print_usercreation,
  367:                     modify => \&modify_usercreation,
  368:                   },
  369:         'selfcreation' => 
  370:                   { text => 'Users self-creating accounts',
  371:                     help => 'Domain_Configuration_Self_Creation', 
  372:                     header => [{col1 => 'Self-creation with institutional username',
  373:                                 col2 => 'Enabled?'},
  374:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  375:                                 col2 => 'Information user can enter'},
  376:                                {col1 => 'Self-creation with e-mail verification',
  377:                                 col2 => 'Settings'}],
  378:                     print => \&print_selfcreation,
  379:                     modify => \&modify_selfcreation,
  380:                   },
  381:         'usermodification' =>
  382:                   { text => 'User modification',
  383:                     help => 'Domain_Configuration_User_Modification',
  384:                     header => [{col1 => 'Target user has role',
  385:                                 col2 => 'User information updatable in author context'},
  386:                                {col1 => 'Target user has role',
  387:                                 col2 => 'User information updatable in course context'}],
  388:                     print => \&print_usermodification,
  389:                     modify => \&modify_usermodification,
  390:                   },
  391:         'scantron' =>
  392:                   { text => 'Bubblesheet format',
  393:                     help => 'Domain_Configuration_Scantron_Format',
  394:                     header => [ {col1 => 'Bubblesheet format file',
  395:                                  col2 => ''},
  396:                                 {col1 => 'Bubblesheet data upload formats',
  397:                                  col2 => 'Settings'}],
  398:                     print => \&print_scantron,
  399:                     modify => \&modify_scantron,
  400:                   },
  401:         'requestcourses' => 
  402:                  {text => 'Request creation of courses',
  403:                   help => 'Domain_Configuration_Request_Courses',
  404:                   header => [{col1 => 'User affiliation',
  405:                               col2 => 'Availability/Processing of requests',},
  406:                              {col1 => 'Setting',
  407:                               col2 => 'Value'},
  408:                              {col1 => 'Available textbooks',
  409:                               col2 => ''},
  410:                              {col1 => 'Available templates',
  411:                               col2 => ''},
  412:                              {col1 => 'Validation (not official courses)',
  413:                               col2 => 'Value'},],
  414:                   print => \&print_quotas,
  415:                   modify => \&modify_quotas,
  416:                  },
  417:         'requestauthor' =>
  418:                  {text => 'Request Authoring Space',
  419:                   help => 'Domain_Configuration_Request_Author',
  420:                   header => [{col1 => 'User affiliation',
  421:                               col2 => 'Availability/Processing of requests',},
  422:                              {col1 => 'Setting',
  423:                               col2 => 'Value'}],
  424:                   print => \&print_quotas,
  425:                   modify => \&modify_quotas,
  426:                  },
  427:         'coursecategories' =>
  428:                   { text => 'Cataloging of courses/communities',
  429:                     help => 'Domain_Configuration_Cataloging_Courses',
  430:                     header => [{col1 => 'Catalog type/availability',
  431:                                 col2 => '',},
  432:                                {col1 => 'Category settings for standard catalog',
  433:                                 col2 => '',},
  434:                                {col1 => 'Categories',
  435:                                 col2 => '',
  436:                                }],
  437:                     print => \&print_coursecategories,
  438:                     modify => \&modify_coursecategories,
  439:                   },
  440:         'serverstatuses' =>
  441:                  {text   => 'Access to server status pages',
  442:                   help   => 'Domain_Configuration_Server_Status',
  443:                   header => [{col1 => 'Status Page',
  444:                               col2 => 'Other named users',
  445:                               col3 => 'Specific IPs',
  446:                             }],
  447:                   print => \&print_serverstatuses,
  448:                   modify => \&modify_serverstatuses,
  449:                  },
  450:         'helpsettings' =>
  451:                  {text   => 'Support settings',
  452:                   help   => 'Domain_Configuration_Help_Settings',
  453:                   header => [{col1 => 'Help Page Settings (logged-in users)',
  454:                               col2 => 'Value'},
  455:                              {col1 => 'Helpdesk Roles',
  456:                               col2 => 'Settings'},],
  457:                   print  => \&print_helpsettings,
  458:                   modify => \&modify_helpsettings,
  459:                  },
  460:         'coursedefaults' => 
  461:                  {text => 'Course/Community defaults',
  462:                   help => 'Domain_Configuration_Course_Defaults',
  463:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  464:                               col2 => 'Value',},
  465:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  466:                               col2 => 'Value',},],
  467:                   print => \&print_coursedefaults,
  468:                   modify => \&modify_coursedefaults,
  469:                  },
  470:         'selfenrollment' => 
  471:                  {text   => 'Self-enrollment in Course/Community',
  472:                   help   => 'Domain_Configuration_Selfenrollment',
  473:                   header => [{col1 => 'Configuration Rights',
  474:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  475:                              {col1 => 'Defaults',
  476:                               col2 => 'Value'},
  477:                              {col1 => 'Self-enrollment validation (optional)',
  478:                               col2 => 'Value'},],
  479:                   print => \&print_selfenrollment,
  480:                   modify => \&modify_selfenrollment,
  481:                  },
  482:         'usersessions' =>
  483:                  {text  => 'User session hosting/offloading',
  484:                   help  => 'Domain_Configuration_User_Sessions',
  485:                   header => [{col1 => 'Domain server',
  486:                               col2 => 'Servers to offload sessions to when busy'},
  487:                              {col1 => 'Hosting of users from other domains',
  488:                               col2 => 'Rules'},
  489:                              {col1 => "Hosting domain's own users elsewhere",
  490:                               col2 => 'Rules'}],
  491:                   print => \&print_usersessions,
  492:                   modify => \&modify_usersessions,
  493:                  },
  494:         'loadbalancing' =>
  495:                  {text  => 'Dedicated Load Balancer(s)',
  496:                   help  => 'Domain_Configuration_Load_Balancing',
  497:                   header => [{col1 => 'Balancers',
  498:                               col2 => 'Default destinations',
  499:                               col3 => 'User affiliation',
  500:                               col4 => 'Overrides'},
  501:                             ],
  502:                   print => \&print_loadbalancing,
  503:                   modify => \&modify_loadbalancing,
  504:                  },
  505:         'ltitools' =>
  506:                  {text => 'External Tools (LTI)',
  507:                   help => 'Domain_Configuration_LTI_Tools',
  508:                   header => [{col1 => 'Setting',
  509:                               col2 => 'Value',}],
  510:                   print => \&print_ltitools,
  511:                   modify => \&modify_ltitools,
  512:                  },
  513:     );
  514:     if (keys(%servers) > 1) {
  515:         $prefs{'login'}  = { text   => 'Log-in page options',
  516:                              help   => 'Domain_Configuration_Login_Page',
  517:                             header => [{col1 => 'Log-in Service',
  518:                                         col2 => 'Server Setting',},
  519:                                        {col1 => 'Log-in Page Items',
  520:                                         col2 => ''},
  521:                                        {col1 => 'Log-in Help',
  522:                                         col2 => 'Value'},
  523:                                        {col1 => 'Custom HTML in document head',
  524:                                         col2 => 'Value'}],
  525:                             print => \&print_login,
  526:                             modify => \&modify_login,
  527:                            };
  528:     }
  529: 
  530:     my @roles = ('student','coordinator','author','admin');
  531:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  532:     &Apache::lonhtmlcommon::add_breadcrumb
  533:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  534:       text=>"Settings to display/modify"});
  535:     my $confname = $dom.'-domainconfig';
  536: 
  537:     if ($phase eq 'process') {
  538:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  539:                                                               \%prefs,\%domconfig,$confname,\@roles);
  540:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  541:             $r->rflush();
  542:             &devalidate_remote_domconfs($dom,$result);
  543:         }
  544:     } elsif ($phase eq 'display') {
  545:         my $js = &recaptcha_js().
  546:                  &toggle_display_js();
  547:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  548:             my ($othertitle,$usertypes,$types) =
  549:                 &Apache::loncommon::sorted_inst_types($dom);
  550:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  551:                                           $domconfig{'loadbalancing'}).
  552:                    &new_spares_js().
  553:                    &common_domprefs_js().
  554:                    &Apache::loncommon::javascript_array_indexof();
  555:         }
  556:         if (grep(/^requestcourses$/,@actions)) {
  557:             my $javascript_validations;
  558:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  559:             $js .= <<END;
  560: <script type="text/javascript">
  561: $javascript_validations
  562: </script>
  563: $coursebrowserjs
  564: END
  565:         }
  566:         if (grep(/^selfcreation$/,@actions)) {
  567:             $js .= &selfcreate_javascript();
  568:         }
  569:         if (grep(/^contacts$/,@actions)) {
  570:             $js .= &contacts_javascript();
  571:         }
  572:         if (grep(/^scantron$/,@actions)) {
  573:             $js .= &scantron_javascript();
  574:         }
  575:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  576:     } else {
  577: # check if domconfig user exists for the domain.
  578:         my $servadm = $r->dir_config('lonAdmEMail');
  579:         my ($configuserok,$author_ok,$switchserver) =
  580:             &config_check($dom,$confname,$servadm);
  581:         unless ($configuserok eq 'ok') {
  582:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  583:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  584:                           $confname).
  585:                       '<br />'
  586:             );
  587:             if ($switchserver) {
  588:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  589:                           '<br />'.
  590:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  591:                           '<br />'.
  592:                           &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).
  593:                           '<br />'.
  594:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  595:                 );
  596:             } else {
  597:                 $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.').
  598:                           '<br />'.
  599:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  600:                 );
  601:             }
  602:             $r->print(&Apache::loncommon::end_page());
  603:             return OK;
  604:         }
  605:         if (keys(%domconfig) == 0) {
  606:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  607:             my @ids=&Apache::lonnet::current_machine_ids();
  608:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  609:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  610:                 my @loginimages = ('img','logo','domlogo','login');
  611:                 my $custom_img_count = 0;
  612:                 foreach my $img (@loginimages) {
  613:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  614:                         $custom_img_count ++;
  615:                     }
  616:                 }
  617:                 foreach my $role (@roles) {
  618:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  619:                         $custom_img_count ++;
  620:                     }
  621:                 }
  622:                 if ($custom_img_count > 0) {
  623:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  624:                     my $switch_server = &check_switchserver($dom,$confname);
  625:                     $r->print(
  626:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  627:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  628:     &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 />'.
  629:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  630:                     if ($switch_server) {
  631:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  632:                     }
  633:                     $r->print(&Apache::loncommon::end_page());
  634:                     return OK;
  635:                 }
  636:             }
  637:         }
  638:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  639:     }
  640:     return OK;
  641: }
  642: 
  643: sub process_changes {
  644:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  645:     my %domconfig;
  646:     if (ref($values) eq 'HASH') {
  647:         %domconfig = %{$values};
  648:     }
  649:     my $output;
  650:     if ($action eq 'login') {
  651:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  652:     } elsif ($action eq 'rolecolors') {
  653:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  654:                                      $lastactref,%domconfig);
  655:     } elsif ($action eq 'quotas') {
  656:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  657:     } elsif ($action eq 'autoenroll') {
  658:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  659:     } elsif ($action eq 'autoupdate') {
  660:         $output = &modify_autoupdate($dom,%domconfig);
  661:     } elsif ($action eq 'autocreate') {
  662:         $output = &modify_autocreate($dom,%domconfig);
  663:     } elsif ($action eq 'directorysrch') {
  664:         $output = &modify_directorysrch($dom,$lastactref,%domconfig);
  665:     } elsif ($action eq 'usercreation') {
  666:         $output = &modify_usercreation($dom,%domconfig);
  667:     } elsif ($action eq 'selfcreation') {
  668:         $output = &modify_selfcreation($dom,$lastactref,%domconfig);
  669:     } elsif ($action eq 'usermodification') {
  670:         $output = &modify_usermodification($dom,%domconfig);
  671:     } elsif ($action eq 'contacts') {
  672:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  673:     } elsif ($action eq 'defaults') {
  674:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  675:     } elsif ($action eq 'scantron') {
  676:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  677:     } elsif ($action eq 'coursecategories') {
  678:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  679:     } elsif ($action eq 'serverstatuses') {
  680:         $output = &modify_serverstatuses($dom,%domconfig);
  681:     } elsif ($action eq 'requestcourses') {
  682:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  683:     } elsif ($action eq 'requestauthor') {
  684:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  685:     } elsif ($action eq 'helpsettings') {
  686:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  687:     } elsif ($action eq 'coursedefaults') {
  688:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  689:     } elsif ($action eq 'selfenrollment') {
  690:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  691:     } elsif ($action eq 'usersessions') {
  692:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  693:     } elsif ($action eq 'loadbalancing') {
  694:         $output = &modify_loadbalancing($dom,%domconfig);
  695:     } elsif ($action eq 'passwords') {
  696:         $output = &modify_passwords($r,$dom,$confname,$lastactref,%domconfig);
  697:     } elsif ($action eq 'ltitools') {
  698:         $output = &modify_ltitools($r,$dom,$action,$lastactref,%domconfig);
  699:     }
  700:     return $output;
  701: }
  702: 
  703: sub print_config_box {
  704:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  705:     my $rowtotal = 0;
  706:     my $output;
  707:     if ($action eq 'coursecategories') {
  708:         $output = &coursecategories_javascript($settings);
  709:     } elsif ($action eq 'defaults') {
  710:         $output = &defaults_javascript($settings); 
  711:     } elsif ($action eq 'passwords') {
  712:         $output = &passwords_javascript();
  713:     } elsif ($action eq 'helpsettings') {
  714:         my (%privs,%levelscurrent);
  715:         my %full=();
  716:         my %levels=(
  717:                      course => {},
  718:                      domain => {},
  719:                      system => {},
  720:                    );
  721:         my $context = 'domain';
  722:         my $crstype = 'Course';
  723:         my $formname = 'display';
  724:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  725:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  726:         $output =
  727:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full,
  728:                                                       \@templateroles);
  729:     }
  730:     $output .=
  731:          '<table class="LC_nested_outer">
  732:           <tr>
  733:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  734:            &mt($item->{text}).'&nbsp;'.
  735:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  736:           '</tr>';
  737:     $rowtotal ++;
  738:     my $numheaders = 1;
  739:     if (ref($item->{'header'}) eq 'ARRAY') {
  740:         $numheaders = scalar(@{$item->{'header'}});
  741:     }
  742:     if ($numheaders > 1) {
  743:         my $colspan = '';
  744:         my $rightcolspan = '';
  745:         my $leftnobr = '';
  746:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  747:             ($action eq 'directorysrch') ||
  748:             (($action eq 'login') && ($numheaders < 4))) {
  749:             $colspan = ' colspan="2"';
  750:         }
  751:         if ($action eq 'usersessions') {
  752:             $rightcolspan = ' colspan="3"'; 
  753:         }
  754:         if ($action eq 'passwords') {
  755:             $leftnobr = ' LC_nobreak';
  756:         }
  757:         $output .= '
  758:           <tr>
  759:            <td>
  760:             <table class="LC_nested">
  761:              <tr class="LC_info_row">
  762:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  763:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  764:              </tr>';
  765:         $rowtotal ++;
  766:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  767:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  768:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'directorysrch') ||
  769:             ($action eq 'helpsettings') || ($action eq 'contacts')) {
  770:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  771:         } elsif ($action eq 'passwords') {
  772:             $output .= $item->{'print'}->('top',$dom,$confname,$settings,\$rowtotal);
  773:         } elsif ($action eq 'coursecategories') {
  774:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  775:         } elsif ($action eq 'scantron') {
  776:             $output .= $item->{'print'}->($r,'top',$dom,$confname,$settings,\$rowtotal);
  777:         } elsif ($action eq 'login') {
  778:             if ($numheaders == 4) {
  779:                 $colspan = ' colspan="2"';
  780:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  781:             } else {
  782:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  783:             }
  784:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  785:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  786:         } elsif ($action eq 'rolecolors') {
  787:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  788:         }
  789:         $output .= '
  790:            </table>
  791:           </td>
  792:          </tr>
  793:          <tr>
  794:            <td>
  795:             <table class="LC_nested">
  796:              <tr class="LC_info_row">
  797:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  798:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  799:              </tr>';
  800:             $rowtotal ++;
  801:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  802:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  803:             ($action eq 'usersessions') || ($action eq 'coursecategories') ||
  804:             ($action eq 'contacts') || ($action eq 'passwords')) {
  805:             if ($action eq 'coursecategories') {
  806:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  807:                 $colspan = ' colspan="2"';
  808:             } elsif ($action eq 'passwords') {
  809:                 $output .= $item->{'print'}->('middle',$dom,$confname,$settings,\$rowtotal);
  810:             } else {
  811:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  812:             }
  813:             $output .= '
  814:            </table>
  815:           </td>
  816:          </tr>
  817:          <tr>
  818:            <td>
  819:             <table class="LC_nested">
  820:              <tr class="LC_info_row">
  821:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  822:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  823:              </tr>'."\n";
  824:             if ($action eq 'coursecategories') {
  825:                 $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  826:             } elsif ($action eq 'passwords') {
  827:                 $output .= $item->{'print'}->('lower',$dom,$confname,$settings,\$rowtotal).'
  828:              </tr>
  829:             </table>
  830:            </td>
  831:           </tr>
  832:           <tr>
  833:            <td>
  834:             <table class="LC_nested">
  835:              <tr class="LC_info_row">
  836:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  837:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'."\n".
  838:               $item->{'print'}->('bottom',$dom,$confname,$settings,\$rowtotal).'
  839:             </table>
  840:           </td>
  841:          </tr>
  842:          <tr>';
  843:             } else {
  844:                 $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  845:             }
  846:             $rowtotal ++;
  847:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  848:                  ($action eq 'defaults') || ($action eq 'directorysrch') ||
  849:                  ($action eq 'helpsettings')) {
  850:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  851:         } elsif ($action eq 'scantron') {
  852:             $output .= $item->{'print'}->($r,'bottom',$dom,$confname,$settings,\$rowtotal);
  853:         } elsif ($action eq 'login') {
  854:             if ($numheaders == 4) {
  855:                 $output .= &print_login('page',$dom,$confname,$phase,$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.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  864:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  865:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  866:                 $rowtotal ++;
  867:             } else {
  868:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  869:             }
  870:             $output .= '
  871:            </table>
  872:           </td>
  873:          </tr>
  874:          <tr>
  875:            <td>
  876:             <table class="LC_nested">
  877:              <tr class="LC_info_row">';
  878:             if ($numheaders == 4) {
  879:                 $output .= '
  880:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  881:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  882:              </tr>';
  883:             } else {
  884:                 $output .= '
  885:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  886:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  887:              </tr>';
  888:             }
  889:             $rowtotal ++;
  890:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal);
  891:         } elsif ($action eq 'requestcourses') {
  892:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  893:             $rowtotal ++;
  894:             $output .= &print_studentcode($settings,\$rowtotal).'
  895:            </table>
  896:           </td>
  897:          </tr>
  898:          <tr>
  899:            <td>
  900:             <table class="LC_nested">
  901:              <tr class="LC_info_row">
  902:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  903:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  904:                        &textbookcourses_javascript($settings).
  905:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
  906:             </table>
  907:            </td>
  908:           </tr>
  909:          <tr>
  910:            <td>
  911:             <table class="LC_nested">
  912:              <tr class="LC_info_row">
  913:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  914:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
  915:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
  916:             </table>
  917:            </td>
  918:           </tr>
  919:           <tr>
  920:            <td>
  921:             <table class="LC_nested">
  922:              <tr class="LC_info_row">
  923:               <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
  924:               <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
  925:              </tr>'.
  926:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
  927:         } elsif ($action eq 'requestauthor') {
  928:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  929:             $rowtotal ++;
  930:         } elsif ($action eq 'rolecolors') {
  931:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  932:            </table>
  933:           </td>
  934:          </tr>
  935:          <tr>
  936:            <td>
  937:             <table class="LC_nested">
  938:              <tr class="LC_info_row">
  939:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  940:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  941:               <td class="LC_right_item" valign="top">'.
  942:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  943:              </tr>'.
  944:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  945:            </table>
  946:           </td>
  947:          </tr>
  948:          <tr>
  949:            <td>
  950:             <table class="LC_nested">
  951:              <tr class="LC_info_row">
  952:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  953:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  954:              </tr>'.
  955:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  956:             $rowtotal += 2;
  957:         }
  958:     } else {
  959:         $output .= '
  960:           <tr>
  961:            <td>
  962:             <table class="LC_nested">
  963:              <tr class="LC_info_row">';
  964:         if ($action eq 'login') {
  965:             $output .= '  
  966:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  967:         } elsif ($action eq 'serverstatuses') {
  968:             $output .= '
  969:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  970:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  971: 
  972:         } else {
  973:             $output .= '
  974:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  975:         }
  976:         if (defined($item->{'header'}->[0]->{'col3'})) {
  977:             $output .= '<td class="LC_left_item" valign="top">'.
  978:                        &mt($item->{'header'}->[0]->{'col2'});
  979:             if ($action eq 'serverstatuses') {
  980:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  981:             } 
  982:         } else {
  983:             $output .= '<td class="LC_right_item" valign="top">'.
  984:                        &mt($item->{'header'}->[0]->{'col2'});
  985:         }
  986:         $output .= '</td>';
  987:         if ($item->{'header'}->[0]->{'col3'}) {
  988:             if (defined($item->{'header'}->[0]->{'col4'})) {
  989:                 $output .= '<td class="LC_left_item" valign="top">'.
  990:                             &mt($item->{'header'}->[0]->{'col3'});
  991:             } else {
  992:                 $output .= '<td class="LC_right_item" valign="top">'.
  993:                            &mt($item->{'header'}->[0]->{'col3'});
  994:             }
  995:             if ($action eq 'serverstatuses') {
  996:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  997:             }
  998:             $output .= '</td>';
  999:         }
 1000:         if ($item->{'header'}->[0]->{'col4'}) {
 1001:             $output .= '<td class="LC_right_item" valign="top">'.
 1002:                        &mt($item->{'header'}->[0]->{'col4'});
 1003:         }
 1004:         $output .= '</tr>';
 1005:         $rowtotal ++;
 1006:         if ($action eq 'quotas') {
 1007:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1008:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
 1009:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing') ||
 1010:                  ($action eq 'ltitools')) {
 1011:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
 1012:         }
 1013:     }
 1014:     $output .= '
 1015:    </table>
 1016:   </td>
 1017:  </tr>
 1018: </table><br />';
 1019:     return ($output,$rowtotal);
 1020: }
 1021: 
 1022: sub print_login {
 1023:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
 1024:     my ($css_class,$datatable);
 1025:     my %choices = &login_choices();
 1026: 
 1027:     if ($caller eq 'service') {
 1028:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 1029:         my $choice = $choices{'disallowlogin'};
 1030:         $css_class = ' class="LC_odd_row"';
 1031:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
 1032:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1033:                       '<th>'.$choices{'server'}.'</th>'.
 1034:                       '<th>'.$choices{'serverpath'}.'</th>'.
 1035:                       '<th>'.$choices{'custompath'}.'</th>'.
 1036:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
 1037:         my %disallowed;
 1038:         if (ref($settings) eq 'HASH') {
 1039:             if (ref($settings->{'loginvia'}) eq 'HASH') {
 1040:                %disallowed = %{$settings->{'loginvia'}};
 1041:             }
 1042:         }
 1043:         foreach my $lonhost (sort(keys(%servers))) {
 1044:             my $direct = 'selected="selected"';
 1045:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1046:                 if ($disallowed{$lonhost}{'server'} ne '') {
 1047:                     $direct = '';
 1048:                 }
 1049:             }
 1050:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1051:                           '<td><select name="'.$lonhost.'_server">'.
 1052:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1053:                           '</option>';
 1054:             foreach my $hostid (sort(keys(%servers))) {
 1055:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1056:                 my $selected = '';
 1057:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1058:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1059:                         $selected = 'selected="selected"';
 1060:                     }
 1061:                 }
 1062:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1063:                               $servers{$hostid}.'</option>';
 1064:             }
 1065:             $datatable .= '</select></td>'.
 1066:                           '<td><select name="'.$lonhost.'_serverpath">';
 1067:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1068:                 my $pathname = $path;
 1069:                 if ($path eq 'custom') {
 1070:                     $pathname = &mt('Custom Path').' ->';
 1071:                 }
 1072:                 my $selected = '';
 1073:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1074:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1075:                         $selected = 'selected="selected"';
 1076:                     }
 1077:                 } elsif ($path eq '') {
 1078:                     $selected = 'selected="selected"';
 1079:                 }
 1080:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1081:             }
 1082:             $datatable .= '</select></td>';
 1083:             my ($custom,$exempt);
 1084:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1085:                 $custom = $disallowed{$lonhost}{'custompath'};
 1086:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1087:             }
 1088:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1089:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1090:                           '</tr>';
 1091:         }
 1092:         $datatable .= '</table></td></tr>';
 1093:         return $datatable;
 1094:     } elsif ($caller eq 'page') {
 1095:         my %defaultchecked = ( 
 1096:                                'coursecatalog' => 'on',
 1097:                                'helpdesk'      => 'on',
 1098:                                'adminmail'     => 'off',
 1099:                                'newuser'       => 'off',
 1100:                              );
 1101:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1102:         my (%checkedon,%checkedoff);
 1103:         foreach my $item (@toggles) {
 1104:             if ($defaultchecked{$item} eq 'on') { 
 1105:                 $checkedon{$item} = ' checked="checked" ';
 1106:                 $checkedoff{$item} = ' ';
 1107:             } elsif ($defaultchecked{$item} eq 'off') {
 1108:                 $checkedoff{$item} = ' checked="checked" ';
 1109:                 $checkedon{$item} = ' ';
 1110:             }
 1111:         }
 1112:         my @images = ('img','logo','domlogo','login');
 1113:         my @logintext = ('textcol','bgcol');
 1114:         my @bgs = ('pgbg','mainbg','sidebg');
 1115:         my @links = ('link','alink','vlink');
 1116:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1117:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1118:         my (%is_custom,%designs);
 1119:         my %defaults = (
 1120:                        font => $defaultdesign{'login.font'},
 1121:                        );
 1122:         foreach my $item (@images) {
 1123:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1124:             $defaults{'showlogo'}{$item} = 1;
 1125:         }
 1126:         foreach my $item (@bgs) {
 1127:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1128:         }
 1129:         foreach my $item (@logintext) {
 1130:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1131:         }
 1132:         foreach my $item (@links) {
 1133:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1134:         }
 1135:         if (ref($settings) eq 'HASH') {
 1136:             foreach my $item (@toggles) {
 1137:                 if ($settings->{$item} eq '1') {
 1138:                     $checkedon{$item} =  ' checked="checked" ';
 1139:                     $checkedoff{$item} = ' ';
 1140:                 } elsif ($settings->{$item} eq '0') {
 1141:                     $checkedoff{$item} =  ' checked="checked" ';
 1142:                     $checkedon{$item} = ' ';
 1143:                 }
 1144:             }
 1145:             foreach my $item (@images) {
 1146:                 if (defined($settings->{$item})) {
 1147:                     $designs{$item} = $settings->{$item};
 1148:                     $is_custom{$item} = 1;
 1149:                 }
 1150:                 if (defined($settings->{'showlogo'}{$item})) {
 1151:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1152:                 }
 1153:             }
 1154:             foreach my $item (@logintext) {
 1155:                 if ($settings->{$item} ne '') {
 1156:                     $designs{'logintext'}{$item} = $settings->{$item};
 1157:                     $is_custom{$item} = 1;
 1158:                 }
 1159:             }
 1160:             if ($settings->{'font'} ne '') {
 1161:                 $designs{'font'} = $settings->{'font'};
 1162:                 $is_custom{'font'} = 1;
 1163:             }
 1164:             foreach my $item (@bgs) {
 1165:                 if ($settings->{$item} ne '') {
 1166:                     $designs{'bgs'}{$item} = $settings->{$item};
 1167:                     $is_custom{$item} = 1;
 1168:                 }
 1169:             }
 1170:             foreach my $item (@links) {
 1171:                 if ($settings->{$item} ne '') {
 1172:                     $designs{'links'}{$item} = $settings->{$item};
 1173:                     $is_custom{$item} = 1;
 1174:                 }
 1175:             }
 1176:         } else {
 1177:             if ($designhash{$dom.'.login.font'} ne '') {
 1178:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1179:                 $is_custom{'font'} = 1;
 1180:             }
 1181:             foreach my $item (@images) {
 1182:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1183:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1184:                     $is_custom{$item} = 1;
 1185:                 }
 1186:             }
 1187:             foreach my $item (@bgs) {
 1188:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1189:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1190:                     $is_custom{$item} = 1;
 1191:                 }
 1192:             }
 1193:             foreach my $item (@links) {
 1194:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1195:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1196:                     $is_custom{$item} = 1;
 1197:                 }
 1198:             }
 1199:         }
 1200:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1201:                                                       logo => 'Institution Logo',
 1202:                                                       domlogo => 'Domain Logo',
 1203:                                                       login => 'Login box');
 1204:         my $itemcount = 1;
 1205:         foreach my $item (@toggles) {
 1206:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1207:             $datatable .=  
 1208:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1209:                 '</td><td>'.
 1210:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1211:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1212:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1213:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1214:                 '</tr>';
 1215:             $itemcount ++;
 1216:         }
 1217:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1218:         $datatable .= '</tr></table></td></tr>';
 1219:     } elsif ($caller eq 'help') {
 1220:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1221:         my $switchserver = &check_switchserver($dom,$confname);
 1222:         my $itemcount = 1;
 1223:         $defaulturl = '/adm/loginproblems.html';
 1224:         $defaulttype = 'default';
 1225:         %lt = &Apache::lonlocal::texthash (
 1226:                      del     => 'Delete?',
 1227:                      rep     => 'Replace:',
 1228:                      upl     => 'Upload:',
 1229:                      default => 'Default',
 1230:                      custom  => 'Custom',
 1231:                                              );
 1232:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1233:         my @currlangs;
 1234:         if (ref($settings) eq 'HASH') {
 1235:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1236:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1237:                     next if ($settings->{'helpurl'}{$key} eq '');
 1238:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1239:                     $type{$key} = 'custom';
 1240:                     unless ($key eq 'nolang') {
 1241:                         push(@currlangs,$key);
 1242:                     }
 1243:                 }
 1244:             } elsif ($settings->{'helpurl'} ne '') {
 1245:                 $type{'nolang'} = 'custom';
 1246:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1247:             }
 1248:         }
 1249:         foreach my $lang ('nolang',sort(@currlangs)) {
 1250:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1251:             $datatable .= '<tr'.$css_class.'>';
 1252:             if ($url{$lang} eq '') {
 1253:                 $url{$lang} = $defaulturl;
 1254:             }
 1255:             if ($type{$lang} eq '') {
 1256:                 $type{$lang} = $defaulttype;
 1257:             }
 1258:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1259:             if ($lang eq 'nolang') {
 1260:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1261:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1262:             } else {
 1263:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1264:                                   $langchoices{$lang},
 1265:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1266:             }
 1267:             $datatable .= '</span></td>'."\n".
 1268:                           '<td class="LC_left_item">';
 1269:             if ($type{$lang} eq 'custom') {
 1270:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1271:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1272:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1273:             } else {
 1274:                 $datatable .= $lt{'upl'};
 1275:             }
 1276:             $datatable .='<br />';
 1277:             if ($switchserver) {
 1278:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1279:             } else {
 1280:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1281:             }
 1282:             $datatable .= '</td></tr>';
 1283:             $itemcount ++;
 1284:         }
 1285:         my @addlangs;
 1286:         foreach my $lang (sort(keys(%langchoices))) {
 1287:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1288:             push(@addlangs,$lang);
 1289:         }
 1290:         if (@addlangs > 0) {
 1291:             my %toadd;
 1292:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1293:             $toadd{''} = &mt('Select');
 1294:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1295:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1296:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1297:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1298:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1299:             if ($switchserver) {
 1300:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1301:             } else {
 1302:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1303:             }
 1304:             $datatable .= '</td></tr>';
 1305:             $itemcount ++;
 1306:         }
 1307:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1308:     } elsif ($caller eq 'headtag') {
 1309:         my %domservers = &Apache::lonnet::get_servers($dom);
 1310:         my $choice = $choices{'headtag'};
 1311:         $css_class = ' class="LC_odd_row"';
 1312:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1313:                       '<td align="left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1314:                       '<th>'.$choices{'current'}.'</th>'.
 1315:                       '<th>'.$choices{'action'}.'</th>'.
 1316:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1317:         my (%currurls,%currexempt);
 1318:         if (ref($settings) eq 'HASH') {
 1319:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1320:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1321:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1322:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1323:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1324:                     }
 1325:                 }
 1326:             }
 1327:         }
 1328:         my %lt = &Apache::lonlocal::texthash(
 1329:                                                del  => 'Delete?',
 1330:                                                rep  => 'Replace:',
 1331:                                                upl  => 'Upload:',
 1332:                                                curr => 'View contents',
 1333:                                                none => 'None',
 1334:         );
 1335:         my $switchserver = &check_switchserver($dom,$confname);
 1336:         foreach my $lonhost (sort(keys(%domservers))) {
 1337:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1338:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1339:             if ($currurls{$lonhost}) {
 1340:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1341:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1342:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1343:                               '">'.$lt{'curr'}.'</a></td>'.
 1344:                               '<td><span class="LC_nobreak"><label>'.
 1345:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1346:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1347:             } else {
 1348:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1349:             }
 1350:             $datatable .='<br />';
 1351:             if ($switchserver) {
 1352:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1353:             } else {
 1354:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1355:             }
 1356:             $datatable .= '</td><td><input type="text" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1357:         }
 1358:         $datatable .= '</table></td></tr>';
 1359:     }
 1360:     return $datatable;
 1361: }
 1362: 
 1363: sub login_choices {
 1364:     my %choices =
 1365:         &Apache::lonlocal::texthash (
 1366:             coursecatalog => 'Display Course/Community Catalog link?',
 1367:             adminmail     => "Display Administrator's E-mail Address?",
 1368:             helpdesk      => 'Display "Contact Helpdesk" link',
 1369:             disallowlogin => "Login page requests redirected",
 1370:             hostid        => "Server",
 1371:             server        => "Redirect to:",
 1372:             serverpath    => "Path",
 1373:             custompath    => "Custom", 
 1374:             exempt        => "Exempt IP(s)",
 1375:             directlogin   => "No redirect",
 1376:             newuser       => "Link to create a user account",
 1377:             img           => "Header",
 1378:             logo          => "Main Logo",
 1379:             domlogo       => "Domain Logo",
 1380:             login         => "Log-in Header", 
 1381:             textcol       => "Text color",
 1382:             bgcol         => "Box color",
 1383:             bgs           => "Background colors",
 1384:             links         => "Link colors",
 1385:             font          => "Font color",
 1386:             pgbg          => "Header",
 1387:             mainbg        => "Page",
 1388:             sidebg        => "Login box",
 1389:             link          => "Link",
 1390:             alink         => "Active link",
 1391:             vlink         => "Visited link",
 1392:             headtag       => "Custom markup",
 1393:             action        => "Action",
 1394:             current       => "Current",
 1395:         );
 1396:     return %choices;
 1397: }
 1398: 
 1399: sub print_rolecolors {
 1400:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1401:     my %choices = &color_font_choices();
 1402:     my @bgs = ('pgbg','tabbg','sidebg');
 1403:     my @links = ('link','alink','vlink');
 1404:     my @images = ('img');
 1405:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1406:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1407:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1408:     my (%is_custom,%designs);
 1409:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1410:     if (ref($settings) eq 'HASH') {
 1411:         if (ref($settings->{$role}) eq 'HASH') {
 1412:             if ($settings->{$role}->{'img'} ne '') {
 1413:                 $designs{'img'} = $settings->{$role}->{'img'};
 1414:                 $is_custom{'img'} = 1;
 1415:             }
 1416:             if ($settings->{$role}->{'font'} ne '') {
 1417:                 $designs{'font'} = $settings->{$role}->{'font'};
 1418:                 $is_custom{'font'} = 1;
 1419:             }
 1420:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1421:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1422:                 $is_custom{'fontmenu'} = 1;
 1423:             }
 1424:             foreach my $item (@bgs) {
 1425:                 if ($settings->{$role}->{$item} ne '') {
 1426:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1427:                     $is_custom{$item} = 1;
 1428:                 }
 1429:             }
 1430:             foreach my $item (@links) {
 1431:                 if ($settings->{$role}->{$item} ne '') {
 1432:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1433:                     $is_custom{$item} = 1;
 1434:                 }
 1435:             }
 1436:         }
 1437:     } else {
 1438:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1439:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1440:             $is_custom{'img'} = 1;
 1441:         }
 1442:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1443:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1444:             $is_custom{'fontmenu'} = 1; 
 1445:         }
 1446:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1447:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1448:             $is_custom{'font'} = 1;
 1449:         }
 1450:         foreach my $item (@bgs) {
 1451:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1452:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1453:                 $is_custom{$item} = 1;
 1454:             
 1455:             }
 1456:         }
 1457:         foreach my $item (@links) {
 1458:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1459:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1460:                 $is_custom{$item} = 1;
 1461:             }
 1462:         }
 1463:     }
 1464:     my $itemcount = 1;
 1465:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1466:     $datatable .= '</tr></table></td></tr>';
 1467:     return $datatable;
 1468: }
 1469: 
 1470: sub role_defaults {
 1471:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1472:     my %defaults;
 1473:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1474:         return %defaults;
 1475:     }
 1476:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1477:     if ($role eq 'login') {
 1478:         %defaults = (
 1479:                        font => $defaultdesign{$role.'.font'},
 1480:                     );
 1481:         if (ref($logintext) eq 'ARRAY') {
 1482:             foreach my $item (@{$logintext}) {
 1483:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1484:             }
 1485:         }
 1486:         foreach my $item (@{$images}) {
 1487:             $defaults{'showlogo'}{$item} = 1;
 1488:         }
 1489:     } else {
 1490:         %defaults = (
 1491:                        img => $defaultdesign{$role.'.img'},
 1492:                        font => $defaultdesign{$role.'.font'},
 1493:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1494:                     );
 1495:     }
 1496:     foreach my $item (@{$bgs}) {
 1497:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1498:     }
 1499:     foreach my $item (@{$links}) {
 1500:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1501:     }
 1502:     foreach my $item (@{$images}) {
 1503:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1504:     }
 1505:     return %defaults;
 1506: }
 1507: 
 1508: sub display_color_options {
 1509:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1510:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1511:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1512:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1513:     my $datatable = '<tr'.$css_class.'>'.
 1514:         '<td>'.$choices->{'font'}.'</td>';
 1515:     if (!$is_custom->{'font'}) {
 1516:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1517:     } else {
 1518:         $datatable .= '<td>&nbsp;</td>';
 1519:     }
 1520:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1521: 
 1522:     $datatable .= '<td><span class="LC_nobreak">'.
 1523:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1524:                   ' value="'.$current_color.'" />&nbsp;'.
 1525:                   '&nbsp;</span></td></tr>';
 1526:     unless ($role eq 'login') { 
 1527:         $datatable .= '<tr'.$css_class.'>'.
 1528:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1529:         if (!$is_custom->{'fontmenu'}) {
 1530:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1531:         } else {
 1532:             $datatable .= '<td>&nbsp;</td>';
 1533:         }
 1534: 	$current_color = $designs->{'fontmenu'} ?
 1535: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1536:         $datatable .= '<td><span class="LC_nobreak">'.
 1537:                       '<input class="colorchooser" type="text" size="10" name="'
 1538: 		      .$role.'_fontmenu"'.
 1539:                       ' value="'.$current_color.'" />&nbsp;'.
 1540:                       '&nbsp;</span></td></tr>';
 1541:     }
 1542:     my $switchserver = &check_switchserver($dom,$confname);
 1543:     foreach my $img (@{$images}) {
 1544: 	$itemcount ++;
 1545:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1546:         $datatable .= '<tr'.$css_class.'>'.
 1547:                       '<td>'.$choices->{$img};
 1548:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1549:         if ($role eq 'login') {
 1550:             if ($img eq 'login') {
 1551:                 $login_hdr_pick =
 1552:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1553:                 $logincolors =
 1554:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1555:                                        $designs,$defaults);
 1556:             } elsif ($img ne 'domlogo') {
 1557:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1558:             }
 1559:         }
 1560:         $datatable .= '</td>';
 1561:         if ($designs->{$img} ne '') {
 1562:             $imgfile = $designs->{$img};
 1563: 	    $img_import = ($imgfile =~ m{^/adm/});
 1564:         } else {
 1565:             $imgfile = $defaults->{$img};
 1566:         }
 1567:         if ($imgfile) {
 1568:             my ($showfile,$fullsize);
 1569:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1570:                 my $urldir = $1;
 1571:                 my $filename = $2;
 1572:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1573:                 if (@info) {
 1574:                     my $thumbfile = 'tn-'.$filename;
 1575:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1576:                     if (@thumb) {
 1577:                         $showfile = $urldir.'/'.$thumbfile;
 1578:                     } else {
 1579:                         $showfile = $imgfile;
 1580:                     }
 1581:                 } else {
 1582:                     $showfile = '';
 1583:                 }
 1584:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1585:                 $showfile = $imgfile;
 1586:                 my $imgdir = $1;
 1587:                 my $filename = $2;
 1588:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1589:                     $showfile = "/$imgdir/tn-".$filename;
 1590:                 } else {
 1591:                     my $input = $londocroot.$imgfile;
 1592:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1593:                     if (!-e $output) {
 1594:                         my ($width,$height) = &thumb_dimensions();
 1595:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1596:                         if ($fullwidth ne '' && $fullheight ne '') {
 1597:                             if ($fullwidth > $width && $fullheight > $height) { 
 1598:                                 my $size = $width.'x'.$height;
 1599:                                 my @args = ('convert','-sample',$size,$input,$output);
 1600:                                 system({$args[0]} @args);
 1601:                                 $showfile = "/$imgdir/tn-".$filename;
 1602:                             }
 1603:                         }
 1604:                     }
 1605:                 }
 1606:             }
 1607:             if ($showfile) {
 1608:                 if ($showfile =~ m{^/(adm|res)/}) {
 1609:                     if ($showfile =~ m{^/res/}) {
 1610:                         my $local_showfile =
 1611:                             &Apache::lonnet::filelocation('',$showfile);
 1612:                         &Apache::lonnet::repcopy($local_showfile);
 1613:                     }
 1614:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1615:                 }
 1616:                 if ($imgfile) {
 1617:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1618:                         if ($imgfile =~ m{^/res/}) {
 1619:                             my $local_imgfile =
 1620:                                 &Apache::lonnet::filelocation('',$imgfile);
 1621:                             &Apache::lonnet::repcopy($local_imgfile);
 1622:                         }
 1623:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1624:                     } else {
 1625:                         $fullsize = $imgfile;
 1626:                     }
 1627:                 }
 1628:                 $datatable .= '<td>';
 1629:                 if ($img eq 'login') {
 1630:                     $datatable .= $login_hdr_pick;
 1631:                 } 
 1632:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1633:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1634:             } else {
 1635:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1636:                               &mt('Upload:').'<br />';
 1637:             }
 1638:         } else {
 1639:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1640:                           &mt('Upload:').'<br />';
 1641:         }
 1642:         if ($switchserver) {
 1643:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1644:         } else {
 1645:             if ($img ne 'login') { # suppress file selection for Log-in header
 1646:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1647:             }
 1648:         }
 1649:         $datatable .= '</td></tr>';
 1650:     }
 1651:     $itemcount ++;
 1652:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1653:     $datatable .= '<tr'.$css_class.'>'.
 1654:                   '<td>'.$choices->{'bgs'}.'</td>';
 1655:     my $bgs_def;
 1656:     foreach my $item (@{$bgs}) {
 1657:         if (!$is_custom->{$item}) {
 1658:             $bgs_def .= '<td><span class="LC_nobreak">'.$choices->{$item}.'</span>&nbsp;<span class="css_default_'.$role.'_'.$item.'" style="background-color: '.$defaults->{'bgs'}{$item}.';">&nbsp;&nbsp;&nbsp;</span><br />'.$defaults->{'bgs'}{$item}.'</td>';
 1659:         }
 1660:     }
 1661:     if ($bgs_def) {
 1662:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1663:     } else {
 1664:         $datatable .= '<td>&nbsp;</td>';
 1665:     }
 1666:     $datatable .= '<td class="LC_right_item">'.
 1667:                   '<table border="0"><tr>';
 1668: 
 1669:     foreach my $item (@{$bgs}) {
 1670:         $datatable .= '<td align="center">'.$choices->{$item};
 1671: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1672:         if ($designs->{'bgs'}{$item}) {
 1673:             $datatable .= '&nbsp;';
 1674:         }
 1675:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1676:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1677:     }
 1678:     $datatable .= '</tr></table></td></tr>';
 1679:     $itemcount ++;
 1680:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1681:     $datatable .= '<tr'.$css_class.'>'.
 1682:                   '<td>'.$choices->{'links'}.'</td>';
 1683:     my $links_def;
 1684:     foreach my $item (@{$links}) {
 1685:         if (!$is_custom->{$item}) {
 1686:             $links_def .= '<td>'.$choices->{$item}.'<br /><span class="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1687:         }
 1688:     }
 1689:     if ($links_def) {
 1690:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1691:     } else {
 1692:         $datatable .= '<td>&nbsp;</td>';
 1693:     }
 1694:     $datatable .= '<td class="LC_right_item">'.
 1695:                   '<table border="0"><tr>';
 1696:     foreach my $item (@{$links}) {
 1697: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1698:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1699:         if ($designs->{'links'}{$item}) {
 1700:             $datatable.='&nbsp;';
 1701:         }
 1702:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1703:                       '" /></td>';
 1704:     }
 1705:     $$rowtotal += $itemcount;
 1706:     return $datatable;
 1707: }
 1708: 
 1709: sub logo_display_options {
 1710:     my ($img,$defaults,$designs) = @_;
 1711:     my $checkedon;
 1712:     if (ref($defaults) eq 'HASH') {
 1713:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1714:             if ($defaults->{'showlogo'}{$img}) {
 1715:                 $checkedon = 'checked="checked" ';     
 1716:             }
 1717:         } 
 1718:     }
 1719:     if (ref($designs) eq 'HASH') {
 1720:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1721:             if (defined($designs->{'showlogo'}{$img})) {
 1722:                 if ($designs->{'showlogo'}{$img} == 0) {
 1723:                     $checkedon = '';
 1724:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1725:                     $checkedon = 'checked="checked" ';
 1726:                 }
 1727:             }
 1728:         }
 1729:     }
 1730:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1731:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1732:            &mt('show').'</label>'."\n";
 1733: }
 1734: 
 1735: sub login_header_options  {
 1736:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1737:     my $output = '';
 1738:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1739:         $output .= &mt('Text default(s):').'<br />';
 1740:         if (!$is_custom->{'textcol'}) {
 1741:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1742:                        '&nbsp;&nbsp;&nbsp;';
 1743:         }
 1744:         if (!$is_custom->{'bgcol'}) {
 1745:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1746:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1747:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1748:         }
 1749:         $output .= '<br />';
 1750:     }
 1751:     $output .='<br />';
 1752:     return $output;
 1753: }
 1754: 
 1755: sub login_text_colors {
 1756:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1757:     my $color_menu = '<table border="0"><tr>';
 1758:     foreach my $item (@{$logintext}) {
 1759:         $color_menu .= '<td align="center">'.$choices->{$item};
 1760:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1761:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1762:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1763:     }
 1764:     $color_menu .= '</tr></table><br />';
 1765:     return $color_menu;
 1766: }
 1767: 
 1768: sub image_changes {
 1769:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1770:     my $output;
 1771:     if ($img eq 'login') {
 1772:         $output = '</td><td>'.$logincolors; # suppress image for Log-in header
 1773:     } elsif (!$is_custom) {
 1774:         if ($img ne 'domlogo') {
 1775:             $output = &mt('Default image:').'<br />';
 1776:         } else {
 1777:             $output = &mt('Default in use:').'<br />';
 1778:         }
 1779:     }
 1780:     if ($img ne 'login') {
 1781:         if ($img_import) {
 1782:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1783:         }
 1784:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1785:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1786:         if ($is_custom) {
 1787:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1788:                        '<input type="checkbox" name="'.
 1789:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1790:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1791:         } else {
 1792:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1793:         }
 1794:     }
 1795:     return $output;
 1796: }
 1797: 
 1798: sub print_quotas {
 1799:     my ($dom,$settings,$rowtotal,$action) = @_;
 1800:     my $context;
 1801:     if ($action eq 'quotas') {
 1802:         $context = 'tools';
 1803:     } else {
 1804:         $context = $action;
 1805:     }
 1806:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1807:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1808:     my $typecount = 0;
 1809:     my ($css_class,%titles);
 1810:     if ($context eq 'requestcourses') {
 1811:         @usertools = ('official','unofficial','community','textbook');
 1812:         @options =('norequest','approval','validate','autolimit');
 1813:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1814:         %titles = &courserequest_titles();
 1815:     } elsif ($context eq 'requestauthor') {
 1816:         @usertools = ('author');
 1817:         @options = ('norequest','approval','automatic');
 1818:         %titles = &authorrequest_titles();
 1819:     } else {
 1820:         @usertools = ('aboutme','blog','webdav','portfolio');
 1821:         %titles = &tool_titles();
 1822:     }
 1823:     if (ref($types) eq 'ARRAY') {
 1824:         foreach my $type (@{$types}) {
 1825:             my ($currdefquota,$currauthorquota);
 1826:             unless (($context eq 'requestcourses') ||
 1827:                     ($context eq 'requestauthor')) {
 1828:                 if (ref($settings) eq 'HASH') {
 1829:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1830:                         $currdefquota = $settings->{defaultquota}->{$type};
 1831:                     } else {
 1832:                         $currdefquota = $settings->{$type};
 1833:                     }
 1834:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1835:                         $currauthorquota = $settings->{authorquota}->{$type};
 1836:                     }
 1837:                 }
 1838:             }
 1839:             if (defined($usertypes->{$type})) {
 1840:                 $typecount ++;
 1841:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1842:                 $datatable .= '<tr'.$css_class.'>'.
 1843:                               '<td>'.$usertypes->{$type}.'</td>'.
 1844:                               '<td class="LC_left_item">';
 1845:                 if ($context eq 'requestcourses') {
 1846:                     $datatable .= '<table><tr>';
 1847:                 }
 1848:                 my %cell;  
 1849:                 foreach my $item (@usertools) {
 1850:                     if ($context eq 'requestcourses') {
 1851:                         my ($curroption,$currlimit);
 1852:                         if (ref($settings) eq 'HASH') {
 1853:                             if (ref($settings->{$item}) eq 'HASH') {
 1854:                                 $curroption = $settings->{$item}->{$type};
 1855:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1856:                                     $currlimit = $1; 
 1857:                                 }
 1858:                             }
 1859:                         }
 1860:                         if (!$curroption) {
 1861:                             $curroption = 'norequest';
 1862:                         }
 1863:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1864:                         foreach my $option (@options) {
 1865:                             my $val = $option;
 1866:                             if ($option eq 'norequest') {
 1867:                                 $val = 0;  
 1868:                             }
 1869:                             if ($option eq 'validate') {
 1870:                                 my $canvalidate = 0;
 1871:                                 if (ref($validations{$item}) eq 'HASH') { 
 1872:                                     if ($validations{$item}{$type}) {
 1873:                                         $canvalidate = 1;
 1874:                                     }
 1875:                                 }
 1876:                                 next if (!$canvalidate);
 1877:                             }
 1878:                             my $checked = '';
 1879:                             if ($option eq $curroption) {
 1880:                                 $checked = ' checked="checked"';
 1881:                             } elsif ($option eq 'autolimit') {
 1882:                                 if ($curroption =~ /^autolimit/) {
 1883:                                     $checked = ' checked="checked"';
 1884:                                 }                       
 1885:                             } 
 1886:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1887:                                   '<input type="radio" name="crsreq_'.$item.
 1888:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1889:                                   $titles{$option}.'</label>';
 1890:                             if ($option eq 'autolimit') {
 1891:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1892:                                                 $item.'_limit_'.$type.'" size="1" '.
 1893:                                                 'value="'.$currlimit.'" />';
 1894:                             }
 1895:                             $cell{$item} .= '</span> ';
 1896:                             if ($option eq 'autolimit') {
 1897:                                 $cell{$item} .= $titles{'unlimited'};
 1898:                             }
 1899:                         }
 1900:                     } elsif ($context eq 'requestauthor') {
 1901:                         my $curroption;
 1902:                         if (ref($settings) eq 'HASH') {
 1903:                             $curroption = $settings->{$type};
 1904:                         }
 1905:                         if (!$curroption) {
 1906:                             $curroption = 'norequest';
 1907:                         }
 1908:                         foreach my $option (@options) {
 1909:                             my $val = $option;
 1910:                             if ($option eq 'norequest') {
 1911:                                 $val = 0;
 1912:                             }
 1913:                             my $checked = '';
 1914:                             if ($option eq $curroption) {
 1915:                                 $checked = ' checked="checked"';
 1916:                             }
 1917:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1918:                                   '<input type="radio" name="authorreq_'.$type.
 1919:                                   '" value="'.$val.'"'.$checked.' />'.
 1920:                                   $titles{$option}.'</label></span>&nbsp; ';
 1921:                         }
 1922:                     } else {
 1923:                         my $checked = 'checked="checked" ';
 1924:                         if (ref($settings) eq 'HASH') {
 1925:                             if (ref($settings->{$item}) eq 'HASH') {
 1926:                                 if ($settings->{$item}->{$type} == 0) {
 1927:                                     $checked = '';
 1928:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1929:                                     $checked =  'checked="checked" ';
 1930:                                 }
 1931:                             }
 1932:                         }
 1933:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1934:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1935:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1936:                                       '</label></span>&nbsp; ';
 1937:                     }
 1938:                 }
 1939:                 if ($context eq 'requestcourses') {
 1940:                     $datatable .= '</tr><tr>';
 1941:                     foreach my $item (@usertools) {
 1942:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1943:                     }
 1944:                     $datatable .= '</tr></table>';
 1945:                 }
 1946:                 $datatable .= '</td>';
 1947:                 unless (($context eq 'requestcourses') ||
 1948:                         ($context eq 'requestauthor')) {
 1949:                     $datatable .= 
 1950:                               '<td class="LC_right_item">'.
 1951:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1952:                               '<input type="text" name="quota_'.$type.
 1953:                               '" value="'.$currdefquota.
 1954:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1955:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1956:                               '<input type="text" name="authorquota_'.$type.
 1957:                               '" value="'.$currauthorquota.
 1958:                               '" size="5" /></span></td>';
 1959:                 }
 1960:                 $datatable .= '</tr>';
 1961:             }
 1962:         }
 1963:     }
 1964:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1965:         $defaultquota = '20';
 1966:         $authorquota = '500';
 1967:         if (ref($settings) eq 'HASH') {
 1968:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1969:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1970:             } elsif (defined($settings->{'default'})) {
 1971:                 $defaultquota = $settings->{'default'};
 1972:             }
 1973:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1974:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1975:             }
 1976:         }
 1977:     }
 1978:     $typecount ++;
 1979:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1980:     $datatable .= '<tr'.$css_class.'>'.
 1981:                   '<td>'.$othertitle.'</td>'.
 1982:                   '<td class="LC_left_item">';
 1983:     if ($context eq 'requestcourses') {
 1984:         $datatable .= '<table><tr>';
 1985:     }
 1986:     my %defcell;
 1987:     foreach my $item (@usertools) {
 1988:         if ($context eq 'requestcourses') {
 1989:             my ($curroption,$currlimit);
 1990:             if (ref($settings) eq 'HASH') {
 1991:                 if (ref($settings->{$item}) eq 'HASH') {
 1992:                     $curroption = $settings->{$item}->{'default'};
 1993:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1994:                         $currlimit = $1;
 1995:                     }
 1996:                 }
 1997:             }
 1998:             if (!$curroption) {
 1999:                 $curroption = 'norequest';
 2000:             }
 2001:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2002:             foreach my $option (@options) {
 2003:                 my $val = $option;
 2004:                 if ($option eq 'norequest') {
 2005:                     $val = 0;
 2006:                 }
 2007:                 if ($option eq 'validate') {
 2008:                     my $canvalidate = 0;
 2009:                     if (ref($validations{$item}) eq 'HASH') {
 2010:                         if ($validations{$item}{'default'}) {
 2011:                             $canvalidate = 1;
 2012:                         }
 2013:                     }
 2014:                     next if (!$canvalidate);
 2015:                 }
 2016:                 my $checked = '';
 2017:                 if ($option eq $curroption) {
 2018:                     $checked = ' checked="checked"';
 2019:                 } elsif ($option eq 'autolimit') {
 2020:                     if ($curroption =~ /^autolimit/) {
 2021:                         $checked = ' checked="checked"';
 2022:                     }
 2023:                 }
 2024:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 2025:                                   '<input type="radio" name="crsreq_'.$item.
 2026:                                   '_default" value="'.$val.'"'.$checked.' />'.
 2027:                                   $titles{$option}.'</label>';
 2028:                 if ($option eq 'autolimit') {
 2029:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2030:                                        $item.'_limit_default" size="1" '.
 2031:                                        'value="'.$currlimit.'" />';
 2032:                 }
 2033:                 $defcell{$item} .= '</span> ';
 2034:                 if ($option eq 'autolimit') {
 2035:                     $defcell{$item} .= $titles{'unlimited'};
 2036:                 }
 2037:             }
 2038:         } elsif ($context eq 'requestauthor') {
 2039:             my $curroption;
 2040:             if (ref($settings) eq 'HASH') {
 2041:                 $curroption = $settings->{'default'};
 2042:             }
 2043:             if (!$curroption) {
 2044:                 $curroption = 'norequest';
 2045:             }
 2046:             foreach my $option (@options) {
 2047:                 my $val = $option;
 2048:                 if ($option eq 'norequest') {
 2049:                     $val = 0;
 2050:                 }
 2051:                 my $checked = '';
 2052:                 if ($option eq $curroption) {
 2053:                     $checked = ' checked="checked"';
 2054:                 }
 2055:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2056:                               '<input type="radio" name="authorreq_default"'.
 2057:                               ' value="'.$val.'"'.$checked.' />'.
 2058:                               $titles{$option}.'</label></span>&nbsp; ';
 2059:             }
 2060:         } else {
 2061:             my $checked = 'checked="checked" ';
 2062:             if (ref($settings) eq 'HASH') {
 2063:                 if (ref($settings->{$item}) eq 'HASH') {
 2064:                     if ($settings->{$item}->{'default'} == 0) {
 2065:                         $checked = '';
 2066:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2067:                         $checked = 'checked="checked" ';
 2068:                     }
 2069:                 }
 2070:             }
 2071:             $datatable .= '<span class="LC_nobreak"><label>'.
 2072:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2073:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2074:                           '</label></span>&nbsp; ';
 2075:         }
 2076:     }
 2077:     if ($context eq 'requestcourses') {
 2078:         $datatable .= '</tr><tr>';
 2079:         foreach my $item (@usertools) {
 2080:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2081:         }
 2082:         $datatable .= '</tr></table>';
 2083:     }
 2084:     $datatable .= '</td>';
 2085:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2086:         $datatable .= '<td class="LC_right_item">'.
 2087:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2088:                       '<input type="text" name="defaultquota" value="'.
 2089:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2090:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2091:                       '<input type="text" name="authorquota" value="'.
 2092:                       $authorquota.'" size="5" /></span></td>';
 2093:     }
 2094:     $datatable .= '</tr>';
 2095:     $typecount ++;
 2096:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2097:     $datatable .= '<tr'.$css_class.'>'.
 2098:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2099:     if ($context eq 'requestcourses') {
 2100:         $datatable .= &mt('(overrides affiliation, if set)').
 2101:                       '</td>'.
 2102:                       '<td class="LC_left_item">'.
 2103:                       '<table><tr>';
 2104:     } else {
 2105:         $datatable .= &mt('(overrides affiliation, if checked)').
 2106:                       '</td>'.
 2107:                       '<td class="LC_left_item" colspan="2">'.
 2108:                       '<br />';
 2109:     }
 2110:     my %advcell;
 2111:     foreach my $item (@usertools) {
 2112:         if ($context eq 'requestcourses') {
 2113:             my ($curroption,$currlimit);
 2114:             if (ref($settings) eq 'HASH') {
 2115:                 if (ref($settings->{$item}) eq 'HASH') {
 2116:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2117:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2118:                         $currlimit = $1;
 2119:                     }
 2120:                 }
 2121:             }
 2122:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2123:             my $checked = '';
 2124:             if ($curroption eq '') {
 2125:                 $checked = ' checked="checked"';
 2126:             }
 2127:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2128:                                '<input type="radio" name="crsreq_'.$item.
 2129:                                '__LC_adv" value=""'.$checked.' />'.
 2130:                                &mt('No override set').'</label></span>&nbsp; ';
 2131:             foreach my $option (@options) {
 2132:                 my $val = $option;
 2133:                 if ($option eq 'norequest') {
 2134:                     $val = 0;
 2135:                 }
 2136:                 if ($option eq 'validate') {
 2137:                     my $canvalidate = 0;
 2138:                     if (ref($validations{$item}) eq 'HASH') {
 2139:                         if ($validations{$item}{'_LC_adv'}) {
 2140:                             $canvalidate = 1;
 2141:                         }
 2142:                     }
 2143:                     next if (!$canvalidate);
 2144:                 }
 2145:                 my $checked = '';
 2146:                 if ($val eq $curroption) {
 2147:                     $checked = ' checked="checked"';
 2148:                 } elsif ($option eq 'autolimit') {
 2149:                     if ($curroption =~ /^autolimit/) {
 2150:                         $checked = ' checked="checked"';
 2151:                     }
 2152:                 }
 2153:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2154:                                   '<input type="radio" name="crsreq_'.$item.
 2155:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2156:                                   $titles{$option}.'</label>';
 2157:                 if ($option eq 'autolimit') {
 2158:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2159:                                        $item.'_limit__LC_adv" size="1" '.
 2160:                                        'value="'.$currlimit.'" />';
 2161:                 }
 2162:                 $advcell{$item} .= '</span> ';
 2163:                 if ($option eq 'autolimit') {
 2164:                     $advcell{$item} .= $titles{'unlimited'};
 2165:                 }
 2166:             }
 2167:         } elsif ($context eq 'requestauthor') {
 2168:             my $curroption;
 2169:             if (ref($settings) eq 'HASH') {
 2170:                 $curroption = $settings->{'_LC_adv'};
 2171:             }
 2172:             my $checked = '';
 2173:             if ($curroption eq '') {
 2174:                 $checked = ' checked="checked"';
 2175:             }
 2176:             $datatable .= '<span class="LC_nobreak"><label>'.
 2177:                           '<input type="radio" name="authorreq__LC_adv"'.
 2178:                           ' value=""'.$checked.' />'.
 2179:                           &mt('No override set').'</label></span>&nbsp; ';
 2180:             foreach my $option (@options) {
 2181:                 my $val = $option;
 2182:                 if ($option eq 'norequest') {
 2183:                     $val = 0;
 2184:                 }
 2185:                 my $checked = '';
 2186:                 if ($val eq $curroption) {
 2187:                     $checked = ' checked="checked"';
 2188:                 }
 2189:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2190:                               '<input type="radio" name="authorreq__LC_adv"'.
 2191:                               ' value="'.$val.'"'.$checked.' />'.
 2192:                               $titles{$option}.'</label></span>&nbsp; ';
 2193:             }
 2194:         } else {
 2195:             my $checked = 'checked="checked" ';
 2196:             if (ref($settings) eq 'HASH') {
 2197:                 if (ref($settings->{$item}) eq 'HASH') {
 2198:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2199:                         $checked = '';
 2200:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2201:                         $checked = 'checked="checked" ';
 2202:                     }
 2203:                 }
 2204:             }
 2205:             $datatable .= '<span class="LC_nobreak"><label>'.
 2206:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2207:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2208:                           '</label></span>&nbsp; ';
 2209:         }
 2210:     }
 2211:     if ($context eq 'requestcourses') {
 2212:         $datatable .= '</tr><tr>';
 2213:         foreach my $item (@usertools) {
 2214:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2215:         }
 2216:         $datatable .= '</tr></table>';
 2217:     }
 2218:     $datatable .= '</td></tr>';
 2219:     $$rowtotal += $typecount;
 2220:     return $datatable;
 2221: }
 2222: 
 2223: sub print_requestmail {
 2224:     my ($dom,$action,$settings,$rowtotal,$customcss,$rowstyle) = @_;
 2225:     my ($now,$datatable,%currapp);
 2226:     $now = time;
 2227:     if (ref($settings) eq 'HASH') {
 2228:         if (ref($settings->{'notify'}) eq 'HASH') {
 2229:             if ($settings->{'notify'}{'approval'} ne '') {
 2230:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2231:             }
 2232:         }
 2233:     }
 2234:     my $numinrow = 2;
 2235:     my $css_class;
 2236:     if ($$rowtotal%2) {
 2237:         $css_class = 'LC_odd_row';
 2238:     }
 2239:     if ($customcss) {
 2240:         $css_class .= " $customcss";
 2241:     }
 2242:     $css_class =~ s/^\s+//;
 2243:     if ($css_class) {
 2244:         $css_class = ' class="'.$css_class.'"';
 2245:     }
 2246:     if ($rowstyle) {
 2247:         $css_class .= ' style="'.$rowstyle.'"';
 2248:     }
 2249:     my $text;
 2250:     if ($action eq 'requestcourses') {
 2251:         $text = &mt('Receive notification of course requests requiring approval');
 2252:     } elsif ($action eq 'requestauthor') {
 2253:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2254:     } else {
 2255:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2256:     }
 2257:     $datatable = '<tr'.$css_class.'>'.
 2258:                  ' <td>'.$text.'</td>'.
 2259:                  ' <td class="LC_left_item">';
 2260:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2261:                                                  $action.'notifyapproval',%currapp);
 2262:     if ($numdc > 0) {
 2263:         $datatable .= $table;
 2264:     } else {
 2265:         $datatable .= &mt('There are no active Domain Coordinators');
 2266:     }
 2267:     $datatable .='</td></tr>';
 2268:     return $datatable;
 2269: }
 2270: 
 2271: sub print_studentcode {
 2272:     my ($settings,$rowtotal) = @_;
 2273:     my $rownum = 0; 
 2274:     my ($output,%current);
 2275:     my @crstypes = ('official','unofficial','community','textbook');
 2276:     if (ref($settings) eq 'HASH') {
 2277:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2278:             foreach my $type (@crstypes) {
 2279:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2280:             }
 2281:         }
 2282:     }
 2283:     $output .= '<tr>'.
 2284:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2285:                '<td class="LC_left_item">';
 2286:     foreach my $type (@crstypes) {
 2287:         my $check = ' ';
 2288:         if ($current{$type}) {
 2289:             $check = ' checked="checked" ';
 2290:         }
 2291:         $output .= '<span class="LC_nobreak"><label>'.
 2292:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2293:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2294:     }
 2295:     $output .= '</td></tr>';
 2296:     $$rowtotal ++;
 2297:     return $output;
 2298: }
 2299: 
 2300: sub print_textbookcourses {
 2301:     my ($dom,$type,$settings,$rowtotal) = @_;
 2302:     my $rownum = 0;
 2303:     my $css_class;
 2304:     my $itemcount = 1;
 2305:     my $maxnum = 0;
 2306:     my $bookshash;
 2307:     if (ref($settings) eq 'HASH') {
 2308:         $bookshash = $settings->{$type};
 2309:     }
 2310:     my %ordered;
 2311:     if (ref($bookshash) eq 'HASH') {
 2312:         foreach my $item (keys(%{$bookshash})) {
 2313:             if (ref($bookshash->{$item}) eq 'HASH') {
 2314:                 my $num = $bookshash->{$item}{'order'};
 2315:                 $ordered{$num} = $item;
 2316:             }
 2317:         }
 2318:     }
 2319:     my $confname = $dom.'-domainconfig';
 2320:     my $switchserver = &check_switchserver($dom,$confname);
 2321:     my $maxnum = scalar(keys(%ordered));
 2322:     my $datatable;
 2323:     if (keys(%ordered)) {
 2324:         my @items = sort { $a <=> $b } keys(%ordered);
 2325:         for (my $i=0; $i<@items; $i++) {
 2326:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2327:             my $key = $ordered{$items[$i]};
 2328:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2329:             my $coursetitle = $coursehash{'description'};
 2330:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2331:             if (ref($bookshash->{$key}) eq 'HASH') {
 2332:                 $subject = $bookshash->{$key}->{'subject'};
 2333:                 $title = $bookshash->{$key}->{'title'};
 2334:                 if ($type eq 'textbooks') {
 2335:                     $publisher = $bookshash->{$key}->{'publisher'};
 2336:                     $author = $bookshash->{$key}->{'author'};
 2337:                     $image = $bookshash->{$key}->{'image'};
 2338:                     if ($image ne '') {
 2339:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2340:                         my $imagethumb = "$path/tn-".$imagefile;
 2341:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2342:                     }
 2343:                 }
 2344:             }
 2345:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2346:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2347:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2348:             for (my $k=0; $k<=$maxnum; $k++) {
 2349:                 my $vpos = $k+1;
 2350:                 my $selstr;
 2351:                 if ($k == $i) {
 2352:                     $selstr = ' selected="selected" ';
 2353:                 }
 2354:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2355:             }
 2356:             $datatable .= '</select>'.('&nbsp;'x2).
 2357:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2358:                 &mt('Delete?').'</label></span></td>'.
 2359:                 '<td colspan="2">'.
 2360:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2361:                 ('&nbsp;'x2).
 2362:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2363:             if ($type eq 'textbooks') {
 2364:                 $datatable .= ('&nbsp;'x2).
 2365:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2366:                               ('&nbsp;'x2).
 2367:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2368:                               ('&nbsp;'x2).
 2369:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2370:                 if ($image) {
 2371:                     $datatable .= '<span class="LC_nobreak">'.
 2372:                                   $imgsrc.
 2373:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2374:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2375:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2376:                 }
 2377:                 if ($switchserver) {
 2378:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2379:                 } else {
 2380:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2381:                 }
 2382:             }
 2383:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2384:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2385:                           $coursetitle.'</span></td></tr>'."\n";
 2386:             $itemcount ++;
 2387:         }
 2388:     }
 2389:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2390:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2391:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2392:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2393:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2394:     for (my $k=0; $k<$maxnum+1; $k++) {
 2395:         my $vpos = $k+1;
 2396:         my $selstr;
 2397:         if ($k == $maxnum) {
 2398:             $selstr = ' selected="selected" ';
 2399:         }
 2400:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2401:     }
 2402:     $datatable .= '</select>&nbsp;'."\n".
 2403:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</span></td>'."\n".
 2404:                   '<td colspan="2">'.
 2405:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2406:                   ('&nbsp;'x2).
 2407:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2408:                   ('&nbsp;'x2);
 2409:     if ($type eq 'textbooks') {
 2410:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2411:                       ('&nbsp;'x2).
 2412:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2413:                       ('&nbsp;'x2).
 2414:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2415:         if ($switchserver) {
 2416:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2417:         } else {
 2418:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2419:         }
 2420:         $datatable .= '</span>'."\n";
 2421:     }
 2422:     $datatable .= '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2423:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2424:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2425:                   &Apache::loncommon::selectcourse_link
 2426:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course').
 2427:                   '</span></td>'."\n".
 2428:                   '</tr>'."\n";
 2429:     $itemcount ++;
 2430:     return $datatable;
 2431: }
 2432: 
 2433: sub textbookcourses_javascript {
 2434:     my ($settings) = @_;
 2435:     return unless(ref($settings) eq 'HASH');
 2436:     my (%ordered,%total,%jstext);
 2437:     foreach my $type ('textbooks','templates') {
 2438:         $total{$type} = 0;
 2439:         if (ref($settings->{$type}) eq 'HASH') {
 2440:             foreach my $item (keys(%{$settings->{$type}})) {
 2441:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2442:                     my $num = $settings->{$type}->{$item}{'order'};
 2443:                     $ordered{$type}{$num} = $item;
 2444:                 }
 2445:             }
 2446:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2447:         }
 2448:         my @jsarray = ();
 2449:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2450:             push(@jsarray,$ordered{$type}{$item});
 2451:         }
 2452:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2453:     }
 2454:     return <<"ENDSCRIPT";
 2455: <script type="text/javascript">
 2456: // <![CDATA[
 2457: function reorderBooks(form,item,caller) {
 2458:     var changedVal;
 2459: $jstext{'textbooks'};
 2460: $jstext{'templates'};
 2461:     var newpos;
 2462:     var maxh;
 2463:     if (caller == 'textbooks') {  
 2464:         newpos = 'textbooks_addbook_pos';
 2465:         maxh = 1 + $total{'textbooks'};
 2466:     } else {
 2467:         newpos = 'templates_addbook_pos';
 2468:         maxh = 1 + $total{'templates'};
 2469:     }
 2470:     var current = new Array;
 2471:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2472:     if (item == newpos) {
 2473:         changedVal = newitemVal;
 2474:     } else {
 2475:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2476:         current[newitemVal] = newpos;
 2477:     }
 2478:     if (caller == 'textbooks') {
 2479:         for (var i=0; i<textbooks.length; i++) {
 2480:             var elementName = 'textbooks_'+textbooks[i];
 2481:             if (elementName != item) {
 2482:                 if (form.elements[elementName]) {
 2483:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2484:                     current[currVal] = elementName;
 2485:                 }
 2486:             }
 2487:         }
 2488:     }
 2489:     if (caller == 'templates') {
 2490:         for (var i=0; i<templates.length; i++) {
 2491:             var elementName = 'templates_'+templates[i];
 2492:             if (elementName != item) {
 2493:                 if (form.elements[elementName]) {
 2494:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2495:                     current[currVal] = elementName;
 2496:                 }
 2497:             }
 2498:         }
 2499:     }
 2500:     var oldVal;
 2501:     for (var j=0; j<maxh; j++) {
 2502:         if (current[j] == undefined) {
 2503:             oldVal = j;
 2504:         }
 2505:     }
 2506:     if (oldVal < changedVal) {
 2507:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2508:            var elementName = current[k];
 2509:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2510:         }
 2511:     } else {
 2512:         for (var k=changedVal; k<oldVal; k++) {
 2513:             var elementName = current[k];
 2514:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2515:         }
 2516:     }
 2517:     return;
 2518: }
 2519: 
 2520: // ]]>
 2521: </script>
 2522: 
 2523: ENDSCRIPT
 2524: }
 2525: 
 2526: sub ltitools_javascript {
 2527:     my ($settings) = @_;
 2528:     return unless(ref($settings) eq 'HASH');
 2529:     my (%ordered,$total,%jstext);
 2530:     $total = 0;
 2531:     foreach my $item (keys(%{$settings})) {
 2532:         if (ref($settings->{$item}) eq 'HASH') {
 2533:             my $num = $settings->{$item}{'order'};
 2534:             $ordered{$num} = $item;
 2535:         }
 2536:     }
 2537:     $total = scalar(keys(%{$settings}));
 2538:     my @jsarray = ();
 2539:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2540:         push(@jsarray,$ordered{$item});
 2541:     }
 2542:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 2543:     return <<"ENDSCRIPT";
 2544: <script type="text/javascript">
 2545: // <![CDATA[
 2546: function reorderLTI(form,item) {
 2547:     var changedVal;
 2548: $jstext
 2549:     var newpos = 'ltitools_add_pos';
 2550:     var maxh = 1 + $total;
 2551:     var current = new Array;
 2552:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2553:     if (item == newpos) {
 2554:         changedVal = newitemVal;
 2555:     } else {
 2556:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2557:         current[newitemVal] = newpos;
 2558:     }
 2559:     for (var i=0; i<ltitools.length; i++) {
 2560:         var elementName = 'ltitools_'+ltitools[i];
 2561:         if (elementName != item) {
 2562:             if (form.elements[elementName]) {
 2563:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2564:                 current[currVal] = elementName;
 2565:             }
 2566:         }
 2567:     }
 2568:     var oldVal;
 2569:     for (var j=0; j<maxh; j++) {
 2570:         if (current[j] == undefined) {
 2571:             oldVal = j;
 2572:         }
 2573:     }
 2574:     if (oldVal < changedVal) {
 2575:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2576:            var elementName = current[k];
 2577:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2578:         }
 2579:     } else {
 2580:         for (var k=changedVal; k<oldVal; k++) {
 2581:             var elementName = current[k];
 2582:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2583:         }
 2584:     }
 2585:     return;
 2586: }
 2587: 
 2588: // ]]>
 2589: </script>
 2590: 
 2591: ENDSCRIPT
 2592: }
 2593: 
 2594: sub print_autoenroll {
 2595:     my ($dom,$settings,$rowtotal) = @_;
 2596:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2597:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,$failsafe);
 2598:     if (ref($settings) eq 'HASH') {
 2599:         if (exists($settings->{'run'})) {
 2600:             if ($settings->{'run'} eq '0') {
 2601:                 $runoff = ' checked="checked" ';
 2602:                 $runon = ' ';
 2603:             } else {
 2604:                 $runon = ' checked="checked" ';
 2605:                 $runoff = ' ';
 2606:             }
 2607:         } else {
 2608:             if ($autorun) {
 2609:                 $runon = ' checked="checked" ';
 2610:                 $runoff = ' ';
 2611:             } else {
 2612:                 $runoff = ' checked="checked" ';
 2613:                 $runon = ' ';
 2614:             }
 2615:         }
 2616:         if (exists($settings->{'co-owners'})) {
 2617:             if ($settings->{'co-owners'} eq '0') {
 2618:                 $coownersoff = ' checked="checked" ';
 2619:                 $coownerson = ' ';
 2620:             } else {
 2621:                 $coownerson = ' checked="checked" ';
 2622:                 $coownersoff = ' ';
 2623:             }
 2624:         } else {
 2625:             $coownersoff = ' checked="checked" ';
 2626:             $coownerson = ' ';
 2627:         }
 2628:         if (exists($settings->{'sender_domain'})) {
 2629:             $defdom = $settings->{'sender_domain'};
 2630:         }
 2631:         if (exists($settings->{'autofailsafe'})) {
 2632:             $failsafe = $settings->{'autofailsafe'};
 2633:         }
 2634:     } else {
 2635:         if ($autorun) {
 2636:             $runon = ' checked="checked" ';
 2637:             $runoff = ' ';
 2638:         } else {
 2639:             $runoff = ' checked="checked" ';
 2640:             $runon = ' ';
 2641:         }
 2642:     }
 2643:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2644:     my $notif_sender;
 2645:     if (ref($settings) eq 'HASH') {
 2646:         $notif_sender = $settings->{'sender_uname'};
 2647:     }
 2648:     my $datatable='<tr class="LC_odd_row">'.
 2649:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2650:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2651:                   '<input type="radio" name="autoenroll_run"'.
 2652:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2653:                   '<label><input type="radio" name="autoenroll_run"'.
 2654:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2655:                   '</tr><tr>'.
 2656:                   '<td>'.&mt('Notification messages - sender').
 2657:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2658:                   &mt('username').':&nbsp;'.
 2659:                   '<input type="text" name="sender_uname" value="'.
 2660:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2661:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2662:                   '<tr class="LC_odd_row">'.
 2663:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2664:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2665:                   '<input type="radio" name="autoassign_coowners"'.
 2666:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2667:                   '<label><input type="radio" name="autoassign_coowners"'.
 2668:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2669:                   '</tr><tr>'.
 2670:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 2671:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2672:                   '<input type="text" name="autoenroll_failsafe"'.
 2673:                   ' value="'.$failsafe.'" size="4" /></span></td></tr>';
 2674:     $$rowtotal += 4;
 2675:     return $datatable;
 2676: }
 2677: 
 2678: sub print_autoupdate {
 2679:     my ($position,$dom,$settings,$rowtotal) = @_;
 2680:     my $datatable;
 2681:     if ($position eq 'top') {
 2682:         my $updateon = ' ';
 2683:         my $updateoff = ' checked="checked" ';
 2684:         my $classlistson = ' ';
 2685:         my $classlistsoff = ' checked="checked" ';
 2686:         if (ref($settings) eq 'HASH') {
 2687:             if ($settings->{'run'} eq '1') {
 2688:                 $updateon = $updateoff;
 2689:                 $updateoff = ' ';
 2690:             }
 2691:             if ($settings->{'classlists'} eq '1') {
 2692:                 $classlistson = $classlistsoff;
 2693:                 $classlistsoff = ' ';
 2694:             }
 2695:         }
 2696:         my %title = (
 2697:                    run => 'Auto-update active?',
 2698:                    classlists => 'Update information in classlists?',
 2699:                     );
 2700:         $datatable = '<tr class="LC_odd_row">'. 
 2701:                   '<td>'.&mt($title{'run'}).'</td>'.
 2702:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2703:                   '<input type="radio" name="autoupdate_run"'.
 2704:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2705:                   '<label><input type="radio" name="autoupdate_run"'.
 2706:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2707:                   '</tr><tr>'.
 2708:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2709:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2710:                   '<label><input type="radio" name="classlists"'.
 2711:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2712:                   '<label><input type="radio" name="classlists"'.
 2713:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2714:                   '</tr>';
 2715:         $$rowtotal += 2;
 2716:     } elsif ($position eq 'middle') {
 2717:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2718:         my $numinrow = 3;
 2719:         my $locknamesettings;
 2720:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2721:                                      $dom,$numinrow,$othertitle,
 2722:                                     'lockablenames',$rowtotal);
 2723:         $$rowtotal ++;
 2724:     } else {
 2725:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2726:         my @fields = ('lastname','firstname','middlename','generation',
 2727:                       'permanentemail','id');
 2728:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2729:         my $numrows = 0;
 2730:         if (ref($types) eq 'ARRAY') {
 2731:             if (@{$types} > 0) {
 2732:                 $datatable = 
 2733:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2734:                                          \@fields,$types,\$numrows);
 2735:                     $$rowtotal += @{$types}; 
 2736:             }
 2737:         }
 2738:         $datatable .= 
 2739:             &usertype_update_row($settings,{'default' => $othertitle},
 2740:                                  \%fieldtitles,\@fields,['default'],
 2741:                                  \$numrows);
 2742:         $$rowtotal ++;     
 2743:     }
 2744:     return $datatable;
 2745: }
 2746: 
 2747: sub print_autocreate {
 2748:     my ($dom,$settings,$rowtotal) = @_;
 2749:     my (%createon,%createoff,%currhash);
 2750:     my @types = ('xml','req');
 2751:     if (ref($settings) eq 'HASH') {
 2752:         foreach my $item (@types) {
 2753:             $createoff{$item} = ' checked="checked" ';
 2754:             $createon{$item} = ' ';
 2755:             if (exists($settings->{$item})) {
 2756:                 if ($settings->{$item}) {
 2757:                     $createon{$item} = ' checked="checked" ';
 2758:                     $createoff{$item} = ' ';
 2759:                 }
 2760:             }
 2761:         }
 2762:         if ($settings->{'xmldc'} ne '') {
 2763:             $currhash{$settings->{'xmldc'}} = 1;
 2764:         }
 2765:     } else {
 2766:         foreach my $item (@types) {
 2767:             $createoff{$item} = ' checked="checked" ';
 2768:             $createon{$item} = ' ';
 2769:         }
 2770:     }
 2771:     $$rowtotal += 2;
 2772:     my $numinrow = 2;
 2773:     my $datatable='<tr class="LC_odd_row">'.
 2774:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2775:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2776:                   '<input type="radio" name="autocreate_xml"'.
 2777:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2778:                   '<label><input type="radio" name="autocreate_xml"'.
 2779:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2780:                   '</td></tr><tr>'.
 2781:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2782:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2783:                   '<input type="radio" name="autocreate_req"'.
 2784:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2785:                   '<label><input type="radio" name="autocreate_req"'.
 2786:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2787:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2788:                                                    'autocreate_xmldc',%currhash);
 2789:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 2790:     if ($numdc > 1) {
 2791:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 2792:                       '</td><td class="LC_left_item">';
 2793:     } else {
 2794:         $datatable .= &mt('Course creation processed as:').
 2795:                       '</td><td class="LC_right_item">';
 2796:     }
 2797:     $datatable .= $dctable.'</td></tr>';
 2798:     $$rowtotal += $rows;
 2799:     return $datatable;
 2800: }
 2801: 
 2802: sub print_directorysrch {
 2803:     my ($position,$dom,$settings,$rowtotal) = @_;
 2804:     my $datatable;
 2805:     if ($position eq 'top') {
 2806:         my $instsrchon = ' ';
 2807:         my $instsrchoff = ' checked="checked" ';
 2808:         my ($exacton,$containson,$beginson);
 2809:         my $instlocalon = ' ';
 2810:         my $instlocaloff = ' checked="checked" ';
 2811:         if (ref($settings) eq 'HASH') {
 2812:             if ($settings->{'available'} eq '1') {
 2813:                 $instsrchon = $instsrchoff;
 2814:                 $instsrchoff = ' ';
 2815:             }
 2816:             if ($settings->{'localonly'} eq '1') {
 2817:                 $instlocalon = $instlocaloff;
 2818:                 $instlocaloff = ' ';
 2819:             }
 2820:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2821:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 2822:                     if ($type eq 'exact') {
 2823:                         $exacton = ' checked="checked" ';
 2824:                     } elsif ($type eq 'contains') {
 2825:                         $containson = ' checked="checked" ';
 2826:                     } elsif ($type eq 'begins') {
 2827:                         $beginson = ' checked="checked" ';
 2828:                     }
 2829:                 }
 2830:             } else {
 2831:                 if ($settings->{'searchtypes'} eq 'exact') {
 2832:                     $exacton = ' checked="checked" ';
 2833:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 2834:                     $containson = ' checked="checked" ';
 2835:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 2836:                     $exacton = ' checked="checked" ';
 2837:                     $containson = ' checked="checked" ';
 2838:                 }
 2839:             }
 2840:         }
 2841:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2842:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2843: 
 2844:         my $numinrow = 4;
 2845:         my $cansrchrow = 0;
 2846:         $datatable='<tr class="LC_odd_row">'.
 2847:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 2848:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2849:                    '<input type="radio" name="dirsrch_available"'.
 2850:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2851:                    '<label><input type="radio" name="dirsrch_available"'.
 2852:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2853:                    '</tr><tr>'.
 2854:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 2855:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2856:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 2857:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2858:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 2859:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2860:                    '</tr>';
 2861:         $$rowtotal += 2;
 2862:         if (ref($usertypes) eq 'HASH') {
 2863:             if (keys(%{$usertypes}) > 0) {
 2864:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2865:                                              $numinrow,$othertitle,'cansearch',
 2866:                                              $rowtotal);
 2867:                 $cansrchrow = 1;
 2868:             }
 2869:         }
 2870:         if ($cansrchrow) {
 2871:             $$rowtotal ++;
 2872:             $datatable .= '<tr>';
 2873:         } else {
 2874:             $datatable .= '<tr class="LC_odd_row">';
 2875:         }
 2876:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2877:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2878:         foreach my $title (@{$titleorder}) {
 2879:             if (defined($searchtitles->{$title})) {
 2880:                 my $check = ' ';
 2881:                 if (ref($settings) eq 'HASH') {
 2882:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2883:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2884:                             $check = ' checked="checked" ';
 2885:                         }
 2886:                     }
 2887:                 }
 2888:                 $datatable .= '<td class="LC_left_item">'.
 2889:                               '<span class="LC_nobreak"><label>'.
 2890:                               '<input type="checkbox" name="searchby" '.
 2891:                               'value="'.$title.'"'.$check.'/>'.
 2892:                               $searchtitles->{$title}.'</label></span></td>';
 2893:             }
 2894:         }
 2895:         $datatable .= '</tr></table></td></tr>';
 2896:         $$rowtotal ++;
 2897:         if ($cansrchrow) {
 2898:             $datatable .= '<tr class="LC_odd_row">';
 2899:         } else {
 2900:             $datatable .= '<tr>';
 2901:         }
 2902:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2903:                       '<td class="LC_left_item" colspan="2">'.
 2904:                       '<span class="LC_nobreak"><label>'.
 2905:                       '<input type="checkbox" name="searchtypes" '.
 2906:                       $exacton.' value="exact" />'.&mt('Exact match').
 2907:                       '</label>&nbsp;'.
 2908:                       '<label><input type="checkbox" name="searchtypes" '.
 2909:                       $beginson.' value="begins" />'.&mt('Begins with').
 2910:                       '</label>&nbsp;'.
 2911:                       '<label><input type="checkbox" name="searchtypes" '.
 2912:                       $containson.' value="contains" />'.&mt('Contains').
 2913:                       '</label></span></td></tr>';
 2914:         $$rowtotal ++;
 2915:     } else {
 2916:         my $domsrchon = ' checked="checked" ';
 2917:         my $domsrchoff = ' ';
 2918:         my $domlocalon = ' ';
 2919:         my $domlocaloff = ' checked="checked" ';
 2920:         if (ref($settings) eq 'HASH') {
 2921:             if ($settings->{'lclocalonly'} eq '1') {
 2922:                 $domlocalon = $domlocaloff;
 2923:                 $domlocaloff = ' ';
 2924:             }
 2925:             if ($settings->{'lcavailable'} eq '0') {
 2926:                 $domsrchoff = $domsrchon;
 2927:                 $domsrchon = ' ';
 2928:             }
 2929:         }
 2930:         $datatable='<tr class="LC_odd_row">'.
 2931:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 2932:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2933:                       '<input type="radio" name="dirsrch_domavailable"'.
 2934:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2935:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 2936:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2937:                       '</tr><tr>'.
 2938:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 2939:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2940:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 2941:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2942:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 2943:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2944:                       '</tr>';
 2945:         $$rowtotal += 2;
 2946:     }
 2947:     return $datatable;
 2948: }
 2949: 
 2950: sub print_contacts {
 2951:     my ($position,$dom,$settings,$rowtotal) = @_;
 2952:     my $datatable;
 2953:     my @contacts = ('adminemail','supportemail');
 2954:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 2955:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings);
 2956:     if ($position eq 'top') {
 2957:         if (ref($settings) eq 'HASH') {
 2958:             foreach my $item (@contacts) {
 2959:                 if (exists($settings->{$item})) {
 2960:                     $to{$item} = $settings->{$item};
 2961:                 }
 2962:             }
 2963:         }
 2964:     } elsif ($position eq 'middle') {
 2965:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 2966:                      'updatesmail','idconflictsmail','hostipmail');
 2967:         foreach my $type (@mailings) {
 2968:             $otheremails{$type} = '';
 2969:         }
 2970:     } else {
 2971:         @mailings = ('helpdeskmail','otherdomsmail');
 2972:         foreach my $type (@mailings) {
 2973:             $otheremails{$type} = '';
 2974:         }
 2975:         $bccemails{'helpdeskmail'} = '';
 2976:         $bccemails{'otherdomsmail'} = '';
 2977:         $includestr{'helpdeskmail'} = '';
 2978:         $includestr{'otherdomsmail'} = '';
 2979:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 2980:     }
 2981:     if (ref($settings) eq 'HASH') {
 2982:         unless ($position eq 'top') {
 2983:             foreach my $type (@mailings) {
 2984:                 if (exists($settings->{$type})) {
 2985:                     if (ref($settings->{$type}) eq 'HASH') {
 2986:                         foreach my $item (@contacts) {
 2987:                             if ($settings->{$type}{$item}) {
 2988:                                 $checked{$type}{$item} = ' checked="checked" ';
 2989:                             }
 2990:                         }
 2991:                         $otheremails{$type} = $settings->{$type}{'others'};
 2992:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 2993:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 2994:                             if ($settings->{$type}{'include'} ne '') {
 2995:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 2996:                                 $includestr{$type} = &unescape($includestr{$type});
 2997:                             }
 2998:                         }
 2999:                     }
 3000:                 } elsif ($type eq 'lonstatusmail') {
 3001:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3002:                 }
 3003:             }
 3004:         }
 3005:         if ($position eq 'bottom') {
 3006:             foreach my $type (@mailings) {
 3007:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 3008:                 if ($settings->{$type}{'include'} ne '') {
 3009:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3010:                     $includestr{$type} = &unescape($includestr{$type});
 3011:                 }
 3012:             }
 3013:             if (ref($settings->{'helpform'}) eq 'HASH') {
 3014:                 if (ref($fields) eq 'ARRAY') {
 3015:                     foreach my $field (@{$fields}) {
 3016:                         $currfield{$field} = $settings->{'helpform'}{$field};
 3017:                     }
 3018:                 }
 3019:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 3020:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 3021:                 } else {
 3022:                     $maxsize = '1.0';
 3023:                 }
 3024:             } else {
 3025:                 if (ref($fields) eq 'ARRAY') {
 3026:                     foreach my $field (@{$fields}) {
 3027:                         $currfield{$field} = 'yes';
 3028:                     }
 3029:                 }
 3030:                 $maxsize = '1.0';
 3031:             }
 3032:         }
 3033:     } else {
 3034:         if ($position eq 'top') {
 3035:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 3036:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 3037:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 3038:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 3039:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3040:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 3041:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 3042:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 3043:             $checked{'hostipmail'}{'adminemail'} = ' checked="checked" ';
 3044:         } elsif ($position eq 'bottom') {
 3045:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 3046:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 3047:             if (ref($fields) eq 'ARRAY') {
 3048:                 foreach my $field (@{$fields}) {
 3049:                     $currfield{$field} = 'yes';
 3050:                 }
 3051:             }
 3052:             $maxsize = '1.0';
 3053:         }
 3054:     }
 3055:     my ($titles,$short_titles) = &contact_titles();
 3056:     my $rownum = 0;
 3057:     my $css_class;
 3058:     if ($position eq 'top') {
 3059:         foreach my $item (@contacts) {
 3060:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3061:             $datatable .= '<tr'.$css_class.'>'. 
 3062:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 3063:                           '</span></td><td class="LC_right_item">'.
 3064:                           '<input type="text" name="'.$item.'" value="'.
 3065:                           $to{$item}.'" /></td></tr>';
 3066:             $rownum ++;
 3067:         }
 3068:     } elsif ($position eq 'bottom') {
 3069:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3070:         $datatable .= '<tr'.$css_class.'>'.
 3071:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 3072:                       &mt('(e-mail, subject, and description always shown)').
 3073:                       '</td><td class="LC_left_item">';
 3074:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 3075:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 3076:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 3077:             foreach my $field (@{$fields}) {
 3078:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 3079:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 3080:                     $datatable .= ' '.&mt('(logged-in users)');
 3081:                 }
 3082:                 $datatable .='</td><td>';
 3083:                 my $clickaction;
 3084:                 if ($field eq 'screenshot') {
 3085:                     $clickaction = ' onclick="screenshotSize(this);"';
 3086:                 }
 3087:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 3088:                     foreach my $option (@{$possoptions->{$field}}) {
 3089:                         my $checked;
 3090:                         if ($currfield{$field} eq $option) {
 3091:                             $checked = ' checked="checked"';
 3092:                         }
 3093:                         $datatable .= '<span class="LC_nobreak"><label>'.
 3094:                                       '<input type="radio" name="helpform_'.$field.'" '.
 3095:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 3096:                                       '</label></span>'.('&nbsp;'x2);
 3097:                     }
 3098:                 }
 3099:                 if ($field eq 'screenshot') {
 3100:                     my $display;
 3101:                     if ($currfield{$field} eq 'no') {
 3102:                         $display = ' style="display:none"';
 3103:                     }
 3104:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.'>'.
 3105:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 3106:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 3107:                 }
 3108:                 $datatable .= '</td></tr>';
 3109:             }
 3110:             $datatable .= '</table>';
 3111:         }
 3112:         $datatable .= '</td></tr>'."\n";
 3113:         $rownum ++;
 3114:     }
 3115:     unless ($position eq 'top') {
 3116:         foreach my $type (@mailings) {
 3117:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3118:             $datatable .= '<tr'.$css_class.'>'.
 3119:                           '<td><span class="LC_nobreak">'.
 3120:                           $titles->{$type}.': </span></td>'.
 3121:                           '<td class="LC_left_item">';
 3122:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3123:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 3124:             }
 3125:             $datatable .= '<span class="LC_nobreak">';
 3126:             foreach my $item (@contacts) {
 3127:                 $datatable .= '<label>'.
 3128:                               '<input type="checkbox" name="'.$type.'"'.
 3129:                               $checked{$type}{$item}.
 3130:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 3131:                               '</label>&nbsp;';
 3132:             }
 3133:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3134:                           '<input type="text" name="'.$type.'_others" '.
 3135:                           'value="'.$otheremails{$type}.'"  />';
 3136:             my %locchecked;
 3137:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3138:                 foreach my $loc ('s','b') {
 3139:                     if ($includeloc{$type} eq $loc) {
 3140:                         $locchecked{$loc} = ' checked="checked"';
 3141:                         last;
 3142:                     }
 3143:                 }
 3144:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3145:                               '<input type="text" name="'.$type.'_bcc" '.
 3146:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 3147:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3148:                               &mt('Text automatically added to e-mail:').' '.
 3149:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br />'.
 3150:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3151:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3152:                               ('&nbsp;'x2).
 3153:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3154:                               '</span></fieldset>';
 3155:             }
 3156:             $datatable .= '</td></tr>'."\n";
 3157:             $rownum ++;
 3158:         }
 3159:     }
 3160:     if ($position eq 'middle') {
 3161:         my %choices;
 3162:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 3163:                                        &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3164:                                        &mt('LON-CAPA core group - MSU'),600,500));
 3165:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 3166:                                         &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3167:                                         &mt('LON-CAPA core group - MSU'),600,500));
 3168:         my @toggles = ('reporterrors','reportupdates');
 3169:         my %defaultchecked = ('reporterrors'  => 'on',
 3170:                               'reportupdates' => 'on');
 3171:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3172:                                                    \%choices,$rownum);
 3173:         $datatable .= $reports;
 3174:     } elsif ($position eq 'bottom') {
 3175:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3176:         my (@posstypes,%usertypeshash);
 3177:         if (ref($types) eq 'ARRAY') {
 3178:             @posstypes = @{$types};
 3179:         }
 3180:         if (@posstypes) {
 3181:             if (ref($usertypes) eq 'HASH') {
 3182:                 %usertypeshash = %{$usertypes};
 3183:             }
 3184:             my @overridden;
 3185:             my $numinrow = 4;
 3186:             if (ref($settings) eq 'HASH') {
 3187:                 if (ref($settings->{'overrides'}) eq 'HASH') {
 3188:                     foreach my $key (sort(keys(%{$settings->{'overrides'}}))) {
 3189:                         if (ref($settings->{'overrides'}{$key}) eq 'HASH') {
 3190:                             push(@overridden,$key);
 3191:                             foreach my $item (@contacts) {
 3192:                                 if ($settings->{'overrides'}{$key}{$item}) {
 3193:                                     $checked{'override_'.$key}{$item} = ' checked="checked" ';
 3194:                                 }
 3195:                             }
 3196:                             $otheremails{'override_'.$key} = $settings->{'overrides'}{$key}{'others'};
 3197:                             $bccemails{'override_'.$key} = $settings->{'overrides'}{$key}{'bcc'};
 3198:                             $includeloc{'override_'.$key} = '';
 3199:                             $includestr{'override_'.$key} = '';
 3200:                             if ($settings->{'overrides'}{$key}{'include'} ne '') {
 3201:                                 ($includeloc{'override_'.$key},$includestr{'override_'.$key}) =
 3202:                                     split(/:/,$settings->{'overrides'}{$key}{'include'},2);
 3203:                                 $includestr{'override_'.$key} = &unescape($includestr{'override_'.$key});
 3204:                             }
 3205:                         }
 3206:                     }
 3207:                 }
 3208:             }
 3209:             my $customclass = 'LC_helpdesk_override';
 3210:             my $optionsprefix = 'LC_options_helpdesk_';
 3211: 
 3212:             my $onclicktypes = "toggleHelpdeskRow(this.form,'overrides','$customclass','$optionsprefix');";
 3213: 
 3214:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 3215:                                          $numinrow,$othertitle,'overrides',
 3216:                                          \$rownum,$onclicktypes,$customclass);
 3217:             $rownum ++;
 3218:             $usertypeshash{'default'} = $othertitle;
 3219:             foreach my $status (@posstypes) {
 3220:                 my $css_class;
 3221:                 if ($rownum%2) {
 3222:                     $css_class = 'LC_odd_row ';
 3223:                 }
 3224:                 $css_class .= $customclass;
 3225:                 my $rowid = $optionsprefix.$status;
 3226:                 my $hidden = 1;
 3227:                 my $currstyle = 'display:none';
 3228:                 if (grep(/^\Q$status\E$/,@overridden)) {
 3229:                     $currstyle = 'display:table-row';
 3230:                     $hidden = 0;
 3231:                 }
 3232:                 my $key = 'override_'.$status;
 3233:                 $datatable .= &overridden_helpdesk($checked{$key},$otheremails{$key},$bccemails{$key},
 3234:                                                   $includeloc{$key},$includestr{$key},$status,$rowid,
 3235:                                                   $usertypeshash{$status},$css_class,$currstyle,
 3236:                                                   \@contacts,$short_titles);
 3237:                 unless ($hidden) {
 3238:                     $rownum ++;
 3239:                 }
 3240:             }
 3241:         }
 3242:     }
 3243:     $$rowtotal += $rownum;
 3244:     return $datatable;
 3245: }
 3246: 
 3247: sub overridden_helpdesk {
 3248:     my ($checked,$otheremails,$bccemails,$includeloc,$includestr,$type,$rowid,
 3249:         $typetitle,$css_class,$rowstyle,$contacts,$short_titles) = @_;
 3250:     my $class = 'LC_left_item';
 3251:     if ($css_class) {
 3252:         $css_class = ' class="'.$css_class.'"';
 3253:     }
 3254:     if ($rowid) {
 3255:         $rowid = ' id="'.$rowid.'"';
 3256:     }
 3257:     if ($rowstyle) {
 3258:         $rowstyle = ' style="'.$rowstyle.'"';
 3259:     }
 3260:     my ($output,$description);
 3261:     $description = &mt('Helpdesk requests from: [_1] in this domain (overrides default)',"<b>$typetitle</b>");
 3262:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 3263:               "<td>$description</td>\n".
 3264:               '<td class="'.$class.'" colspan="2">'.
 3265:               '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>'.
 3266:               '<span class="LC_nobreak">';
 3267:     if (ref($contacts) eq 'ARRAY') {
 3268:         foreach my $item (@{$contacts}) {
 3269:             my $check;
 3270:             if (ref($checked) eq 'HASH') {
 3271:                $check = $checked->{$item};
 3272:             }
 3273:             my $title;
 3274:             if (ref($short_titles) eq 'HASH') {
 3275:                 $title = $short_titles->{$item};
 3276:             }
 3277:             $output .= '<label>'.
 3278:                        '<input type="checkbox" name="override_'.$type.'"'.$check.
 3279:                        ' value="'.$item.'" />'.$title.'</label>&nbsp;';
 3280:         }
 3281:     }
 3282:     $output .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3283:                '<input type="text" name="override_'.$type.'_others" '.
 3284:                'value="'.$otheremails.'"  />';
 3285:     my %locchecked;
 3286:     foreach my $loc ('s','b') {
 3287:         if ($includeloc eq $loc) {
 3288:             $locchecked{$loc} = ' checked="checked"';
 3289:             last;
 3290:         }
 3291:     }
 3292:     $output .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3293:                '<input type="text" name="override_'.$type.'_bcc" '.
 3294:                'value="'.$bccemails.'"  /></fieldset>'.
 3295:                '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3296:                &mt('Text automatically added to e-mail:').' '.
 3297:                '<input type="text" name="override_'.$type.'_includestr" value="'.$includestr.'" /><br />'.
 3298:                '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3299:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3300:                ('&nbsp;'x2).
 3301:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3302:                '</span></fieldset>'.
 3303:                '</td></tr>'."\n";
 3304:     return $output;
 3305: }
 3306: 
 3307: sub contacts_javascript {
 3308:     return <<"ENDSCRIPT";
 3309: 
 3310: <script type="text/javascript">
 3311: // <![CDATA[
 3312: 
 3313: function screenshotSize(field) {
 3314:     if (document.getElementById('help_screenshotsize')) {
 3315:         if (field.value == 'no') {
 3316:             document.getElementById('help_screenshotsize').style.display="none";
 3317:         } else {
 3318:             document.getElementById('help_screenshotsize').style.display="";
 3319:         }
 3320:     }
 3321:     return;
 3322: }
 3323: 
 3324: function toggleHelpdeskRow(form,checkbox,target,prefix,docount) {
 3325:     if (form.elements[checkbox].length != undefined) {
 3326:         var count = 0;
 3327:         if (docount) {
 3328:             for (var i=0; i<form.elements[checkbox].length; i++) {
 3329:                 if (form.elements[checkbox][i].checked) {
 3330:                     count ++;
 3331:                 }
 3332:             }
 3333:         }
 3334:         for (var i=0; i<form.elements[checkbox].length; i++) {
 3335:             var type = form.elements[checkbox][i].value;
 3336:             if (document.getElementById(prefix+type)) {
 3337:                 if (form.elements[checkbox][i].checked) {
 3338:                     document.getElementById(prefix+type).style.display = 'table-row';
 3339:                     if (count % 2 == 1) {
 3340:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 3341:                     } else {
 3342:                         document.getElementById(prefix+type).className = target;
 3343:                     }
 3344:                     count ++;
 3345:                 } else {
 3346:                     document.getElementById(prefix+type).style.display = 'none';
 3347:                 }
 3348:             }
 3349:         }
 3350:     }
 3351:     return;
 3352: }
 3353: 
 3354: // ]]>
 3355: </script>
 3356: 
 3357: ENDSCRIPT
 3358: }
 3359: 
 3360: sub print_helpsettings {
 3361:     my ($position,$dom,$settings,$rowtotal) = @_;
 3362:     my $confname = $dom.'-domainconfig';
 3363:     my $formname = 'display';
 3364:     my ($datatable,$itemcount);
 3365:     if ($position eq 'top') {
 3366:         $itemcount = 1;
 3367:         my (%choices,%defaultchecked,@toggles);
 3368:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 3369:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 3370:                                      &mt('LON-CAPA bug tracker'),600,500));
 3371:         %defaultchecked = ('submitbugs' => 'on');
 3372:         @toggles = ('submitbugs');
 3373:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3374:                                                      \%choices,$itemcount);
 3375:         $$rowtotal ++;
 3376:     } else {
 3377:         my $css_class;
 3378:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 3379:         my (%customroles,%ordered,%current);
 3380:         if (ref($settings) eq 'HASH') {
 3381:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 3382:                 %current = %{$settings->{'adhoc'}};
 3383:             }
 3384:         }
 3385:         my $count = 0;
 3386:         foreach my $key (sort(keys(%existing))) {
 3387:             if ($key=~/^rolesdef\_(\w+)$/) {
 3388:                 my $rolename = $1;
 3389:                 my (%privs,$order);
 3390:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 3391:                 $customroles{$rolename} = \%privs;
 3392:                 if (ref($current{$rolename}) eq 'HASH') {
 3393:                     $order = $current{$rolename}{'order'};
 3394:                 }
 3395:                 if ($order eq '') {
 3396:                     $order = $count;
 3397:                 }
 3398:                 $ordered{$order} = $rolename;
 3399:                 $count++;
 3400:             }
 3401:         }
 3402:         my $maxnum = scalar(keys(%ordered));
 3403:         my @roles_by_num = ();
 3404:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3405:             push(@roles_by_num,$item);
 3406:         }
 3407:         my $context = 'domprefs';
 3408:         my $crstype = 'Course';
 3409:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3410:         my @accesstypes = ('all','dh','da','none');
 3411:         my ($numstatustypes,@jsarray);
 3412:         if (ref($types) eq 'ARRAY') {
 3413:             if (@{$types} > 0) {
 3414:                 $numstatustypes = scalar(@{$types});
 3415:                 push(@accesstypes,'status');
 3416:                 @jsarray = ('bystatus');
 3417:             }
 3418:         }
 3419:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 3420:         if (keys(%domhelpdesk)) {
 3421:             push(@accesstypes,('inc','exc'));
 3422:             push(@jsarray,('notinc','notexc'));
 3423:         }
 3424:         my $hiddenstr = join("','",@jsarray);
 3425:         $datatable .= &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname);
 3426:         my $context = 'domprefs';
 3427:         my $crstype = 'Course';
 3428:         my $prefix = 'helproles_';
 3429:         my $add_class = 'LC_hidden';
 3430:         foreach my $num (@roles_by_num) {
 3431:             my $role = $ordered{$num};
 3432:             my ($desc,$access,@statuses);
 3433:             if (ref($current{$role}) eq 'HASH') {
 3434:                 $desc = $current{$role}{'desc'};
 3435:                 $access = $current{$role}{'access'};
 3436:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 3437:                     @statuses = @{$current{$role}{'insttypes'}};
 3438:                 }
 3439:             }
 3440:             if ($desc eq '') {
 3441:                 $desc = $role;
 3442:             }
 3443:             my $identifier = 'custhelp'.$num;
 3444:             my %full=();
 3445:             my %levels= (
 3446:                          course => {},
 3447:                          domain => {},
 3448:                          system => {},
 3449:                         );
 3450:             my %levelscurrent=(
 3451:                                course => {},
 3452:                                domain => {},
 3453:                                system => {},
 3454:                               );
 3455:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 3456:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3457:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3458:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 3459:             $datatable .= '<tr '.$css_class.'><td valign="top"><b>'.$role.'</b><br />'.
 3460:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 3461:             for (my $k=0; $k<=$maxnum; $k++) {
 3462:                 my $vpos = $k+1;
 3463:                 my $selstr;
 3464:                 if ($k == $num) {
 3465:                     $selstr = ' selected="selected" ';
 3466:                 }
 3467:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3468:             }
 3469:             $datatable .= '</select>'.('&nbsp;'x2).
 3470:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 3471:                           '</td>'.
 3472:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3473:                           &mt('Name shown to users:').
 3474:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 3475:                           '</fieldset>'.
 3476:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 3477:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 3478:                           '<fieldset>'.
 3479:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 3480:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 3481:                                                                    \%levelscurrent,$identifier,
 3482:                                                                    'LC_hidden',$prefix.$num.'_privs').
 3483:                           '</fieldset></td>';
 3484:             $itemcount ++;
 3485:         }
 3486:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3487:         my $newcust = 'custhelp'.$count;
 3488:         my (%privs,%levelscurrent);
 3489:         my %full=();
 3490:         my %levels= (
 3491:                      course => {},
 3492:                      domain => {},
 3493:                      system => {},
 3494:                     );
 3495:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 3496:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3497:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 3498:         $datatable .= '<tr '.$css_class.'><td valign="top"><span class="LC_nobreak"><label>'.
 3499:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 3500:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 3501:         for (my $k=0; $k<$maxnum+1; $k++) {
 3502:             my $vpos = $k+1;
 3503:             my $selstr;
 3504:             if ($k == $maxnum) {
 3505:                 $selstr = ' selected="selected" ';
 3506:             }
 3507:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3508:         }
 3509:         $datatable .= '</select>&nbsp;'."\n".
 3510:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 3511:                       '</label></span></td>'.
 3512:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3513:                       '<span class="LC_nobreak">'.
 3514:                       &mt('Internal name:').
 3515:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 3516:                       '</span>'.('&nbsp;'x4).
 3517:                       '<span class="LC_nobreak">'.
 3518:                       &mt('Name shown to users:').
 3519:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 3520:                       '</span></fieldset>'.
 3521:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 3522:                                              $usertypes,$types,\%domhelpdesk).
 3523:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 3524:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 3525:                                                                 \@templateroles,$newcust).
 3526:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 3527:                                                                \%levelscurrent,$newcust).
 3528:                       '</fieldset>'.
 3529:                       &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname).
 3530:                       '</td></tr>';
 3531:         $count ++;
 3532:         $$rowtotal += $count;
 3533:     }
 3534:     return $datatable;
 3535: }
 3536: 
 3537: sub adhocbutton {
 3538:     my ($prefix,$num,$field,$visibility) = @_;
 3539:     my %lt = &Apache::lonlocal::texthash(
 3540:                                           show => 'Show details',
 3541:                                           hide => 'Hide details',
 3542:                                         );
 3543:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 3544:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 3545:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 3546:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 3547: }
 3548: 
 3549: sub helpsettings_javascript {
 3550:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 3551:     return unless(ref($roles_by_num) eq 'ARRAY');
 3552:     my %html_js_lt = &Apache::lonlocal::texthash(
 3553:                                           show => 'Show details',
 3554:                                           hide => 'Hide details',
 3555:                                         );
 3556:     &html_escape(\%html_js_lt);
 3557:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 3558:     return <<"ENDSCRIPT";
 3559: <script type="text/javascript">
 3560: // <![CDATA[
 3561: 
 3562: function reorderHelpRoles(form,item) {
 3563:     var changedVal;
 3564: $jstext
 3565:     var newpos = 'helproles_${total}_pos';
 3566:     var maxh = 1 + $total;
 3567:     var current = new Array();
 3568:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3569:     if (item == newpos) {
 3570:         changedVal = newitemVal;
 3571:     } else {
 3572:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3573:         current[newitemVal] = newpos;
 3574:     }
 3575:     for (var i=0; i<helproles.length; i++) {
 3576:         var elementName = 'helproles_'+helproles[i]+'_pos';
 3577:         if (elementName != item) {
 3578:             if (form.elements[elementName]) {
 3579:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3580:                 current[currVal] = elementName;
 3581:             }
 3582:         }
 3583:     }
 3584:     var oldVal;
 3585:     for (var j=0; j<maxh; j++) {
 3586:         if (current[j] == undefined) {
 3587:             oldVal = j;
 3588:         }
 3589:     }
 3590:     if (oldVal < changedVal) {
 3591:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3592:            var elementName = current[k];
 3593:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3594:         }
 3595:     } else {
 3596:         for (var k=changedVal; k<oldVal; k++) {
 3597:             var elementName = current[k];
 3598:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3599:         }
 3600:     }
 3601:     return;
 3602: }
 3603: 
 3604: function helpdeskAccess(num) {
 3605:     var curraccess = null;
 3606:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 3607:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 3608:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 3609:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 3610:             }
 3611:         }
 3612:     }
 3613:     var shown = Array();
 3614:     var hidden = Array();
 3615:     if (curraccess == 'none') {
 3616:         hidden = Array('$hiddenstr');
 3617:     } else {
 3618:         if (curraccess == 'status') {
 3619:             shown = Array('bystatus');
 3620:             hidden = Array('notinc','notexc');
 3621:         } else {
 3622:             if (curraccess == 'exc') {
 3623:                 shown = Array('notexc');
 3624:                 hidden = Array('notinc','bystatus');
 3625:             }
 3626:             if (curraccess == 'inc') {
 3627:                 shown = Array('notinc');
 3628:                 hidden = Array('notexc','bystatus');
 3629:             }
 3630:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 3631:                 hidden = Array('notinc','notexc','bystatus');
 3632:             }
 3633:         }
 3634:     }
 3635:     if (hidden.length > 0) {
 3636:         for (var i=0; i<hidden.length; i++) {
 3637:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 3638:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 3639:             }
 3640:         }
 3641:     }
 3642:     if (shown.length > 0) {
 3643:         for (var i=0; i<shown.length; i++) {
 3644:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 3645:                 if (shown[i] == 'privs') {
 3646:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 3647:                 } else {
 3648:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 3649:                 }
 3650:             }
 3651:         }
 3652:     }
 3653:     return;
 3654: }
 3655: 
 3656: function toggleHelpdeskItem(num,field) {
 3657:     if (document.getElementById('helproles_'+num+'_'+field)) {
 3658:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 3659:             document.getElementById('helproles_'+num+'_'+field).className =
 3660:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 3661:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3662:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 3663:             }
 3664:         } else {
 3665:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 3666:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3667:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 3668:             }
 3669:         }
 3670:     }
 3671:     return;
 3672: }
 3673: 
 3674: // ]]>
 3675: </script>
 3676: 
 3677: ENDSCRIPT
 3678: }
 3679: 
 3680: sub helpdeskroles_access {
 3681:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 3682:         $usertypes,$types,$domhelpdesk) = @_;
 3683:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 3684:     my %lt = &Apache::lonlocal::texthash(
 3685:                     'rou'    => 'Role usage',
 3686:                     'whi'    => 'Which helpdesk personnel may use this role?',
 3687:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 3688:                     'dh'     => 'All with domain helpdesk role',
 3689:                     'da'     => 'All with domain helpdesk assistant role',
 3690:                     'none'   => 'None',
 3691:                     'status' => 'Determined based on institutional status',
 3692:                     'inc'    => 'Include all, but exclude specific personnel',
 3693:                     'exc'    => 'Exclude all, but include specific personnel',
 3694:                   );
 3695:     my %usecheck = (
 3696:                      all => ' checked="checked"',
 3697:                    );
 3698:     my %displaydiv = (
 3699:                       status => 'none',
 3700:                       inc    => 'none',
 3701:                       exc    => 'none',
 3702:                       priv   => 'block',
 3703:                      );
 3704:     my $output;
 3705:     if (ref($current) eq 'HASH') {
 3706:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 3707:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 3708:                 $usecheck{$current->{access}} = $usecheck{'all'};
 3709:                 delete($usecheck{'all'});
 3710:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 3711:                     my $access = $1;
 3712:                     $displaydiv{$access} = 'inline';
 3713:                 } elsif ($current->{access} eq 'none') {
 3714:                     $displaydiv{'priv'} = 'none';
 3715:                 }
 3716:             }
 3717:         }
 3718:     }
 3719:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 3720:               '<p>'.$lt{'whi'}.'</p>';
 3721:     foreach my $access (@{$accesstypes}) {
 3722:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 3723:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 3724:                    $lt{$access}.'</label>';
 3725:         if ($access eq 'status') {
 3726:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 3727:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 3728:                                                                  $othertitle,$usertypes,$types).
 3729:                        '</div>';
 3730:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 3731:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 3732:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3733:                        '</div>';
 3734:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 3735:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 3736:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3737:                        '</div>';
 3738:         }
 3739:         $output .= '</p>';
 3740:     }
 3741:     $output .= '</fieldset>';
 3742:     return $output;
 3743: }
 3744: 
 3745: sub radiobutton_prefs {
 3746:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 3747:         $additional,$align) = @_;
 3748:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 3749:                    (ref($choices) eq 'HASH'));
 3750: 
 3751:     my (%checkedon,%checkedoff,$datatable,$css_class);
 3752: 
 3753:     foreach my $item (@{$toggles}) {
 3754:         if ($defaultchecked->{$item} eq 'on') {
 3755:             $checkedon{$item} = ' checked="checked" ';
 3756:             $checkedoff{$item} = ' ';
 3757:         } elsif ($defaultchecked->{$item} eq 'off') {
 3758:             $checkedoff{$item} = ' checked="checked" ';
 3759:             $checkedon{$item} = ' ';
 3760:         }
 3761:     }
 3762:     if (ref($settings) eq 'HASH') {
 3763:         foreach my $item (@{$toggles}) {
 3764:             if ($settings->{$item} eq '1') {
 3765:                 $checkedon{$item} =  ' checked="checked" ';
 3766:                 $checkedoff{$item} = ' ';
 3767:             } elsif ($settings->{$item} eq '0') {
 3768:                 $checkedoff{$item} =  ' checked="checked" ';
 3769:                 $checkedon{$item} = ' ';
 3770:             }
 3771:         }
 3772:     }
 3773:     if ($onclick) {
 3774:         $onclick = ' onclick="'.$onclick.'"';
 3775:     }
 3776:     foreach my $item (@{$toggles}) {
 3777:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3778:         $datatable .=
 3779:             '<tr'.$css_class.'><td valign="top">'.
 3780:             '<span class="LC_nobreak">'.$choices->{$item}.
 3781:             '</span></td>';
 3782:         if ($align eq 'left') {
 3783:             $datatable .= '<td class="LC_left_item">';
 3784:         } else {
 3785:             $datatable .= '<td class="LC_right_item">';
 3786:         }
 3787:         $datatable .=
 3788:             '<span class="LC_nobreak">'.
 3789:             '<label><input type="radio" name="'.
 3790:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 3791:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 3792:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 3793:             '</span>'.$additional.
 3794:             '</td>'.
 3795:             '</tr>';
 3796:         $itemcount ++;
 3797:     }
 3798:     return ($datatable,$itemcount);
 3799: }
 3800: 
 3801: sub print_ltitools {
 3802:     my ($dom,$settings,$rowtotal) = @_;
 3803:     my $rownum = 0;
 3804:     my $css_class;
 3805:     my $itemcount = 1;
 3806:     my $maxnum = 0;
 3807:     my %ordered;
 3808:     if (ref($settings) eq 'HASH') {
 3809:         foreach my $item (keys(%{$settings})) {
 3810:             if (ref($settings->{$item}) eq 'HASH') {
 3811:                 my $num = $settings->{$item}{'order'};
 3812:                 $ordered{$num} = $item;
 3813:             }
 3814:         }
 3815:     }
 3816:     my $confname = $dom.'-domainconfig';
 3817:     my $switchserver = &check_switchserver($dom,$confname);
 3818:     my $maxnum = scalar(keys(%ordered));
 3819:     my $datatable = &ltitools_javascript($settings);
 3820:     my %lt = &ltitools_names();
 3821:     my @courseroles = ('cc','in','ta','ep','st');
 3822:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 3823:     my @fields = ('fullname','firstname','lastname','email','user','roles');
 3824:     if (keys(%ordered)) {
 3825:         my @items = sort { $a <=> $b } keys(%ordered);
 3826:         for (my $i=0; $i<@items; $i++) {
 3827:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3828:             my $item = $ordered{$items[$i]};
 3829:             my ($title,$key,$secret,$url,$imgsrc,$version);
 3830:             if (ref($settings->{$item}) eq 'HASH') {
 3831:                 $title = $settings->{$item}->{'title'};
 3832:                 $url = $settings->{$item}->{'url'};
 3833:                 $key = $settings->{$item}->{'key'};
 3834:                 $secret = $settings->{$item}->{'secret'};
 3835:                 my $image = $settings->{$item}->{'image'};
 3836:                 if ($image ne '') {
 3837:                     $imgsrc = '<img src="'.$image.'" alt="'.&mt('Tool Provider icon').'" />';
 3838:                 }
 3839:             }
 3840:             my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'ltitools_".$item."'".');"';
 3841:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 3842:                          .'<select name="ltitools_'.$item.'"'.$chgstr.'>';
 3843:             for (my $k=0; $k<=$maxnum; $k++) {
 3844:                 my $vpos = $k+1;
 3845:                 my $selstr;
 3846:                 if ($k == $i) {
 3847:                     $selstr = ' selected="selected" ';
 3848:                 }
 3849:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3850:             }
 3851:             $datatable .= '</select>'.('&nbsp;'x2).
 3852:                 '<label><input type="checkbox" name="ltitools_del" value="'.$item.'" />'.
 3853:                 &mt('Delete?').'</label></span></td>'.
 3854:                 '<td colspan="2">'.
 3855:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 3856:                 '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="30" name="ltitools_title_'.$i.'" value="'.$title.'" /></span> '.
 3857:                 ('&nbsp;'x2).
 3858:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_version_'.$i.'">'.
 3859:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 3860:                 ('&nbsp;'x2).
 3861:                 '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_msgtype_'.$i.'">'.
 3862:                 '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 3863:                 '<br /><br />'.
 3864:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="30" name="ltitools_url_'.$i.'"'.
 3865:                 ' value="'.$url.'" /></span>'.
 3866:                 ('&nbsp;'x2).
 3867:                 '<span class="LC_nobreak">'.$lt{'key'}.
 3868:                 '<input type="text" size="25" name="ltitools_key_'.$i.'" value="'.$key.'" /></span> '.
 3869:                 ('&nbsp;'x2).
 3870:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 3871:                 '<input type="password" size="20" name="ltitools_secret_'.$i.'" value="'.$secret.'" />'.
 3872:                 '<label><input type="checkbox" name="visible" onclick="if (this.checked) { this.form.ltitools_secret_'.$i.'.type='."'text'".' } else { this.form.ltitools_secret_'.$i.'.type='."'password'".' }" />'.&mt('Visible input').'</label>'.
 3873:                 '<input type="hidden" name="ltitools_id_'.$i.'" value="'.$item.'" /></span>'.
 3874:                 '</fieldset>'.
 3875:                 '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 3876:                 '<span class="LC_nobreak">'.&mt('Display target:');
 3877:             my %currdisp;
 3878:             if (ref($settings->{$item}->{'display'}) eq 'HASH') {
 3879:                 if ($settings->{$item}->{'display'}->{'target'} eq 'window') {
 3880:                     $currdisp{'window'} = ' checked="checked"';
 3881:                 } elsif ($settings->{$item}->{'display'}->{'target'} eq 'tab') {
 3882:                     $currdisp{'tab'} = ' checked="checked"';
 3883:                 } else {
 3884:                     $currdisp{'iframe'} = ' checked="checked"';
 3885:                 }
 3886:                 if ($settings->{$item}->{'display'}->{'width'} =~ /^(\d+)$/) {
 3887:                     $currdisp{'width'} = $1;
 3888:                 }
 3889:                 if ($settings->{$item}->{'display'}->{'height'} =~ /^(\d+)$/) {
 3890:                      $currdisp{'height'} = $1;
 3891:                 }
 3892:                 $currdisp{'linktext'} = $settings->{$item}->{'display'}->{'linktext'};
 3893:                 $currdisp{'explanation'} = $settings->{$item}->{'display'}->{'explanation'};
 3894:             } else {
 3895:                 $currdisp{'iframe'} = ' checked="checked"';
 3896:             }
 3897:             foreach my $disp ('iframe','tab','window') {
 3898:                 $datatable .= '<label><input type="radio" name="ltitools_target_'.$i.'" value="'.$disp.'"'.$currdisp{$disp}.' />'.
 3899:                               $lt{$disp}.'</label>'.('&nbsp;'x2);
 3900:             }
 3901:             $datatable .= ('&nbsp;'x4);
 3902:             foreach my $dimen ('width','height') {
 3903:                 $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 3904:                               '<input type="text" name="ltitools_'.$dimen.'_'.$i.'" size="5" value="'.$currdisp{$dimen}.'" /></label>'.
 3905:                               ('&nbsp;'x2);
 3906:             }
 3907:             $datatable .= '<br />'.
 3908:                           '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 3909:                           '<input type="text" name="ltitools_linktext_'.$i.'" size="25" value="'.$currdisp{'linktext'}.'" /></label></div>'.
 3910:                           '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 3911:                           '<textarea name="ltitools_explanation_'.$i.'" rows="5" cols="40">'.$currdisp{'explanation'}.
 3912:                           '</textarea></div><div style=""></div><br />';
 3913:             $datatable .= '<br />';
 3914:             foreach my $extra ('passback','roster') {
 3915:                 my $checkedon = '';
 3916:                 my $checkedoff = ' checked="checked"';
 3917:                 if ($settings->{$item}->{$extra}) {
 3918:                     $checkedon = $checkedoff;
 3919:                     $checkedoff = '';
 3920:                 }
 3921:                 $datatable .= $lt{$extra}.'&nbsp;'.
 3922:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="1"'.$checkedon.' />'.
 3923:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 3924:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="0"'.$checkedoff.' />'.
 3925:                               &mt('No').'</label>'.('&nbsp;'x4);
 3926:             }
 3927:             $datatable .= '<br /><br /><span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 3928:             if ($imgsrc) {
 3929:                 $datatable .= $imgsrc.
 3930:                               '<label><input type="checkbox" name="ltitools_image_del"'.
 3931:                               ' value="'.$item.'" />'.&mt('Delete?').'</label></span> '.
 3932:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 3933:             } else {
 3934:                 $datatable .= '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 3935:             }
 3936:             if ($switchserver) {
 3937:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3938:             } else {
 3939:                 $datatable .= '<input type="file" name="ltitools_image_'.$i.'" value="" />';
 3940:             }
 3941:             $datatable .= '</span></fieldset>';
 3942:             my (%checkedfields,%rolemaps);
 3943:             if (ref($settings->{$item}) eq 'HASH') {
 3944:                 if (ref($settings->{$item}->{'fields'}) eq 'HASH') {
 3945:                     %checkedfields = %{$settings->{$item}->{'fields'}};
 3946:                 }
 3947:                 if (ref($settings->{$item}->{'roles'}) eq 'HASH') {
 3948:                     %rolemaps = %{$settings->{$item}->{'roles'}};
 3949:                     $checkedfields{'roles'} = 1;
 3950:                 }
 3951:             }
 3952:             $datatable .= '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 3953:                           '<span class="LC_nobreak">';
 3954:             foreach my $field (@fields) {
 3955:                 my $checked;
 3956:                 if ($checkedfields{$field}) {
 3957:                     $checked = ' checked="checked"';
 3958:                 }
 3959:                 $datatable .= '<label>'.
 3960:                               '<input type="checkbox" name="ltitools_fields_'.$i.'" value="'.$field.'"'.$checked.' />'.
 3961:                               $lt{$field}.'</label>'.('&nbsp;' x2);
 3962:             }
 3963:             $datatable .= '</span></fieldset>'.
 3964:                           '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 3965:             foreach my $role (@courseroles) {
 3966:                 my ($selected,$selectnone);
 3967:                 if (!$rolemaps{$role}) {
 3968:                     $selectnone = ' selected="selected"';
 3969:                 }
 3970:                 $datatable .= '<td align="center">'.
 3971:                               &Apache::lonnet::plaintext($role,'Course').'<br />'.
 3972:                               '<select name="ltitools_roles_'.$role.'_'.$i.'">'.
 3973:                               '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 3974:                 foreach my $ltirole (@ltiroles) {
 3975:                     unless ($selectnone) {
 3976:                         if ($rolemaps{$role} eq $ltirole) {
 3977:                             $selected = ' selected="selected"';
 3978:                         } else {
 3979:                             $selected = '';
 3980:                         }
 3981:                     }
 3982:                     $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 3983:                 }
 3984:                 $datatable .= '</select></td>';
 3985:             }
 3986:             $datatable .= '</tr></table></fieldset>';
 3987:             my %courseconfig;
 3988:             if (ref($settings->{$item}) eq 'HASH') {
 3989:                 if (ref($settings->{$item}->{'crsconf'}) eq 'HASH') {
 3990:                     %courseconfig = %{$settings->{$item}->{'crsconf'}};
 3991:                 }
 3992:             }
 3993:             $datatable .= '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 3994:             foreach my $item ('label','title','target','linktext','explanation') {
 3995:                 my $checked;
 3996:                 if ($courseconfig{$item}) {
 3997:                     $checked = ' checked="checked"';
 3998:                 }
 3999:                 $datatable .= '<label>'.
 4000:                        '<input type="checkbox" name="ltitools_courseconfig_'.$i.'" value="'.$item.'"'.$checked.' />'.
 4001:                        $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 4002:             }
 4003:             $datatable .= '</span></fieldset>'.
 4004:                           '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 4005:                           '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>';
 4006:             if (ref($settings->{$item}->{'custom'}) eq 'HASH') {
 4007:                 my %custom = %{$settings->{$item}->{'custom'}};
 4008:                 if (keys(%custom) > 0) {
 4009:                     foreach my $key (sort(keys(%custom))) {
 4010:                         $datatable .= '<tr><td><span class="LC_nobreak">'.
 4011:                                       '<label><input type="checkbox" name="ltitools_customdel_'.$i.'" value="'.
 4012:                                       $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 4013:                                       '<td><input type="text" name="ltitools_customval_'.$key.'_'.$i.'"'.
 4014:                                       ' value="'.$custom{$key}.'" /></td></tr>';
 4015:                     }
 4016:                 }
 4017:             }
 4018:             $datatable .= '<tr><td><span class="LC_nobreak">'.
 4019:                           '<label><input type="checkbox" name="ltitools_customadd" value="'.$i.'" />'.
 4020:                           &mt('Add').'</label></span></td><td><input type="text" name="ltitools_custom_name_'.$i.'" />'.
 4021:                           '</td><td><input type="text" name="ltitools_custom_value_'.$i.'" /></td></tr>';
 4022:             $datatable .= '</table></fieldset></td></tr>'."\n";
 4023:             $itemcount ++;
 4024:         }
 4025:     }
 4026:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4027:     my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'ltitools_add_pos'".');"';
 4028:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 4029:                   '<input type="hidden" name="ltitools_maxnum" value="'.$maxnum.'" />'."\n".
 4030:                   '<select name="ltitools_add_pos"'.$chgstr.'>';
 4031:     for (my $k=0; $k<$maxnum+1; $k++) {
 4032:         my $vpos = $k+1;
 4033:         my $selstr;
 4034:         if ($k == $maxnum) {
 4035:             $selstr = ' selected="selected" ';
 4036:         }
 4037:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4038:     }
 4039:     $datatable .= '</select>&nbsp;'."\n".
 4040:                   '<input type="checkbox" name="ltitools_add" value="1" />'.&mt('Add').'</td>'."\n".
 4041:                   '<td colspan="2">'.
 4042:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4043:                   '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="30" name="ltitools_add_title" value="" /></span> '."\n".
 4044:                   ('&nbsp;'x2).
 4045:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_add_version">'.
 4046:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 4047:                   ('&nbsp;'x2).
 4048:                   '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_add_msgtype">'.
 4049:                   '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 4050:                   '<br />'.
 4051:                   '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="30" name="ltitools_add_url" value="" /></span> '."\n".
 4052:                   ('&nbsp;'x2).
 4053:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="ltitools_add_key" value="" /></span> '."\n".
 4054:                   ('&nbsp;'x2).
 4055:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="ltitools_add_secret" value="" />'.
 4056:                   '<label><input type="checkbox" name="visible" onclick="if (this.checked) { this.form.ltitools_add_secret.type='."'text'".' } else { this.form.ltitools_add_secret.type='."'password'".' }" />'.&mt('Visible input').'</label></span> '."\n".
 4057:                   '</fieldset>'.
 4058:                   '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 4059:                   '<span class="LC_nobreak">'.&mt('Display target:');
 4060:     my %defaultdisp;
 4061:     $defaultdisp{'iframe'} = ' checked="checked"';
 4062:     foreach my $disp ('iframe','tab','window') {
 4063:         $datatable .= '<label><input type="radio" name="ltitools_add_target" value="'.$disp.'"'.$defaultdisp{$disp}.' />'.
 4064:                       $lt{$disp}.'</label>'.('&nbsp;'x2);
 4065:     }
 4066:     $datatable .= ('&nbsp;'x4);
 4067:     foreach my $dimen ('width','height') {
 4068:         $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 4069:                       '<input type="text" name="ltitools_add_'.$dimen.'" size="5" /></label>'.
 4070:                       ('&nbsp;'x2);
 4071:     }
 4072:     $datatable .= '<br />'.
 4073:                   '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 4074:                   '<input type="text" name="ltitools_add_linktext" size="5" /></label></div>'.
 4075:                   '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 4076:                   '<textarea name=ltitools_add_explanation" rows="5" cols="40"></textarea>'.
 4077:                   '</div><div style=""></div><br />';
 4078:     foreach my $extra ('passback','roster') {
 4079:         $datatable .= $lt{$extra}.'&nbsp;'.
 4080:                       '<label><input type="radio" name="ltitools_add_'.$extra.'" value="1" />'.
 4081:                       &mt('Yes').'</label>'.('&nbsp;'x2).
 4082:                       '<label><input type="radio" name="ltitools_add_'.$extra.'" value="0" checked="checked" />'.
 4083:                       &mt('No').'</label>'.('&nbsp;'x4);
 4084:     }
 4085:     $datatable .= '<br /><br /><span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;'.
 4086:                   '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 4087:     if ($switchserver) {
 4088:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4089:     } else {
 4090:         $datatable .= '<input type="file" name="ltitools_add_image" value="" />';
 4091:     }
 4092:     $datatable .= '</span></fieldset>'.
 4093:                   '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 4094:                   '<span class="LC_nobreak">';
 4095:     foreach my $field (@fields) {
 4096:         $datatable .= '<label>'.
 4097:                       '<input type="checkbox" name="ltitools_add_fields" value="'.$field.'" />'.
 4098:                       $lt{$field}.'</label>'.('&nbsp;' x2);
 4099:     }
 4100:     $datatable .= '</span></fieldset>'.
 4101:                   '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 4102:     foreach my $role (@courseroles) {
 4103:         my ($checked,$checkednone);
 4104:         $datatable .= '<td align="center">'.
 4105:                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 4106:                       '<select name="ltitools_add_roles_'.$role.'">'.
 4107:                       '<option value="" selected="selected">'.&mt('Select').'</option>';
 4108:         foreach my $ltirole (@ltiroles) {
 4109:             $datatable .= '<option value="'.$ltirole.'">'.$ltirole.'</option>';
 4110:         }
 4111:         $datatable .= '</select></td>';
 4112:     }
 4113:     $datatable .= '</tr></table></fieldset>'.
 4114:                   '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 4115:     foreach my $item ('label','title','target','linktext','explanation') {
 4116:         $datatable .= '<label>'.
 4117:                       '<input type="checkbox" name="ltitools_courseconfig" value="'.$item.'" checked="checked" />'.
 4118:                       $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 4119:     }
 4120:     $datatable .= '</span></fieldset>'.
 4121:                   '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 4122:                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 4123:                   '<tr><td><span class="LC_nobreak">'.
 4124:                   '<label><input type="checkbox" name="ltitools_add_custom" value="1" />'.
 4125:                   &mt('Add').'</label></span></td><td><input type="text" name="ltitools_add_custom_name" />'.
 4126:                   '</td><td><input type="text" name="ltitools_add_custom_value" /></td></tr>'.
 4127:                   '</table></fieldset></td></tr>'."\n".
 4128:                   '</td>'."\n".
 4129:                   '</tr>'."\n";
 4130:     $itemcount ++;
 4131:     return $datatable;
 4132: }
 4133: 
 4134: sub ltitools_names {
 4135:     my %lt = &Apache::lonlocal::texthash(
 4136:                                           'title'          => 'Title',
 4137:                                           'version'        => 'Version',
 4138:                                           'msgtype'        => 'Message Type',
 4139:                                           'url'            => 'URL',
 4140:                                           'key'            => 'Key',
 4141:                                           'secret'         => 'Secret',
 4142:                                           'icon'           => 'Icon',
 4143:                                           'user'           => 'Username:domain',
 4144:                                           'fullname'       => 'Full Name',
 4145:                                           'firstname'      => 'First Name',
 4146:                                           'lastname'       => 'Last Name',
 4147:                                           'email'          => 'E-mail',
 4148:                                           'roles'          => 'Role',
 4149:                                           'window'         => 'Window',
 4150:                                           'tab'            => 'Tab',
 4151:                                           'iframe'         => 'iFrame',
 4152:                                           'height'         => 'Height',
 4153:                                           'width'          => 'Width',
 4154:                                           'linktext'       => 'Default Link Text',
 4155:                                           'explanation'    => 'Default Explanation',
 4156:                                           'passback'       => 'Tool can return grades:',
 4157:                                           'roster'         => 'Tool can retrieve roster:',
 4158:                                           'crstarget'      => 'Display target',
 4159:                                           'crslabel'       => 'Course label',
 4160:                                           'crstitle'       => 'Course title',
 4161:                                           'crslinktext'    => 'Link Text',
 4162:                                           'crsexplanation' => 'Explanation',
 4163:                                         );
 4164: 
 4165:     return %lt;
 4166: }
 4167: 
 4168: sub print_coursedefaults {
 4169:     my ($position,$dom,$settings,$rowtotal) = @_;
 4170:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 4171:     my $itemcount = 1;
 4172:     my %choices =  &Apache::lonlocal::texthash (
 4173:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 4174:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 4175:         coursecredits        => 'Credits can be specified for courses',
 4176:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 4177:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 4178:         texengine            => 'Default method to display mathematics',
 4179:         postsubmit           => 'Disable submit button/keypress following student submission',
 4180:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 4181:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 4182:     );
 4183:     my %staticdefaults = (
 4184:                            anonsurvey_threshold => 10,
 4185:                            uploadquota          => 500,
 4186:                            postsubmit           => 60,
 4187:                            mysqltables          => 172800,
 4188:                          );
 4189:     if ($position eq 'top') {
 4190:         %defaultchecked = (
 4191:                             'uselcmath'       => 'on',
 4192:                             'usejsme'         => 'on',
 4193:                             'canclone'        => 'none',
 4194:                           );
 4195:         @toggles = ('uselcmath','usejsme');
 4196:         my $deftex = $Apache::lonnet::deftex;
 4197:         if (ref($settings) eq 'HASH') {
 4198:             if ($settings->{'texengine'}) {
 4199:                 if ($settings->{'texengine'} =~ /^(MathJax|mimetex|tth)$/) {
 4200:                     $deftex = $settings->{'texengine'};
 4201:                 }
 4202:             }
 4203:         }
 4204:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4205:         my $mathdisp = '<tr'.$css_class.'><td style="vertical-align: top">'.
 4206:                        '<span class="LC_nobreak">'.$choices{'texengine'}.
 4207:                        '</span></td><td class="LC_right_item">'.
 4208:                        '<select name="texengine">'."\n";
 4209:         my %texoptions = (
 4210:                             MathJax  => 'MathJax',
 4211:                             mimetex  => &mt('Convert to Images'),
 4212:                             tth      => &mt('TeX to HTML'),
 4213:                          );
 4214:         foreach my $renderer ('MathJax','mimetex','tth') {
 4215:             my $selected = '';
 4216:             if ($renderer eq $deftex) {
 4217:                 $selected = ' selected="selected"';
 4218:             }
 4219:             $mathdisp .= '<option value="'.$renderer.'"'.$selected.'>'.$texoptions{$renderer}.'</option>'."\n";
 4220:         }
 4221:         $mathdisp .= '</select></td></tr>'."\n";
 4222:         $itemcount ++;
 4223:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4224:                                                      \%choices,$itemcount);
 4225:         $datatable = $mathdisp.$datatable;
 4226:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4227:         $datatable .=
 4228:             '<tr'.$css_class.'><td valign="top">'.
 4229:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 4230:             '</span></td><td class="LC_left_item">';
 4231:         my $currcanclone = 'none';
 4232:         my $onclick;
 4233:         my @cloneoptions = ('none','domain');
 4234:         my %clonetitles = (
 4235:                              none     => 'No additional course requesters',
 4236:                              domain   => "Any course requester in course's domain",
 4237:                              instcode => 'Course requests for official courses ...',
 4238:                           );
 4239:         my (%codedefaults,@code_order,@posscodes);
 4240:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 4241:                                                     \@code_order) eq 'ok') {
 4242:             if (@code_order > 0) {
 4243:                 push(@cloneoptions,'instcode');
 4244:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 4245:             }
 4246:         }
 4247:         if (ref($settings) eq 'HASH') {
 4248:             if ($settings->{'canclone'}) {
 4249:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 4250:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 4251:                         if (@code_order > 0) {
 4252:                             $currcanclone = 'instcode';
 4253:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 4254:                         }
 4255:                     }
 4256:                 } elsif ($settings->{'canclone'} eq 'domain') {
 4257:                     $currcanclone = $settings->{'canclone'};
 4258:                 }
 4259:             }
 4260:         }
 4261:         foreach my $option (@cloneoptions) {
 4262:             my ($checked,$additional);
 4263:             if ($currcanclone eq $option) {
 4264:                 $checked = ' checked="checked"';
 4265:             }
 4266:             if ($option eq 'instcode') {
 4267:                 if (@code_order) {
 4268:                     my $show = 'none';
 4269:                     if ($checked) {
 4270:                         $show = 'block';
 4271:                     }
 4272:                     $additional = '<div id="cloneinstcode" style="display:'.$show.'" />'.
 4273:                                   &mt('Institutional codes for new and cloned course have identical:').
 4274:                                   '<br />';
 4275:                     foreach my $item (@code_order) {
 4276:                         my $codechk;
 4277:                         if ($checked) {
 4278:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 4279:                                 $codechk = ' checked="checked"';
 4280:                             }
 4281:                         }
 4282:                         $additional .= '<label>'.
 4283:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 4284:                                        $item.'</label>';
 4285:                     }
 4286:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 4287:                 }
 4288:             }
 4289:             $datatable .=
 4290:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 4291:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 4292:                 '</label>&nbsp;'.$additional.'</span><br />';
 4293:         }
 4294:         $datatable .= '</td>'.
 4295:                       '</tr>';
 4296:         $itemcount ++;
 4297:     } else {
 4298:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4299:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 4300:         my $currusecredits = 0;
 4301:         my $postsubmitclient = 1;
 4302:         my @types = ('official','unofficial','community','textbook');
 4303:         if (ref($settings) eq 'HASH') {
 4304:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 4305:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 4306:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 4307:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 4308:                 }
 4309:             }
 4310:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 4311:                 foreach my $type (@types) {
 4312:                     next if ($type eq 'community');
 4313:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 4314:                     if ($defcredits{$type} ne '') {
 4315:                         $currusecredits = 1;
 4316:                     }
 4317:                 }
 4318:             }
 4319:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 4320:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 4321:                     $postsubmitclient = 0;
 4322:                     foreach my $type (@types) {
 4323:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4324:                     }
 4325:                 } else {
 4326:                     foreach my $type (@types) {
 4327:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 4328:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 4329:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 4330:                             } else {
 4331:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4332:                             }
 4333:                         } else {
 4334:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4335:                         }
 4336:                     }
 4337:                 }
 4338:             } else {
 4339:                 foreach my $type (@types) {
 4340:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4341:                 }
 4342:             }
 4343:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 4344:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 4345:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 4346:                 }
 4347:             } else {
 4348:                 foreach my $type (@types) {
 4349:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 4350:                 }
 4351:             }
 4352:         } else {
 4353:             foreach my $type (@types) {
 4354:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4355:             }
 4356:         }
 4357:         if (!$currdefresponder) {
 4358:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 4359:         } elsif ($currdefresponder < 1) {
 4360:             $currdefresponder = 1;
 4361:         }
 4362:         foreach my $type (@types) {
 4363:             if ($curruploadquota{$type} eq '') {
 4364:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 4365:             }
 4366:         }
 4367:         $datatable .=
 4368:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4369:                 $choices{'anonsurvey_threshold'}.
 4370:                 '</span></td>'.
 4371:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 4372:                 '<input type="text" name="anonsurvey_threshold"'.
 4373:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 4374:                 '</td></tr>'."\n";
 4375:         $itemcount ++;
 4376:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4377:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4378:                       $choices{'uploadquota'}.
 4379:                       '</span></td>'.
 4380:                       '<td align="right" class="LC_right_item">'.
 4381:                       '<table><tr>';
 4382:         foreach my $type (@types) {
 4383:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 4384:                            '<input type="text" name="uploadquota_'.$type.'"'.
 4385:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 4386:         }
 4387:         $datatable .= '</tr></table></td></tr>'."\n";
 4388:         $itemcount ++;
 4389:         my $onclick = "toggleDisplay(this.form,'credits');";
 4390:         my $display = 'none';
 4391:         if ($currusecredits) {
 4392:             $display = 'block';
 4393:         }
 4394:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 4395:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 4396:         foreach my $type (@types) {
 4397:             next if ($type eq 'community');
 4398:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 4399:                            '<input type="text" name="'.$type.'_credits"'.
 4400:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 4401:         }
 4402:         $additional .= '</tr></table></div>'."\n";
 4403:         %defaultchecked = ('coursecredits' => 'off');
 4404:         @toggles = ('coursecredits');
 4405:         my $current = {
 4406:                         'coursecredits' => $currusecredits,
 4407:                       };
 4408:         (my $table,$itemcount) =
 4409:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 4410:                                \%choices,$itemcount,$onclick,$additional,'left');
 4411:         $datatable .= $table;
 4412:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 4413:         my $display = 'none';
 4414:         if ($postsubmitclient) {
 4415:             $display = 'block';
 4416:         }
 4417:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 4418:                       &mt('Number of seconds submit is disabled').'<br />'.
 4419:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 4420:                       '<table><tr>';
 4421:         foreach my $type (@types) {
 4422:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 4423:                            '<input type="text" name="'.$type.'_timeout" value="'.
 4424:                            $deftimeout{$type}.'" size="5" /></td>';
 4425:         }
 4426:         $additional .= '</tr></table></div>'."\n";
 4427:         %defaultchecked = ('postsubmit' => 'on');
 4428:         @toggles = ('postsubmit');
 4429:         $current = {
 4430:                        'postsubmit' => $postsubmitclient,
 4431:                    };
 4432:         ($table,$itemcount) =
 4433:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 4434:                                \%choices,$itemcount,$onclick,$additional,'left');
 4435:         $datatable .= $table;
 4436:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4437:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4438:                       $choices{'mysqltables'}.
 4439:                       '</span></td>'.
 4440:                       '<td align="right" class="LC_right_item">'.
 4441:                       '<table><tr>';
 4442:         foreach my $type (@types) {
 4443:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 4444:                            '<input type="text" name="mysqltables_'.$type.'"'.
 4445:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 4446:         }
 4447:         $datatable .= '</tr></table></td></tr>'."\n";
 4448:         $itemcount ++;
 4449: 
 4450:     }
 4451:     $$rowtotal += $itemcount;
 4452:     return $datatable;
 4453: }
 4454: 
 4455: sub print_selfenrollment {
 4456:     my ($position,$dom,$settings,$rowtotal) = @_;
 4457:     my ($css_class,$datatable);
 4458:     my $itemcount = 1;
 4459:     my @types = ('official','unofficial','community','textbook');
 4460:     if (($position eq 'top') || ($position eq 'middle')) {
 4461:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 4462:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 4463:         my @rows;
 4464:         my $key;
 4465:         if ($position eq 'top') {
 4466:             $key = 'admin'; 
 4467:             if (ref($rowsref) eq 'ARRAY') {
 4468:                 @rows = @{$rowsref};
 4469:             }
 4470:         } elsif ($position eq 'middle') {
 4471:             $key = 'default';
 4472:             @rows = ('types','registered','approval','limit');
 4473:         }
 4474:         foreach my $row (@rows) {
 4475:             if (defined($titlesref->{$row})) {
 4476:                 $itemcount ++;
 4477:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4478:                 $datatable .= '<tr'.$css_class.'>'.
 4479:                               '<td>'.$titlesref->{$row}.'</td>'.
 4480:                               '<td class="LC_left_item">'.
 4481:                               '<table><tr>';
 4482:                 my (%current,%currentcap);
 4483:                 if (ref($settings) eq 'HASH') {
 4484:                     if (ref($settings->{$key}) eq 'HASH') {
 4485:                         foreach my $type (@types) {
 4486:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4487:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 4488:                             }
 4489:                             if (($row eq 'limit') && ($key eq 'default')) {
 4490:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4491:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 4492:                                 }
 4493:                             }
 4494:                         }
 4495:                     }
 4496:                 }
 4497:                 my %roles = (
 4498:                              '0' => &Apache::lonnet::plaintext('dc'),
 4499:                             ); 
 4500:             
 4501:                 foreach my $type (@types) {
 4502:                     unless (($row eq 'registered') && ($key eq 'default')) {
 4503:                         $datatable .= '<th>'.&mt($type).'</th>';
 4504:                     }
 4505:                 }
 4506:                 unless (($row eq 'registered') && ($key eq 'default')) {
 4507:                     $datatable .= '</tr><tr>';
 4508:                 }
 4509:                 foreach my $type (@types) {
 4510:                     if ($type eq 'community') {
 4511:                         $roles{'1'} = &mt('Community personnel');
 4512:                     } else {
 4513:                         $roles{'1'} = &mt('Course personnel');
 4514:                     }
 4515:                     $datatable .= '<td style="vertical-align: top">';
 4516:                     if ($position eq 'top') {
 4517:                         my %checked;
 4518:                         if ($current{$type} eq '0') {
 4519:                             $checked{'0'} = ' checked="checked"';
 4520:                         } else {
 4521:                             $checked{'1'} = ' checked="checked"';
 4522:                         }
 4523:                         foreach my $role ('1','0') {
 4524:                             $datatable .= '<span class="LC_nobreak"><label>'.
 4525:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 4526:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 4527:                                           $roles{$role}.'</label></span> ';
 4528:                         }
 4529:                     } else {
 4530:                         if ($row eq 'types') {
 4531:                             my %checked;
 4532:                             if ($current{$type} =~ /^(all|dom)$/) {
 4533:                                 $checked{$1} = ' checked="checked"';
 4534:                             } else {
 4535:                                 $checked{''} = ' checked="checked"';
 4536:                             }
 4537:                             foreach my $val ('','dom','all') {
 4538:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4539:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4540:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4541:                             }
 4542:                         } elsif ($row eq 'registered') {
 4543:                             my %checked;
 4544:                             if ($current{$type} eq '1') {
 4545:                                 $checked{'1'} = ' checked="checked"';
 4546:                             } else {
 4547:                                 $checked{'0'} = ' checked="checked"';
 4548:                             }
 4549:                             foreach my $val ('0','1') {
 4550:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4551:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4552:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4553:                             }
 4554:                         } elsif ($row eq 'approval') {
 4555:                             my %checked;
 4556:                             if ($current{$type} =~ /^([12])$/) {
 4557:                                 $checked{$1} = ' checked="checked"';
 4558:                             } else {
 4559:                                 $checked{'0'} = ' checked="checked"';
 4560:                             }
 4561:                             for my $val (0..2) {
 4562:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4563:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4564:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4565:                             }
 4566:                         } elsif ($row eq 'limit') {
 4567:                             my %checked;
 4568:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 4569:                                 $checked{$1} = ' checked="checked"';
 4570:                             } else {
 4571:                                 $checked{'none'} = ' checked="checked"';
 4572:                             }
 4573:                             my $cap;
 4574:                             if ($currentcap{$type} =~ /^\d+$/) {
 4575:                                 $cap = $currentcap{$type};
 4576:                             }
 4577:                             foreach my $val ('none','allstudents','selfenrolled') {
 4578:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4579:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4580:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4581:                             }
 4582:                             $datatable .= '<br />'.
 4583:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 4584:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 4585:                                           '</span>'; 
 4586:                         }
 4587:                     }
 4588:                     $datatable .= '</td>';
 4589:                 }
 4590:                 $datatable .= '</tr>';
 4591:             }
 4592:             $datatable .= '</table></td></tr>';
 4593:         }
 4594:     } elsif ($position eq 'bottom') {
 4595:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 4596:     }
 4597:     $$rowtotal += $itemcount;
 4598:     return $datatable;
 4599: }
 4600: 
 4601: sub print_validation_rows {
 4602:     my ($caller,$dom,$settings,$rowtotal) = @_;
 4603:     my ($itemsref,$namesref,$fieldsref);
 4604:     if ($caller eq 'selfenroll') { 
 4605:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 4606:     } elsif ($caller eq 'requestcourses') {
 4607:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 4608:     }
 4609:     my %currvalidation;
 4610:     if (ref($settings) eq 'HASH') {
 4611:         if (ref($settings->{'validation'}) eq 'HASH') {
 4612:             %currvalidation = %{$settings->{'validation'}};
 4613:         }
 4614:     }
 4615:     my $datatable;
 4616:     my $itemcount = 0;
 4617:     foreach my $item (@{$itemsref}) {
 4618:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4619:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4620:                       $namesref->{$item}.
 4621:                       '</span></td>'.
 4622:                       '<td class="LC_left_item">';
 4623:         if (($item eq 'url') || ($item eq 'button')) {
 4624:             $datatable .= '<span class="LC_nobreak">'.
 4625:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 4626:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 4627:         } elsif ($item eq 'fields') {
 4628:             my @currfields;
 4629:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 4630:                 @currfields = @{$currvalidation{$item}};
 4631:             }
 4632:             foreach my $field (@{$fieldsref}) {
 4633:                 my $check = '';
 4634:                 if (grep(/^\Q$field\E$/,@currfields)) {
 4635:                     $check = ' checked="checked"';
 4636:                 }
 4637:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4638:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 4639:                               ' value="'.$field.'"'.$check.' />'.$field.
 4640:                               '</label></span> ';
 4641:             }
 4642:         } elsif ($item eq 'markup') {
 4643:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5">'.
 4644:                            $currvalidation{$item}.
 4645:                               '</textarea>';
 4646:         }
 4647:         $datatable .= '</td></tr>'."\n";
 4648:         if (ref($rowtotal)) {
 4649:             $itemcount ++;
 4650:         }
 4651:     }
 4652:     if ($caller eq 'requestcourses') {
 4653:         my %currhash;
 4654:         if (ref($settings) eq 'HASH') {
 4655:             if (ref($settings->{'validation'}) eq 'HASH') {
 4656:                 if ($settings->{'validation'}{'dc'} ne '') {
 4657:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 4658:                 }
 4659:             }
 4660:         }
 4661:         my $numinrow = 2;
 4662:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 4663:                                                        'validationdc',%currhash);
 4664:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4665:         $datatable .= '<tr'.$css_class.'><td>';
 4666:         if ($numdc > 1) {
 4667:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 4668:         } else {
 4669:             $datatable .=  &mt('Course creation processed as: ');
 4670:         }
 4671:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 4672:         $itemcount ++;
 4673:     }
 4674:     if (ref($rowtotal)) {
 4675:         $$rowtotal += $itemcount;
 4676:     }
 4677:     return $datatable;
 4678: }
 4679: 
 4680: sub print_passwords {
 4681:     my ($position,$dom,$confname,$settings,$rowtotal) = @_;
 4682:     my ($datatable,$css_class);
 4683:     my $itemcount = 0;
 4684:     my %titles = &Apache::lonlocal::texthash (
 4685:         captcha        => '"Forgot Password" CAPTCHA validation',
 4686:         link           => 'Reset link expiration (hours)',
 4687:         case           => 'Case-sensitive usernames/e-mail',
 4688:         prelink        => 'Information required (form 1)',
 4689:         postlink       => 'Information required (form 2)',
 4690:         emailsrc       => 'LON-CAPA e-mail address type(s)',
 4691:         customtext     => 'Domain specific text (HTML)',
 4692:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
 4693:         intauth_check  => 'Check bcrypt cost if authenticated',
 4694:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
 4695:         permanent      => 'Permanent e-mail address',
 4696:         critical       => 'Critical notification address',
 4697:         notify         => 'Notification address',
 4698:         min            => 'Minimum password length',
 4699:         max            => 'Maximum password length',
 4700:         chars          => 'Required characters',
 4701:         numsaved       => 'Number of previous passwords to save and disallow reuse',
 4702:     );
 4703:     if ($position eq 'top') {
 4704:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4705:         my $shownlinklife = 2;
 4706:         my $prelink = 'both';
 4707:         my (%casesens,%postlink,%emailsrc,$nostdtext,$customurl);
 4708:         if (ref($settings) eq 'HASH') {
 4709:             if ($settings->{resetlink} =~ /^\d+(|\.\d*)$/) {
 4710:                 $shownlinklife = $settings->{resetlink};
 4711:             }
 4712:             if (ref($settings->{resetcase}) eq 'ARRAY') {
 4713:                 map { $casesens{$_} = 1; } (@{$settings->{resetcase}});
 4714:             }
 4715:             if ($settings->{resetprelink} =~ /^(both|either)$/) {
 4716:                 $prelink = $settings->{resetprelink};
 4717:             }
 4718:             if (ref($settings->{resetpostlink}) eq 'HASH') {
 4719:                 %postlink = %{$settings->{resetpostlink}};
 4720:             }
 4721:             if (ref($settings->{resetemail}) eq 'ARRAY') {
 4722:                 map { $emailsrc{$_} = 1; } (@{$settings->{resetemail}});
 4723:             }
 4724:             if ($settings->{resetremove}) {
 4725:                 $nostdtext = 1;
 4726:             }
 4727:             if ($settings->{resetcustom}) {
 4728:                 $customurl = $settings->{resetcustom};
 4729:             }
 4730:         } else {
 4731:             if (ref($types) eq 'ARRAY') {
 4732:                 foreach my $item (@{$types}) {
 4733:                     $casesens{$item} = 1;
 4734:                     $postlink{$item} = ['username','email'];
 4735:                 }
 4736:             }
 4737:             $casesens{'default'} = 1;
 4738:             $postlink{'default'} = ['username','email'];
 4739:             $prelink = 'both';
 4740:             %emailsrc = (
 4741:                           permanent => 1,
 4742:                           critical  => 1,
 4743:                           notify    => 1,
 4744:             );
 4745:         }
 4746:         $datatable = &captcha_choice('passwords',$settings,$$rowtotal);
 4747:         $itemcount ++;
 4748:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4749:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'link'}.'</td>'.
 4750:                       '<td class="LC_left_item">'.
 4751:                       '<input type="textbox" value="'.$shownlinklife.'" '.
 4752:                       'name="passwords_link" size="3" /></td></tr>';
 4753:         $itemcount ++;
 4754:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4755:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'case'}.'</td>'.
 4756:                       '<td class="LC_left_item">';
 4757:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 4758:             foreach my $item (@{$types}) {
 4759:                 my $checkedcase;
 4760:                 if ($casesens{$item}) {
 4761:                     $checkedcase = ' checked="checked"';
 4762:                 }
 4763:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4764:                               '<input type="checkbox" name="passwords_case_sensitive" value="'.
 4765:                               $item.'"'.$checkedcase.' />'.$usertypes->{$item}.'</label>'.
 4766:                               '</span>&nbsp;&nbsp; ';
 4767:             }
 4768:         }
 4769:         my $checkedcase;
 4770:         if ($casesens{'default'}) {
 4771:             $checkedcase = ' checked="checked"';
 4772:         }
 4773:         $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 4774:                       'name="passwords_case_sensitive" value="default"'.$checkedcase.' />'.
 4775:                       $othertitle.'</label></span></td>';
 4776:         $itemcount ++;
 4777:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4778:         my %checkedpre = (
 4779:                              both => ' checked="checked"',
 4780:                              either => '',
 4781:                          );
 4782:         if ($prelink eq 'either') {
 4783:             $checkedpre{either} = ' checked="checked"';
 4784:             $checkedpre{both} = '';
 4785:         }
 4786:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'prelink'}.'</td>'.
 4787:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 4788:                       '<label><input type="radio" name="passwords_prelink" value="both"'.$checkedpre{'both'}.' />'.
 4789:                       &mt('Both username and e-mail address').'</label></span>&nbsp;&nbsp; '.
 4790:                       '<span class="LC_nobreak"><label>'.
 4791:                       '<input type="radio" name="passwords_prelink" value="either"'.$checkedpre{'either'}.' />'.
 4792:                       &mt('Either username or e-mail address').'</label></span></td></tr>';
 4793:         $itemcount ++;
 4794:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4795:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'postlink'}.'</td>'.
 4796:                       '<td class="LC_left_item">';
 4797:         my %postlinked;
 4798:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 4799:             foreach my $item (@{$types}) {
 4800:                 undef(%postlinked);
 4801:                 $datatable .= '<fieldset style="display: inline-block;">'.
 4802:                               '<legend>'.$usertypes->{$item}.'</legend>';
 4803:                 if (ref($postlink{$item}) eq 'ARRAY') {
 4804:                     map { $postlinked{$_} = 1; } (@{$postlink{$item}});
 4805:                 }
 4806:                 foreach my $field ('email','username') {
 4807:                     my $checked;
 4808:                     if ($postlinked{$field}) {
 4809:                         $checked = ' checked="checked"';
 4810:                     }
 4811:                     $datatable .= '<span class="LC_nobreak"><label>'.
 4812:                                   '<input type="checkbox" name="passwords_postlink_'.$item.'" value="'.
 4813:                                   $field.'"'.$checked.' />'.$field.'</label>'.
 4814:                                   '<span>&nbsp;&nbsp; ';
 4815:                 }
 4816:                 $datatable .= '</fieldset>';
 4817:             }
 4818:         }
 4819:         if (ref($postlink{'default'}) eq 'ARRAY') {
 4820:             map { $postlinked{$_} = 1; } (@{$postlink{'default'}});
 4821:         }
 4822:         $datatable .= '<fieldset style="display: inline-block;">'.
 4823:                       '<legend>'.$othertitle.'</legend>';
 4824:         foreach my $field ('email','username') {
 4825:             my $checked;
 4826:             if ($postlinked{$field}) {
 4827:                 $checked = ' checked="checked"';
 4828:             }
 4829:             $datatable .= '<span class="LC_nobreak"><label>'.
 4830:                           '<input type="checkbox" name="passwords_postlink_default" value="'.
 4831:                           $field.'"'.$checked.' />'.$field.'</label>'.
 4832:                           '<span>&nbsp;&nbsp; ';
 4833:         }
 4834:         $datatable .= '</fieldset></td></tr>';
 4835:         $itemcount ++;
 4836:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4837:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'emailsrc'}.'</td>'.
 4838:                       '<td class="LC_left_item">';
 4839:         foreach my $type ('permanent','critical','notify') {
 4840:             my $checkedemail;
 4841:             if ($emailsrc{$type}) {
 4842:                 $checkedemail = ' checked="checked"';
 4843:             }
 4844:             $datatable .= '<span class="LC_nobreak"><label>'.
 4845:                           '<input type="checkbox" name="passwords_emailsrc" value="'.
 4846:                           $type.'"'.$checkedemail.' />'.$titles{$type}.'</label>'.
 4847:                           '<span>&nbsp;&nbsp; ';
 4848:         }
 4849:         $datatable .= '</td></tr>';
 4850:         $itemcount ++;
 4851:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4852:         my $switchserver = &check_switchserver($dom,$confname);
 4853:         my ($showstd,$noshowstd);
 4854:         if ($nostdtext) {
 4855:             $noshowstd = ' checked="checked"';
 4856:         } else {
 4857:             $showstd = ' checked="checked"';
 4858:         }
 4859:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'customtext'}.'</td>'.
 4860:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 4861:                       &mt('Retain standard text:').
 4862:                       '<label><input type="radio" name="passwords_stdtext" value="1"'.$showstd.' />'.
 4863:                       &mt('Yes').'</label>'.'&nbsp;'.
 4864:                       '<label><input type="radio" name="passwords_stdtext" value="0"'.$noshowstd.' />'.
 4865:                       &mt('No').'</label></span><br />'.
 4866:                       '<span class="LC_fontsize_small">'.
 4867:                       &mt('(If you use the same account ...  reset a password from this page.)').'</span><br /><br />'.
 4868:                       &mt('Include custom text:');
 4869:         if ($customurl) {
 4870:             my $link =  &Apache::loncommon::modal_link($customurl,&mt('custom text'),600,500,
 4871:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 4872:             $datatable .= '<span class="LC_nobreak">&nbsp;'.$link.
 4873:                           '<label><input type="checkbox" name="passwords_custom_del"'.
 4874:                           ' value="1" />'.&mt('Delete?').'</label></span>'.
 4875:                           ' <span class="LC_nobreak">&nbsp;'.&mt('Replace:').'</span>';
 4876:         }
 4877:         if ($switchserver) {
 4878:             $datatable .= '<span class="LC_nobreak">&nbsp;'.&mt('Upload to library server: [_1]',$switchserver).'</span>';
 4879:         } else {
 4880:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 4881:                          '<input type="file" name="passwords_customfile" /></span>';
 4882:         }
 4883:         $datatable .= '</td></tr>';
 4884:     } elsif ($position eq 'middle') {
 4885:         my %domconf = &Apache::lonnet::get_dom('configuration',['defaults'],$dom);
 4886:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 4887:         my %defaults;
 4888:         if (ref($domconf{'defaults'}) eq 'HASH') {
 4889:             %defaults = %{$domconf{'defaults'}};
 4890:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 4891:                 $defaults{'intauth_cost'} = 10;
 4892:             }
 4893:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 4894:                 $defaults{'intauth_check'} = 0;
 4895:             }
 4896:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 4897:                 $defaults{'intauth_switch'} = 0;
 4898:             }
 4899:         } else {
 4900:             %defaults = (
 4901:                           'intauth_cost'   => 10,
 4902:                           'intauth_check'  => 0,
 4903:                           'intauth_switch' => 0,
 4904:                         );
 4905:         }
 4906:         foreach my $item (@items) {
 4907:             if ($itemcount%2) {
 4908:                 $css_class = '';
 4909:             } else {
 4910:                 $css_class = ' class="LC_odd_row" ';
 4911:             }
 4912:             $datatable .= '<tr'.$css_class.'>'.
 4913:                           '<td><span class="LC_nobreak">'.$titles{$item}.
 4914:                           '</span></td><td class="LC_left_item" colspan="3">';
 4915:             if ($item eq 'intauth_switch') {
 4916:                 my @options = (0,1,2);
 4917:                 my %optiondesc = &Apache::lonlocal::texthash (
 4918:                                    0 => 'No',
 4919:                                    1 => 'Yes',
 4920:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 4921:                                  );
 4922:                 $datatable .= '<table width="100%">';
 4923:                 foreach my $option (@options) {
 4924:                     my $checked = ' ';
 4925:                     if ($defaults{$item} eq $option) {
 4926:                         $checked = ' checked="checked"';
 4927:                     }
 4928:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 4929:                                   '<label><input type="radio" name="'.$item.
 4930:                                   '" value="'.$option.'"'.$checked.' />'.
 4931:                                   $optiondesc{$option}.'</label></span></td></tr>';
 4932:                 }
 4933:                 $datatable .= '</table>';
 4934:             } elsif ($item eq 'intauth_check') {
 4935:                 my @options = (0,1,2);
 4936:                 my %optiondesc = &Apache::lonlocal::texthash (
 4937:                                    0 => 'No',
 4938:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 4939:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 4940:                                  );
 4941:                 $datatable .= '<table width="100%">';
 4942:                 foreach my $option (@options) {
 4943:                     my $checked = ' ';
 4944:                     my $onclick;
 4945:                     if ($defaults{$item} eq $option) {
 4946:                         $checked = ' checked="checked"';
 4947:                     }
 4948:                     if ($option == 2) {
 4949:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 4950:                     }
 4951:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 4952:                                   '<label><input type="radio" name="'.$item.
 4953:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 4954:                                   $optiondesc{$option}.'</label></span></td></tr>';
 4955:                 }
 4956:                 $datatable .= '</table>';
 4957:             } else {
 4958:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 4959:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />';
 4960:             }
 4961:             $datatable .= '</td></tr>';
 4962:             $itemcount ++;
 4963:         }
 4964:     } elsif ($position eq 'lower') {
 4965:         my ($min,$max,%chars,$numsaved);
 4966:         $min = $Apache::lonnet::passwdmin;
 4967:         if (ref($settings) eq 'HASH') {
 4968:             if ($settings->{min}) {
 4969:                 $min = $settings->{min};
 4970:             }
 4971:             if ($settings->{max}) {
 4972:                 $max = $settings->{max};
 4973:             }
 4974:             if (ref($settings->{chars}) eq 'ARRAY') {
 4975:                 map { $chars{$_} = 1; } (@{$settings->{chars}});
 4976:             }
 4977:             if ($settings->{numsaved}) {
 4978:                 $numsaved = $settings->{numsaved};
 4979:             }
 4980:         }
 4981:         my %rulenames = &Apache::lonlocal::texthash(
 4982:                                                      uc => 'At least one upper case letter',
 4983:                                                      lc => 'At least one lower case letter',
 4984:                                                      num => 'At least one number',
 4985:                                                      spec => 'At least one non-alphanumeric',
 4986:                                                    );
 4987:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4988:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'min'}.'</td>'.
 4989:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 4990:                       '<input type="text" name="passwords_min" value="'.$min.'" size="3" '.
 4991:                       'onblur="javascript:warnIntPass(this);" />'.
 4992:                       '<span class="LC_fontsize_small"> '.&mt('(Enter an integer: 7 or larger)').'</span>'.
 4993:                       '</span></td></tr>';
 4994:         $itemcount ++;
 4995:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4996:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'max'}.'</td>'.
 4997:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 4998:                       '<input type="text" name="passwords_max" value="'.$max.'" size="3" '.
 4999:                       'onblur="javascript:warnIntPass(this);" />'.
 5000:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no maximum)').'</span>'.
 5001:                       '</span></td></tr>';
 5002:         $itemcount ++;
 5003:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5004:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'chars'}.'<br />'.
 5005:                       '<span class="LC_nobreak LC_fontsize_small">'.&mt('(Leave unchecked if not required)').
 5006:                       '</span></td>';
 5007:         my $numinrow = 2;
 5008:         my @possrules = ('uc','lc','num','spec');
 5009:         $datatable .= '<td class="LC_left_item"><table>';
 5010:         for (my $i=0; $i<@possrules; $i++) {
 5011:             my ($rem,$checked);
 5012:             if ($chars{$possrules[$i]}) {
 5013:                 $checked = ' checked="checked"';
 5014:             }
 5015:             $rem = $i%($numinrow);
 5016:             if ($rem == 0) {
 5017:                 if ($i > 0) {
 5018:                     $datatable .= '</tr>';
 5019:                 }
 5020:                 $datatable .= '<tr>';
 5021:             }
 5022:             $datatable .= '<td><span class="LC_nobreak"><label>'.
 5023:                           '<input type="checkbox" name="passwords_chars" value="'.$possrules[$i].'"'.$checked.' />'.
 5024:                           $rulenames{$possrules[$i]}.'</label></span></td>';
 5025:         }
 5026:         my $rem = @possrules%($numinrow);
 5027:         my $colsleft = $numinrow - $rem;
 5028:         if ($colsleft > 1 ) {
 5029:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5030:                           '&nbsp;</td>';
 5031:         } elsif ($colsleft == 1) {
 5032:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 5033:         }
 5034:         $datatable .='</table></td></tr>';
 5035:         $itemcount ++;
 5036:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5037:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'numsaved'}.'</td>'.
 5038:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 5039:                       '<input type="text" name="passwords_numsaved" value="'.$numsaved.'" size="3" '.
 5040:                       'onblur="javascript:warnIntPass(this);" />'.
 5041:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank to not save previous passwords)').'</span>'.
 5042:                       '</span></td></tr>';
 5043:     } else {
 5044:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5045:         my %ownerchg = (
 5046:                           by  => {},
 5047:                           for => {},
 5048:                        );
 5049:         my %ownertitles = &Apache::lonlocal::texthash (
 5050:                             by  => 'Course owner status(es) allowed',
 5051:                             for => 'Student status(es) allowed',
 5052:                           );
 5053:         if (ref($settings) eq 'HASH') {
 5054:             if (ref($settings->{crsownerchg}) eq 'HASH') {
 5055:                 if (ref($settings->{crsownerchg}{'by'}) eq 'ARRAY') {
 5056:                     map { $ownerchg{by}{$_} = 1; } (@{$settings->{crsownerchg}{'by'}});
 5057:                 }
 5058:                 if (ref($settings->{crsownerchg}{'for'}) eq 'ARRAY') {
 5059:                     map { $ownerchg{for}{$_} = 1; } (@{$settings->{crsownerchg}{'for'}});
 5060:                 }
 5061:             }
 5062:         }
 5063:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5064:         $datatable .= '<tr '.$css_class.'>'.
 5065:                       '<td>'.
 5066:                       &mt('Requirements').'<ul>'.
 5067:                       '<li>'.&mt("Course 'type' is not a Community").'</li>'.
 5068:                       '<li>'.&mt('User is Course Coordinator and also course owner').'</li>'.
 5069:                       '<li>'.&mt("Student's only active roles are student role(s) in course(s) owned by this user").'</li>'.
 5070:                       '<li>'.&mt('User, course, and student share same domain').'</li>'.
 5071:                       '</ul>'.
 5072:                       '</td>'.
 5073:                       '<td class="LC_left_item">';
 5074:         foreach my $item ('by','for') {
 5075:             $datatable .= '<fieldset style="display: inline-block;">'.
 5076:                           '<legend>'.$ownertitles{$item}.'</legend>';
 5077:             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 5078:                 foreach my $type (@{$types}) {
 5079:                     my $checked;
 5080:                     if ($ownerchg{$item}{$type}) {
 5081:                         $checked = ' checked="checked"';
 5082:                     }
 5083:                     $datatable .= '<span class="LC_nobreak"><label>'.
 5084:                                   '<input type="checkbox" name="passwords_crsowner_'.$item.'" value="'.
 5085:                                   $type.'"'.$checked.' />'.$usertypes->{$type}.'</label>'.
 5086:                                   '</span>&nbsp;&nbsp; ';
 5087:                 }
 5088:             }
 5089:             my $checked;
 5090:             if ($ownerchg{$item}{'default'}) {
 5091:                 $checked = ' checked="checked"';
 5092:             }
 5093:             $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 5094:                           'name="passwords_crsowner_'.$item.'" value="default"'.$checked.' />'.
 5095:                           $othertitle.'</label></span></fieldset>';
 5096:         }
 5097:         $datatable .= '</td></tr>';
 5098:     }
 5099:     return $datatable;
 5100: }
 5101: 
 5102: sub print_usersessions {
 5103:     my ($position,$dom,$settings,$rowtotal) = @_;
 5104:     my ($css_class,$datatable,%checked,%choices);
 5105:     my (%by_ip,%by_location,@intdoms);
 5106:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 5107: 
 5108:     my @alldoms = &Apache::lonnet::all_domains();
 5109:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 5110:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5111:     my %altids = &id_for_thisdom(%servers);
 5112:     my $itemcount = 1;
 5113:     if ($position eq 'top') {
 5114:         if (keys(%serverhomes) > 1) {
 5115:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 5116:             my $curroffloadnow;
 5117:             if (ref($settings) eq 'HASH') {
 5118:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 5119:                     $curroffloadnow = $settings->{'offloadnow'};
 5120:                 }
 5121:             }
 5122:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$curroffloadnow,$rowtotal);
 5123:         } else {
 5124:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 5125:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 5126:         }
 5127:     } else {
 5128:         if (keys(%by_location) == 0) {
 5129:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 5130:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 5131:         } else {
 5132:             my %lt = &usersession_titles();
 5133:             my $numinrow = 5;
 5134:             my $prefix;
 5135:             my @types;
 5136:             if ($position eq 'bottom') {
 5137:                 $prefix = 'remote';
 5138:                 @types = ('version','excludedomain','includedomain');
 5139:             } else {
 5140:                 $prefix = 'hosted';
 5141:                 @types = ('excludedomain','includedomain');
 5142:             }
 5143:             my (%current,%checkedon,%checkedoff);
 5144:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 5145:             my @locations = sort(keys(%by_location));
 5146:             foreach my $type (@types) {
 5147:                 $checkedon{$type} = '';
 5148:                 $checkedoff{$type} = ' checked="checked"';
 5149:             }
 5150:             if (ref($settings) eq 'HASH') {
 5151:                 if (ref($settings->{$prefix}) eq 'HASH') {
 5152:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 5153:                         $current{$key} = $settings->{$prefix}{$key};
 5154:                         if ($key eq 'version') {
 5155:                             if ($current{$key} ne '') {
 5156:                                 $checkedon{$key} = ' checked="checked"';
 5157:                                 $checkedoff{$key} = '';
 5158:                             }
 5159:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 5160:                             $checkedon{$key} = ' checked="checked"';
 5161:                             $checkedoff{$key} = '';
 5162:                         }
 5163:                     }
 5164:                 }
 5165:             }
 5166:             foreach my $type (@types) {
 5167:                 next if ($type ne 'version' && !@locations);
 5168:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5169:                 $datatable .= '<tr'.$css_class.'>
 5170:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 5171:                                <span class="LC_nobreak">&nbsp;
 5172:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 5173:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 5174:                 if ($type eq 'version') {
 5175:                     my $selector = '<select name="'.$prefix.'_version">';
 5176:                     foreach my $version (@lcversions) {
 5177:                         my $selected = '';
 5178:                         if ($current{'version'} eq $version) {
 5179:                             $selected = ' selected="selected"';
 5180:                         }
 5181:                         $selector .= ' <option value="'.$version.'"'.
 5182:                                      $selected.'>'.$version.'</option>';
 5183:                     }
 5184:                     $selector .= '</select> ';
 5185:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 5186:                 } else {
 5187:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 5188:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 5189:                                  ' />'.('&nbsp;'x2).
 5190:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 5191:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 5192:                                  "\n".
 5193:                                  '</div><div><table>';
 5194:                     my $rem;
 5195:                     for (my $i=0; $i<@locations; $i++) {
 5196:                         my ($showloc,$value,$checkedtype);
 5197:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 5198:                             my $ip = $by_location{$locations[$i]}->[0];
 5199:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 5200:                                  $value = join(':',@{$by_ip{$ip}});
 5201:                                 $showloc = join(', ',@{$by_ip{$ip}});
 5202:                                 if (ref($current{$type}) eq 'ARRAY') {
 5203:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 5204:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 5205:                                             $checkedtype = ' checked="checked"';
 5206:                                             last;
 5207:                                         }
 5208:                                     }
 5209:                                 }
 5210:                             }
 5211:                         }
 5212:                         $rem = $i%($numinrow);
 5213:                         if ($rem == 0) {
 5214:                             if ($i > 0) {
 5215:                                 $datatable .= '</tr>';
 5216:                             }
 5217:                             $datatable .= '<tr>';
 5218:                         }
 5219:                         $datatable .= '<td class="LC_left_item">'.
 5220:                                       '<span class="LC_nobreak"><label>'.
 5221:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 5222:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 5223:                                       '</label></span></td>';
 5224:                     }
 5225:                     $rem = @locations%($numinrow);
 5226:                     my $colsleft = $numinrow - $rem;
 5227:                     if ($colsleft > 1 ) {
 5228:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5229:                                       '&nbsp;</td>';
 5230:                     } elsif ($colsleft == 1) {
 5231:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 5232:                     }
 5233:                     $datatable .= '</tr></table>';
 5234:                 }
 5235:                 $datatable .= '</td></tr>';
 5236:                 $itemcount ++;
 5237:             }
 5238:         }
 5239:     }
 5240:     $$rowtotal += $itemcount;
 5241:     return $datatable;
 5242: }
 5243: 
 5244: sub build_location_hashes {
 5245:     my ($intdoms,$by_ip,$by_location) = @_;
 5246:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 5247:                   (ref($by_location) eq 'HASH')); 
 5248:     my %iphost = &Apache::lonnet::get_iphost();
 5249:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 5250:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 5251:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 5252:         foreach my $id (@{$iphost{$primary_ip}}) {
 5253:             my $intdom = &Apache::lonnet::internet_dom($id);
 5254:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 5255:                 push(@{$intdoms},$intdom);
 5256:             }
 5257:         }
 5258:     }
 5259:     foreach my $ip (keys(%iphost)) {
 5260:         if (ref($iphost{$ip}) eq 'ARRAY') {
 5261:             foreach my $id (@{$iphost{$ip}}) {
 5262:                 my $location = &Apache::lonnet::internet_dom($id);
 5263:                 if ($location) {
 5264:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 5265:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 5266:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 5267:                             push(@{$by_ip->{$ip}},$location);
 5268:                         }
 5269:                     } else {
 5270:                         $by_ip->{$ip} = [$location];
 5271:                     }
 5272:                 }
 5273:             }
 5274:         }
 5275:     }
 5276:     foreach my $ip (sort(keys(%{$by_ip}))) {
 5277:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 5278:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 5279:             my $first = $by_ip->{$ip}->[0];
 5280:             if (ref($by_location->{$first}) eq 'ARRAY') {
 5281:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 5282:                     push(@{$by_location->{$first}},$ip);
 5283:                 }
 5284:             } else {
 5285:                 $by_location->{$first} = [$ip];
 5286:             }
 5287:         }
 5288:     }
 5289:     return;
 5290: }
 5291: 
 5292: sub current_offloads_to {
 5293:     my ($dom,$settings,$servers) = @_;
 5294:     my (%spareid,%otherdomconfigs);
 5295:     if (ref($servers) eq 'HASH') {
 5296:         foreach my $lonhost (sort(keys(%{$servers}))) {
 5297:             my $gotspares;
 5298:             if (ref($settings) eq 'HASH') {
 5299:                 if (ref($settings->{'spares'}) eq 'HASH') {
 5300:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 5301:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 5302:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 5303:                         $gotspares = 1;
 5304:                     }
 5305:                 }
 5306:             }
 5307:             unless ($gotspares) {
 5308:                 my $gotspares;
 5309:                 my $serverhomeID =
 5310:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 5311:                 my $serverhomedom =
 5312:                     &Apache::lonnet::host_domain($serverhomeID);
 5313:                 if ($serverhomedom ne $dom) {
 5314:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 5315:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 5316:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 5317:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 5318:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 5319:                                 $gotspares = 1;
 5320:                             }
 5321:                         }
 5322:                     } else {
 5323:                         $otherdomconfigs{$serverhomedom} =
 5324:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 5325:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 5326:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 5327:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 5328:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 5329:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 5330:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 5331:                                         $gotspares = 1;
 5332:                                     }
 5333:                                 }
 5334:                             }
 5335:                         }
 5336:                     }
 5337:                 }
 5338:             }
 5339:             unless ($gotspares) {
 5340:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 5341:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 5342:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 5343:                } else {
 5344:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 5345:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 5346:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 5347:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 5348:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 5349:                     } else {
 5350:                         my %what = (
 5351:                              spareid => 1,
 5352:                         );
 5353:                         my ($result,$returnhash) = 
 5354:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 5355:                         if ($result eq 'ok') { 
 5356:                             if (ref($returnhash) eq 'HASH') {
 5357:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 5358:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 5359:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 5360:                                 }
 5361:                             }
 5362:                         }
 5363:                     }
 5364:                 }
 5365:             }
 5366:         }
 5367:     }
 5368:     return %spareid;
 5369: }
 5370: 
 5371: sub spares_row {
 5372:     my ($dom,$servers,$spareid,$serverhomes,$altids,$curroffloadnow,$rowtotal) = @_;
 5373:     my $css_class;
 5374:     my $numinrow = 4;
 5375:     my $itemcount = 1;
 5376:     my $datatable;
 5377:     my %typetitles = &sparestype_titles();
 5378:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 5379:         foreach my $server (sort(keys(%{$servers}))) {
 5380:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 5381:             my ($othercontrol,$serverdom);
 5382:             if ($serverhome ne $server) {
 5383:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 5384:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 5385:             } else {
 5386:                 $serverdom = &Apache::lonnet::host_domain($server);
 5387:                 if ($serverdom ne $dom) {
 5388:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 5389:                 }
 5390:             }
 5391:             next unless (ref($spareid->{$server}) eq 'HASH');
 5392:             my $checkednow;
 5393:             if (ref($curroffloadnow) eq 'HASH') {
 5394:                 if ($curroffloadnow->{$server}) {
 5395:                     $checkednow = ' checked="checked"';
 5396:                 }
 5397:             }
 5398:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5399:             $datatable .= '<tr'.$css_class.'>
 5400:                            <td rowspan="2">
 5401:                             <span class="LC_nobreak">'.
 5402:                           &mt('[_1] when busy, offloads to:'
 5403:                               ,'<b>'.$server.'</b>').'</span><br />'.
 5404:                           '<span class="LC_nobreak">'."\n".
 5405:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 5406:                           '&nbsp;'.&mt('Switch active users on next access').'</label></span>'.
 5407:                           "\n";
 5408:             my (%current,%canselect);
 5409:             my @choices = 
 5410:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 5411:             foreach my $type ('primary','default') {
 5412:                 if (ref($spareid->{$server}) eq 'HASH') {
 5413:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 5414:                         my @spares = @{$spareid->{$server}{$type}};
 5415:                         if (@spares > 0) {
 5416:                             if ($othercontrol) {
 5417:                                 $current{$type} = join(', ',@spares);
 5418:                             } else {
 5419:                                 $current{$type} .= '<table>';
 5420:                                 my $numspares = scalar(@spares);
 5421:                                 for (my $i=0;  $i<@spares; $i++) {
 5422:                                     my $rem = $i%($numinrow);
 5423:                                     if ($rem == 0) {
 5424:                                         if ($i > 0) {
 5425:                                             $current{$type} .= '</tr>';
 5426:                                         }
 5427:                                         $current{$type} .= '<tr>';
 5428:                                     }
 5429:                                     $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;'.
 5430:                                                        $spareid->{$server}{$type}[$i].
 5431:                                                        '</label></td>'."\n";
 5432:                                 }
 5433:                                 my $rem = @spares%($numinrow);
 5434:                                 my $colsleft = $numinrow - $rem;
 5435:                                 if ($colsleft > 1 ) {
 5436:                                     $current{$type} .= '<td colspan="'.$colsleft.
 5437:                                                        '" class="LC_left_item">'.
 5438:                                                        '&nbsp;</td>';
 5439:                                 } elsif ($colsleft == 1) {
 5440:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 5441:                                 }
 5442:                                 $current{$type} .= '</tr></table>';
 5443:                             }
 5444:                         }
 5445:                     }
 5446:                     if ($current{$type} eq '') {
 5447:                         $current{$type} = &mt('None specified');
 5448:                     }
 5449:                     if ($othercontrol) {
 5450:                         if ($type eq 'primary') {
 5451:                             $canselect{$type} = $othercontrol;
 5452:                         }
 5453:                     } else {
 5454:                         $canselect{$type} = 
 5455:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 5456:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 5457:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 5458:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 5459:                         if (@choices > 0) {
 5460:                             foreach my $lonhost (@choices) {
 5461:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 5462:                             }
 5463:                         }
 5464:                         $canselect{$type} .= '</select>'."\n";
 5465:                     }
 5466:                 } else {
 5467:                     $current{$type} = &mt('Could not be determined');
 5468:                     if ($type eq 'primary') {
 5469:                         $canselect{$type} =  $othercontrol;
 5470:                     }
 5471:                 }
 5472:                 if ($type eq 'default') {
 5473:                     $datatable .= '<tr'.$css_class.'>';
 5474:                 }
 5475:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 5476:                               '<td>'.$current{$type}.'</td>'."\n".
 5477:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 5478:             }
 5479:             $itemcount ++;
 5480:         }
 5481:     }
 5482:     $$rowtotal += $itemcount;
 5483:     return $datatable;
 5484: }
 5485: 
 5486: sub possible_newspares {
 5487:     my ($server,$currspares,$serverhomes,$altids) = @_;
 5488:     my $serverhostname = &Apache::lonnet::hostname($server);
 5489:     my %excluded;
 5490:     if ($serverhostname ne '') {
 5491:         %excluded = (
 5492:                        $serverhostname => 1,
 5493:                     );
 5494:     }
 5495:     if (ref($currspares) eq 'HASH') {
 5496:         foreach my $type (keys(%{$currspares})) {
 5497:             if (ref($currspares->{$type}) eq 'ARRAY') {
 5498:                 if (@{$currspares->{$type}} > 0) {
 5499:                     foreach my $curr (@{$currspares->{$type}}) {
 5500:                         my $hostname = &Apache::lonnet::hostname($curr);
 5501:                         $excluded{$hostname} = 1;
 5502:                     }
 5503:                 }
 5504:             }
 5505:         }
 5506:     }
 5507:     my @choices;
 5508:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 5509:         if (keys(%{$serverhomes}) > 1) {
 5510:             foreach my $name (sort(keys(%{$serverhomes}))) {
 5511:                 unless ($excluded{$name}) {
 5512:                     if (exists($altids->{$serverhomes->{$name}})) {
 5513:                         push(@choices,$altids->{$serverhomes->{$name}});
 5514:                     } else {
 5515:                         push(@choices,$serverhomes->{$name});
 5516:                     }
 5517:                 }
 5518:             }
 5519:         }
 5520:     }
 5521:     return sort(@choices);
 5522: }
 5523: 
 5524: sub print_loadbalancing {
 5525:     my ($dom,$settings,$rowtotal) = @_;
 5526:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 5527:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 5528:     my $numinrow = 1;
 5529:     my $datatable;
 5530:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5531:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
 5532:     if (ref($settings) eq 'HASH') {
 5533:         %existing = %{$settings};
 5534:     }
 5535:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 5536:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 5537:                                   \%currtargets,\%currrules,\%currcookies);
 5538:     } else {
 5539:         return;
 5540:     }
 5541:     my ($othertitle,$usertypes,$types) =
 5542:         &Apache::loncommon::sorted_inst_types($dom);
 5543:     my $rownum = 8;
 5544:     if (ref($types) eq 'ARRAY') {
 5545:         $rownum += scalar(@{$types});
 5546:     }
 5547:     my @css_class = ('LC_odd_row','LC_even_row');
 5548:     my $balnum = 0;
 5549:     my $islast;
 5550:     my (@toshow,$disabledtext);
 5551:     if (keys(%currbalancer) > 0) {
 5552:         @toshow = sort(keys(%currbalancer));
 5553:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 5554:             push(@toshow,'');
 5555:         }
 5556:     } else {
 5557:         @toshow = ('');
 5558:         $disabledtext = &mt('No existing load balancer');
 5559:     }
 5560:     foreach my $lonhost (@toshow) {
 5561:         if ($balnum == scalar(@toshow)-1) {
 5562:             $islast = 1;
 5563:         } else {
 5564:             $islast = 0;
 5565:         }
 5566:         my $cssidx = $balnum%2;
 5567:         my $targets_div_style = 'display: none';
 5568:         my $disabled_div_style = 'display: block';
 5569:         my $homedom_div_style = 'display: none';
 5570:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 5571:                       '<td rowspan="'.$rownum.'" valign="top">'.
 5572:                       '<p>';
 5573:         if ($lonhost eq '') {
 5574:             $datatable .= '<span class="LC_nobreak">';
 5575:             if (keys(%currbalancer) > 0) {
 5576:                 $datatable .= &mt('Add balancer:');
 5577:             } else {
 5578:                 $datatable .= &mt('Enable balancer:');
 5579:             }
 5580:             $datatable .= '&nbsp;'.
 5581:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 5582:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 5583:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 5584:                           '<option value="" selected="selected">'.&mt('None').
 5585:                           '</option>'."\n";
 5586:             foreach my $server (sort(keys(%servers))) {
 5587:                 next if ($currbalancer{$server});
 5588:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 5589:             }
 5590:             $datatable .=
 5591:                 '</select>'."\n".
 5592:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 5593:         } else {
 5594:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 5595:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 5596:                            &mt('Stop balancing').'</label>'.
 5597:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 5598:             $targets_div_style = 'display: block';
 5599:             $disabled_div_style = 'display: none';
 5600:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 5601:                 $homedom_div_style = 'display: block';
 5602:             }
 5603:         }
 5604:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 5605:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 5606:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 5607:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 5608:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 5609:         my @sparestypes = ('primary','default');
 5610:         my %typetitles = &sparestype_titles();
 5611:         my %hostherechecked = (
 5612:                                   no => ' checked="checked"',
 5613:                               );
 5614:         my %balcookiechecked = (
 5615:                                   no => ' checked="checked"',
 5616:                                );
 5617:         foreach my $sparetype (@sparestypes) {
 5618:             my $targettable;
 5619:             for (my $i=0; $i<$numspares; $i++) {
 5620:                 my $checked;
 5621:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 5622:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5623:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5624:                             $checked = ' checked="checked"';
 5625:                         }
 5626:                     }
 5627:                 }
 5628:                 my ($chkboxval,$disabled);
 5629:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 5630:                     $chkboxval = $spares[$i];
 5631:                 }
 5632:                 if (exists($currbalancer{$spares[$i]})) {
 5633:                     $disabled = ' disabled="disabled"';
 5634:                 }
 5635:                 $targettable .=
 5636:                     '<td><span class="LC_nobreak"><label>'.
 5637:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 5638:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 5639:                     '</span></label></span></td>';
 5640:                 my $rem = $i%($numinrow);
 5641:                 if ($rem == 0) {
 5642:                     if (($i > 0) && ($i < $numspares-1)) {
 5643:                         $targettable .= '</tr>';
 5644:                     }
 5645:                     if ($i < $numspares-1) {
 5646:                         $targettable .= '<tr>';
 5647:                     }
 5648:                 }
 5649:             }
 5650:             if ($targettable ne '') {
 5651:                 my $rem = $numspares%($numinrow);
 5652:                 my $colsleft = $numinrow - $rem;
 5653:                 if ($colsleft > 1 ) {
 5654:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5655:                                     '&nbsp;</td>';
 5656:                 } elsif ($colsleft == 1) {
 5657:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 5658:                 }
 5659:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 5660:                                '<table><tr>'.$targettable.'</tr></table><br />';
 5661:             }
 5662:             $hostherechecked{$sparetype} = '';
 5663:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 5664:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5665:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5666:                         $hostherechecked{$sparetype} = ' checked="checked"';
 5667:                         $hostherechecked{'no'} = '';
 5668:                     }
 5669:                 }
 5670:             }
 5671:         }
 5672:         if ($currcookies{$lonhost}) {
 5673:             %balcookiechecked = (
 5674:                                     yes => ' checked="checked"',
 5675:                                 );
 5676:         }
 5677:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 5678:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 5679:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 5680:         foreach my $sparetype (@sparestypes) {
 5681:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 5682:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 5683:                           '</i></label><br />';
 5684:         }
 5685:         $datatable .= &mt('Use balancer cookie').'<br />'.
 5686:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="1"'.
 5687:                       $balcookiechecked{'yes'}.' />'.&mt('Yes').'</label><br />'.
 5688:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="0"'.
 5689:                       $balcookiechecked{'no'}.' />'.&mt('No').'</label><br />'.
 5690:                       '</div></td></tr>'.
 5691:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 5692:                                            $othertitle,$usertypes,$types,\%servers,
 5693:                                            \%currbalancer,$lonhost,
 5694:                                            $targets_div_style,$homedom_div_style,
 5695:                                            $css_class[$cssidx],$balnum,$islast);
 5696:         $$rowtotal += $rownum;
 5697:         $balnum ++;
 5698:     }
 5699:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 5700:     return $datatable;
 5701: }
 5702: 
 5703: sub get_loadbalancers_config {
 5704:     my ($servers,$existing,$currbalancer,$currtargets,$currrules,$currcookies) = @_;
 5705:     return unless ((ref($servers) eq 'HASH') &&
 5706:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 5707:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH') &&
 5708:                    (ref($currcookies) eq 'HASH'));
 5709:     if (keys(%{$existing}) > 0) {
 5710:         my $oldlonhost;
 5711:         foreach my $key (sort(keys(%{$existing}))) {
 5712:             if ($key eq 'lonhost') {
 5713:                 $oldlonhost = $existing->{'lonhost'};
 5714:                 $currbalancer->{$oldlonhost} = 1;
 5715:             } elsif ($key eq 'targets') {
 5716:                 if ($oldlonhost) {
 5717:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 5718:                 }
 5719:             } elsif ($key eq 'rules') {
 5720:                 if ($oldlonhost) {
 5721:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 5722:                 }
 5723:             } elsif (ref($existing->{$key}) eq 'HASH') {
 5724:                 $currbalancer->{$key} = 1;
 5725:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 5726:                 $currrules->{$key} = $existing->{$key}{'rules'};
 5727:                 if ($existing->{$key}{'cookie'}) {
 5728:                     $currcookies->{$key} = 1;
 5729:                 }
 5730:             }
 5731:         }
 5732:     } else {
 5733:         my ($balancerref,$targetsref) =
 5734:                 &Apache::lonnet::get_lonbalancer_config($servers);
 5735:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 5736:             foreach my $server (sort(keys(%{$balancerref}))) {
 5737:                 $currbalancer->{$server} = 1;
 5738:                 $currtargets->{$server} = $targetsref->{$server};
 5739:             }
 5740:         }
 5741:     }
 5742:     return;
 5743: }
 5744: 
 5745: sub loadbalancing_rules {
 5746:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 5747:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 5748:         $css_class,$balnum,$islast) = @_;
 5749:     my $output;
 5750:     my $num = 0;
 5751:     my ($alltypes,$othertypes,$titles) =
 5752:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 5753:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 5754:         foreach my $type (@{$alltypes}) {
 5755:             $num ++;
 5756:             my $current;
 5757:             if (ref($currrules) eq 'HASH') {
 5758:                 $current = $currrules->{$type};
 5759:             }
 5760:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5761:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 5762:                     $current = '';
 5763:                 }
 5764:             }
 5765:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 5766:                                              $servers,$currbalancer,$lonhost,$dom,
 5767:                                              $targets_div_style,$homedom_div_style,
 5768:                                              $css_class,$balnum,$num,$islast);
 5769:         }
 5770:     }
 5771:     return $output;
 5772: }
 5773: 
 5774: sub loadbalancing_titles {
 5775:     my ($dom,$intdom,$usertypes,$types) = @_;
 5776:     my %othertypes = (
 5777:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 5778:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 5779:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 5780:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 5781:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 5782:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 5783:                      );
 5784:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 5785:     my @available;
 5786:     if (ref($types) eq 'ARRAY') {
 5787:         @available = @{$types};
 5788:     }
 5789:     unless (grep(/^default$/,@available)) {
 5790:         push(@available,'default');
 5791:     }
 5792:     unshift(@alltypes,@available);
 5793:     my %titles;
 5794:     foreach my $type (@alltypes) {
 5795:         if ($type =~ /^_LC_/) {
 5796:             $titles{$type} = $othertypes{$type};
 5797:         } elsif ($type eq 'default') {
 5798:             $titles{$type} = &mt('All users from [_1]',$dom);
 5799:             if (ref($types) eq 'ARRAY') {
 5800:                 if (@{$types} > 0) {
 5801:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 5802:                 }
 5803:             }
 5804:         } elsif (ref($usertypes) eq 'HASH') {
 5805:             $titles{$type} = $usertypes->{$type};
 5806:         }
 5807:     }
 5808:     return (\@alltypes,\%othertypes,\%titles);
 5809: }
 5810: 
 5811: sub loadbalance_rule_row {
 5812:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 5813:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 5814:     my @rulenames;
 5815:     my %ruletitles = &offloadtype_text();
 5816:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 5817:         @rulenames = ('balancer','offloadedto','specific');
 5818:     } else {
 5819:         @rulenames = ('default','homeserver');
 5820:         if ($type eq '_LC_external') {
 5821:             push(@rulenames,'externalbalancer');
 5822:         } else {
 5823:             push(@rulenames,'specific');
 5824:         }
 5825:         push(@rulenames,'none');
 5826:     }
 5827:     my $style = $targets_div_style;
 5828:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5829:         $style = $homedom_div_style;
 5830:     }
 5831:     my $space;
 5832:     if ($islast && $num == 1) {
 5833:         $space = '<div display="inline-block">&nbsp;</div>';
 5834:     }
 5835:     my $output =
 5836:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 5837:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 5838:         '<td valaign="top">'.$space.
 5839:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 5840:     for (my $i=0; $i<@rulenames; $i++) {
 5841:         my $rule = $rulenames[$i];
 5842:         my ($checked,$extra);
 5843:         if ($rulenames[$i] eq 'default') {
 5844:             $rule = '';
 5845:         }
 5846:         if ($rulenames[$i] eq 'specific') {
 5847:             if (ref($servers) eq 'HASH') {
 5848:                 my $default;
 5849:                 if (($current ne '') && (exists($servers->{$current}))) {
 5850:                     $checked = ' checked="checked"';
 5851:                 }
 5852:                 unless ($checked) {
 5853:                     $default = ' selected="selected"';
 5854:                 }
 5855:                 $extra =
 5856:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5857:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5858:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 5859:                     '<option value=""'.$default.'></option>'."\n";
 5860:                 foreach my $server (sort(keys(%{$servers}))) {
 5861:                     if (ref($currbalancer) eq 'HASH') {
 5862:                         next if (exists($currbalancer->{$server}));
 5863:                     }
 5864:                     my $selected;
 5865:                     if ($server eq $current) {
 5866:                         $selected = ' selected="selected"';
 5867:                     }
 5868:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 5869:                 }
 5870:                 $extra .= '</select>';
 5871:             }
 5872:         } elsif ($rule eq $current) {
 5873:             $checked = ' checked="checked"';
 5874:         }
 5875:         $output .= '<span class="LC_nobreak"><label>'.
 5876:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 5877:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 5878:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 5879:                    ')"'.$checked.' />&nbsp;';
 5880:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 5881:             $output .= $ruletitles{'particular'};
 5882:         } else {
 5883:             $output .= $ruletitles{$rulenames[$i]};
 5884:         }
 5885:         $output .= '</label>'.$extra.'</span><br />'."\n";
 5886:     }
 5887:     $output .= '</div></td></tr>'."\n";
 5888:     return $output;
 5889: }
 5890: 
 5891: sub offloadtype_text {
 5892:     my %ruletitles = &Apache::lonlocal::texthash (
 5893:            'default'          => 'Offloads to default destinations',
 5894:            'homeserver'       => "Offloads to user's home server",
 5895:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 5896:            'specific'         => 'Offloads to specific server',
 5897:            'none'             => 'No offload',
 5898:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 5899:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 5900:            'particular'       => 'Session hosted (after re-auth) on server:',
 5901:     );
 5902:     return %ruletitles;
 5903: }
 5904: 
 5905: sub sparestype_titles {
 5906:     my %typestitles = &Apache::lonlocal::texthash (
 5907:                           'primary' => 'primary',
 5908:                           'default' => 'default',
 5909:                       );
 5910:     return %typestitles;
 5911: }
 5912: 
 5913: sub contact_titles {
 5914:     my %titles = &Apache::lonlocal::texthash (
 5915:                    'supportemail'    => 'Support E-mail address',
 5916:                    'adminemail'      => 'Default Server Admin E-mail address',
 5917:                    'errormail'       => 'Error reports to be e-mailed to',
 5918:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 5919:                    'helpdeskmail'    => "Helpdesk requests from all users in this domain",
 5920:                    'otherdomsmail'   => 'Helpdesk requests from users in other (unconfigured) domains',
 5921:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 5922:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 5923:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 5924:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 5925:                    'hostipmail'      => 'E-mail from nightly check of hostname/IP network changes',
 5926:                  );
 5927:     my %short_titles = &Apache::lonlocal::texthash (
 5928:                            adminemail   => 'Admin E-mail address',
 5929:                            supportemail => 'Support E-mail',
 5930:                        );   
 5931:     return (\%titles,\%short_titles);
 5932: }
 5933: 
 5934: sub helpform_fields {
 5935:     my %titles =  &Apache::lonlocal::texthash (
 5936:                        'username'   => 'Name',
 5937:                        'user'       => 'Username/domain',
 5938:                        'phone'      => 'Phone',
 5939:                        'cc'         => 'Cc e-mail',
 5940:                        'course'     => 'Course Details',
 5941:                        'section'    => 'Sections',
 5942:                        'screenshot' => 'File upload',
 5943:     );
 5944:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 5945:     my %possoptions = (
 5946:                         username     => ['yes','no','req'],
 5947:                         phone        => ['yes','no','req'],
 5948:                         user         => ['yes','no'],
 5949:                         cc           => ['yes','no'],
 5950:                         course       => ['yes','no'],
 5951:                         section      => ['yes','no'],
 5952:                         screenshot   => ['yes','no'],
 5953:                       );
 5954:     my %fieldoptions = &Apache::lonlocal::texthash (
 5955:                          'yes'  => 'Optional',
 5956:                          'req'  => 'Required',
 5957:                          'no'   => "Not shown",
 5958:     );
 5959:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 5960: }
 5961: 
 5962: sub tool_titles {
 5963:     my %titles = &Apache::lonlocal::texthash (
 5964:                      aboutme    => 'Personal web page',
 5965:                      blog       => 'Blog',
 5966:                      webdav     => 'WebDAV',
 5967:                      portfolio  => 'Portfolio',
 5968:                      official   => 'Official courses (with institutional codes)',
 5969:                      unofficial => 'Unofficial courses',
 5970:                      community  => 'Communities',
 5971:                      textbook   => 'Textbook courses',
 5972:                  );
 5973:     return %titles;
 5974: }
 5975: 
 5976: sub courserequest_titles {
 5977:     my %titles = &Apache::lonlocal::texthash (
 5978:                                    official   => 'Official',
 5979:                                    unofficial => 'Unofficial',
 5980:                                    community  => 'Communities',
 5981:                                    textbook   => 'Textbook',
 5982:                                    norequest  => 'Not allowed',
 5983:                                    approval   => 'Approval by Dom. Coord.',
 5984:                                    validate   => 'With validation',
 5985:                                    autolimit  => 'Numerical limit',
 5986:                                    unlimited  => '(blank for unlimited)',
 5987:                  );
 5988:     return %titles;
 5989: }
 5990: 
 5991: sub authorrequest_titles {
 5992:     my %titles = &Apache::lonlocal::texthash (
 5993:                                    norequest  => 'Not allowed',
 5994:                                    approval   => 'Approval by Dom. Coord.',
 5995:                                    automatic  => 'Automatic approval',
 5996:                  );
 5997:     return %titles;
 5998: }
 5999: 
 6000: sub courserequest_conditions {
 6001:     my %conditions = &Apache::lonlocal::texthash (
 6002:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 6003:        validate   => '(Processing of request subject to institutional validation).',
 6004:                  );
 6005:     return %conditions;
 6006: }
 6007: 
 6008: 
 6009: sub print_usercreation {
 6010:     my ($position,$dom,$settings,$rowtotal) = @_;
 6011:     my $numinrow = 4;
 6012:     my $datatable;
 6013:     if ($position eq 'top') {
 6014:         $$rowtotal ++;
 6015:         my $rowcount = 0;
 6016:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 6017:         if (ref($rules) eq 'HASH') {
 6018:             if (keys(%{$rules}) > 0) {
 6019:                 $datatable .= &user_formats_row('username',$settings,$rules,
 6020:                                                 $ruleorder,$numinrow,$rowcount);
 6021:                 $$rowtotal ++;
 6022:                 $rowcount ++;
 6023:             }
 6024:         }
 6025:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 6026:         if (ref($idrules) eq 'HASH') {
 6027:             if (keys(%{$idrules}) > 0) {
 6028:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 6029:                                                 $idruleorder,$numinrow,$rowcount);
 6030:                 $$rowtotal ++;
 6031:                 $rowcount ++;
 6032:             }
 6033:         }
 6034:         if ($rowcount == 0) {
 6035:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 6036:             $$rowtotal ++;
 6037:             $rowcount ++;
 6038:         }
 6039:     } elsif ($position eq 'middle') {
 6040:         my @creators = ('author','course','requestcrs');
 6041:         my ($rules,$ruleorder) =
 6042:             &Apache::lonnet::inst_userrules($dom,'username');
 6043:         my %lt = &usercreation_types();
 6044:         my %checked;
 6045:         if (ref($settings) eq 'HASH') {
 6046:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 6047:                 foreach my $item (@creators) {
 6048:                     $checked{$item} = $settings->{'cancreate'}{$item};
 6049:                 }
 6050:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 6051:                 foreach my $item (@creators) {
 6052:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 6053:                         $checked{$item} = 'none';
 6054:                     }
 6055:                 }
 6056:             }
 6057:         }
 6058:         my $rownum = 0;
 6059:         foreach my $item (@creators) {
 6060:             $rownum ++;
 6061:             if ($checked{$item} eq '') {
 6062:                 $checked{$item} = 'any';
 6063:             }
 6064:             my $css_class;
 6065:             if ($rownum%2) {
 6066:                 $css_class = '';
 6067:             } else {
 6068:                 $css_class = ' class="LC_odd_row" ';
 6069:             }
 6070:             $datatable .= '<tr'.$css_class.'>'.
 6071:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 6072:                          '</span></td><td align="right">';
 6073:             my @options = ('any');
 6074:             if (ref($rules) eq 'HASH') {
 6075:                 if (keys(%{$rules}) > 0) {
 6076:                     push(@options,('official','unofficial'));
 6077:                 }
 6078:             }
 6079:             push(@options,'none');
 6080:             foreach my $option (@options) {
 6081:                 my $type = 'radio';
 6082:                 my $check = ' ';
 6083:                 if ($checked{$item} eq $option) {
 6084:                     $check = ' checked="checked" ';
 6085:                 } 
 6086:                 $datatable .= '<span class="LC_nobreak"><label>'.
 6087:                               '<input type="'.$type.'" name="can_createuser_'.
 6088:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 6089:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 6090:             }
 6091:             $datatable .= '</td></tr>';
 6092:         }
 6093:     } else {
 6094:         my @contexts = ('author','course','domain');
 6095:         my @authtypes = ('int','krb4','krb5','loc');
 6096:         my %checked;
 6097:         if (ref($settings) eq 'HASH') {
 6098:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 6099:                 foreach my $item (@contexts) {
 6100:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 6101:                         foreach my $auth (@authtypes) {
 6102:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 6103:                                 $checked{$item}{$auth} = ' checked="checked" ';
 6104:                             }
 6105:                         }
 6106:                     }
 6107:                 }
 6108:             }
 6109:         } else {
 6110:             foreach my $item (@contexts) {
 6111:                 foreach my $auth (@authtypes) {
 6112:                     $checked{$item}{$auth} = ' checked="checked" ';
 6113:                 }
 6114:             }
 6115:         }
 6116:         my %title = &context_names();
 6117:         my %authname = &authtype_names();
 6118:         my $rownum = 0;
 6119:         my $css_class; 
 6120:         foreach my $item (@contexts) {
 6121:             if ($rownum%2) {
 6122:                 $css_class = '';
 6123:             } else {
 6124:                 $css_class = ' class="LC_odd_row" ';
 6125:             }
 6126:             $datatable .=   '<tr'.$css_class.'>'.
 6127:                             '<td>'.$title{$item}.
 6128:                             '</td><td class="LC_left_item">'.
 6129:                             '<span class="LC_nobreak">';
 6130:             foreach my $auth (@authtypes) {
 6131:                 $datatable .= '<label>'. 
 6132:                               '<input type="checkbox" name="'.$item.'_auth" '.
 6133:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 6134:                               $authname{$auth}.'</label>&nbsp;';
 6135:             }
 6136:             $datatable .= '</span></td></tr>';
 6137:             $rownum ++;
 6138:         }
 6139:         $$rowtotal += $rownum;
 6140:     }
 6141:     return $datatable;
 6142: }
 6143: 
 6144: sub print_selfcreation {
 6145:     my ($position,$dom,$settings,$rowtotal) = @_;
 6146:     my (@selfcreate,$createsettings,$processing,$emailoptions,$emailverified,
 6147:         $emaildomain,$datatable);
 6148:     if (ref($settings) eq 'HASH') {
 6149:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 6150:             $createsettings = $settings->{'cancreate'};
 6151:             if (ref($createsettings) eq 'HASH') {
 6152:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 6153:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 6154:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 6155:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 6156:                         @selfcreate = ('email','login','sso');
 6157:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 6158:                         @selfcreate = ($createsettings->{'selfcreate'});
 6159:                     }
 6160:                 }
 6161:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 6162:                     $processing = $createsettings->{'selfcreateprocessing'};
 6163:                 }
 6164:                 if (ref($createsettings->{'emailoptions'}) eq 'HASH') {
 6165:                     $emailoptions = $createsettings->{'emailoptions'};
 6166:                 }
 6167:                 if (ref($createsettings->{'emailverified'}) eq 'HASH') {
 6168:                     $emailverified = $createsettings->{'emailverified'};
 6169:                 }
 6170:                 if (ref($createsettings->{'emaildomain'}) eq 'HASH') {
 6171:                     $emaildomain = $createsettings->{'emaildomain'};
 6172:                 }
 6173:             }
 6174:         }
 6175:     }
 6176:     my %radiohash;
 6177:     my $numinrow = 4;
 6178:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 6179:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6180:     if ($position eq 'top') {
 6181:         my %choices = &Apache::lonlocal::texthash (
 6182:                                                       cancreate_login      => 'Institutional Login',
 6183:                                                       cancreate_sso        => 'Institutional Single Sign On',
 6184:                                                   );
 6185:         my @toggles = sort(keys(%choices));
 6186:         my %defaultchecked = (
 6187:                                'cancreate_login' => 'off',
 6188:                                'cancreate_sso'   => 'off',
 6189:                              );
 6190:         my ($onclick,$itemcount);
 6191:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 6192:                                                      \%choices,$itemcount,$onclick);
 6193:         $$rowtotal += $itemcount;
 6194: 
 6195:         if (ref($usertypes) eq 'HASH') {
 6196:             if (keys(%{$usertypes}) > 0) {
 6197:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 6198:                                              $dom,$numinrow,$othertitle,
 6199:                                              'statustocreate',$rowtotal);
 6200:                 $$rowtotal ++;
 6201:             }
 6202:         }
 6203:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 6204:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 6205:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 6206:         my $rem;
 6207:         my $numperrow = 2;
 6208:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 6209:         $datatable .= '<tr'.$css_class.'>'.
 6210:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 6211:                      '<td class="LC_left_item">'."\n".
 6212:                      '<table>'."\n";
 6213:         for (my $i=0; $i<@fields; $i++) {
 6214:             $rem = $i%($numperrow);
 6215:             if ($rem == 0) {
 6216:                 if ($i > 0) {
 6217:                     $datatable .= '</tr>';
 6218:                 }
 6219:                 $datatable .= '<tr>';
 6220:             }
 6221:             my $currval;
 6222:             if (ref($createsettings) eq 'HASH') {
 6223:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 6224:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 6225:                 }
 6226:             }
 6227:             $datatable .= '<td class="LC_left_item">'.
 6228:                           '<span class="LC_nobreak">'.
 6229:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 6230:                           'value="'.$currval.'" size="10" />&nbsp;'.
 6231:                           $fieldtitles{$fields[$i]}.'</span></td>';
 6232:         }
 6233:         my $colsleft = $numperrow - $rem;
 6234:         if ($colsleft > 1 ) {
 6235:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 6236:                          '&nbsp;</td>';
 6237:         } elsif ($colsleft == 1) {
 6238:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 6239:         }
 6240:         $datatable .= '</tr></table></td></tr>';
 6241:         $$rowtotal ++;
 6242:     } elsif ($position eq 'middle') {
 6243:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 6244:         my @posstypes;
 6245:         if (ref($types) eq 'ARRAY') {
 6246:             @posstypes = @{$types};
 6247:         }
 6248:         unless (grep(/^default$/,@posstypes)) {
 6249:             push(@posstypes,'default');
 6250:         }
 6251:         my %usertypeshash;
 6252:         if (ref($usertypes) eq 'HASH') {
 6253:             %usertypeshash = %{$usertypes};
 6254:         }
 6255:         $usertypeshash{'default'} = $othertitle;
 6256:         foreach my $status (@posstypes) {
 6257:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 6258:                                                    $numinrow,$$rowtotal,\%usertypeshash);
 6259:             $$rowtotal ++;
 6260:         }
 6261:     } else {
 6262:         my %choices = &Apache::lonlocal::texthash (
 6263:                           'cancreate_email' => 'Non-institutional username (via e-mail verification)',
 6264:                                                   );
 6265:         my @toggles = sort(keys(%choices));
 6266:         my %defaultchecked = (
 6267:                                'cancreate_email' => 'off',
 6268:                              );
 6269:         my $customclass = 'LC_selfcreate_email';
 6270:         my $classprefix = 'LC_canmodify_emailusername_';
 6271:         my $optionsprefix = 'LC_options_emailusername_';
 6272:         my $display = 'none';
 6273:         my $rowstyle = 'display:none';
 6274:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 6275:             $display = 'block';
 6276:             $rowstyle = 'display:table-row';
 6277:         }
 6278:         my $onclick = "toggleRows(this.form,'cancreate_email','selfassign','$customclass','$classprefix','$optionsprefix');";
 6279:         ($datatable,$$rowtotal) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 6280:                                                      \%choices,$$rowtotal,$onclick);
 6281:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal,$customclass,
 6282:                                          $rowstyle);
 6283:         $$rowtotal ++;
 6284:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal,$customclass,
 6285:                                       $rowstyle);
 6286:         $$rowtotal ++;
 6287:         my (@ordered,@posstypes,%usertypeshash);
 6288:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 6289:         my ($emailrules,$emailruleorder) =
 6290:             &Apache::lonnet::inst_userrules($dom,'email');
 6291:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 6292:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 6293:         if (ref($types) eq 'ARRAY') {
 6294:             @posstypes = @{$types};
 6295:         }
 6296:         if (@posstypes) {
 6297:             unless (grep(/^default$/,@posstypes)) {
 6298:                 push(@posstypes,'default');
 6299:             }
 6300:             if (ref($usertypes) eq 'HASH') {
 6301:                 %usertypeshash = %{$usertypes};
 6302:             }
 6303:             my $currassign;
 6304:             if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
 6305:                 $currassign = {
 6306:                                   selfassign => $domdefaults{'inststatusguest'},
 6307:                               };
 6308:                 @ordered = @{$domdefaults{'inststatusguest'}};
 6309:             } else {
 6310:                 $currassign = { selfassign => [] };
 6311:             }
 6312:             my $onclicktypes = "toggleDataRow(this.form,'selfassign','$customclass','$optionsprefix',);".
 6313:                                "toggleDataRow(this.form,'selfassign','$customclass','$classprefix',1);";
 6314:             $datatable .= &insttypes_row($currassign,$types,$usertypes,$dom,
 6315:                                          $numinrow,$othertitle,'selfassign',
 6316:                                          $rowtotal,$onclicktypes,$customclass,
 6317:                                          $rowstyle);
 6318:             $$rowtotal ++;
 6319:             $usertypeshash{'default'} = $othertitle;
 6320:             foreach my $status (@posstypes) {
 6321:                 my $css_class;
 6322:                 if ($$rowtotal%2) {
 6323:                     $css_class = 'LC_odd_row ';
 6324:                 }
 6325:                 $css_class .= $customclass;
 6326:                 my $rowid = $optionsprefix.$status;
 6327:                 my $hidden = 1;
 6328:                 my $currstyle = 'display:none';
 6329:                 if (grep(/^\Q$status\E$/,@ordered)) {
 6330:                     $currstyle = $rowstyle;
 6331:                     $hidden = 0;
 6332:                 }
 6333:                 $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 6334:                                              $emailrules,$emailruleorder,$settings,$status,$rowid,
 6335:                                              $usertypeshash{$status},$css_class,$currstyle,$intdom);
 6336:                 unless ($hidden) {
 6337:                     $$rowtotal ++;
 6338:                 }
 6339:             }
 6340:         } else {
 6341:             my $css_class;
 6342:             if ($$rowtotal%2) {
 6343:                 $css_class = 'LC_odd_row ';
 6344:             }
 6345:             $css_class .= $customclass;
 6346:             $usertypeshash{'default'} = $othertitle;
 6347:             $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 6348:                                          $emailrules,$emailruleorder,$settings,'default','',
 6349:                                          $othertitle,$css_class,$rowstyle,$intdom);
 6350:             $$rowtotal ++;
 6351:         }
 6352:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 6353:         $numinrow = 1;
 6354:         if (@posstypes) {
 6355:             foreach my $status (@posstypes) {
 6356:                 my $rowid = $classprefix.$status;
 6357:                 my $datarowstyle = 'display:none';
 6358:                 if (grep(/^\Q$status\E$/,@ordered)) {
 6359:                     $datarowstyle = $rowstyle;
 6360:                 }
 6361:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 6362:                                                        $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 6363:                                                        $infotitles,$rowid,$customclass,$datarowstyle);
 6364:                 unless ($datarowstyle eq 'display:none') {
 6365:                     $$rowtotal ++;
 6366:                 }
 6367:             }
 6368:         } else {
 6369:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_default',$settings,
 6370:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 6371:                                                    $infotitles,'',$customclass,$rowstyle);
 6372:         }
 6373:     }
 6374:     return $datatable;
 6375: }
 6376: 
 6377: sub selfcreate_javascript {
 6378:     return <<"ENDSCRIPT";
 6379: 
 6380: <script type="text/javascript">
 6381: // <![CDATA[
 6382: 
 6383: function toggleRows(form,radio,checkbox,target,prefix,altprefix) {
 6384:     var x = document.getElementsByClassName(target);
 6385:     var insttypes = 0;
 6386:     var insttypeRegExp = new RegExp(prefix);
 6387:     if ((x.length != undefined) && (x.length > 0)) {
 6388:         if (form.elements[radio].length != undefined) {
 6389:             for (var i=0; i<form.elements[radio].length; i++) {
 6390:                 if (form.elements[radio][i].checked) {
 6391:                     if (form.elements[radio][i].value == 1) {
 6392:                         for (var j=0; j<x.length; j++) {
 6393:                             if (x[j].id == 'undefined') {
 6394:                                 x[j].style.display = 'table-row';
 6395:                             } else if (insttypeRegExp.test(x[j].id)) {
 6396:                                 insttypes ++;
 6397:                             } else {
 6398:                                 x[j].style.display = 'table-row';
 6399:                             }
 6400:                         }
 6401:                     } else {
 6402:                         for (var j=0; j<x.length; j++) {
 6403:                             x[j].style.display = 'none';
 6404:                         }
 6405:                     }
 6406:                     break;
 6407:                 }
 6408:             }
 6409:             if (insttypes > 0) {
 6410:                 toggleDataRow(form,checkbox,target,altprefix);
 6411:                 toggleDataRow(form,checkbox,target,prefix,1);
 6412:             }
 6413:         }
 6414:     }
 6415:     return;
 6416: }
 6417: 
 6418: function toggleDataRow(form,checkbox,target,prefix,docount) {
 6419:     if (form.elements[checkbox].length != undefined) {
 6420:         var count = 0;
 6421:         if (docount) {
 6422:             for (var i=0; i<form.elements[checkbox].length; i++) {
 6423:                 if (form.elements[checkbox][i].checked) {
 6424:                     count ++;
 6425:                 }
 6426:             }
 6427:         }
 6428:         for (var i=0; i<form.elements[checkbox].length; i++) {
 6429:             var type = form.elements[checkbox][i].value;
 6430:             if (document.getElementById(prefix+type)) {
 6431:                 if (form.elements[checkbox][i].checked) {
 6432:                     document.getElementById(prefix+type).style.display = 'table-row';
 6433:                     if (count % 2 == 1) {
 6434:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 6435:                     } else {
 6436:                         document.getElementById(prefix+type).className = target;
 6437:                     }
 6438:                     count ++;
 6439:                 } else {
 6440:                     document.getElementById(prefix+type).style.display = 'none';
 6441:                 }
 6442:             }
 6443:         }
 6444:     }
 6445:     return;
 6446: }
 6447: 
 6448: function toggleEmailOptions(form,radio,prefix,altprefix,status) {
 6449:     var caller = radio+'_'+status;
 6450:     if (form.elements[caller].length != undefined) {
 6451:         for (var i=0; i<form.elements[caller].length; i++) {
 6452:             if (form.elements[caller][i].checked) {
 6453:                 if (document.getElementById(altprefix+'_inst_'+status)) {
 6454:                     var curr = form.elements[caller][i].value;
 6455:                     if (prefix) {
 6456:                         document.getElementById(prefix+'_'+status).style.display = 'none';
 6457:                     }
 6458:                     document.getElementById(altprefix+'_inst_'+status).style.display = 'none';
 6459:                     document.getElementById(altprefix+'_noninst_'+status).style.display = 'none';
 6460:                     if (curr == 'custom') {
 6461:                         if (prefix) {
 6462:                             document.getElementById(prefix+'_'+status).style.display = 'inline';
 6463:                         }
 6464:                     } else if (curr == 'inst') {
 6465:                         document.getElementById(altprefix+'_inst_'+status).style.display = 'inline';
 6466:                     } else if (curr == 'noninst') {
 6467:                         document.getElementById(altprefix+'_noninst_'+status).style.display = 'inline';
 6468:                     }
 6469:                     break;
 6470:                 }
 6471:             }
 6472:         }
 6473:     }
 6474: }
 6475: 
 6476: // ]]>
 6477: </script>
 6478: 
 6479: ENDSCRIPT
 6480: }
 6481: 
 6482: sub noninst_users {
 6483:     my ($processing,$emailverified,$emailoptions,$emaildomain,$emailrules,
 6484:         $emailruleorder,$settings,$type,$rowid,$typetitle,$css_class,$rowstyle,$intdom) = @_;
 6485:     my $class = 'LC_left_item';
 6486:     if ($css_class) {
 6487:         $css_class = ' class="'.$css_class.'"';
 6488:     }
 6489:     if ($rowid) {
 6490:         $rowid = ' id="'.$rowid.'"';
 6491:     }
 6492:     if ($rowstyle) {
 6493:         $rowstyle = ' style="'.$rowstyle.'"';
 6494:     }
 6495:     my ($output,$description);
 6496:     if ($type eq 'default') {
 6497:         $description = &mt('Requests for: [_1]',$typetitle);
 6498:     } else {
 6499:         $description = &mt('Requests for: [_1] (status self-reported)',$typetitle);
 6500:     }
 6501:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 6502:               "<td>$description</td>\n".
 6503:               '<td class="'.$class.'" colspan="2">'.
 6504:               '<table><tr>';
 6505:     my %headers = &Apache::lonlocal::texthash(
 6506:               approve  => 'Processing',
 6507:               email    => 'E-mail',
 6508:               username => 'Username',
 6509:     );
 6510:     foreach my $item ('approve','email','username') {
 6511:         $output .= '<th>'.$headers{$item}.'</th>';
 6512:     }
 6513:     $output .= '</tr><tr>';
 6514:     foreach my $item ('approve','email','username') {
 6515:         $output .= '<td valign="top">';
 6516:         my (%choices,@options,$hashref,$defoption,$name,$onclick,$hascustom);
 6517:         if ($item eq 'approve') {
 6518:             %choices = &Apache::lonlocal::texthash (
 6519:                                                      automatic => 'Automatically approved',
 6520:                                                      approval  => 'Queued for approval',
 6521:                                                    );
 6522:             @options = ('automatic','approval');
 6523:             $hashref = $processing;
 6524:             $defoption = 'automatic';
 6525:             $name = 'cancreate_emailprocess_'.$type;
 6526:         } elsif ($item eq 'email') {
 6527:             %choices = &Apache::lonlocal::texthash (
 6528:                                                      any     => 'Any e-mail',
 6529:                                                      inst    => 'Institutional only',
 6530:                                                      noninst => 'Non-institutional only',
 6531:                                                      custom  => 'Custom restrictions',
 6532:                                                    );
 6533:             @options = ('any','inst','noninst');
 6534:             my $showcustom;
 6535:             if (ref($emailrules) eq 'HASH') {
 6536:                 if (keys(%{$emailrules}) > 0) {
 6537:                     push(@options,'custom');
 6538:                     $showcustom = 'cancreate_emailrule';
 6539:                     if (ref($settings) eq 'HASH') {
 6540:                         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
 6541:                             foreach my $rule (@{$settings->{'email_rule'}}) {
 6542:                                 if (exists($emailrules->{$rule})) {
 6543:                                     $hascustom ++;
 6544:                                 }
 6545:                             }
 6546:                         } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
 6547:                             if (ref($settings->{'email_rule'}{$type}) eq 'ARRAY') {
 6548:                                 foreach my $rule (@{$settings->{'email_rule'}{$type}}) {
 6549:                                     if (exists($emailrules->{$rule})) {
 6550:                                         $hascustom ++;
 6551:                                     }
 6552:                                 }
 6553:                             }
 6554:                         }
 6555:                     }
 6556:                 }
 6557:             }
 6558:             $onclick = ' onclick="toggleEmailOptions(this.form,'."'cancreate_emailoptions','$showcustom',".
 6559:                                                      "'cancreate_emaildomain','$type'".');"';
 6560:             $hashref = $emailoptions;
 6561:             $defoption = 'any';
 6562:             $name = 'cancreate_emailoptions_'.$type;
 6563:         } elsif ($item eq 'username') {
 6564:             %choices = &Apache::lonlocal::texthash (
 6565:                                                      all    => 'Same as e-mail',
 6566:                                                      first  => 'Omit @domain',
 6567:                                                      free   => 'Free to choose',
 6568:                                                    );
 6569:             @options = ('all','first','free');
 6570:             $hashref = $emailverified;
 6571:             $defoption = 'all';
 6572:             $name = 'cancreate_usernameoptions_'.$type;
 6573:         }
 6574:         foreach my $option (@options) {
 6575:             my $checked;
 6576:             if (ref($hashref) eq 'HASH') {
 6577:                 if ($type eq '') {
 6578:                     if (!exists($hashref->{'default'})) {
 6579:                         if ($option eq $defoption) {
 6580:                             $checked = ' checked="checked"';
 6581:                         }
 6582:                     } else {
 6583:                         if ($hashref->{'default'} eq $option) {
 6584:                             $checked = ' checked="checked"';
 6585:                         }
 6586:                     }
 6587:                 } else {
 6588:                     if (!exists($hashref->{$type})) {
 6589:                         if ($option eq $defoption) {
 6590:                             $checked = ' checked="checked"';
 6591:                         }
 6592:                     } else {
 6593:                         if ($hashref->{$type} eq $option) {
 6594:                             $checked = ' checked="checked"';
 6595:                         }
 6596:                     }
 6597:                 }
 6598:             } elsif (($item eq 'email') && ($hascustom)) {
 6599:                 if ($option eq 'custom') {
 6600:                     $checked = ' checked="checked"';
 6601:                 }
 6602:             } elsif ($option eq $defoption) {
 6603:                 $checked = ' checked="checked"';
 6604:             }
 6605:             $output .= '<span class="LC_nobreak"><label>'.
 6606:                        '<input type="radio" name="'.$name.'"'.
 6607:                        $checked.' value="'.$option.'"'.$onclick.' />'.
 6608:                        $choices{$option}.'</label></span><br />';
 6609:             if ($item eq 'email') {
 6610:                 if ($option eq 'custom') {
 6611:                     my $id = 'cancreate_emailrule_'.$type;
 6612:                     my $display = 'none';
 6613:                     if ($checked) {
 6614:                         $display = 'inline';
 6615:                     }
 6616:                     my $numinrow = 2;
 6617:                     $output .= '<fieldset id="'.$id.'" style="display:'.$display.';">'.
 6618:                                '<legend>'.&mt('Disallow').'</legend><table>'.
 6619:                                &user_formats_row('email',$settings,$emailrules,
 6620:                                                  $emailruleorder,$numinrow,'',$type);
 6621:                               '</table></fieldset>';
 6622:                 } elsif (($option eq 'inst') || ($option eq 'noninst')) {
 6623:                     my %text = &Apache::lonlocal::texthash (
 6624:                                                              inst    => 'must end:',
 6625:                                                              noninst => 'cannot end:',
 6626:                                                            );
 6627:                     my $value;
 6628:                     if (ref($emaildomain) eq 'HASH') {
 6629:                         if (ref($emaildomain->{$type}) eq 'HASH') {
 6630:                             $value = $emaildomain->{$type}->{$option};
 6631:                         }
 6632:                     }
 6633:                     if ($value eq '') {
 6634:                         $value = '@'.$intdom;
 6635:                     }
 6636:                     my $condition = 'cancreate_emaildomain_'.$option.'_'.$type;
 6637:                     my $display = 'none';
 6638:                     if ($checked) {
 6639:                         $display = 'inline';
 6640:                     }
 6641:                     $output .= '<div id="'.$condition.'" style="display:'.$display.';">'.
 6642:                                '<span class="LC_domprefs_email">'.$text{$option}.'</span> '.
 6643:                                '<input type="text" name="'.$condition.'" value="'.$value.'" size="10" />'.
 6644:                                '</div>';
 6645:                 }
 6646:             }
 6647:         }
 6648:         $output .= '</td>'."\n";
 6649:     }
 6650:     $output .= "</tr></table></td></tr>\n";
 6651:     return $output;
 6652: }
 6653: 
 6654: sub captcha_choice {
 6655:     my ($context,$settings,$itemcount,$customcss,$rowstyle) = @_;
 6656:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 6657:         $vertext,$currver); 
 6658:     my %lt = &captcha_phrases();
 6659:     $keyentry = 'hidden';
 6660:     my $colspan=2;
 6661:     if ($context eq 'cancreate') {
 6662:         $rowname = &mt('CAPTCHA validation');
 6663:     } elsif ($context eq 'login') {
 6664:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 6665:     } elsif ($context eq 'passwords') {
 6666:         $rowname = &mt('"Forgot Password" CAPTCHA validation');
 6667:         $colspan=1;
 6668:     }
 6669:     if (ref($settings) eq 'HASH') {
 6670:         if ($settings->{'captcha'}) {
 6671:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 6672:         } else {
 6673:             $checked{'original'} = ' checked="checked"';
 6674:         }
 6675:         if ($settings->{'captcha'} eq 'recaptcha') {
 6676:             $pubtext = $lt{'pub'};
 6677:             $privtext = $lt{'priv'};
 6678:             $keyentry = 'text';
 6679:             $vertext = $lt{'ver'};
 6680:             $currver = $settings->{'recaptchaversion'};
 6681:             if ($currver ne '2') {
 6682:                 $currver = 1;
 6683:             }
 6684:         }
 6685:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 6686:             $currpub = $settings->{'recaptchakeys'}{'public'};
 6687:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 6688:         }
 6689:     } else {
 6690:         $checked{'original'} = ' checked="checked"';
 6691:     }
 6692:     my $css_class;
 6693:     if ($itemcount%2) {
 6694:         $css_class = 'LC_odd_row';
 6695:     }
 6696:     if ($customcss) {
 6697:         $css_class .= " $customcss";
 6698:     }
 6699:     $css_class =~ s/^\s+//;
 6700:     if ($css_class) {
 6701:         $css_class = ' class="'.$css_class.'"';
 6702:     }
 6703:     if ($rowstyle) {
 6704:         $css_class .= ' style="'.$rowstyle.'"';
 6705:     }
 6706:     my $output = '<tr'.$css_class.'>'.
 6707:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="'.$colspan.'">'."\n".
 6708:                  '<table><tr><td>'."\n";
 6709:     foreach my $option ('original','recaptcha','notused') {
 6710:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 6711:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 6712:                    $lt{$option}.'</label></span>';
 6713:         unless ($option eq 'notused') {
 6714:             $output .= ('&nbsp;'x2)."\n";
 6715:         }
 6716:     }
 6717: #
 6718: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 6719: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 6720: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 6721: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 6722: #
 6723:     $output .= '</td></tr>'."\n".
 6724:                '<tr><td class="LC_zero_height">'."\n".
 6725:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 6726:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 6727:                $currpub.'" size="40" /></span><br />'."\n".
 6728:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 6729:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 6730:                $currpriv.'" size="40" /></span><br />'.
 6731:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 6732:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 6733:                $currver.'" size="3" /></span><br />'.
 6734:                '</td></tr></table>'."\n".
 6735:                '</td></tr>';
 6736:     return $output;
 6737: }
 6738: 
 6739: sub user_formats_row {
 6740:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount,$status) = @_;
 6741:     my $output;
 6742:     my %text = (
 6743:                    'username' => 'new usernames',
 6744:                    'id'       => 'IDs',
 6745:                );
 6746:     unless ($type eq 'email') {
 6747:         my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 6748:         $output = '<tr '.$css_class.'>'.
 6749:                   '<td><span class="LC_nobreak">'.
 6750:                   &mt("Format rules to check for $text{$type}: ").
 6751:                   '</td><td class="LC_left_item" colspan="2"><table>';
 6752:     }
 6753:     my $rem;
 6754:     if (ref($ruleorder) eq 'ARRAY') {
 6755:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 6756:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 6757:                 my $rem = $i%($numinrow);
 6758:                 if ($rem == 0) {
 6759:                     if ($i > 0) {
 6760:                         $output .= '</tr>';
 6761:                     }
 6762:                     $output .= '<tr>';
 6763:                 }
 6764:                 my $check = ' ';
 6765:                 if (ref($settings) eq 'HASH') {
 6766:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 6767:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 6768:                             $check = ' checked="checked" ';
 6769:                         }
 6770:                     } elsif ((ref($settings->{$type.'_rule'}) eq 'HASH') && ($status ne '')) {
 6771:                         if (ref($settings->{$type.'_rule'}->{$status}) eq 'ARRAY') {
 6772:                             if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}->{$status}})) {
 6773:                                 $check = ' checked="checked" ';
 6774:                             }
 6775:                         }
 6776:                     }
 6777:                 }
 6778:                 my $name = $type.'_rule';
 6779:                 if ($type eq 'email') {
 6780:                     $name .= '_'.$status;
 6781:                 }
 6782:                 $output .= '<td class="LC_left_item">'.
 6783:                            '<span class="LC_nobreak"><label>'.
 6784:                            '<input type="checkbox" name="'.$name.'" '.
 6785:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 6786:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 6787:             }
 6788:         }
 6789:         $rem = @{$ruleorder}%($numinrow);
 6790:     }
 6791:     my $colsleft;
 6792:     if ($rem) {
 6793:         $colsleft = $numinrow - $rem;
 6794:     }
 6795:     if ($colsleft > 1 ) {
 6796:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 6797:                    '&nbsp;</td>';
 6798:     } elsif ($colsleft == 1) {
 6799:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 6800:     }
 6801:     $output .= '</tr></table>';
 6802:     unless ($type eq 'email') {
 6803:         $output .= '</td></tr>';
 6804:     }
 6805:     return $output;
 6806: }
 6807: 
 6808: sub usercreation_types {
 6809:     my %lt = &Apache::lonlocal::texthash (
 6810:                     author     => 'When adding a co-author',
 6811:                     course     => 'When adding a user to a course',
 6812:                     requestcrs => 'When requesting a course',
 6813:                     any        => 'Any',
 6814:                     official   => 'Institutional only ',
 6815:                     unofficial => 'Non-institutional only',
 6816:                     none       => 'None',
 6817:     );
 6818:     return %lt;
 6819: }
 6820: 
 6821: sub selfcreation_types {
 6822:     my %lt = &Apache::lonlocal::texthash (
 6823:                     selfcreate => 'User creates own account',
 6824:                     any        => 'Any',
 6825:                     official   => 'Institutional only ',
 6826:                     unofficial => 'Non-institutional only',
 6827:                     email      => 'E-mail address',
 6828:                     login      => 'Institutional Login',
 6829:                     sso        => 'SSO',
 6830:              );
 6831: }
 6832: 
 6833: sub authtype_names {
 6834:     my %lt = &Apache::lonlocal::texthash(
 6835:                       int    => 'Internal',
 6836:                       krb4   => 'Kerberos 4',
 6837:                       krb5   => 'Kerberos 5',
 6838:                       loc    => 'Local',
 6839:                   );
 6840:     return %lt;
 6841: }
 6842: 
 6843: sub context_names {
 6844:     my %context_title = &Apache::lonlocal::texthash(
 6845:        author => 'Creating users when an Author',
 6846:        course => 'Creating users when in a course',
 6847:        domain => 'Creating users when a Domain Coordinator',
 6848:     );
 6849:     return %context_title;
 6850: }
 6851: 
 6852: sub print_usermodification {
 6853:     my ($position,$dom,$settings,$rowtotal) = @_;
 6854:     my $numinrow = 4;
 6855:     my ($context,$datatable,$rowcount);
 6856:     if ($position eq 'top') {
 6857:         $rowcount = 0;
 6858:         $context = 'author'; 
 6859:         foreach my $role ('ca','aa') {
 6860:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 6861:                                                    $numinrow,$rowcount);
 6862:             $$rowtotal ++;
 6863:             $rowcount ++;
 6864:         }
 6865:     } elsif ($position eq 'bottom') {
 6866:         $context = 'course';
 6867:         $rowcount = 0;
 6868:         foreach my $role ('st','ep','ta','in','cr') {
 6869:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 6870:                                                    $numinrow,$rowcount);
 6871:             $$rowtotal ++;
 6872:             $rowcount ++;
 6873:         }
 6874:     }
 6875:     return $datatable;
 6876: }
 6877: 
 6878: sub print_defaults {
 6879:     my ($position,$dom,$settings,$rowtotal) = @_;
 6880:     my $rownum = 0;
 6881:     my ($datatable,$css_class,$titles);
 6882:     unless ($position eq 'bottom') {
 6883:         $titles = &defaults_titles($dom);
 6884:     }
 6885:     if ($position eq 'top') {
 6886:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 6887:                      'datelocale_def','portal_def');
 6888:         my %defaults;
 6889:         if (ref($settings) eq 'HASH') {
 6890:             %defaults = %{$settings};
 6891:         } else {
 6892:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6893:             foreach my $item (@items) {
 6894:                 $defaults{$item} = $domdefaults{$item};
 6895:             }
 6896:         }
 6897:         foreach my $item (@items) {
 6898:             if ($rownum%2) {
 6899:                 $css_class = '';
 6900:             } else {
 6901:                 $css_class = ' class="LC_odd_row" ';
 6902:             }
 6903:             $datatable .= '<tr'.$css_class.'>'.
 6904:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 6905:                           '</span></td><td class="LC_right_item" colspan="3">';
 6906:             if ($item eq 'auth_def') {
 6907:                 my @authtypes = ('internal','krb4','krb5','localauth');
 6908:                 my %shortauth = (
 6909:                                  internal => 'int',
 6910:                                  krb4 => 'krb4',
 6911:                                  krb5 => 'krb5',
 6912:                                  localauth  => 'loc'
 6913:                                 );
 6914:                 my %authnames = &authtype_names();
 6915:                 foreach my $auth (@authtypes) {
 6916:                     my $checked = ' ';
 6917:                     if ($defaults{$item} eq $auth) {
 6918:                         $checked = ' checked="checked" ';
 6919:                     }
 6920:                     $datatable .= '<label><input type="radio" name="'.$item.
 6921:                                   '" value="'.$auth.'"'.$checked.'/>'.
 6922:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 6923:                 }
 6924:             } elsif ($item eq 'timezone_def') {
 6925:                 my $includeempty = 1;
 6926:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 6927:             } elsif ($item eq 'datelocale_def') {
 6928:                 my $includeempty = 1;
 6929:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 6930:             } elsif ($item eq 'lang_def') {
 6931:                 my $includeempty = 1;
 6932:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 6933:             } else {
 6934:                 my $size;
 6935:                 if ($item eq 'portal_def') {
 6936:                     $size = ' size="25"';
 6937:                 }
 6938:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 6939:                               $defaults{$item}.'"'.$size.' />';
 6940:             }
 6941:             $datatable .= '</td></tr>';
 6942:             $rownum ++;
 6943:         }
 6944:     } else {
 6945:         my %defaults;
 6946:         if (ref($settings) eq 'HASH') {
 6947:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 6948:                 my $maxnum = @{$settings->{'inststatusorder'}};
 6949:                 for (my $i=0; $i<$maxnum; $i++) {
 6950:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 6951:                     my $item = $settings->{'inststatusorder'}->[$i];
 6952:                     my $title = $settings->{'inststatustypes'}->{$item};
 6953:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 6954:                     $datatable .= '<tr'.$css_class.'>'.
 6955:                                   '<td><span class="LC_nobreak">'.
 6956:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 6957:                     for (my $k=0; $k<=$maxnum; $k++) {
 6958:                         my $vpos = $k+1;
 6959:                         my $selstr;
 6960:                         if ($k == $i) {
 6961:                             $selstr = ' selected="selected" ';
 6962:                         }
 6963:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6964:                     }
 6965:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 6966:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 6967:                                   &mt('delete').'</span></td>'.
 6968:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed:').
 6969:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 6970:                                   '</span></td></tr>';
 6971:                 }
 6972:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 6973:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 6974:                 $datatable .= '<tr '.$css_class.'>'.
 6975:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 6976:                 for (my $k=0; $k<=$maxnum; $k++) {
 6977:                     my $vpos = $k+1;
 6978:                     my $selstr;
 6979:                     if ($k == $maxnum) {
 6980:                         $selstr = ' selected="selected" ';
 6981:                     }
 6982:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6983:                 }
 6984:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 6985:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 6986:                               '&nbsp;'.&mt('(new)').
 6987:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 6988:                               &mt('Name displayed:').
 6989:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 6990:                               '</tr>'."\n";
 6991:                 $rownum ++;
 6992:             }
 6993:         }
 6994:     }
 6995:     $$rowtotal += $rownum;
 6996:     return $datatable;
 6997: }
 6998: 
 6999: sub get_languages_hash {
 7000:     my %langchoices;
 7001:     foreach my $id (&Apache::loncommon::languageids()) {
 7002:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 7003:         if ($code ne '') {
 7004:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 7005:         }
 7006:     }
 7007:     return %langchoices;
 7008: }
 7009: 
 7010: sub defaults_titles {
 7011:     my ($dom) = @_;
 7012:     my %titles = &Apache::lonlocal::texthash (
 7013:                    'auth_def'      => 'Default authentication type',
 7014:                    'auth_arg_def'  => 'Default authentication argument',
 7015:                    'lang_def'      => 'Default language',
 7016:                    'timezone_def'  => 'Default timezone',
 7017:                    'datelocale_def' => 'Default locale for dates',
 7018:                    'portal_def'     => 'Portal/Default URL',
 7019:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
 7020:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
 7021:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
 7022:                  );
 7023:     if ($dom) {
 7024:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 7025:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 7026:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 7027:         $protocol = 'http' if ($protocol ne 'https');
 7028:         if ($uint_dom) {
 7029:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 7030:                                          $uint_dom);
 7031:         }
 7032:     }
 7033:     return (\%titles);
 7034: }
 7035: 
 7036: sub print_scantron {
 7037:     my ($r,$position,$dom,$confname,$settings,$rowtotal) = @_;
 7038:     if ($position eq 'top') {
 7039:         return &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
 7040:     } else {
 7041:         return &print_scantronconfig($dom,$settings,\$rowtotal);
 7042:     }
 7043: }
 7044: 
 7045: sub scantron_javascript {
 7046:     return <<"ENDSCRIPT";
 7047: 
 7048: <script type="text/javascript">
 7049: // <![CDATA[
 7050: 
 7051: function toggleScantron(form) {
 7052:     var csvfieldset = new Array();
 7053:     if (document.getElementById('scantroncsv_cols')) {
 7054:         csvfieldset.push(document.getElementById('scantroncsv_cols'));
 7055:     }
 7056:     if (document.getElementById('scantroncsv_options')) {
 7057:         csvfieldset.push(document.getElementById('scantroncsv_options'));
 7058:     }
 7059:     if (csvfieldset.length) {
 7060:         if (document.getElementById('scantronconfcsv')) {
 7061:             var scantroncsv = document.getElementById('scantronconfcsv');
 7062:             if (scantroncsv.checked) {
 7063:                 for (var i=0; i<csvfieldset.length; i++) {
 7064:                     csvfieldset[i].style.display = 'block';
 7065:                 }
 7066:             } else {
 7067:                 for (var i=0; i<csvfieldset.length; i++) {
 7068:                     csvfieldset[i].style.display = 'none';
 7069:                 }
 7070:                 var csvselects = document.getElementsByClassName('scantronconfig_csv');
 7071:                 if (csvselects.length) {
 7072:                     for (var j=0; j<csvselects.length; j++) {
 7073:                         csvselects[j].selectedIndex = 0;
 7074:                     }
 7075:                 }
 7076:             }
 7077:         }
 7078:     }
 7079:     return;
 7080: }
 7081: // ]]>
 7082: </script>
 7083: 
 7084: ENDSCRIPT
 7085: 
 7086: }
 7087: 
 7088: sub print_scantronformat {
 7089:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 7090:     my $itemcount = 1;
 7091:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 7092:         %confhash);
 7093:     my $switchserver = &check_switchserver($dom,$confname);
 7094:     my %lt = &Apache::lonlocal::texthash (
 7095:                 default => 'Default bubblesheet format file error',
 7096:                 custom  => 'Custom bubblesheet format file error',
 7097:              );
 7098:     my %scantronfiles = (
 7099:         default => 'default.tab',
 7100:         custom => 'custom.tab',
 7101:     );
 7102:     foreach my $key (keys(%scantronfiles)) {
 7103:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 7104:                               .$scantronfiles{$key};
 7105:     }
 7106:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 7107:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 7108:         if (!$switchserver) {
 7109:             my $servadm = $r->dir_config('lonAdmEMail');
 7110:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 7111:             if ($configuserok eq 'ok') {
 7112:                 if ($author_ok eq 'ok') {
 7113:                     my %legacyfile = (
 7114:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab',
 7115:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab',
 7116:                     );
 7117:                     my %md5chk;
 7118:                     foreach my $type (keys(%legacyfile)) {
 7119:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 7120:                         chomp($md5chk{$type});
 7121:                     }
 7122:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 7123:                         foreach my $type (keys(%legacyfile)) {
 7124:                             ($scantronurls{$type},my $error) =
 7125:                                 &legacy_scantronformat($r,$dom,$confname,
 7126:                                                  $type,$legacyfile{$type},
 7127:                                                  $scantronurls{$type},
 7128:                                                  $scantronfiles{$type});
 7129:                             if ($error ne '') {
 7130:                                 $error{$type} = $error;
 7131:                             }
 7132:                         }
 7133:                         if (keys(%error) == 0) {
 7134:                             $is_custom = 1;
 7135:                             $confhash{'scantron'}{'scantronformat'} =
 7136:                                 $scantronurls{'custom'};
 7137:                             my $putresult =
 7138:                                 &Apache::lonnet::put_dom('configuration',
 7139:                                                          \%confhash,$dom);
 7140:                             if ($putresult ne 'ok') {
 7141:                                 $error{'custom'} =
 7142:                                     '<span class="LC_error">'.
 7143:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 7144:                             }
 7145:                         }
 7146:                     } else {
 7147:                         ($scantronurls{'default'},my $error) =
 7148:                             &legacy_scantronformat($r,$dom,$confname,
 7149:                                           'default',$legacyfile{'default'},
 7150:                                           $scantronurls{'default'},
 7151:                                           $scantronfiles{'default'});
 7152:                         if ($error eq '') {
 7153:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 7154:                             my $putresult =
 7155:                                 &Apache::lonnet::put_dom('configuration',
 7156:                                                          \%confhash,$dom);
 7157:                             if ($putresult ne 'ok') {
 7158:                                 $error{'default'} =
 7159:                                     '<span class="LC_error">'.
 7160:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 7161:                             }
 7162:                         } else {
 7163:                             $error{'default'} = $error;
 7164:                         }
 7165:                     }
 7166:                 }
 7167:             }
 7168:         } else {
 7169:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 7170:         }
 7171:     }
 7172:     if (ref($settings) eq 'HASH') {
 7173:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 7174:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 7175:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 7176:                 $scantronurl = '';
 7177:             } else {
 7178:                 $scantronurl = $settings->{'scantronformat'};
 7179:             }
 7180:             $is_custom = 1;
 7181:         } else {
 7182:             $scantronurl = $scantronurls{'default'};
 7183:         }
 7184:     } else {
 7185:         if ($is_custom) {
 7186:             $scantronurl = $scantronurls{'custom'};
 7187:         } else {
 7188:             $scantronurl = $scantronurls{'default'};
 7189:         }
 7190:     }
 7191:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7192:     $datatable .= '<tr'.$css_class.'>';
 7193:     if (!$is_custom) {
 7194:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 7195:                       '<span class="LC_nobreak">';
 7196:         if ($scantronurl) {
 7197:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 7198:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 7199:         } else {
 7200:             $datatable = &mt('File unavailable for display');
 7201:         }
 7202:         $datatable .= '</span></td>';
 7203:         if (keys(%error) == 0) { 
 7204:             $datatable .= '<td valign="bottom">';
 7205:             if (!$switchserver) {
 7206:                 $datatable .= &mt('Upload:').'<br />';
 7207:             }
 7208:         } else {
 7209:             my $errorstr;
 7210:             foreach my $key (sort(keys(%error))) {
 7211:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 7212:             }
 7213:             $datatable .= '<td>'.$errorstr;
 7214:         }
 7215:     } else {
 7216:         if (keys(%error) > 0) {
 7217:             my $errorstr;
 7218:             foreach my $key (sort(keys(%error))) {
 7219:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 7220:             } 
 7221:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 7222:         } elsif ($scantronurl) {
 7223:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 7224:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 7225:             $datatable .= '<td><span class="LC_nobreak">'.
 7226:                           $link.
 7227:                           '<label><input type="checkbox" name="scantronformat_del"'.
 7228:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 7229:                           '<td><span class="LC_nobreak">&nbsp;'.
 7230:                           &mt('Replace:').'</span><br />';
 7231:         }
 7232:     }
 7233:     if (keys(%error) == 0) {
 7234:         if ($switchserver) {
 7235:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 7236:         } else {
 7237:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 7238:                          '<input type="file" name="scantronformat" /></span>';
 7239:         }
 7240:     }
 7241:     $datatable .= '</td></tr>';
 7242:     $$rowtotal ++;
 7243:     return $datatable;
 7244: }
 7245: 
 7246: sub legacy_scantronformat {
 7247:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 7248:     my ($url,$error);
 7249:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 7250:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 7251:         (my $result,$url) =
 7252:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 7253:                          '','',$newfile);
 7254:         if ($result ne 'ok') {
 7255:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 7256:         }
 7257:     }
 7258:     return ($url,$error);
 7259: }
 7260: 
 7261: sub print_scantronconfig {
 7262:     my ($dom,$settings,$rowtotal) = @_;
 7263:     my $itemcount = 2;
 7264:     my $is_checked = ' checked="checked"';
 7265:     my %optionson = (
 7266:                      hdr => ' checked="checked"',
 7267:                      pad => ' checked="checked"',
 7268:                      rem => ' checked="checked"',
 7269:                     );
 7270:     my %optionsoff = (
 7271:                       hdr => '',
 7272:                       pad => '',
 7273:                       rem => '',
 7274:                      );
 7275:     my $currcsvsty = 'none';
 7276:     my ($datatable,%csvfields,%checked,%onclick,%csvoptions);
 7277:     my @fields = &scantroncsv_fields();
 7278:     my %titles = &scantronconfig_titles();
 7279:     if (ref($settings) eq 'HASH') {
 7280:         if (ref($settings->{config}) eq 'HASH') {
 7281:             if ($settings->{config}->{dat}) {
 7282:                 $checked{'dat'} = $is_checked;
 7283:             }
 7284:             if (ref($settings->{config}->{csv}) eq 'HASH') {
 7285:                 if (ref($settings->{config}->{csv}->{fields}) eq 'HASH') {
 7286:                     %csvfields = %{$settings->{config}->{csv}->{fields}};
 7287:                     if (keys(%csvfields) > 0) {
 7288:                         $checked{'csv'} = $is_checked;
 7289:                         $currcsvsty = 'block';
 7290:                     }
 7291:                 }
 7292:                 if (ref($settings->{config}->{csv}->{options}) eq 'HASH') {
 7293:                     %csvoptions = %{$settings->{config}->{csv}->{options}};
 7294:                     foreach my $option (keys(%optionson)) {
 7295:                         unless ($csvoptions{$option}) {
 7296:                             $optionsoff{$option} = $optionson{$option};
 7297:                             $optionson{$option} = '';
 7298:                         }
 7299:                     }
 7300:                 }
 7301:             }
 7302:         } else {
 7303:             $checked{'dat'} = $is_checked;
 7304:         }
 7305:     } else {
 7306:         $checked{'dat'} = $is_checked;
 7307:     }
 7308:     $onclick{'csv'} = ' onclick="toggleScantron(this.form);"';
 7309:     my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 7310:     $datatable = '<tr '.$css_class.'><td>'.&mt('Supported formats').'</td>'.
 7311:                  '<td class="LC_left_item" valign="top"><span class="LC_nobreak">';
 7312:     foreach my $item ('dat','csv') {
 7313:         my $id;
 7314:         if ($item eq 'csv') {
 7315:             $id = 'id="scantronconfcsv" ';
 7316:         }
 7317:         $datatable .= '<label><input type="checkbox" name="scantronconfig" '.$id.'value="'.$item.'"'.$checked{$item}.$onclick{$item}.' />'.
 7318:                       $titles{$item}.'</label>'.('&nbsp;'x3);
 7319:         if ($item eq 'csv') {
 7320:             $datatable .= '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_cols">'.
 7321:                           '<legend>'.&mt('CSV Column Mapping').'</legend>'.
 7322:                           '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Location').'</th></tr>'."\n";
 7323:             foreach my $col (@fields) {
 7324:                 my $selnone;
 7325:                 if ($csvfields{$col} eq '') {
 7326:                     $selnone = ' selected="selected"';
 7327:                 }
 7328:                 $datatable .= '<tr><td>'.$titles{$col}.'</td>'.
 7329:                               '<td><select name="scantronconfig_csv_'.$col.'" class="scantronconfig_csv">'.
 7330:                               '<option value=""'.$selnone.'></option>';
 7331:                 for (my $i=0; $i<20; $i++) {
 7332:                     my $shown = $i+1;
 7333:                     my $sel;
 7334:                     unless ($selnone) {
 7335:                         if (exists($csvfields{$col})) {
 7336:                             if ($csvfields{$col} == $i) {
 7337:                                 $sel = ' selected="selected"';
 7338:                             }
 7339:                         }
 7340:                     }
 7341:                     $datatable .= '<option value="'.$i.'"'.$sel.'>'.$shown.'</option>';
 7342:                 }
 7343:                 $datatable .= '</select></td></tr>';
 7344:            }
 7345:            $datatable .= '</table></fieldset>'.
 7346:                          '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_options">'.
 7347:                          '<legend>'.&mt('CSV Options').'</legend>';
 7348:            foreach my $option ('hdr','pad','rem') {
 7349:                $datatable .= '<span class="LC_nobreak">'.$titles{$option}.':'.
 7350:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="1"'.$optionson{$option}.' />'.
 7351:                          &mt('Yes').'</label>'.('&nbsp;'x2)."\n".
 7352:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="0"'.$optionsoff{$option}.' />'.&mt('No').'</label></span><br />';
 7353:            }
 7354:            $datatable .= '</fieldset>';
 7355:            $itemcount ++;
 7356:         }
 7357:     }
 7358:     $datatable .= '</td></tr>';
 7359:     $$rowtotal ++;
 7360:     return $datatable;
 7361: }
 7362: 
 7363: sub scantronconfig_titles {
 7364:     return &Apache::lonlocal::texthash(
 7365:                                           dat => 'Standard format (.dat)',
 7366:                                           csv => 'Comma separated values (.csv)',
 7367:                                           hdr => 'Remove first line in file (contains column titles)',
 7368:                                           pad => 'Prepend 0s to PaperID',
 7369:                                           rem => 'Remove leading spaces (except Question Response columns)',
 7370:                                           CODE => 'CODE',
 7371:                                           ID   => 'Student ID',
 7372:                                           PaperID => 'Paper ID',
 7373:                                           FirstName => 'First Name',
 7374:                                           LastName => 'Last Name',
 7375:                                           FirstQuestion => 'First Question Response',
 7376:                                           Section => 'Section',
 7377:     );
 7378: }
 7379: 
 7380: sub scantroncsv_fields {
 7381:     return ('PaperID','LastName','FirstName','ID','Section','CODE','FirstQuestion');
 7382: }
 7383: 
 7384: sub print_coursecategories {
 7385:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 7386:     my $datatable;
 7387:     if ($position eq 'top') {
 7388:         my (%checked);
 7389:         my @catitems = ('unauth','auth');
 7390:         my @cattypes = ('std','domonly','codesrch','none');
 7391:         $checked{'unauth'} = 'std';
 7392:         $checked{'auth'} = 'std';
 7393:         if (ref($settings) eq 'HASH') {
 7394:             foreach my $type (@cattypes) {
 7395:                 if ($type eq $settings->{'unauth'}) {
 7396:                     $checked{'unauth'} = $type;
 7397:                 }
 7398:                 if ($type eq $settings->{'auth'}) {
 7399:                     $checked{'auth'} = $type;
 7400:                 }
 7401:             }
 7402:         }
 7403:         my %lt = &Apache::lonlocal::texthash (
 7404:                                                unauth   => 'Catalog type for unauthenticated users',
 7405:                                                auth     => 'Catalog type for authenticated users',
 7406:                                                none     => 'No catalog',
 7407:                                                std      => 'Standard catalog',
 7408:                                                domonly  => 'Domain-only catalog',
 7409:                                                codesrch => "Code search form",
 7410:                                              );
 7411:        my $itemcount = 0;
 7412:        foreach my $item (@catitems) {
 7413:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 7414:            $datatable .= '<tr '.$css_class.'>'.
 7415:                          '<td>'.$lt{$item}.'</td>'.
 7416:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 7417:            foreach my $type (@cattypes) {
 7418:                my $ischecked;
 7419:                if ($checked{$item} eq $type) {
 7420:                    $ischecked=' checked="checked"';
 7421:                }
 7422:                $datatable .= '<label>'.
 7423:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 7424:                              ' />'.$lt{$type}.'</label>&nbsp;';
 7425:            }
 7426:            $datatable .= '</span></td></tr>';
 7427:            $itemcount ++;
 7428:         }
 7429:         $$rowtotal += $itemcount;
 7430:     } elsif ($position eq 'middle') {
 7431:         my $toggle_cats_crs = ' ';
 7432:         my $toggle_cats_dom = ' checked="checked" ';
 7433:         my $can_cat_crs = ' ';
 7434:         my $can_cat_dom = ' checked="checked" ';
 7435:         my $toggle_catscomm_comm = ' ';
 7436:         my $toggle_catscomm_dom = ' checked="checked" ';
 7437:         my $can_catcomm_comm = ' ';
 7438:         my $can_catcomm_dom = ' checked="checked" ';
 7439: 
 7440:         if (ref($settings) eq 'HASH') {
 7441:             if ($settings->{'togglecats'} eq 'crs') {
 7442:                 $toggle_cats_crs = $toggle_cats_dom;
 7443:                 $toggle_cats_dom = ' ';
 7444:             }
 7445:             if ($settings->{'categorize'} eq 'crs') {
 7446:                 $can_cat_crs = $can_cat_dom;
 7447:                 $can_cat_dom = ' ';
 7448:             }
 7449:             if ($settings->{'togglecatscomm'} eq 'comm') {
 7450:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 7451:                 $toggle_catscomm_dom = ' ';
 7452:             }
 7453:             if ($settings->{'categorizecomm'} eq 'comm') {
 7454:                 $can_catcomm_comm = $can_catcomm_dom;
 7455:                 $can_catcomm_dom = ' ';
 7456:             }
 7457:         }
 7458:         my %title = &Apache::lonlocal::texthash (
 7459:                      togglecats     => 'Show/Hide a course in catalog',
 7460:                      togglecatscomm => 'Show/Hide a community in catalog',
 7461:                      categorize     => 'Assign a category to a course',
 7462:                      categorizecomm => 'Assign a category to a community',
 7463:                     );
 7464:         my %level = &Apache::lonlocal::texthash (
 7465:                      dom  => 'Set in Domain',
 7466:                      crs  => 'Set in Course',
 7467:                      comm => 'Set in Community',
 7468:                     );
 7469:         $datatable = '<tr class="LC_odd_row">'.
 7470:                   '<td>'.$title{'togglecats'}.'</td>'.
 7471:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 7472:                   '<input type="radio" name="togglecats"'.
 7473:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7474:                   '<label><input type="radio" name="togglecats"'.
 7475:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 7476:                   '</tr><tr>'.
 7477:                   '<td>'.$title{'categorize'}.'</td>'.
 7478:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 7479:                   '<label><input type="radio" name="categorize"'.
 7480:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7481:                   '<label><input type="radio" name="categorize"'.
 7482:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 7483:                   '</tr><tr class="LC_odd_row">'.
 7484:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 7485:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 7486:                   '<input type="radio" name="togglecatscomm"'.
 7487:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7488:                   '<label><input type="radio" name="togglecatscomm"'.
 7489:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 7490:                   '</tr><tr>'.
 7491:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 7492:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 7493:                   '<label><input type="radio" name="categorizecomm"'.
 7494:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7495:                   '<label><input type="radio" name="categorizecomm"'.
 7496:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 7497:                   '</tr>';
 7498:         $$rowtotal += 4;
 7499:     } else {
 7500:         my $css_class;
 7501:         my $itemcount = 1;
 7502:         my $cathash; 
 7503:         if (ref($settings) eq 'HASH') {
 7504:             $cathash = $settings->{'cats'};
 7505:         }
 7506:         if (ref($cathash) eq 'HASH') {
 7507:             my (@cats,@trails,%allitems,%idx,@jsarray);
 7508:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 7509:                                                    \%allitems,\%idx,\@jsarray);
 7510:             my $maxdepth = scalar(@cats);
 7511:             my $colattrib = '';
 7512:             if ($maxdepth > 2) {
 7513:                 $colattrib = ' colspan="2" ';
 7514:             }
 7515:             my @path;
 7516:             if (@cats > 0) {
 7517:                 if (ref($cats[0]) eq 'ARRAY') {
 7518:                     my $numtop = @{$cats[0]};
 7519:                     my $maxnum = $numtop;
 7520:                     my %default_names = (
 7521:                           instcode    => &mt('Official courses'),
 7522:                           communities => &mt('Communities'),
 7523:                     );
 7524: 
 7525:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 7526:                         ($cathash->{'instcode::0'} eq '') ||
 7527:                         (!grep(/^communities$/,@{$cats[0]})) || 
 7528:                         ($cathash->{'communities::0'} eq '')) {
 7529:                         $maxnum ++;
 7530:                     }
 7531:                     my $lastidx;
 7532:                     for (my $i=0; $i<$numtop; $i++) {
 7533:                         my $parent = $cats[0][$i];
 7534:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7535:                         my $item = &escape($parent).'::0';
 7536:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 7537:                         $lastidx = $idx{$item};
 7538:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7539:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 7540:                         for (my $k=0; $k<=$maxnum; $k++) {
 7541:                             my $vpos = $k+1;
 7542:                             my $selstr;
 7543:                             if ($k == $i) {
 7544:                                 $selstr = ' selected="selected" ';
 7545:                             }
 7546:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7547:                         }
 7548:                         $datatable .= '</select></span></td><td>';
 7549:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 7550:                             $datatable .=  '<span class="LC_nobreak">'
 7551:                                            .$default_names{$parent}.'</span>';
 7552:                             if ($parent eq 'instcode') {
 7553:                                 $datatable .= '<br /><span class="LC_nobreak">('
 7554:                                               .&mt('with institutional codes')
 7555:                                               .')</span></td><td'.$colattrib.'>';
 7556:                             } else {
 7557:                                 $datatable .= '<table><tr><td>';
 7558:                             }
 7559:                             $datatable .= '<span class="LC_nobreak">'
 7560:                                           .'<label><input type="radio" name="'
 7561:                                           .$parent.'" value="1" checked="checked" />'
 7562:                                           .&mt('Display').'</label>';
 7563:                             if ($parent eq 'instcode') {
 7564:                                 $datatable .= '&nbsp;';
 7565:                             } else {
 7566:                                 $datatable .= '</span></td></tr><tr><td>'
 7567:                                               .'<span class="LC_nobreak">';
 7568:                             }
 7569:                             $datatable .= '<label><input type="radio" name="'
 7570:                                           .$parent.'" value="0" />'
 7571:                                           .&mt('Do not display').'</label></span>';
 7572:                             if ($parent eq 'communities') {
 7573:                                 $datatable .= '</td></tr></table>';
 7574:                             }
 7575:                             $datatable .= '</td>';
 7576:                         } else {
 7577:                             $datatable .= $parent
 7578:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 7579:                                           .'<input type="checkbox" name="deletecategory" '
 7580:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 7581:                         }
 7582:                         my $depth = 1;
 7583:                         push(@path,$parent);
 7584:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 7585:                         pop(@path);
 7586:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 7587:                         $itemcount ++;
 7588:                     }
 7589:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7590:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 7591:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 7592:                     for (my $k=0; $k<=$maxnum; $k++) {
 7593:                         my $vpos = $k+1;
 7594:                         my $selstr;
 7595:                         if ($k == $numtop) {
 7596:                             $selstr = ' selected="selected" ';
 7597:                         }
 7598:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7599:                     }
 7600:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 7601:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 7602:                                   .'</tr>'."\n";
 7603:                     $itemcount ++;
 7604:                     foreach my $default ('instcode','communities') {
 7605:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 7606:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7607:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 7608:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 7609:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 7610:                             for (my $k=0; $k<=$maxnum; $k++) {
 7611:                                 my $vpos = $k+1;
 7612:                                 my $selstr;
 7613:                                 if ($k == $maxnum) {
 7614:                                     $selstr = ' selected="selected" ';
 7615:                                 }
 7616:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7617:                             }
 7618:                             $datatable .= '</select></span></td>'.
 7619:                                           '<td><span class="LC_nobreak">'.
 7620:                                           $default_names{$default}.'</span>';
 7621:                             if ($default eq 'instcode') {
 7622:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 7623:                                               .&mt('with institutional codes').')</span>';
 7624:                             }
 7625:                             $datatable .= '</td>'
 7626:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 7627:                                           .&mt('Display').'</label>&nbsp;'
 7628:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 7629:                                           .&mt('Do not display').'</label></span></td></tr>';
 7630:                         }
 7631:                     }
 7632:                 }
 7633:             } else {
 7634:                 $datatable .= &initialize_categories($itemcount);
 7635:             }
 7636:         } else {
 7637:             $datatable .= '<tr><td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td></tr>'
 7638:                           .&initialize_categories($itemcount);
 7639:         }
 7640:         $$rowtotal += $itemcount;
 7641:     }
 7642:     return $datatable;
 7643: }
 7644: 
 7645: sub print_serverstatuses {
 7646:     my ($dom,$settings,$rowtotal) = @_;
 7647:     my $datatable;
 7648:     my @pages = &serverstatus_pages();
 7649:     my (%namedaccess,%machineaccess);
 7650:     foreach my $type (@pages) {
 7651:         $namedaccess{$type} = '';
 7652:         $machineaccess{$type}= '';
 7653:     }
 7654:     if (ref($settings) eq 'HASH') {
 7655:         foreach my $type (@pages) {
 7656:             if (exists($settings->{$type})) {
 7657:                 if (ref($settings->{$type}) eq 'HASH') {
 7658:                     foreach my $key (keys(%{$settings->{$type}})) {
 7659:                         if ($key eq 'namedusers') {
 7660:                             $namedaccess{$type} = $settings->{$type}->{$key};
 7661:                         } elsif ($key eq 'machines') {
 7662:                             $machineaccess{$type} = $settings->{$type}->{$key};
 7663:                         }
 7664:                     }
 7665:                 }
 7666:             }
 7667:         }
 7668:     }
 7669:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 7670:     my $rownum = 0;
 7671:     my $css_class;
 7672:     foreach my $type (@pages) {
 7673:         $rownum ++;
 7674:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 7675:         $datatable .= '<tr'.$css_class.'>'.
 7676:                       '<td><span class="LC_nobreak">'.
 7677:                       $titles->{$type}.'</span></td>'.
 7678:                       '<td class="LC_left_item">'.
 7679:                       '<input type="text" name="'.$type.'_namedusers" '.
 7680:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 7681:                       '<td class="LC_right_item">'.
 7682:                       '<span class="LC_nobreak">'.
 7683:                       '<input type="text" name="'.$type.'_machines" '.
 7684:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 7685:                       '</span></td></tr>'."\n";
 7686:     }
 7687:     $$rowtotal += $rownum;
 7688:     return $datatable;
 7689: }
 7690: 
 7691: sub serverstatus_pages {
 7692:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 7693:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 7694:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 7695:             'uniquecodes','diskusage','coursecatalog');
 7696: }
 7697: 
 7698: sub defaults_javascript {
 7699:     my ($settings) = @_;
 7700:     return unless (ref($settings) eq 'HASH');
 7701:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 7702:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 7703:         if ($maxnum eq '') {
 7704:             $maxnum = 0;
 7705:         }
 7706:         $maxnum ++;
 7707:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 7708:         return <<"ENDSCRIPT";
 7709: <script type="text/javascript">
 7710: // <![CDATA[
 7711: function reorderTypes(form,caller) {
 7712:     var changedVal;
 7713: $jstext 
 7714:     var newpos = 'addinststatus_pos';
 7715:     var current = new Array;
 7716:     var maxh = $maxnum;
 7717:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 7718:     var oldVal;
 7719:     if (caller == newpos) {
 7720:         changedVal = newitemVal;
 7721:     } else {
 7722:         var curritem = 'inststatus_pos_'+caller;
 7723:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 7724:         current[newitemVal] = newpos;
 7725:     }
 7726:     for (var i=0; i<inststatuses.length; i++) {
 7727:         if (inststatuses[i] != caller) {
 7728:             var elementName = 'inststatus_pos_'+inststatuses[i];
 7729:             if (form.elements[elementName]) {
 7730:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 7731:                 current[currVal] = elementName;
 7732:             }
 7733:         }
 7734:     }
 7735:     for (var j=0; j<maxh; j++) {
 7736:         if (current[j] == undefined) {
 7737:             oldVal = j;
 7738:         }
 7739:     }
 7740:     if (oldVal < changedVal) {
 7741:         for (var k=oldVal+1; k<=changedVal ; k++) {
 7742:            var elementName = current[k];
 7743:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 7744:         }
 7745:     } else {
 7746:         for (var k=changedVal; k<oldVal; k++) {
 7747:             var elementName = current[k];
 7748:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 7749:         }
 7750:     }
 7751:     return;
 7752: }
 7753: 
 7754: // ]]>
 7755: </script>
 7756: 
 7757: ENDSCRIPT
 7758:     }
 7759: }
 7760: 
 7761: sub passwords_javascript {
 7762:     my %intalert = &Apache::lonlocal::texthash (
 7763:         authcheck => '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.',
 7764:         authcost => 'Warning: bcrypt encryption cost for internal authentication must be an integer.',
 7765:         passmin => 'Warning: minimum password length must be a positive integer greater than 6.',
 7766:         passmax => 'Warning: maximum password length must be a positive integer (or blank).',
 7767:         passexp => 'Warning: days before password expiration must be a positive integer (or blank).',
 7768:         passnum => 'Warning: number of previous passwords to save must be a positive integer (or blank).',
 7769:     );
 7770:     &js_escape(\%intalert);
 7771:     my $defmin = $Apache::lonnet::passwdmin;
 7772:     my $intauthjs = <<"ENDSCRIPT";
 7773: 
 7774: function warnIntAuth(field) {
 7775:     if (field.name == 'intauth_check') {
 7776:         if (field.value == '2') {
 7777:             alert('$intalert{authcheck}');
 7778:         }
 7779:     }
 7780:     if (field.name == 'intauth_cost') {
 7781:         field.value.replace(/\s/g,'');
 7782:         if (field.value != '') {
 7783:             var regexdigit=/^\\d+\$/;
 7784:             if (!regexdigit.test(field.value)) {
 7785:                 alert('$intalert{authcost}');
 7786:             }
 7787:         }
 7788:     }
 7789:     return;
 7790: }
 7791: 
 7792: function warnIntPass(field) {
 7793:     field.value.replace(/^\s+/,'');
 7794:     field.value.replace(/\s+\$/,'');
 7795:     var regexdigit=/^\\d+\$/;
 7796:     if (field.name == 'passwords_min') {
 7797:         if (field.value == '') {
 7798:             alert('$intalert{passmin}');
 7799:             field.value = '$defmin';
 7800:         } else {
 7801:             if (!regexdigit.test(field.value)) {
 7802:                 alert('$intalert{passmin}');
 7803:                 field.value = '$defmin';
 7804:             }
 7805:             var minval = parseInt(field.value,10);
 7806:             if (minval < $defmin) {
 7807:                 alert('$intalert{passmin}');
 7808:                 field.value = '$defmin';
 7809:             }
 7810:         }
 7811:     } else {
 7812:         if (field.value == '0') {
 7813:             field.value = '';
 7814:         }
 7815:         if (field.value != '') {
 7816:             if (field.name == 'passwords_expire') {
 7817:                 var regexpposnum=/^\\d+(|\\.\\d*)\$/;
 7818:                 if (!regexpposnum.test(field.value)) {
 7819:                     alert('$intalert{passexp}');
 7820:                     field.value = '';
 7821:                 } else {
 7822:                     var expval = parseFloat(field.value);
 7823:                     if (expval == 0) {
 7824:                         alert('$intalert{passexp}');
 7825:                         field.value = '';
 7826:                     }
 7827:                 }
 7828:             } else {
 7829:                 if (!regexdigit.test(field.value)) {
 7830:                     if (field.name == 'passwords_max') {
 7831:                         alert('$intalert{passmax}');
 7832:                     } else {
 7833:                         if (field.name == 'passwords_numsaved') {
 7834:                             alert('$intalert{passnum}');
 7835:                         }
 7836:                     }
 7837:                     field.value = '';
 7838:                 }
 7839:             }
 7840:         }
 7841:     }
 7842:     return;
 7843: }
 7844: 
 7845: ENDSCRIPT
 7846:     return &Apache::lonhtmlcommon::scripttag($intauthjs);
 7847: }
 7848: 
 7849: sub coursecategories_javascript {
 7850:     my ($settings) = @_;
 7851:     my ($output,$jstext,$cathash);
 7852:     if (ref($settings) eq 'HASH') {
 7853:         $cathash = $settings->{'cats'};
 7854:     }
 7855:     if (ref($cathash) eq 'HASH') {
 7856:         my (@cats,@jsarray,%idx);
 7857:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 7858:         if (@jsarray > 0) {
 7859:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 7860:             for (my $i=0; $i<@jsarray; $i++) {
 7861:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 7862:                     my $catstr = join('","',@{$jsarray[$i]});
 7863:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 7864:                 }
 7865:             }
 7866:         }
 7867:     } else {
 7868:         $jstext  = '    var categories = Array(1);'."\n".
 7869:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 7870:     }
 7871:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 7872:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 7873:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
 7874:     &js_escape(\$instcode_reserved);
 7875:     &js_escape(\$communities_reserved);
 7876:     &js_escape(\$choose_again);
 7877:     $output = <<"ENDSCRIPT";
 7878: <script type="text/javascript">
 7879: // <![CDATA[
 7880: function reorderCats(form,parent,item,idx) {
 7881:     var changedVal;
 7882: $jstext
 7883:     var newpos = 'addcategory_pos';
 7884:     if (parent == '') {
 7885:         var has_instcode = 0;
 7886:         var maxtop = categories[idx].length;
 7887:         for (var j=0; j<maxtop; j++) {
 7888:             if (categories[idx][j] == 'instcode::0') {
 7889:                 has_instcode == 1;
 7890:             }
 7891:         }
 7892:         if (has_instcode == 0) {
 7893:             categories[idx][maxtop] = 'instcode_pos';
 7894:         }
 7895:     } else {
 7896:         newpos += '_'+parent;
 7897:     }
 7898:     var maxh = 1 + categories[idx].length;
 7899:     var current = new Array;
 7900:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 7901:     if (item == newpos) {
 7902:         changedVal = newitemVal;
 7903:     } else {
 7904:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 7905:         current[newitemVal] = newpos;
 7906:     }
 7907:     for (var i=0; i<categories[idx].length; i++) {
 7908:         var elementName = categories[idx][i];
 7909:         if (elementName != item) {
 7910:             if (form.elements[elementName]) {
 7911:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 7912:                 current[currVal] = elementName;
 7913:             }
 7914:         }
 7915:     }
 7916:     var oldVal;
 7917:     for (var j=0; j<maxh; j++) {
 7918:         if (current[j] == undefined) {
 7919:             oldVal = j;
 7920:         }
 7921:     }
 7922:     if (oldVal < changedVal) {
 7923:         for (var k=oldVal+1; k<=changedVal ; k++) {
 7924:            var elementName = current[k];
 7925:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 7926:         }
 7927:     } else {
 7928:         for (var k=changedVal; k<oldVal; k++) {
 7929:             var elementName = current[k];
 7930:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 7931:         }
 7932:     }
 7933:     return;
 7934: }
 7935: 
 7936: function categoryCheck(form) {
 7937:     if (form.elements['addcategory_name'].value == 'instcode') {
 7938:         alert('$instcode_reserved\\n$choose_again');
 7939:         return false;
 7940:     }
 7941:     if (form.elements['addcategory_name'].value == 'communities') {
 7942:         alert('$communities_reserved\\n$choose_again');
 7943:         return false;
 7944:     }
 7945:     return true;
 7946: }
 7947: 
 7948: // ]]>
 7949: </script>
 7950: 
 7951: ENDSCRIPT
 7952:     return $output;
 7953: }
 7954: 
 7955: sub initialize_categories {
 7956:     my ($itemcount) = @_;
 7957:     my ($datatable,$css_class,$chgstr);
 7958:     my %default_names = (
 7959:                       instcode    => 'Official courses (with institutional codes)',
 7960:                       communities => 'Communities',
 7961:                         );
 7962:     my $select0 = ' selected="selected"';
 7963:     my $select1 = '';
 7964:     foreach my $default ('instcode','communities') {
 7965:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7966:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','0'".');"';
 7967:         if ($default eq 'communities') {
 7968:             $select1 = $select0;
 7969:             $select0 = '';
 7970:         }
 7971:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7972:                      .'<select name="'.$default.'_pos">'
 7973:                      .'<option value="0"'.$select0.'>1</option>'
 7974:                      .'<option value="1"'.$select1.'>2</option>'
 7975:                      .'<option value="2">3</option></select>&nbsp;'
 7976:                      .$default_names{$default}
 7977:                      .'</span></td><td><span class="LC_nobreak">'
 7978:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 7979:                      .&mt('Display').'</label>&nbsp;<label>'
 7980:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 7981:                  .'</label></span></td></tr>';
 7982:         $itemcount ++;
 7983:     }
 7984:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7985:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 7986:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7987:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 7988:                   .'<option value="0">1</option>'
 7989:                   .'<option value="1">2</option>'
 7990:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 7991:                   .&mt('Add category').'</span></td><td><span class="LC_nobreak">'.&mt('Name:')
 7992:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></span>'
 7993:                   .'</td></tr>';
 7994:     return $datatable;
 7995: }
 7996: 
 7997: sub build_category_rows {
 7998:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 7999:     my ($text,$name,$item,$chgstr);
 8000:     if (ref($cats) eq 'ARRAY') {
 8001:         my $maxdepth = scalar(@{$cats});
 8002:         if (ref($cats->[$depth]) eq 'HASH') {
 8003:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 8004:                 my $numchildren = @{$cats->[$depth]{$parent}};
 8005:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8006:                 $text .= '<td><table class="LC_data_table">';
 8007:                 my ($idxnum,$parent_name,$parent_item);
 8008:                 my $higher = $depth - 1;
 8009:                 if ($higher == 0) {
 8010:                     $parent_name = &escape($parent).'::'.$higher;
 8011:                 } else {
 8012:                     if (ref($path) eq 'ARRAY') {
 8013:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 8014:                     }
 8015:                 }
 8016:                 $parent_item = 'addcategory_pos_'.$parent_name;
 8017:                 for (my $j=0; $j<=$numchildren; $j++) {
 8018:                     if ($j < $numchildren) {
 8019:                         $name = $cats->[$depth]{$parent}[$j];
 8020:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 8021:                         $idxnum = $idx->{$item};
 8022:                     } else {
 8023:                         $name = $parent_name;
 8024:                         $item = $parent_item;
 8025:                     }
 8026:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 8027:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 8028:                     for (my $i=0; $i<=$numchildren; $i++) {
 8029:                         my $vpos = $i+1;
 8030:                         my $selstr;
 8031:                         if ($j == $i) {
 8032:                             $selstr = ' selected="selected" ';
 8033:                         }
 8034:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 8035:                     }
 8036:                     $text .= '</select>&nbsp;';
 8037:                     if ($j < $numchildren) {
 8038:                         my $deeper = $depth+1;
 8039:                         $text .= $name.'&nbsp;'
 8040:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 8041:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 8042:                         if(ref($path) eq 'ARRAY') {
 8043:                             push(@{$path},$name);
 8044:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 8045:                             pop(@{$path});
 8046:                         }
 8047:                     } else {
 8048:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="text" size="20" name="addcategory_name_';
 8049:                         if ($j == $numchildren) {
 8050:                             $text .= $name;
 8051:                         } else {
 8052:                             $text .= $item;
 8053:                         }
 8054:                         $text .= '" value="" />';
 8055:                     }
 8056:                     $text .= '</td></tr>';
 8057:                 }
 8058:                 $text .= '</table></td>';
 8059:             } else {
 8060:                 my $higher = $depth-1;
 8061:                 if ($higher == 0) {
 8062:                     $name = &escape($parent).'::'.$higher;
 8063:                 } else {
 8064:                     if (ref($path) eq 'ARRAY') {
 8065:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 8066:                     }
 8067:                 }
 8068:                 my $colspan;
 8069:                 if ($parent ne 'instcode') {
 8070:                     $colspan = $maxdepth - $depth - 1;
 8071:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="text" size="20" name="subcat_'.$name.'" value="" /></td>';
 8072:                 }
 8073:             }
 8074:         }
 8075:     }
 8076:     return $text;
 8077: }
 8078: 
 8079: sub modifiable_userdata_row {
 8080:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref,
 8081:         $rowid,$customcss,$rowstyle) = @_;
 8082:     my ($role,$rolename,$statustype);
 8083:     $role = $item;
 8084:     if ($context eq 'cancreate') {
 8085:         if ($item =~ /^(emailusername)_(.+)$/) {
 8086:             $role = $1;
 8087:             $statustype = $2;
 8088:             if (ref($usertypes) eq 'HASH') {
 8089:                 if ($usertypes->{$statustype}) {
 8090:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 8091:                 } else {
 8092:                     $rolename = &mt('Data provided by user');
 8093:                 }
 8094:             }
 8095:         }
 8096:     } elsif ($context eq 'selfcreate') {
 8097:         if (ref($usertypes) eq 'HASH') {
 8098:             $rolename = $usertypes->{$role};
 8099:         } else {
 8100:             $rolename = $role;
 8101:         }
 8102:     } else {
 8103:         if ($role eq 'cr') {
 8104:             $rolename = &mt('Custom role');
 8105:         } else {
 8106:             $rolename = &Apache::lonnet::plaintext($role);
 8107:         }
 8108:     }
 8109:     my (@fields,%fieldtitles);
 8110:     if (ref($fieldsref) eq 'ARRAY') {
 8111:         @fields = @{$fieldsref};
 8112:     } else {
 8113:         @fields = ('lastname','firstname','middlename','generation',
 8114:                    'permanentemail','id');
 8115:     }
 8116:     if ((ref($titlesref) eq 'HASH')) {
 8117:         %fieldtitles = %{$titlesref};
 8118:     } else {
 8119:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8120:     }
 8121:     my $output;
 8122:     my $css_class;
 8123:     if ($rowcount%2) {
 8124:         $css_class = 'LC_odd_row';
 8125:     }
 8126:     if ($customcss) {
 8127:         $css_class .= " $customcss";
 8128:     }
 8129:     $css_class =~ s/^\s+//;
 8130:     if ($css_class) {
 8131:         $css_class = ' class="'.$css_class.'"';
 8132:     }
 8133:     if ($rowstyle) {
 8134:         $css_class .= ' style="'.$rowstyle.'"';
 8135:     }
 8136:     if ($rowid) {
 8137:         $rowid = ' id="'.$rowid.'"';
 8138:     }
 8139: 
 8140:     $output = '<tr '.$css_class.$rowid.'>'.
 8141:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 8142:               '<td class="LC_left_item" colspan="2"><table>';
 8143:     my $rem;
 8144:     my %checks;
 8145:     if (ref($settings) eq 'HASH') {
 8146:         if (ref($settings->{$context}) eq 'HASH') {
 8147:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 8148:                 my $hashref = $settings->{$context}->{$role};
 8149:                 if ($role eq 'emailusername') {
 8150:                     if ($statustype) {
 8151:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 8152:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 8153:                             if (ref($hashref) eq 'HASH') { 
 8154:                                 foreach my $field (@fields) {
 8155:                                     if ($hashref->{$field}) {
 8156:                                         $checks{$field} = $hashref->{$field};
 8157:                                     }
 8158:                                 }
 8159:                             }
 8160:                         }
 8161:                     }
 8162:                 } else {
 8163:                     if (ref($hashref) eq 'HASH') {
 8164:                         foreach my $field (@fields) {
 8165:                             if ($hashref->{$field}) {
 8166:                                 $checks{$field} = ' checked="checked" ';
 8167:                             }
 8168:                         }
 8169:                     }
 8170:                 }
 8171:             }
 8172:         }
 8173:     }
 8174: 
 8175:     my $total = scalar(@fields);
 8176:     for (my $i=0; $i<$total; $i++) {
 8177:         $rem = $i%($numinrow);
 8178:         if ($rem == 0) {
 8179:             if ($i > 0) {
 8180:                 $output .= '</tr>';
 8181:             }
 8182:             $output .= '<tr>';
 8183:         }
 8184:         my $check = ' ';
 8185:         unless ($role eq 'emailusername') {
 8186:             if (exists($checks{$fields[$i]})) {
 8187:                 $check = $checks{$fields[$i]};
 8188:             } else {
 8189:                 if ($role eq 'st') {
 8190:                     if (ref($settings) ne 'HASH') {
 8191:                         $check = ' checked="checked" '; 
 8192:                     }
 8193:                 }
 8194:             }
 8195:         }
 8196:         $output .= '<td class="LC_left_item">'.
 8197:                    '<span class="LC_nobreak">';
 8198:         if ($role eq 'emailusername') {
 8199:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 8200:                 $checks{$fields[$i]} = 'omit';
 8201:             }
 8202:             foreach my $option ('required','optional','omit') {
 8203:                 my $checked='';
 8204:                 if ($checks{$fields[$i]} eq $option) {
 8205:                     $checked='checked="checked" ';
 8206:                 }
 8207:                 $output .= '<label>'.
 8208:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 8209:                            &mt($option).'</label>'.('&nbsp;' x2);
 8210:             }
 8211:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 8212:         } else {
 8213:             $output .= '<label>'.
 8214:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 8215:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 8216:                        '</label>';
 8217:         }
 8218:         $output .= '</span></td>';
 8219:     }
 8220:     $rem = $total%$numinrow;
 8221:     my $colsleft;
 8222:     if ($rem) {
 8223:         $colsleft = $numinrow - $rem;
 8224:     }
 8225:     if ($colsleft > 1) {
 8226:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8227:                    '&nbsp;</td>';
 8228:     } elsif ($colsleft == 1) {
 8229:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 8230:     }
 8231:     $output .= '</tr></table></td></tr>';
 8232:     return $output;
 8233: }
 8234: 
 8235: sub insttypes_row {
 8236:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
 8237:         $customcss,$rowstyle) = @_;
 8238:     my %lt = &Apache::lonlocal::texthash (
 8239:                       cansearch => 'Users allowed to search',
 8240:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 8241:                       lockablenames => 'User preference to lock name',
 8242:                       selfassign    => 'Self-reportable affiliations',
 8243:                       overrides     => "Override domain's helpdesk settings based on requester's affiliation",
 8244:              );
 8245:     my $showdom;
 8246:     if ($context eq 'cansearch') {
 8247:         $showdom = ' ('.$dom.')';
 8248:     }
 8249:     my $class = 'LC_left_item';
 8250:     if ($context eq 'statustocreate') {
 8251:         $class = 'LC_right_item';
 8252:     }
 8253:     my $css_class;
 8254:     if ($$rowtotal%2) {
 8255:         $css_class = 'LC_odd_row';
 8256:     }
 8257:     if ($customcss) {
 8258:         $css_class .= ' '.$customcss;
 8259:     }
 8260:     $css_class =~ s/^\s+//;
 8261:     if ($css_class) {
 8262:         $css_class = ' class="'.$css_class.'"';
 8263:     }
 8264:     if ($rowstyle) {
 8265:         $css_class .= ' style="'.$rowstyle.'"';
 8266:     }
 8267:     if ($onclick) {
 8268:         $onclick = 'onclick="'.$onclick.'" ';
 8269:     }
 8270:     my $output = '<tr'.$css_class.'>'.
 8271:                  '<td>'.$lt{$context}.$showdom.
 8272:                  '</td><td class="'.$class.'" colspan="2"><table>';
 8273:     my $rem;
 8274:     if (ref($types) eq 'ARRAY') {
 8275:         for (my $i=0; $i<@{$types}; $i++) {
 8276:             if (defined($usertypes->{$types->[$i]})) {
 8277:                 my $rem = $i%($numinrow);
 8278:                 if ($rem == 0) {
 8279:                     if ($i > 0) {
 8280:                         $output .= '</tr>';
 8281:                     }
 8282:                     $output .= '<tr>';
 8283:                 }
 8284:                 my $check = ' ';
 8285:                 if (ref($settings) eq 'HASH') {
 8286:                     if (ref($settings->{$context}) eq 'ARRAY') {
 8287:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 8288:                             $check = ' checked="checked" ';
 8289:                         }
 8290:                     } elsif (ref($settings->{$context}) eq 'HASH') {
 8291:                         if (ref($settings->{$context}->{$types->[$i]}) eq 'HASH') {
 8292:                             $check = ' checked="checked" ';
 8293:                         }
 8294:                     } elsif ($context eq 'statustocreate') {
 8295:                         $check = ' checked="checked" ';
 8296:                     }
 8297:                 }
 8298:                 $output .= '<td class="LC_left_item">'.
 8299:                            '<span class="LC_nobreak"><label>'.
 8300:                            '<input type="checkbox" name="'.$context.'" '.
 8301:                            'value="'.$types->[$i].'"'.$check.$onclick.'/>'.
 8302:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 8303:             }
 8304:         }
 8305:         $rem = @{$types}%($numinrow);
 8306:     }
 8307:     my $colsleft = $numinrow - $rem;
 8308:     if ($context eq 'overrides') {
 8309:         if ($colsleft > 1) {
 8310:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 8311:         } else {
 8312:             $output .= '<td class="LC_left_item">';
 8313:         }
 8314:         $output .= '&nbsp;';
 8315:     } else {
 8316:         if ($rem == 0) {
 8317:             $output .= '<tr>';
 8318:         }
 8319:         if ($colsleft > 1) {
 8320:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 8321:         } else {
 8322:             $output .= '<td class="LC_left_item">';
 8323:         }
 8324:         my $defcheck = ' ';
 8325:         if (ref($settings) eq 'HASH') {  
 8326:             if (ref($settings->{$context}) eq 'ARRAY') {
 8327:                 if (grep(/^default$/,@{$settings->{$context}})) {
 8328:                     $defcheck = ' checked="checked" ';
 8329:                 }
 8330:             } elsif ($context eq 'statustocreate') {
 8331:                 $defcheck = ' checked="checked" ';
 8332:             }
 8333:         }
 8334:         $output .= '<span class="LC_nobreak"><label>'.
 8335:                    '<input type="checkbox" name="'.$context.'" '.
 8336:                    'value="default"'.$defcheck.$onclick.' />'.
 8337:                    $othertitle.'</label></span>';
 8338:     }
 8339:     $output .= '</td></tr></table></td></tr>';
 8340:     return $output;
 8341: }
 8342: 
 8343: sub sorted_searchtitles {
 8344:     my %searchtitles = &Apache::lonlocal::texthash(
 8345:                          'uname' => 'username',
 8346:                          'lastname' => 'last name',
 8347:                          'lastfirst' => 'last name, first name',
 8348:                      );
 8349:     my @titleorder = ('uname','lastname','lastfirst');
 8350:     return (\%searchtitles,\@titleorder);
 8351: }
 8352: 
 8353: sub sorted_searchtypes {
 8354:     my %srchtypes_desc = (
 8355:                            exact    => 'is exact match',
 8356:                            contains => 'contains ..',
 8357:                            begins   => 'begins with ..',
 8358:                          );
 8359:     my @srchtypeorder = ('exact','begins','contains');
 8360:     return (\%srchtypes_desc,\@srchtypeorder);
 8361: }
 8362: 
 8363: sub usertype_update_row {
 8364:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 8365:     my $datatable;
 8366:     my $numinrow = 4;
 8367:     foreach my $type (@{$types}) {
 8368:         if (defined($usertypes->{$type})) {
 8369:             $$rownums ++;
 8370:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 8371:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 8372:                           '</td><td class="LC_left_item"><table>';
 8373:             for (my $i=0; $i<@{$fields}; $i++) {
 8374:                 my $rem = $i%($numinrow);
 8375:                 if ($rem == 0) {
 8376:                     if ($i > 0) {
 8377:                         $datatable .= '</tr>';
 8378:                     }
 8379:                     $datatable .= '<tr>';
 8380:                 }
 8381:                 my $check = ' ';
 8382:                 if (ref($settings) eq 'HASH') {
 8383:                     if (ref($settings->{'fields'}) eq 'HASH') {
 8384:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 8385:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 8386:                                 $check = ' checked="checked" ';
 8387:                             }
 8388:                         }
 8389:                     }
 8390:                 }
 8391: 
 8392:                 if ($i == @{$fields}-1) {
 8393:                     my $colsleft = $numinrow - $rem;
 8394:                     if ($colsleft > 1) {
 8395:                         $datatable .= '<td colspan="'.$colsleft.'">';
 8396:                     } else {
 8397:                         $datatable .= '<td>';
 8398:                     }
 8399:                 } else {
 8400:                     $datatable .= '<td>';
 8401:                 }
 8402:                 $datatable .= '<span class="LC_nobreak"><label>'.
 8403:                               '<input type="checkbox" name="updateable_'.$type.
 8404:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 8405:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 8406:             }
 8407:             $datatable .= '</tr></table></td></tr>';
 8408:         }
 8409:     }
 8410:     return $datatable;
 8411: }
 8412: 
 8413: sub modify_login {
 8414:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 8415:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 8416:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 8417:     %title = ( coursecatalog => 'Display course catalog',
 8418:                adminmail => 'Display administrator E-mail address',
 8419:                helpdesk  => 'Display "Contact Helpdesk" link',
 8420:                newuser => 'Link for visitors to create a user account',
 8421:                loginheader => 'Log-in box header');
 8422:     @offon = ('off','on');
 8423:     if (ref($domconfig{login}) eq 'HASH') {
 8424:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 8425:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 8426:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 8427:             }
 8428:         }
 8429:     }
 8430:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 8431:                                            \%domconfig,\%loginhash);
 8432:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 8433:     foreach my $item (@toggles) {
 8434:         $loginhash{login}{$item} = $env{'form.'.$item};
 8435:     }
 8436:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 8437:     if (ref($colchanges{'login'}) eq 'HASH') {  
 8438:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 8439:                                          \%loginhash);
 8440:     }
 8441: 
 8442:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8443:     my %domservers = &Apache::lonnet::get_servers($dom);
 8444:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 8445:     if (keys(%servers) > 1) {
 8446:         foreach my $lonhost (keys(%servers)) {
 8447:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 8448:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 8449:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 8450:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 8451:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 8452:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 8453:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 8454:                         $changes{'loginvia'}{$lonhost} = 1;
 8455:                     } else {
 8456:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 8457:                         $changes{'loginvia'}{$lonhost} = 1;
 8458:                     }
 8459:                 } else {
 8460:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 8461:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 8462:                         $changes{'loginvia'}{$lonhost} = 1;
 8463:                     }
 8464:                 }
 8465:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 8466:                     foreach my $item (@loginvia_attribs) {
 8467:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 8468:                     }
 8469:                 } else {
 8470:                     foreach my $item (@loginvia_attribs) {
 8471:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 8472:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 8473:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 8474:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 8475:                                 $new = '/';
 8476:                             }
 8477:                         }
 8478:                         if (($item eq 'custompath') && 
 8479:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 8480:                             $new = '';
 8481:                         }
 8482:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 8483:                             $changes{'loginvia'}{$lonhost} = 1;
 8484:                         }
 8485:                         if ($item eq 'exempt') {
 8486:                             $new = &check_exempt_addresses($new);
 8487:                         }
 8488:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 8489:                     }
 8490:                 }
 8491:             } else {
 8492:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 8493:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 8494:                     $changes{'loginvia'}{$lonhost} = 1;
 8495:                     foreach my $item (@loginvia_attribs) {
 8496:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 8497:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 8498:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 8499:                                 $new = '/';
 8500:                             }
 8501:                         }
 8502:                         if (($item eq 'custompath') && 
 8503:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 8504:                             $new = '';
 8505:                         }
 8506:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 8507:                     }
 8508:                 }
 8509:             }
 8510:         }
 8511:     }
 8512: 
 8513:     my $servadm = $r->dir_config('lonAdmEMail');
 8514:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 8515:     if (ref($domconfig{'login'}) eq 'HASH') {
 8516:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 8517:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 8518:                 if ($lang eq 'nolang') {
 8519:                     push(@currlangs,$lang);
 8520:                 } elsif (defined($langchoices{$lang})) {
 8521:                     push(@currlangs,$lang);
 8522:                 } else {
 8523:                     next;
 8524:                 }
 8525:             }
 8526:         }
 8527:     }
 8528:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 8529:     if (@currlangs > 0) {
 8530:         foreach my $lang (@currlangs) {
 8531:             if (grep(/^\Q$lang\E$/,@delurls)) {
 8532:                 $changes{'helpurl'}{$lang} = 1;
 8533:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 8534:                 $changes{'helpurl'}{$lang} = 1;
 8535:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 8536:                 push(@newlangs,$lang);
 8537:             } else {
 8538:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 8539:             }
 8540:         }
 8541:     }
 8542:     unless (grep(/^nolang$/,@currlangs)) {
 8543:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 8544:             $changes{'helpurl'}{'nolang'} = 1;
 8545:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 8546:             push(@newlangs,'nolang');
 8547:         }
 8548:     }
 8549:     if ($env{'form.loginhelpurl_add_lang'}) {
 8550:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 8551:             ($env{'form.loginhelpurl_add_file.filename'})) {
 8552:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 8553:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 8554:         }
 8555:     }
 8556:     if ((@newlangs > 0) || ($addedfile)) {
 8557:         my $error;
 8558:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 8559:         if ($configuserok eq 'ok') {
 8560:             if ($switchserver) {
 8561:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 8562:             } elsif ($author_ok eq 'ok') {
 8563:                 my @allnew = @newlangs;
 8564:                 if ($addedfile ne '') {
 8565:                     push(@allnew,$addedfile);
 8566:                 }
 8567:                 foreach my $lang (@allnew) {
 8568:                     my $formelem = 'loginhelpurl_'.$lang;
 8569:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 8570:                         $formelem = 'loginhelpurl_add_file';
 8571:                     }
 8572:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 8573:                                                                "help/$lang",'','',$newfile{$lang});
 8574:                     if ($result eq 'ok') {
 8575:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 8576:                         $changes{'helpurl'}{$lang} = 1;
 8577:                     } else {
 8578:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 8579:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 8580:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 8581:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 8582:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 8583:                         }
 8584:                     }
 8585:                 }
 8586:             } else {
 8587:                 $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);
 8588:             }
 8589:         } else {
 8590:             $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);
 8591:         }
 8592:         if ($error) {
 8593:             &Apache::lonnet::logthis($error);
 8594:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8595:         }
 8596:     }
 8597: 
 8598:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
 8599:     if (ref($domconfig{'login'}) eq 'HASH') {
 8600:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
 8601:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
 8602:                 if ($domservers{$lonhost}) {
 8603:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 8604:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
 8605:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
 8606:                     }
 8607:                 }
 8608:             }
 8609:         }
 8610:     }
 8611:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
 8612:     foreach my $lonhost (sort(keys(%domservers))) {
 8613:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 8614:             $changes{'headtag'}{$lonhost} = 1;
 8615:         } else {
 8616:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
 8617:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
 8618:             }
 8619:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
 8620:                 push(@newhosts,$lonhost);
 8621:             } elsif ($currheadtagurls{$lonhost}) {
 8622:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
 8623:                 if ($currexempt{$lonhost}) {
 8624:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
 8625:                         $changes{'headtag'}{$lonhost} = 1;
 8626:                     }
 8627:                 } elsif ($possexempt{$lonhost}) {
 8628:                     $changes{'headtag'}{$lonhost} = 1;
 8629:                 }
 8630:                 if ($possexempt{$lonhost}) {
 8631:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 8632:                 }
 8633:             }
 8634:         }
 8635:     }
 8636:     if (@newhosts) {
 8637:         my $error;
 8638:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 8639:         if ($configuserok eq 'ok') {
 8640:             if ($switchserver) {
 8641:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
 8642:             } elsif ($author_ok eq 'ok') {
 8643:                 foreach my $lonhost (@newhosts) {
 8644:                     my $formelem = 'loginheadtag_'.$lonhost;
 8645:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 8646:                                                                           "login/headtag/$lonhost",'','',
 8647:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
 8648:                     if ($result eq 'ok') {
 8649:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
 8650:                         $changes{'headtag'}{$lonhost} = 1;
 8651:                         if ($possexempt{$lonhost}) {
 8652:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 8653:                         }
 8654:                     } else {
 8655:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
 8656:                                            $newheadtagurls{$lonhost},$result);
 8657:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 8658:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
 8659:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
 8660:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
 8661:                         }
 8662:                     }
 8663:                 }
 8664:             } else {
 8665:                 $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);
 8666:             }
 8667:         } else {
 8668:             $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);
 8669:         }
 8670:         if ($error) {
 8671:             &Apache::lonnet::logthis($error);
 8672:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8673:         }
 8674:     }
 8675:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 8676: 
 8677:     my $defaulthelpfile = '/adm/loginproblems.html';
 8678:     my $defaulttext = &mt('Default in use');
 8679: 
 8680:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 8681:                                              $dom);
 8682:     if ($putresult eq 'ok') {
 8683:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 8684:         my %defaultchecked = (
 8685:                     'coursecatalog' => 'on',
 8686:                     'helpdesk'      => 'on',
 8687:                     'adminmail'     => 'off',
 8688:                     'newuser'       => 'off',
 8689:         );
 8690:         if (ref($domconfig{'login'}) eq 'HASH') {
 8691:             foreach my $item (@toggles) {
 8692:                 if ($defaultchecked{$item} eq 'on') { 
 8693:                     if (($domconfig{'login'}{$item} eq '0') &&
 8694:                         ($env{'form.'.$item} eq '1')) {
 8695:                         $changes{$item} = 1;
 8696:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 8697:                               $domconfig{'login'}{$item} eq '1') &&
 8698:                              ($env{'form.'.$item} eq '0')) {
 8699:                         $changes{$item} = 1;
 8700:                     }
 8701:                 } elsif ($defaultchecked{$item} eq 'off') {
 8702:                     if (($domconfig{'login'}{$item} eq '1') &&
 8703:                         ($env{'form.'.$item} eq '0')) {
 8704:                         $changes{$item} = 1;
 8705:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 8706:                               $domconfig{'login'}{$item} eq '0') &&
 8707:                              ($env{'form.'.$item} eq '1')) {
 8708:                         $changes{$item} = 1;
 8709:                     }
 8710:                 }
 8711:             }
 8712:         }
 8713:         if (keys(%changes) > 0 || $colchgtext) {
 8714:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 8715:             if (ref($lastactref) eq 'HASH') {
 8716:                 $lastactref->{'domainconfig'} = 1;
 8717:             }
 8718:             $resulttext = &mt('Changes made:').'<ul>';
 8719:             foreach my $item (sort(keys(%changes))) {
 8720:                 if ($item eq 'loginvia') {
 8721:                     if (ref($changes{$item}) eq 'HASH') {
 8722:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 8723:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 8724:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 8725:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 8726:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 8727:                                     $protocol = 'http' if ($protocol ne 'https');
 8728:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 8729: 
 8730:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 8731:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 8732:                                     } else {
 8733:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 8734:                                     }
 8735:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 8736:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 8737:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 8738:                                     }
 8739:                                     $resulttext .= '</li>';
 8740:                                 } else {
 8741:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 8742:                                 }
 8743:                             } else {
 8744:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 8745:                             }
 8746:                         }
 8747:                         $resulttext .= '</ul></li>';
 8748:                     }
 8749:                 } elsif ($item eq 'helpurl') {
 8750:                     if (ref($changes{$item}) eq 'HASH') {
 8751:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 8752:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 8753:                                 my ($chg,$link);
 8754:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 8755:                                 if ($lang eq 'nolang') {
 8756:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 8757:                                 } else {
 8758:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 8759:                                 }
 8760:                                 $resulttext .= '<li>'.$chg.'</li>';
 8761:                             } else {
 8762:                                 my $chg;
 8763:                                 if ($lang eq 'nolang') {
 8764:                                     $chg = &mt('custom log-in help file for no preferred language');
 8765:                                 } else {
 8766:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 8767:                                 }
 8768:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 8769:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 8770:                                                       '?inhibitmenu=yes',$chg,600,500).
 8771:                                                '</li>';
 8772:                             }
 8773:                         }
 8774:                     }
 8775:                 } elsif ($item eq 'headtag') {
 8776:                     if (ref($changes{$item}) eq 'HASH') {
 8777:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 8778:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 8779:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
 8780:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 8781:                                 $resulttext .= '<li><a href="'.
 8782:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
 8783:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 8784:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
 8785:                                 if ($possexempt{$lonhost}) {
 8786:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
 8787:                                 } else {
 8788:                                     $resulttext .= &mt('included for any client IP');
 8789:                                 }
 8790:                                 $resulttext .= '</li>';
 8791:                             }
 8792:                         }
 8793:                     }
 8794:                 } elsif ($item eq 'captcha') {
 8795:                     if (ref($loginhash{'login'}) eq 'HASH') {
 8796:                         my $chgtxt;
 8797:                         if ($loginhash{'login'}{$item} eq 'notused') {
 8798:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 8799:                         } else {
 8800:                             my %captchas = &captcha_phrases();
 8801:                             if ($captchas{$loginhash{'login'}{$item}}) {
 8802:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 8803:                             } else {
 8804:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 8805:                             }
 8806:                         }
 8807:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 8808:                     }
 8809:                 } elsif ($item eq 'recaptchakeys') {
 8810:                     if (ref($loginhash{'login'}) eq 'HASH') {
 8811:                         my ($privkey,$pubkey);
 8812:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 8813:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 8814:                             $privkey = $loginhash{'login'}{$item}{'private'};
 8815:                         }
 8816:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 8817:                         if (!$pubkey) {
 8818:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 8819:                         } else {
 8820:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 8821:                         }
 8822:                         if (!$privkey) {
 8823:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 8824:                         } else {
 8825:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 8826:                         }
 8827:                         $chgtxt .= '</ul>';
 8828:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 8829:                     }
 8830:                 } elsif ($item eq 'recaptchaversion') {
 8831:                     if (ref($loginhash{'login'}) eq 'HASH') {
 8832:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
 8833:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
 8834:                                            '</li>';
 8835:                         }
 8836:                     }
 8837:                 } else {
 8838:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 8839:                 }
 8840:             }
 8841:             $resulttext .= $colchgtext.'</ul>';
 8842:         } else {
 8843:             $resulttext = &mt('No changes made to log-in page settings');
 8844:         }
 8845:     } else {
 8846:         $resulttext = '<span class="LC_error">'.
 8847: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 8848:     }
 8849:     if ($errors) {
 8850:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 8851:                        $errors.'</ul>';
 8852:     }
 8853:     return $resulttext;
 8854: }
 8855: 
 8856: sub check_exempt_addresses {
 8857:     my ($iplist) = @_;
 8858:     $iplist =~ s/^\s+//;
 8859:     $iplist =~ s/\s+$//;
 8860:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
 8861:     my (@okips,$new);
 8862:     foreach my $ip (@poss_ips) {
 8863:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 8864:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 8865:                 push(@okips,$ip);
 8866:             }
 8867:         }
 8868:     }
 8869:     if (@okips > 0) {
 8870:         $new = join(',',@okips);
 8871:     } else {
 8872:         $new = '';
 8873:     }
 8874:     return $new;
 8875: }
 8876: 
 8877: sub color_font_choices {
 8878:     my %choices =
 8879:         &Apache::lonlocal::texthash (
 8880:             img => "Header",
 8881:             bgs => "Background colors",
 8882:             links => "Link colors",
 8883:             images => "Images",
 8884:             font => "Font color",
 8885:             fontmenu => "Font menu",
 8886:             pgbg => "Page",
 8887:             tabbg => "Header",
 8888:             sidebg => "Border",
 8889:             link => "Link",
 8890:             alink => "Active link",
 8891:             vlink => "Visited link",
 8892:         );
 8893:     return %choices;
 8894: }
 8895: 
 8896: sub modify_rolecolors {
 8897:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 8898:     my ($resulttext,%rolehash);
 8899:     $rolehash{'rolecolors'} = {};
 8900:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 8901:         if ($domconfig{'rolecolors'} eq '') {
 8902:             $domconfig{'rolecolors'} = {};
 8903:         }
 8904:     }
 8905:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 8906:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 8907:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 8908:                                              $dom);
 8909:     if ($putresult eq 'ok') {
 8910:         if (keys(%changes) > 0) {
 8911:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 8912:             if (ref($lastactref) eq 'HASH') {
 8913:                 $lastactref->{'domainconfig'} = 1;
 8914:             }
 8915:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 8916:                                              $rolehash{'rolecolors'});
 8917:         } else {
 8918:             $resulttext = &mt('No changes made to default color schemes');
 8919:         }
 8920:     } else {
 8921:         $resulttext = '<span class="LC_error">'.
 8922: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 8923:     }
 8924:     if ($errors) {
 8925:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 8926:                        $errors.'</ul>';
 8927:     }
 8928:     return $resulttext;
 8929: }
 8930: 
 8931: sub modify_colors {
 8932:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 8933:     my (%changes,%choices);
 8934:     my @bgs;
 8935:     my @links = ('link','alink','vlink');
 8936:     my @logintext;
 8937:     my @images;
 8938:     my $servadm = $r->dir_config('lonAdmEMail');
 8939:     my $errors;
 8940:     my %defaults;
 8941:     foreach my $role (@{$roles}) {
 8942:         if ($role eq 'login') {
 8943:             %choices = &login_choices();
 8944:             @logintext = ('textcol','bgcol');
 8945:         } else {
 8946:             %choices = &color_font_choices();
 8947:         }
 8948:         if ($role eq 'login') {
 8949:             @images = ('img','logo','domlogo','login');
 8950:             @bgs = ('pgbg','mainbg','sidebg');
 8951:         } else {
 8952:             @images = ('img');
 8953:             @bgs = ('pgbg','tabbg','sidebg');
 8954:         }
 8955:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 8956:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 8957:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 8958:         }
 8959:         if ($role eq 'login') {
 8960:             foreach my $item (@logintext) {
 8961:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 8962:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 8963:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 8964:                 }
 8965:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 8966:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 8967:                 }
 8968:             }
 8969:         } else {
 8970:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 8971:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 8972:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 8973:             }
 8974:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 8975:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 8976:             }
 8977:         }
 8978:         foreach my $item (@bgs) {
 8979:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 8980:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 8981:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 8982:             }
 8983:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 8984:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 8985:             }
 8986:         }
 8987:         foreach my $item (@links) {
 8988:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 8989:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 8990:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 8991:             }
 8992:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 8993:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 8994:             }
 8995:         }
 8996:         my ($configuserok,$author_ok,$switchserver) = 
 8997:             &config_check($dom,$confname,$servadm);
 8998:         my ($width,$height) = &thumb_dimensions();
 8999:         if (ref($domconfig->{$role}) ne 'HASH') {
 9000:             $domconfig->{$role} = {};
 9001:         }
 9002:         foreach my $img (@images) {
 9003:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 9004:                 if (defined($env{'form.login_showlogo_'.$img})) {
 9005:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 9006:                 } else { 
 9007:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 9008:                 }
 9009:             } 
 9010: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 9011: 		 && !defined($domconfig->{$role}{$img})
 9012: 		 && !$env{'form.'.$role.'_del_'.$img}
 9013: 		 && $env{'form.'.$role.'_import_'.$img}) {
 9014: 		# import the old configured image from the .tab setting
 9015: 		# if they haven't provided a new one 
 9016: 		$domconfig->{$role}{$img} = 
 9017: 		    $env{'form.'.$role.'_import_'.$img};
 9018: 	    }
 9019:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 9020:                 my $error;
 9021:                 if ($configuserok eq 'ok') {
 9022:                     if ($switchserver) {
 9023:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 9024:                     } else {
 9025:                         if ($author_ok eq 'ok') {
 9026:                             my ($result,$logourl) = 
 9027:                                 &publishlogo($r,'upload',$role.'_'.$img,
 9028:                                            $dom,$confname,$img,$width,$height);
 9029:                             if ($result eq 'ok') {
 9030:                                 $confhash->{$role}{$img} = $logourl;
 9031:                                 $changes{$role}{'images'}{$img} = 1;
 9032:                             } else {
 9033:                                 $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);
 9034:                             }
 9035:                         } else {
 9036:                             $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);
 9037:                         }
 9038:                     }
 9039:                 } else {
 9040:                     $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);
 9041:                 }
 9042:                 if ($error) {
 9043:                     &Apache::lonnet::logthis($error);
 9044:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9045:                 }
 9046:             } elsif ($domconfig->{$role}{$img} ne '') {
 9047:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 9048:                     my $error;
 9049:                     if ($configuserok eq 'ok') {
 9050: # is confname an author?
 9051:                         if ($switchserver eq '') {
 9052:                             if ($author_ok eq 'ok') {
 9053:                                 my ($result,$logourl) = 
 9054:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 9055:                                             $dom,$confname,$img,$width,$height);
 9056:                                 if ($result eq 'ok') {
 9057:                                     $confhash->{$role}{$img} = $logourl;
 9058: 				    $changes{$role}{'images'}{$img} = 1;
 9059:                                 }
 9060:                             }
 9061:                         }
 9062:                     }
 9063:                 }
 9064:             }
 9065:         }
 9066:         if (ref($domconfig) eq 'HASH') {
 9067:             if (ref($domconfig->{$role}) eq 'HASH') {
 9068:                 foreach my $img (@images) {
 9069:                     if ($domconfig->{$role}{$img} ne '') {
 9070:                         if ($env{'form.'.$role.'_del_'.$img}) {
 9071:                             $confhash->{$role}{$img} = '';
 9072:                             $changes{$role}{'images'}{$img} = 1;
 9073:                         } else {
 9074:                             if ($confhash->{$role}{$img} eq '') {
 9075:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 9076:                             }
 9077:                         }
 9078:                     } else {
 9079:                         if ($env{'form.'.$role.'_del_'.$img}) {
 9080:                             $confhash->{$role}{$img} = '';
 9081:                             $changes{$role}{'images'}{$img} = 1;
 9082:                         } 
 9083:                     }
 9084:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 9085:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 9086:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 9087:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 9088:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 9089:                             }
 9090:                         } else {
 9091:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 9092:                                 $changes{$role}{'showlogo'}{$img} = 1;
 9093:                             }
 9094:                         }
 9095:                     }
 9096:                 }
 9097:                 if ($domconfig->{$role}{'font'} ne '') {
 9098:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 9099:                         $changes{$role}{'font'} = 1;
 9100:                     }
 9101:                 } else {
 9102:                     if ($confhash->{$role}{'font'}) {
 9103:                         $changes{$role}{'font'} = 1;
 9104:                     }
 9105:                 }
 9106:                 if ($role ne 'login') {
 9107:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 9108:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 9109:                             $changes{$role}{'fontmenu'} = 1;
 9110:                         }
 9111:                     } else {
 9112:                         if ($confhash->{$role}{'fontmenu'}) {
 9113:                             $changes{$role}{'fontmenu'} = 1;
 9114:                         }
 9115:                     }
 9116:                 }
 9117:                 foreach my $item (@bgs) {
 9118:                     if ($domconfig->{$role}{$item} ne '') {
 9119:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 9120:                             $changes{$role}{'bgs'}{$item} = 1;
 9121:                         } 
 9122:                     } else {
 9123:                         if ($confhash->{$role}{$item}) {
 9124:                             $changes{$role}{'bgs'}{$item} = 1;
 9125:                         }
 9126:                     }
 9127:                 }
 9128:                 foreach my $item (@links) {
 9129:                     if ($domconfig->{$role}{$item} ne '') {
 9130:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 9131:                             $changes{$role}{'links'}{$item} = 1;
 9132:                         }
 9133:                     } else {
 9134:                         if ($confhash->{$role}{$item}) {
 9135:                             $changes{$role}{'links'}{$item} = 1;
 9136:                         }
 9137:                     }
 9138:                 }
 9139:                 foreach my $item (@logintext) {
 9140:                     if ($domconfig->{$role}{$item} ne '') {
 9141:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 9142:                             $changes{$role}{'logintext'}{$item} = 1;
 9143:                         }
 9144:                     } else {
 9145:                         if ($confhash->{$role}{$item}) {
 9146:                             $changes{$role}{'logintext'}{$item} = 1;
 9147:                         }
 9148:                     }
 9149:                 }
 9150:             } else {
 9151:                 &default_change_checker($role,\@images,\@links,\@bgs,
 9152:                                         \@logintext,$confhash,\%changes); 
 9153:             }
 9154:         } else {
 9155:             &default_change_checker($role,\@images,\@links,\@bgs,
 9156:                                     \@logintext,$confhash,\%changes); 
 9157:         }
 9158:     }
 9159:     return ($errors,%changes);
 9160: }
 9161: 
 9162: sub config_check {
 9163:     my ($dom,$confname,$servadm) = @_;
 9164:     my ($configuserok,$author_ok,$switchserver,%currroles);
 9165:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 9166:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 9167:                                                    $confname,$servadm);
 9168:     if ($configuserok eq 'ok') {
 9169:         $switchserver = &check_switchserver($dom,$confname);
 9170:         if ($switchserver eq '') {
 9171:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 9172:         }
 9173:     }
 9174:     return ($configuserok,$author_ok,$switchserver);
 9175: }
 9176: 
 9177: sub default_change_checker {
 9178:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 9179:     foreach my $item (@{$links}) {
 9180:         if ($confhash->{$role}{$item}) {
 9181:             $changes->{$role}{'links'}{$item} = 1;
 9182:         }
 9183:     }
 9184:     foreach my $item (@{$bgs}) {
 9185:         if ($confhash->{$role}{$item}) {
 9186:             $changes->{$role}{'bgs'}{$item} = 1;
 9187:         }
 9188:     }
 9189:     foreach my $item (@{$logintext}) {
 9190:         if ($confhash->{$role}{$item}) {
 9191:             $changes->{$role}{'logintext'}{$item} = 1;
 9192:         }
 9193:     }
 9194:     foreach my $img (@{$images}) {
 9195:         if ($env{'form.'.$role.'_del_'.$img}) {
 9196:             $confhash->{$role}{$img} = '';
 9197:             $changes->{$role}{'images'}{$img} = 1;
 9198:         }
 9199:         if ($role eq 'login') {
 9200:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 9201:                 $changes->{$role}{'showlogo'}{$img} = 1;
 9202:             }
 9203:         }
 9204:     }
 9205:     if ($confhash->{$role}{'font'}) {
 9206:         $changes->{$role}{'font'} = 1;
 9207:     }
 9208: }
 9209: 
 9210: sub display_colorchgs {
 9211:     my ($dom,$changes,$roles,$confhash) = @_;
 9212:     my (%choices,$resulttext);
 9213:     if (!grep(/^login$/,@{$roles})) {
 9214:         $resulttext = &mt('Changes made:').'<br />';
 9215:     }
 9216:     foreach my $role (@{$roles}) {
 9217:         if ($role eq 'login') {
 9218:             %choices = &login_choices();
 9219:         } else {
 9220:             %choices = &color_font_choices();
 9221:         }
 9222:         if (ref($changes->{$role}) eq 'HASH') {
 9223:             if ($role ne 'login') {
 9224:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 9225:             }
 9226:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 9227:                 if ($role ne 'login') {
 9228:                     $resulttext .= '<ul>';
 9229:                 }
 9230:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 9231:                     if ($role ne 'login') {
 9232:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 9233:                     }
 9234:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 9235:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 9236:                             if ($confhash->{$role}{$key}{$item}) {
 9237:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 9238:                             } else {
 9239:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 9240:                             }
 9241:                         } elsif ($confhash->{$role}{$item} eq '') {
 9242:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 9243:                         } else {
 9244:                             my $newitem = $confhash->{$role}{$item};
 9245:                             if ($key eq 'images') {
 9246:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 9247:                             }
 9248:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 9249:                         }
 9250:                     }
 9251:                     if ($role ne 'login') {
 9252:                         $resulttext .= '</ul></li>';
 9253:                     }
 9254:                 } else {
 9255:                     if ($confhash->{$role}{$key} eq '') {
 9256:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 9257:                     } else {
 9258:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 9259:                     }
 9260:                 }
 9261:                 if ($role ne 'login') {
 9262:                     $resulttext .= '</ul>';
 9263:                 }
 9264:             }
 9265:         }
 9266:     }
 9267:     return $resulttext;
 9268: }
 9269: 
 9270: sub thumb_dimensions {
 9271:     return ('200','50');
 9272: }
 9273: 
 9274: sub check_dimensions {
 9275:     my ($inputfile) = @_;
 9276:     my ($fullwidth,$fullheight);
 9277:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 9278:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 9279:             my $imageinfo = <PIPE>;
 9280:             if (!close(PIPE)) {
 9281:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 9282:             }
 9283:             chomp($imageinfo);
 9284:             my ($fullsize) = 
 9285:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 9286:             if ($fullsize) {
 9287:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 9288:             }
 9289:         }
 9290:     }
 9291:     return ($fullwidth,$fullheight);
 9292: }
 9293: 
 9294: sub check_configuser {
 9295:     my ($uhome,$dom,$confname,$servadm) = @_;
 9296:     my ($configuserok,%currroles);
 9297:     if ($uhome eq 'no_host') {
 9298:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 9299:         my $configpass = &LONCAPA::Enrollment::create_password($dom);
 9300:         $configuserok = 
 9301:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 9302:                              $configpass,'','','','','',undef,$servadm);
 9303:     } else {
 9304:         $configuserok = 'ok';
 9305:         %currroles = 
 9306:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 9307:     }
 9308:     return ($configuserok,%currroles);
 9309: }
 9310: 
 9311: sub check_authorstatus {
 9312:     my ($dom,$confname,%currroles) = @_;
 9313:     my $author_ok;
 9314:     if (!$currroles{':'.$dom.':au'}) {
 9315:         my $start = time;
 9316:         my $end = 0;
 9317:         $author_ok = 
 9318:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 9319:                                         'au',$end,$start,'','','domconfig');
 9320:     } else {
 9321:         $author_ok = 'ok';
 9322:     }
 9323:     return $author_ok;
 9324: }
 9325: 
 9326: sub publishlogo {
 9327:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 9328:     my ($output,$fname,$logourl);
 9329:     if ($action eq 'upload') {
 9330:         $fname=$env{'form.'.$formname.'.filename'};
 9331:         chop($env{'form.'.$formname});
 9332:     } else {
 9333:         ($fname) = ($formname =~ /([^\/]+)$/);
 9334:     }
 9335:     if ($savefileas ne '') {
 9336:         $fname = $savefileas;
 9337:     }
 9338:     $fname=&Apache::lonnet::clean_filename($fname);
 9339: # See if there is anything left
 9340:     unless ($fname) { return ('error: no uploaded file'); }
 9341:     $fname="$subdir/$fname";
 9342:     my $docroot=$r->dir_config('lonDocRoot');
 9343:     my $filepath="$docroot/priv";
 9344:     my $relpath = "$dom/$confname";
 9345:     my ($fnamepath,$file,$fetchthumb);
 9346:     $file=$fname;
 9347:     if ($fname=~m|/|) {
 9348:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 9349:     }
 9350:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 9351:     my $count;
 9352:     for ($count=5;$count<=$#parts;$count++) {
 9353:         $filepath.="/$parts[$count]";
 9354:         if ((-e $filepath)!=1) {
 9355:             mkdir($filepath,02770);
 9356:         }
 9357:     }
 9358:     # Check for bad extension and disallow upload
 9359:     if ($file=~/\.(\w+)$/ &&
 9360:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 9361:         $output = 
 9362:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 9363:     } elsif ($file=~/\.(\w+)$/ &&
 9364:         !defined(&Apache::loncommon::fileembstyle($1))) {
 9365:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 9366:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 9367:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 9368:     } elsif (-d "$filepath/$file") {
 9369:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 9370:     } else {
 9371:         my $source = $filepath.'/'.$file;
 9372:         my $logfile;
 9373:         if (!open($logfile,">>",$source.'.log')) {
 9374:             return (&mt('No write permission to Authoring Space'));
 9375:         }
 9376:         print $logfile
 9377: "\n================= Publish ".localtime()." ================\n".
 9378: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 9379: # Save the file
 9380:         if (!open(FH,">",$source)) {
 9381:             &Apache::lonnet::logthis('Failed to create '.$source);
 9382:             return (&mt('Failed to create file'));
 9383:         }
 9384:         if ($action eq 'upload') {
 9385:             if (!print FH ($env{'form.'.$formname})) {
 9386:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 9387:                 return (&mt('Failed to write file'));
 9388:             }
 9389:         } else {
 9390:             my $original = &Apache::lonnet::filelocation('',$formname);
 9391:             if(!copy($original,$source)) {
 9392:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 9393:                 return (&mt('Failed to write file'));
 9394:             }
 9395:         }
 9396:         close(FH);
 9397:         chmod(0660, $source); # Permissions to rw-rw---.
 9398: 
 9399:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 9400:         my $copyfile=$targetdir.'/'.$file;
 9401: 
 9402:         my @parts=split(/\//,$targetdir);
 9403:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 9404:         for (my $count=5;$count<=$#parts;$count++) {
 9405:             $path.="/$parts[$count]";
 9406:             if (!-e $path) {
 9407:                 print $logfile "\nCreating directory ".$path;
 9408:                 mkdir($path,02770);
 9409:             }
 9410:         }
 9411:         my $versionresult;
 9412:         if (-e $copyfile) {
 9413:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 9414:         } else {
 9415:             $versionresult = 'ok';
 9416:         }
 9417:         if ($versionresult eq 'ok') {
 9418:             if (copy($source,$copyfile)) {
 9419:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 9420:                 $output = 'ok';
 9421:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 9422:                 push(@{$modified_urls},[$copyfile,$source]);
 9423:                 my $metaoutput = 
 9424:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 9425:                 unless ($registered_cleanup) {
 9426:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 9427:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 9428:                     $registered_cleanup=1;
 9429:                 }
 9430:             } else {
 9431:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 9432:                 $output = &mt('Failed to copy file to RES space').", $!";
 9433:             }
 9434:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 9435:                 my $inputfile = $filepath.'/'.$file;
 9436:                 my $outfile = $filepath.'/'.'tn-'.$file;
 9437:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 9438:                 if ($fullwidth ne '' && $fullheight ne '') { 
 9439:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 9440:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 9441:                         my @args = ('convert','-sample',$thumbsize,$inputfile,$outfile);
 9442:                         system({$args[0]} @args);
 9443:                         chmod(0660, $filepath.'/tn-'.$file);
 9444:                         if (-e $outfile) {
 9445:                             my $copyfile=$targetdir.'/tn-'.$file;
 9446:                             if (copy($outfile,$copyfile)) {
 9447:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 9448:                                 my $thumb_metaoutput = 
 9449:                                     &write_metadata($dom,$confname,$formname,
 9450:                                                     $targetdir,'tn-'.$file,$logfile);
 9451:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 9452:                                 unless ($registered_cleanup) {
 9453:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 9454:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 9455:                                     $registered_cleanup=1;
 9456:                                 }
 9457:                             } else {
 9458:                                 print $logfile "\nUnable to write ".$copyfile.
 9459:                                                ':'.$!."\n";
 9460:                             }
 9461:                         }
 9462:                     }
 9463:                 }
 9464:             }
 9465:         } else {
 9466:             $output = $versionresult;
 9467:         }
 9468:     }
 9469:     return ($output,$logourl);
 9470: }
 9471: 
 9472: sub logo_versioning {
 9473:     my ($targetdir,$file,$logfile) = @_;
 9474:     my $target = $targetdir.'/'.$file;
 9475:     my ($maxversion,$fn,$extn,$output);
 9476:     $maxversion = 0;
 9477:     if ($file =~ /^(.+)\.(\w+)$/) {
 9478:         $fn=$1;
 9479:         $extn=$2;
 9480:     }
 9481:     opendir(DIR,$targetdir);
 9482:     while (my $filename=readdir(DIR)) {
 9483:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 9484:             $maxversion=($1>$maxversion)?$1:$maxversion;
 9485:         }
 9486:     }
 9487:     $maxversion++;
 9488:     print $logfile "\nCreating old version ".$maxversion."\n";
 9489:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 9490:     if (copy($target,$copyfile)) {
 9491:         print $logfile "Copied old target to ".$copyfile."\n";
 9492:         $copyfile=$copyfile.'.meta';
 9493:         if (copy($target.'.meta',$copyfile)) {
 9494:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 9495:             $output = 'ok';
 9496:         } else {
 9497:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 9498:             $output = &mt('Failed to copy old meta').", $!, ";
 9499:         }
 9500:     } else {
 9501:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 9502:         $output = &mt('Failed to copy old target').", $!, ";
 9503:     }
 9504:     return $output;
 9505: }
 9506: 
 9507: sub write_metadata {
 9508:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 9509:     my (%metadatafields,%metadatakeys,$output);
 9510:     $metadatafields{'title'}=$formname;
 9511:     $metadatafields{'creationdate'}=time;
 9512:     $metadatafields{'lastrevisiondate'}=time;
 9513:     $metadatafields{'copyright'}='public';
 9514:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 9515:                                          $env{'user.domain'};
 9516:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 9517:     $metadatafields{'domain'}=$dom;
 9518:     {
 9519:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 9520:         my $mfh;
 9521:         if (open($mfh,">",$targetdir.'/'.$file.'.meta')) {
 9522:             foreach (sort(keys(%metadatafields))) {
 9523:                 unless ($_=~/\./) {
 9524:                     my $unikey=$_;
 9525:                     $unikey=~/^([A-Za-z]+)/;
 9526:                     my $tag=$1;
 9527:                     $tag=~tr/A-Z/a-z/;
 9528:                     print $mfh "\n\<$tag";
 9529:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 9530:                         my $value=$metadatafields{$unikey.'.'.$_};
 9531:                         $value=~s/\"/\'\'/g;
 9532:                         print $mfh ' '.$_.'="'.$value.'"';
 9533:                     }
 9534:                     print $mfh '>'.
 9535:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 9536:                             .'</'.$tag.'>';
 9537:                 }
 9538:             }
 9539:             $output = 'ok';
 9540:             print $logfile "\nWrote metadata";
 9541:             close($mfh);
 9542:         } else {
 9543:             print $logfile "\nFailed to open metadata file";
 9544:             $output = &mt('Could not write metadata');
 9545:         }
 9546:     }
 9547:     return $output;
 9548: }
 9549: 
 9550: sub notifysubscribed {
 9551:     foreach my $targetsource (@{$modified_urls}){
 9552:         next unless (ref($targetsource) eq 'ARRAY');
 9553:         my ($target,$source)=@{$targetsource};
 9554:         if ($source ne '') {
 9555:             if (open(my $logfh,">>",$source.'.log')) {
 9556:                 print $logfh "\nCleanup phase: Notifications\n";
 9557:                 my @subscribed=&subscribed_hosts($target);
 9558:                 foreach my $subhost (@subscribed) {
 9559:                     print $logfh "\nNotifying host ".$subhost.':';
 9560:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 9561:                     print $logfh $reply;
 9562:                 }
 9563:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 9564:                 foreach my $subhost (@subscribedmeta) {
 9565:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 9566:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 9567:                                                         $subhost);
 9568:                     print $logfh $reply;
 9569:                 }
 9570:                 print $logfh "\n============ Done ============\n";
 9571:                 close($logfh);
 9572:             }
 9573:         }
 9574:     }
 9575:     return OK;
 9576: }
 9577: 
 9578: sub subscribed_hosts {
 9579:     my ($target) = @_;
 9580:     my @subscribed;
 9581:     if (open(my $fh,"<","$target.subscription")) {
 9582:         while (my $subline=<$fh>) {
 9583:             if ($subline =~ /^($match_lonid):/) {
 9584:                 my $host = $1;
 9585:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 9586:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 9587:                         push(@subscribed,$host);
 9588:                     }
 9589:                 }
 9590:             }
 9591:         }
 9592:     }
 9593:     return @subscribed;
 9594: }
 9595: 
 9596: sub check_switchserver {
 9597:     my ($dom,$confname) = @_;
 9598:     my ($allowed,$switchserver);
 9599:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 9600:     if ($home eq 'no_host') {
 9601:         $home = &Apache::lonnet::domain($dom,'primary');
 9602:     }
 9603:     my @ids=&Apache::lonnet::current_machine_ids();
 9604:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 9605:     if (!$allowed) {
 9606: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 9607:     }
 9608:     return $switchserver;
 9609: }
 9610: 
 9611: sub modify_quotas {
 9612:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 9613:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 9614:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 9615:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 9616:         $validationfieldsref);
 9617:     if ($action eq 'quotas') {
 9618:         $context = 'tools'; 
 9619:     } else {
 9620:         $context = $action;
 9621:     }
 9622:     if ($context eq 'requestcourses') {
 9623:         @usertools = ('official','unofficial','community','textbook');
 9624:         @options =('norequest','approval','validate','autolimit');
 9625:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 9626:         %titles = &courserequest_titles();
 9627:         $toolregexp = join('|',@usertools);
 9628:         %conditions = &courserequest_conditions();
 9629:         $confname = $dom.'-domainconfig';
 9630:         my $servadm = $r->dir_config('lonAdmEMail');
 9631:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 9632:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 9633:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 9634:     } elsif ($context eq 'requestauthor') {
 9635:         @usertools = ('author');
 9636:         %titles = &authorrequest_titles();
 9637:     } else {
 9638:         @usertools = ('aboutme','blog','webdav','portfolio');
 9639:         %titles = &tool_titles();
 9640:     }
 9641:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9642:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9643:     foreach my $key (keys(%env)) {
 9644:         if ($context eq 'requestcourses') {
 9645:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 9646:                 my $item = $1;
 9647:                 my $type = $2;
 9648:                 if ($type =~ /^limit_(.+)/) {
 9649:                     $limithash{$item}{$1} = $env{$key};
 9650:                 } else {
 9651:                     $confhash{$item}{$type} = $env{$key};
 9652:                 }
 9653:             }
 9654:         } elsif ($context eq 'requestauthor') {
 9655:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 9656:                 $confhash{$1} = $env{$key};
 9657:             }
 9658:         } else {
 9659:             if ($key =~ /^form\.quota_(.+)$/) {
 9660:                 $confhash{'defaultquota'}{$1} = $env{$key};
 9661:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 9662:                 $confhash{'authorquota'}{$1} = $env{$key};
 9663:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 9664:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 9665:             }
 9666:         }
 9667:     }
 9668:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 9669:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 9670:         @approvalnotify = sort(@approvalnotify);
 9671:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 9672:         my @crstypes = ('official','unofficial','community','textbook');
 9673:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 9674:         foreach my $type (@hasuniquecode) {
 9675:             if (grep(/^\Q$type\E$/,@crstypes)) {
 9676:                 $confhash{'uniquecode'}{$type} = 1;
 9677:             }
 9678:         }
 9679:         my (%newbook,%allpos);
 9680:         if ($context eq 'requestcourses') {
 9681:             foreach my $type ('textbooks','templates') {
 9682:                 @{$allpos{$type}} = (); 
 9683:                 my $invalid;
 9684:                 if ($type eq 'textbooks') {
 9685:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
 9686:                 } else {
 9687:                     $invalid = &mt('Invalid LON-CAPA course for template');
 9688:                 }
 9689:                 if ($env{'form.'.$type.'_addbook'}) {
 9690:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
 9691:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
 9692:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
 9693:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
 9694:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 9695:                         } else {
 9696:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
 9697:                             my $position = $env{'form.'.$type.'_addbook_pos'};
 9698:                             $position =~ s/\D+//g;
 9699:                             if ($position ne '') {
 9700:                                 $allpos{$type}[$position] = $newbook{$type};
 9701:                             }
 9702:                         }
 9703:                     } else {
 9704:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 9705:                     }
 9706:                 }
 9707:             } 
 9708:         }
 9709:         if (ref($domconfig{$action}) eq 'HASH') {
 9710:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 9711:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 9712:                     $changes{'notify'}{'approval'} = 1;
 9713:                 }
 9714:             } else {
 9715:                 if ($confhash{'notify'}{'approval'}) {
 9716:                     $changes{'notify'}{'approval'} = 1;
 9717:                 }
 9718:             }
 9719:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 9720:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 9721:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 9722:                         unless ($confhash{'uniquecode'}{$crstype}) {
 9723:                             $changes{'uniquecode'} = 1;
 9724:                         }
 9725:                     }
 9726:                     unless ($changes{'uniquecode'}) {
 9727:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 9728:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 9729:                                 $changes{'uniquecode'} = 1;
 9730:                             }
 9731:                         }
 9732:                     }
 9733:                } else {
 9734:                    $changes{'uniquecode'} = 1;
 9735:                }
 9736:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 9737:                 $changes{'uniquecode'} = 1;
 9738:             }
 9739:             if ($context eq 'requestcourses') {
 9740:                 foreach my $type ('textbooks','templates') {
 9741:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
 9742:                         my %deletions;
 9743:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
 9744:                         if (@todelete) {
 9745:                             map { $deletions{$_} = 1; } @todelete;
 9746:                         }
 9747:                         my %imgdeletions;
 9748:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
 9749:                         if (@todeleteimages) {
 9750:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
 9751:                         }
 9752:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
 9753:                         for (my $i=0; $i<=$maxnum; $i++) {
 9754:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
 9755:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
 9756:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
 9757:                                 if ($deletions{$key}) {
 9758:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
 9759:                                         #FIXME need to obsolete item in RES space
 9760:                                     }
 9761:                                     next;
 9762:                                 } else {
 9763:                                     my $newpos = $env{'form.'.$itemid};
 9764:                                     $newpos =~ s/\D+//g;
 9765:                                     foreach my $item ('subject','title','publisher','author') {
 9766:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 9767:                                                  ($type eq 'templates'));
 9768:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
 9769:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
 9770:                                             $changes{$type}{$key} = 1;
 9771:                                         }
 9772:                                     }
 9773:                                     $allpos{$type}[$newpos] = $key;
 9774:                                 }
 9775:                                 if ($imgdeletions{$key}) {
 9776:                                     $changes{$type}{$key} = 1;
 9777:                                     #FIXME need to obsolete item in RES space
 9778:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
 9779:                                     my ($cdom,$cnum) = split(/_/,$key);
 9780:                                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
 9781:                                         $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
 9782:                                     } else {
 9783:                                         my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
 9784:                                                                                       $cdom,$cnum,$type,$configuserok,
 9785:                                                                                       $switchserver,$author_ok);
 9786:                                         if ($imgurl) {
 9787:                                             $confhash{$type}{$key}{'image'} = $imgurl;
 9788:                                             $changes{$type}{$key} = 1; 
 9789:                                         }
 9790:                                         if ($error) {
 9791:                                             &Apache::lonnet::logthis($error);
 9792:                                             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9793:                                         }
 9794:                                     } 
 9795:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
 9796:                                     $confhash{$type}{$key}{'image'} = 
 9797:                                         $domconfig{$action}{$type}{$key}{'image'};
 9798:                                 }
 9799:                             }
 9800:                         }
 9801:                     }
 9802:                 }
 9803:             }
 9804:         } else {
 9805:             if ($confhash{'notify'}{'approval'}) {
 9806:                 $changes{'notify'}{'approval'} = 1;
 9807:             }
 9808:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 9809:                 $changes{'uniquecode'} = 1;
 9810:             }
 9811:         }
 9812:         if ($context eq 'requestcourses') {
 9813:             foreach my $type ('textbooks','templates') {
 9814:                 if ($newbook{$type}) {
 9815:                     $changes{$type}{$newbook{$type}} = 1;
 9816:                     foreach my $item ('subject','title','publisher','author') {
 9817:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 9818:                                  ($type eq 'template'));
 9819:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
 9820:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
 9821:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
 9822:                         }
 9823:                     }
 9824:                     if ($type eq 'textbooks') {
 9825:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
 9826:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
 9827:                             if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
 9828:                                 $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
 9829:                             } else {
 9830:                                 my ($imageurl,$error) =
 9831:                                     &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
 9832:                                                             $configuserok,$switchserver,$author_ok);
 9833:                                 if ($imageurl) {
 9834:                                     $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
 9835:                                 }
 9836:                                 if ($error) {
 9837:                                     &Apache::lonnet::logthis($error);
 9838:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9839:                                 }
 9840:                             }
 9841:                         }
 9842:                     }
 9843:                 }
 9844:                 if (@{$allpos{$type}} > 0) {
 9845:                     my $idx = 0;
 9846:                     foreach my $item (@{$allpos{$type}}) {
 9847:                         if ($item ne '') {
 9848:                             $confhash{$type}{$item}{'order'} = $idx;
 9849:                             if (ref($domconfig{$action}) eq 'HASH') {
 9850:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
 9851:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
 9852:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
 9853:                                             $changes{$type}{$item} = 1;
 9854:                                         }
 9855:                                     }
 9856:                                 }
 9857:                             }
 9858:                             $idx ++;
 9859:                         }
 9860:                     }
 9861:                 }
 9862:             }
 9863:             if (ref($validationitemsref) eq 'ARRAY') {
 9864:                 foreach my $item (@{$validationitemsref}) {
 9865:                     if ($item eq 'fields') {
 9866:                         my @changed;
 9867:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 9868:                         if (@{$confhash{'validation'}{$item}} > 0) {
 9869:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 9870:                         }
 9871:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9872:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9873:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 9874:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 9875:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
 9876:                                 } else {
 9877:                                     @changed = @{$confhash{'validation'}{$item}};
 9878:                                 }
 9879:                             } else {
 9880:                                 @changed = @{$confhash{'validation'}{$item}};
 9881:                             }
 9882:                         } else {
 9883:                             @changed = @{$confhash{'validation'}{$item}};
 9884:                         }
 9885:                         if (@changed) {
 9886:                             if ($confhash{'validation'}{$item}) {
 9887:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 9888:                             } else {
 9889:                                 $changes{'validation'}{$item} = &mt('None');
 9890:                             }
 9891:                         }
 9892:                     } else {
 9893:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 9894:                         if ($item eq 'markup') {
 9895:                             if ($env{'form.requestcourses_validation_'.$item}) {
 9896:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 9897:                             }
 9898:                         }
 9899:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9900:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9901:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 9902:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 9903:                                 }
 9904:                             } else {
 9905:                                 if ($confhash{'validation'}{$item} ne '') {
 9906:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 9907:                                 }
 9908:                             }
 9909:                         } else {
 9910:                             if ($confhash{'validation'}{$item} ne '') {
 9911:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 9912:                             }
 9913:                         }
 9914:                     }
 9915:                 }
 9916:             }
 9917:             if ($env{'form.validationdc'}) {
 9918:                 my $newval = $env{'form.validationdc'};
 9919:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
 9920:                 if (exists($domcoords{$newval})) {
 9921:                     $confhash{'validation'}{'dc'} = $newval;
 9922:                 }
 9923:             }
 9924:             if (ref($confhash{'validation'}) eq 'HASH') {
 9925:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9926:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9927:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 9928:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 9929:                                 if ($confhash{'validation'}{'dc'} eq '') {
 9930:                                     $changes{'validation'}{'dc'} = &mt('None');
 9931:                                 } else {
 9932:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9933:                                 }
 9934:                             }
 9935:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
 9936:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9937:                         }
 9938:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 9939:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9940:                     }
 9941:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 9942:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9943:                 }
 9944:             } else {
 9945:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9946:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9947:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 9948:                             $changes{'validation'}{'dc'} = &mt('None');
 9949:                         }
 9950:                     }
 9951:                 }
 9952:             }
 9953:         }
 9954:     } else {
 9955:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 9956:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 9957:     }
 9958:     foreach my $item (@usertools) {
 9959:         foreach my $type (@{$types},'default','_LC_adv') {
 9960:             my $unset; 
 9961:             if ($context eq 'requestcourses') {
 9962:                 $unset = '0';
 9963:                 if ($type eq '_LC_adv') {
 9964:                     $unset = '';
 9965:                 }
 9966:                 if ($confhash{$item}{$type} eq 'autolimit') {
 9967:                     $confhash{$item}{$type} .= '=';
 9968:                     unless ($limithash{$item}{$type} =~ /\D/) {
 9969:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 9970:                     }
 9971:                 }
 9972:             } elsif ($context eq 'requestauthor') {
 9973:                 $unset = '0';
 9974:                 if ($type eq '_LC_adv') {
 9975:                     $unset = '';
 9976:                 }
 9977:             } else {
 9978:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 9979:                     $confhash{$item}{$type} = 1;
 9980:                 } else {
 9981:                     $confhash{$item}{$type} = 0;
 9982:                 }
 9983:             }
 9984:             if (ref($domconfig{$action}) eq 'HASH') {
 9985:                 if ($action eq 'requestauthor') {
 9986:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 9987:                         $changes{$type} = 1;
 9988:                     }
 9989:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 9990:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 9991:                         $changes{$item}{$type} = 1;
 9992:                     }
 9993:                 } else {
 9994:                     if ($context eq 'requestcourses') {
 9995:                         if ($confhash{$item}{$type} ne $unset) {
 9996:                             $changes{$item}{$type} = 1;
 9997:                         }
 9998:                     } else {
 9999:                         if (!$confhash{$item}{$type}) {
10000:                             $changes{$item}{$type} = 1;
10001:                         }
10002:                     }
10003:                 }
10004:             } else {
10005:                 if ($context eq 'requestcourses') {
10006:                     if ($confhash{$item}{$type} ne $unset) {
10007:                         $changes{$item}{$type} = 1;
10008:                     }
10009:                 } elsif ($context eq 'requestauthor') {
10010:                     if ($confhash{$type} ne $unset) {
10011:                         $changes{$type} = 1;
10012:                     }
10013:                 } else {
10014:                     if (!$confhash{$item}{$type}) {
10015:                         $changes{$item}{$type} = 1;
10016:                     }
10017:                 }
10018:             }
10019:         }
10020:     }
10021:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
10022:         if (ref($domconfig{'quotas'}) eq 'HASH') {
10023:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
10024:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
10025:                     if (exists($confhash{'defaultquota'}{$key})) {
10026:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
10027:                             $changes{'defaultquota'}{$key} = 1;
10028:                         }
10029:                     } else {
10030:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
10031:                     }
10032:                 }
10033:             } else {
10034:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
10035:                     if (exists($confhash{'defaultquota'}{$key})) {
10036:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
10037:                             $changes{'defaultquota'}{$key} = 1;
10038:                         }
10039:                     } else {
10040:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
10041:                     }
10042:                 }
10043:             }
10044:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
10045:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
10046:                     if (exists($confhash{'authorquota'}{$key})) {
10047:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
10048:                             $changes{'authorquota'}{$key} = 1;
10049:                         }
10050:                     } else {
10051:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
10052:                     }
10053:                 }
10054:             }
10055:         }
10056:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
10057:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
10058:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
10059:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
10060:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
10061:                             $changes{'defaultquota'}{$key} = 1;
10062:                         }
10063:                     } else {
10064:                         if (!exists($domconfig{'quotas'}{$key})) {
10065:                             $changes{'defaultquota'}{$key} = 1;
10066:                         }
10067:                     }
10068:                 } else {
10069:                     $changes{'defaultquota'}{$key} = 1;
10070:                 }
10071:             }
10072:         }
10073:         if (ref($confhash{'authorquota'}) eq 'HASH') {
10074:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
10075:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
10076:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
10077:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
10078:                             $changes{'authorquota'}{$key} = 1;
10079:                         }
10080:                     } else {
10081:                         $changes{'authorquota'}{$key} = 1;
10082:                     }
10083:                 } else {
10084:                     $changes{'authorquota'}{$key} = 1;
10085:                 }
10086:             }
10087:         }
10088:     }
10089: 
10090:     if ($context eq 'requestauthor') {
10091:         $domdefaults{'requestauthor'} = \%confhash;
10092:     } else {
10093:         foreach my $key (keys(%confhash)) {
10094:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
10095:                 $domdefaults{$key} = $confhash{$key};
10096:             }
10097:         }
10098:     }
10099: 
10100:     my %quotahash = (
10101:                       $action => { %confhash }
10102:                     );
10103:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
10104:                                              $dom);
10105:     if ($putresult eq 'ok') {
10106:         if (keys(%changes) > 0) {
10107:             my $cachetime = 24*60*60;
10108:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10109:             if (ref($lastactref) eq 'HASH') {
10110:                 $lastactref->{'domdefaults'} = 1;
10111:             }
10112:             $resulttext = &mt('Changes made:').'<ul>';
10113:             unless (($context eq 'requestcourses') ||
10114:                     ($context eq 'requestauthor')) {
10115:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
10116:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
10117:                     foreach my $type (@{$types},'default') {
10118:                         if (defined($changes{'defaultquota'}{$type})) {
10119:                             my $typetitle = $usertypes->{$type};
10120:                             if ($type eq 'default') {
10121:                                 $typetitle = $othertitle;
10122:                             }
10123:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
10124:                         }
10125:                     }
10126:                     $resulttext .= '</ul></li>';
10127:                 }
10128:                 if (ref($changes{'authorquota'}) eq 'HASH') {
10129:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
10130:                     foreach my $type (@{$types},'default') {
10131:                         if (defined($changes{'authorquota'}{$type})) {
10132:                             my $typetitle = $usertypes->{$type};
10133:                             if ($type eq 'default') {
10134:                                 $typetitle = $othertitle;
10135:                             }
10136:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
10137:                         }
10138:                     }
10139:                     $resulttext .= '</ul></li>';
10140:                 }
10141:             }
10142:             my %newenv;
10143:             foreach my $item (@usertools) {
10144:                 my (%haschgs,%inconf);
10145:                 if ($context eq 'requestauthor') {
10146:                     %haschgs = %changes;
10147:                     %inconf = %confhash;
10148:                 } else {
10149:                     if (ref($changes{$item}) eq 'HASH') {
10150:                         %haschgs = %{$changes{$item}};
10151:                     }
10152:                     if (ref($confhash{$item}) eq 'HASH') {
10153:                         %inconf = %{$confhash{$item}};
10154:                     }
10155:                 }
10156:                 if (keys(%haschgs) > 0) {
10157:                     my $newacc = 
10158:                         &Apache::lonnet::usertools_access($env{'user.name'},
10159:                                                           $env{'user.domain'},
10160:                                                           $item,'reload',$context);
10161:                     if (($context eq 'requestcourses') ||
10162:                         ($context eq 'requestauthor')) {
10163:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
10164:                             $newenv{'environment.canrequest.'.$item} = $newacc;
10165:                         }
10166:                     } else {
10167:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
10168:                             $newenv{'environment.availabletools.'.$item} = $newacc;
10169:                         }
10170:                     }
10171:                     unless ($context eq 'requestauthor') {
10172:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
10173:                     }
10174:                     foreach my $type (@{$types},'default','_LC_adv') {
10175:                         if ($haschgs{$type}) {
10176:                             my $typetitle = $usertypes->{$type};
10177:                             if ($type eq 'default') {
10178:                                 $typetitle = $othertitle;
10179:                             } elsif ($type eq '_LC_adv') {
10180:                                 $typetitle = 'LON-CAPA Advanced Users'; 
10181:                             }
10182:                             if ($inconf{$type}) {
10183:                                 if ($context eq 'requestcourses') {
10184:                                     my $cond;
10185:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
10186:                                         if ($1 eq '') {
10187:                                             $cond = &mt('(Automatic processing of any request).');
10188:                                         } else {
10189:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
10190:                                         }
10191:                                     } else { 
10192:                                         $cond = $conditions{$inconf{$type}};
10193:                                     }
10194:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
10195:                                 } elsif ($context eq 'requestauthor') {
10196:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
10197:                                                              $titles{$inconf{$type}},$typetitle);
10198: 
10199:                                 } else {
10200:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
10201:                                 }
10202:                             } else {
10203:                                 if ($type eq '_LC_adv') {
10204:                                     if ($inconf{$type} eq '0') {
10205:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
10206:                                     } else { 
10207:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
10208:                                     }
10209:                                 } else {
10210:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
10211:                                 }
10212:                             }
10213:                         }
10214:                     }
10215:                     unless ($context eq 'requestauthor') {
10216:                         $resulttext .= '</ul></li>';
10217:                     }
10218:                 }
10219:             }
10220:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
10221:                 if (ref($changes{'notify'}) eq 'HASH') {
10222:                     if ($changes{'notify'}{'approval'}) {
10223:                         if (ref($confhash{'notify'}) eq 'HASH') {
10224:                             if ($confhash{'notify'}{'approval'}) {
10225:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
10226:                             } else {
10227:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
10228:                             }
10229:                         }
10230:                     }
10231:                 }
10232:             }
10233:             if ($action eq 'requestcourses') {
10234:                 my @offon = ('off','on');
10235:                 if ($changes{'uniquecode'}) {
10236:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
10237:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
10238:                         $resulttext .= '<li>'.
10239:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
10240:                                        '</li>';
10241:                     } else {
10242:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
10243:                                        '</li>';
10244:                     }
10245:                 }
10246:                 foreach my $type ('textbooks','templates') {
10247:                     if (ref($changes{$type}) eq 'HASH') {
10248:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
10249:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
10250:                             my %coursehash = &Apache::lonnet::coursedescription($key);
10251:                             my $coursetitle = $coursehash{'description'};
10252:                             my $position = $confhash{$type}{$key}{'order'} + 1;
10253:                             $resulttext .= '<li>';
10254:                             foreach my $item ('subject','title','publisher','author') {
10255:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
10256:                                          ($type eq 'templates'));
10257:                                 my $name = $item.':';
10258:                                 $name =~ s/^(\w)/\U$1/;
10259:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
10260:                             }
10261:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
10262:                             if ($type eq 'textbooks') {
10263:                                 if ($confhash{$type}{$key}{'image'}) {
10264:                                     $resulttext .= ' '.&mt('Image: [_1]',
10265:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
10266:                                                    ' alt="Textbook cover" />').'<br />';
10267:                                 }
10268:                             }
10269:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
10270:                         }
10271:                         $resulttext .= '</ul></li>';
10272:                     }
10273:                 }
10274:                 if (ref($changes{'validation'}) eq 'HASH') {
10275:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
10276:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
10277:                         foreach my $item (@{$validationitemsref}) {
10278:                             if (exists($changes{'validation'}{$item})) {
10279:                                 if ($item eq 'markup') {
10280:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
10281:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
10282:                                 } else {
10283:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
10284:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
10285:                                 }
10286:                             }
10287:                         }
10288:                         if (exists($changes{'validation'}{'dc'})) {
10289:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
10290:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
10291:                         }
10292:                     }
10293:                 }
10294:             }
10295:             $resulttext .= '</ul>';
10296:             if (keys(%newenv)) {
10297:                 &Apache::lonnet::appenv(\%newenv);
10298:             }
10299:         } else {
10300:             if ($context eq 'requestcourses') {
10301:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
10302:             } elsif ($context eq 'requestauthor') {
10303:                 $resulttext = &mt('No changes made to rights to request author space.');
10304:             } else {
10305:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
10306:             }
10307:         }
10308:     } else {
10309:         $resulttext = '<span class="LC_error">'.
10310: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
10311:     }
10312:     if ($errors) {
10313:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
10314:                        '<ul>'.$errors.'</ul></p>';
10315:     }
10316:     return $resulttext;
10317: }
10318: 
10319: sub process_textbook_image {
10320:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
10321:     my $filename = $env{'form.'.$caller.'.filename'};
10322:     my ($error,$url);
10323:     my ($width,$height) = (50,50);
10324:     if ($configuserok eq 'ok') {
10325:         if ($switchserver) {
10326:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
10327:                          $switchserver);
10328:         } elsif ($author_ok eq 'ok') {
10329:             my ($result,$imageurl) =
10330:                 &publishlogo($r,'upload',$caller,$dom,$confname,
10331:                              "$type/$cdom/$cnum/cover",$width,$height);
10332:             if ($result eq 'ok') {
10333:                 $url = $imageurl;
10334:             } else {
10335:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
10336:             }
10337:         } else {
10338:             $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);
10339:         }
10340:     } else {
10341:         $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);
10342:     }
10343:     return ($url,$error);
10344: }
10345: 
10346: sub modify_ltitools {
10347:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
10348:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10349:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
10350:     my $confname = $dom.'-domainconfig';
10351:     my $servadm = $r->dir_config('lonAdmEMail');
10352:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
10353:     my (%posslti,%possfield);
10354:     my @courseroles = ('cc','in','ta','ep','st');
10355:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
10356:     map { $posslti{$_} = 1; } @ltiroles;
10357:     my @allfields = ('fullname','firstname','lastname','email','user','roles');
10358:     map { $possfield{$_} = 1; } @allfields;
10359:     my %lt = &ltitools_names();
10360:     if ($env{'form.ltitools_add'}) {
10361:         my $title = $env{'form.ltitools_add_title'};
10362:         $title =~ s/(`)/'/g;
10363:         ($newid,my $error) = &get_ltitools_id($dom,$title);
10364:         if ($newid) {
10365:             my $position = $env{'form.ltitools_add_pos'};
10366:             $position =~ s/\D+//g;
10367:             if ($position ne '') {
10368:                 $allpos[$position] = $newid;
10369:             }
10370:             $changes{$newid} = 1;
10371:             foreach my $item ('title','url','key','secret') {
10372:                 $env{'form.ltitools_add_'.$item} =~ s/(`)/'/g;
10373:                 if ($env{'form.ltitools_add_'.$item}) {
10374:                     if (($item eq 'key') || ($item eq 'secret')) {
10375:                         $encconfig{$newid}{$item} = $env{'form.ltitools_add_'.$item};
10376:                     } else {
10377:                         $confhash{$newid}{$item} = $env{'form.ltitools_add_'.$item};
10378:                     }
10379:                 }
10380:             }
10381:             if ($env{'form.ltitools_add_version'} eq 'LTI-1p0') {
10382:                 $confhash{$newid}{'version'} = $env{'form.ltitools_add_version'};
10383:             }
10384:             if ($env{'form.ltitools_add_msgtype'} eq 'basic-lti-launch-request') {
10385:                 $confhash{$newid}{'msgtype'} = $env{'form.ltitools_add_msgtype'};
10386:             }
10387:             foreach my $item ('width','height','linktext','explanation') {
10388:                 $env{'form.ltitools_add_'.$item} =~ s/^\s+//;
10389:                 $env{'form.ltitools_add_'.$item} =~ s/\s+$//;
10390:                 if (($item eq 'width') || ($item eq 'height')) {
10391:                     if ($env{'form.ltitools_add_'.$item} =~ /^\d+$/) {
10392:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
10393:                     }
10394:                 } else {
10395:                     if ($env{'form.ltitools_add_'.$item} ne '') {
10396:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
10397:                     }
10398:                 }
10399:             }
10400:             if ($env{'form.ltitools_add_target'} eq 'window') {
10401:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
10402:             } elsif ($env{'form.ltitools_add_target'} eq 'tab') {
10403:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
10404:             } else {
10405:                 $confhash{$newid}{'display'}{'target'} = 'iframe';
10406:             }
10407:             foreach my $item ('passback','roster') {
10408:                 if ($env{'form.ltitools_add_'.$item}) {
10409:                     $confhash{$newid}{$item} = 1;
10410:                 }
10411:             }
10412:             if ($env{'form.ltitools_add_image.filename'} ne '') {
10413:                 my ($imageurl,$error) =
10414:                     &process_ltitools_image($r,$dom,$confname,'ltitools_add_image',$newid,
10415:                                             $configuserok,$switchserver,$author_ok);
10416:                 if ($imageurl) {
10417:                     $confhash{$newid}{'image'} = $imageurl;
10418:                 }
10419:                 if ($error) {
10420:                     &Apache::lonnet::logthis($error);
10421:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10422:                 }
10423:             }
10424:             my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_add_fields');
10425:             foreach my $field (@fields) {
10426:                 if ($possfield{$field}) {
10427:                     if ($field eq 'roles') {
10428:                         foreach my $role (@courseroles) {
10429:                             my $choice = $env{'form.ltitools_add_roles_'.$role};
10430:                             if (($choice ne '') && ($posslti{$choice})) {
10431:                                 $confhash{$newid}{'roles'}{$role} = $choice;
10432:                                 if ($role eq 'cc') {
10433:                                     $confhash{$newid}{'roles'}{'co'} = $choice;
10434:                                 }
10435:                             }
10436:                         }
10437:                     } else {
10438:                         $confhash{$newid}{'fields'}{$field} = 1;
10439:                     }
10440:                 }
10441:             }
10442:             my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig');
10443:             foreach my $item (@courseconfig) {
10444:                 $confhash{$newid}{'crsconf'}{$item} = 1;
10445:             }
10446:             if ($env{'form.ltitools_add_custom'}) {
10447:                 my $name = $env{'form.ltitools_add_custom_name'};
10448:                 my $value = $env{'form.ltitools_add_custom_value'};
10449:                 $value =~ s/(`)/'/g;
10450:                 $name =~ s/(`)/'/g;
10451:                 $confhash{$newid}{'custom'}{$name} = $value;
10452:             }
10453:         } else {
10454:             my $error = &mt('Failed to acquire unique ID for new external tool');
10455:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10456:         }
10457:     }
10458:     if (ref($domconfig{$action}) eq 'HASH') {
10459:         my %deletions;
10460:         my @todelete = &Apache::loncommon::get_env_multiple('form.ltitools_del');
10461:         if (@todelete) {
10462:             map { $deletions{$_} = 1; } @todelete;
10463:         }
10464:         my %customadds;
10465:         my @newcustom = &Apache::loncommon::get_env_multiple('form.ltitools_customadd');
10466:         if (@newcustom) {
10467:             map { $customadds{$_} = 1; } @newcustom;
10468:         }
10469:         my %imgdeletions;
10470:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.ltitools_image_del');
10471:         if (@todeleteimages) {
10472:             map { $imgdeletions{$_} = 1; } @todeleteimages;
10473:         }
10474:         my $maxnum = $env{'form.ltitools_maxnum'};
10475:         for (my $i=0; $i<=$maxnum; $i++) {
10476:             my $itemid = $env{'form.ltitools_id_'.$i};
10477:             $itemid =~ s/\D+//g;
10478:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
10479:                 if ($deletions{$itemid}) {
10480:                     if ($domconfig{$action}{$itemid}{'image'}) {
10481:                         #FIXME need to obsolete item in RES space
10482:                     }
10483:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'title'};
10484:                     next;
10485:                 } else {
10486:                     my $newpos = $env{'form.ltitools_'.$itemid};
10487:                     $newpos =~ s/\D+//g;
10488:                     foreach my $item ('title','url') {
10489:                         $confhash{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
10490:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
10491:                             $changes{$itemid} = 1;
10492:                         }
10493:                     }
10494:                     foreach my $item ('key','secret') {
10495:                         $encconfig{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
10496:                         if ($domconfig{$action}{$itemid}{$item} ne $encconfig{$itemid}{$item}) {
10497:                             $changes{$itemid} = 1;
10498:                         }
10499:                     }
10500:                     if ($env{'form.ltitools_version_'.$i} eq 'LTI-1p0') {
10501:                         $confhash{$itemid}{'version'} = $env{'form.ltitools_version_'.$i};
10502:                     }
10503:                     if ($env{'form.ltitools_msgtype_'.$i} eq 'basic-lti-launch-request') {
10504:                         $confhash{$itemid}{'msgtype'} = $env{'form.ltitools_msgtype_'.$i};
10505:                     }
10506:                     foreach my $size ('width','height') {
10507:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/^\s+//;
10508:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/\s+$//;
10509:                         if ($env{'form.ltitools_'.$size.'_'.$i} =~ /^\d+$/) {
10510:                             $confhash{$itemid}{'display'}{$size} = $env{'form.ltitools_'.$size.'_'.$i};
10511:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10512:                                 if ($domconfig{$action}{$itemid}{'display'}{$size} ne $confhash{$itemid}{'display'}{$size}) {
10513:                                     $changes{$itemid} = 1;
10514:                                 }
10515:                             } else {
10516:                                 $changes{$itemid} = 1;
10517:                             }
10518:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10519:                             if ($domconfig{$action}{$itemid}{'display'}{$size} ne '') {
10520:                                 $changes{$itemid} = 1;
10521:                             }
10522:                         }
10523:                     }
10524:                     foreach my $item ('linktext','explanation') {
10525:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/^\s+//;
10526:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/\s+$//;
10527:                         if ($env{'form.ltitools_'.$item.'_'.$i} ne '') {
10528:                             $confhash{$itemid}{'display'}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
10529:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10530:                                 if ($domconfig{$action}{$itemid}{'display'}{$item} ne $confhash{$itemid}{'display'}{$item}) {
10531:                                     $changes{$itemid} = 1;
10532:                                 }
10533:                             } else {
10534:                                 $changes{$itemid} = 1;
10535:                             }
10536:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10537:                             if ($domconfig{$action}{$itemid}{'display'}{$item} ne '') {
10538:                                 $changes{$itemid} = 1;
10539:                             }
10540:                         }
10541:                     }
10542:                     if ($env{'form.ltitools_target_'.$i} eq 'window') {
10543:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
10544:                     } elsif ($env{'form.ltitools_target_'.$i} eq 'tab') {
10545:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
10546:                     } else {
10547:                         $confhash{$itemid}{'display'}{'target'} = 'iframe';
10548:                     }
10549:                     if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10550:                         if ($domconfig{$action}{$itemid}{'display'}{'target'} ne $confhash{$itemid}{'display'}{'target'}) {
10551:                             $changes{$itemid} = 1;
10552:                         }
10553:                     } else {
10554:                         $changes{$itemid} = 1;
10555:                     }
10556:                     foreach my $extra ('passback','roster') {
10557:                         if ($env{'form.ltitools_'.$extra.'_'.$i}) {
10558:                             $confhash{$itemid}{$extra} = 1;
10559:                         }
10560:                         if ($domconfig{$action}{$itemid}{$extra} ne $confhash{$itemid}{$extra}) {
10561:                             $changes{$itemid} = 1;
10562:                         }
10563:                     }
10564:                     my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig_'.$i);
10565:                     foreach my $item ('label','title','target','linktext','explanation') {
10566:                         if (grep(/^\Q$item\E$/,@courseconfig)) {
10567:                             $confhash{$itemid}{'crsconf'}{$item} = 1;
10568:                             if (ref($domconfig{$action}{$itemid}{'crsconf'}) eq 'HASH') {
10569:                                 if ($domconfig{$action}{$itemid}{'crsconf'}{$item} ne $confhash{$itemid}{'crsconf'}{$item}) {
10570:                                     $changes{$itemid} = 1;
10571:                                 }
10572:                             } else {
10573:                                 $changes{$itemid} = 1;
10574:                             }
10575:                         }
10576:                     }
10577:                     my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_fields_'.$i);
10578:                     foreach my $field (@fields) {
10579:                         if ($possfield{$field}) {
10580:                             if ($field eq 'roles') {
10581:                                 foreach my $role (@courseroles) {
10582:                                     my $choice = $env{'form.ltitools_roles_'.$role.'_'.$i};
10583:                                     if (($choice ne '') && ($posslti{$choice})) {
10584:                                         $confhash{$itemid}{'roles'}{$role} = $choice;
10585:                                         if ($role eq 'cc') {
10586:                                             $confhash{$itemid}{'roles'}{'co'} = $choice;
10587:                                         }
10588:                                     }
10589:                                     if (ref($domconfig{$action}{$itemid}{'roles'}) eq 'HASH') {
10590:                                         if ($domconfig{$action}{$itemid}{'roles'}{$role} ne $confhash{$itemid}{'roles'}{$role}) {
10591:                                             $changes{$itemid} = 1;
10592:                                         }
10593:                                     } elsif ($confhash{$itemid}{'roles'}{$role}) {
10594:                                         $changes{$itemid} = 1;
10595:                                     }
10596:                                 }
10597:                             } else {
10598:                                 $confhash{$itemid}{'fields'}{$field} = 1;
10599:                                 if (ref($domconfig{$action}{$itemid}{'fields'}) eq 'HASH') {
10600:                                     if ($domconfig{$action}{$itemid}{'fields'}{$field} ne $confhash{$itemid}{'fields'}{$field}) {
10601:                                         $changes{$itemid} = 1;
10602:                                     }
10603:                                 } else {
10604:                                     $changes{$itemid} = 1;
10605:                                 }
10606:                             }
10607:                         }
10608:                     }
10609:                     $allpos[$newpos] = $itemid;
10610:                 }
10611:                 if ($imgdeletions{$itemid}) {
10612:                     $changes{$itemid} = 1;
10613:                     #FIXME need to obsolete item in RES space
10614:                 } elsif ($env{'form.ltitools_image_'.$i.'.filename'}) {
10615:                     my ($imgurl,$error) = &process_ltitools_image($r,$dom,$confname,'ltitools_image_'.$i,
10616:                                                                  $itemid,$configuserok,$switchserver,
10617:                                                                  $author_ok);
10618:                     if ($imgurl) {
10619:                         $confhash{$itemid}{'image'} = $imgurl;
10620:                         $changes{$itemid} = 1;
10621:                     }
10622:                     if ($error) {
10623:                         &Apache::lonnet::logthis($error);
10624:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10625:                     }
10626:                 } elsif ($domconfig{$action}{$itemid}{'image'}) {
10627:                     $confhash{$itemid}{'image'} =
10628:                        $domconfig{$action}{$itemid}{'image'};
10629:                 }
10630:                 if ($customadds{$i}) {
10631:                     my $name = $env{'form.ltitools_custom_name_'.$i};
10632:                     $name =~ s/(`)/'/g;
10633:                     $name =~ s/^\s+//;
10634:                     $name =~ s/\s+$//;
10635:                     my $value = $env{'form.ltitools_custom_value_'.$i};
10636:                     $value =~ s/(`)/'/g;
10637:                     $value =~ s/^\s+//;
10638:                     $value =~ s/\s+$//;
10639:                     if ($name ne '') {
10640:                         $confhash{$itemid}{'custom'}{$name} = $value;
10641:                         $changes{$itemid} = 1;
10642:                     }
10643:                 }
10644:                 my %customdels;
10645:                 my @customdeletions = &Apache::loncommon::get_env_multiple('form.ltitools_customdel_'.$i);
10646:                 if (@customdeletions) {
10647:                     $changes{$itemid} = 1;
10648:                 }
10649:                 map { $customdels{$_} = 1; } @customdeletions;
10650:                 if (ref($domconfig{$action}{$itemid}{'custom'}) eq 'HASH') {
10651:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}{'custom'}})) {
10652:                         unless ($customdels{$key}) {
10653:                             if ($env{'form.ltitools_customval_'.$key.'_'.$i} ne '') {
10654:                                 $confhash{$itemid}{'custom'}{$key} = $env{'form.ltitools_customval_'.$key.'_'.$i};
10655:                             }
10656:                             if ($domconfig{$action}{$itemid}{'custom'}{$key} ne $env{'form.ltitools_customval_'.$key.'_'.$i}) {
10657:                                 $changes{$itemid} = 1;
10658:                             }
10659:                         }
10660:                     }
10661:                 }
10662:                 unless ($changes{$itemid}) {
10663:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}})) {
10664:                         if (ref($domconfig{$action}{$itemid}{$key}) eq 'HASH') {
10665:                             if (ref($confhash{$itemid}{$key}) eq 'HASH') {
10666:                                 foreach my $innerkey (keys(%{$domconfig{$action}{$itemid}{$key}})) {
10667:                                     unless (exists($confhash{$itemid}{$key}{$innerkey})) {
10668:                                         $changes{$itemid} = 1;
10669:                                         last;
10670:                                     }
10671:                                 }
10672:                             } elsif (keys(%{$domconfig{$action}{$itemid}{$key}}) > 0) {
10673:                                 $changes{$itemid} = 1;
10674:                             }
10675:                         }
10676:                         last if ($changes{$itemid});
10677:                     }
10678:                 }
10679:             }
10680:         }
10681:     }
10682:     if (@allpos > 0) {
10683:         my $idx = 0;
10684:         foreach my $itemid (@allpos) {
10685:             if ($itemid ne '') {
10686:                 $confhash{$itemid}{'order'} = $idx;
10687:                 if (ref($domconfig{$action}) eq 'HASH') {
10688:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
10689:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
10690:                             $changes{$itemid} = 1;
10691:                         }
10692:                     }
10693:                 }
10694:                 $idx ++;
10695:             }
10696:         }
10697:     }
10698:     my %ltitoolshash = (
10699:                           $action => { %confhash }
10700:                        );
10701:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,
10702:                                              $dom);
10703:     if ($putresult eq 'ok') {
10704:         my %ltienchash = (
10705:                              $action => { %encconfig }
10706:                          );
10707:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom);
10708:         if (keys(%changes) > 0) {
10709:             my $cachetime = 24*60*60;
10710:             my %ltiall = %confhash;
10711:             foreach my $id (keys(%ltiall)) {
10712:                 if (ref($encconfig{$id}) eq 'HASH') {
10713:                     foreach my $item ('key','secret') {
10714:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
10715:                     }
10716:                 }
10717:             }
10718:             &Apache::lonnet::do_cache_new('ltitools',$dom,\%ltiall,$cachetime);
10719:             if (ref($lastactref) eq 'HASH') {
10720:                 $lastactref->{'ltitools'} = 1;
10721:             }
10722:             $resulttext = &mt('Changes made:').'<ul>';
10723:             my %bynum;
10724:             foreach my $itemid (sort(keys(%changes))) {
10725:                 my $position = $confhash{$itemid}{'order'};
10726:                 $bynum{$position} = $itemid;
10727:             }
10728:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
10729:                 my $itemid = $bynum{$pos};
10730:                 if (ref($confhash{$itemid}) ne 'HASH') {
10731:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
10732:                 } else {
10733:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'title'}.'</b>';
10734:                     if ($confhash{$itemid}{'image'}) {
10735:                         $resulttext .= '&nbsp;'.
10736:                                        '<img src="'.$confhash{$itemid}{'image'}.'"'.
10737:                                        ' alt="'.&mt('Tool Provider icon').'" />';
10738:                     }
10739:                     $resulttext .= '</li><ul>';
10740:                     my $position = $pos + 1;
10741:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
10742:                     foreach my $item ('version','msgtype','url') {
10743:                         if ($confhash{$itemid}{$item} ne '') {
10744:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
10745:                         }
10746:                     }
10747:                     if ($encconfig{$itemid}{'key'} ne '') {
10748:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
10749:                     }
10750:                     if ($encconfig{$itemid}{'secret'} ne '') {
10751:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
10752:                         my $num = length($encconfig{$itemid}{'secret'});
10753:                         $resulttext .= ('*'x$num).'</li>';
10754:                     }
10755:                     $resulttext .= '<li>'.&mt('Configurable in course:');
10756:                     my @possconfig = ('label','title','target','linktext','explanation');
10757:                     my $numconfig = 0;
10758:                     if (ref($confhash{$itemid}{'crsconf'}) eq 'HASH') {
10759:                         foreach my $item (@possconfig) {
10760:                             if ($confhash{$itemid}{'crsconf'}{$item}) {
10761:                                 $numconfig ++;
10762:                                 $resulttext .= ' "'.$lt{'crs'.$item}.'"';
10763:                             }
10764:                         }
10765:                     }
10766:                     if (!$numconfig) {
10767:                         $resulttext .= &mt('None');
10768:                     }
10769:                     $resulttext .= '</li>';
10770:                     foreach my $item ('passback','roster') {
10771:                         $resulttext .= '<li>'.$lt{$item}.'&nbsp;';
10772:                         if ($confhash{$itemid}{$item}) {
10773:                             $resulttext .= &mt('Yes');
10774:                         } else {
10775:                             $resulttext .= &mt('No');
10776:                         }
10777:                         $resulttext .= '</li>';
10778:                     }
10779:                     if (ref($confhash{$itemid}{'display'}) eq 'HASH') {
10780:                         my $displaylist;
10781:                         if ($confhash{$itemid}{'display'}{'target'}) {
10782:                             $displaylist = &mt('Display target').':&nbsp;'.
10783:                                            $confhash{$itemid}{'display'}{'target'}.',';
10784:                         }
10785:                         foreach my $size ('width','height') {
10786:                             if ($confhash{$itemid}{'display'}{$size}) {
10787:                                 $displaylist .= ('&nbsp;'x2).$lt{$size}.':&nbsp;'.
10788:                                                 $confhash{$itemid}{'display'}{$size}.',';
10789:                             }
10790:                         }
10791:                         if ($displaylist) {
10792:                             $displaylist =~ s/,$//;
10793:                             $resulttext .= '<li>'.$displaylist.'</li>';
10794:                         }
10795:                         foreach my $item ('linktext','explanation') {
10796:                             if ($confhash{$itemid}{'display'}{$item}) {
10797:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{'display'}{$item}.'</li>';
10798:                             }
10799:                         }
10800:                     }
10801:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
10802:                         my $fieldlist;
10803:                         foreach my $field (@allfields) {
10804:                             if ($confhash{$itemid}{'fields'}{$field}) {
10805:                                 $fieldlist .= ('&nbsp;'x2).$lt{$field}.',';
10806:                             }
10807:                         }
10808:                         if ($fieldlist) {
10809:                             $fieldlist =~ s/,$//;
10810:                             $resulttext .= '<li>'.&mt('Data sent').':'.$fieldlist.'</li>';
10811:                         }
10812:                     }
10813:                     if (ref($confhash{$itemid}{'roles'}) eq 'HASH') {
10814:                         my $rolemaps;
10815:                         foreach my $role (@courseroles) {
10816:                             if ($confhash{$itemid}{'roles'}{$role}) {
10817:                                 $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
10818:                                              $confhash{$itemid}{'roles'}{$role}.',';
10819:                             }
10820:                         }
10821:                         if ($rolemaps) {
10822:                             $rolemaps =~ s/,$//;
10823:                             $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
10824:                         }
10825:                     }
10826:                     if (ref($confhash{$itemid}{'custom'}) eq 'HASH') {
10827:                         my $customlist;
10828:                         if (keys(%{$confhash{$itemid}{'custom'}})) {
10829:                             foreach my $key (sort(keys(%{$confhash{$itemid}{'custom'}}))) {
10830:                                 $customlist .= $key.':'.$confhash{$itemid}{'custom'}{$key}.('&nbsp;'x2);
10831:                             }
10832:                         }
10833:                         if ($customlist) {
10834:                             $resulttext .= '<li>'.&mt('Custom items').':'.$customlist.'</li>';
10835:                         }
10836:                     }
10837:                     $resulttext .= '</ul></li>';
10838:                 }
10839:             }
10840:             $resulttext .= '</ul>';
10841:         } else {
10842:             $resulttext = &mt('No changes made.');
10843:         }
10844:     } else {
10845:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
10846:     }
10847:     if ($errors) {
10848:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
10849:                        $errors.'</ul>';
10850:     }
10851:     return $resulttext;
10852: }
10853: 
10854: sub process_ltitools_image {
10855:     my ($r,$dom,$confname,$caller,$itemid,$configuserok,$switchserver,$author_ok) = @_;
10856:     my $filename = $env{'form.'.$caller.'.filename'};
10857:     my ($error,$url);
10858:     my ($width,$height) = (21,21);
10859:     if ($configuserok eq 'ok') {
10860:         if ($switchserver) {
10861:             $error = &mt('Upload of Tool Provider (LTI) icon is not permitted to this server: [_1]',
10862:                          $switchserver);
10863:         } elsif ($author_ok eq 'ok') {
10864:             my ($result,$imageurl,$madethumb) =
10865:                 &publishlogo($r,'upload',$caller,$dom,$confname,
10866:                              "ltitools/$itemid/icon",$width,$height);
10867:             if ($result eq 'ok') {
10868:                 if ($madethumb) {
10869:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
10870:                     my $imagethumb = "$path/tn-".$imagefile;
10871:                     $url = $imagethumb;
10872:                 } else {
10873:                     $url = $imageurl;
10874:                 }
10875:             } else {
10876:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
10877:             }
10878:         } else {
10879:             $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);
10880:         }
10881:     } else {
10882:         $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);
10883:     }
10884:     return ($url,$error);
10885: }
10886: 
10887: sub get_ltitools_id {
10888:     my ($cdom,$title) = @_;
10889:     # get lock on ltitools db
10890:     my $lockhash = {
10891:                       lock => $env{'user.name'}.
10892:                               ':'.$env{'user.domain'},
10893:                    };
10894:     my $tries = 0;
10895:     my $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
10896:     my ($id,$error);
10897: 
10898:     while (($gotlock ne 'ok') && ($tries<10)) {
10899:         $tries ++;
10900:         sleep (0.1);
10901:         $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
10902:     }
10903:     if ($gotlock eq 'ok') {
10904:         my %currids = &Apache::lonnet::dump_dom('ltitools',$cdom);
10905:         if ($currids{'lock'}) {
10906:             delete($currids{'lock'});
10907:             if (keys(%currids)) {
10908:                 my @curr = sort { $a <=> $b } keys(%currids);
10909:                 if ($curr[-1] =~ /^\d+$/) {
10910:                     $id = 1 + $curr[-1];
10911:                 }
10912:             } else {
10913:                 $id = 1;
10914:             }
10915:             if ($id) {
10916:                 unless (&Apache::lonnet::newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
10917:                     $error = 'nostore';
10918:                 }
10919:             } else {
10920:                 $error = 'nonumber';
10921:             }
10922:         }
10923:         my $dellockoutcome = &Apache::lonnet::del_dom('ltitools',['lock'],$cdom);
10924:     } else {
10925:         $error = 'nolock';
10926:     }
10927:     return ($id,$error);
10928: }
10929: 
10930: sub modify_autoenroll {
10931:     my ($dom,$lastactref,%domconfig) = @_;
10932:     my ($resulttext,%changes);
10933:     my %currautoenroll;
10934:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
10935:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
10936:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
10937:         }
10938:     }
10939:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
10940:     my %title = ( run => 'Auto-enrollment active',
10941:                   sender => 'Sender for notification messages',
10942:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
10943:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
10944:     my @offon = ('off','on');
10945:     my $sender_uname = $env{'form.sender_uname'};
10946:     my $sender_domain = $env{'form.sender_domain'};
10947:     if ($sender_domain eq '') {
10948:         $sender_uname = '';
10949:     } elsif ($sender_uname eq '') {
10950:         $sender_domain = '';
10951:     }
10952:     my $coowners = $env{'form.autoassign_coowners'};
10953:     my $failsafe = $env{'form.autoenroll_failsafe'};
10954:     $failsafe =~ s{^\s+|\s+$}{}g;
10955:     if ($failsafe =~ /\D/) {
10956:         undef($failsafe);
10957:     }
10958:     my %autoenrollhash =  (
10959:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
10960:                                        'sender_uname' => $sender_uname,
10961:                                        'sender_domain' => $sender_domain,
10962:                                        'co-owners' => $coowners,
10963:                                        'autofailsafe' => $failsafe,
10964:                                 }
10965:                      );
10966:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
10967:                                              $dom);
10968:     if ($putresult eq 'ok') {
10969:         if (exists($currautoenroll{'run'})) {
10970:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
10971:                  $changes{'run'} = 1;
10972:              }
10973:         } elsif ($autorun) {
10974:             if ($env{'form.autoenroll_run'} ne '1') {
10975:                  $changes{'run'} = 1;
10976:             }
10977:         }
10978:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
10979:             $changes{'sender'} = 1;
10980:         }
10981:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
10982:             $changes{'sender'} = 1;
10983:         }
10984:         if ($currautoenroll{'co-owners'} ne '') {
10985:             if ($currautoenroll{'co-owners'} ne $coowners) {
10986:                 $changes{'coowners'} = 1;
10987:             }
10988:         } elsif ($coowners) {
10989:             $changes{'coowners'} = 1;
10990:         }
10991:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
10992:             $changes{'autofailsafe'} = 1;
10993:         }
10994:         if (keys(%changes) > 0) {
10995:             $resulttext = &mt('Changes made:').'<ul>';
10996:             if ($changes{'run'}) {
10997:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
10998:             }
10999:             if ($changes{'sender'}) {
11000:                 if ($sender_uname eq '' || $sender_domain eq '') {
11001:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
11002:                 } else {
11003:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
11004:                 }
11005:             }
11006:             if ($changes{'coowners'}) {
11007:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
11008:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
11009:                 if (ref($lastactref) eq 'HASH') {
11010:                     $lastactref->{'domainconfig'} = 1;
11011:                 }
11012:             }
11013:             if ($changes{'autofailsafe'}) {
11014:                 if ($failsafe ne '') {
11015:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$failsafe).'</li>';
11016:                 } else {
11017:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section: deleted');
11018:                 }
11019:                 &Apache::lonnet::get_domain_defaults($dom,1);
11020:                 if (ref($lastactref) eq 'HASH') {
11021:                     $lastactref->{'domdefaults'} = 1;
11022:                 }
11023:             }
11024:             $resulttext .= '</ul>';
11025:         } else {
11026:             $resulttext = &mt('No changes made to auto-enrollment settings');
11027:         }
11028:     } else {
11029:         $resulttext = '<span class="LC_error">'.
11030: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
11031:     }
11032:     return $resulttext;
11033: }
11034: 
11035: sub modify_autoupdate {
11036:     my ($dom,%domconfig) = @_;
11037:     my ($resulttext,%currautoupdate,%fields,%changes);
11038:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
11039:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
11040:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
11041:         }
11042:     }
11043:     my @offon = ('off','on');
11044:     my %title = &Apache::lonlocal::texthash (
11045:                    run => 'Auto-update:',
11046:                    classlists => 'Updates to user information in classlists?'
11047:                 );
11048:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
11049:     my %fieldtitles = &Apache::lonlocal::texthash (
11050:                         id => 'Student/Employee ID',
11051:                         permanentemail => 'E-mail address',
11052:                         lastname => 'Last Name',
11053:                         firstname => 'First Name',
11054:                         middlename => 'Middle Name',
11055:                         generation => 'Generation',
11056:                       );
11057:     $othertitle = &mt('All users');
11058:     if (keys(%{$usertypes}) >  0) {
11059:         $othertitle = &mt('Other users');
11060:     }
11061:     foreach my $key (keys(%env)) {
11062:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
11063:             my ($usertype,$item) = ($1,$2);
11064:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
11065:                 if ($usertype eq 'default') {   
11066:                     push(@{$fields{$1}},$2);
11067:                 } elsif (ref($types) eq 'ARRAY') {
11068:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
11069:                         push(@{$fields{$1}},$2);
11070:                     }
11071:                 }
11072:             }
11073:         }
11074:     }
11075:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
11076:     @lockablenames = sort(@lockablenames);
11077:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
11078:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
11079:         if (@changed) {
11080:             $changes{'lockablenames'} = 1;
11081:         }
11082:     } else {
11083:         if (@lockablenames) {
11084:             $changes{'lockablenames'} = 1;
11085:         }
11086:     }
11087:     my %updatehash = (
11088:                       autoupdate => { run => $env{'form.autoupdate_run'},
11089:                                       classlists => $env{'form.classlists'},
11090:                                       fields => {%fields},
11091:                                       lockablenames => \@lockablenames,
11092:                                     }
11093:                      );
11094:     foreach my $key (keys(%currautoupdate)) {
11095:         if (($key eq 'run') || ($key eq 'classlists')) {
11096:             if (exists($updatehash{autoupdate}{$key})) {
11097:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
11098:                     $changes{$key} = 1;
11099:                 }
11100:             }
11101:         } elsif ($key eq 'fields') {
11102:             if (ref($currautoupdate{$key}) eq 'HASH') {
11103:                 foreach my $item (@{$types},'default') {
11104:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
11105:                         my $change = 0;
11106:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
11107:                             if (!exists($fields{$item})) {
11108:                                 $change = 1;
11109:                                 last;
11110:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
11111:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
11112:                                     $change = 1;
11113:                                     last;
11114:                                 }
11115:                             }
11116:                         }
11117:                         if ($change) {
11118:                             push(@{$changes{$key}},$item);
11119:                         }
11120:                     } 
11121:                 }
11122:             }
11123:         } elsif ($key eq 'lockablenames') {
11124:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
11125:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
11126:                 if (@changed) {
11127:                     $changes{'lockablenames'} = 1;
11128:                 }
11129:             } else {
11130:                 if (@lockablenames) {
11131:                     $changes{'lockablenames'} = 1;
11132:                 }
11133:             }
11134:         }
11135:     }
11136:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
11137:         if (@lockablenames) {
11138:             $changes{'lockablenames'} = 1;
11139:         }
11140:     }
11141:     foreach my $item (@{$types},'default') {
11142:         if (defined($fields{$item})) {
11143:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
11144:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
11145:                     my $change = 0;
11146:                     if (ref($fields{$item}) eq 'ARRAY') {
11147:                         foreach my $type (@{$fields{$item}}) {
11148:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
11149:                                 $change = 1;
11150:                                 last;
11151:                             }
11152:                         }
11153:                     }
11154:                     if ($change) {
11155:                         push(@{$changes{'fields'}},$item);
11156:                     }
11157:                 } else {
11158:                     push(@{$changes{'fields'}},$item);
11159:                 }
11160:             } else {
11161:                 push(@{$changes{'fields'}},$item);
11162:             }
11163:         }
11164:     }
11165:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
11166:                                              $dom);
11167:     if ($putresult eq 'ok') {
11168:         if (keys(%changes) > 0) {
11169:             $resulttext = &mt('Changes made:').'<ul>';
11170:             foreach my $key (sort(keys(%changes))) {
11171:                 if ($key eq 'lockablenames') {
11172:                     $resulttext .= '<li>';
11173:                     if (@lockablenames) {
11174:                         $usertypes->{'default'} = $othertitle;
11175:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
11176:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
11177:                     } else {
11178:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
11179:                     }
11180:                     $resulttext .= '</li>';
11181:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
11182:                     foreach my $item (@{$changes{$key}}) {
11183:                         my @newvalues;
11184:                         foreach my $type (@{$fields{$item}}) {
11185:                             push(@newvalues,$fieldtitles{$type});
11186:                         }
11187:                         my $newvaluestr;
11188:                         if (@newvalues > 0) {
11189:                             $newvaluestr = join(', ',@newvalues);
11190:                         } else {
11191:                             $newvaluestr = &mt('none');
11192:                         }
11193:                         if ($item eq 'default') {
11194:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
11195:                         } else {
11196:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
11197:                         }
11198:                     }
11199:                 } else {
11200:                     my $newvalue;
11201:                     if ($key eq 'run') {
11202:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
11203:                     } else {
11204:                         $newvalue = $offon[$env{'form.'.$key}];
11205:                     }
11206:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
11207:                 }
11208:             }
11209:             $resulttext .= '</ul>';
11210:         } else {
11211:             $resulttext = &mt('No changes made to autoupdates');
11212:         }
11213:     } else {
11214:         $resulttext = '<span class="LC_error">'.
11215: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
11216:     }
11217:     return $resulttext;
11218: }
11219: 
11220: sub modify_autocreate {
11221:     my ($dom,%domconfig) = @_;
11222:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
11223:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
11224:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
11225:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
11226:         }
11227:     }
11228:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
11229:                  req => 'Auto-creation of validated requests for official courses',
11230:                  xmldc => 'Identity of course creator of courses from XML files',
11231:                );
11232:     my @types = ('xml','req');
11233:     foreach my $item (@types) {
11234:         $newvals{$item} = $env{'form.autocreate_'.$item};
11235:         $newvals{$item} =~ s/\D//g;
11236:         $newvals{$item} = 0 if ($newvals{$item} eq '');
11237:     }
11238:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
11239:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
11240:     unless (exists($domcoords{$newvals{'xmldc'}})) {
11241:         $newvals{'xmldc'} = '';
11242:     } 
11243:     %autocreatehash =  (
11244:                         autocreate => { xml => $newvals{'xml'},
11245:                                         req => $newvals{'req'},
11246:                                       }
11247:                        );
11248:     if ($newvals{'xmldc'} ne '') {
11249:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
11250:     }
11251:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
11252:                                              $dom);
11253:     if ($putresult eq 'ok') {
11254:         my @items = @types;
11255:         if ($newvals{'xml'}) {
11256:             push(@items,'xmldc');
11257:         }
11258:         foreach my $item (@items) {
11259:             if (exists($currautocreate{$item})) {
11260:                 if ($currautocreate{$item} ne $newvals{$item}) {
11261:                     $changes{$item} = 1;
11262:                 }
11263:             } elsif ($newvals{$item}) {
11264:                 $changes{$item} = 1;
11265:             }
11266:         }
11267:         if (keys(%changes) > 0) {
11268:             my @offon = ('off','on'); 
11269:             $resulttext = &mt('Changes made:').'<ul>';
11270:             foreach my $item (@types) {
11271:                 if ($changes{$item}) {
11272:                     my $newtxt = $offon[$newvals{$item}];
11273:                     $resulttext .= '<li>'.
11274:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
11275:                                        '<b>','</b>').
11276:                                    '</li>';
11277:                 }
11278:             }
11279:             if ($changes{'xmldc'}) {
11280:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
11281:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
11282:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
11283:             }
11284:             $resulttext .= '</ul>';
11285:         } else {
11286:             $resulttext = &mt('No changes made to auto-creation settings');
11287:         }
11288:     } else {
11289:         $resulttext = '<span class="LC_error">'.
11290:             &mt('An error occurred: [_1]',$putresult).'</span>';
11291:     }
11292:     return $resulttext;
11293: }
11294: 
11295: sub modify_directorysrch {
11296:     my ($dom,$lastactref,%domconfig) = @_;
11297:     my ($resulttext,%changes);
11298:     my %currdirsrch;
11299:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
11300:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
11301:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
11302:         }
11303:     }
11304:     my %title = ( available => 'Institutional directory search available',
11305:                   localonly => 'Other domains can search institution',
11306:                   lcavailable => 'LON-CAPA directory search available',
11307:                   lclocalonly => 'Other domains can search LON-CAPA domain',
11308:                   searchby => 'Search types',
11309:                   searchtypes => 'Search latitude');
11310:     my @offon = ('off','on');
11311:     my @otherdoms = ('Yes','No');
11312: 
11313:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
11314:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
11315:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
11316: 
11317:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
11318:     if (keys(%{$usertypes}) == 0) {
11319:         @cansearch = ('default');
11320:     } else {
11321:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
11322:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
11323:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
11324:                     push(@{$changes{'cansearch'}},$type);
11325:                 }
11326:             }
11327:             foreach my $type (@cansearch) {
11328:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
11329:                     push(@{$changes{'cansearch'}},$type);
11330:                 }
11331:             }
11332:         } else {
11333:             push(@{$changes{'cansearch'}},@cansearch);
11334:         }
11335:     }
11336: 
11337:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
11338:         foreach my $by (@{$currdirsrch{'searchby'}}) {
11339:             if (!grep(/^\Q$by\E$/,@searchby)) {
11340:                 push(@{$changes{'searchby'}},$by);
11341:             }
11342:         }
11343:         foreach my $by (@searchby) {
11344:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
11345:                 push(@{$changes{'searchby'}},$by);
11346:             }
11347:         }
11348:     } else {
11349:         push(@{$changes{'searchby'}},@searchby);
11350:     }
11351: 
11352:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
11353:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
11354:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
11355:                 push(@{$changes{'searchtypes'}},$type);
11356:             }
11357:         }
11358:         foreach my $type (@searchtypes) {
11359:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
11360:                 push(@{$changes{'searchtypes'}},$type);
11361:             }
11362:         }
11363:     } else {
11364:         if (exists($currdirsrch{'searchtypes'})) {
11365:             foreach my $type (@searchtypes) {  
11366:                 if ($type ne $currdirsrch{'searchtypes'}) { 
11367:                     push(@{$changes{'searchtypes'}},$type);
11368:                 }
11369:             }
11370:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
11371:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
11372:             }   
11373:         } else {
11374:             push(@{$changes{'searchtypes'}},@searchtypes); 
11375:         }
11376:     }
11377: 
11378:     my %dirsrch_hash =  (
11379:             directorysrch => { available => $env{'form.dirsrch_available'},
11380:                                cansearch => \@cansearch,
11381:                                localonly => $env{'form.dirsrch_instlocalonly'},
11382:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
11383:                                lcavailable => $env{'form.dirsrch_domavailable'},
11384:                                searchby => \@searchby,
11385:                                searchtypes => \@searchtypes,
11386:                              }
11387:             );
11388:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
11389:                                              $dom);
11390:     if ($putresult eq 'ok') {
11391:         if (exists($currdirsrch{'available'})) {
11392:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
11393:                  $changes{'available'} = 1;
11394:              }
11395:         } else {
11396:             if ($env{'form.dirsrch_available'} eq '1') {
11397:                 $changes{'available'} = 1;
11398:             }
11399:         }
11400:         if (exists($currdirsrch{'lcavailable'})) {
11401:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
11402:                 $changes{'lcavailable'} = 1;
11403:             }
11404:         } else {
11405:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
11406:                 $changes{'lcavailable'} = 1;
11407:             }
11408:         }
11409:         if (exists($currdirsrch{'localonly'})) {
11410:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
11411:                 $changes{'localonly'} = 1;
11412:             }
11413:         } else {
11414:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
11415:                 $changes{'localonly'} = 1;
11416:             }
11417:         }
11418:         if (exists($currdirsrch{'lclocalonly'})) {
11419:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
11420:                 $changes{'lclocalonly'} = 1;
11421:             }
11422:         } else {
11423:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
11424:                 $changes{'lclocalonly'} = 1;
11425:             }
11426:         }
11427:         if (keys(%changes) > 0) {
11428:             $resulttext = &mt('Changes made:').'<ul>';
11429:             if ($changes{'available'}) {
11430:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
11431:             }
11432:             if ($changes{'lcavailable'}) {
11433:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
11434:             }
11435:             if ($changes{'localonly'}) {
11436:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
11437:             }
11438:             if ($changes{'lclocalonly'}) {
11439:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
11440:             }
11441:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
11442:                 my $chgtext;
11443:                 if (ref($usertypes) eq 'HASH') {
11444:                     if (keys(%{$usertypes}) > 0) {
11445:                         foreach my $type (@{$types}) {
11446:                             if (grep(/^\Q$type\E$/,@cansearch)) {
11447:                                 $chgtext .= $usertypes->{$type}.'; ';
11448:                             }
11449:                         }
11450:                         if (grep(/^default$/,@cansearch)) {
11451:                             $chgtext .= $othertitle;
11452:                         } else {
11453:                             $chgtext =~ s/\; $//;
11454:                         }
11455:                         $resulttext .=
11456:                             '<li>'.
11457:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
11458:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
11459:                             '</li>';
11460:                     }
11461:                 }
11462:             }
11463:             if (ref($changes{'searchby'}) eq 'ARRAY') {
11464:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
11465:                 my $chgtext;
11466:                 foreach my $type (@{$titleorder}) {
11467:                     if (grep(/^\Q$type\E$/,@searchby)) {
11468:                         if (defined($searchtitles->{$type})) {
11469:                             $chgtext .= $searchtitles->{$type}.'; ';
11470:                         }
11471:                     }
11472:                 }
11473:                 $chgtext =~ s/\; $//;
11474:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
11475:             }
11476:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
11477:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
11478:                 my $chgtext;
11479:                 foreach my $type (@{$srchtypeorder}) {
11480:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
11481:                         if (defined($srchtypes_desc->{$type})) {
11482:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
11483:                         }
11484:                     }
11485:                 }
11486:                 $chgtext =~ s/\; $//;
11487:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
11488:             }
11489:             $resulttext .= '</ul>';
11490:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
11491:             if (ref($lastactref) eq 'HASH') {
11492:                 $lastactref->{'directorysrch'} = 1;
11493:             }
11494:         } else {
11495:             $resulttext = &mt('No changes made to directory search settings');
11496:         }
11497:     } else {
11498:         $resulttext = '<span class="LC_error">'.
11499:                       &mt('An error occurred: [_1]',$putresult).'</span>';
11500:     }
11501:     return $resulttext;
11502: }
11503: 
11504: sub modify_contacts {
11505:     my ($dom,$lastactref,%domconfig) = @_;
11506:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
11507:     if (ref($domconfig{'contacts'}) eq 'HASH') {
11508:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
11509:             $currsetting{$key} = $domconfig{'contacts'}{$key};
11510:         }
11511:     }
11512:     my (%others,%to,%bcc,%includestr,%includeloc);
11513:     my @contacts = ('supportemail','adminemail');
11514:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
11515:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail','hostipmail');
11516:     my @toggles = ('reporterrors','reportupdates');
11517:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
11518:     foreach my $type (@mailings) {
11519:         @{$newsetting{$type}} = 
11520:             &Apache::loncommon::get_env_multiple('form.'.$type);
11521:         foreach my $item (@contacts) {
11522:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
11523:                 $contacts_hash{contacts}{$type}{$item} = 1;
11524:             } else {
11525:                 $contacts_hash{contacts}{$type}{$item} = 0;
11526:             }
11527:         }
11528:         $others{$type} = $env{'form.'.$type.'_others'};
11529:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
11530:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
11531:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
11532:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
11533:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
11534:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
11535:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
11536:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
11537:             }
11538:         }
11539:     }
11540:     foreach my $item (@contacts) {
11541:         $to{$item} = $env{'form.'.$item};
11542:         $contacts_hash{'contacts'}{$item} = $to{$item};
11543:     }
11544:     foreach my $item (@toggles) {
11545:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
11546:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
11547:         }
11548:     }
11549:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
11550:         foreach my $field (@{$fields}) {
11551:             if (ref($possoptions->{$field}) eq 'ARRAY') {
11552:                 my $value = $env{'form.helpform_'.$field};
11553:                 $value =~ s/^\s+|\s+$//g;
11554:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
11555:                     $contacts_hash{'contacts'}{'helpform'}{$field} = $value;
11556:                     if ($field eq 'screenshot') {
11557:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
11558:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
11559:                             $contacts_hash{'contacts'}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
11560:                         }
11561:                     }
11562:                 }
11563:             }
11564:         }
11565:     }
11566:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
11567:     my (@statuses,%usertypeshash,@overrides);
11568:     if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
11569:         @statuses = @{$types};
11570:         if (ref($usertypes) eq 'HASH') {
11571:             %usertypeshash = %{$usertypes};
11572:         }
11573:     }
11574:     if (@statuses) {
11575:         my @possoverrides = &Apache::loncommon::get_env_multiple('form.overrides');
11576:         foreach my $type (@possoverrides) {
11577:             if (($type ne '') && (grep(/^\Q$type\E$/,@statuses))) {
11578:                 push(@overrides,$type);
11579:             }
11580:         }
11581:         if (@overrides) {
11582:             foreach my $type (@overrides) {
11583:                 my @standard = &Apache::loncommon::get_env_multiple('form.override_'.$type);
11584:                 foreach my $item (@contacts) {
11585:                     if (grep(/^\Q$item\E$/,@standard)) {
11586:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 1;
11587:                         $newsetting{'override_'.$type}{$item} = 1;
11588:                     } else {
11589:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 0;
11590:                         $newsetting{'override_'.$type}{$item} = 0;
11591:                     }
11592:                 }
11593:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'others'} = $env{'form.override_'.$type.'_others'};
11594:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
11595:                 $newsetting{'override_'.$type}{'others'} = $env{'form.override_'.$type.'_others'};
11596:                 $newsetting{'override_'.$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
11597:                 if (($env{'form.override_'.$type.'_includestr'} ne '') && ($env{'form.override_'.$type.'_includeloc'} =~ /^s|b$/)) {
11598:                     $includestr{$type} = $env{'form.override_'.$type.'_includestr'};
11599:                     $includeloc{$type} = $env{'form.override_'.$type.'_includeloc'};
11600:                     $contacts_hash{'contacts'}{'overrides'}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
11601:                     $newsetting{'override_'.$type}{'include'} = $contacts_hash{'contacts'}{'overrides'}{$type}{'include'};
11602:                 }
11603:             }    
11604:         }
11605:     }
11606:     if (keys(%currsetting) > 0) {
11607:         foreach my $item (@contacts) {
11608:             if ($to{$item} ne $currsetting{$item}) {
11609:                 $changes{$item} = 1;
11610:             }
11611:         }
11612:         foreach my $type (@mailings) {
11613:             foreach my $item (@contacts) {
11614:                 if (ref($currsetting{$type}) eq 'HASH') {
11615:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
11616:                         push(@{$changes{$type}},$item);
11617:                     }
11618:                 } else {
11619:                     push(@{$changes{$type}},@{$newsetting{$type}});
11620:                 }
11621:             }
11622:             if ($others{$type} ne $currsetting{$type}{'others'}) {
11623:                 push(@{$changes{$type}},'others');
11624:             }
11625:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
11626:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
11627:                     push(@{$changes{$type}},'bcc'); 
11628:                 }
11629:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
11630:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
11631:                     push(@{$changes{$type}},'include');
11632:                 }
11633:             }
11634:         }
11635:         if (ref($fields) eq 'ARRAY') {
11636:             if (ref($currsetting{'helpform'}) eq 'HASH') {
11637:                 foreach my $field (@{$fields}) {
11638:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
11639:                         push(@{$changes{'helpform'}},$field);
11640:                     }
11641:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
11642:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
11643:                             push(@{$changes{'helpform'}},'maxsize');
11644:                         }
11645:                     }
11646:                 }
11647:             } else {
11648:                 foreach my $field (@{$fields}) {
11649:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
11650:                         push(@{$changes{'helpform'}},$field);
11651:                     }
11652:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
11653:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
11654:                             push(@{$changes{'helpform'}},'maxsize');
11655:                         }
11656:                     }
11657:                 }
11658:             }
11659:         }
11660:         if (@statuses) {
11661:             if (ref($currsetting{'overrides'}) eq 'HASH') {
11662:                 foreach my $key (keys(%{$currsetting{'overrides'}})) {
11663:                     if (ref($currsetting{'overrides'}{$key}) eq 'HASH') {
11664:                         if (ref($newsetting{'override_'.$key}) eq 'HASH') {
11665:                             foreach my $item (@contacts,'bcc','others','include') {
11666:                                 if ($currsetting{'overrides'}{$key}{$item} ne $newsetting{'override_'.$key}{$item}) {
11667:                                     push(@{$changes{'overrides'}},$key);
11668:                                     last;
11669:                                 }
11670:                             }
11671:                         } else {
11672:                             push(@{$changes{'overrides'}},$key);
11673:                         }
11674:                     }
11675:                 }
11676:                 foreach my $key (@overrides) {
11677:                     unless (exists($currsetting{'overrides'}{$key})) {
11678:                         push(@{$changes{'overrides'}},$key);
11679:                     }
11680:                 }
11681:             } else {
11682:                 foreach my $key (@overrides) {
11683:                     push(@{$changes{'overrides'}},$key);
11684:                 }
11685:             }
11686:         }
11687:     } else {
11688:         my %default;
11689:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
11690:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
11691:         $default{'errormail'} = 'adminemail';
11692:         $default{'packagesmail'} = 'adminemail';
11693:         $default{'helpdeskmail'} = 'supportemail';
11694:         $default{'otherdomsmail'} = 'supportemail';
11695:         $default{'lonstatusmail'} = 'adminemail';
11696:         $default{'requestsmail'} = 'adminemail';
11697:         $default{'updatesmail'} = 'adminemail';
11698:         $default{'hostipmail'} = 'adminemail';
11699:         foreach my $item (@contacts) {
11700:            if ($to{$item} ne $default{$item}) {
11701:                $changes{$item} = 1;
11702:            }
11703:         }
11704:         foreach my $type (@mailings) {
11705:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
11706:                 push(@{$changes{$type}},@{$newsetting{$type}});
11707:             }
11708:             if ($others{$type} ne '') {
11709:                 push(@{$changes{$type}},'others');
11710:             }
11711:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
11712:                 if ($bcc{$type} ne '') {
11713:                     push(@{$changes{$type}},'bcc');
11714:                 }
11715:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
11716:                     push(@{$changes{$type}},'include');
11717:                 }
11718:             }
11719:         }
11720:         if (ref($fields) eq 'ARRAY') {
11721:             foreach my $field (@{$fields}) {
11722:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
11723:                     push(@{$changes{'helpform'}},$field);
11724:                 }
11725:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
11726:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
11727:                         push(@{$changes{'helpform'}},'maxsize');
11728:                     }
11729:                 }
11730:             }
11731:         }
11732:     }
11733:     foreach my $item (@toggles) {
11734:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
11735:             $changes{$item} = 1;
11736:         } elsif ((!$env{'form.'.$item}) &&
11737:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
11738:             $changes{$item} = 1;
11739:         }
11740:     }
11741:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
11742:                                              $dom);
11743:     if ($putresult eq 'ok') {
11744:         if (keys(%changes) > 0) {
11745:             &Apache::loncommon::devalidate_domconfig_cache($dom);
11746:             if (ref($lastactref) eq 'HASH') {
11747:                 $lastactref->{'domainconfig'} = 1;
11748:             }
11749:             my ($titles,$short_titles)  = &contact_titles();
11750:             $resulttext = &mt('Changes made:').'<ul>';
11751:             foreach my $item (@contacts) {
11752:                 if ($changes{$item}) {
11753:                     $resulttext .= '<li>'.$titles->{$item}.
11754:                                     &mt(' set to: ').
11755:                                     '<span class="LC_cusr_emph">'.
11756:                                     $to{$item}.'</span></li>';
11757:                 }
11758:             }
11759:             foreach my $type (@mailings) {
11760:                 if (ref($changes{$type}) eq 'ARRAY') {
11761:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
11762:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
11763:                     } else {
11764:                         $resulttext .= '<li>'.$titles->{$type}.': ';
11765:                     }
11766:                     my @text;
11767:                     foreach my $item (@{$newsetting{$type}}) {
11768:                         push(@text,$short_titles->{$item});
11769:                     }
11770:                     if ($others{$type} ne '') {
11771:                         push(@text,$others{$type});
11772:                     }
11773:                     if (@text) {
11774:                         $resulttext .= '<span class="LC_cusr_emph">'.
11775:                                        join(', ',@text).'</span>';
11776:                     }
11777:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
11778:                         if ($bcc{$type} ne '') {
11779:                             my $bcctext;
11780:                             if (@text) {
11781:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
11782:                             } else {
11783:                                 $bcctext = '(Bcc)';
11784:                             }
11785:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
11786:                         } elsif (!@text) {
11787:                             $resulttext .= &mt('No one');
11788:                         }
11789:                         if ($includestr{$type} ne '') {
11790:                             if ($includeloc{$type} eq 'b') {
11791:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
11792:                             } elsif ($includeloc{$type} eq 's') {
11793:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
11794:                             }
11795:                         }
11796:                     } elsif (!@text) {
11797:                         $resulttext .= &mt('No recipients');
11798:                     }
11799:                     $resulttext .= '</li>';
11800:                 }
11801:             }
11802:             if (ref($changes{'overrides'}) eq 'ARRAY') {
11803:                 my @deletions;
11804:                 foreach my $type (@{$changes{'overrides'}}) {
11805:                     if ($usertypeshash{$type}) {
11806:                         if (grep(/^\Q$type\E/,@overrides)) {
11807:                             $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation set for [_1]",
11808:                                                       $usertypeshash{$type}).'<ul><li>';
11809:                             if (ref($newsetting{'override_'.$type}) eq 'HASH') {
11810:                                 my @text;
11811:                                 foreach my $item (@contacts) {
11812:                                     if ($newsetting{'override_'.$type}{$item}) {
11813:                                         push(@text,$short_titles->{$item});
11814:                                     }
11815:                                 }
11816:                                 if ($newsetting{'override_'.$type}{'others'} ne '') {
11817:                                     push(@text,$newsetting{'override_'.$type}{'others'});
11818:                                 }
11819: 
11820:                                 if (@text) {
11821:                                     $resulttext .= &mt('Helpdesk e-mail sent to: [_1]',
11822:                                                        '<span class="LC_cusr_emph">'.join(', ',@text).'</span>');
11823:                                 }
11824:                                 if ($newsetting{'override_'.$type}{'bcc'} ne '') {
11825:                                     my $bcctext;
11826:                                     if (@text) {
11827:                                         $bcctext = '&nbsp;'.&mt('with Bcc to');
11828:                                     } else {
11829:                                         $bcctext = '(Bcc)';
11830:                                     }
11831:                                     $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$newsetting{'override_'.$type}{'bcc'}.'</span>';
11832:                                 } elsif (!@text) {
11833:                                      $resulttext .= &mt('Helpdesk e-mail sent to no one');
11834:                                 }
11835:                                 $resulttext .= '</li>';
11836:                                 if ($newsetting{'override_'.$type}{'include'} ne '') {
11837:                                     my ($loc,$str) = split(/:/,$newsetting{'override_'.$type}{'include'});
11838:                                     if ($loc eq 'b') {
11839:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail body:').' '.&unescape($str).'</li>';
11840:                                     } elsif ($loc eq 's') {
11841:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail subject:').' '.&unescape($str).'</li>';
11842:                                     }
11843:                                 }
11844:                             }
11845:                             $resulttext .= '</li></ul></li>';
11846:                         } else {
11847:                             push(@deletions,$usertypeshash{$type});
11848:                         }
11849:                     }
11850:                 }
11851:                 if (@deletions) {
11852:                     $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation discontinued for: [_1]",
11853:                                               join(', ',@deletions)).'</li>';
11854:                 }
11855:             }
11856:             my @offon = ('off','on');
11857:             if ($changes{'reporterrors'}) {
11858:                 $resulttext .= '<li>'.
11859:                                &mt('E-mail error reports to [_1] set to "'.
11860:                                    $offon[$env{'form.reporterrors'}].'".',
11861:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
11862:                                        &mt('LON-CAPA core group - MSU'),600,500)).
11863:                                '</li>';
11864:             }
11865:             if ($changes{'reportupdates'}) {
11866:                 $resulttext .= '<li>'.
11867:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
11868:                                     $offon[$env{'form.reportupdates'}].'".',
11869:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
11870:                                         &mt('LON-CAPA core group - MSU'),600,500)).
11871:                                 '</li>';
11872:             }
11873:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
11874:                 my (@optional,@required,@unused,$maxsizechg);
11875:                 foreach my $field (@{$changes{'helpform'}}) {
11876:                     if ($field eq 'maxsize') {
11877:                         $maxsizechg = 1;
11878:                         next;
11879:                     }
11880:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
11881:                         push(@optional,$field);
11882:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
11883:                         push(@unused,$field);
11884:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
11885:                         push(@required,$field);
11886:                     }
11887:                 }
11888:                 if (@optional) {
11889:                     $resulttext .= '<li>'.
11890:                                    &mt('Help form fields changed to "Optional": [_1].',
11891:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
11892:                                    '</li>';
11893:                 }
11894:                 if (@required) {
11895:                     $resulttext .= '<li>'.
11896:                                    &mt('Help form fields changed to "Required": [_1].',
11897:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
11898:                                    '</li>';
11899:                 }
11900:                 if (@unused) {
11901:                     $resulttext .= '<li>'.
11902:                                    &mt('Help form fields changed to "Not shown": [_1].',
11903:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
11904:                                    '</li>';
11905:                 }
11906:                 if ($maxsizechg) {
11907:                     $resulttext .= '<li>'.
11908:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
11909:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
11910:                                    '</li>';
11911:                 }
11912:             }
11913:             $resulttext .= '</ul>';
11914:         } else {
11915:             $resulttext = &mt('No changes made to contacts and form settings');
11916:         }
11917:     } else {
11918:         $resulttext = '<span class="LC_error">'.
11919:             &mt('An error occurred: [_1].',$putresult).'</span>';
11920:     }
11921:     return $resulttext;
11922: }
11923: 
11924: sub modify_passwords {
11925:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
11926:     my ($resulttext,%current,%changes,%newvalues,@oktypes,$errors,
11927:         $updatedefaults,$updateconf);
11928:     my $customfn = 'resetpw.html';
11929:     if (ref($domconfig{'passwords'}) eq 'HASH') {
11930:         %current = %{$domconfig{'passwords'}};
11931:     }
11932:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11933:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
11934:     if (ref($types) eq 'ARRAY') {
11935:         @oktypes = @{$types};
11936:     }
11937:     push(@oktypes,'default');
11938: 
11939:     my %titles = &Apache::lonlocal::texthash (
11940:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
11941:         intauth_check  => 'Check bcrypt cost if authenticated',
11942:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
11943:         permanent      => 'Permanent e-mail address',
11944:         critical       => 'Critical notification address',
11945:         notify         => 'Notification address',
11946:         min            => 'Minimum password length',
11947:         max            => 'Maximum password length',
11948:         chars          => 'Required characters',
11949:         numsaved       => 'Number of previous passwords to save',
11950:         reset          => 'Resetting Forgotten Password',
11951:         intauth        => 'Encryption of Stored Passwords (Internal Auth)',
11952:         rules          => 'Rules for LON-CAPA Passwords',
11953:         crsownerchg    => 'Course Owner Changing Student Passwords',
11954:         username       => 'Username',
11955:         email          => 'E-mail address',
11956:     );
11957: 
11958: #
11959: # Retrieve current domain configuration for internal authentication from $domconfig{'defaults'}.
11960: #
11961:     my (%curr_defaults,%save_defaults);
11962:     if (ref($domconfig{'defaults'}) eq 'HASH') {
11963:         foreach my $key (keys(%{$domconfig{'defaults'}})) {
11964:             if ($key =~ /^intauth_(cost|check|switch)$/) {
11965:                 $curr_defaults{$key} = $domconfig{'defaults'}{$key};
11966:             } else {
11967:                 $save_defaults{$key} = $domconfig{'defaults'}{$key};
11968:             }
11969:         }
11970:     }
11971:     my %staticdefaults = (
11972:         'resetlink'      => 2,
11973:         'resetcase'      => \@oktypes,
11974:         'resetprelink'   => 'both',
11975:         'resetemail'     => ['critical','notify','permanent'],
11976:         'intauth_cost'   => 10,
11977:         'intauth_check'  => 0,
11978:         'intauth_switch' => 0,
11979:     );
11980:     $staticdefaults{'min'} = $Apache::lonnet::passwdmin;
11981:     foreach my $type (@oktypes) {
11982:         $staticdefaults{'resetpostlink'}{$type} = ['email','username'];
11983:     }
11984:     my $linklife = $env{'form.passwords_link'};
11985:     $linklife =~ s/^\s+|\s+$//g;
11986:     if (($linklife =~ /^\d+(|\.\d*)$/) && ($linklife > 0)) {
11987:         $newvalues{'resetlink'} = $linklife;
11988:         if ($current{'resetlink'}) {
11989:             if ($current{'resetlink'} ne $linklife) {
11990:                 $changes{'reset'} = 1;
11991:             }
11992:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
11993:             if ($staticdefaults{'resetlink'} ne $linklife) {
11994:                 $changes{'reset'} = 1;
11995:             }
11996:         }
11997:     } elsif ($current{'resetlink'}) {
11998:         $changes{'reset'} = 1;
11999:     }
12000:     my @casesens;
12001:     my @posscase = &Apache::loncommon::get_env_multiple('form.passwords_case_sensitive');
12002:     foreach my $case (sort(@posscase)) {
12003:         if (grep(/^\Q$case\E$/,@oktypes)) {
12004:             push(@casesens,$case);
12005:         }
12006:     }
12007:     $newvalues{'resetcase'} = \@casesens;
12008:     if (ref($current{'resetcase'}) eq 'ARRAY') {
12009:         my @diffs = &Apache::loncommon::compare_arrays($current{'resetcase'},\@casesens);
12010:         if (@diffs > 0) {
12011:             $changes{'reset'} = 1;
12012:         }
12013:     } elsif (!ref($domconfig{passwords}) eq 'HASH') {
12014:         my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetcase'},\@casesens);
12015:         if (@diffs > 0) {
12016:             $changes{'reset'} = 1;
12017:         }
12018:     }
12019:     if ($env{'form.passwords_prelink'} =~ /^(both|either)$/) {
12020:         $newvalues{'resetprelink'} = $env{'form.passwords_prelink'};
12021:         if (exists($current{'resetprelink'})) {
12022:             if ($current{'resetprelink'} ne $newvalues{'resetprelink'}) {
12023:                 $changes{'reset'} = 1;
12024:             }
12025:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
12026:             if ($staticdefaults{'resetprelink'} ne $newvalues{'resetprelink'}) {
12027:                 $changes{'reset'} = 1;
12028:             }
12029:         }
12030:     } elsif ($current{'resetprelink'}) {
12031:         $changes{'reset'} = 1;
12032:     }
12033:     foreach my $type (@oktypes) {
12034:         my @possplink = &Apache::loncommon::get_env_multiple('form.passwords_postlink_'.$type);
12035:         my @postlink;
12036:         foreach my $item (sort(@possplink)) {
12037:             if ($item =~ /^(email|username)$/) {
12038:                 push(@postlink,$item);
12039:             }
12040:         }
12041:         $newvalues{'resetpostlink'}{$type} = \@postlink;
12042:         unless ($changes{'reset'}) {
12043:             if (ref($current{'resetpostlink'}) eq 'HASH') {
12044:                 if (ref($current{'resetpostlink'}{$type}) eq 'ARRAY') {
12045:                     my @diffs = &Apache::loncommon::compare_arrays($current{'resetpostlink'}{$type},\@postlink);
12046:                     if (@diffs > 0) {
12047:                         $changes{'reset'} = 1;
12048:                     }
12049:                 } else {
12050:                     $changes{'reset'} = 1;
12051:                 }
12052:             } elsif (!ref($domconfig{passwords}) eq 'HASH') {
12053:                 my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetpostlink'}{$type},\@postlink);
12054:                 if (@diffs > 0) {
12055:                     $changes{'reset'} = 1;
12056:                 }
12057:             }
12058:         }
12059:     }
12060:     my @possemailsrc = &Apache::loncommon::get_env_multiple('form.passwords_emailsrc');
12061:     my @resetemail;
12062:     foreach my $item (sort(@possemailsrc)) {
12063:         if ($item =~ /^(permanent|critical|notify)$/) {
12064:             push(@resetemail,$item);
12065:         }
12066:     }
12067:     $newvalues{'resetemail'} = \@resetemail;
12068:     unless ($changes{'reset'}) {
12069:         if (ref($current{'resetemail'}) eq 'ARRAY') {
12070:             my @diffs = &Apache::loncommon::compare_arrays($current{'resetemail'},\@resetemail);
12071:             if (@diffs > 0) {
12072:                 $changes{'reset'} = 1;
12073:             }
12074:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
12075:             my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetemail'},\@resetemail);
12076:             if (@diffs > 0) {
12077:                 $changes{'reset'} = 1;
12078:             }
12079:         }
12080:     }
12081:     if ($env{'form.passwords_stdtext'} == 0) {
12082:         $newvalues{'resetremove'} = 1;
12083:         unless ($current{'resetremove'}) {
12084:             $changes{'reset'} = 1;
12085:         }
12086:     } elsif ($current{'resetremove'}) {
12087:         $changes{'reset'} = 1;
12088:     }
12089:     if ($env{'form.passwords_customfile.filename'} ne '') {
12090:         my $servadm = $r->dir_config('lonAdmEMail');
12091:         my $servadm = $r->dir_config('lonAdmEMail');
12092:         my ($configuserok,$author_ok,$switchserver) =
12093:             &config_check($dom,$confname,$servadm);
12094:         my $error;
12095:         if ($configuserok eq 'ok') {
12096:             if ($switchserver) {
12097:                 $error = &mt("Upload of file containing domain-specific text is not permitted to this server: [_1]",$switchserver);
12098:             } else {
12099:                 if ($author_ok eq 'ok') {
12100:                     my ($result,$customurl) =
12101:                         &publishlogo($r,'upload','passwords_customfile',$dom,
12102:                                      $confname,'customtext/resetpw','','',$customfn);
12103:                     if ($result eq 'ok') {
12104:                         $newvalues{'resetcustom'} = $customurl;
12105:                         $changes{'reset'} = 1;
12106:                     } else {
12107:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$customfn,$result);
12108:                     }
12109:                 } else {
12110:                     $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].",$customfn,$confname,$dom,$author_ok);
12111:                 }
12112:             }
12113:         } else {
12114:             $error = &mt("Upload of [_1] failed because a Domain Configuration user ([_2]) could not be created in domain: [_3].  Error was: [_4].",$customfn,$confname,$dom,$configuserok);
12115:         }
12116:         if ($error) {
12117:             &Apache::lonnet::logthis($error);
12118:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12119:         }
12120:     } elsif ($current{'resetcustom'}) {
12121:         if ($env{'form.passwords_custom_del'}) {
12122:             $changes{'reset'} = 1;
12123:         } else {
12124:             $newvalues{'resetcustom'} = $current{'resetcustom'};
12125:         }
12126:     }
12127:     $env{'form.intauth_cost'} =~ s/^\s+|\s+$//g;
12128:     if (($env{'form.intauth_cost'} ne '') && ($env{'form.intauth_cost'} =~ /^\d+$/)) {
12129:         $save_defaults{'intauth_cost'} = $env{'form.intauth_cost'};
12130:         if ($save_defaults{'intauth_cost'} ne $curr_defaults{'intauth_cost'}) {
12131:             $changes{'intauth'} = 1;
12132:         }
12133:     } else {
12134:         $save_defaults{'intauth_cost'} = $curr_defaults{'intauth_cost'};
12135:     }
12136:     if ($env{'form.intauth_check'} =~ /^(0|1|2)$/) {
12137:         $save_defaults{'intauth_check'} = $env{'form.intauth_check'};
12138:         if ($save_defaults{'intauth_check'} ne $curr_defaults{'intauth_check'}) {
12139:             $changes{'intauth'} = 1;
12140:         }
12141:     } else {
12142:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
12143:     }
12144:     if ($env{'form.intauth_switch'} =~ /^(0|1|2)$/) {
12145:         $save_defaults{'intauth_switch'} = $env{'form.intauth_switch'};
12146:         if ($save_defaults{'intauth_switch'} ne $curr_defaults{'intauth_switch'}) {
12147:             $changes{'intauth'} = 1;
12148:         }
12149:     } else {
12150:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
12151:     }
12152:     foreach my $item ('cost','check','switch') {
12153:         if ($save_defaults{'intauth_'.$item} ne $domdefaults{'intauth_'.$item}) {
12154:             $domdefaults{'intauth_'.$item} = $save_defaults{'intauth_'.$item};
12155:             $updatedefaults = 1;
12156:         }
12157:     }
12158:     foreach my $rule ('min','max','numsaved') {
12159:         $env{'form.passwords_'.$rule} =~ s/^\s+|\s+$//g;
12160:         my $ruleok;
12161:         if ($rule eq 'min') {
12162:             if ($env{'form.passwords_'.$rule} =~ /^\d+$/) {
12163:                 if ($env{'form.passwords_'.$rule} >= $Apache::lonnet::passwdmin) {
12164:                     $ruleok = 1;
12165:                 }
12166:             }
12167:         } elsif (($env{'form.passwords_'.$rule} =~ /^\d+$/) &&
12168:                  ($env{'form.passwords_'.$rule} ne '0')) {
12169:             $ruleok = 1;
12170:         }
12171:         if ($ruleok) {
12172:             $newvalues{$rule} = $env{'form.passwords_'.$rule};
12173:             if (exists($current{$rule})) {
12174:                 if ($newvalues{$rule} ne $current{$rule}) {
12175:                     $changes{'rules'} = 1;
12176:                 }
12177:             } elsif ($rule eq 'min') {
12178:                 if ($staticdefaults{$rule} ne $newvalues{$rule}) {
12179:                     $changes{'rules'} = 1;
12180:                 }
12181:             } else {
12182:                 $changes{'rules'} = 1;
12183:             }
12184:         } elsif (exists($current{$rule})) {
12185:             $changes{'rules'} = 1;
12186:         }
12187:     }
12188:     my @posschars = &Apache::loncommon::get_env_multiple('form.passwords_chars');
12189:     my @chars;
12190:     foreach my $item (sort(@posschars)) {
12191:         if ($item =~ /^(uc|lc|num|spec)$/) {
12192:             push(@chars,$item);
12193:         }
12194:     }
12195:     $newvalues{'chars'} = \@chars;
12196:     unless ($changes{'rules'}) {
12197:         if (ref($current{'chars'}) eq 'ARRAY') {
12198:             my @diffs = &Apache::loncommon::compare_arrays($current{'chars'},\@chars);
12199:             if (@diffs > 0) {
12200:                 $changes{'rules'} = 1;
12201:             }
12202:         } else {
12203:             if (@chars > 0) {
12204:                 $changes{'rules'} = 1;
12205:             }
12206:         }
12207:     }
12208:     my %crsownerchg = (
12209:                         by => [],
12210:                         for => [],
12211:                       );
12212:     foreach my $item ('by','for') {
12213:         my @posstypes = &Apache::loncommon::get_env_multiple('form.passwords_crsowner_'.$item);
12214:         foreach my $type (sort(@posstypes)) {
12215:             if (grep(/^\Q$type\E$/,@oktypes)) {
12216:                 push(@{$crsownerchg{$item}},$type);
12217:             }
12218:         }
12219:     }
12220:     $newvalues{'crsownerchg'} = \%crsownerchg;
12221:     if (ref($current{'crsownerchg'}) eq 'HASH') {
12222:         foreach my $item ('by','for') {
12223:             if (ref($current{'crsownerchg'}{$item}) eq 'ARRAY') {
12224:                 my @diffs = &Apache::loncommon::compare_arrays($current{'crsownerchg'}{$item},$crsownerchg{$item});
12225:                 if (@diffs > 0) {
12226:                     $changes{'crsownerchg'} = 1;
12227:                     last;
12228:                 }
12229:             }
12230:         }
12231:     } elsif (!(ref($domconfig{passwords}) eq 'HASH')) {
12232:         foreach my $item ('by','for') {
12233:             if (@{$crsownerchg{$item}} > 0) {
12234:                 $changes{'crsownerchg'} = 1;
12235:                 last;
12236:             }
12237:         }
12238:     }
12239: 
12240:     my %confighash = (
12241:                         defaults  => \%save_defaults,
12242:                         passwords => \%newvalues,
12243:                      );
12244:     &process_captcha('passwords',\%changes,$confighash{'passwords'},$domconfig{'passwords'});
12245: 
12246:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
12247:     if ($putresult eq 'ok') {
12248:         if (keys(%changes) > 0) {
12249:             $resulttext = &mt('Changes made: ').'<ul>';
12250:             foreach my $key ('reset','intauth','rules','crsownerchg') {
12251:                 if ($changes{$key}) {
12252:                     unless ($key eq 'intauth') {
12253:                         $updateconf = 1;
12254:                     }
12255:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
12256:                     if ($key eq 'reset') {
12257:                         if ($confighash{'passwords'}{'captcha'} eq 'original') {
12258:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: original CAPTCHA').'</li>';
12259:                         } elsif ($confighash{'passwords'}{'captcha'} eq 'recaptcha') {
12260:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: reCAPTCHA').' '.
12261:                                            &mt('version: [_1]',$confighash{'passwords'}{'recaptchaversion'}).'<br />';
12262:                             if (ref($confighash{'passwords'}{'recaptchakeys'}) eq 'HASH') {
12263:                                 $resulttext .= &mt('Public key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'public'}).'</br>'.
12264:                                                &mt('Private key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'private'}).'</li>';
12265:                             }
12266:                         } else {
12267:                             $resulttext .= '<li>'.&mt('No CAPTCHA validation').'</li>';
12268:                         }
12269:                         if ($confighash{'passwords'}{'resetlink'}) {
12270:                             $resulttext .= '<li>'.&mt('Reset link expiration set to [quant,_1,hour]',$confighash{'passwords'}{'resetlink'}).'</li>';
12271:                         } else {
12272:                             $resulttext .= '<li>'.&mt('No reset link expiration set.').' '.
12273:                                                   &mt('Will default to 2 hours').'</li>';
12274:                         }
12275:                         if (ref($confighash{'passwords'}{'resetcase'}) eq 'ARRAY') {
12276:                             if (@{$confighash{'passwords'}{'resetcase'}} == 0) {
12277:                                 $resulttext .= '<li>'.&mt('User input for username and/or e-mail address not case sensitive for "Forgot Password" web form').'</li>';
12278:                             } else {
12279:                                 my $casesens;
12280:                                 foreach my $type (@{$confighash{'passwords'}{'resetcase'}}) {
12281:                                     if ($type eq 'default') {
12282:                                         $casesens .= $othertitle.', ';
12283:                                     } elsif ($usertypes->{$type} ne '') {
12284:                                         $casesens .= $usertypes->{$type}.', ';
12285:                                     }
12286:                                 }
12287:                                 $casesens =~ s/\Q, \E$//;
12288:                                 $resulttext .= '<li>'.&mt('"Forgot Password" web form input for username and/or e-mail address is case-sensitive for: [_1]',$casesens).'</li>';
12289:                             }
12290:                         } else {
12291:                             $resulttext .= '<li>'.&mt('Case-sensitivity not set for "Forgot Password" web form').' '.&mt('Will default to case-sensitive for username and/or e-mail address for all').'</li>';
12292:                         }
12293:                         if ($confighash{'passwords'}{'resetprelink'} eq 'either') {
12294:                             $resulttext .= '<li>'.&mt('Users can enter either a username or an e-mail address in "Forgot Password" web form').'</li>';
12295:                         } else {
12296:                             $resulttext .= '<li>'.&mt('Users can enter both a username and an e-mail address in "Forgot Password" web form').'</li>';
12297:                         }
12298:                         if (ref($confighash{'passwords'}{'resetpostlink'}) eq 'HASH') {
12299:                             my $output;
12300:                             if (ref($types) eq 'ARRAY') {
12301:                                 foreach my $type (@{$types}) {
12302:                                     if (ref($confighash{'passwords'}{'resetpostlink'}{$type}) eq 'ARRAY') {
12303:                                         if (@{$confighash{'passwords'}{'resetpostlink'}{$type}} == 0) {
12304:                                             $output .= $usertypes->{$type}.' -- '.&mt('none');
12305:                                         } else {
12306:                                             $output .= $usertypes->{$type}.' -- '.
12307:                                                        join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{$type}})).'; ';
12308:                                         }
12309:                                     }
12310:                                 }
12311:                             }
12312:                             if (ref($confighash{'passwords'}{'resetpostlink'}{'default'}) eq 'ARRAY') {
12313:                                 if (@{$confighash{'passwords'}{'resetpostlink'}{'default'}} == 0) {
12314:                                     $output .= $othertitle.' -- '.&mt('none');
12315:                                 } else {
12316:                                     $output .= $othertitle.' -- '.
12317:                                                join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{'default'}}));
12318:                                 }
12319:                             }
12320:                             if ($output) {
12321:                                 $resulttext .= '<li>'.&mt('Information required for new password form (by user type) set to: [_1]',$output).'</li>';
12322:                             } else {
12323:                                 $resulttext .= '<li>'.&mt('Information required for new password form not set.').' '.&mt('Will default to requiring both the username and an e-mail address').'</li>';
12324:                             }
12325:                         } else {
12326:                             $resulttext .= '<li>'.&mt('Information required for new password form not set.').' '.&mt('Will default to requiring both the username and an e-mail address').'</li>';
12327:                         }
12328:                         if (ref($confighash{'passwords'}{'resetemail'}) eq 'ARRAY') {
12329:                             if (@{$confighash{'passwords'}{'resetemail'}} > 0) {
12330:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$confighash{'passwords'}{'resetemail'}})).'</li>';
12331:                             } else {
12332:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
12333:                             }
12334:                         } else {
12335:                             $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA usedfor verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
12336:                         }
12337:                         if ($confighash{'passwords'}{'resetremove'}) {
12338:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form not shown').'</li>';
12339:                         } else {
12340:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form is shown').'</li>';
12341:                         }
12342:                         if ($confighash{'passwords'}{'resetcustom'}) {
12343:                             my $customlink = &Apache::loncommon::modal_link($confighash{'passwords'}{'resetcustom'},
12344:                                                                             &mt('custom text'),600,500,undef,undef,
12345:                                                                             undef,undef,'background-color:#ffffff');
12346:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" form includes: [_1]',$customlink).'</li>';
12347:                         } else {
12348:                             $resulttext .= '<li>'.&mt('No custom text included in preamble to "Forgot Password" form').'</li>';
12349:                         }
12350:                     } elsif ($key eq 'intauth') {
12351:                         foreach my $item ('cost','switch','check') {
12352:                             my $value = $save_defaults{$key.'_'.$item};
12353:                             if ($item eq 'switch') {
12354:                                 my %optiondesc = &Apache::lonlocal::texthash (
12355:                                                      0 => 'No',
12356:                                                      1 => 'Yes',
12357:                                                      2 => 'Yes, and copy existing passwd file to passwd.bak file',
12358:                                                  );
12359:                                 if ($value =~ /^(0|1|2)$/) {
12360:                                     $value = $optiondesc{$value};
12361:                                 } else {
12362:                                     $value = &mt('none -- defaults to No');
12363:                                 }
12364:                             } elsif ($item eq 'check') {
12365:                                 my %optiondesc = &Apache::lonlocal::texthash (
12366:                                                      0 => 'No',
12367:                                                      1 => 'Yes, allow login then update passwd file using default cost (if higher)',
12368:                                                      2 => 'Yes, disallow login if stored cost is less than domain default',
12369:                                                  );
12370:                                 if ($value =~ /^(0|1|2)$/) {
12371:                                     $value = $optiondesc{$value};
12372:                                 } else {
12373:                                     $value = &mt('none -- defaults to No');
12374:                                 }
12375:                             }
12376:                             $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$titles{$key.'_'.$item},$value).'</li>';
12377:                         }
12378:                     } elsif ($key eq 'rules') {
12379:                         foreach my $rule ('min','max','numsaved') {
12380:                             if ($confighash{'passwords'}{$rule} eq '') {
12381:                                 if ($rule eq 'min') {
12382:                                     $resulttext .= '<li>'.&mt('[_1] not set.',$titles{$rule});
12383:                                                    ' '.&mt('Default of [_1] will be used',
12384:                                                            $Apache::lonnet::passwdmin).'</li>';
12385:                                 } else {
12386:                                     $resulttext .= '<li>'.&mt('[_1] set to none',$titles{$rule}).'</li>';
12387:                                 }
12388:                             } else {
12389:                                 $resulttext .= '<li>'.&mt('[_1] set to [_2]',$titles{$rule},$confighash{'passwords'}{$rule}).'</li>';
12390:                             }
12391:                         }
12392:                         if (ref($confighash{'passwords'}{'chars'}) eq 'ARRAY') {
12393:                             if (@{$confighash{'passwords'}{'chars'}} > 0) {
12394:                                 my %rulenames = &Apache::lonlocal::texthash(
12395:                                                      uc => 'At least one upper case letter',
12396:                                                      lc => 'At least one lower case letter',
12397:                                                      num => 'At least one number',
12398:                                                      spec => 'At least one non-alphanumeric',
12399:                                                    );
12400:                                 my $needed = '<ul><li>'.
12401:                                              join('</li><li>',map {$rulenames{$_} } @{$confighash{'passwords'}{'chars'}}).
12402:                                              '</li></ul>';
12403:                                 $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$titles{'chars'},$needed).'</li>';
12404:                             } else {
12405:                                 $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
12406:                             }
12407:                         } else {
12408:                             $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
12409:                         }
12410:                     } elsif ($key eq 'crsownerchg') {
12411:                         if (ref($confighash{'passwords'}{'crsownerchg'}) eq 'HASH') {
12412:                             if ((@{$confighash{'passwords'}{'crsownerchg'}{'by'}} == 0) ||
12413:                                 (@{$confighash{'passwords'}{'crsownerchg'}{'for'}} == 0)) {
12414:                                 $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
12415:                             } else {
12416:                                 my %crsownerstr;
12417:                                 foreach my $item ('by','for') {
12418:                                     if (ref($confighash{'passwords'}{'crsownerchg'}{$item}) eq 'ARRAY') {
12419:                                         foreach my $type (@{$confighash{'passwords'}{'crsownerchg'}{$item}}) {
12420:                                             if ($type eq 'default') {
12421:                                                 $crsownerstr{$item} .= $othertitle.', ';
12422:                                             } elsif ($usertypes->{$type} ne '') {
12423:                                                 $crsownerstr{$item} .= $usertypes->{$type}.', ';
12424:                                             }
12425:                                         }
12426:                                         $crsownerstr{$item} =~ s/\Q, \E$//;
12427:                                     }
12428:                                 }
12429:                                 $resulttext .= '<li>'.&mt('Course owner (with status: [_1]) may change passwords for students (with status: [_2]).',
12430:                                            $crsownerstr{'by'},$crsownerstr{'for'}).'</li>';
12431:                             }
12432:                         } else {
12433:                             $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
12434:                         }
12435:                     }
12436:                     $resulttext .= '</ul></li>';
12437:                 }
12438:             }
12439:             $resulttext .= '</ul>';
12440:         } else {
12441:             $resulttext = &mt('No changes made to password settings');
12442:         }
12443:         my $cachetime = 24*60*60;
12444:         if ($updatedefaults) {
12445:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12446:             if (ref($lastactref) eq 'HASH') {
12447:                 $lastactref->{'domdefaults'} = 1;
12448:             }
12449:         }
12450:         if ($updateconf) {
12451:             &Apache::lonnet::do_cache_new('passwdconf',$dom,$confighash{'passwords'},$cachetime);
12452:             if (ref($lastactref) eq 'HASH') {
12453:                 $lastactref->{'passwdconf'} = 1;
12454:             }
12455:         }
12456:     } else {
12457:         $resulttext = '<span class="LC_error">'.
12458:             &mt('An error occurred: [_1]',$putresult).'</span>';
12459:     }
12460:     if ($errors) {
12461:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
12462:                        $errors.'</ul></p>';
12463:     }
12464:     return $resulttext;
12465: }
12466: 
12467: sub modify_usercreation {
12468:     my ($dom,%domconfig) = @_;
12469:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
12470:     my $warningmsg;
12471:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
12472:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
12473:             if ($key eq 'cancreate') {
12474:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
12475:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
12476:                         if (($item eq 'requestcrs') || ($item eq 'course') || ($item eq 'author')) {
12477:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
12478:                         } else {
12479:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
12480:                         }
12481:                     }
12482:                 }
12483:             } elsif ($key eq 'email_rule') {
12484:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
12485:             } else {
12486:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
12487:             }
12488:         }
12489:     }
12490:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
12491:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
12492:     my @contexts = ('author','course','requestcrs');
12493:     foreach my $item(@contexts) {
12494:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
12495:     }
12496:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
12497:         foreach my $item (@contexts) {
12498:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
12499:                 push(@{$changes{'cancreate'}},$item);
12500:             }
12501:         }
12502:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
12503:         foreach my $item (@contexts) {
12504:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
12505:                 if ($cancreate{$item} ne 'any') {
12506:                     push(@{$changes{'cancreate'}},$item);
12507:                 }
12508:             } else {
12509:                 if ($cancreate{$item} ne 'none') {
12510:                     push(@{$changes{'cancreate'}},$item);
12511:                 }
12512:             }
12513:         }
12514:     } else {
12515:         foreach my $item (@contexts)  {
12516:             push(@{$changes{'cancreate'}},$item);
12517:         }
12518:     }
12519: 
12520:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
12521:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
12522:             if (!grep(/^\Q$type\E$/,@username_rule)) {
12523:                 push(@{$changes{'username_rule'}},$type);
12524:             }
12525:         }
12526:         foreach my $type (@username_rule) {
12527:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
12528:                 push(@{$changes{'username_rule'}},$type);
12529:             }
12530:         }
12531:     } else {
12532:         push(@{$changes{'username_rule'}},@username_rule);
12533:     }
12534: 
12535:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
12536:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
12537:             if (!grep(/^\Q$type\E$/,@id_rule)) {
12538:                 push(@{$changes{'id_rule'}},$type);
12539:             }
12540:         }
12541:         foreach my $type (@id_rule) {
12542:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
12543:                 push(@{$changes{'id_rule'}},$type);
12544:             }
12545:         }
12546:     } else {
12547:         push(@{$changes{'id_rule'}},@id_rule);
12548:     }
12549: 
12550:     my @authen_contexts = ('author','course','domain');
12551:     my @authtypes = ('int','krb4','krb5','loc');
12552:     my %authhash;
12553:     foreach my $item (@authen_contexts) {
12554:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
12555:         foreach my $auth (@authtypes) {
12556:             if (grep(/^\Q$auth\E$/,@authallowed)) {
12557:                 $authhash{$item}{$auth} = 1;
12558:             } else {
12559:                 $authhash{$item}{$auth} = 0;
12560:             }
12561:         }
12562:     }
12563:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
12564:         foreach my $item (@authen_contexts) {
12565:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
12566:                 foreach my $auth (@authtypes) {
12567:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
12568:                         push(@{$changes{'authtypes'}},$item);
12569:                         last;
12570:                     }
12571:                 }
12572:             }
12573:         }
12574:     } else {
12575:         foreach my $item (@authen_contexts) {
12576:             push(@{$changes{'authtypes'}},$item);
12577:         }
12578:     }
12579: 
12580:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
12581:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
12582:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
12583:     $save_usercreate{'id_rule'} = \@id_rule;
12584:     $save_usercreate{'username_rule'} = \@username_rule,
12585:     $save_usercreate{'authtypes'} = \%authhash;
12586: 
12587:     my %usercreation_hash =  (
12588:         usercreation     => \%save_usercreate,
12589:     );
12590: 
12591:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
12592:                                              $dom);
12593: 
12594:     if ($putresult eq 'ok') {
12595:         if (keys(%changes) > 0) {
12596:             $resulttext = &mt('Changes made:').'<ul>';
12597:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
12598:                 my %lt = &usercreation_types();
12599:                 foreach my $type (@{$changes{'cancreate'}}) {
12600:                     my $chgtext = $lt{$type}.', ';
12601:                     if ($cancreate{$type} eq 'none') {
12602:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
12603:                     } elsif ($cancreate{$type} eq 'any') {
12604:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
12605:                     } elsif ($cancreate{$type} eq 'official') {
12606:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
12607:                     } elsif ($cancreate{$type} eq 'unofficial') {
12608:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
12609:                     }
12610:                     $resulttext .= '<li>'.$chgtext.'</li>';
12611:                 }
12612:             }
12613:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
12614:                 my ($rules,$ruleorder) = 
12615:                     &Apache::lonnet::inst_userrules($dom,'username');
12616:                 my $chgtext = '<ul>';
12617:                 foreach my $type (@username_rule) {
12618:                     if (ref($rules->{$type}) eq 'HASH') {
12619:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
12620:                     }
12621:                 }
12622:                 $chgtext .= '</ul>';
12623:                 if (@username_rule > 0) {
12624:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
12625:                 } else {
12626:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
12627:                 }
12628:             }
12629:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
12630:                 my ($idrules,$idruleorder) = 
12631:                     &Apache::lonnet::inst_userrules($dom,'id');
12632:                 my $chgtext = '<ul>';
12633:                 foreach my $type (@id_rule) {
12634:                     if (ref($idrules->{$type}) eq 'HASH') {
12635:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
12636:                     }
12637:                 }
12638:                 $chgtext .= '</ul>';
12639:                 if (@id_rule > 0) {
12640:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
12641:                 } else {
12642:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
12643:                 }
12644:             }
12645:             my %authname = &authtype_names();
12646:             my %context_title = &context_names();
12647:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
12648:                 my $chgtext = '<ul>';
12649:                 foreach my $type (@{$changes{'authtypes'}}) {
12650:                     my @allowed;
12651:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
12652:                     foreach my $auth (@authtypes) {
12653:                         if ($authhash{$type}{$auth}) {
12654:                             push(@allowed,$authname{$auth});
12655:                         }
12656:                     }
12657:                     if (@allowed > 0) {
12658:                         $chgtext .= join(', ',@allowed).'</li>';
12659:                     } else {
12660:                         $chgtext .= &mt('none').'</li>';
12661:                     }
12662:                 }
12663:                 $chgtext .= '</ul>';
12664:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
12665:                 $resulttext .= '</li>';
12666:             }
12667:             $resulttext .= '</ul>';
12668:         } else {
12669:             $resulttext = &mt('No changes made to user creation settings');
12670:         }
12671:     } else {
12672:         $resulttext = '<span class="LC_error">'.
12673:             &mt('An error occurred: [_1]',$putresult).'</span>';
12674:     }
12675:     if ($warningmsg ne '') {
12676:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
12677:     }
12678:     return $resulttext;
12679: }
12680: 
12681: sub modify_selfcreation {
12682:     my ($dom,$lastactref,%domconfig) = @_;
12683:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%curr_inststatus,%changes,%cancreate);
12684:     my (%save_usercreate,%save_usermodify,%save_inststatus,@types,%usertypes);
12685:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12686:     my ($othertitle,$usertypesref,$typesref) = &Apache::loncommon::sorted_inst_types($dom);
12687:     if (ref($typesref) eq 'ARRAY') {
12688:         @types = @{$typesref};
12689:     }
12690:     if (ref($usertypesref) eq 'HASH') {
12691:         %usertypes = %{$usertypesref};
12692:     }
12693:     $usertypes{'default'} = $othertitle;
12694: #
12695: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
12696: #
12697:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
12698:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
12699:             if ($key eq 'cancreate') {
12700:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
12701:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
12702:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
12703:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
12704:                             ($item eq 'recaptchaversion') || ($item eq 'notify') ||
12705:                             ($item eq 'emailusername') || ($item eq 'shibenv') ||
12706:                             ($item eq 'selfcreateprocessing') || ($item eq 'emailverified') ||
12707:                             ($item eq 'emailoptions') || ($item eq 'emaildomain')) {
12708:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
12709:                         } else {
12710:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
12711:                         }
12712:                     }
12713:                 }
12714:             } elsif ($key eq 'email_rule') {
12715:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
12716:             } else {
12717:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
12718:             }
12719:         }
12720:     }
12721: #
12722: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
12723: #
12724:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
12725:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
12726:             if ($key eq 'selfcreate') {
12727:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
12728:             } else {
12729:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
12730:             }
12731:         }
12732:     }
12733: #
12734: # Retrieve current domain configuration for institutional status types from $domconfig{'inststatus'}.
12735: #
12736:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
12737:         foreach my $key (keys(%{$domconfig{'inststatus'}})) {
12738:             if ($key eq 'inststatusguest') {
12739:                 $curr_inststatus{$key} = $domconfig{'inststatus'}{$key};
12740:             } else {
12741:                 $save_inststatus{$key} = $domconfig{'inststatus'}{$key};
12742:             }
12743:         }
12744:     }
12745: 
12746:     my @contexts = ('selfcreate');
12747:     @{$cancreate{'selfcreate'}} = ();
12748:     %{$cancreate{'emailusername'}} = ();
12749:     if (@types) {
12750:         @{$cancreate{'statustocreate'}} = ();
12751:     }
12752:     %{$cancreate{'selfcreateprocessing'}} = ();
12753:     %{$cancreate{'shibenv'}} = ();
12754:     %{$cancreate{'emailverified'}} = ();
12755:     %{$cancreate{'emailoptions'}} = ();
12756:     %{$cancreate{'emaildomain'}} = ();
12757:     my %selfcreatetypes = (
12758:                              sso   => 'users authenticated by institutional single sign on',
12759:                              login => 'users authenticated by institutional log-in',
12760:                              email => 'users verified by e-mail',
12761:                           );
12762: #
12763: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
12764: # is permitted.
12765: #
12766: 
12767:     my ($emailrules,$emailruleorder) = &Apache::lonnet::inst_userrules($dom,'email');
12768: 
12769:     my (@statuses,%email_rule);
12770:     foreach my $item ('login','sso','email') {
12771:         if ($item eq 'email') {
12772:             if ($env{'form.cancreate_email'}) {
12773:                 if (@types) {
12774:                     my @poss_statuses = &Apache::loncommon::get_env_multiple('form.selfassign');
12775:                     foreach my $status (@poss_statuses) {
12776:                         if (grep(/^\Q$status\E$/,(@types,'default'))) {
12777:                             push(@statuses,$status);
12778:                         }
12779:                     }
12780:                     $save_inststatus{'inststatusguest'} = \@statuses;
12781:                 } else {
12782:                     push(@statuses,'default');
12783:                 }
12784:                 if (@statuses) {
12785:                     my %curr_rule;
12786:                     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
12787:                         foreach my $type (@statuses) {
12788:                             $curr_rule{$type} = $curr_usercreation{'email_rule'};
12789:                         }
12790:                     } elsif (ref($curr_usercreation{'email_rule'}) eq 'HASH') {
12791:                         foreach my $type (@statuses) {
12792:                             $curr_rule{$type} = $curr_usercreation{'email_rule'}{$type};
12793:                         }
12794:                     }
12795:                     push(@{$cancreate{'selfcreate'}},'email');
12796:                     push(@contexts,('selfcreateprocessing','emailverified','emailoptions'));
12797:                     my %curremaildom;
12798:                     if (ref($curr_usercreation{'cancreate'}{'emaildomain'}) eq 'HASH') {
12799:                         %curremaildom = %{$curr_usercreation{'cancreate'}{'emaildomain'}};
12800:                     }
12801:                     foreach my $type (@statuses) {
12802:                         if ($env{'form.cancreate_emailprocess_'.$type} =~ /^(?:approval|automatic)$/) {
12803:                             $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
12804:                         }
12805:                         if ($env{'form.cancreate_usernameoptions_'.$type} =~ /^(?:all|first|free)$/) {
12806:                             $cancreate{'emailverified'}{$type} = $env{'form.cancreate_usernameoptions_'.$type};
12807:                         }
12808:                         if ($env{'form.cancreate_emailoptions_'.$type} =~ /^(any|inst|noninst|custom)$/) {
12809: #
12810: # Retrieve rules (if any) governing types of e-mail address which may be used to verify a username.
12811: #
12812:                             my $chosen = $1;
12813:                             if (($chosen eq 'inst') || ($chosen eq 'noninst')) {
12814:                                 my $emaildom;
12815:                                 if ($env{'form.cancreate_emaildomain_'.$chosen.'_'.$type} =~ /^\@[^\@]+$/) {
12816:                                     $emaildom = $env{'form.cancreate_emaildomain_'.$chosen.'_'.$type};
12817:                                     $cancreate{'emaildomain'}{$type}{$chosen} = $emaildom;
12818:                                     if (ref($curremaildom{$type}) eq 'HASH') {
12819:                                         if (exists($curremaildom{$type}{$chosen})) {
12820:                                             if ($curremaildom{$type}{$chosen} ne $emaildom) {
12821:                                                 push(@{$changes{'cancreate'}},'emaildomain');
12822:                                             }
12823:                                         } elsif ($emaildom ne '') {
12824:                                             push(@{$changes{'cancreate'}},'emaildomain');
12825:                                         }
12826:                                     } elsif ($emaildom ne '') {
12827:                                         push(@{$changes{'cancreate'}},'emaildomain');
12828:                                     }
12829:                                 }
12830:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
12831:                             } elsif ($chosen eq 'custom') {
12832:                                 my @possemail_rules = &Apache::loncommon::get_env_multiple('form.email_rule_'.$type);
12833:                                 $email_rule{$type} = [];
12834:                                 if (ref($emailrules) eq 'HASH') {
12835:                                     foreach my $rule (@possemail_rules) {
12836:                                         if (exists($emailrules->{$rule})) {
12837:                                             push(@{$email_rule{$type}},$rule);
12838:                                         }
12839:                                     }
12840:                                 }
12841:                                 if (@{$email_rule{$type}}) {
12842:                                     $cancreate{'emailoptions'}{$type} = 'custom';
12843:                                     if (ref($curr_rule{$type}) eq 'ARRAY') {
12844:                                         if (@{$curr_rule{$type}} > 0) {
12845:                                             foreach my $rule (@{$curr_rule{$type}}) {
12846:                                                 if (!grep(/^\Q$rule\E$/,@{$email_rule{$type}})) {
12847:                                                     push(@{$changes{'email_rule'}},$type);
12848:                                                 }
12849:                                             }
12850:                                         }
12851:                                         foreach my $type (@{$email_rule{$type}}) {
12852:                                             if (!grep(/^\Q$type\E$/,@{$curr_rule{$type}})) {
12853:                                                 push(@{$changes{'email_rule'}},$type);
12854:                                             }
12855:                                         }
12856:                                     } else {
12857:                                         push(@{$changes{'email_rule'}},$type);
12858:                                     }
12859:                                 }
12860:                             } else {
12861:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
12862:                             }
12863:                         }
12864:                     }
12865:                     if (@types) {
12866:                         if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
12867:                             my @changed = &Apache::loncommon::compare_arrays(\@statuses,$curr_inststatus{'inststatusguest'});
12868:                             if (@changed) {
12869:                                 push(@{$changes{'inststatus'}},'inststatusguest');
12870:                             }
12871:                         } else {
12872:                             push(@{$changes{'inststatus'}},'inststatusguest');
12873:                         }
12874:                     }
12875:                 } else {
12876:                     delete($env{'form.cancreate_email'});
12877:                     if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
12878:                         if (@{$curr_inststatus{'inststatusguest'}} > 0) {
12879:                             push(@{$changes{'inststatus'}},'inststatusguest');
12880:                         }
12881:                     }
12882:                 }
12883:             } else {
12884:                 $save_inststatus{'inststatusguest'} = [];
12885:                 if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
12886:                     if (@{$curr_inststatus{'inststatusguest'}} > 0) {
12887:                         push(@{$changes{'inststatus'}},'inststatusguest');
12888:                     }
12889:                 }
12890:             }
12891:         } else {
12892:             if ($env{'form.cancreate_'.$item}) {
12893:                 push(@{$cancreate{'selfcreate'}},$item);
12894:             }
12895:         }
12896:     }
12897:     my (%userinfo,%savecaptcha);
12898:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
12899: #
12900: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
12901: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
12902: #
12903: 
12904:     if ($env{'form.cancreate_email'}) {
12905:         push(@contexts,'emailusername');
12906:         if (@statuses) {
12907:             foreach my $type (@statuses) {
12908:                 if (ref($infofields) eq 'ARRAY') {
12909:                     foreach my $field (@{$infofields}) {
12910:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
12911:                             $cancreate{'emailusername'}{$type}{$field} = $1;
12912:                         }
12913:                     }
12914:                 }
12915:             }
12916:         }
12917: #
12918: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
12919: # queued requests for self-creation of account verified by e-mail.
12920: #
12921: 
12922:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
12923:         @approvalnotify = sort(@approvalnotify);
12924:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
12925:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
12926:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
12927:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
12928:                     push(@{$changes{'cancreate'}},'notify');
12929:                 }
12930:             } else {
12931:                 if ($cancreate{'notify'}{'approval'}) {
12932:                     push(@{$changes{'cancreate'}},'notify');
12933:                 }
12934:             }
12935:         } elsif ($cancreate{'notify'}{'approval'}) {
12936:             push(@{$changes{'cancreate'}},'notify');
12937:         }
12938: 
12939:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
12940:     }
12941: #  
12942: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
12943: # institutional log-in.
12944: #
12945:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
12946:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
12947:                ($domdefaults{'auth_def'} eq 'localauth'))) {
12948:             $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.').' '.
12949:                           &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.');
12950:         }
12951:     }
12952:     my @fields = ('lastname','firstname','middlename','generation',
12953:                   'permanentemail','id');
12954:     my @shibfields = (@fields,'inststatus');
12955:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
12956: #
12957: # Where usernames may created for institutional log-in and/or institutional single sign on:
12958: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
12959: # may self-create accounts 
12960: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
12961: # which the user may supply, if institutional data is unavailable.
12962: #
12963:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
12964:         if (@types) {
12965:             @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
12966:             push(@contexts,'statustocreate');
12967:             foreach my $type (@types) {
12968:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
12969:                 foreach my $field (@fields) {
12970:                     if (grep(/^\Q$field\E$/,@modifiable)) {
12971:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
12972:                     } else {
12973:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
12974:                     }
12975:                 }
12976:             }
12977:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
12978:                 foreach my $type (@types) {
12979:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
12980:                         foreach my $field (@fields) {
12981:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
12982:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
12983:                                 push(@{$changes{'selfcreate'}},$type);
12984:                                 last;
12985:                             }
12986:                         }
12987:                     }
12988:                 }
12989:             } else {
12990:                 foreach my $type (@types) {
12991:                     push(@{$changes{'selfcreate'}},$type);
12992:                 }
12993:             }
12994:         }
12995:         foreach my $field (@shibfields) {
12996:             if ($env{'form.shibenv_'.$field} ne '') {
12997:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
12998:             }
12999:         }
13000:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
13001:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
13002:                 foreach my $field (@shibfields) {
13003:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
13004:                         push(@{$changes{'cancreate'}},'shibenv');
13005:                     }
13006:                 }
13007:             } else {
13008:                 foreach my $field (@shibfields) {
13009:                     if ($env{'form.shibenv_'.$field}) {
13010:                         push(@{$changes{'cancreate'}},'shibenv');
13011:                         last;
13012:                     }
13013:                 }
13014:             }
13015:         }
13016:     }
13017:     foreach my $item (@contexts) {
13018:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
13019:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
13020:                 if (ref($cancreate{$item}) eq 'ARRAY') {
13021:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
13022:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13023:                             push(@{$changes{'cancreate'}},$item);
13024:                         }
13025:                     }
13026:                 }
13027:             }
13028:             if (ref($cancreate{$item}) eq 'ARRAY') {
13029:                 foreach my $type (@{$cancreate{$item}}) {
13030:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
13031:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13032:                             push(@{$changes{'cancreate'}},$item);
13033:                         }
13034:                     }
13035:                 }
13036:             }
13037:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
13038:             if (ref($cancreate{$item}) eq 'HASH') {
13039:                 foreach my $type (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
13040:                     if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
13041:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$type}})) {
13042:                             unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
13043:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13044:                                     push(@{$changes{'cancreate'}},$item);
13045:                                 }
13046:                             }
13047:                         }
13048:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
13049:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
13050:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13051:                                 push(@{$changes{'cancreate'}},$item);
13052:                             }
13053:                         }
13054:                     }
13055:                 }
13056:                 foreach my $type (keys(%{$cancreate{$item}})) {
13057:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
13058:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
13059:                             if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
13060:                                 unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
13061:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13062:                                         push(@{$changes{'cancreate'}},$item);
13063:                                     }
13064:                                 }
13065:                             } else {
13066:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13067:                                     push(@{$changes{'cancreate'}},$item);
13068:                                 }
13069:                             }
13070:                         }
13071:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
13072:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
13073:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13074:                                 push(@{$changes{'cancreate'}},$item);
13075:                             }
13076:                         }
13077:                     }
13078:                 }
13079:             }
13080:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
13081:             if (ref($cancreate{$item}) eq 'ARRAY') {
13082:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
13083:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13084:                         push(@{$changes{'cancreate'}},$item);
13085:                     }
13086:                 }
13087:             }
13088:         } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
13089:             if (ref($cancreate{$item}) eq 'HASH') {
13090:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13091:                     push(@{$changes{'cancreate'}},$item);
13092:                 }
13093:             }
13094:         } elsif ($item eq 'emailusername') {
13095:             if (ref($cancreate{$item}) eq 'HASH') {
13096:                 foreach my $type (keys(%{$cancreate{$item}})) {
13097:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
13098:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
13099:                             if ($cancreate{$item}{$type}{$field}) {
13100:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13101:                                     push(@{$changes{'cancreate'}},$item);
13102:                                 }
13103:                                 last;
13104:                             }
13105:                         }
13106:                     }
13107:                 }
13108:             }
13109:         }
13110:     }
13111: #
13112: # Populate %save_usercreate hash with updates to self-creation configuration.
13113: #
13114:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
13115:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
13116:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
13117:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
13118:     if (ref($cancreate{'notify'}) eq 'HASH') {
13119:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
13120:     }
13121:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
13122:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
13123:     }
13124:     if (ref($cancreate{'emailverified'}) eq 'HASH') {
13125:         $save_usercreate{'cancreate'}{'emailverified'} = $cancreate{'emailverified'};
13126:     }
13127:     if (ref($cancreate{'emailoptions'}) eq 'HASH') {
13128:         $save_usercreate{'cancreate'}{'emailoptions'} = $cancreate{'emailoptions'};
13129:     }
13130:     if (ref($cancreate{'emaildomain'}) eq 'HASH') {
13131:         $save_usercreate{'cancreate'}{'emaildomain'} = $cancreate{'emaildomain'};
13132:     }
13133:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
13134:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
13135:     }
13136:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
13137:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
13138:     }
13139:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
13140:     $save_usercreate{'email_rule'} = \%email_rule;
13141: 
13142:     my %userconfig_hash = (
13143:             usercreation     => \%save_usercreate,
13144:             usermodification => \%save_usermodify,
13145:             inststatus       => \%save_inststatus,
13146:     );
13147: 
13148:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
13149:                                              $dom);
13150: #
13151: # Accumulate details of changes to domain configuration for self-creation of usernames in $resulttext
13152: #
13153:     if ($putresult eq 'ok') {
13154:         if (keys(%changes) > 0) {
13155:             $resulttext = &mt('Changes made:').'<ul>';
13156:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
13157:                 my %lt = &selfcreation_types();
13158:                 foreach my $type (@{$changes{'cancreate'}}) {
13159:                     my $chgtext = '';
13160:                     if ($type eq 'selfcreate') {
13161:                         if (@{$cancreate{$type}} == 0) {
13162:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
13163:                         } else {
13164:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
13165:                                         '<ul>';
13166:                             foreach my $case (@{$cancreate{$type}}) {
13167:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
13168:                             }
13169:                             $chgtext .= '</ul>';
13170:                             if (ref($cancreate{$type}) eq 'ARRAY') {
13171:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
13172:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
13173:                                         if (@{$cancreate{'statustocreate'}} == 0) {
13174:                                             $chgtext .= '<span class="LC_warning">'.
13175:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts via log-in or single sign-on.").
13176:                                                         '</span><br />';
13177:                                         }
13178:                                     }
13179:                                 }
13180:                                 if (grep(/^email$/,@{$cancreate{$type}})) {
13181:                                     if (!@statuses) {
13182:                                         $chgtext .= '<span class="LC_warning">'.
13183:                                                     &mt("However, e-mail verification is currently set to 'unavailable' for all user types (including 'other'), so self-creation of accounts is not possible for non-institutional log-in.").
13184:                                                     '</span><br />';
13185: 
13186:                                     }
13187:                                 }
13188:                             }
13189:                         }
13190:                     } elsif ($type eq 'shibenv') {
13191:                         if (keys(%{$cancreate{$type}}) == 0) {
13192:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information').'<br />'; 
13193:                         } else {
13194:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
13195:                                         '<ul>';
13196:                             foreach my $field (@shibfields) {
13197:                                 next if ($cancreate{$type}{$field} eq '');
13198:                                 if ($field eq 'inststatus') {
13199:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
13200:                                 } else {
13201:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
13202:                                 }
13203:                             }
13204:                             $chgtext .= '</ul>';
13205:                         }
13206:                     } elsif ($type eq 'statustocreate') {
13207:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
13208:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
13209:                             if (@{$cancreate{'selfcreate'}} > 0) {
13210:                                 if (@{$cancreate{'statustocreate'}} == 0) {
13211:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
13212:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
13213:                                         $chgtext .= '<br />'.
13214:                                                     '<span class="LC_warning">'.
13215:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
13216:                                                     '</span>';
13217:                                     }
13218:                                 } elsif (keys(%usertypes) > 0) {
13219:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
13220:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
13221:                                     } else {
13222:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
13223:                                     }
13224:                                     $chgtext .= '<ul>';
13225:                                     foreach my $case (@{$cancreate{$type}}) {
13226:                                         if ($case eq 'default') {
13227:                                             $chgtext .= '<li>'.$othertitle.'</li>';
13228:                                         } else {
13229:                                             $chgtext .= '<li>'.$usertypes{$case}.'</li>';
13230:                                         }
13231:                                     }
13232:                                     $chgtext .= '</ul>';
13233:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
13234:                                         $chgtext .= '<span class="LC_warning">'.
13235:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
13236:                                                     '</span>';
13237:                                     }
13238:                                 }
13239:                             } else {
13240:                                 if (@{$cancreate{$type}} == 0) {
13241:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
13242:                                 } else {
13243:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
13244:                                 }
13245:                             }
13246:                             $chgtext .= '<br />';
13247:                         }
13248:                     } elsif ($type eq 'selfcreateprocessing') {
13249:                         my %choices = &Apache::lonlocal::texthash (
13250:                                                                     automatic => 'Automatic approval',
13251:                                                                     approval  => 'Queued for approval',
13252:                                                                   );
13253:                         if (@types) {
13254:                             if (@statuses) {
13255:                                 $chgtext .= &mt('Processing of requests to create account with e-mail verification set as follows:').
13256:                                             '<ul>';
13257:                                 foreach my $status (@statuses) {
13258:                                     if ($status eq 'default') {
13259:                                         $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
13260:                                     } else {
13261:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
13262:                                     }
13263:                                 }
13264:                                 $chgtext .= '</ul>';
13265:                             }
13266:                         } else {
13267:                             $chgtext .= &mt('Processing of requests to create account with e-mail verification set to: "[_1]"',
13268:                                             $choices{$cancreate{'selfcreateprocessing'}{'default'}});
13269:                         }
13270:                     } elsif ($type eq 'emailverified') {
13271:                         my %options = &Apache::lonlocal::texthash (
13272:                                                                     all   => 'Same as e-mail',
13273:                                                                     first => 'Omit @domain',
13274:                                                                     free  => 'Free to choose',
13275:                                                                   );
13276:                         if (@types) {
13277:                             if (@statuses) {
13278:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, username is set as follows:').
13279:                                             '<ul>';
13280:                                 foreach my $status (@statuses) {
13281:                                     if ($status eq 'default') {
13282:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
13283:                                     } else {
13284:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
13285:                                     }
13286:                                 }
13287:                                 $chgtext .= '</ul>';
13288:                             }
13289:                         } else {
13290:                             $chgtext .= &mt("For self-created accounts verified by e-mail address, user's username is: '[_1]'",
13291:                                             $options{$cancreate{'emailverified'}{'default'}});
13292:                         }
13293:                     } elsif ($type eq 'emailoptions') {
13294:                         my %options = &Apache::lonlocal::texthash (
13295:                                                                     any     => 'Any e-mail',
13296:                                                                     inst    => 'Institutional only',
13297:                                                                     noninst => 'Non-institutional only',
13298:                                                                     custom  => 'Custom restrictions',
13299:                                                                   );
13300:                         if (@types) {
13301:                             if (@statuses) {
13302:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, requirements for e-mail address are as follows:').
13303:                                             '<ul>';
13304:                                 foreach my $status (@statuses) {
13305:                                     if ($type eq 'default') {
13306:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
13307:                                     } else {
13308:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
13309:                                     }
13310:                                 }
13311:                                 $chgtext .= '</ul>';
13312:                             }
13313:                         } else {
13314:                             if ($cancreate{'emailoptions'}{'default'} eq 'any') {
13315:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, any e-mail may be used');
13316:                             } else {
13317:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, e-mail restricted to: "[_1]"',
13318:                                                 $options{$cancreate{'emailoptions'}{'default'}});
13319:                             }
13320:                         }
13321:                     } elsif ($type eq 'emaildomain') {
13322:                         my $output;
13323:                         if (@statuses) {
13324:                             foreach my $type (@statuses) {
13325:                                 if (ref($cancreate{'emaildomain'}{$type}) eq 'HASH') {
13326:                                     if ($cancreate{'emailoptions'}{$type} eq 'inst') {
13327:                                         if ($type eq 'default') {
13328:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
13329:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
13330:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
13331:                                             } else {
13332:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address needs to end: [_1]",
13333:                                                                                         $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
13334:                                             }
13335:                                         } else {
13336:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
13337:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
13338:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
13339:                                             } else {
13340:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address needs to end: [_1]",
13341:                                                                                               $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
13342:                                             }
13343:                                         }
13344:                                     } elsif ($cancreate{'emailoptions'}{$type} eq 'noninst') {
13345:                                         if ($type eq 'default') {
13346:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
13347:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
13348:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
13349:                                             } else {
13350:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address must not end: [_1]",
13351:                                                                                         $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
13352:                                             }
13353:                                         } else {
13354:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
13355:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
13356:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
13357:                                             } else {
13358:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address must not end: [_1]",
13359:                                                                                                 $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
13360:                                             }
13361:                                         }
13362:                                     }
13363:                                 }
13364:                             }
13365:                         }
13366:                         if ($output ne '') {
13367:                             $chgtext .= &mt('For self-created accounts verified by e-mail address:').
13368:                                         '<ul>'.$output.'</ul>';
13369:                         }
13370:                     } elsif ($type eq 'captcha') {
13371:                         if ($savecaptcha{$type} eq 'notused') {
13372:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
13373:                         } else {
13374:                             my %captchas = &captcha_phrases();
13375:                             if ($captchas{$savecaptcha{$type}}) {
13376:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
13377:                             } else {
13378:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
13379:                             }
13380:                         }
13381:                     } elsif ($type eq 'recaptchakeys') {
13382:                         my ($privkey,$pubkey);
13383:                         if (ref($savecaptcha{$type}) eq 'HASH') {
13384:                             $pubkey = $savecaptcha{$type}{'public'};
13385:                             $privkey = $savecaptcha{$type}{'private'};
13386:                         }
13387:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
13388:                         if (!$pubkey) {
13389:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
13390:                         } else {
13391:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
13392:                         }
13393:                         if (!$privkey) {
13394:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
13395:                         } else {
13396:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
13397:                         }
13398:                         $chgtext .= '</ul>';
13399:                     } elsif ($type eq 'recaptchaversion') {
13400:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
13401:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
13402:                         }
13403:                     } elsif ($type eq 'emailusername') {
13404:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
13405:                             if (@statuses) {
13406:                                 foreach my $type (@statuses) {
13407:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
13408:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
13409:                                             $chgtext .= &mt('When self-creating account with e-mail verification, the following information will be provided by [_1]:',"'$usertypes{$type}'").
13410:                                                     '<ul>';
13411:                                             foreach my $field (@{$infofields}) {
13412:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
13413:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
13414:                                                 }
13415:                                             }
13416:                                             $chgtext .= '</ul>';
13417:                                         } else {
13418:                                             $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
13419:                                         }
13420:                                     } else {
13421:                                         $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
13422:                                     }
13423:                                 }
13424:                             }
13425:                         }
13426:                     } elsif ($type eq 'notify') {
13427:                         my $numapprove = 0;
13428:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
13429:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
13430:                                 if ($cancreate{'notify'}{'approval'}) {
13431:                                     $chgtext .= &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
13432:                                     $numapprove ++;
13433:                                 }
13434:                             }
13435:                         }
13436:                         unless ($numapprove) {
13437:                             $chgtext .= &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
13438:                         }
13439:                     }
13440:                     if ($chgtext) {
13441:                         $resulttext .= '<li>'.$chgtext.'</li>';
13442:                     }
13443:                 }
13444:             }
13445:             if ((ref($changes{'email_rule'}) eq 'ARRAY') && (@{$changes{'email_rule'}} > 0)) {
13446:                 my ($emailrules,$emailruleorder) =
13447:                     &Apache::lonnet::inst_userrules($dom,'email');
13448:                 foreach my $type (@{$changes{'email_rule'}}) {
13449:                     if (ref($email_rule{$type}) eq 'ARRAY') {
13450:                         my $chgtext = '<ul>';
13451:                         foreach my $rule (@{$email_rule{$type}}) {
13452:                             if (ref($emailrules->{$rule}) eq 'HASH') {
13453:                                 $chgtext .= '<li>'.$emailrules->{$rule}{'name'}.'</li>';
13454:                             }
13455:                         }
13456:                         $chgtext .= '</ul>';
13457:                         my $typename;
13458:                         if (@types) {
13459:                             if ($type eq 'default') {
13460:                                 $typename = $othertitle;
13461:                             } else {
13462:                                 $typename = $usertypes{$type};
13463:                             }
13464:                             $chgtext .= &mt('(Affiliation: [_1])',$typename);
13465:                         }
13466:                         if (@{$email_rule{$type}} > 0) {
13467:                             $resulttext .= '<li>'.
13468:                                            &mt('Accounts may not be created by users verified by e-mail, for e-mail addresses of the following types: ',
13469:                                                $usertypes{$type}).
13470:                                            $chgtext.
13471:                                            '</li>';
13472:                         } else {
13473:                             $resulttext .= '<li>'.
13474:                                            &mt('There are now no restrictions on e-mail addresses which may be used for verification when a user requests an account.').
13475:                                            '</li>'.
13476:                                            &mt('(Affiliation: [_1])',$typename);
13477:                         }
13478:                     }
13479:                 }
13480:             }
13481:             if (ref($changes{'inststatus'}) eq 'ARRAY') {
13482:                 if (ref($save_inststatus{'inststatusguest'}) eq 'ARRAY') {
13483:                     if (@{$save_inststatus{'inststatusguest'}} > 0) {
13484:                         my $chgtext = '<ul>';
13485:                         foreach my $type (@{$save_inststatus{'inststatusguest'}}) {
13486:                             $chgtext .= '<li>'.$usertypes{$type}.'</li>';
13487:                         }
13488:                         $chgtext .= '</ul>';
13489:                         $resulttext .= '<li>'.
13490:                                        &mt('A user will self-report one of the following affiliations when requesting an account verified by e-mail: ').
13491:                                           $chgtext.
13492:                                        '</li>';
13493:                     } else {
13494:                         $resulttext .= '<li>'.
13495:                                        &mt('No affiliations available for self-reporting when requesting an account verified by e-mail.').
13496:                                        '</li>';
13497:                     }
13498:                 }
13499:             }
13500:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
13501:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
13502:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
13503:                 foreach my $type (@{$changes{'selfcreate'}}) {
13504:                     my $typename = $type;
13505:                     if (keys(%usertypes) > 0) {
13506:                         if ($usertypes{$type} ne '') {
13507:                             $typename = $usertypes{$type};
13508:                         }
13509:                     }
13510:                     my @modifiable;
13511:                     $resulttext .= '<li>'.
13512:                                     &mt('Self-creation of account by users with status: [_1]',
13513:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
13514:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
13515:                     foreach my $field (@fields) {
13516:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
13517:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
13518:                         }
13519:                     }
13520:                     if (@modifiable > 0) {
13521:                         $resulttext .= join(', ',@modifiable);
13522:                     } else {
13523:                         $resulttext .= &mt('none');
13524:                     }
13525:                     $resulttext .= '</li>';
13526:                 }
13527:                 $resulttext .= '</ul></li>';
13528:             }
13529:             $resulttext .= '</ul>';
13530:             my $cachetime = 24*60*60;
13531:             $domdefaults{'inststatusguest'} = $save_inststatus{'inststatusguest'};
13532:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13533:             if (ref($lastactref) eq 'HASH') {
13534:                 $lastactref->{'domdefaults'} = 1;
13535:             }
13536:         } else {
13537:             $resulttext = &mt('No changes made to self-creation settings');
13538:         }
13539:     } else {
13540:         $resulttext = '<span class="LC_error">'.
13541:             &mt('An error occurred: [_1]',$putresult).'</span>';
13542:     }
13543:     if ($warningmsg ne '') {
13544:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
13545:     }
13546:     return $resulttext;
13547: }
13548: 
13549: sub process_captcha {
13550:     my ($container,$changes,$newsettings,$currsettings) = @_;
13551:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH'));
13552:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
13553:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
13554:         $newsettings->{'captcha'} = 'original';
13555:     }
13556:     my %current;
13557:     if (ref($currsettings) eq 'HASH') {
13558:         %current = %{$currsettings};
13559:     }
13560:     if ($current{'captcha'} ne $newsettings->{'captcha'}) {
13561:         if ($container eq 'cancreate') {
13562:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
13563:                 push(@{$changes->{'cancreate'}},'captcha');
13564:             } elsif (!defined($changes->{'cancreate'})) {
13565:                 $changes->{'cancreate'} = ['captcha'];
13566:             }
13567:         } elsif ($container eq 'passwords') {
13568:             $changes->{'reset'} = 1;
13569:         } else {
13570:             $changes->{'captcha'} = 1;
13571:         }
13572:     }
13573:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
13574:     if ($newsettings->{'captcha'} eq 'recaptcha') {
13575:         $newpub = $env{'form.'.$container.'_recaptchapub'};
13576:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
13577:         $newpub =~ s/[^\w\-]//g;
13578:         $newpriv =~ s/[^\w\-]//g;
13579:         $newsettings->{'recaptchakeys'} = {
13580:                                              public  => $newpub,
13581:                                              private => $newpriv,
13582:                                           };
13583:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
13584:         $newversion =~ s/\D//g;
13585:         if ($newversion ne '2') {
13586:             $newversion = 1;
13587:         }
13588:         $newsettings->{'recaptchaversion'} = $newversion;
13589:     }
13590:     if (ref($current{'recaptchakeys'}) eq 'HASH') {
13591:         $currpub = $current{'recaptchakeys'}{'public'};
13592:         $currpriv = $current{'recaptchakeys'}{'private'};
13593:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
13594:             $newsettings->{'recaptchakeys'} = {
13595:                                                  public  => '',
13596:                                                  private => '',
13597:                                               }
13598:         }
13599:     }
13600:     if ($current{'captcha'} eq 'recaptcha') {
13601:         $currversion = $current{'recaptchaversion'};
13602:         if ($currversion ne '2') {
13603:             $currversion = 1;
13604:         }
13605:     }
13606:     if ($currversion ne $newversion) {
13607:         if ($container eq 'cancreate') {
13608:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
13609:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
13610:             } elsif (!defined($changes->{'cancreate'})) {
13611:                 $changes->{'cancreate'} = ['recaptchaversion'];
13612:             }
13613:         } elsif ($container eq 'passwords') {
13614:             $changes->{'reset'} = 1;
13615:         } else {
13616:             $changes->{'recaptchaversion'} = 1;
13617:         }
13618:     }
13619:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
13620:         if ($container eq 'cancreate') {
13621:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
13622:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
13623:             } elsif (!defined($changes->{'cancreate'})) {
13624:                 $changes->{'cancreate'} = ['recaptchakeys'];
13625:             }
13626:         } elsif ($container eq 'passwords') {
13627:             $changes->{'reset'} = 1;
13628:         } else {
13629:             $changes->{'recaptchakeys'} = 1;
13630:         }
13631:     }
13632:     return;
13633: }
13634: 
13635: sub modify_usermodification {
13636:     my ($dom,%domconfig) = @_;
13637:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
13638:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
13639:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
13640:             if ($key eq 'selfcreate') {
13641:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
13642:             } else {  
13643:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
13644:             }
13645:         }
13646:     }
13647:     my @contexts = ('author','course');
13648:     my %context_title = (
13649:                            author => 'In author context',
13650:                            course => 'In course context',
13651:                         );
13652:     my @fields = ('lastname','firstname','middlename','generation',
13653:                   'permanentemail','id');
13654:     my %roles = (
13655:                   author => ['ca','aa'],
13656:                   course => ['st','ep','ta','in','cr'],
13657:                 );
13658:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
13659:     foreach my $context (@contexts) {
13660:         foreach my $role (@{$roles{$context}}) {
13661:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
13662:             foreach my $item (@fields) {
13663:                 if (grep(/^\Q$item\E$/,@modifiable)) {
13664:                     $modifyhash{$context}{$role}{$item} = 1;
13665:                 } else {
13666:                     $modifyhash{$context}{$role}{$item} = 0;
13667:                 }
13668:             }
13669:         }
13670:         if (ref($curr_usermodification{$context}) eq 'HASH') {
13671:             foreach my $role (@{$roles{$context}}) {
13672:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
13673:                     foreach my $field (@fields) {
13674:                         if ($modifyhash{$context}{$role}{$field} ne 
13675:                                 $curr_usermodification{$context}{$role}{$field}) {
13676:                             push(@{$changes{$context}},$role);
13677:                             last;
13678:                         }
13679:                     }
13680:                 }
13681:             }
13682:         } else {
13683:             foreach my $context (@contexts) {
13684:                 foreach my $role (@{$roles{$context}}) {
13685:                     push(@{$changes{$context}},$role);
13686:                 }
13687:             }
13688:         }
13689:     }
13690:     my %usermodification_hash =  (
13691:                                    usermodification => \%modifyhash,
13692:                                  );
13693:     my $putresult = &Apache::lonnet::put_dom('configuration',
13694:                                              \%usermodification_hash,$dom);
13695:     if ($putresult eq 'ok') {
13696:         if (keys(%changes) > 0) {
13697:             $resulttext = &mt('Changes made: ').'<ul>';
13698:             foreach my $context (@contexts) {
13699:                 if (ref($changes{$context}) eq 'ARRAY') {
13700:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
13701:                     if (ref($changes{$context}) eq 'ARRAY') {
13702:                         foreach my $role (@{$changes{$context}}) {
13703:                             my $rolename;
13704:                             if ($role eq 'cr') {
13705:                                 $rolename = &mt('Custom');
13706:                             } else {
13707:                                 $rolename = &Apache::lonnet::plaintext($role);
13708:                             }
13709:                             my @modifiable;
13710:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
13711:                             foreach my $field (@fields) {
13712:                                 if ($modifyhash{$context}{$role}{$field}) {
13713:                                     push(@modifiable,$fieldtitles{$field});
13714:                                 }
13715:                             }
13716:                             if (@modifiable > 0) {
13717:                                 $resulttext .= join(', ',@modifiable);
13718:                             } else {
13719:                                 $resulttext .= &mt('none'); 
13720:                             }
13721:                             $resulttext .= '</li>';
13722:                         }
13723:                         $resulttext .= '</ul></li>';
13724:                     }
13725:                 }
13726:             }
13727:             $resulttext .= '</ul>';
13728:         } else {
13729:             $resulttext = &mt('No changes made to user modification settings');
13730:         }
13731:     } else {
13732:         $resulttext = '<span class="LC_error">'.
13733:             &mt('An error occurred: [_1]',$putresult).'</span>';
13734:     }
13735:     return $resulttext;
13736: }
13737: 
13738: sub modify_defaults {
13739:     my ($dom,$lastactref,%domconfig) = @_;
13740:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
13741:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13742:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
13743:                  'portal_def');
13744:     my @authtypes = ('internal','krb4','krb5','localauth');
13745:     foreach my $item (@items) {
13746:         $newvalues{$item} = $env{'form.'.$item};
13747:         if ($item eq 'auth_def') {
13748:             if ($newvalues{$item} ne '') {
13749:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
13750:                     push(@errors,$item);
13751:                 }
13752:             }
13753:         } elsif ($item eq 'lang_def') {
13754:             if ($newvalues{$item} ne '') {
13755:                 if ($newvalues{$item} =~ /^(\w+)/) {
13756:                     my $langcode = $1;
13757:                     if ($langcode ne 'x_chef') {
13758:                         if (code2language($langcode) eq '') {
13759:                             push(@errors,$item);
13760:                         }
13761:                     }
13762:                 } else {
13763:                     push(@errors,$item);
13764:                 }
13765:             }
13766:         } elsif ($item eq 'timezone_def') {
13767:             if ($newvalues{$item} ne '') {
13768:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
13769:                     push(@errors,$item);   
13770:                 }
13771:             }
13772:         } elsif ($item eq 'datelocale_def') {
13773:             if ($newvalues{$item} ne '') {
13774:                 my @datelocale_ids = DateTime::Locale->ids();
13775:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
13776:                     push(@errors,$item);
13777:                 }
13778:             }
13779:         } elsif ($item eq 'portal_def') {
13780:             if ($newvalues{$item} ne '') {
13781:                 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])\/?$/) {
13782:                     push(@errors,$item);
13783:                 }
13784:             }
13785:         }
13786:         if (grep(/^\Q$item\E$/,@errors)) {
13787:             $newvalues{$item} = $domdefaults{$item};
13788:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
13789:             $changes{$item} = 1;
13790:         }
13791:         $domdefaults{$item} = $newvalues{$item};
13792:     }
13793:     my %staticdefaults = (
13794:                            'intauth_cost'   => 10,
13795:                            'intauth_check'  => 0,
13796:                            'intauth_switch' => 0,
13797:                          );
13798:     foreach my $item ('intauth_cost','intauth_check','intauth_switch') {
13799:         if (exists($domdefaults{$item})) {
13800:             $newvalues{$item} = $domdefaults{$item};
13801:         } else {
13802:             $newvalues{$item} = $staticdefaults{$item};
13803:         }
13804:     }
13805:     my %defaults_hash = (
13806:                          defaults => \%newvalues,
13807:                         );
13808:     my $title = &defaults_titles();
13809: 
13810:     my $currinststatus;
13811:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
13812:         $currinststatus = $domconfig{'inststatus'};
13813:     } else {
13814:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
13815:         $currinststatus = {
13816:                              inststatustypes => $usertypes,
13817:                              inststatusorder => $types,
13818:                              inststatusguest => [],
13819:                           };
13820:     }
13821:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
13822:     my @allpos;
13823:     my %alltypes;
13824:     my @inststatusguest;
13825:     if (ref($currinststatus) eq 'HASH') {
13826:         if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
13827:             foreach my $type (@{$currinststatus->{'inststatusguest'}}) {
13828:                 unless (grep(/^\Q$type\E$/,@todelete)) {
13829:                     push(@inststatusguest,$type);
13830:                 }
13831:             }
13832:         }
13833:     }
13834:     my ($currtitles,$currorder);
13835:     if (ref($currinststatus) eq 'HASH') {
13836:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
13837:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
13838:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
13839:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
13840:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
13841:                     }
13842:                 }
13843:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
13844:                     my $position = $env{'form.inststatus_pos_'.$type};
13845:                     $position =~ s/\D+//g;
13846:                     $allpos[$position] = $type;
13847:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
13848:                     $alltypes{$type} =~ s/`//g;
13849:                 }
13850:             }
13851:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
13852:             $currtitles =~ s/,$//;
13853:         }
13854:     }
13855:     if ($env{'form.addinststatus'}) {
13856:         my $newtype = $env{'form.addinststatus'};
13857:         $newtype =~ s/\W//g;
13858:         unless (exists($alltypes{$newtype})) {
13859:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
13860:             $alltypes{$newtype} =~ s/`//g; 
13861:             my $position = $env{'form.addinststatus_pos'};
13862:             $position =~ s/\D+//g;
13863:             if ($position ne '') {
13864:                 $allpos[$position] = $newtype;
13865:             }
13866:         }
13867:     }
13868:     my @orderedstatus;
13869:     foreach my $type (@allpos) {
13870:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
13871:             push(@orderedstatus,$type);
13872:         }
13873:     }
13874:     foreach my $type (keys(%alltypes)) {
13875:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
13876:             delete($alltypes{$type});
13877:         }
13878:     }
13879:     $defaults_hash{'inststatus'} = {
13880:                                      inststatustypes => \%alltypes,
13881:                                      inststatusorder => \@orderedstatus,
13882:                                      inststatusguest => \@inststatusguest,
13883:                                    };
13884:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
13885:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
13886:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
13887:         }
13888:     }
13889:     if ($currorder ne join(',',@orderedstatus)) {
13890:         $changes{'inststatus'}{'inststatusorder'} = 1;
13891:     }
13892:     my $newtitles;
13893:     foreach my $item (@orderedstatus) {
13894:         $newtitles .= $alltypes{$item}.',';
13895:     }
13896:     $newtitles =~ s/,$//;
13897:     if ($currtitles ne $newtitles) {
13898:         $changes{'inststatus'}{'inststatustypes'} = 1;
13899:     }
13900:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
13901:                                              $dom);
13902:     if ($putresult eq 'ok') {
13903:         if (keys(%changes) > 0) {
13904:             $resulttext = &mt('Changes made:').'<ul>';
13905:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
13906:             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";
13907:             foreach my $item (sort(keys(%changes))) {
13908:                 if ($item eq 'inststatus') {
13909:                     if (ref($changes{'inststatus'}) eq 'HASH') {
13910:                         if (@orderedstatus) {
13911:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
13912:                             foreach my $type (@orderedstatus) { 
13913:                                 $resulttext .= $alltypes{$type}.', ';
13914:                             }
13915:                             $resulttext =~ s/, $//;
13916:                             $resulttext .= '</li>';
13917:                         } else {
13918:                             $resulttext .= '<li>'.&mt('Institutional user status types deleted').'</li>';
13919:                         }
13920:                     }
13921:                 } else {
13922:                     my $value = $env{'form.'.$item};
13923:                     if ($value eq '') {
13924:                         $value = &mt('none');
13925:                     } elsif ($item eq 'auth_def') {
13926:                         my %authnames = &authtype_names();
13927:                         my %shortauth = (
13928:                                           internal   => 'int',
13929:                                           krb4       => 'krb4',
13930:                                           krb5       => 'krb5',
13931:                                           localauth  => 'loc',
13932:                         );
13933:                         $value = $authnames{$shortauth{$value}};
13934:                     }
13935:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
13936:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
13937:                 }
13938:             }
13939:             $resulttext .= '</ul>';
13940:             $mailmsgtext .= "\n";
13941:             my $cachetime = 24*60*60;
13942:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13943:             if (ref($lastactref) eq 'HASH') {
13944:                 $lastactref->{'domdefaults'} = 1;
13945:             }
13946:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
13947:                 my $notify = 1;
13948:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
13949:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
13950:                         $notify = 0;
13951:                     }
13952:                 }
13953:                 if ($notify) {
13954:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
13955:                                                "LON-CAPA Domain Settings Change - $dom",
13956:                                                $mailmsgtext);
13957:                 }
13958:             }
13959:         } else {
13960:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
13961:         }
13962:     } else {
13963:         $resulttext = '<span class="LC_error">'.
13964:             &mt('An error occurred: [_1]',$putresult).'</span>';
13965:     }
13966:     if (@errors > 0) {
13967:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
13968:         foreach my $item (@errors) {
13969:             $resulttext .= ' "'.$title->{$item}.'",';
13970:         }
13971:         $resulttext =~ s/,$//;
13972:     }
13973:     return $resulttext;
13974: }
13975: 
13976: sub modify_scantron {
13977:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
13978:     my ($resulttext,%confhash,%changes,$errors);
13979:     my $custom = 'custom.tab';
13980:     my $default = 'default.tab';
13981:     my $servadm = $r->dir_config('lonAdmEMail');
13982:     my ($configuserok,$author_ok,$switchserver) =
13983:         &config_check($dom,$confname,$servadm);
13984:     if ($env{'form.scantronformat.filename'} ne '') {
13985:         my $error;
13986:         if ($configuserok eq 'ok') {
13987:             if ($switchserver) {
13988:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
13989:             } else {
13990:                 if ($author_ok eq 'ok') {
13991:                     my ($result,$scantronurl) =
13992:                         &publishlogo($r,'upload','scantronformat',$dom,
13993:                                      $confname,'scantron','','',$custom);
13994:                     if ($result eq 'ok') {
13995:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
13996:                         $changes{'scantronformat'} = 1;
13997:                     } else {
13998:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
13999:                     }
14000:                 } else {
14001:                     $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);
14002:                 }
14003:             }
14004:         } else {
14005:             $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);
14006:         }
14007:         if ($error) {
14008:             &Apache::lonnet::logthis($error);
14009:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
14010:         }
14011:     }
14012:     if (ref($domconfig{'scantron'}) eq 'HASH') {
14013:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
14014:             if ($env{'form.scantronformat_del'}) {
14015:                 $confhash{'scantron'}{'scantronformat'} = '';
14016:                 $changes{'scantronformat'} = 1;
14017:             } else {
14018:                 $confhash{'scantron'}{'scantronformat'} = $domconfig{'scantron'}{'scantronformat'};
14019:             }
14020:         }
14021:     }
14022:     my @options = ('hdr','pad','rem');
14023:     my @fields = &scantroncsv_fields();
14024:     my %titles = &scantronconfig_titles();
14025:     my @formats = &Apache::loncommon::get_env_multiple('form.scantronconfig');
14026:     my ($newdat,$currdat,%newcol,%currcol);
14027:     if (grep(/^dat$/,@formats)) {
14028:         $confhash{'scantron'}{config}{dat} = 1;
14029:         $newdat = 1;
14030:     } else {
14031:         $newdat = 0;
14032:     }
14033:     if (grep(/^csv$/,@formats)) {
14034:         my %bynum;
14035:         foreach my $field (@fields) {
14036:             if ($env{'form.scantronconfig_csv_'.$field} =~ /^(\d+)$/) {
14037:                 my $posscol = $1;
14038:                 if (($posscol < 20) && (!$bynum{$posscol})) {
14039:                     $confhash{'scantron'}{config}{csv}{fields}{$field} = $posscol;
14040:                     $bynum{$posscol} = $field;
14041:                     $newcol{$field} = $posscol;
14042:                 }
14043:             }
14044:         }
14045:         if (keys(%newcol)) {
14046:             foreach my $option (@options) {
14047:                 if ($env{'form.scantroncsv_'.$option}) {
14048:                     $confhash{'scantron'}{config}{csv}{options}{$option} = 1;
14049:                 }
14050:             }
14051:         }
14052:     }
14053:     $currdat = 1;
14054:     if (ref($domconfig{'scantron'}) eq 'HASH') {
14055:         if (ref($domconfig{'scantron'}{'config'}) eq 'HASH') {
14056:             unless (exists($domconfig{'scantron'}{'config'}{'dat'})) {
14057:                 $currdat = 0;
14058:             }
14059:             if (ref($domconfig{'scantron'}{'config'}{'csv'}) eq 'HASH') {
14060:                 if (ref($domconfig{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
14061:                     %currcol = %{$domconfig{'scantron'}{'config'}{'csv'}{'fields'}};
14062:                 }
14063:             }
14064:         }
14065:     }
14066:     if ($currdat != $newdat) {
14067:         $changes{'config'} = 1;
14068:     } else {
14069:         foreach my $field (@fields) {
14070:             if ($currcol{$field} ne '') {
14071:                 if ($currcol{$field} ne $newcol{$field}) {
14072:                     $changes{'config'} = 1;
14073:                     last;
14074:                 }
14075:             } elsif ($newcol{$field} ne '') {
14076:                 $changes{'config'} = 1;
14077:                 last;
14078:             }
14079:         }
14080:     }
14081:     if (keys(%confhash) > 0) {
14082:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
14083:                                                  $dom);
14084:         if ($putresult eq 'ok') {
14085:             if (keys(%changes) > 0) {
14086:                 if (ref($confhash{'scantron'}) eq 'HASH') {
14087:                     $resulttext = &mt('Changes made:').'<ul>';
14088:                     if ($changes{'scantronformat'}) {
14089:                         if ($confhash{'scantron'}{'scantronformat'} eq '') {
14090:                             $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
14091:                         } else {
14092:                             $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
14093:                         }
14094:                     }
14095:                     if ($changes{'config'}) {
14096:                         if (ref($confhash{'scantron'}{'config'}) eq 'HASH') {
14097:                             if ($confhash{'scantron'}{'config'}{'dat'}) {
14098:                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .dat format').'</li>';
14099:                             }
14100:                             if (ref($confhash{'scantron'}{'config'}{'csv'}) eq 'HASH') {
14101:                                 if (ref($confhash{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
14102:                                     if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'fields'}})) {
14103:                                         $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following fields/column numbers supported:').'<ul>';
14104:                                         foreach my $field (@fields) {
14105:                                             if ($confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} ne '') {
14106:                                                 my $showcol = $confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} + 1;
14107:                                                 $resulttext .= '<li>'.$titles{$field}.': '.$showcol.'</li>';
14108:                                             }
14109:                                         }
14110:                                         $resulttext .= '</ul></li>';
14111:                                         if (ref($confhash{'scantron'}{'config'}{'csv'}{'options'}) eq 'HASH') {
14112:                                             if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'options'}})) {
14113:                                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following options:').'<ul>';
14114:                                                 foreach my $option (@options) {
14115:                                                     if ($confhash{'scantron'}{'config'}{'csv'}{'options'}{$option} ne '') {
14116:                                                         $resulttext .= '<li>'.$titles{$option}.'</li>';
14117:                                                     }
14118:                                                 }
14119:                                                 $resulttext .= '</ul></li>';
14120:                                             }
14121:                                         }
14122:                                     }
14123:                                 }
14124:                             }
14125:                         } else {
14126:                             $resulttext .= '<li>'.&mt('No bubblesheet data upload formats set -- will default to assuming .dat format').'</li>';
14127:                         }
14128:                     }
14129:                     $resulttext .= '</ul>';
14130:                 } else {
14131:                     $resulttext = &mt('Changes made to bubblesheet format file.');
14132:                 }
14133:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
14134:                 if (ref($lastactref) eq 'HASH') {
14135:                     $lastactref->{'domainconfig'} = 1;
14136:                 }
14137:             } else {
14138:                 $resulttext = &mt('No changes made to bubblesheet format settings');
14139:             }
14140:         } else {
14141:             $resulttext = '<span class="LC_error">'.
14142:                 &mt('An error occurred: [_1]',$putresult).'</span>';
14143:         }
14144:     } else {
14145:         $resulttext = &mt('No changes made to bubblesheet format settings');
14146:     }
14147:     if ($errors) {
14148:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
14149:                        $errors.'</ul>';
14150:     }
14151:     return $resulttext;
14152: }
14153: 
14154: sub modify_coursecategories {
14155:     my ($dom,$lastactref,%domconfig) = @_;
14156:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
14157:         $cathash);
14158:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
14159:     my @catitems = ('unauth','auth');
14160:     my @cattypes = ('std','domonly','codesrch','none');
14161:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
14162:         $cathash = $domconfig{'coursecategories'}{'cats'};
14163:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
14164:             $changes{'togglecats'} = 1;
14165:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
14166:         }
14167:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
14168:             $changes{'categorize'} = 1;
14169:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
14170:         }
14171:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
14172:             $changes{'togglecatscomm'} = 1;
14173:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
14174:         }
14175:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
14176:             $changes{'categorizecomm'} = 1;
14177:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
14178:         }
14179:         foreach my $item (@catitems) {
14180:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
14181:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
14182:                     $changes{$item} = 1;
14183:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
14184:                 }
14185:             }
14186:         }
14187:     } else {
14188:         $changes{'togglecats'} = 1;
14189:         $changes{'categorize'} = 1;
14190:         $changes{'togglecatscomm'} = 1;
14191:         $changes{'categorizecomm'} = 1;
14192:         $domconfig{'coursecategories'} = {
14193:                                              togglecats => $env{'form.togglecats'},
14194:                                              categorize => $env{'form.categorize'},
14195:                                              togglecatscomm => $env{'form.togglecatscomm'},
14196:                                              categorizecomm => $env{'form.categorizecomm'},
14197:                                          };
14198:         foreach my $item (@catitems) {
14199:             if ($env{'form.coursecat_'.$item} ne 'std') {
14200:                 $changes{$item} = 1;
14201:             }
14202:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
14203:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
14204:             }
14205:         }
14206:     }
14207:     if (ref($cathash) eq 'HASH') {
14208:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
14209:             push (@deletecategory,'instcode::0');
14210:         }
14211:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
14212:             push(@deletecategory,'communities::0');
14213:         }
14214:     }
14215:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
14216:     if (ref($cathash) eq 'HASH') {
14217:         if (@deletecategory > 0) {
14218:             #FIXME Need to remove category from all courses using a deleted category 
14219:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
14220:             foreach my $item (@deletecategory) {
14221:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
14222:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
14223:                     $deletions{$item} = 1;
14224:                     &recurse_cat_deletes($item,$cathash,\%deletions);
14225:                 }
14226:             }
14227:         }
14228:         foreach my $item (keys(%{$cathash})) {
14229:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
14230:             if ($cathash->{$item} ne $env{'form.'.$item}) {
14231:                 $reorderings{$item} = 1;
14232:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
14233:             }
14234:             if ($env{'form.addcategory_name_'.$item} ne '') {
14235:                 my $newcat = $env{'form.addcategory_name_'.$item};
14236:                 my $newdepth = $depth+1;
14237:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
14238:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
14239:                 $adds{$newitem} = 1; 
14240:             }
14241:             if ($env{'form.subcat_'.$item} ne '') {
14242:                 my $newcat = $env{'form.subcat_'.$item};
14243:                 my $newdepth = $depth+1;
14244:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
14245:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
14246:                 $adds{$newitem} = 1;
14247:             }
14248:         }
14249:     }
14250:     if ($env{'form.instcode'} eq '1') {
14251:         if (ref($cathash) eq 'HASH') {
14252:             my $newitem = 'instcode::0';
14253:             if ($cathash->{$newitem} eq '') {  
14254:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
14255:                 $adds{$newitem} = 1;
14256:             }
14257:         } else {
14258:             my $newitem = 'instcode::0';
14259:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
14260:             $adds{$newitem} = 1;
14261:         }
14262:     }
14263:     if ($env{'form.communities'} eq '1') {
14264:         if (ref($cathash) eq 'HASH') {
14265:             my $newitem = 'communities::0';
14266:             if ($cathash->{$newitem} eq '') {
14267:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
14268:                 $adds{$newitem} = 1;
14269:             }
14270:         } else {
14271:             my $newitem = 'communities::0';
14272:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
14273:             $adds{$newitem} = 1;
14274:         }
14275:     }
14276:     if ($env{'form.addcategory_name'} ne '') {
14277:         if (($env{'form.addcategory_name'} ne 'instcode') &&
14278:             ($env{'form.addcategory_name'} ne 'communities')) {
14279:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
14280:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
14281:             $adds{$newitem} = 1;
14282:         }
14283:     }
14284:     my $putresult;
14285:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
14286:         if (keys(%deletions) > 0) {
14287:             foreach my $key (keys(%deletions)) {
14288:                 if ($predelallitems{$key} ne '') {
14289:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
14290:                 }
14291:             }
14292:         }
14293:         my (@chkcats,@chktrails,%chkallitems);
14294:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
14295:         if (ref($chkcats[0]) eq 'ARRAY') {
14296:             my $depth = 0;
14297:             my $chg = 0;
14298:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
14299:                 my $name = $chkcats[0][$i];
14300:                 my $item;
14301:                 if ($name eq '') {
14302:                     $chg ++;
14303:                 } else {
14304:                     $item = &escape($name).'::0';
14305:                     if ($chg) {
14306:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
14307:                     }
14308:                     $depth ++; 
14309:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
14310:                     $depth --;
14311:                 }
14312:             }
14313:         }
14314:     }
14315:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
14316:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
14317:         if ($putresult eq 'ok') {
14318:             my %title = (
14319:                          togglecats     => 'Show/Hide a course in catalog',
14320:                          categorize     => 'Assign a category to a course',
14321:                          togglecatscomm => 'Show/Hide a community in catalog',
14322:                          categorizecomm => 'Assign a category to a community',
14323:                         );
14324:             my %level = (
14325:                          dom  => 'set in Domain ("Modify Course/Community")',
14326:                          crs  => 'set in Course ("Course Configuration")',
14327:                          comm => 'set in Community ("Community Configuration")',
14328:                          none     => 'No catalog',
14329:                          std      => 'Standard catalog',
14330:                          domonly  => 'Domain-only catalog',
14331:                          codesrch => 'Code search form',
14332:                         );
14333:             $resulttext = &mt('Changes made:').'<ul>';
14334:             if ($changes{'togglecats'}) {
14335:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
14336:             }
14337:             if ($changes{'categorize'}) {
14338:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
14339:             }
14340:             if ($changes{'togglecatscomm'}) {
14341:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
14342:             }
14343:             if ($changes{'categorizecomm'}) {
14344:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
14345:             }
14346:             if ($changes{'unauth'}) {
14347:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
14348:             }
14349:             if ($changes{'auth'}) {
14350:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
14351:             }
14352:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
14353:                 my $cathash;
14354:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
14355:                     $cathash = $domconfig{'coursecategories'}{'cats'};
14356:                 } else {
14357:                     $cathash = {};
14358:                 } 
14359:                 my (@cats,@trails,%allitems);
14360:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
14361:                 if (keys(%deletions) > 0) {
14362:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
14363:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
14364:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
14365:                     }
14366:                     $resulttext .= '</ul></li>';
14367:                 }
14368:                 if (keys(%reorderings) > 0) {
14369:                     my %sort_by_trail;
14370:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
14371:                     foreach my $key (keys(%reorderings)) {
14372:                         if ($allitems{$key} ne '') {
14373:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
14374:                         }
14375:                     }
14376:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
14377:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
14378:                     }
14379:                     $resulttext .= '</ul></li>';
14380:                 }
14381:                 if (keys(%adds) > 0) {
14382:                     my %sort_by_trail;
14383:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
14384:                     foreach my $key (keys(%adds)) {
14385:                         if ($allitems{$key} ne '') {
14386:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
14387:                         }
14388:                     }
14389:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
14390:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
14391:                     }
14392:                     $resulttext .= '</ul></li>';
14393:                 }
14394:                 &Apache::lonnet::do_cache_new('cats',$dom,$cathash,3600);
14395:                 if (ref($lastactref) eq 'HASH') {
14396:                     $lastactref->{'cats'} = 1;
14397:                 }
14398:             }
14399:             $resulttext .= '</ul>';
14400:             if ($changes{'unauth'} || $changes{'auth'}) {
14401:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
14402:                 if ($changes{'auth'}) {
14403:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
14404:                 }
14405:                 if ($changes{'unauth'}) {
14406:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
14407:                 }
14408:                 my $cachetime = 24*60*60;
14409:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14410:                 if (ref($lastactref) eq 'HASH') {
14411:                     $lastactref->{'domdefaults'} = 1;
14412:                 }
14413:             }
14414:         } else {
14415:             $resulttext = '<span class="LC_error">'.
14416:                           &mt('An error occurred: [_1]',$putresult).'</span>';
14417:         }
14418:     } else {
14419:         $resulttext = &mt('No changes made to course and community categories');
14420:     }
14421:     return $resulttext;
14422: }
14423: 
14424: sub modify_serverstatuses {
14425:     my ($dom,%domconfig) = @_;
14426:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
14427:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
14428:         %currserverstatus = %{$domconfig{'serverstatuses'}};
14429:     }
14430:     my @pages = &serverstatus_pages();
14431:     foreach my $type (@pages) {
14432:         $newserverstatus{$type}{'namedusers'} = '';
14433:         $newserverstatus{$type}{'machines'} = '';
14434:         if (defined($env{'form.'.$type.'_namedusers'})) {
14435:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
14436:             my @okusers;
14437:             foreach my $user (@users) {
14438:                 my ($uname,$udom) = split(/:/,$user);
14439:                 if (($udom =~ /^$match_domain$/) &&   
14440:                     (&Apache::lonnet::domain($udom)) &&
14441:                     ($uname =~ /^$match_username$/)) {
14442:                     if (!grep(/^\Q$user\E/,@okusers)) {
14443:                         push(@okusers,$user);
14444:                     }
14445:                 }
14446:             }
14447:             if (@okusers > 0) {
14448:                  @okusers = sort(@okusers);
14449:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
14450:             }
14451:         }
14452:         if (defined($env{'form.'.$type.'_machines'})) {
14453:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
14454:             my @okmachines;
14455:             foreach my $ip (@machines) {
14456:                 my @parts = split(/\./,$ip);
14457:                 next if (@parts < 4);
14458:                 my $badip = 0;
14459:                 for (my $i=0; $i<4; $i++) {
14460:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
14461:                         $badip = 1;
14462:                         last;
14463:                     }
14464:                 }
14465:                 if (!$badip) {
14466:                     push(@okmachines,$ip);     
14467:                 }
14468:             }
14469:             @okmachines = sort(@okmachines);
14470:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
14471:         }
14472:     }
14473:     my %serverstatushash =  (
14474:                                 serverstatuses => \%newserverstatus,
14475:                             );
14476:     foreach my $type (@pages) {
14477:         foreach my $setting ('namedusers','machines') {
14478:             my (@current,@new);
14479:             if (ref($currserverstatus{$type}) eq 'HASH') {
14480:                 if ($currserverstatus{$type}{$setting} ne '') { 
14481:                     @current = split(/,/,$currserverstatus{$type}{$setting});
14482:                 }
14483:             }
14484:             if ($newserverstatus{$type}{$setting} ne '') {
14485:                 @new = split(/,/,$newserverstatus{$type}{$setting});
14486:             }
14487:             if (@current > 0) {
14488:                 if (@new > 0) {
14489:                     foreach my $item (@current) {
14490:                         if (!grep(/^\Q$item\E$/,@new)) {
14491:                             $changes{$type}{$setting} = 1;
14492:                             last;
14493:                         }
14494:                     }
14495:                     foreach my $item (@new) {
14496:                         if (!grep(/^\Q$item\E$/,@current)) {
14497:                             $changes{$type}{$setting} = 1;
14498:                             last;
14499:                         }
14500:                     }
14501:                 } else {
14502:                     $changes{$type}{$setting} = 1;
14503:                 }
14504:             } elsif (@new > 0) {
14505:                 $changes{$type}{$setting} = 1;
14506:             }
14507:         }
14508:     }
14509:     if (keys(%changes) > 0) {
14510:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
14511:         my $putresult = &Apache::lonnet::put_dom('configuration',
14512:                                                  \%serverstatushash,$dom);
14513:         if ($putresult eq 'ok') {
14514:             $resulttext .= &mt('Changes made:').'<ul>';
14515:             foreach my $type (@pages) {
14516:                 if (ref($changes{$type}) eq 'HASH') {
14517:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
14518:                     if ($changes{$type}{'namedusers'}) {
14519:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
14520:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
14521:                         } else {
14522:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
14523:                         }
14524:                     }
14525:                     if ($changes{$type}{'machines'}) {
14526:                         if ($newserverstatus{$type}{'machines'} eq '') {
14527:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
14528:                         } else {
14529:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
14530:                         }
14531: 
14532:                     }
14533:                     $resulttext .= '</ul></li>';
14534:                 }
14535:             }
14536:             $resulttext .= '</ul>';
14537:         } else {
14538:             $resulttext = '<span class="LC_error">'.
14539:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
14540: 
14541:         }
14542:     } else {
14543:         $resulttext = &mt('No changes made to access to server status pages');
14544:     }
14545:     return $resulttext;
14546: }
14547: 
14548: sub modify_helpsettings {
14549:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
14550:     my ($resulttext,$errors,%changes,%helphash);
14551:     my %defaultchecked = ('submitbugs' => 'on');
14552:     my @offon = ('off','on');
14553:     my @toggles = ('submitbugs');
14554:     my %current = ('submitbugs' => '',
14555:                    'adhoc'      => {},
14556:                   );
14557:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
14558:         %current = %{$domconfig{'helpsettings'}};
14559:     }
14560:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14561:     foreach my $item (@toggles) {
14562:         if ($defaultchecked{$item} eq 'on') { 
14563:             if ($current{$item} eq '') {
14564:                 if ($env{'form.'.$item} eq '0') {
14565:                     $changes{$item} = 1;
14566:                 }
14567:             } elsif ($current{$item} ne $env{'form.'.$item}) {
14568:                 $changes{$item} = 1;
14569:             }
14570:         } elsif ($defaultchecked{$item} eq 'off') {
14571:             if ($current{$item} eq '') {
14572:                 if ($env{'form.'.$item} eq '1') {
14573:                     $changes{$item} = 1;
14574:                 }
14575:             } elsif ($current{$item} ne $env{'form.'.$item}) {
14576:                 $changes{$item} = 1;
14577:             }
14578:         }
14579:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
14580:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
14581:         }
14582:     }
14583:     my $maxnum = $env{'form.helproles_maxnum'};
14584:     my $confname = $dom.'-domainconfig';
14585:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
14586:     my (@allpos,%newsettings,%changedprivs,$newrole);
14587:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
14588:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
14589:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
14590:     my %lt = &Apache::lonlocal::texthash(
14591:                     s      => 'system',
14592:                     d      => 'domain',
14593:                     order  => 'Display order',
14594:                     access => 'Role usage',
14595:                     all    => 'All with domain helpdesk or helpdesk assistant role',
14596:                     dh     => 'All with domain helpdesk role',
14597:                     da     => 'All with domain helpdesk assistant role',
14598:                     none   => 'None',
14599:                     status => 'Determined based on institutional status',
14600:                     inc    => 'Include all, but exclude specific personnel',
14601:                     exc    => 'Exclude all, but include specific personnel',
14602:     );
14603:     for (my $num=0; $num<=$maxnum; $num++) {
14604:         my ($prefix,$identifier,$rolename,%curr);
14605:         if ($num == $maxnum) {
14606:             next unless ($env{'form.newcusthelp'} == $maxnum);
14607:             $identifier = 'custhelp'.$num;
14608:             $prefix = 'helproles_'.$num;
14609:             $rolename = $env{'form.custhelpname'.$num};
14610:             $rolename=~s/[^A-Za-z0-9]//gs;
14611:             next if ($rolename eq '');
14612:             next if (exists($existing{'rolesdef_'.$rolename}));
14613:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
14614:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
14615:                                                      $newprivs{'c'},$confname,$dom);
14616:             if ($result ne 'ok') {
14617:                 $errors .= '<li><span class="LC_error">'.
14618:                            &mt('An error occurred storing the new custom role: [_1]',
14619:                            $result).'</span></li>';
14620:                 next;
14621:             } else {
14622:                 $changedprivs{$rolename} = \%newprivs;
14623:                 $newrole = $rolename;
14624:             }
14625:         } else {
14626:             $prefix = 'helproles_'.$num;
14627:             $rolename = $env{'form.'.$prefix};
14628:             next if ($rolename eq '');
14629:             next unless (exists($existing{'rolesdef_'.$rolename}));
14630:             $identifier = 'custhelp'.$num;
14631:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
14632:             my %currprivs;
14633:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
14634:                 split(/\_/,$existing{'rolesdef_'.$rolename});
14635:             foreach my $level ('c','d','s') {
14636:                 if ($newprivs{$level} ne $currprivs{$level}) {
14637:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
14638:                                                              $newprivs{'c'},$confname,$dom);
14639:                     if ($result ne 'ok') {
14640:                         $errors .= '<li><span class="LC_error">'.
14641:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
14642:                                        $rolename,$result).'</span></li>';
14643:                     } else {
14644:                         $changedprivs{$rolename} = \%newprivs;
14645:                     }
14646:                     last;
14647:                 }
14648:             }
14649:             if (ref($current{'adhoc'}) eq 'HASH') {
14650:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
14651:                     %curr = %{$current{'adhoc'}{$rolename}};
14652:                 }
14653:             }
14654:         }
14655:         my $newpos = $env{'form.'.$prefix.'_pos'};
14656:         $newpos =~ s/\D+//g;
14657:         $allpos[$newpos] = $rolename;
14658:         my $newdesc = $env{'form.'.$prefix.'_desc'};
14659:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
14660:         if ($curr{'desc'}) {
14661:             if ($curr{'desc'} ne $newdesc) {
14662:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
14663:                 $newsettings{$rolename}{'desc'} = $newdesc;
14664:             }
14665:         } elsif ($newdesc ne '') {
14666:             $changes{'customrole'}{$rolename}{'desc'} = 1;
14667:             $newsettings{$rolename}{'desc'} = $newdesc;
14668:         }
14669:         my $access = $env{'form.'.$prefix.'_access'};
14670:         if (grep(/^\Q$access\E$/,@accesstypes)) {
14671:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
14672:             if ($access eq 'status') {
14673:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
14674:                 if (scalar(@statuses) == 0) {
14675:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
14676:                 } else {
14677:                     my (@shownstatus,$numtypes);
14678:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
14679:                     if (ref($types) eq 'ARRAY') {
14680:                         $numtypes = scalar(@{$types});
14681:                         foreach my $type (sort(@statuses)) {
14682:                             if ($type eq 'default') {
14683:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
14684:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
14685:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
14686:                                 push(@shownstatus,$usertypes->{$type});
14687:                             }
14688:                         }
14689:                     }
14690:                     if (grep(/^default$/,@statuses)) {
14691:                         push(@shownstatus,$othertitle);
14692:                     }
14693:                     if (scalar(@shownstatus) == 1+$numtypes) {
14694:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
14695:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
14696:                     } else {
14697:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
14698:                         if (ref($curr{'status'}) eq 'ARRAY') {
14699:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
14700:                             if (@diffs) {
14701:                                 $changes{'customrole'}{$rolename}{$access} = 1;
14702:                             }
14703:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
14704:                             $changes{'customrole'}{$rolename}{$access} = 1;
14705:                         }
14706:                     }
14707:                 }
14708:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
14709:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
14710:                 my @newspecstaff;
14711:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
14712:                 foreach my $person (sort(@personnel)) {
14713:                     if ($domhelpdesk{$person}) {
14714:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
14715:                     }
14716:                 }
14717:                 if (ref($curr{$access}) eq 'ARRAY') {
14718:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
14719:                     if (@diffs) {
14720:                         $changes{'customrole'}{$rolename}{$access} = 1;
14721:                     }
14722:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
14723:                     $changes{'customrole'}{$rolename}{$access} = 1;
14724:                 }
14725:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
14726:                     my ($uname,$udom) = split(/:/,$person);
14727:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
14728:                 }
14729:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
14730:             }
14731:         } else {
14732:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
14733:         }
14734:         unless ($curr{'access'} eq $access) {
14735:             $changes{'customrole'}{$rolename}{'access'} = 1;
14736:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
14737:         }
14738:     }
14739:     if (@allpos > 0) {
14740:         my $idx = 0;
14741:         foreach my $rolename (@allpos) {
14742:             if ($rolename ne '') {
14743:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
14744:                 if (ref($current{'adhoc'}) eq 'HASH') {
14745:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
14746:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
14747:                             $changes{'customrole'}{$rolename}{'order'} = 1;
14748:                             $newsettings{$rolename}{'order'} = $idx+1;
14749:                         }
14750:                     }
14751:                 }
14752:                 $idx ++;
14753:             }
14754:         }
14755:     }
14756:     my $putresult;
14757:     if (keys(%changes) > 0) {
14758:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
14759:         if ($putresult eq 'ok') {
14760:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
14761:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
14762:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
14763:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
14764:                 }
14765:             }
14766:             my $cachetime = 24*60*60;
14767:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14768:             if (ref($lastactref) eq 'HASH') {
14769:                 $lastactref->{'domdefaults'} = 1;
14770:             }
14771:         } else {
14772:             $errors .= '<li><span class="LC_error">'.
14773:                        &mt('An error occurred storing the settings: [_1]',
14774:                            $putresult).'</span></li>';
14775:         }
14776:     }
14777:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
14778:         $resulttext = &mt('Changes made:').'<ul>';
14779:         my (%shownprivs,@levelorder);
14780:         @levelorder = ('c','d','s');
14781:         if ((keys(%changes)) && ($putresult eq 'ok')) {
14782:             foreach my $item (sort(keys(%changes))) {
14783:                 if ($item eq 'submitbugs') {
14784:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
14785:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
14786:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
14787:                 } elsif ($item eq 'customrole') {
14788:                     if (ref($changes{'customrole'}) eq 'HASH') {
14789:                         my @keyorder = ('order','desc','access','status','exc','inc');
14790:                         my %keytext = &Apache::lonlocal::texthash(
14791:                                                                    order  => 'Order',
14792:                                                                    desc   => 'Role description',
14793:                                                                    access => 'Role usage',
14794:                                                                    status => 'Allowed institutional types',
14795:                                                                    exc    => 'Allowed personnel',
14796:                                                                    inc    => 'Disallowed personnel',
14797:                         );
14798:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
14799:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
14800:                                 if ($role eq $newrole) {
14801:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
14802:                                                               $role).'<ul>';
14803:                                 } else {
14804:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
14805:                                                               $role).'<ul>';
14806:                                 }
14807:                                 foreach my $key (@keyorder) {
14808:                                     if ($changes{'customrole'}{$role}{$key}) {
14809:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
14810:                                                                   $keytext{$key},$newsettings{$role}{$key}).
14811:                                                        '</li>';
14812:                                     }
14813:                                 }
14814:                                 if (ref($changedprivs{$role}) eq 'HASH') {
14815:                                     $shownprivs{$role} = 1;
14816:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
14817:                                     foreach my $level (@levelorder) {
14818:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
14819:                                             next if ($item eq '');
14820:                                             my ($priv) = split(/\&/,$item,2);
14821:                                             if (&Apache::lonnet::plaintext($priv)) {
14822:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
14823:                                                 unless ($level eq 'c') {
14824:                                                     $resulttext .= ' ('.$lt{$level}.')';
14825:                                                 }
14826:                                                 $resulttext .= '</li>';
14827:                                             }
14828:                                         }
14829:                                     }
14830:                                     $resulttext .= '</ul>';
14831:                                 }
14832:                                 $resulttext .= '</ul></li>';
14833:                             }
14834:                         }
14835:                     }
14836:                 }
14837:             }
14838:         }
14839:         if (keys(%changedprivs)) {
14840:             foreach my $role (sort(keys(%changedprivs))) {
14841:                 unless ($shownprivs{$role}) {
14842:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
14843:                                               $role).'<ul>'.
14844:                                    '<li>'.&mt('Privileges set to :').'<ul>';
14845:                     foreach my $level (@levelorder) {
14846:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
14847:                             next if ($item eq '');
14848:                             my ($priv) = split(/\&/,$item,2);
14849:                             if (&Apache::lonnet::plaintext($priv)) {
14850:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
14851:                                 unless ($level eq 'c') {
14852:                                     $resulttext .= ' ('.$lt{$level}.')';
14853:                                 }
14854:                                 $resulttext .= '</li>';
14855:                             }
14856:                         }
14857:                     }
14858:                     $resulttext .= '</ul></li></ul></li>';
14859:                 }
14860:             }
14861:         }
14862:         $resulttext .= '</ul>';
14863:     } else {
14864:         $resulttext = &mt('No changes made to help settings');
14865:     }
14866:     if ($errors) {
14867:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
14868:                                     $errors.'</ul>';
14869:     }
14870:     return $resulttext;
14871: }
14872: 
14873: sub modify_coursedefaults {
14874:     my ($dom,$lastactref,%domconfig) = @_;
14875:     my ($resulttext,$errors,%changes,%defaultshash);
14876:     my %defaultchecked = (
14877:                            'uselcmath'       => 'on',
14878:                            'usejsme'         => 'on'
14879:                          );
14880:     my @toggles = ('uselcmath','usejsme');
14881:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
14882:                    'uploadquota_community','uploadquota_textbook','mysqltables_official',
14883:                    'mysqltables_unofficial','mysqltables_community','mysqltables_textbook');
14884:     my @types = ('official','unofficial','community','textbook');
14885:     my %staticdefaults = (
14886:                            anonsurvey_threshold => 10,
14887:                            uploadquota          => 500,
14888:                            postsubmit           => 60,
14889:                            mysqltables          => 172800,
14890:                          );
14891:     my %texoptions = (
14892:                         MathJax  => 'MathJax',
14893:                         mimetex  => &mt('Convert to Images'),
14894:                         tth      => &mt('TeX to HTML'),
14895:                      );
14896:     $defaultshash{'coursedefaults'} = {};
14897: 
14898:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
14899:         if ($domconfig{'coursedefaults'} eq '') {
14900:             $domconfig{'coursedefaults'} = {};
14901:         }
14902:     }
14903: 
14904:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
14905:         foreach my $item (@toggles) {
14906:             if ($defaultchecked{$item} eq 'on') {
14907:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
14908:                     ($env{'form.'.$item} eq '0')) {
14909:                     $changes{$item} = 1;
14910:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
14911:                     $changes{$item} = 1;
14912:                 }
14913:             } elsif ($defaultchecked{$item} eq 'off') {
14914:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
14915:                     ($env{'form.'.$item} eq '1')) {
14916:                     $changes{$item} = 1;
14917:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
14918:                     $changes{$item} = 1;
14919:                 }
14920:             }
14921:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
14922:         }
14923:         foreach my $item (@numbers) {
14924:             my ($currdef,$newdef);
14925:             $newdef = $env{'form.'.$item};
14926:             if ($item eq 'anonsurvey_threshold') {
14927:                 $currdef = $domconfig{'coursedefaults'}{$item};
14928:                 $newdef =~ s/\D//g;
14929:                 if ($newdef eq '' || $newdef < 1) {
14930:                     $newdef = 1;
14931:                 }
14932:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
14933:             } else {
14934:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
14935:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
14936:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
14937:                 }
14938:                 $newdef =~ s/[^\w.\-]//g;
14939:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
14940:             }
14941:             if ($currdef ne $newdef) {
14942:                 if ($item eq 'anonsurvey_threshold') {
14943:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
14944:                         $changes{$item} = 1;
14945:                     }
14946:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
14947:                     my $setting = $1;
14948:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
14949:                         $changes{$setting} = 1;
14950:                     }
14951:                 }
14952:             }
14953:         }
14954:         my $texengine;
14955:         if ($env{'form.texengine'} =~ /^(MathJax|mimetex|tth)$/) {
14956:             $texengine = $env{'form.texengine'};
14957:             my $currdef = $domconfig{'coursedefaults'}{'texengine'};
14958:             if ($currdef eq '') {
14959:                 unless ($texengine eq $Apache::lonnet::deftex) {
14960:                     $changes{'texengine'} = 1;
14961:                 }
14962:             } elsif ($currdef ne $texengine) {
14963:                 $changes{'texengine'} = 1;
14964:             }
14965:         }
14966:         if ($texengine ne '') {
14967:             $defaultshash{'coursedefaults'}{'texengine'} = $texengine;
14968:         }
14969:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
14970:         my @currclonecode;
14971:         if (ref($currclone) eq 'HASH') {
14972:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
14973:                 @currclonecode = @{$currclone->{'instcode'}};
14974:             }
14975:         }
14976:         my $newclone;
14977:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
14978:             $newclone = $env{'form.canclone'};
14979:         }
14980:         if ($newclone eq 'instcode') {
14981:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
14982:             my (%codedefaults,@code_order,@clonecode);
14983:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
14984:                                                     \@code_order);
14985:             foreach my $item (@code_order) {
14986:                 if (grep(/^\Q$item\E$/,@newcodes)) {
14987:                     push(@clonecode,$item);
14988:                 }
14989:             }
14990:             if (@clonecode) {
14991:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
14992:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
14993:                 if (@diffs) {
14994:                     $changes{'canclone'} = 1;
14995:                 }
14996:             } else {
14997:                 $newclone eq '';
14998:             }
14999:         } elsif ($newclone ne '') {
15000:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
15001:         }
15002:         if ($newclone ne $currclone) {
15003:             $changes{'canclone'} = 1;
15004:         }
15005:         my %credits;
15006:         foreach my $type (@types) {
15007:             unless ($type eq 'community') {
15008:                 $credits{$type} = $env{'form.'.$type.'_credits'};
15009:                 $credits{$type} =~ s/[^\d.]+//g;
15010:             }
15011:         }
15012:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
15013:             ($env{'form.coursecredits'} eq '1')) {
15014:             $changes{'coursecredits'} = 1;
15015:             foreach my $type (keys(%credits)) {
15016:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
15017:             }
15018:         } else {
15019:             if ($env{'form.coursecredits'} eq '1') {
15020:                 foreach my $type (@types) {
15021:                     unless ($type eq 'community') {
15022:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
15023:                             $changes{'coursecredits'} = 1;
15024:                         }
15025:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
15026:                     }
15027:                 }
15028:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
15029:                 foreach my $type (@types) {
15030:                     unless ($type eq 'community') {
15031:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
15032:                             $changes{'coursecredits'} = 1;
15033:                             last;
15034:                         }
15035:                     }
15036:                 }
15037:             }
15038:         }
15039:         if ($env{'form.postsubmit'} eq '1') {
15040:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
15041:             my %currtimeout;
15042:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
15043:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
15044:                     $changes{'postsubmit'} = 1;
15045:                 }
15046:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
15047:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
15048:                 }
15049:             } else {
15050:                 $changes{'postsubmit'} = 1;
15051:             }
15052:             foreach my $type (@types) {
15053:                 my $timeout = $env{'form.'.$type.'_timeout'};
15054:                 $timeout =~ s/\D//g;
15055:                 if ($timeout == $staticdefaults{'postsubmit'}) {
15056:                     $timeout = '';
15057:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
15058:                     $timeout = '0';
15059:                 }
15060:                 unless ($timeout eq '') {
15061:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
15062:                 }
15063:                 if (exists($currtimeout{$type})) {
15064:                     if ($timeout ne $currtimeout{$type}) {
15065:                         $changes{'postsubmit'} = 1;
15066:                     }
15067:                 } elsif ($timeout ne '') {
15068:                     $changes{'postsubmit'} = 1;
15069:                 }
15070:             }
15071:         } else {
15072:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
15073:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
15074:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
15075:                     $changes{'postsubmit'} = 1;
15076:                 }
15077:             } else {
15078:                 $changes{'postsubmit'} = 1;
15079:             }
15080:         }
15081:     }
15082:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
15083:                                              $dom);
15084:     if ($putresult eq 'ok') {
15085:         if (keys(%changes) > 0) {
15086:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15087:             if (($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
15088:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
15089:                 ($changes{'canclone'}) || ($changes{'mysqltables'}) || ($changes{'texengine'})) {
15090:                 foreach my $item ('uselcmath','usejsme','texengine') {
15091:                     if ($changes{$item}) {
15092:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
15093:                     }
15094:                 }
15095:                 if ($changes{'coursecredits'}) {
15096:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
15097:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
15098:                             $domdefaults{$type.'credits'} =
15099:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
15100:                         }
15101:                     }
15102:                 }
15103:                 if ($changes{'postsubmit'}) {
15104:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
15105:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
15106:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
15107:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
15108:                                 $domdefaults{$type.'postsubtimeout'} =
15109:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
15110:                             }
15111:                         }
15112:                     }
15113:                 }
15114:                 if ($changes{'uploadquota'}) {
15115:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
15116:                         foreach my $type (@types) {
15117:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
15118:                         }
15119:                     }
15120:                 }
15121:                 if ($changes{'canclone'}) {
15122:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
15123:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
15124:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
15125:                             if (@clonecodes) {
15126:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
15127:                             }
15128:                         }
15129:                     } else {
15130:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
15131:                     }
15132:                 }
15133:                 my $cachetime = 24*60*60;
15134:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15135:                 if (ref($lastactref) eq 'HASH') {
15136:                     $lastactref->{'domdefaults'} = 1;
15137:                 }
15138:             }
15139:             $resulttext = &mt('Changes made:').'<ul>';
15140:             foreach my $item (sort(keys(%changes))) {
15141:                 if ($item eq 'uselcmath') {
15142:                     if ($env{'form.'.$item} eq '1') {
15143:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
15144:                     } else {
15145:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
15146:                     }
15147:                 } elsif ($item eq 'usejsme') {
15148:                     if ($env{'form.'.$item} eq '1') {
15149:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
15150:                     } else {
15151:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
15152:                     }
15153:                 } elsif ($item eq 'texengine') {
15154:                     if ($defaultshash{'coursedefaults'}{'texengine'} ne '') {
15155:                         $resulttext .= '<li>'.&mt('Default method to display mathematics set to: "[_1]"',
15156:                                                   $texoptions{$defaultshash{'coursedefaults'}{'texengine'}}).'</li>';
15157:                     }
15158:                 } elsif ($item eq 'anonsurvey_threshold') {
15159:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
15160:                 } elsif ($item eq 'uploadquota') {
15161:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
15162:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
15163:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
15164:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
15165:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
15166: 
15167:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
15168:                                        '</ul>'.
15169:                                        '</li>';
15170:                     } else {
15171:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
15172:                     }
15173:                 } elsif ($item eq 'mysqltables') {
15174:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
15175:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
15176:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
15177:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
15178:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
15179:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
15180:                                        '</ul>'.
15181:                                        '</li>';
15182:                     } else {
15183:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
15184:                     }
15185:                 } elsif ($item eq 'postsubmit') {
15186:                     if ($domdefaults{'postsubmit'} eq 'off') {
15187:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
15188:                     } else {
15189:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
15190:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
15191:                             $resulttext .= &mt('durations:').'<ul>';
15192:                             foreach my $type (@types) {
15193:                                 $resulttext .= '<li>';
15194:                                 my $timeout;
15195:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
15196:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
15197:                                 }
15198:                                 my $display;
15199:                                 if ($timeout eq '0') {
15200:                                     $display = &mt('unlimited');
15201:                                 } elsif ($timeout eq '') {
15202:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
15203:                                 } else {
15204:                                     $display = &mt('[quant,_1,second]',$timeout);
15205:                                 }
15206:                                 if ($type eq 'community') {
15207:                                     $resulttext .= &mt('Communities');
15208:                                 } elsif ($type eq 'official') {
15209:                                     $resulttext .= &mt('Official courses');
15210:                                 } elsif ($type eq 'unofficial') {
15211:                                     $resulttext .= &mt('Unofficial courses');
15212:                                 } elsif ($type eq 'textbook') {
15213:                                     $resulttext .= &mt('Textbook courses');
15214:                                 }
15215:                                 $resulttext .= ' -- '.$display.'</li>';
15216:                             }
15217:                             $resulttext .= '</ul>';
15218:                         }
15219:                         $resulttext .= '</li>';
15220:                     }
15221:                 } elsif ($item eq 'coursecredits') {
15222:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
15223:                         if (($domdefaults{'officialcredits'} eq '') &&
15224:                             ($domdefaults{'unofficialcredits'} eq '') &&
15225:                             ($domdefaults{'textbookcredits'} eq '')) {
15226:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
15227:                         } else {
15228:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
15229:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
15230:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
15231:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
15232:                                            '</ul>'.
15233:                                            '</li>';
15234:                         }
15235:                     } else {
15236:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
15237:                     }
15238:                 } elsif ($item eq 'canclone') {
15239:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
15240:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
15241:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
15242:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
15243:                         }
15244:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
15245:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
15246:                     } else {
15247:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
15248:                     }
15249:                 }
15250:             }
15251:             $resulttext .= '</ul>';
15252:         } else {
15253:             $resulttext = &mt('No changes made to course defaults');
15254:         }
15255:     } else {
15256:         $resulttext = '<span class="LC_error">'.
15257:             &mt('An error occurred: [_1]',$putresult).'</span>';
15258:     }
15259:     return $resulttext;
15260: }
15261: 
15262: sub modify_selfenrollment {
15263:     my ($dom,$lastactref,%domconfig) = @_;
15264:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
15265:     my @types = ('official','unofficial','community','textbook');
15266:     my %titles = &tool_titles();
15267:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
15268:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
15269:     $ordered{'default'} = ['types','registered','approval','limit'];
15270: 
15271:     my (%roles,%shown,%toplevel);
15272:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
15273: 
15274:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
15275:         if ($domconfig{'selfenrollment'} eq '') {
15276:             $domconfig{'selfenrollment'} = {};
15277:         }
15278:     }
15279:     %toplevel = (
15280:                   admin      => 'Configuration Rights',
15281:                   default    => 'Default settings',
15282:                   validation => 'Validation of self-enrollment requests',
15283:                 );
15284:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
15285: 
15286:     if (ref($ordered{'admin'}) eq 'ARRAY') {
15287:         foreach my $item (@{$ordered{'admin'}}) {
15288:             foreach my $type (@types) {
15289:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
15290:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
15291:                 } else {
15292:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
15293:                 }
15294:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
15295:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
15296:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
15297:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
15298:                             push(@{$changes{'admin'}{$type}},$item);
15299:                         }
15300:                     } else {
15301:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
15302:                             push(@{$changes{'admin'}{$type}},$item);
15303:                         }
15304:                     }
15305:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
15306:                     push(@{$changes{'admin'}{$type}},$item);
15307:                 }
15308:             }
15309:         }
15310:     }
15311: 
15312:     foreach my $item (@{$ordered{'default'}}) {
15313:         foreach my $type (@types) {
15314:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
15315:             if ($item eq 'types') {
15316:                 unless (($value eq 'all') || ($value eq 'dom')) {
15317:                     $value = '';
15318:                 }
15319:             } elsif ($item eq 'registered') {
15320:                 unless ($value eq '1') {
15321:                     $value = 0;
15322:                 }
15323:             } elsif ($item eq 'approval') {
15324:                 unless ($value =~ /^[012]$/) {
15325:                     $value = 0;
15326:                 }
15327:             } else {
15328:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
15329:                     $value = 'none';
15330:                 }
15331:             }
15332:             $selfenrollhash{'default'}{$type}{$item} = $value;
15333:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
15334:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
15335:                     if ($selfenrollhash{'default'}{$type}{$item} ne
15336:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
15337:                          push(@{$changes{'default'}{$type}},$item);
15338:                     }
15339:                 } else {
15340:                     push(@{$changes{'default'}{$type}},$item);
15341:                 }
15342:             } else {
15343:                 push(@{$changes{'default'}{$type}},$item);
15344:             }
15345:             if ($item eq 'limit') {
15346:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
15347:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
15348:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
15349:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
15350:                     }
15351:                 } else {
15352:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
15353:                 }
15354:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
15355:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
15356:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
15357:                          push(@{$changes{'default'}{$type}},'cap');
15358:                     }
15359:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
15360:                     push(@{$changes{'default'}{$type}},'cap');
15361:                 }
15362:             }
15363:         }
15364:     }
15365: 
15366:     foreach my $item (@{$itemsref}) {
15367:         if ($item eq 'fields') {
15368:             my @changed;
15369:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
15370:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
15371:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
15372:             }
15373:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
15374:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
15375:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
15376:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
15377:                 } else {
15378:                     @changed = @{$selfenrollhash{'validation'}{$item}};
15379:                 }
15380:             } else {
15381:                 @changed = @{$selfenrollhash{'validation'}{$item}};
15382:             }
15383:             if (@changed) {
15384:                 if ($selfenrollhash{'validation'}{$item}) { 
15385:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
15386:                 } else {
15387:                     $changes{'validation'}{$item} = &mt('None');
15388:                 }
15389:             }
15390:         } else {
15391:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
15392:             if ($item eq 'markup') {
15393:                if ($env{'form.selfenroll_validation_'.$item}) {
15394:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
15395:                }
15396:             }
15397:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
15398:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
15399:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
15400:                 }
15401:             }
15402:         }
15403:     }
15404: 
15405:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
15406:                                              $dom);
15407:     if ($putresult eq 'ok') {
15408:         if (keys(%changes) > 0) {
15409:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15410:             $resulttext = &mt('Changes made:').'<ul>';
15411:             foreach my $key ('admin','default','validation') {
15412:                 if (ref($changes{$key}) eq 'HASH') {
15413:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
15414:                     if ($key eq 'validation') {
15415:                         foreach my $item (@{$itemsref}) {
15416:                             if (exists($changes{$key}{$item})) {
15417:                                 if ($item eq 'markup') {
15418:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
15419:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
15420:                                 } else {  
15421:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
15422:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
15423:                                 }
15424:                             }
15425:                         }
15426:                     } else {
15427:                         foreach my $type (@types) {
15428:                             if ($type eq 'community') {
15429:                                 $roles{'1'} = &mt('Community personnel');
15430:                             } else {
15431:                                 $roles{'1'} = &mt('Course personnel');
15432:                             }
15433:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
15434:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
15435:                                     if ($key eq 'admin') {
15436:                                         my @mgrdc = ();
15437:                                         if (ref($ordered{$key}) eq 'ARRAY') {
15438:                                             foreach my $item (@{$ordered{'admin'}}) {
15439:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
15440:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
15441:                                                         push(@mgrdc,$item);
15442:                                                     }
15443:                                                 }
15444:                                             }
15445:                                             if (@mgrdc) {
15446:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
15447:                                             } else {
15448:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
15449:                                             }
15450:                                         }
15451:                                     } else {
15452:                                         if (ref($ordered{$key}) eq 'ARRAY') {
15453:                                             foreach my $item (@{$ordered{$key}}) {
15454:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
15455:                                                     $domdefaults{$type.'selfenroll'.$item} =
15456:                                                         $selfenrollhash{$key}{$type}{$item};
15457:                                                 }
15458:                                             }
15459:                                         }
15460:                                     }
15461:                                 }
15462:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
15463:                                 foreach my $item (@{$ordered{$key}}) {
15464:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
15465:                                         $resulttext .= '<li>';
15466:                                         if ($key eq 'admin') {
15467:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
15468:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
15469:                                         } else {
15470:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
15471:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
15472:                                         }
15473:                                         $resulttext .= '</li>';
15474:                                     }
15475:                                 }
15476:                                 $resulttext .= '</ul></li>';
15477:                             }
15478:                         }
15479:                         $resulttext .= '</ul></li>'; 
15480:                     }
15481:                 }
15482:             }
15483:             if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
15484:                 my $cachetime = 24*60*60;
15485:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15486:                 if (ref($lastactref) eq 'HASH') {
15487:                     $lastactref->{'domdefaults'} = 1;
15488:                 }
15489:             }
15490:             $resulttext .= '</ul>';
15491:         } else {
15492:             $resulttext = &mt('No changes made to self-enrollment settings');
15493:         }
15494:     } else {
15495:         $resulttext = '<span class="LC_error">'.
15496:             &mt('An error occurred: [_1]',$putresult).'</span>';
15497:     }
15498:     return $resulttext;
15499: }
15500: 
15501: sub modify_usersessions {
15502:     my ($dom,$lastactref,%domconfig) = @_;
15503:     my @hostingtypes = ('version','excludedomain','includedomain');
15504:     my @offloadtypes = ('primary','default');
15505:     my %types = (
15506:                   remote => \@hostingtypes,
15507:                   hosted => \@hostingtypes,
15508:                   spares => \@offloadtypes,
15509:                 );
15510:     my @prefixes = ('remote','hosted','spares');
15511:     my @lcversions = &Apache::lonnet::all_loncaparevs();
15512:     my (%by_ip,%by_location,@intdoms);
15513:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
15514:     my @locations = sort(keys(%by_location));
15515:     my (%defaultshash,%changes);
15516:     foreach my $prefix (@prefixes) {
15517:         $defaultshash{'usersessions'}{$prefix} = {};
15518:     }
15519:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15520:     my $resulttext;
15521:     my %iphost = &Apache::lonnet::get_iphost();
15522:     foreach my $prefix (@prefixes) {
15523:         next if ($prefix eq 'spares');
15524:         foreach my $type (@{$types{$prefix}}) {
15525:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
15526:             if ($type eq 'version') {
15527:                 my $value = $env{'form.'.$prefix.'_'.$type};
15528:                 my $okvalue;
15529:                 if ($value ne '') {
15530:                     if (grep(/^\Q$value\E$/,@lcversions)) {
15531:                         $okvalue = $value;
15532:                     }
15533:                 }
15534:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
15535:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
15536:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
15537:                             if ($inuse == 0) {
15538:                                 $changes{$prefix}{$type} = 1;
15539:                             } else {
15540:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
15541:                                     $changes{$prefix}{$type} = 1;
15542:                                 }
15543:                                 if ($okvalue ne '') {
15544:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
15545:                                 } 
15546:                             }
15547:                         } else {
15548:                             if (($inuse == 1) && ($okvalue ne '')) {
15549:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
15550:                                 $changes{$prefix}{$type} = 1;
15551:                             }
15552:                         }
15553:                     } else {
15554:                         if (($inuse == 1) && ($okvalue ne '')) {
15555:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
15556:                             $changes{$prefix}{$type} = 1;
15557:                         }
15558:                     }
15559:                 } else {
15560:                     if (($inuse == 1) && ($okvalue ne '')) {
15561:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
15562:                         $changes{$prefix}{$type} = 1;
15563:                     }
15564:                 }
15565:             } else {
15566:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
15567:                 my @okvals;
15568:                 foreach my $val (@vals) {
15569:                     if ($val =~ /:/) {
15570:                         my @items = split(/:/,$val);
15571:                         foreach my $item (@items) {
15572:                             if (ref($by_location{$item}) eq 'ARRAY') {
15573:                                 push(@okvals,$item);
15574:                             }
15575:                         }
15576:                     } else {
15577:                         if (ref($by_location{$val}) eq 'ARRAY') {
15578:                             push(@okvals,$val);
15579:                         }
15580:                     }
15581:                 }
15582:                 @okvals = sort(@okvals);
15583:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
15584:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
15585:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
15586:                             if ($inuse == 0) {
15587:                                 $changes{$prefix}{$type} = 1; 
15588:                             } else {
15589:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
15590:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
15591:                                 if (@changed > 0) {
15592:                                     $changes{$prefix}{$type} = 1;
15593:                                 }
15594:                             }
15595:                         } else {
15596:                             if ($inuse == 1) {
15597:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
15598:                                 $changes{$prefix}{$type} = 1;
15599:                             }
15600:                         } 
15601:                     } else {
15602:                         if ($inuse == 1) {
15603:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
15604:                             $changes{$prefix}{$type} = 1;
15605:                         }
15606:                     }
15607:                 } else {
15608:                     if ($inuse == 1) {
15609:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
15610:                         $changes{$prefix}{$type} = 1;
15611:                     }
15612:                 }
15613:             }
15614:         }
15615:     }
15616: 
15617:     my @alldoms = &Apache::lonnet::all_domains();
15618:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
15619:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
15620:     my $savespares;
15621: 
15622:     foreach my $lonhost (sort(keys(%servers))) {
15623:         my $serverhomeID =
15624:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
15625:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
15626:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
15627:         my %spareschg;
15628:         foreach my $type (@{$types{'spares'}}) {
15629:             my @okspares;
15630:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
15631:             foreach my $server (@checked) {
15632:                 if (&Apache::lonnet::hostname($server) ne '') {
15633:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
15634:                         unless (grep(/^\Q$server\E$/,@okspares)) {
15635:                             push(@okspares,$server);
15636:                         }
15637:                     }
15638:                 }
15639:             }
15640:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
15641:             my $newspare;
15642:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
15643:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
15644:                     $newspare = $new;
15645:                 }
15646:             }
15647:             my @spares;
15648:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
15649:                 @spares = sort(@okspares,$newspare);
15650:             } else {
15651:                 @spares = sort(@okspares);
15652:             }
15653:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
15654:             if (ref($spareid{$lonhost}) eq 'HASH') {
15655:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
15656:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
15657:                     if (@diffs > 0) {
15658:                         $spareschg{$type} = 1;
15659:                     }
15660:                 }
15661:             }
15662:         }
15663:         if (keys(%spareschg) > 0) {
15664:             $changes{'spares'}{$lonhost} = \%spareschg;
15665:         }
15666:     }
15667:     $defaultshash{'usersessions'}{'offloadnow'} = {};
15668:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
15669:     my @okoffload;
15670:     if (@offloadnow) {
15671:         foreach my $server (@offloadnow) {
15672:             if (&Apache::lonnet::hostname($server) ne '') {
15673:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
15674:                     push(@okoffload,$server);
15675:                 }
15676:             }
15677:         }
15678:         if (@okoffload) {
15679:             foreach my $lonhost (@okoffload) {
15680:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
15681:             }
15682:         }
15683:     }
15684:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
15685:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
15686:             if (ref($changes{'spares'}) eq 'HASH') {
15687:                 if (keys(%{$changes{'spares'}}) > 0) {
15688:                     $savespares = 1;
15689:                 }
15690:             }
15691:         } else {
15692:             $savespares = 1;
15693:         }
15694:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
15695:             foreach my $lonhost (keys(%{$domconfig{'usersessions'}{'offloadnow'}})) {
15696:                 unless ($defaultshash{'usersessions'}{'offloadnow'}{$lonhost}) {
15697:                     $changes{'offloadnow'} = 1;
15698:                     last;
15699:                 }
15700:             }
15701:             unless ($changes{'offloadnow'}) {
15702:                 foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{'offloadnow'}})) {
15703:                     unless ($domconfig{'usersessions'}{'offloadnow'}{$lonhost}) {
15704:                         $changes{'offloadnow'} = 1;
15705:                         last;
15706:                     }
15707:                 }
15708:             }
15709:         } elsif (@okoffload) {
15710:             $changes{'offloadnow'} = 1;
15711:         }
15712:     } elsif (@okoffload) {
15713:         $changes{'offloadnow'} = 1;
15714:     }
15715:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
15716:     if ((keys(%changes) > 0) || ($savespares)) {
15717:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
15718:                                                  $dom);
15719:         if ($putresult eq 'ok') {
15720:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
15721:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
15722:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
15723:                 }
15724:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
15725:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
15726:                 }
15727:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
15728:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
15729:                 }
15730:             }
15731:             my $cachetime = 24*60*60;
15732:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15733:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
15734:             if (ref($lastactref) eq 'HASH') {
15735:                 $lastactref->{'domdefaults'} = 1;
15736:                 $lastactref->{'usersessions'} = 1;
15737:             }
15738:             if (keys(%changes) > 0) {
15739:                 my %lt = &usersession_titles();
15740:                 $resulttext = &mt('Changes made:').'<ul>';
15741:                 foreach my $prefix (@prefixes) {
15742:                     if (ref($changes{$prefix}) eq 'HASH') {
15743:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
15744:                         if ($prefix eq 'spares') {
15745:                             if (ref($changes{$prefix}) eq 'HASH') {
15746:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
15747:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
15748:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
15749:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
15750:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
15751:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
15752:                                         foreach my $type (@{$types{$prefix}}) {
15753:                                             if ($changes{$prefix}{$lonhost}{$type}) {
15754:                                                 my $offloadto = &mt('None');
15755:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
15756:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
15757:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
15758:                                                     }
15759:                                                 }
15760:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
15761:                                             }
15762:                                         }
15763:                                     }
15764:                                     $resulttext .= '</li>';
15765:                                 }
15766:                             }
15767:                         } else {
15768:                             foreach my $type (@{$types{$prefix}}) {
15769:                                 if (defined($changes{$prefix}{$type})) {
15770:                                     my $newvalue;
15771:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
15772:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
15773:                                             if ($type eq 'version') {
15774:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
15775:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
15776:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
15777:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
15778:                                                 }
15779:                                             }
15780:                                         }
15781:                                     }
15782:                                     if ($newvalue eq '') {
15783:                                         if ($type eq 'version') {
15784:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
15785:                                         } else {
15786:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
15787:                                         }
15788:                                     } else {
15789:                                         if ($type eq 'version') {
15790:                                             $newvalue .= ' '.&mt('(or later)'); 
15791:                                         }
15792:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
15793:                                     }
15794:                                 }
15795:                             }
15796:                         }
15797:                         $resulttext .= '</ul>';
15798:                     }
15799:                 }
15800:                 if ($changes{'offloadnow'}) {
15801:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
15802:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
15803:                             $resulttext .= '<li>'.&mt('Switch active users on next access, for server(s):').'<ul>';
15804:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
15805:                                 $resulttext .= '<li>'.$lonhost.'</li>';
15806:                             }
15807:                             $resulttext .= '</ul>';
15808:                         } else {
15809:                             $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.');
15810:                         }
15811:                     } else {
15812:                         $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.').'</li>';
15813:                     }
15814:                 }
15815:                 $resulttext .= '</ul>';
15816:             } else {
15817:                 $resulttext = $nochgmsg;
15818:             }
15819:         } else {
15820:             $resulttext = '<span class="LC_error">'.
15821:                           &mt('An error occurred: [_1]',$putresult).'</span>';
15822:         }
15823:     } else {
15824:         $resulttext = $nochgmsg;
15825:     }
15826:     return $resulttext;
15827: }
15828: 
15829: sub modify_loadbalancing {
15830:     my ($dom,%domconfig) = @_;
15831:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
15832:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
15833:     my ($othertitle,$usertypes,$types) =
15834:         &Apache::loncommon::sorted_inst_types($dom);
15835:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
15836:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
15837:     my @sparestypes = ('primary','default');
15838:     my %typetitles = &sparestype_titles();
15839:     my $resulttext;
15840:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
15841:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
15842:         %existing = %{$domconfig{'loadbalancing'}};
15843:     }
15844:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
15845:                               \%currtargets,\%currrules,\%currcookies);
15846:     my ($saveloadbalancing,%defaultshash,%changes);
15847:     my ($alltypes,$othertypes,$titles) =
15848:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
15849:     my %ruletitles = &offloadtype_text();
15850:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
15851:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
15852:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
15853:         if ($balancer eq '') {
15854:             next;
15855:         }
15856:         if (!exists($servers{$balancer})) {
15857:             if (exists($currbalancer{$balancer})) {
15858:                 push(@{$changes{'delete'}},$balancer);
15859:             }
15860:             next;
15861:         }
15862:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
15863:             push(@{$changes{'delete'}},$balancer);
15864:             next;
15865:         }
15866:         if (!exists($currbalancer{$balancer})) {
15867:             push(@{$changes{'add'}},$balancer);
15868:         }
15869:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
15870:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
15871:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
15872:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
15873:             $saveloadbalancing = 1;
15874:         }
15875:         foreach my $sparetype (@sparestypes) {
15876:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
15877:             my @offloadto;
15878:             foreach my $target (@targets) {
15879:                 if (($servers{$target}) && ($target ne $balancer)) {
15880:                     if ($sparetype eq 'default') {
15881:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
15882:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
15883:                         }
15884:                     }
15885:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
15886:                         push(@offloadto,$target);
15887:                     }
15888:                 }
15889:             }
15890:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
15891:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
15892:                     push(@offloadto,$balancer);
15893:                 }
15894:             }
15895:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
15896:         }
15897:         if ($env{'form.loadbalancing_cookie_'.$i}) {
15898:             $defaultshash{'loadbalancing'}{$balancer}{'cookie'} = 1;
15899:             if (exists($currbalancer{$balancer})) {
15900:                 unless ($currcookies{$balancer}) {
15901:                     $changes{'curr'}{$balancer}{'cookie'} = 1;
15902:                 }
15903:             }
15904:         } elsif (exists($currbalancer{$balancer})) {
15905:             if ($currcookies{$balancer}) {
15906:                 $changes{'curr'}{$balancer}{'cookie'} = 1;
15907:             }
15908:         }
15909:         if (ref($currtargets{$balancer}) eq 'HASH') {
15910:             foreach my $sparetype (@sparestypes) {
15911:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
15912:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
15913:                     if (@targetdiffs > 0) {
15914:                         $changes{'curr'}{$balancer}{'targets'} = 1;
15915:                     }
15916:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
15917:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
15918:                         $changes{'curr'}{$balancer}{'targets'} = 1;
15919:                     }
15920:                 }
15921:             }
15922:         } else {
15923:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
15924:                 foreach my $sparetype (@sparestypes) {
15925:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
15926:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
15927:                             $changes{'curr'}{$balancer}{'targets'} = 1;
15928:                         }
15929:                     }
15930:                 }
15931:             }
15932:         }
15933:         my $ishomedom;
15934:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
15935:             $ishomedom = 1;
15936:         }
15937:         if (ref($alltypes) eq 'ARRAY') {
15938:             foreach my $type (@{$alltypes}) {
15939:                 my $rule;
15940:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
15941:                          (!$ishomedom)) {
15942:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
15943:                 }
15944:                 if ($rule eq 'specific') {
15945:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
15946:                     if (exists($servers{$specifiedhost})) {
15947:                         $rule = $specifiedhost;
15948:                     }
15949:                 }
15950:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
15951:                 if (ref($currrules{$balancer}) eq 'HASH') {
15952:                     if ($rule ne $currrules{$balancer}{$type}) {
15953:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
15954:                     }
15955:                 } elsif ($rule ne '') {
15956:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
15957:                 }
15958:             }
15959:         }
15960:     }
15961:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
15962:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
15963:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
15964:             $defaultshash{'loadbalancing'} = {};
15965:         }
15966:         my $putresult = &Apache::lonnet::put_dom('configuration',
15967:                                                  \%defaultshash,$dom);
15968:         if ($putresult eq 'ok') {
15969:             if (keys(%changes) > 0) {
15970:                 my %toupdate;
15971:                 if (ref($changes{'delete'}) eq 'ARRAY') {
15972:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
15973:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
15974:                         $toupdate{$balancer} = 1;
15975:                     }
15976:                 }
15977:                 if (ref($changes{'add'}) eq 'ARRAY') {
15978:                     foreach my $balancer (sort(@{$changes{'add'}})) {
15979:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
15980:                         $toupdate{$balancer} = 1;
15981:                     }
15982:                 }
15983:                 if (ref($changes{'curr'}) eq 'HASH') {
15984:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
15985:                         $toupdate{$balancer} = 1;
15986:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
15987:                             if ($changes{'curr'}{$balancer}{'targets'}) {
15988:                                 my %offloadstr;
15989:                                 foreach my $sparetype (@sparestypes) {
15990:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
15991:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
15992:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
15993:                                         }
15994:                                     }
15995:                                 }
15996:                                 if (keys(%offloadstr) == 0) {
15997:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
15998:                                 } else {
15999:                                     my $showoffload;
16000:                                     foreach my $sparetype (@sparestypes) {
16001:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
16002:                                         if (defined($offloadstr{$sparetype})) {
16003:                                             $showoffload .= $offloadstr{$sparetype};
16004:                                         } else {
16005:                                             $showoffload .= &mt('None');
16006:                                         }
16007:                                         $showoffload .= ('&nbsp;'x3);
16008:                                     }
16009:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
16010:                                 }
16011:                             }
16012:                         }
16013:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
16014:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
16015:                                 foreach my $type (@{$alltypes}) {
16016:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
16017:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
16018:                                         my $balancetext;
16019:                                         if ($rule eq '') {
16020:                                             $balancetext =  $ruletitles{'default'};
16021:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
16022:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
16023:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
16024:                                                 foreach my $sparetype (@sparestypes) {
16025:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
16026:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
16027:                                                     }
16028:                                                 }
16029:                                                 foreach my $item (@{$alltypes}) {
16030:                                                     next if ($item =~  /^_LC_ipchange/);
16031:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
16032:                                                     if ($hasrule eq 'homeserver') {
16033:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
16034:                                                     } else {
16035:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
16036:                                                             if ($servers{$hasrule}) {
16037:                                                                 $toupdate{$hasrule} = 1;
16038:                                                             }
16039:                                                         }
16040:                                                     }
16041:                                                 }
16042:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
16043:                                                     $balancetext =  $ruletitles{$rule};
16044:                                                 } else {
16045:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
16046:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
16047:                                                     if ($receiver) {
16048:                                                         $toupdate{$receiver};
16049:                                                     }
16050:                                                 }
16051:                                             } else {
16052:                                                 $balancetext =  $ruletitles{$rule};
16053:                                             }
16054:                                         } else {
16055:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
16056:                                         }
16057:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
16058:                                     }
16059:                                 }
16060:                             }
16061:                         }
16062:                         if ($changes{'curr'}{$balancer}{'cookie'}) {
16063:                             $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use enabled',
16064:                                                       $balancer).'</li>';
16065:                         }
16066:                         if (keys(%toupdate)) {
16067:                             my %thismachine;
16068:                             my $updatedhere;
16069:                             my $cachetime = 60*60*24;
16070:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
16071:                             foreach my $lonhost (keys(%toupdate)) {
16072:                                 if ($thismachine{$lonhost}) {
16073:                                     unless ($updatedhere) {
16074:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
16075:                                                                       $defaultshash{'loadbalancing'},
16076:                                                                       $cachetime);
16077:                                         $updatedhere = 1;
16078:                                     }
16079:                                 } else {
16080:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
16081:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
16082:                                 }
16083:                             }
16084:                         }
16085:                     }
16086:                 }
16087:                 if ($resulttext ne '') {
16088:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
16089:                 } else {
16090:                     $resulttext = $nochgmsg;
16091:                 }
16092:             } else {
16093:                 $resulttext = $nochgmsg;
16094:             }
16095:         } else {
16096:             $resulttext = '<span class="LC_error">'.
16097:                           &mt('An error occurred: [_1]',$putresult).'</span>';
16098:         }
16099:     } else {
16100:         $resulttext = $nochgmsg;
16101:     }
16102:     return $resulttext;
16103: }
16104: 
16105: sub recurse_check {
16106:     my ($chkcats,$categories,$depth,$name) = @_;
16107:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
16108:         my $chg = 0;
16109:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
16110:             my $category = $chkcats->[$depth]{$name}[$j];
16111:             my $item;
16112:             if ($category eq '') {
16113:                 $chg ++;
16114:             } else {
16115:                 my $deeper = $depth + 1;
16116:                 $item = &escape($category).':'.&escape($name).':'.$depth;
16117:                 if ($chg) {
16118:                     $categories->{$item} -= $chg;
16119:                 }
16120:                 &recurse_check($chkcats,$categories,$deeper,$category);
16121:                 $deeper --;
16122:             }
16123:         }
16124:     }
16125:     return;
16126: }
16127: 
16128: sub recurse_cat_deletes {
16129:     my ($item,$coursecategories,$deletions) = @_;
16130:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
16131:     my $subdepth = $depth + 1;
16132:     if (ref($coursecategories) eq 'HASH') {
16133:         foreach my $subitem (keys(%{$coursecategories})) {
16134:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
16135:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
16136:                 delete($coursecategories->{$subitem});
16137:                 $deletions->{$subitem} = 1;
16138:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
16139:             }
16140:         }
16141:     }
16142:     return;
16143: }
16144: 
16145: sub active_dc_picker {
16146:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
16147:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
16148:     my @domcoord = keys(%domcoords);
16149:     if (keys(%currhash)) {
16150:         foreach my $dc (keys(%currhash)) {
16151:             unless (exists($domcoords{$dc})) {
16152:                 push(@domcoord,$dc);
16153:             }
16154:         }
16155:     }
16156:     @domcoord = sort(@domcoord);
16157:     my $numdcs = scalar(@domcoord);
16158:     my $rows = 0;
16159:     my $table;
16160:     if ($numdcs > 1) {
16161:         $table = '<table>';
16162:         for (my $i=0; $i<@domcoord; $i++) {
16163:             my $rem = $i%($numinrow);
16164:             if ($rem == 0) {
16165:                 if ($i > 0) {
16166:                     $table .= '</tr>';
16167:                 }
16168:                 $table .= '<tr>';
16169:                 $rows ++;
16170:             }
16171:             my $check = '';
16172:             if ($inputtype eq 'radio') {
16173:                 if (keys(%currhash) == 0) {
16174:                     if (!$i) {
16175:                         $check = ' checked="checked"';
16176:                     }
16177:                 } elsif (exists($currhash{$domcoord[$i]})) {
16178:                     $check = ' checked="checked"';
16179:                 }
16180:             } else {
16181:                 if (exists($currhash{$domcoord[$i]})) {
16182:                     $check = ' checked="checked"';
16183:                 }
16184:             }
16185:             if ($i == @domcoord - 1) {
16186:                 my $colsleft = $numinrow - $rem;
16187:                 if ($colsleft > 1) {
16188:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
16189:                 } else {
16190:                     $table .= '<td class="LC_left_item">';
16191:                 }
16192:             } else {
16193:                 $table .= '<td class="LC_left_item">';
16194:             }
16195:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
16196:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
16197:             $table .= '<span class="LC_nobreak"><label>'.
16198:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
16199:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
16200:             if ($user ne $dcname.':'.$dcdom) {
16201:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
16202:             }
16203:             $table .= '</label></span></td>';
16204:         }
16205:         $table .= '</tr></table>';
16206:     } elsif ($numdcs == 1) {
16207:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
16208:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
16209:         if ($inputtype eq 'radio') {
16210:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
16211:             if ($user ne $dcname.':'.$dcdom) {
16212:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
16213:             }
16214:         } else {
16215:             my $check;
16216:             if (exists($currhash{$domcoord[0]})) {
16217:                 $check = ' checked="checked"';
16218:             }
16219:             $table = '<span class="LC_nobreak"><label>'.
16220:                      '<input type="checkbox" name="'.$name.'" '.
16221:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
16222:             if ($user ne $dcname.':'.$dcdom) {
16223:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
16224:             }
16225:             $table .= '</label></span>';
16226:             $rows ++;
16227:         }
16228:     }
16229:     return ($numdcs,$table,$rows);
16230: }
16231: 
16232: sub usersession_titles {
16233:     return &Apache::lonlocal::texthash(
16234:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
16235:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
16236:                spares => 'Servers offloaded to, when busy',
16237:                version => 'LON-CAPA version requirement',
16238:                excludedomain => 'Allow all, but exclude specific domains',
16239:                includedomain => 'Deny all, but include specific domains',
16240:                primary => 'Primary (checked first)',
16241:                default => 'Default',
16242:            );
16243: }
16244: 
16245: sub id_for_thisdom {
16246:     my (%servers) = @_;
16247:     my %altids;
16248:     foreach my $server (keys(%servers)) {
16249:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
16250:         if ($serverhome ne $server) {
16251:             $altids{$serverhome} = $server;
16252:         }
16253:     }
16254:     return %altids;
16255: }
16256: 
16257: sub count_servers {
16258:     my ($currbalancer,%servers) = @_;
16259:     my (@spares,$numspares);
16260:     foreach my $lonhost (sort(keys(%servers))) {
16261:         next if ($currbalancer eq $lonhost);
16262:         push(@spares,$lonhost);
16263:     }
16264:     if ($currbalancer) {
16265:         $numspares = scalar(@spares);
16266:     } else {
16267:         $numspares = scalar(@spares) - 1;
16268:     }
16269:     return ($numspares,@spares);
16270: }
16271: 
16272: sub lonbalance_targets_js {
16273:     my ($dom,$types,$servers,$settings) = @_;
16274:     my $select = &mt('Select');
16275:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
16276:     if (ref($servers) eq 'HASH') {
16277:         $alltargets = join("','",sort(keys(%{$servers})));
16278:         my @homedoms;
16279:         foreach my $server (sort(keys(%{$servers}))) {
16280:             if (&Apache::lonnet::host_domain($server) eq $dom) {
16281:                 push(@homedoms,'1');
16282:             } else {
16283:                 push(@homedoms,'0');
16284:             }
16285:         }
16286:         $allishome = join("','",@homedoms);
16287:     }
16288:     if (ref($types) eq 'ARRAY') {
16289:         if (@{$types} > 0) {
16290:             @alltypes = @{$types};
16291:         }
16292:     }
16293:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
16294:     $allinsttypes = join("','",@alltypes);
16295:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
16296:     if (ref($settings) eq 'HASH') {
16297:         %existing = %{$settings};
16298:     }
16299:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
16300:                               \%currtargets,\%currrules,\%currcookies);
16301:     my $balancers = join("','",sort(keys(%currbalancer)));
16302:     return <<"END";
16303: 
16304: <script type="text/javascript">
16305: // <![CDATA[
16306: 
16307: currBalancers = new Array('$balancers');
16308: 
16309: function toggleTargets(balnum) {
16310:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
16311:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
16312:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
16313:     var prevbalancer = prevhostitem.value;
16314:     var baltotal = document.getElementById('loadbalancing_total').value;
16315:     prevhostitem.value = balancer;
16316:     if (prevbalancer != '') {
16317:         var prevIdx = currBalancers.indexOf(prevbalancer);
16318:         if (prevIdx != -1) {
16319:             currBalancers.splice(prevIdx,1);
16320:         }
16321:     }
16322:     if (balancer == '') {
16323:         hideSpares(balnum);
16324:     } else {
16325:         var currIdx = currBalancers.indexOf(balancer);
16326:         if (currIdx == -1) {
16327:             currBalancers.push(balancer);
16328:         }
16329:         var homedoms = new Array('$allishome');
16330:         var ishomedom = homedoms[lonhostitem.selectedIndex];
16331:         showSpares(balancer,ishomedom,balnum);
16332:     }
16333:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
16334:     return;
16335: }
16336: 
16337: function showSpares(balancer,ishomedom,balnum) {
16338:     var alltargets = new Array('$alltargets');
16339:     var insttypes = new Array('$allinsttypes');
16340:     var offloadtypes = new Array('primary','default');
16341: 
16342:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
16343:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
16344:  
16345:     for (var i=0; i<offloadtypes.length; i++) {
16346:         var count = 0;
16347:         for (var j=0; j<alltargets.length; j++) {
16348:             if (alltargets[j] != balancer) {
16349:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
16350:                 item.value = alltargets[j];
16351:                 item.style.textAlign='left';
16352:                 item.style.textFace='normal';
16353:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
16354:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
16355:                     item.disabled = '';
16356:                 } else {
16357:                     item.disabled = 'disabled';
16358:                     item.checked = false;
16359:                 }
16360:                 count ++;
16361:             }
16362:         }
16363:     }
16364:     for (var k=0; k<insttypes.length; k++) {
16365:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
16366:             if (ishomedom == 1) {
16367:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
16368:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
16369:             } else {
16370:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
16371:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
16372:             }
16373:         } else {
16374:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
16375:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
16376:         }
16377:         if ((insttypes[k] != '_LC_external') && 
16378:             ((insttypes[k] != '_LC_internetdom') ||
16379:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
16380:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
16381:             item.options.length = 0;
16382:             item.options[0] = new Option("","",true,true);
16383:             var idx = 0;
16384:             for (var m=0; m<alltargets.length; m++) {
16385:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
16386:                     idx ++;
16387:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
16388:                 }
16389:             }
16390:         }
16391:     }
16392:     return;
16393: }
16394: 
16395: function hideSpares(balnum) {
16396:     var alltargets = new Array('$alltargets');
16397:     var insttypes = new Array('$allinsttypes');
16398:     var offloadtypes = new Array('primary','default');
16399: 
16400:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
16401:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
16402: 
16403:     var total = alltargets.length - 1;
16404:     for (var i=0; i<offloadtypes; i++) {
16405:         for (var j=0; j<total; j++) {
16406:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
16407:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
16408:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
16409:         }
16410:     }
16411:     for (var k=0; k<insttypes.length; k++) {
16412:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
16413:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
16414:         if (insttypes[k] != '_LC_external') {
16415:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
16416:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
16417:         }
16418:     }
16419:     return;
16420: }
16421: 
16422: function checkOffloads(item,balnum,type) {
16423:     var alltargets = new Array('$alltargets');
16424:     var offloadtypes = new Array('primary','default');
16425:     if (item.checked) {
16426:         var total = alltargets.length - 1;
16427:         var other;
16428:         if (type == offloadtypes[0]) {
16429:             other = offloadtypes[1];
16430:         } else {
16431:             other = offloadtypes[0];
16432:         }
16433:         for (var i=0; i<total; i++) {
16434:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
16435:             if (server == item.value) {
16436:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
16437:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
16438:                 }
16439:             }
16440:         }
16441:     }
16442:     return;
16443: }
16444: 
16445: function singleServerToggle(balnum,type) {
16446:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
16447:     if (offloadtoSelIdx == 0) {
16448:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
16449:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
16450: 
16451:     } else {
16452:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
16453:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
16454:     }
16455:     return;
16456: }
16457: 
16458: function balanceruleChange(formname,balnum,type) {
16459:     if (type == '_LC_external') {
16460:         return;
16461:     }
16462:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
16463:     for (var i=0; i<typesRules.length; i++) {
16464:         if (formname.elements[typesRules[i]].checked) {
16465:             if (formname.elements[typesRules[i]].value != 'specific') {
16466:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
16467:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
16468:             } else {
16469:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
16470:             }
16471:         }
16472:     }
16473:     return;
16474: }
16475: 
16476: function balancerDeleteChange(balnum) {
16477:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
16478:     var baltotal = document.getElementById('loadbalancing_total').value;
16479:     var addtarget;
16480:     var removetarget;
16481:     var action = 'delete';
16482:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
16483:         var lonhost = hostitem.value;
16484:         var currIdx = currBalancers.indexOf(lonhost);
16485:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
16486:             if (currIdx != -1) {
16487:                 currBalancers.splice(currIdx,1);
16488:             }
16489:             addtarget = lonhost;
16490:         } else {
16491:             if (currIdx == -1) {
16492:                 currBalancers.push(lonhost);
16493:             }
16494:             removetarget = lonhost;
16495:             action = 'undelete';
16496:         }
16497:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
16498:     }
16499:     return;
16500: }
16501: 
16502: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
16503:     if (baltotal > 1) {
16504:         var offloadtypes = new Array('primary','default');
16505:         var alltargets = new Array('$alltargets');
16506:         var insttypes = new Array('$allinsttypes');
16507:         for (var i=0; i<baltotal; i++) {
16508:             if (i != balnum) {
16509:                 for (var j=0; j<offloadtypes.length; j++) {
16510:                     var total = alltargets.length - 1;
16511:                     for (var k=0; k<total; k++) {
16512:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
16513:                         var server = serveritem.value;
16514:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
16515:                             if (server == addtarget) {
16516:                                 serveritem.disabled = '';
16517:                             }
16518:                         }
16519:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
16520:                             if (server == removetarget) {
16521:                                 serveritem.disabled = 'disabled';
16522:                                 serveritem.checked = false;
16523:                             }
16524:                         }
16525:                     }
16526:                 }
16527:                 for (var j=0; j<insttypes.length; j++) {
16528:                     if (insttypes[j] != '_LC_external') {
16529:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
16530:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
16531:                             var currSel = singleserver.selectedIndex;
16532:                             var currVal = singleserver.options[currSel].value;
16533:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
16534:                                 var numoptions = singleserver.options.length;
16535:                                 var needsnew = 1;
16536:                                 for (var k=0; k<numoptions; k++) {
16537:                                     if (singleserver.options[k] == addtarget) {
16538:                                         needsnew = 0;
16539:                                         break;
16540:                                     }
16541:                                 }
16542:                                 if (needsnew == 1) {
16543:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
16544:                                 }
16545:                             }
16546:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
16547:                                 singleserver.options.length = 0;
16548:                                 if ((currVal) && (currVal != removetarget)) {
16549:                                     singleserver.options[0] = new Option("","",false,false);
16550:                                 } else {
16551:                                     singleserver.options[0] = new Option("","",true,true);
16552:                                 }
16553:                                 var idx = 0;
16554:                                 for (var m=0; m<alltargets.length; m++) {
16555:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
16556:                                         idx ++;
16557:                                         if (currVal == alltargets[m]) {
16558:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
16559:                                         } else {
16560:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
16561:                                         }
16562:                                     }
16563:                                 }
16564:                             }
16565:                         }
16566:                     }
16567:                 }
16568:             }
16569:         }
16570:     }
16571:     return;
16572: }
16573: 
16574: // ]]>
16575: </script>
16576: 
16577: END
16578: }
16579: 
16580: sub new_spares_js {
16581:     my @sparestypes = ('primary','default');
16582:     my $types = join("','",@sparestypes);
16583:     my $select = &mt('Select');
16584:     return <<"END";
16585: 
16586: <script type="text/javascript">
16587: // <![CDATA[
16588: 
16589: function updateNewSpares(formname,lonhost) {
16590:     var types = new Array('$types');
16591:     var include = new Array();
16592:     var exclude = new Array();
16593:     for (var i=0; i<types.length; i++) {
16594:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
16595:         for (var j=0; j<spareboxes.length; j++) {
16596:             if (formname.elements[spareboxes[j]].checked) {
16597:                 exclude.push(formname.elements[spareboxes[j]].value);
16598:             } else {
16599:                 include.push(formname.elements[spareboxes[j]].value);
16600:             }
16601:         }
16602:     }
16603:     for (var i=0; i<types.length; i++) {
16604:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
16605:         var selIdx = newSpare.selectedIndex;
16606:         var currnew = newSpare.options[selIdx].value;
16607:         var okSpares = new Array();
16608:         for (var j=0; j<newSpare.options.length; j++) {
16609:             var possible = newSpare.options[j].value;
16610:             if (possible != '') {
16611:                 if (exclude.indexOf(possible) == -1) {
16612:                     okSpares.push(possible);
16613:                 } else {
16614:                     if (currnew == possible) {
16615:                         selIdx = 0;
16616:                     }
16617:                 }
16618:             }
16619:         }
16620:         for (var k=0; k<include.length; k++) {
16621:             if (okSpares.indexOf(include[k]) == -1) {
16622:                 okSpares.push(include[k]);
16623:             }
16624:         }
16625:         okSpares.sort();
16626:         newSpare.options.length = 0;
16627:         if (selIdx == 0) {
16628:             newSpare.options[0] = new Option("$select","",true,true);
16629:         } else {
16630:             newSpare.options[0] = new Option("$select","",false,false);
16631:         }
16632:         for (var m=0; m<okSpares.length; m++) {
16633:             var idx = m+1;
16634:             var selThis = 0;
16635:             if (selIdx != 0) {
16636:                 if (okSpares[m] == currnew) {
16637:                     selThis = 1;
16638:                 }
16639:             }
16640:             if (selThis == 1) {
16641:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
16642:             } else {
16643:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
16644:             }
16645:         }
16646:     }
16647:     return;
16648: }
16649: 
16650: function checkNewSpares(lonhost,type) {
16651:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
16652:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
16653:     if (chosen != '') { 
16654:         var othertype;
16655:         var othernewSpare;
16656:         if (type == 'primary') {
16657:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
16658:         }
16659:         if (type == 'default') {
16660:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
16661:         }
16662:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
16663:             othernewSpare.selectedIndex = 0;
16664:         }
16665:     }
16666:     return;
16667: }
16668: 
16669: // ]]>
16670: </script>
16671: 
16672: END
16673: 
16674: }
16675: 
16676: sub common_domprefs_js {
16677:     return <<"END";
16678: 
16679: <script type="text/javascript">
16680: // <![CDATA[
16681: 
16682: function getIndicesByName(formname,item) {
16683:     var group = new Array();
16684:     for (var i=0;i<formname.elements.length;i++) {
16685:         if (formname.elements[i].name == item) {
16686:             group.push(formname.elements[i].id);
16687:         }
16688:     }
16689:     return group;
16690: }
16691: 
16692: // ]]>
16693: </script>
16694: 
16695: END
16696: 
16697: }
16698: 
16699: sub recaptcha_js {
16700:     my %lt = &captcha_phrases();
16701:     return <<"END";
16702: 
16703: <script type="text/javascript">
16704: // <![CDATA[
16705: 
16706: function updateCaptcha(caller,context) {
16707:     var privitem;
16708:     var pubitem;
16709:     var privtext;
16710:     var pubtext;
16711:     var versionitem;
16712:     var versiontext;
16713:     if (document.getElementById(context+'_recaptchapub')) {
16714:         pubitem = document.getElementById(context+'_recaptchapub');
16715:     } else {
16716:         return;
16717:     }
16718:     if (document.getElementById(context+'_recaptchapriv')) {
16719:         privitem = document.getElementById(context+'_recaptchapriv');
16720:     } else {
16721:         return;
16722:     }
16723:     if (document.getElementById(context+'_recaptchapubtxt')) {
16724:         pubtext = document.getElementById(context+'_recaptchapubtxt');
16725:     } else {
16726:         return;
16727:     }
16728:     if (document.getElementById(context+'_recaptchaprivtxt')) {
16729:         privtext = document.getElementById(context+'_recaptchaprivtxt');
16730:     } else {
16731:         return;
16732:     }
16733:     if (document.getElementById(context+'_recaptchaversion')) {
16734:         versionitem = document.getElementById(context+'_recaptchaversion');
16735:     } else {
16736:         return;
16737:     }
16738:     if (document.getElementById(context+'_recaptchavertxt')) {
16739:         versiontext = document.getElementById(context+'_recaptchavertxt');
16740:     } else {
16741:         return;
16742:     }
16743:     if (caller.checked) {
16744:         if (caller.value == 'recaptcha') {
16745:             pubitem.type = 'text';
16746:             privitem.type = 'text';
16747:             pubitem.size = '40';
16748:             privitem.size = '40';
16749:             pubtext.innerHTML = "$lt{'pub'}";
16750:             privtext.innerHTML = "$lt{'priv'}";
16751:             versionitem.type = 'text';
16752:             versionitem.size = '3';
16753:             versiontext.innerHTML = "$lt{'ver'}";
16754:         } else {
16755:             pubitem.type = 'hidden';
16756:             privitem.type = 'hidden';
16757:             versionitem.type = 'hidden';
16758:             pubtext.innerHTML = '';
16759:             privtext.innerHTML = '';
16760:             versiontext.innerHTML = '';
16761:         }
16762:     }
16763:     return;
16764: }
16765: 
16766: // ]]>
16767: </script>
16768: 
16769: END
16770: 
16771: }
16772: 
16773: sub toggle_display_js {
16774:     return <<"END";
16775: 
16776: <script type="text/javascript">
16777: // <![CDATA[
16778: 
16779: function toggleDisplay(domForm,caller) {
16780:     if (document.getElementById(caller)) {
16781:         var divitem = document.getElementById(caller);
16782:         var optionsElement = domForm.coursecredits;
16783:         var checkval = 1;
16784:         var dispval = 'block';
16785:         var selfcreateRegExp = /^cancreate_emailverified/;
16786:         if (caller == 'emailoptions') {
16787:             optionsElement = domForm.cancreate_email; 
16788:         }
16789:         if (caller == 'studentsubmission') {
16790:             optionsElement = domForm.postsubmit;
16791:         }
16792:         if (caller == 'cloneinstcode') {
16793:             optionsElement = domForm.canclone;
16794:             checkval = 'instcode';
16795:         }
16796:         if (selfcreateRegExp.test(caller)) {
16797:             optionsElement = domForm.elements[caller];
16798:             checkval = 'other';
16799:             dispval = 'inline'
16800:         }
16801:         if (optionsElement.length) {
16802:             var currval;
16803:             for (var i=0; i<optionsElement.length; i++) {
16804:                 if (optionsElement[i].checked) {
16805:                    currval = optionsElement[i].value;
16806:                 }
16807:             }
16808:             if (currval == checkval) {
16809:                 divitem.style.display = dispval;
16810:             } else {
16811:                 divitem.style.display = 'none';
16812:             }
16813:         }
16814:     }
16815:     return;
16816: }
16817: 
16818: // ]]>
16819: </script>
16820: 
16821: END
16822: 
16823: }
16824: 
16825: sub captcha_phrases {
16826:     return &Apache::lonlocal::texthash (
16827:                  priv => 'Private key',
16828:                  pub  => 'Public key',
16829:                  original  => 'original (CAPTCHA)',
16830:                  recaptcha => 'successor (ReCAPTCHA)',
16831:                  notused   => 'unused',
16832:                  ver => 'ReCAPTCHA version (1 or 2)',
16833:     );
16834: }
16835: 
16836: sub devalidate_remote_domconfs {
16837:     my ($dom,$cachekeys) = @_;
16838:     return unless (ref($cachekeys) eq 'HASH');
16839:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
16840:     my %thismachine;
16841:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
16842:     my @posscached = ('domainconfig','domdefaults','usersessions',
16843:                       'ltitools','directorysrch','passwdconf','cats');
16844:     if (keys(%servers)) {
16845:         foreach my $server (keys(%servers)) {
16846:             next if ($thismachine{$server});
16847:             my @cached;
16848:             foreach my $name (@posscached) {
16849:                 if ($cachekeys->{$name}) {
16850:                     push(@cached,&escape($name).':'.&escape($dom));
16851:                 }
16852:             }
16853:             if (@cached) {
16854:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
16855:             }
16856:         }
16857:     }
16858:     return;
16859: }
16860: 
16861: 1;

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